Skip to content

Commit 13d3a03

Browse files
Fix potential bug with non-ansi path to the game folder
1 parent 6da190d commit 13d3a03

File tree

5 files changed

+52
-19
lines changed

5 files changed

+52
-19
lines changed

src/GUI/SetUpWindowsWrapper.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -125,7 +125,7 @@ void SetUpWindowsWrapper::LoadFromTheGameWindow_AcceptConfiguration()
125125
// TODO: Make it load vanila Generals
126126
// Also as work with non-ascii paths
127127
// Also as search in big-archives (see more at GZH source code)
128-
QString gamePath = QString::fromStdString(Registry::GetPathToGame(Registry::Games::GeneralsZeroHour));
128+
QString gamePath = QString::fromStdWString(Registry::GetPathToGame(Registry::Games::GeneralsZeroHour));
129129
QString pathDataEngGenCsf = gamePath + "Data\\English\\generals.csf";
130130
QString pathEngBig = gamePath + "\\EnglishZH.big";
131131

src/Logger.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,7 @@ using namespace std;
9898
Log() << "C&C: " << Registry::ToString(game) << " not installed" << endl;
9999
else
100100
Log() << "C&C: " << Registry::ToString(game) << " installed at ["
101-
<< Registry::GetPathToGame(game) << ']' << endl;
101+
<< ToQString(Registry::GetPathToGame(game)).toStdString() << ']' << endl;
102102
}
103103

104104
LogFile << endl;

src/Registry.cpp

Lines changed: 35 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
#include <windows.h>
44
#include <tchar.h>
55

6+
#include "StringExt.hpp"
67
#include "Registry.hpp"
78

89
using namespace std;
@@ -22,7 +23,7 @@ Registry::WindowsBit Registry::GetWindowsBit()
2223
return windowsBit;
2324
}
2425

25-
string Registry::GetTextFromKey(const Registry::RootFolder Folder, const char* pPathToFolder, const char* pKeyName)
26+
string Registry::GetTextFromKeyA(const Registry::RootFolder Folder, const char* pPathToFolder, const char* pKeyName)
2627
{
2728
HKEY rKey;
2829
DWORD Size = 256;
@@ -48,11 +49,37 @@ string Registry::GetTextFromKey(const Registry::RootFolder Folder, const char* p
4849
return returnValue;
4950
}
5051

51-
string Registry::GetPathToGame(const Games game)
52+
wstring Registry::GetTextFromKeyW(const Registry::RootFolder Folder, const wchar_t* pPathToFolder, const wchar_t* pKeyName)
5253
{
53-
string Key = "InstallPath";
54-
string Path = Registry::PATHS_TO_GAMES.find(game)->second.find(GetWindowsBit())->second;
55-
return GetTextFromKey(Registry::RootFolder::HKLM, Path.c_str(), Key.c_str());
54+
HKEY rKey;
55+
DWORD Size = 256;
56+
WCHAR Reget[Size] = { 0 };
57+
58+
switch (Folder)
59+
{
60+
case Registry::RootFolder::HKCU :
61+
RegOpenKeyExW(HKEY_CURRENT_USER, pPathToFolder, 0, KEY_READ, &rKey);
62+
break;
63+
64+
case Registry::RootFolder::HKLM :
65+
RegOpenKeyExW(HKEY_LOCAL_MACHINE, pPathToFolder, 0, KEY_READ, &rKey);
66+
break;
67+
}
68+
69+
RegQueryValueExW(rKey, pKeyName, NULL, NULL, (LPBYTE)&Reget, &Size);
70+
RegCloseKey(rKey);
71+
72+
wstring returnValue(Reget);
73+
returnValue.shrink_to_fit();
74+
75+
return returnValue;
76+
}
77+
78+
wstring Registry::GetPathToGame(const Games game)
79+
{
80+
wstring Key = L"InstallPath";
81+
wstring Path = Registry::PATHS_TO_GAMES.find(game)->second.find(GetWindowsBit())->second;
82+
return GetTextFromKeyW(Registry::RootFolder::HKLM, Path.c_str(), Key.c_str());
5683
}
5784

5885
string Registry::ToString(Games game)
@@ -82,21 +109,21 @@ bool Registry::IsWindow32bit() { return GetWindowsBit() == WindowsBit::Win32; }
82109
{
83110
const char Path[] = {"Control Panel\\International"};
84111
const char Key[] = {"LocaleName"};
85-
return GetTextFromKey(Registry::RootFolder::HKCU, &Path[0], &Key[0]).substr(0, 2);
112+
return GetTextFromKeyA(Registry::RootFolder::HKCU, &Path[0], &Key[0]).substr(0, 2);
86113
}
87114

88115
string Registry::GetWindowsVersion()
89116
{
90117
const char Path[] = {"SOFTWARE\\Microsoft\\Windows NT\\CurrentVersion"};
91118
const char Key[] = {"ProductName"};
92-
return GetTextFromKey(Registry::RootFolder::HKLM, &Path[0], &Key[0]);
119+
return GetTextFromKeyA(Registry::RootFolder::HKLM, &Path[0], &Key[0]);
93120
}
94121

95122
string Registry::GetProcessorInfo()
96123
{
97124
const char Path[] = {"HARDWARE\\DESCRIPTION\\System\\CentralProcessor\\0"};
98125
const char Value[] = {"ProcessorNameString"};
99-
return GetTextFromKey(Registry::RootFolder::HKLM, &Path[0], &Value[0]);
126+
return GetTextFromKeyA(Registry::RootFolder::HKLM, &Path[0], &Value[0]);
100127
}
101128

102129
string Registry::GetMemoryInfo()

src/Registry.hpp

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22
#include <string>
33
#include <map>
44
#include <QString>
5-
#include "StringExt.hpp"
65

76
class Registry final
87
{
@@ -25,12 +24,12 @@ class Registry final
2524
HKCU
2625
};
2726

28-
inline static const std::map<Games, std::map<WindowsBit, std::string>> PATHS_TO_GAMES =
27+
inline static const std::map<Games, std::map<WindowsBit, std::wstring>> PATHS_TO_GAMES =
2928
{
30-
{Games::Generals, {{WindowsBit::Win32, "SOFTWARE\\Electronic Arts\\EA Games\\Generals"},
31-
{WindowsBit::Win64, "SOFTWARE\\WOW6432Node\\Electronic Arts\\EA Games\\Generals"}}},
32-
{Games::GeneralsZeroHour, {{WindowsBit::Win32, "SOFTWARE\\Electronic Arts\\EA Games\\Command and Conquer Generals Zero Hour"},
33-
{WindowsBit::Win64, "SOFTWARE\\WOW6432Node\\Electronic Arts\\EA Games\\Command and Conquer Generals Zero Hour"}}},
29+
{Games::Generals, {{WindowsBit::Win32, L"SOFTWARE\\Electronic Arts\\EA Games\\Generals"},
30+
{WindowsBit::Win64, L"SOFTWARE\\WOW6432Node\\Electronic Arts\\EA Games\\Generals"}}},
31+
{Games::GeneralsZeroHour, {{WindowsBit::Win32, L"SOFTWARE\\Electronic Arts\\EA Games\\Command and Conquer Generals Zero Hour"},
32+
{WindowsBit::Win64, L"SOFTWARE\\WOW6432Node\\Electronic Arts\\EA Games\\Command and Conquer Generals Zero Hour"}}},
3433
};
3534

3635
public: // Methods
@@ -41,18 +40,20 @@ class Registry final
4140
/// @brief Returns equal string for enum class value.
4241
static QString ToQString(Games game);
4342
/// @brief Returns actual Windows bit like a enum value.
44-
static WindowsBit GetWindowsBit();
43+
static WindowsBit GetWindowsBit();
4544

4645
/// @brief Returns REG_SZ string value from MS Windows registry.
47-
static std::string GetTextFromKey(RootFolder Folder, const char* pPathToFolder, const char* pKeyName);
46+
static std::string GetTextFromKeyA(RootFolder Folder, const char* pPathToFolder, const char* pKeyName);
47+
/// @brief Returns REG_SZ string value from MS Windows registry.
48+
static std::wstring GetTextFromKeyW(RootFolder Folder, const wchar_t* pPathToFolder, const wchar_t* pKeyName);
4849

4950
/// @brief Checks if Windows is 64-bit.
5051
static bool IsWindow64bit();
5152
/// @brief Checks if Windows is 32-bit.
5253
static bool IsWindow32bit();
5354

5455
/// @brief Sets paths to all games (C&C: Generals and C&C: Generals - Zero Hour).
55-
static std::string GetPathToGame(Games game);
56+
static std::wstring GetPathToGame(Games game);
5657
/// @brief Returns current user language from HKCU\\Control Panel\\International\\Geo\\Name (example: EN).
5758
static std::string GetCurrentUserLanguage();
5859
/// @brief Returns Windows version from HKLM\\SOFTWARE\\Microsoft\\Windows NT\\CurrentVersion\\ProductName.

src/StringExt.hpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,11 @@
66
#define L10N(x) StringExt::l10n(x)
77
#define nameof(x) QString(#x)
88

9+
inline QString ToQString(std::string str) { return QString::fromStdString(str); }
10+
inline QString ToQString(std::wstring str) { return QString::fromStdWString(str); }
11+
inline QString ToQString(const char* ch) { return QString(ch); }
12+
inline QString ToQString(const wchar_t* ch) { return ToQString(std::wstring(ch)); }
13+
914
template<class T>
1015
concept IsSymbol = std::same_as<T, char> || std::same_as<T, wchar_t> || std::same_as<T, QChar>;
1116

0 commit comments

Comments
 (0)