Skip to content

Commit ce1f957

Browse files
committed
refactor(CMake): Simplify registry queries with reusable macro
1 parent 85abc83 commit ce1f957

File tree

3 files changed

+54
-71
lines changed

3 files changed

+54
-71
lines changed

Generals/CMakeLists.txt

Lines changed: 19 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -15,44 +15,28 @@ add_subdirectory(Code)
1515

1616
# If we are building on windows for windows, try and get the game install path from the registry.
1717
if("${CMAKE_HOST_SYSTEM}" MATCHES "Windows" AND "${CMAKE_SYSTEM}" MATCHES "Windows")
18-
# Check the EA App/CD registry path (checks both 32-bit and 64-bit registry views)
19-
if(NOT RTS_INSTALL_PREFIX_GENERALS)
20-
cmake_host_system_information(RESULT _generals_path
21-
QUERY WINDOWS_REGISTRY
22-
"HKEY_LOCAL_MACHINE/SOFTWARE/Electronic Arts/EA Games/Generals"
23-
VALUE InstallPath
24-
VIEW 32_64
25-
ERROR_VARIABLE _reg_error)
26-
if(NOT _reg_error AND _generals_path)
27-
set(RTS_INSTALL_PREFIX_GENERALS "${_generals_path}" CACHE PATH "Generals install path from registry")
28-
endif()
29-
endif()
18+
include(${CMAKE_SOURCE_DIR}/cmake/registry.cmake)
19+
20+
# Check the EA App/CD registry path
21+
fetch_registry_value(
22+
"HKEY_LOCAL_MACHINE/SOFTWARE/Electronic Arts/EA Games/Generals"
23+
"InstallPath"
24+
RTS_INSTALL_PREFIX_GENERALS
25+
"Generals install path from registry")
3026

3127
# Check the "First Decade" registry path
32-
if(NOT RTS_INSTALL_PREFIX_GENERALS)
33-
cmake_host_system_information(RESULT _generals_path
34-
QUERY WINDOWS_REGISTRY
35-
"HKEY_LOCAL_MACHINE/SOFTWARE/Electronic Arts/EA Games/Command and Conquer The First Decade"
36-
VALUE gr_folder
37-
VIEW 32_64
38-
ERROR_VARIABLE _reg_error)
39-
if(NOT _reg_error AND _generals_path)
40-
set(RTS_INSTALL_PREFIX_GENERALS "${_generals_path}" CACHE PATH "Generals install path from registry")
41-
endif()
42-
endif()
28+
fetch_registry_value(
29+
"HKEY_LOCAL_MACHINE/SOFTWARE/Electronic Arts/EA Games/Command and Conquer The First Decade"
30+
"gr_folder"
31+
RTS_INSTALL_PREFIX_GENERALS
32+
"Generals install path from registry")
4333

44-
# Check alternate capitalization (installPath vs InstallPath)
45-
if(NOT RTS_INSTALL_PREFIX_GENERALS)
46-
cmake_host_system_information(RESULT _generals_path
47-
QUERY WINDOWS_REGISTRY
48-
"HKEY_LOCAL_MACHINE/SOFTWARE/Electronic Arts/EA Games/Generals"
49-
VALUE installPath
50-
VIEW 32_64
51-
ERROR_VARIABLE _reg_error)
52-
if(NOT _reg_error AND _generals_path)
53-
set(RTS_INSTALL_PREFIX_GENERALS "${_generals_path}" CACHE PATH "Generals install path from registry")
54-
endif()
55-
endif()
34+
# Check the Steam registry path
35+
fetch_registry_value(
36+
"HKEY_LOCAL_MACHINE/SOFTWARE/Electronic Arts/EA Games/Generals"
37+
"installPath"
38+
RTS_INSTALL_PREFIX_GENERALS
39+
"Generals install path from registry")
5640
endif()
5741

5842
if(RTS_INSTALL_PREFIX_GENERALS)

GeneralsMD/CMakeLists.txt

Lines changed: 20 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -15,44 +15,28 @@ add_subdirectory(Code)
1515

1616
# If we are building on windows for windows, try and get the game install path from the registry.
1717
if("${CMAKE_HOST_SYSTEM}" MATCHES "Windows" AND "${CMAKE_SYSTEM}" MATCHES "Windows")
18-
# Check the EA App/CD registry path (checks both 32-bit and 64-bit registry views)
19-
if(NOT RTS_INSTALL_PREFIX_ZEROHOUR)
20-
cmake_host_system_information(RESULT _zerohour_path
21-
QUERY WINDOWS_REGISTRY
22-
"HKEY_LOCAL_MACHINE/SOFTWARE/Electronic Arts/EA Games/Command and Conquer Generals Zero Hour"
23-
VALUE InstallPath
24-
VIEW 32_64
25-
ERROR_VARIABLE _reg_error)
26-
if(NOT _reg_error AND _zerohour_path)
27-
set(RTS_INSTALL_PREFIX_ZEROHOUR "${_zerohour_path}" CACHE PATH "Zero Hour install path from registry")
28-
endif()
29-
endif()
18+
include(${CMAKE_SOURCE_DIR}/cmake/registry.cmake)
3019

31-
# Check the "First Decade" registry path
32-
if(NOT RTS_INSTALL_PREFIX_ZEROHOUR)
33-
cmake_host_system_information(RESULT _zerohour_path
34-
QUERY WINDOWS_REGISTRY
35-
"HKEY_LOCAL_MACHINE/SOFTWARE/Electronic Arts/EA Games/Command and Conquer The First Decade"
36-
VALUE zh_folder
37-
VIEW 32_64
38-
ERROR_VARIABLE _reg_error)
39-
if(NOT _reg_error AND _zerohour_path)
40-
set(RTS_INSTALL_PREFIX_ZEROHOUR "${_zerohour_path}" CACHE PATH "Zero Hour install path from registry")
41-
endif()
42-
endif()
20+
# Check the EA App/CD registry path
21+
fetch_registry_value(
22+
"HKEY_LOCAL_MACHINE/SOFTWARE/Electronic Arts/EA Games/Command and Conquer Generals Zero Hour"
23+
"InstallPath"
24+
RTS_INSTALL_PREFIX_ZEROHOUR
25+
"Zero Hour install path from registry")
4326

44-
# Check alternate registry location (ZeroHour vs full name)
45-
if(NOT RTS_INSTALL_PREFIX_ZEROHOUR)
46-
cmake_host_system_information(RESULT _zerohour_path
47-
QUERY WINDOWS_REGISTRY
48-
"HKEY_LOCAL_MACHINE/SOFTWARE/Electronic Arts/EA Games/ZeroHour"
49-
VALUE installPath
50-
VIEW 32_64
51-
ERROR_VARIABLE _reg_error)
52-
if(NOT _reg_error AND _zerohour_path)
53-
set(RTS_INSTALL_PREFIX_ZEROHOUR "${_zerohour_path}" CACHE PATH "Zero Hour install path from registry")
54-
endif()
55-
endif()
27+
# Check the "First Decade" registry path
28+
fetch_registry_value(
29+
"HKEY_LOCAL_MACHINE/SOFTWARE/Electronic Arts/EA Games/Command and Conquer The First Decade"
30+
"zh_folder"
31+
RTS_INSTALL_PREFIX_ZEROHOUR
32+
"Zero Hour install path from registry")
33+
34+
# Check the Steam registry path
35+
fetch_registry_value(
36+
"HKEY_LOCAL_MACHINE/SOFTWARE/Electronic Arts/EA Games/ZeroHour"
37+
"installPath"
38+
RTS_INSTALL_PREFIX_ZEROHOUR
39+
"Zero Hour install path from registry")
5640
endif()
5741

5842
if(RTS_INSTALL_PREFIX_ZEROHOUR)

cmake/registry.cmake

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
# Helper macro for querying Windows registry values
2+
# Queries both 32-bit and 64-bit registry views automatically
3+
macro(fetch_registry_value registry_key registry_value output_var description)
4+
if(NOT ${output_var})
5+
cmake_host_system_information(RESULT _variable
6+
QUERY WINDOWS_REGISTRY
7+
"${registry_key}"
8+
VALUE "${registry_value}"
9+
VIEW 32_64)
10+
if(_variable)
11+
set(${output_var} "${_variable}" CACHE PATH "${description}")
12+
endif()
13+
endif()
14+
endmacro()
15+

0 commit comments

Comments
 (0)