Skip to content

Commit d69a3a8

Browse files
Add Registry class to the Windows namespace
1 parent 2ee788b commit d69a3a8

File tree

6 files changed

+199
-192
lines changed

6 files changed

+199
-192
lines changed

src/GUI/LoadFromTheGameWindow.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,14 +35,14 @@ LoadFromTheGameWindow::LoadFromTheGameWindow(QWidget* parent) : QWidget(parent)
3535
connect(btnCancel, &QPushButton::clicked, this, [=, this] { emit btnBackClicked(); });
3636

3737
// configure game buttons
38-
QRadioButton* rdxGenerals = new QRadioButton(Registry::ToQString(Registry::Games::Generals));
38+
QRadioButton* rdxGenerals = new QRadioButton(Windows::Registry::ToQString(Windows::Registry::Games::Generals));
3939
rdxGenerals->setDisabled(true);
4040
rdxGenerals->setObjectName(nameof(rdxGenerals));
4141
QFont rbxGeneralsFont = rdxGenerals->font();
4242
rbxGeneralsFont.setStrikeOut(true);
4343
rdxGenerals->setFont(rbxGeneralsFont);
4444

45-
QRadioButton* rdxZeroHour = new QRadioButton(Registry::ToQString(Registry::Games::GeneralsZeroHour));
45+
QRadioButton* rdxZeroHour = new QRadioButton(Windows::Registry::ToQString(Windows::Registry::Games::GeneralsZeroHour));
4646
rdxZeroHour->setChecked(true);
4747
rdxGenerals->setObjectName(nameof(rdxGenerals));
4848

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::fromStdWString(Registry::GetPathToGame(Registry::Games::GeneralsZeroHour));
128+
QString gamePath = QString::fromStdWString(Windows::Registry::GetPathToGame(Windows::Registry::Games::GeneralsZeroHour));
129129
QString pathDataEngGenCsf = gamePath + "Data\\English\\generals.csf";
130130
QString pathEngBig = gamePath + "\\EnglishZH.big";
131131

src/Logger.cpp

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -78,27 +78,27 @@ using namespace std;
7878
void Logger::LogSystemInformation()
7979
{
8080
// Write to log all necessary information about MS Windows
81-
Log() << "Operation System Information" << endl;
81+
Log() << "Operation System Information" << endl;
8282
Log() << "Version : "
83-
<< Registry::GetWindowsVersion() << ' '
84-
<< GetWindowsBit() << endl;
85-
Log() << "Language : " << Registry::GetCurrentUserLanguage() << endl << endl;
83+
<< Windows::Registry::GetWindowsVersion() << ' '
84+
<< GetWindowsBit() << endl;
85+
Log() << "Language : " << Windows::Registry::GetCurrentUserLanguage() << endl << endl;
8686

8787
// Write to log all information about processor type and memory size
8888
Log() << "Hardware Information" << endl;
89-
Log() << "Processor : " << Registry::GetProcessorInfo() << endl;
90-
Log() << "Memory : " << Registry::GetMemoryInfo() << endl << endl;
89+
Log() << "Processor : " << Windows::Registry::GetProcessorInfo() << endl;
90+
Log() << "Memory : " << Windows::Registry::GetMemoryInfo() << endl << endl;
9191

9292
// Write to log all games paths
9393
Log() << "Software Information" << endl;
9494

95-
for (const auto& game : {Registry::Games::Generals, Registry::Games::GeneralsZeroHour})
95+
for (const auto& game : {Windows::Registry::Games::Generals, Windows::Registry::Games::GeneralsZeroHour})
9696
{
97-
if (Registry::GetPathToGame(game).empty())
98-
Log() << "C&C: " << Registry::ToString(game) << " not installed" << endl;
97+
if (Windows::Registry::GetPathToGame(game).empty())
98+
Log() << "C&C: " << Windows::Registry::ToString(game) << " not installed" << endl;
9999
else
100-
Log() << "C&C: " << Registry::ToString(game) << " installed at ["
101-
<< ToQString(Registry::GetPathToGame(game)).toStdString() << ']' << endl;
100+
Log() << "C&C: " << Windows::Registry::ToString(game) << " installed at ["
101+
<< ToQString(Windows::Registry::GetPathToGame(game)).toStdString() << ']' << endl;
102102
}
103103

104104
LogFile << endl;
@@ -167,7 +167,7 @@ using namespace std;
167167
#pragma region Support methods
168168
const string Logger::GetWindowsBit() const
169169
{
170-
if (Registry::GetWindowsBit() == Registry::WindowsBit::Win32)
170+
if (Windows::Registry::GetWindowsBit() == Windows::Registry::WindowsBit::Win32)
171171
return "32-bit";
172172
else
173173
return "64-bit";

src/Settings.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ void Settings::Parse()
3535
ForceSystemLanguage = json.Query("$." + nameof(ForceSystemLanguage)).toBool();
3636

3737
if (ForceSystemLanguage)
38-
Language = Convert::ToLangEnum(Registry::GetCurrentUserLanguage());
38+
Language = Convert::ToLangEnum(Windows::Registry::GetCurrentUserLanguage());
3939
else
4040
Language = Convert::ToLangEnum(json.Query("$." + nameof(Language)).toString());
4141

src/Windows/Registry.cpp

Lines changed: 125 additions & 122 deletions
Original file line numberDiff line numberDiff line change
@@ -8,147 +8,150 @@
88

99
using namespace std;
1010

11-
Registry::WindowsBit Registry::GetWindowsBit()
11+
namespace Windows
1212
{
13-
HKEY rKey;
14-
WindowsBit windowsBit;
15-
16-
if (RegOpenKeyExA(HKEY_LOCAL_MACHINE, _T("SOFTWARE\\WOW6432Node"), 0, KEY_QUERY_VALUE, &rKey) == ERROR_SUCCESS)
17-
windowsBit = WindowsBit::Win64;
18-
else
19-
windowsBit = WindowsBit::Win32;
20-
21-
RegCloseKey(rKey);
22-
23-
return windowsBit;
24-
}
25-
26-
string Registry::GetTextFromKeyA(const Registry::RootFolder Folder, const char* pPathToFolder, const char* pKeyName)
27-
{
28-
HKEY rKey;
29-
DWORD Size = 256;
30-
TCHAR Reget[Size] = { 0 };
31-
32-
switch (Folder)
13+
Registry::WindowsBit Registry::GetWindowsBit()
3314
{
34-
case Registry::RootFolder::HKCU :
35-
RegOpenKeyExA(HKEY_CURRENT_USER, pPathToFolder, 0, KEY_READ, &rKey);
36-
break;
37-
38-
case Registry::RootFolder::HKLM :
39-
RegOpenKeyExA(HKEY_LOCAL_MACHINE, pPathToFolder, 0, KEY_READ, &rKey);
40-
break;
41-
}
15+
HKEY rKey;
16+
WindowsBit windowsBit;
4217

43-
RegQueryValueExA(rKey, pKeyName, NULL, NULL, (LPBYTE)&Reget, &Size);
44-
RegCloseKey(rKey);
45-
46-
string returnValue(Reget);
47-
returnValue.shrink_to_fit();
48-
49-
return returnValue;
50-
}
18+
if (RegOpenKeyExA(HKEY_LOCAL_MACHINE, _T("SOFTWARE\\WOW6432Node"), 0, KEY_QUERY_VALUE, &rKey) == ERROR_SUCCESS)
19+
windowsBit = WindowsBit::Win64;
20+
else
21+
windowsBit = WindowsBit::Win32;
5122

52-
wstring Registry::GetTextFromKeyW(const Registry::RootFolder Folder, const wchar_t* pPathToFolder, const wchar_t* pKeyName)
53-
{
54-
HKEY rKey;
55-
DWORD Size = 256;
56-
WCHAR Reget[Size] = { 0 };
23+
RegCloseKey(rKey);
5724

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;
25+
return windowsBit;
6726
}
6827

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());
83-
}
84-
85-
string Registry::ToString(Games game)
86-
{
87-
string returnValue;
88-
89-
switch (game)
28+
string Registry::GetTextFromKeyA(const Registry::RootFolder Folder, const char* pPathToFolder, const char* pKeyName)
9029
{
91-
case Games::Generals:
92-
returnValue = "Generals";
93-
break;
94-
case Games::GeneralsZeroHour:
95-
returnValue = "Generals Zero Hour";
96-
break;
97-
}
98-
99-
return returnValue;
100-
}
101-
102-
QString Registry::ToQString(Games game) { return QString::fromStdString(Registry::ToString(game)); }
103-
104-
bool Registry::IsWindow64bit() { return GetWindowsBit() == WindowsBit::Win64; }
105-
bool Registry::IsWindow32bit() { return GetWindowsBit() == WindowsBit::Win32; }
106-
107-
#pragma region Logger methods
108-
string Registry::GetCurrentUserLanguage()
109-
{
110-
const char Path[] = {"Control Panel\\International"};
111-
const char Key[] = {"LocaleName"};
112-
return GetTextFromKeyA(Registry::RootFolder::HKCU, &Path[0], &Key[0]).substr(0, 2);
30+
HKEY rKey;
31+
DWORD Size = 256;
32+
TCHAR Reget[Size] = { 0 };
33+
34+
switch (Folder)
35+
{
36+
case Registry::RootFolder::HKCU :
37+
RegOpenKeyExA(HKEY_CURRENT_USER, pPathToFolder, 0, KEY_READ, &rKey);
38+
break;
39+
40+
case Registry::RootFolder::HKLM :
41+
RegOpenKeyExA(HKEY_LOCAL_MACHINE, pPathToFolder, 0, KEY_READ, &rKey);
42+
break;
43+
}
44+
45+
RegQueryValueExA(rKey, pKeyName, NULL, NULL, (LPBYTE)&Reget, &Size);
46+
RegCloseKey(rKey);
47+
48+
string returnValue(Reget);
49+
returnValue.shrink_to_fit();
50+
51+
return returnValue;
11352
}
11453

115-
string Registry::GetWindowsVersion()
54+
wstring Registry::GetTextFromKeyW(const Registry::RootFolder Folder, const wchar_t* pPathToFolder, const wchar_t* pKeyName)
11655
{
117-
const char Path[] = {"SOFTWARE\\Microsoft\\Windows NT\\CurrentVersion"};
118-
const char Key[] = {"ProductName"};
119-
return GetTextFromKeyA(Registry::RootFolder::HKLM, &Path[0], &Key[0]);
56+
HKEY rKey;
57+
DWORD Size = 256;
58+
WCHAR Reget[Size] = { 0 };
59+
60+
switch (Folder)
61+
{
62+
case Registry::RootFolder::HKCU :
63+
RegOpenKeyExW(HKEY_CURRENT_USER, pPathToFolder, 0, KEY_READ, &rKey);
64+
break;
65+
66+
case Registry::RootFolder::HKLM :
67+
RegOpenKeyExW(HKEY_LOCAL_MACHINE, pPathToFolder, 0, KEY_READ, &rKey);
68+
break;
69+
}
70+
71+
RegQueryValueExW(rKey, pKeyName, NULL, NULL, (LPBYTE)&Reget, &Size);
72+
RegCloseKey(rKey);
73+
74+
wstring returnValue(Reget);
75+
returnValue.shrink_to_fit();
76+
77+
return returnValue;
12078
}
12179

122-
string Registry::GetProcessorInfo()
80+
wstring Registry::GetPathToGame(const Games game)
12381
{
124-
const char Path[] = {"HARDWARE\\DESCRIPTION\\System\\CentralProcessor\\0"};
125-
const char Value[] = {"ProcessorNameString"};
126-
return GetTextFromKeyA(Registry::RootFolder::HKLM, &Path[0], &Value[0]);
82+
wstring Key = L"InstallPath";
83+
wstring Path = Registry::PATHS_TO_GAMES.find(game)->second.find(GetWindowsBit())->second;
84+
return GetTextFromKeyW(Registry::RootFolder::HKLM, Path.c_str(), Key.c_str());
12785
}
12886

129-
string Registry::GetMemoryInfo()
87+
string Registry::ToString(Games game)
13088
{
131-
stringstream ss;
132-
MEMORYSTATUSEX MemStat;
133-
MemStat.dwLength = sizeof (MemStat);
134-
GlobalMemoryStatusEx(&MemStat);
135-
136-
ss << (MemStat.ullTotalPhys/1024)/1024 << "MiB";
137-
return ss.str();
89+
string returnValue;
90+
91+
switch (game)
92+
{
93+
case Games::Generals:
94+
returnValue = "Generals";
95+
break;
96+
case Games::GeneralsZeroHour:
97+
returnValue = "Generals Zero Hour";
98+
break;
99+
}
100+
101+
return returnValue;
138102
}
139103

140-
string Registry::GetUUID()
141-
{
142-
// Magic code by stackoverflow: https://stackoverflow.com/questions/24365331/how-can-i-generate-uuid-in-c-without-using-boost-library
143-
stringstream ss;
104+
QString Registry::ToQString(Games game) { return QString::fromStdString(Registry::ToString(game)); }
144105

145-
UUID uuid;
146-
auto tmpUuidCreate = UuidCreate(&uuid);
147-
char* str;
148-
auto tmpUuidToStringA = UuidToStringA(&uuid, reinterpret_cast<RPC_CSTR*>(&str));
149-
ss << str;
150-
RpcStringFreeA(reinterpret_cast<RPC_CSTR*>(&str));
106+
bool Registry::IsWindow64bit() { return GetWindowsBit() == WindowsBit::Win64; }
107+
bool Registry::IsWindow32bit() { return GetWindowsBit() == WindowsBit::Win32; }
151108

152-
return ss.str();
153-
}
109+
#pragma region Logger methods
110+
string Registry::GetCurrentUserLanguage()
111+
{
112+
const char Path[] = {"Control Panel\\International"};
113+
const char Key[] = {"LocaleName"};
114+
return GetTextFromKeyA(Registry::RootFolder::HKCU, &Path[0], &Key[0]).substr(0, 2);
115+
}
116+
117+
string Registry::GetWindowsVersion()
118+
{
119+
const char Path[] = {"SOFTWARE\\Microsoft\\Windows NT\\CurrentVersion"};
120+
const char Key[] = {"ProductName"};
121+
return GetTextFromKeyA(Registry::RootFolder::HKLM, &Path[0], &Key[0]);
122+
}
123+
124+
string Registry::GetProcessorInfo()
125+
{
126+
const char Path[] = {"HARDWARE\\DESCRIPTION\\System\\CentralProcessor\\0"};
127+
const char Value[] = {"ProcessorNameString"};
128+
return GetTextFromKeyA(Registry::RootFolder::HKLM, &Path[0], &Value[0]);
129+
}
130+
131+
string Registry::GetMemoryInfo()
132+
{
133+
stringstream ss;
134+
MEMORYSTATUSEX MemStat;
135+
MemStat.dwLength = sizeof (MemStat);
136+
GlobalMemoryStatusEx(&MemStat);
137+
138+
ss << (MemStat.ullTotalPhys/1024)/1024 << "MiB";
139+
return ss.str();
140+
}
141+
142+
string Registry::GetUUID()
143+
{
144+
// Magic code by stackoverflow: https://stackoverflow.com/questions/24365331/how-can-i-generate-uuid-in-c-without-using-boost-library
145+
stringstream ss;
146+
147+
UUID uuid;
148+
auto tmpUuidCreate = UuidCreate(&uuid);
149+
char* str;
150+
auto tmpUuidToStringA = UuidToStringA(&uuid, reinterpret_cast<RPC_CSTR*>(&str));
151+
ss << str;
152+
RpcStringFreeA(reinterpret_cast<RPC_CSTR*>(&str));
153+
154+
return ss.str();
155+
}
154156
#pragma endregion
157+
}

0 commit comments

Comments
 (0)