Skip to content

Commit 0378670

Browse files
committed
bugfix(globaldata): Fix the handling of documents folder redirection by using SHGetKnownFolderPath() - Vista+ required
1 parent c8c809c commit 0378670

2 files changed

Lines changed: 56 additions & 0 deletions

File tree

Generals/Code/GameEngine/Source/Common/GlobalData.cpp

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1173,6 +1173,7 @@ void GlobalData::parseGameDataDefinition( INI* ini )
11731173

11741174
TheWritableGlobalData->m_userDataDir.clear();
11751175

1176+
#if defined(_MSC_VER) && _MSC_VER < 1300
11761177
char temp[_MAX_PATH];
11771178
if (::SHGetSpecialFolderPath(nullptr, temp, CSIDL_PERSONAL, true))
11781179
{
@@ -1183,6 +1184,29 @@ void GlobalData::parseGameDataDefinition( INI* ini )
11831184
CreateDirectory(temp, nullptr);
11841185
TheWritableGlobalData->m_userDataDir = temp;
11851186
}
1187+
#else
1188+
// TheSuperHackers @bugfix Mauller 20/03/2026 Fix the handling of folder redirection
1189+
// OneDrive and Group Policy folder redirection is better supported by SHGetKnownFolderPath()
1190+
PWSTR pszPath = nullptr;
1191+
HRESULT hr = SHGetKnownFolderPath(FOLDERID_Documents, KF_FLAG_DEFAULT, nullptr, &pszPath);
1192+
if (SUCCEEDED(hr) && pszPath)
1193+
{
1194+
AsciiString myDocumentsDirectory;
1195+
myDocumentsDirectory.translate(UnicodeString(pszPath));
1196+
1197+
if (myDocumentsDirectory.getCharAt(myDocumentsDirectory.getLength() -1) != '\\')
1198+
myDocumentsDirectory.concat( '\\' );
1199+
1200+
myDocumentsDirectory.concat(TheWritableGlobalData->m_userDataLeafName.str());
1201+
1202+
if (myDocumentsDirectory.getCharAt( myDocumentsDirectory.getLength() - 1) != '\\')
1203+
myDocumentsDirectory.concat( '\\' );
1204+
1205+
CreateDirectory(myDocumentsDirectory.str(), nullptr);
1206+
TheWritableGlobalData->m_userDataDir = myDocumentsDirectory;
1207+
}
1208+
CoTaskMemFree(pszPath);
1209+
#endif
11861210

11871211
// override INI values with user preferences
11881212
OptionPreferences optionPref;

GeneralsMD/Code/GameEngine/Source/Common/GlobalData.cpp

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1038,6 +1038,7 @@ GlobalData::GlobalData()
10381038

10391039
// Set user data directory based on registry settings instead of INI parameters. This allows us to
10401040
// localize the leaf name.
1041+
#if defined(_MSC_VER) && _MSC_VER < 1300
10411042
char temp[_MAX_PATH + 1];
10421043
if (::SHGetSpecialFolderPath(nullptr, temp, CSIDL_PERSONAL, true))
10431044
{
@@ -1062,6 +1063,37 @@ GlobalData::GlobalData()
10621063
CreateDirectory(myDocumentsDirectory.str(), nullptr);
10631064
m_userDataDir = myDocumentsDirectory;
10641065
}
1066+
#else
1067+
// TheSuperHackers @bugfix Mauller 20/03/2026 Fix the handling of folder redirection
1068+
// OneDrive and Group Policy folder redirection is better supported by SHGetKnownFolderPath()
1069+
PWSTR pszPath = nullptr;
1070+
HRESULT hr = SHGetKnownFolderPath(FOLDERID_Documents, KF_FLAG_DEFAULT, nullptr, &pszPath);
1071+
if (SUCCEEDED(hr) && pszPath)
1072+
{
1073+
AsciiString myDocumentsDirectory;
1074+
myDocumentsDirectory.translate(UnicodeString(pszPath));
1075+
1076+
if (myDocumentsDirectory.getCharAt(myDocumentsDirectory.getLength() -1) != '\\')
1077+
myDocumentsDirectory.concat( '\\' );
1078+
1079+
AsciiString leafName;
1080+
1081+
if ( !GetStringFromRegistry( "", "UserDataLeafName", leafName ) )
1082+
{
1083+
// Use something, anything
1084+
// [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\" " ) );
1085+
leafName = "Command and Conquer Generals Zero Hour Data";
1086+
}
1087+
1088+
myDocumentsDirectory.concat( leafName );
1089+
if (myDocumentsDirectory.getCharAt( myDocumentsDirectory.getLength() - 1) != '\\')
1090+
myDocumentsDirectory.concat( '\\' );
1091+
1092+
CreateDirectory(myDocumentsDirectory.str(), nullptr);
1093+
m_userDataDir = myDocumentsDirectory;
1094+
}
1095+
CoTaskMemFree(pszPath);
1096+
#endif
10651097

10661098
//-allAdvice feature
10671099
//m_allAdvice = FALSE;

0 commit comments

Comments
 (0)