@@ -1036,32 +1036,10 @@ GlobalData::GlobalData()
10361036
10371037 m_keyboardCameraRotateSpeed = 0 .1f ;
10381038
1039- // Set user data directory based on registry settings instead of INI parameters. This allows us to
1040- // localize the leaf name.
1041- char temp[_MAX_PATH + 1 ];
1042- if (::SHGetSpecialFolderPath (nullptr , temp, CSIDL_PERSONAL , true ))
1043- {
1044- AsciiString myDocumentsDirectory = temp;
1045-
1046- if (myDocumentsDirectory.getCharAt (myDocumentsDirectory.getLength () -1 ) != ' \\ ' )
1047- myDocumentsDirectory.concat ( ' \\ ' );
1048-
1049- AsciiString leafName;
1050-
1051- if ( !GetStringFromRegistry ( " " , " UserDataLeafName" , leafName ) )
1052- {
1053- // Use something, anything
1054- // [MH] had to remove this, otherwise mapcache build step won't run... DEBUG_CRASH( ( "Could not find registry key UserDataLeafName; defaulting to \"Command and Conquer Generals Zero Hour Data\" " ) );
1055- leafName = " Command and Conquer Generals Zero Hour Data" ;
1056- }
1057-
1058- myDocumentsDirectory.concat ( leafName );
1059- if (myDocumentsDirectory.getCharAt ( myDocumentsDirectory.getLength () - 1 ) != ' \\ ' )
1060- myDocumentsDirectory.concat ( ' \\ ' );
1061-
1062- CreateDirectory (myDocumentsDirectory.str (), nullptr );
1063- m_userDataDir = myDocumentsDirectory;
1064- }
1039+ // Set user data directory based on registry settings instead of INI parameters.
1040+ // This allows us to localize the leaf name.
1041+ m_userDataDir = BuildUserDataPathFromRegistry ();
1042+ CreateDirectory (m_userDataDir.str (), nullptr );
10651043
10661044 // -allAdvice feature
10671045 // m_allAdvice = FALSE;
@@ -1333,3 +1311,61 @@ UnsignedInt GlobalData::generateExeCRC()
13331311
13341312 return exeCRC.get ();
13351313}
1314+
1315+ AsciiString GlobalData::BuildUserDataPathFromRegistry ()
1316+ {
1317+ #if defined(_MSC_VER) && (_MSC_VER < 1300)
1318+ // VC6 lacks FOLDERID_Documents and KF_FLAG_DEFAULT
1319+ const GUID FOLDERID_Documents = { 0xFDD39AD0 , 0x238F , 0x46AF , 0xAD , 0xB4 , 0x6C , 0x85 , 0x48 , 0x03 , 0x69 , 0xC7 };
1320+ const DWORD KF_FLAG_DEFAULT = 0 ;
1321+ #endif
1322+
1323+ typedef HRESULT (WINAPI * PFN_SHGetKnownFolderPath)(const GUID & rfid, DWORD dwFlags, HANDLE hToken, PWSTR * ppszPath);
1324+
1325+ AsciiString myDocumentsDirectory;
1326+ HMODULE shell32module = GetModuleHandleA (" shell32.dll" );
1327+ PFN_SHGetKnownFolderPath pSHGetKnownFolderPath = nullptr ;
1328+
1329+ // TheSuperHackers @bugfix Mauller 20/03/2026 Fix the handling of folder redirection
1330+ // OneDrive and Group Policy folder redirection is better supported by SHGetKnownFolderPath()
1331+ // SHGetKnownFolderPath() is only supported in windows Vista onwards so we check for it being available
1332+ if (shell32module) {
1333+ pSHGetKnownFolderPath = (PFN_SHGetKnownFolderPath)GetProcAddress (shell32module, " SHGetKnownFolderPath" );
1334+ }
1335+
1336+ if (pSHGetKnownFolderPath) {
1337+ PWSTR pszPath = nullptr ;
1338+ HRESULT hr = pSHGetKnownFolderPath (FOLDERID_Documents, KF_FLAG_DEFAULT , nullptr , &pszPath);
1339+
1340+ if (SUCCEEDED (hr) && pszPath) {
1341+ myDocumentsDirectory.translate (pszPath);
1342+ CoTaskMemFree (pszPath);
1343+ }
1344+ }
1345+ else {
1346+ char temp[_MAX_PATH + 1 ];
1347+ if (SHGetSpecialFolderPath (nullptr , temp, CSIDL_PERSONAL , true )) {
1348+ myDocumentsDirectory = temp;
1349+ }
1350+ }
1351+
1352+ if (!myDocumentsDirectory.isEmpty ()) {
1353+ // Now build the full path string
1354+ if (!myDocumentsDirectory.endsWith (" \\ " ))
1355+ myDocumentsDirectory.concat (' \\ ' );
1356+
1357+ AsciiString leafName;
1358+ if (!GetStringFromRegistry (" " , " UserDataLeafName" , leafName))
1359+ {
1360+ // Use something, anything
1361+ // [MH] had to remove this, otherwise mapcache build step won't run... DEBUG_CRASH( ( "Could not find registry key UserDataLeafName; defaulting to \"Command and Conquer Generals Zero Hour Data\" " ) );
1362+ leafName = " Command and Conquer Generals Zero Hour Data" ;
1363+ }
1364+
1365+ myDocumentsDirectory.concat (leafName);
1366+ if (!myDocumentsDirectory.endsWith (" \\ " ))
1367+ myDocumentsDirectory.concat (' \\ ' );
1368+ }
1369+
1370+ return myDocumentsDirectory;
1371+ }
0 commit comments