Skip to content

Commit f8b8287

Browse files
committed
Small refactor
1 parent 729f668 commit f8b8287

File tree

3 files changed

+174
-145
lines changed

3 files changed

+174
-145
lines changed

Launcher/.clang-format

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
BasedOnStyle: LLVM
2+
IndentWidth: 4
3+
ColumnLimit: 100

Launcher/main.cpp

+167-143
Original file line numberDiff line numberDiff line change
@@ -1,159 +1,183 @@
11
#include "stdafx.h"
22

33
int WINAPI WinMain(HINSTANCE, HINSTANCE, char *, int) {
4-
if (!AdjustCurrentPrivilege(SE_DEBUG_NAME)) {
5-
MessageBox(0, L"Failed to adjust privileges to debug", L"Failure", MB_OK);
6-
return 1;
7-
}
8-
9-
start:
10-
auto processInfo = GetProcessInfoByName(L"mirrorsedge.exe");
11-
if (!processInfo.th32ProcessID) {
12-
auto thread = CreateThread(nullptr, 0, [](void *) -> unsigned long {
13-
MessageBox(0, L"Waiting for Mirror's Edge to start. Click OK to stop.", L"Waiting...", MB_OK);
14-
exit(0);
15-
return 0;
16-
}, nullptr, 0, nullptr);
17-
18-
do {
19-
Sleep(200);
20-
processInfo = GetProcessInfoByName(L"mirrorsedge.exe");
21-
} while (!processInfo.th32ProcessID);
22-
23-
TerminateThread(thread, 0);
24-
}
25-
26-
auto status = 0;
27-
auto process = OpenProcess(PROCESS_ALL_ACCESS, false, processInfo.th32ProcessID);
28-
if (process) {
29-
if (HasModule(process, L"mmultiplayer.dll")) {
30-
CloseHandle(process);
31-
return 0;
32-
}
33-
34-
while (!HasModule(process, L"openal32.dll")) {
35-
CloseHandle(process);
36-
Sleep(200);
37-
goto start;
38-
}
39-
40-
auto path = GetDllPath();
41-
if (URLDownloadToFile(nullptr, L"https://github.com/btbd/mmultiplayer/raw/master/Client/binary/Client.dll", path.c_str(), 0, nullptr) != S_OK && !PathFileExists(path.c_str())) {
42-
MessageBox(0, L"Failed to download the latest version", L"Failure", 0);
43-
status = 1;
44-
}
45-
46-
auto argSize = (path.size() + 1) * sizeof(wchar_t);
47-
48-
auto arg = VirtualAllocEx(process, nullptr, argSize, MEM_COMMIT | MEM_RESERVE, PAGE_READWRITE);
49-
if (arg) {
50-
if (WriteProcessMemory(process, arg, path.c_str(), argSize, nullptr)) {
51-
auto thread = CreateRemoteThread(process, nullptr, 0, reinterpret_cast<LPTHREAD_START_ROUTINE>(GetProcAddress(GetModuleHandle(L"kernel32.dll"), "LoadLibraryW")), arg, 0, nullptr);
52-
if (thread) {
53-
WaitForSingleObject(thread, INFINITE);
54-
CloseHandle(thread);
55-
} else {
56-
MessageBox(0, L"Failed to create remote thread", L"Failure", 0);
57-
status = 1;
58-
}
59-
} else {
60-
MessageBox(0, L"Failed to write process memory", L"Failure", 0);
61-
status = 1;
62-
}
63-
64-
VirtualFreeEx(process, arg, 0, MEM_RELEASE);
65-
} else {
66-
MessageBox(0, L"Failed to allocate virtual memory", L"Failure", 0);
67-
status = 1;
68-
}
69-
70-
CloseHandle(process);
71-
} else {
72-
MessageBox(0, L"Failed to open a handle to Mirror's Edge", L"Failure", 0);
73-
status = 1;
74-
}
75-
76-
return status;
4+
if (!AdjustCurrentPrivilege(SE_DEBUG_NAME)) {
5+
MessageBox(0, L"Failed to adjust privileges to debug", L"Failure", MB_OK);
6+
return 1;
7+
}
8+
9+
const auto thread = CreateDialogThread();
10+
for (;; Sleep(200)) {
11+
const auto processInfo = GetProcessInfoByName(L"mirrorsedge.exe");
12+
if (!processInfo.th32ProcessID) {
13+
continue;
14+
}
15+
16+
const auto process = OpenProcess(PROCESS_ALL_ACCESS, false, processInfo.th32ProcessID);
17+
if (!process) {
18+
TerminateThread(thread, 0);
19+
MessageBox(0, L"Failed to open a handle to the process", L"Failure", 0);
20+
return 1;
21+
}
22+
23+
if (HasModule(process, L"mmultiplayer.dll")) {
24+
CloseHandle(process);
25+
return 0;
26+
}
27+
28+
if (!HasModule(process, L"openal32.dll")) {
29+
CloseHandle(process);
30+
continue;
31+
}
32+
33+
TerminateThread(thread, 0);
34+
35+
const auto status = LoadClient(process);
36+
CloseHandle(process);
37+
return !status;
38+
}
39+
}
40+
41+
bool LoadClient(HANDLE process) {
42+
std::wstring path;
43+
if (!GetDllPath(path)) {
44+
MessageBox(0, L"Failed to get temp path", L"Failure", 0);
45+
return false;
46+
}
47+
48+
const auto url = L"https://github.com/btbd/mmultiplayer/raw/master/Client/binary/Client.dll";
49+
if (URLDownloadToFile(nullptr, url, path.c_str(), 0, nullptr) != S_OK &&
50+
!PathFileExists(path.c_str())) {
51+
52+
MessageBox(0, L"Failed to download the latest version", L"Failure", 0);
53+
return false;
54+
}
55+
56+
bool status = false;
57+
58+
const auto size = (path.size() + 1) * sizeof(wchar_t);
59+
const auto arg =
60+
VirtualAllocEx(process, nullptr, size, MEM_COMMIT | MEM_RESERVE, PAGE_READWRITE);
61+
62+
if (arg) {
63+
if (WriteProcessMemory(process, arg, path.c_str(), size, nullptr)) {
64+
const auto thread =
65+
CreateRemoteThread(process, nullptr, 0,
66+
reinterpret_cast<LPTHREAD_START_ROUTINE>(GetProcAddress(
67+
GetModuleHandle(L"kernel32.dll"), "LoadLibraryW")),
68+
arg, 0, nullptr);
69+
70+
if (thread) {
71+
WaitForSingleObject(thread, INFINITE);
72+
CloseHandle(thread);
73+
74+
status = true;
75+
} else {
76+
MessageBox(0, L"Failed to create remote thread", L"Failure", 0);
77+
}
78+
} else {
79+
MessageBox(0, L"Failed to write process memory", L"Failure", 0);
80+
}
81+
82+
VirtualFreeEx(process, arg, 0, MEM_RELEASE);
83+
} else {
84+
MessageBox(0, L"Failed to allocate virtual memory", L"Failure", 0);
85+
}
86+
87+
return status;
88+
}
89+
90+
HANDLE CreateDialogThread() {
91+
return CreateThread(
92+
nullptr, 0,
93+
[](void *) -> unsigned long {
94+
MessageBox(0, L"Waiting for Mirror's Edge to start. Click OK to stop.", L"Waiting...",
95+
MB_OK);
96+
97+
exit(0);
98+
return 0;
99+
},
100+
nullptr, 0, nullptr);
77101
}
78102

79103
bool HasModule(HANDLE process, const wchar_t *module) {
80-
HANDLE snapshot = CreateToolhelp32Snapshot(TH32CS_SNAPMODULE | TH32CS_SNAPMODULE32, GetProcessId(process));
81-
if (snapshot == INVALID_HANDLE_VALUE) {
82-
return false;
83-
}
84-
85-
MODULEENTRY32 entry;
86-
entry.dwSize = sizeof(entry);
87-
if (Module32First(snapshot, &entry)) {
88-
do {
89-
if (_wcsicmp(entry.szModule, module) == 0) {
90-
CloseHandle(snapshot);
91-
return true;
92-
}
93-
} while (Module32Next(snapshot, &entry));
94-
}
95-
96-
CloseHandle(snapshot);
97-
return false;
104+
const auto snapshot =
105+
CreateToolhelp32Snapshot(TH32CS_SNAPMODULE | TH32CS_SNAPMODULE32, GetProcessId(process));
106+
107+
if (snapshot == INVALID_HANDLE_VALUE) {
108+
return false;
109+
}
110+
111+
MODULEENTRY32 entry = {sizeof(entry)};
112+
if (Module32First(snapshot, &entry)) {
113+
do {
114+
if (_wcsicmp(entry.szModule, module) == 0) {
115+
CloseHandle(snapshot);
116+
return true;
117+
}
118+
} while (Module32Next(snapshot, &entry));
119+
}
120+
121+
CloseHandle(snapshot);
122+
return false;
98123
}
99124

100-
std::wstring GetDllPath() {
101-
wchar_t buffer[0x200] = { 0 };
102-
if (!GetTempPath(sizeof(buffer) / sizeof(buffer[0]), buffer)) {
103-
MessageBox(0, L"Failed to get temp path", L"Failure", 0);
104-
exit(1);
105-
}
125+
bool GetDllPath(std::wstring &path) {
126+
wchar_t buffer[0x200] = {0};
127+
if (!GetTempPath(sizeof(buffer) / sizeof(buffer[0]), buffer)) {
128+
return false;
129+
}
106130

107-
return std::wstring(buffer) + L"mmultiplayer.dll";
131+
path = std::wstring(buffer) + L"mmultiplayer.dll";
132+
return true;
108133
}
109134

110135
PROCESSENTRY32 GetProcessInfoByName(const wchar_t *name) {
111-
auto snapshot = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0);
112-
if (snapshot == INVALID_HANDLE_VALUE) {
113-
return { 0 };
114-
}
115-
116-
PROCESSENTRY32 entry;
117-
entry.dwSize = sizeof(entry);
118-
if (Process32First(snapshot, &entry)) {
119-
do {
120-
if (_wcsicmp(entry.szExeFile, name) == 0) {
121-
CloseHandle(snapshot);
122-
return entry;
123-
}
124-
} while (Process32Next(snapshot, &entry));
125-
}
126-
127-
CloseHandle(snapshot);
128-
return { 0 };
136+
const auto snapshot = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0);
137+
if (snapshot == INVALID_HANDLE_VALUE) {
138+
return {0};
139+
}
140+
141+
PROCESSENTRY32 entry = {sizeof(entry)};
142+
if (Process32First(snapshot, &entry)) {
143+
do {
144+
if (_wcsicmp(entry.szExeFile, name) == 0) {
145+
CloseHandle(snapshot);
146+
return entry;
147+
}
148+
} while (Process32Next(snapshot, &entry));
149+
}
150+
151+
CloseHandle(snapshot);
152+
return {0};
129153
}
130154

131155
bool AdjustCurrentPrivilege(const wchar_t *privilege) {
132-
LUID luid;
133-
if (!LookupPrivilegeValue(nullptr, privilege, &luid)) {
134-
return FALSE;
135-
}
136-
137-
TOKEN_PRIVILEGES tp = { 0 };
138-
tp.PrivilegeCount = 1;
139-
tp.Privileges[0].Luid = luid;
140-
tp.Privileges[0].Attributes = SE_PRIVILEGE_ENABLED;
141-
142-
HANDLE token;
143-
if (!OpenProcessToken(GetCurrentProcess(), TOKEN_ADJUST_PRIVILEGES, &token)) {
144-
return FALSE;
145-
}
146-
147-
if (!AdjustTokenPrivileges(token, false, &tp, sizeof(tp), nullptr, nullptr)) {
148-
CloseHandle(token);
149-
return FALSE;
150-
}
151-
152-
if (GetLastError() == ERROR_NOT_ALL_ASSIGNED) {
153-
CloseHandle(token);
154-
return FALSE;
155-
}
156-
157-
CloseHandle(token);
158-
return TRUE;
156+
LUID luid;
157+
if (!LookupPrivilegeValue(nullptr, privilege, &luid)) {
158+
return FALSE;
159+
}
160+
161+
TOKEN_PRIVILEGES tp = {0};
162+
tp.PrivilegeCount = 1;
163+
tp.Privileges[0].Luid = luid;
164+
tp.Privileges[0].Attributes = SE_PRIVILEGE_ENABLED;
165+
166+
HANDLE token;
167+
if (!OpenProcessToken(GetCurrentProcess(), TOKEN_ADJUST_PRIVILEGES, &token)) {
168+
return FALSE;
169+
}
170+
171+
if (!AdjustTokenPrivileges(token, false, &tp, sizeof(tp), nullptr, nullptr)) {
172+
CloseHandle(token);
173+
return FALSE;
174+
}
175+
176+
if (GetLastError() == ERROR_NOT_ALL_ASSIGNED) {
177+
CloseHandle(token);
178+
return FALSE;
179+
}
180+
181+
CloseHandle(token);
182+
return TRUE;
159183
}

Launcher/stdafx.h

+4-2
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
#pragma once
22

3-
#include <stdio.h>
43
#include <Windows.h>
54
#include <shlwapi.h>
5+
#include <stdio.h>
66
#include <tlhelp32.h>
77

88
#pragma comment(lib, "shlwapi.lib")
@@ -11,7 +11,9 @@
1111
#include <string>
1212
#include <thread>
1313

14+
bool LoadClient(HANDLE process);
15+
HANDLE CreateDialogThread();
1416
bool HasModule(HANDLE process, const wchar_t *module);
15-
std::wstring GetDllPath();
17+
bool GetDllPath(std::wstring &path);
1618
PROCESSENTRY32 GetProcessInfoByName(const wchar_t *name);
1719
bool AdjustCurrentPrivilege(const wchar_t *privilege);

0 commit comments

Comments
 (0)