-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathmain.cpp
267 lines (225 loc) · 8.03 KB
/
main.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
#include <cstdlib>
#include <iostream>
#include <string>
#include <map>
#include <windows.h>
#include <cstdio>
#include "utils.h"
using namespace std;
HWND hwnd = NULL;
bool StartProcess(const char* workdir, const char* cmd, PROCESS_INFORMATION* pi)
{
STARTUPINFO si;
ZeroMemory( pi, sizeof( PROCESS_INFORMATION ) );
ZeroMemory( & si, sizeof( si ) );
si.cb = sizeof( si );
return CreateProcess(NULL, (LPSTR)cmd, NULL, NULL, FALSE, 0, NULL, workdir, &si, pi );
}
void ReportError(std::string text)
{
cout << "ERROR: " << text << endl;
MessageBox(hwnd, ("There was a problem starting your game.\nError message: "+text).c_str(), "EengineLauncher - ERROR", MB_ICONERROR | MB_OK);
}
void ReportError(std::string text, int code)
{
cout << "ERROR: " << text << ", code: " << code << endl;
char* codeStr = new char[4];
sprintf(codeStr, "%d", code);
MessageBox(hwnd, ("There was a problem starting your game.\nError message: "+text+"\nError code: "+codeStr).c_str(), "EengineLauncher - ERROR", MB_ICONERROR | MB_OK);
delete codeStr;
}
// --------
std::string colobotDir;
enum ColobotVersion {
VERSION_UNKNOWN,
VERSION_1_9_PL,
VERSION_1_3_EN,
VERSION_1_8_EN,
VERSION_1_8_EN_2,
VERSION_1_9_PL_KS,
VERSION_CEEBOT4_PL,
VERSION_C4S_EN,
VERSION_CEEBOT_A_EN,
VERSION_BC_1_0_EN
};
struct VersionData {
std::string md5;
void* websiteStringPtr;
void* versionStringPtr;
std::string versionStr; // <= 7
std::string fullVersionStr;
};
std::map<ColobotVersion, VersionData> versions;
void LoadVersions()
{
//TODO: Auto-update? Reading from file?
VersionData version_unknown;
version_unknown.fullVersionStr = "Eengine game";
versions[VERSION_UNKNOWN] = version_unknown;
VersionData version_1_9_pl;
version_1_9_pl.md5 = "991ff670d594e064333bf8b3ef14a395";
version_1_9_pl.websiteStringPtr = (void*)0x0053CEE0;
version_1_9_pl.versionStringPtr = (void*)0x005416A8;
version_1_9_pl.versionStr = "1.9 PL";
version_1_9_pl.fullVersionStr = "Colobot";
versions[VERSION_1_9_PL] = version_1_9_pl;
VersionData version_1_3_en;
version_1_3_en.md5 = "19b10cab2e24170e75791e0d8ef6adb2";
version_1_3_en.websiteStringPtr = (void*)0x00537790;
version_1_3_en.versionStringPtr = (void*)0x0053BE28;
version_1_3_en.versionStr = "1.3 EN";
version_1_3_en.fullVersionStr = "Colobot";
versions[VERSION_1_3_EN] = version_1_3_en;
VersionData version_1_8_en;
version_1_8_en.md5 = "60b187eb8caa297af90fb9f1c618f9b0";
version_1_8_en.websiteStringPtr = (void*)0x0053CEE8;
version_1_8_en.versionStringPtr = (void*)0x005416B8;
version_1_8_en.versionStr = "1.8 EN";
version_1_8_en.fullVersionStr = "Colobot";
versions[VERSION_1_8_EN] = version_1_8_en;
VersionData version_1_8_en_2;
version_1_8_en_2.md5 = "44b461930119879487dbc89788deb0c0";
version_1_8_en_2.versionStr = "1.8 EN";
version_1_8_en_2.fullVersionStr = "Colobot";
versions[VERSION_1_8_EN_2] = version_1_8_en_2;
VersionData version_1_9_pl_ks;
version_1_9_pl_ks.md5 = "83e54ee377bcdf9f4930e0892952c4ab";
version_1_9_pl_ks.websiteStringPtr = (void*)0x0053CEE8;
version_1_9_pl_ks.versionStringPtr = (void*)0x005416B0;
version_1_9_pl_ks.versionStr = "1.9 PL";
version_1_9_pl_ks.fullVersionStr = "Colobot";
versions[VERSION_1_9_PL_KS] = version_1_9_pl_ks;
VersionData version_ceebot4_pl;
version_ceebot4_pl.md5 = "c5e4674367ff2f842cc7b5761d6a744a";
version_ceebot4_pl.fullVersionStr = "CeeBot4";
versions[VERSION_CEEBOT4_PL] = version_ceebot4_pl;
VersionData version_c4s_en;
version_c4s_en.md5 = "12b77fc84e07e69fed370e8e3e4c8d8e";
version_c4s_en.fullVersionStr = "CeeBot4 SCHOOL";
versions[VERSION_C4S_EN] = version_c4s_en;
VersionData version_ceebot_a_en;
version_ceebot_a_en.md5 = "d719d5546777ba4d320adc51e8c99c63";
version_ceebot_a_en.fullVersionStr = "CeeBot-A";
versions[VERSION_CEEBOT_A_EN] = version_ceebot_a_en;
VersionData version_bc_1_0_en;
version_bc_1_0_en.md5 = "b85b3d09a12601fee4b8a3c234922882";
version_bc_1_0_en.fullVersionStr = "BuzzingCars";
versions[VERSION_BC_1_0_EN] = version_bc_1_0_en;
}
string checkGame()
{
CHAR* workingDir = new CHAR[513];
GetCurrentDirectory(512, workingDir);
std::string found;
if(fexists(std::string(workingDir)+"\\colobot.exe")) {
found = "colobot.exe";
}
else if(fexists(std::string(workingDir)+"\\ceebot.exe")) {
found = "ceebot.exe";
}
else if(fexists(std::string(workingDir)+"\\ceebot-a.exe")) {
found = "ceebot-a.exe";
}
else if(fexists(std::string(workingDir)+"\\ceebot4.exe")) {
found = "ceebot4.exe";
}
else if(fexists(std::string(workingDir)+"\\buzzingcars.exe")) {
found = "buzzingcars.exe";
}
return found;
}
ColobotVersion GetColobotVersion()
{
std::string game = checkGame();
std::string current = GetFileMD5(colobotDir+game);
for(std::map<ColobotVersion, VersionData>::iterator ver = versions.begin(); ver != versions.end(); ver++) {
if(ver->first == VERSION_UNKNOWN) continue;
if(ver->second.md5 == current) {
return ver->first;
}
}
return VERSION_UNKNOWN;
}
int main(int argc, char *argv[])
{
LoadVersions();
CHAR* workingDir = new CHAR[513];
GetCurrentDirectory(512, workingDir);
std::string game = checkGame();
colobotDir = std::string(workingDir)+"\\";
if(game == "")
{
ReportError("exe not found!");
return EXIT_FAILURE;
}
if(argc >= 2) {
if(!strcmp(argv[1], "getmd5")) {
cout << "MD5: " << GetFileMD5(colobotDir+game) << endl;
MessageBox(hwnd, ("MD5: "+GetFileMD5(colobotDir+game)).c_str(), "EengineLauncher", MB_ICONINFORMATION | MB_OK);
return EXIT_SUCCESS;
}
if(!strcmp(argv[1], "getclassname")) {
hwnd = FindWindow(NULL, "COLOBOT");
char* classname = new char[64];
GetClassName(hwnd, classname, 64);
cout << "Class name: " << classname << endl;
MessageBox(hwnd, (std::string("Class name: ")+classname).c_str(), "EengineLauncher", MB_ICONINFORMATION | MB_OK);
return EXIT_SUCCESS;
}
}
cout << "Setting combatibility mode... ";
HKEY hkey;
LONG res = RegOpenKeyEx(HKEY_CURRENT_USER, "Software\\Microsoft\\Windows NT\\CurrentVersion\\AppCompatFlags\\Layers", 0, KEY_ALL_ACCESS, &hkey);
if(res != ERROR_SUCCESS) {
ReportError("opening key", res);
return EXIT_FAILURE;
}
LPCTSTR data = "WINXPSP3 RUNASADMIN\0";
res = RegSetValueEx(hkey, (LPCTSTR)((colobotDir+game).c_str()), 0, REG_SZ, (LPBYTE)data, strlen(data)+1);
if(res != ERROR_SUCCESS) {
ReportError("writing value", res);
return EXIT_FAILURE;
}
cout << "OK" << endl;
cout << "Checking version... ";
ColobotVersion version = GetColobotVersion();
VersionData versionData = versions[version];
cout << versionData.fullVersionStr << endl;
cout << "Creating process... ";
PROCESS_INFORMATION pi;
if(!StartProcess(colobotDir.c_str(), (colobotDir+game+" -nocd").c_str(), &pi)) {
ReportError("creating process");
return EXIT_FAILURE;
}
HANDLE process = OpenProcess(PROCESS_ALL_ACCESS, TRUE, pi.dwProcessId);
cout << "OK" << endl;
cout << "Waiting for game window... ";
while(hwnd == NULL) {
if(game == "colobot.exe") hwnd = FindWindow("D3D Window", "COLOBOT");
else if(game == "buzzingcars.exe") hwnd = FindWindow("D3D Window", "BuzzingCars");
else hwnd = FindWindow("D3D Window", "CeeBot");
}
cout << "OK" << endl;
cout << "Changing window title... ";
if(!SetWindowText(hwnd, versionData.fullVersionStr.c_str())) {
cout << "ERROR (" << GetLastError() << "), SKIP" << endl;
} else {
cout << "OK" << endl;
}
cout << "Changing website text... ";
if(version == VERSION_UNKNOWN || versionData.websiteStringPtr == NULL) {
cout << "SKIP" << endl;
} else {
const char* newWebsite = "colobot.info"; // must be shorter than 15
SIZE_T written;
if(!WriteProcessMemory(process, versionData.websiteStringPtr, newWebsite, strlen(newWebsite)+1, &written)) {
cout << "ERROR (" << GetLastError() << "), SKIP" << endl;
} else {
cout << "OK" << endl;
}
}
CloseHandle(process);
CloseHandle(pi.hProcess);
CloseHandle(pi.hThread);
return EXIT_SUCCESS;
}