Skip to content

Commit accf632

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

File tree

3 files changed

+48
-71
lines changed

3 files changed

+48
-71
lines changed

Generals/CMakeLists.txt

Lines changed: 16 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -15,44 +15,25 @@ 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+
try_set_install_prefix_from_registry(RTS_INSTALL_PREFIX_GENERALS
22+
"HKEY_LOCAL_MACHINE/SOFTWARE/Electronic Arts/EA Games/Generals"
23+
InstallPath
24+
"Generals install path from registry")
3025

3126
# 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()
27+
try_set_install_prefix_from_registry(RTS_INSTALL_PREFIX_GENERALS
28+
"HKEY_LOCAL_MACHINE/SOFTWARE/Electronic Arts/EA Games/Command and Conquer The First Decade"
29+
gr_folder
30+
"Generals install path from registry")
4331

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()
32+
# Check the Steam registry path
33+
try_set_install_prefix_from_registry(RTS_INSTALL_PREFIX_GENERALS
34+
"HKEY_LOCAL_MACHINE/SOFTWARE/Electronic Arts/EA Games/Generals"
35+
installPath
36+
"Generals install path from registry")
5637
endif()
5738

5839
if(RTS_INSTALL_PREFIX_GENERALS)

GeneralsMD/CMakeLists.txt

Lines changed: 17 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -15,44 +15,25 @@ 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+
try_set_install_prefix_from_registry(RTS_INSTALL_PREFIX_ZEROHOUR
22+
"HKEY_LOCAL_MACHINE/SOFTWARE/Electronic Arts/EA Games/Command and Conquer Generals Zero Hour"
23+
InstallPath
24+
"Zero Hour install path from registry")
4325

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()
26+
# Check the "First Decade" registry path
27+
try_set_install_prefix_from_registry(RTS_INSTALL_PREFIX_ZEROHOUR
28+
"HKEY_LOCAL_MACHINE/SOFTWARE/Electronic Arts/EA Games/Command and Conquer The First Decade"
29+
zh_folder
30+
"Zero Hour install path from registry")
31+
32+
# Check the Steam registry path
33+
try_set_install_prefix_from_registry(RTS_INSTALL_PREFIX_ZEROHOUR
34+
"HKEY_LOCAL_MACHINE/SOFTWARE/Electronic Arts/EA Games/ZeroHour"
35+
installPath
36+
"Zero Hour install path from registry")
5637
endif()
5738

5839
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 paths
2+
# Queries both 32-bit and 64-bit registry views automatically
3+
macro(try_set_install_prefix_from_registry output_var registry_key registry_value description)
4+
if(NOT ${output_var})
5+
cmake_host_system_information(RESULT _temp_path
6+
QUERY WINDOWS_REGISTRY
7+
"${registry_key}"
8+
VALUE ${registry_value}
9+
VIEW 32_64)
10+
if(_temp_path)
11+
set(${output_var} "${_temp_path}" CACHE PATH "${description}")
12+
endif()
13+
endif()
14+
endmacro()
15+

0 commit comments

Comments
 (0)