@@ -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,57 @@ UnsignedInt GlobalData::generateExeCRC()
13331311
13341312 return exeCRC.get ();
13351313}
1314+
1315+ AsciiString GlobalData::BuildUserDataPathFromRegistry ()
1316+ {
1317+ // VC6 lacks FOLDERID_Documents and KF_FLAG_DEFAULT
1318+ const GUID FOLDERID_Documents = { 0xFDD39AD0 , 0x238F , 0x46AF , 0xAD , 0xB4 , 0x6C , 0x85 , 0x48 , 0x03 , 0x69 , 0xC7 };
1319+ const DWORD KF_FLAG_DEFAULT = 0 ;
1320+
1321+ typedef HRESULT (WINAPI* PFN_SHGetKnownFolderPath)(const GUID& rfid, DWORD dwFlags, HANDLE hToken, PWSTR* ppszPath);
1322+
1323+ AsciiString myDocumentsDirectory;
1324+ HMODULE shell32module = GetModuleHandleA (" shell32.dll" );
1325+
1326+ // TheSuperHackers @bugfix Mauller 20/03/2026 Fix the handling of folder redirection
1327+ // OneDrive and Group Policy folder redirection is better supported by SHGetKnownFolderPath()
1328+ // SHGetKnownFolderPath() is only supported in windows Vista onwards so we check for it being available
1329+ if (shell32module && GetProcAddress (shell32module, " SHGetKnownFolderPath" )) {
1330+ PFN_SHGetKnownFolderPath pSHGetKnownFolderPath = (PFN_SHGetKnownFolderPath)GetProcAddress (shell32module, " SHGetKnownFolderPath" );
1331+
1332+ PWSTR pszPath = nullptr ;
1333+ HRESULT hr = pSHGetKnownFolderPath (FOLDERID_Documents, KF_FLAG_DEFAULT, nullptr , &pszPath);
1334+
1335+ if (SUCCEEDED (hr) && pszPath) {
1336+ myDocumentsDirectory.translate (UnicodeString (pszPath));
1337+ CoTaskMemFree (pszPath);
1338+ }
1339+ }
1340+ else {
1341+ char temp[_MAX_PATH + 1 ];
1342+ if (SHGetSpecialFolderPath (nullptr , temp, CSIDL_PERSONAL, true )) {
1343+ myDocumentsDirectory = temp;
1344+ }
1345+ }
1346+
1347+ if (myDocumentsDirectory.isEmpty ())
1348+ return AsciiString::TheEmptyString;
1349+
1350+ // Now build the full path string
1351+ if (!myDocumentsDirectory.endsWith (" \\ " ))
1352+ myDocumentsDirectory.concat (' \\ ' );
1353+
1354+ AsciiString leafName;
1355+ if (!GetStringFromRegistry (" " , " UserDataLeafName" , leafName))
1356+ {
1357+ // Use something, anything
1358+ // [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\" " ) );
1359+ leafName = " Command and Conquer Generals Zero Hour Data" ;
1360+ }
1361+
1362+ myDocumentsDirectory.concat (leafName);
1363+ if (!myDocumentsDirectory.endsWith (" \\ " ))
1364+ myDocumentsDirectory.concat (' \\ ' );
1365+
1366+ return myDocumentsDirectory;
1367+ }
0 commit comments