Skip to content

Commit bd58d61

Browse files
SoulmareKebsCS
andauthored
[modernize/clarity/optimizations] suggested (#186)
* [modernize/clarity/optimizations] suggested * Revert libraries scope resolution operator * [extra] qol * [extra] staged --------- Co-authored-by: KebsCS <lukswi2000@gmail.com>
1 parent 11f5403 commit bd58d61

23 files changed

Lines changed: 1403 additions & 1245 deletions

KBotExt/Auth.cpp

Lines changed: 88 additions & 86 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ ClientInfo Auth::GetClientInfo(const DWORD& pid, bool riotClient)
1414
if (!pid)
1515
return {};
1616

17-
std::string cmdLine = Utils::WstringToString(GetProcessCommandLine(pid));
17+
const std::string cmdLine = Utils::WstringToString(GetProcessCommandLine(pid));
1818
if (cmdLine.empty())
1919
return {};
2020

@@ -27,27 +27,25 @@ ClientInfo Auth::GetClientInfo(const DWORD& pid, bool riotClient)
2727
return info;
2828
}
2929

30-
int Auth::GetPort(const std::string& cmdLine, bool riotClient)
30+
int Auth::GetPort(const std::string& cmdLine, const bool riotClient)
3131
{
3232
std::regex regexStr;
33-
regexStr = riotClient ? ("--riotclient-app-port=(\\d*)") : ("--app-port=(\\d*)");
34-
std::smatch m;
35-
if (std::regex_search(cmdLine, m, regexStr))
33+
regexStr = riotClient ? "--riotclient-app-port=(\\d*)" : "--app-port=(\\d*)";
34+
if (std::smatch m; std::regex_search(cmdLine, m, regexStr))
3635
return std::stoi(m[1].str());
3736

3837
return 0;
3938
}
4039

41-
std::string Auth::GetToken(const std::string& cmdLine, bool riotClient)
40+
std::string Auth::GetToken(const std::string& cmdLine, const bool riotClient)
4241
{
4342
std::regex regexStr;
44-
regexStr = riotClient ? ("--riotclient-auth-token=([\\w-]*)") : ("--remoting-auth-token=([\\w-]*)");
45-
std::smatch m;
46-
if (std::regex_search(cmdLine, m, regexStr))
43+
regexStr = riotClient ? "--riotclient-auth-token=([\\w-]*)" : "--remoting-auth-token=([\\w-]*)";
44+
if (std::smatch m; std::regex_search(cmdLine, m, regexStr))
4745
{
4846
std::string token = "riot:" + m[1].str();
49-
char* tokenArray = &token[0];
50-
return base64.Encode(reinterpret_cast<unsigned char*>(tokenArray), static_cast<unsigned>(token.size())).c_str();
47+
char* tokenArray = token.data();
48+
return base64.Encode(reinterpret_cast<unsigned char*>(tokenArray), static_cast<unsigned>(token.size()));
5149
}
5250

5351
return "";
@@ -61,9 +59,10 @@ std::string Auth::MakeLeagueHeader(const ClientInfo& info)
6159
"Accept: application/json" + "\r\n" +
6260
"Content-Type: application/json" + "\r\n" +
6361
"Origin: https://127.0.0.1:" + std::to_string(info.port) + "\r\n" +
64-
"User-Agent: Mozilla/5.0 (Windows NT 6.2; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) LeagueOfLegendsClient/" + info.version + " (CEF 91) Safari/537.36" + "\r\n" +
62+
"User-Agent: Mozilla/5.0 (Windows NT 6.2; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) LeagueOfLegendsClient/" + info.version +
63+
" (CEF 91) Safari/537.36" + "\r\n" +
6564
"X-Riot-Source: rcp-fe-lol-social" + "\r\n" +
66-
"sec-ch-ua: \"Chromium\";v=\"91\"" + "\r\n" +
65+
R"(sec-ch-ua: "Chromium";v="91")" + "\r\n" +
6766
"sec-ch-ua-mobile: ?0" + "\r\n" +
6867
"Sec-Fetch-Site: same-origin" + "\r\n" +
6968
"Sec-Fetch-Mode: cors" + "\r\n" +
@@ -87,7 +86,8 @@ std::string Auth::MakeRiotHeader(const ClientInfo& info)
8786
"Sec-Fetch-Mode: cors" + "\r\n" +
8887
"Sec-Fetch-Site: same-origin" + "\r\n" +
8988
"Sec-Fetch-User: ?F" + "\r\n" +
90-
"User-Agent: Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) RiotClient/" + info.version + " (CEF 74) Safari/537.36" + "\r\n" +
89+
"User-Agent: Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) RiotClient/" + info.version +
90+
" (CEF 74) Safari/537.36" + "\r\n" +
9191
"sec-ch-ua: Chromium" + "\r\n" +
9292
"Referer: https://127.0.0.1:" + std::to_string(info.port) + "/index.html" + "\r\n" +
9393
"Accept-Encoding: gzip, deflate, br" + "\r\n" +
@@ -96,7 +96,7 @@ std::string Auth::MakeRiotHeader(const ClientInfo& info)
9696

9797
DWORD Auth::GetProcessId(const std::wstring& processName)
9898
{
99-
HANDLE snapshot = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, NULL);
99+
const HANDLE snapshot = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, NULL);
100100
if (snapshot != INVALID_HANDLE_VALUE)
101101
{
102102
PROCESSENTRY32W entry;
@@ -110,7 +110,8 @@ DWORD Auth::GetProcessId(const std::wstring& processName)
110110
CloseHandle(snapshot);
111111
return entry.th32ProcessID;
112112
}
113-
} while (Process32NextW(snapshot, &entry));
113+
}
114+
while (Process32NextW(snapshot, &entry));
114115
}
115116
}
116117
CloseHandle(snapshot);
@@ -119,8 +120,8 @@ DWORD Auth::GetProcessId(const std::wstring& processName)
119120

120121
std::vector<DWORD> Auth::GetAllProcessIds(const std::wstring& processName)
121122
{
122-
std::vector<DWORD>pids;
123-
HANDLE snapshot = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, NULL);
123+
std::vector<DWORD> pids;
124+
const HANDLE snapshot = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, NULL);
124125
if (snapshot != INVALID_HANDLE_VALUE)
125126
{
126127
PROCESSENTRY32W entry;
@@ -133,7 +134,8 @@ std::vector<DWORD> Auth::GetAllProcessIds(const std::wstring& processName)
133134
{
134135
pids.emplace_back(entry.th32ProcessID);
135136
}
136-
} while (Process32NextW(snapshot, &entry));
137+
}
138+
while (Process32NextW(snapshot, &entry));
137139
}
138140
}
139141
CloseHandle(snapshot);
@@ -142,52 +144,54 @@ std::vector<DWORD> Auth::GetAllProcessIds(const std::wstring& processName)
142144

143145
std::wstring Auth::GetProcessCommandLine(const DWORD& processId)
144146
{
145-
typedef NTSTATUS(__stdcall* tNtQueryInformationProcess)
146-
(
147-
HANDLE ProcessHandle,
148-
ULONG ProcessInformationClass,
149-
PVOID ProcessInformation,
150-
ULONG ProcessInformationLength,
151-
PULONG ReturnLength
152-
);
147+
using tNtQueryInformationProcess = NTSTATUS(__stdcall*)
148+
(
149+
HANDLE ProcessHandle,
150+
ULONG ProcessInformationClass,
151+
PVOID ProcessInformation,
152+
ULONG ProcessInformationLength,
153+
PULONG ReturnLength
154+
);
153155

154156
std::wstring result;
155-
HANDLE processHandle = OpenProcess(PROCESS_QUERY_INFORMATION | PROCESS_VM_READ, 0, processId);
157+
const HANDLE processHandle = OpenProcess(PROCESS_QUERY_INFORMATION | PROCESS_VM_READ, 0, processId);
156158

157159
SYSTEM_INFO si;
158160
GetNativeSystemInfo(&si);
159161

160162
BOOL wow;
161163
IsWow64Process(GetCurrentProcess(), &wow);
162164

163-
DWORD ProcessParametersOffset = si.wProcessorArchitecture == PROCESSOR_ARCHITECTURE_AMD64 ? 0x20 : 0x10;
164-
DWORD CommandLineOffset = si.wProcessorArchitecture == PROCESSOR_ARCHITECTURE_AMD64 ? 0x70 : 0x40;
165+
const DWORD ProcessParametersOffset = si.wProcessorArchitecture == PROCESSOR_ARCHITECTURE_AMD64 ? 0x20 : 0x10;
166+
const DWORD CommandLineOffset = si.wProcessorArchitecture == PROCESSOR_ARCHITECTURE_AMD64 ? 0x70 : 0x40;
165167

166-
DWORD pebSize = ProcessParametersOffset + 8; // size until ProcessParameters
167-
PBYTE peb = (PBYTE)malloc(pebSize);
168+
const DWORD pebSize = ProcessParametersOffset + 8; // size until ProcessParameters
169+
const auto peb = static_cast<PBYTE>(malloc(pebSize));
168170
ZeroMemory(peb, pebSize);
169171

170-
DWORD processParametersSize = CommandLineOffset + 16;
171-
PBYTE processParameters = (PBYTE)malloc(processParametersSize);
172+
const DWORD processParametersSize = CommandLineOffset + 16;
173+
const auto processParameters = static_cast<PBYTE>(malloc(processParametersSize));
172174
ZeroMemory(processParameters, processParametersSize);
173175

174176
if (wow)
175177
{
176-
typedef struct _PROCESS_BASIC_INFORMATION_WOW64 {
178+
using PROCESS_BASIC_INFORMATION_WOW64 = struct _PROCESS_BASIC_INFORMATION_WOW64
179+
{
177180
PVOID Reserved1[2];
178181
PVOID64 PebBaseAddress;
179182
PVOID Reserved2[4];
180183
ULONG_PTR UniqueProcessId[2];
181184
PVOID Reserved3[2];
182-
} PROCESS_BASIC_INFORMATION_WOW64;
185+
};
183186

184-
typedef struct _UNICODE_STRING_WOW64 {
187+
using UNICODE_STRING_WOW64 = struct _UNICODE_STRING_WOW64
188+
{
185189
USHORT Length;
186190
USHORT MaximumLength;
187191
PVOID64 Buffer;
188-
} UNICODE_STRING_WOW64;
192+
};
189193

190-
typedef NTSTATUS(NTAPI* tNtWow64ReadVirtualMemory64)(
194+
using tNtWow64ReadVirtualMemory64 = NTSTATUS(NTAPI*)(
191195
IN HANDLE ProcessHandle,
192196
IN PVOID64 BaseAddress,
193197
OUT PVOID Buffer,
@@ -197,38 +201,38 @@ std::wstring Auth::GetProcessCommandLine(const DWORD& processId)
197201
PROCESS_BASIC_INFORMATION_WOW64 pbi;
198202
ZeroMemory(&pbi, sizeof(pbi));
199203

200-
tNtQueryInformationProcess NtQueryInformationProcess =
201-
(tNtQueryInformationProcess)GetProcAddress(GetModuleHandleA("ntdll.dll"), "NtWow64QueryInformationProcess64");
202-
if (NtQueryInformationProcess(processHandle, 0, &pbi, sizeof(pbi), 0) != 0)
204+
if (const auto NtQueryInformationProcess =
205+
reinterpret_cast<tNtQueryInformationProcess>(GetProcAddress(GetModuleHandleA("ntdll.dll"), "NtWow64QueryInformationProcess64"));
206+
NtQueryInformationProcess(processHandle, 0, &pbi, sizeof(pbi), nullptr) != 0)
203207
{
204-
MessageBoxA(0, "NtQueryInformationProcess failed", 0, 0);
208+
MessageBoxA(nullptr, "NtQueryInformationProcess failed", nullptr, 0);
205209
CloseHandle(processHandle);
206210
return {};
207211
}
208212

209-
tNtWow64ReadVirtualMemory64 NtWow64ReadVirtualMemory64 =
210-
(tNtWow64ReadVirtualMemory64)GetProcAddress(GetModuleHandleA("ntdll.dll"), "NtWow64ReadVirtualMemory64");
213+
const auto NtWow64ReadVirtualMemory64 =
214+
reinterpret_cast<tNtWow64ReadVirtualMemory64>(GetProcAddress(GetModuleHandleA("ntdll.dll"), "NtWow64ReadVirtualMemory64"));
211215

212-
if (NtWow64ReadVirtualMemory64(processHandle, pbi.PebBaseAddress, peb, pebSize, NULL) != 0)
216+
if (NtWow64ReadVirtualMemory64(processHandle, pbi.PebBaseAddress, peb, pebSize, nullptr) != 0)
213217
{
214-
MessageBoxA(0, "PEB NtWow64ReadVirtualMemory64 failed", 0, 0);
218+
MessageBoxA(nullptr, "PEB NtWow64ReadVirtualMemory64 failed", nullptr, 0);
215219
CloseHandle(processHandle);
216220
return {};
217221
}
218222

219-
PVOID64 parameters = (PVOID64) * ((PVOID64*)(peb + ProcessParametersOffset));
220-
if (NtWow64ReadVirtualMemory64(processHandle, parameters, processParameters, processParametersSize, NULL) != 0)
223+
if (const auto parameters = *reinterpret_cast<PVOID64*>(peb + ProcessParametersOffset); NtWow64ReadVirtualMemory64(
224+
processHandle, parameters, processParameters, processParametersSize, nullptr) != 0)
221225
{
222-
MessageBoxA(0, "processParameters NtWow64ReadVirtualMemory64 failed", 0, 0);
226+
MessageBoxA(nullptr, "processParameters NtWow64ReadVirtualMemory64 failed", nullptr, 0);
223227
CloseHandle(processHandle);
224228
return {};
225229
}
226230

227-
UNICODE_STRING_WOW64* pCommandLine = (UNICODE_STRING_WOW64*)(processParameters + CommandLineOffset);
228-
PWSTR commandLineCopy = (PWSTR)malloc(pCommandLine->MaximumLength);
229-
if (NtWow64ReadVirtualMemory64(processHandle, pCommandLine->Buffer, commandLineCopy, pCommandLine->MaximumLength, NULL) != 0)
231+
const UNICODE_STRING_WOW64* pCommandLine = reinterpret_cast<UNICODE_STRING_WOW64*>(processParameters + CommandLineOffset);
232+
const auto commandLineCopy = static_cast<PWSTR>(malloc(pCommandLine->MaximumLength));
233+
if (NtWow64ReadVirtualMemory64(processHandle, pCommandLine->Buffer, commandLineCopy, pCommandLine->MaximumLength, nullptr) != 0)
230234
{
231-
MessageBoxA(0, "pCommandLine NtWow64ReadVirtualMemory64 failed", 0, 0);
235+
MessageBoxA(nullptr, "pCommandLine NtWow64ReadVirtualMemory64 failed", nullptr, 0);
232236
CloseHandle(processHandle);
233237
return {};
234238
}
@@ -238,55 +242,56 @@ std::wstring Auth::GetProcessCommandLine(const DWORD& processId)
238242
}
239243
else
240244
{
241-
typedef struct _PROCESS_BASIC_INFORMATION {
245+
using PROCESS_BASIC_INFORMATION = struct _PROCESS_BASIC_INFORMATION
246+
{
242247
LONG ExitStatus;
243248
PVOID PebBaseAddress;
244249
ULONG_PTR AffinityMask;
245250
LONG BasePriority;
246251
HANDLE UniqueProcessId;
247252
HANDLE InheritedFromUniqueProcessId;
248-
} PROCESS_BASIC_INFORMATION;
253+
};
249254

250255
typedef struct _UNICODE_STRING
251256
{
252257
USHORT Length;
253258
USHORT MaximumLength;
254259
PWSTR Buffer;
255-
} UNICODE_STRING, * PUNICODE_STRING;
256-
typedef const UNICODE_STRING* PCUNICODE_STRING;
260+
} UNICODE_STRING, *PUNICODE_STRING [[maybe_unused]];
261+
/*[[maybe_unused]]*/ using PCUNICODE_STRING = const UNICODE_STRING*;
257262

258263
PROCESS_BASIC_INFORMATION pbi;
259264
ZeroMemory(&pbi, sizeof(pbi));
260265

261-
tNtQueryInformationProcess NtQueryInformationProcess =
262-
(tNtQueryInformationProcess)GetProcAddress(GetModuleHandleA("ntdll.dll"), "NtQueryInformationProcess");
263-
if (NtQueryInformationProcess(processHandle, 0, &pbi, sizeof(pbi), 0) != 0)
266+
if (const auto NtQueryInformationProcess =
267+
reinterpret_cast<tNtQueryInformationProcess>(GetProcAddress(GetModuleHandleA("ntdll.dll"), "NtQueryInformationProcess"));
268+
NtQueryInformationProcess(processHandle, 0, &pbi, sizeof(pbi), nullptr) != 0)
264269
{
265-
MessageBoxA(0, "NtQueryInformationProcess failed", 0, 0);
270+
MessageBoxA(nullptr, "NtQueryInformationProcess failed", nullptr, 0);
266271
CloseHandle(processHandle);
267272
return {};
268273
}
269274

270-
if (!ReadProcessMemory(processHandle, pbi.PebBaseAddress, peb, pebSize, NULL))
275+
if (!ReadProcessMemory(processHandle, pbi.PebBaseAddress, peb, pebSize, nullptr))
271276
{
272-
MessageBoxA(0, "PEB ReadProcessMemory failed", 0, 0);
277+
MessageBoxA(nullptr, "PEB ReadProcessMemory failed", nullptr, 0);
273278
CloseHandle(processHandle);
274279
return {};
275280
}
276281

277-
PBYTE* parameters = (PBYTE*)*(LPVOID*)(peb + ProcessParametersOffset);
278-
if (!ReadProcessMemory(processHandle, parameters, processParameters, processParametersSize, NULL))
282+
if (const PBYTE* parameters = static_cast<PBYTE*>(*reinterpret_cast<LPVOID*>(peb + ProcessParametersOffset)); !ReadProcessMemory(
283+
processHandle, parameters, processParameters, processParametersSize, nullptr))
279284
{
280-
MessageBoxA(0, "processParameters ReadProcessMemory failed", 0, 0);
285+
MessageBoxA(nullptr, "processParameters ReadProcessMemory failed", nullptr, 0);
281286
CloseHandle(processHandle);
282287
return {};
283288
}
284289

285-
UNICODE_STRING* pCommandLine = (UNICODE_STRING*)(processParameters + CommandLineOffset);
286-
PWSTR commandLineCopy = (PWSTR)malloc(pCommandLine->MaximumLength);
287-
if (!ReadProcessMemory(processHandle, pCommandLine->Buffer, commandLineCopy, pCommandLine->MaximumLength, NULL))
290+
const UNICODE_STRING* pCommandLine = reinterpret_cast<UNICODE_STRING*>(processParameters + CommandLineOffset);
291+
const auto commandLineCopy = static_cast<PWSTR>(malloc(pCommandLine->MaximumLength));
292+
if (!ReadProcessMemory(processHandle, pCommandLine->Buffer, commandLineCopy, pCommandLine->MaximumLength, nullptr))
288293
{
289-
MessageBoxA(0, "pCommandLine ReadProcessMemory failed", 0, 0);
294+
MessageBoxA(nullptr, "pCommandLine ReadProcessMemory failed", nullptr, 0);
290295
CloseHandle(processHandle);
291296
return {};
292297
}
@@ -300,14 +305,12 @@ std::wstring Auth::GetProcessCommandLine(const DWORD& processId)
300305

301306
std::wstring Auth::GetProcessPath(const DWORD& processId)
302307
{
303-
HANDLE processHandle = OpenProcess(PROCESS_QUERY_INFORMATION | PROCESS_VM_READ, 0, processId);
304-
if (processHandle)
308+
if (const HANDLE processHandle = OpenProcess(PROCESS_QUERY_INFORMATION | PROCESS_VM_READ, 0, processId))
305309
{
306-
WCHAR result[MAX_PATH];
307-
if (GetModuleFileNameExW(processHandle, NULL, result, MAX_PATH))
310+
if (WCHAR result[MAX_PATH]; GetModuleFileNameExW(processHandle, nullptr, result, MAX_PATH))
308311
{
309312
CloseHandle(processHandle);
310-
return std::wstring(result);
313+
return {result};
311314
}
312315
CloseHandle(processHandle);
313316
}
@@ -316,26 +319,25 @@ std::wstring Auth::GetProcessPath(const DWORD& processId)
316319

317320
std::string Auth::GetFileVersion(const std::wstring& file)
318321
{
319-
DWORD versionSize = GetFileVersionInfoSizeW(file.c_str(), 0);
320-
if (versionSize)
322+
if (const DWORD versionSize = GetFileVersionInfoSizeW(file.c_str(), nullptr))
321323
{
322324
std::vector<unsigned char> versionInfo(versionSize);
323325

324-
if (GetFileVersionInfoW(file.c_str(), 0, versionSize, &versionInfo[0]))
326+
if (GetFileVersionInfoW(file.c_str(), 0, versionSize, versionInfo.data()))
325327
{
326-
VS_FIXEDFILEINFO* lpFfi;
328+
VS_FIXEDFILEINFO* lpFfi = nullptr;
327329
UINT size = sizeof(VS_FIXEDFILEINFO);
328-
if (VerQueryValueW(&versionInfo[0], L"\\", (LPVOID*)&lpFfi, &size))
330+
if (VerQueryValueW(versionInfo.data(), L"\\", reinterpret_cast<LPVOID*>(&lpFfi), &size))
329331
{
330-
DWORD dwFileVersionMS = lpFfi->dwFileVersionMS;
331-
DWORD dwFileVersionLS = lpFfi->dwFileVersionLS;
332+
const DWORD dwFileVersionMS = lpFfi->dwFileVersionMS;
333+
const DWORD dwFileVersionLS = lpFfi->dwFileVersionLS;
332334
std::string result = std::format("{}.{}.{}.{}",
333-
HIWORD(dwFileVersionMS), LOWORD(dwFileVersionMS),
334-
HIWORD(dwFileVersionLS), LOWORD(dwFileVersionLS));
335+
HIWORD(dwFileVersionMS), LOWORD(dwFileVersionMS),
336+
HIWORD(dwFileVersionLS), LOWORD(dwFileVersionLS));
335337
return result;
336338
}
337339
}
338340
}
339341

340342
return "";
341-
}
343+
}

KBotExt/Auth.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,4 +38,4 @@ class Auth
3838
static std::wstring GetProcessCommandLine(const DWORD& processId);
3939
static std::wstring GetProcessPath(const DWORD& processId);
4040
static std::string GetFileVersion(const std::wstring& file);
41-
};
41+
};

0 commit comments

Comments
 (0)