-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAutoUpdater.h
More file actions
542 lines (462 loc) · 12.9 KB
/
AutoUpdater.h
File metadata and controls
542 lines (462 loc) · 12.9 KB
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
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
#pragma once
#define VERSION "2026.04.19.2"
//#define DEV
#ifndef DEV
#define SERVER_PHP "https://4d-modding.com"
#define SERVER_FILES "https://4d-modding.com"
//#define SERVER_HTTP
#else
#define SERVER_PHP "http://localhost"
#define SERVER_FILES "http://localhost"
#define SERVER_HTTP
#endif
#include "requests.h"
#include "json.hpp"
#include <fstream>
#include <thread>
#include <atomic>
inline static constexpr uint64_t fdm014xOffset = 0x159BB8;
inline static constexpr int fdm0140InitCrashRptStartValue = 0x74696E49;
inline static constexpr int fdm0141AValue = 0x4005B220;
inline static constexpr uint64_t fdm0214VerOffset = 0x2434C8; // "0.2.1.4 Alpha" used in the title screen
inline static constexpr std::string fdm0214VerName = "0.2.1.4 Alpha";
inline static bool consoleOpen = false;
inline static FILE* fpout;
inline static FILE* fpin;
extern std::string fdmExe;
extern bool attachedToConsole;
extern std::string thisPath;
extern std::vector<std::string> args;
PROCESS_INFORMATION startup(LPCSTR lpApplicationName, const std::vector<std::string>& args, bool suspended = true);
inline static void print(const std::string& msg)
{
if (!consoleOpen)
{
AllocConsole();
freopen_s(&fpout, "CONOUT$", "wt", stdout);
freopen_s(&fpin, "CONIN$", "rt", stdin);
SetConsoleMode(GetStdHandle(STD_OUTPUT_HANDLE), ENABLE_VIRTUAL_TERMINAL_PROCESSING | ENABLE_PROCESSED_OUTPUT);
consoleOpen = true;
}
printf("%s", msg.c_str());
}
// [0] is modloaderVer, [1] is gameVer
std::vector<std::string> GetVers()
{
if(std::filesystem::exists("./core.ver"))
{
std::ifstream file("core.ver");
if (file.is_open())
{
std::string modloaderVer, gameVer;
std::getline(file, modloaderVer);
std::getline(file, gameVer);
file.close();
return { modloaderVer, gameVer };
}
}
else // detection of game version for 0.1.4.0, 0.1.4.1 and 0.2.1.4. also yea it forces to update modloader lol
{
std::ifstream file(fdmExe, std::ios::binary);
if (file.is_open())
{
// 0.1.4.0 check
file.seekg(fdm014xOffset);
int number;
file.read(reinterpret_cast<char*>(&number), sizeof(number));
if (number == fdm0140InitCrashRptStartValue)
{
file.close();
return { "0.0", "0.1.4.0" };
}
// 0.1.4.1 check (same offset lol)
if (number == fdm0141AValue)
{
file.close();
return { "0.0", "0.1.4.1" };
}
// 0.2.1.4 check
file.seekg(fdm0214VerOffset);
char buffer[14]{0};
file.read(buffer, sizeof(buffer) - 1);
std::string ver(buffer);
if (ver == fdm0214VerName)
{
file.close();
return { "0.0", "0.2.1.4" };
}
}
// download latest core.ver from the website lmfao. also yea it forces to update the modloader lol
DownloadFile(SERVER_PHP "/updater/latestGameVer.php", "core.ver");
std::ifstream a("core.ver");
if (a.is_open())
{
std::string modloaderVer, gameVer;
std::getline(a, modloaderVer);
std::getline(a, gameVer);
a.close();
return { "0.0", gameVer };
}
return { "0.0", "0.0" };
}
}
void createDirectories(const std::string& path, const std::string& pathPrefix)
{
std::string dirs = path;
std::string currentPath = "";
size_t pos = 0;
while ((pos = dirs.find_first_of("/")) != std::string::npos)
{
std::string directory = dirs.substr(0, pos);
dirs = dirs.erase(0, pos + 1);
if (!directory.empty() && !std::filesystem::path(directory).has_extension())
{
if (currentPath.empty())
currentPath = directory;
else
currentPath = (std::filesystem::path(currentPath) / directory).string();
auto fullPath = std::filesystem::path(pathPrefix) / currentPath;
if (!std::filesystem::exists(fullPath))
std::filesystem::create_directory(fullPath);
}
else
{
break;
}
}
}
inline std::vector<std::string> split(const std::string& str, char delim)
{
std::vector<std::string> tokens;
size_t pos = 0;
size_t len = str.length();
tokens.reserve(len / 2); // allocate memory for expected number of tokens
while (pos < len)
{
size_t end = str.find_first_of(delim, pos);
if (end == std::string::npos)
{
tokens.emplace_back(str.substr(pos));
break;
}
tokens.emplace_back(str.substr(pos, end - pos));
pos = end + 1;
}
return tokens;
}
inline void trimStart(std::string& s)
{
s.erase(s.begin(), std::find_if(s.begin(), s.end(), [](uint8_t c) {
return !std::isspace(c);
}));
}
inline void trimEnd(std::string& s)
{
s.erase(std::find_if(s.rbegin(), s.rend(), [](uint8_t c) {
return !std::isspace(c);
}).base(), s.end());
}
inline void trim(std::string& s)
{
trimStart(s);
trimEnd(s);
}
/*
Compares two version strings using an operator string
operators are "==" | ">=" | "<=" | "<" | ">" | "!="
it is used by the modloader to check dependency versions
*/
bool versionCompare(const std::string& aVer, const std::string& op, const std::string& bVer)
{
if (op.empty()) return true;
auto splitA = split(aVer, '.');
auto splitB = split(bVer, '.');
int len = std::max(splitA.size(), splitB.size());
int a, b;
for (int i = 0; i < len; i++)
{
a = (i < splitA.size() ? std::stoi(splitA[i]) : 0);
b = (i < splitB.size() ? std::stoi(splitB[i]) : 0);
if (a != b)
break;
}
if (op == "==") return a == b;
else if (op == ">=") return a >= b;
else if (op == "<=") return a <= b;
else if (op == "<") return a < b;
else if (op == ">") return a > b;
else if (op == "!=") return a != b;
return false;
}
bool updateModLoader()
{
std::string result = GET(SERVER_FILES "/core-files/modloader.ver", "");
trim(result);
if (result.size() <= 6) return false;
if (result[0] != '2') return false;
try
{
if (!versionCompare(VERSION, "<", result))
{
return false;
}
if (!attachedToConsole)
{
if (MessageBoxA(0, std::format("New 4DModLoader v{} is available (current is v{})\n", result, VERSION).c_str(), "Auto-Updater", MB_ICONQUESTION | MB_OKCANCEL) != IDOK)
{
return false;
}
}
else
{
print(std::format("New 4DModLoader v{} is available (current is v{})\n", result, VERSION));
print(std::format("Would you like to install the update? [Y/N]\n"));
std::atomic<bool> answered = false;
std::string answer;
std::thread inputThread{ [&]()
{
std::cin >> answer;
answered = true;
}
};
for (int i = 0; i < 5; ++i)
{
print(".");
std::this_thread::sleep_for(std::chrono::seconds(1));
if (answered) break;
}
inputThread.detach();
if (!answered || answer.empty())
{
print(std::format("\ni guess not\n"));
}
if (!answered || answer.empty() || std::tolower(answer[0]) != 'y')
{
return false;
}
}
auto thisPath_ = std::filesystem::path(thisPath);
auto thisPathOld = thisPath_;
thisPathOld.replace_extension(".exe.old");
auto pdbPath_ = thisPath_.parent_path() / "4DModLoader.pdb";
pdbPath_.replace_extension(".pdb");
auto pdbPathOld = pdbPath_;
pdbPathOld.replace_extension(".pdb.old");
try { std::filesystem::rename(thisPath_, thisPathOld); }
catch (const std::exception& e) { print("Failed to update self .exe\n"); return false; }
int64_t bytesDownloaded = 0;
print("Downloading 4DModLoader.exe...\n");
bytesDownloaded = 0;
if (DownloadFile(SERVER_FILES "/core-files/4DModLoader.exe", thisPath_.string(), bytesDownloaded))
{
print(std::format("\tDownloaded ({}).\n", bytesDownloaded));
}
else
{
print("\tFailed.\n");
return false;
}
if (std::filesystem::exists(pdbPath_))
{
try
{
std::filesystem::rename(pdbPath_, pdbPathOld);
print("Downloading 4DModLoader.pdb...\n");
bytesDownloaded = 0;
if (DownloadFile(SERVER_FILES "/core-files/4DModLoader.pdb", pdbPath_.string(), bytesDownloaded))
{
print(std::format("\tDownloaded ({}).\n", bytesDownloaded));
}
else
{
print("\tFailed.\n");
return false;
}
}
catch (const std::exception& e) { print("Failed to update self .pdb\n"); return false; }
}
return true;
}
catch (const std::exception&)
{
return false;
}
return false;
}
void updateCore()
{
// check for game's ver
auto vers = GetVers();
std::string result = POST(
SERVER_PHP "/updater/updateAvailable.php",
std::format("curGameVer={}&curModLoaderVer={}", vers[1], vers[0]));
if (result.size() <= 6)
return;
result.erase(std::remove(result.begin(), result.end(), '\\'), result.end());
if (result == "WRONG_GAME_VERSION" || result == "SAME_MODLOADER_VERSION")
return;
nlohmann::json updateJson = nlohmann::json::parse(result);
std::string verNum = updateJson["versionNumber"];
if (!attachedToConsole)
{
if (MessageBoxA(0, std::format("New 4DModLoader-Core v{} is available for 4D Miner v{}. (current is v{})\nWould you like to update?\n(you can turn this off in settings or use -offline)", verNum, vers[1], vers[0]).c_str(), "Auto-Updater", MB_ICONQUESTION | MB_OKCANCEL) != IDOK)
{
return;
}
}
else
{
print(std::format("New 4DModLoader-Core v{} is available for 4D Miner v{} (current is v{})\n", verNum, vers[1], vers[0]));
print(std::format("Would you like to install the update? [Y/N]\n"));
std::atomic<bool> answered = false;
std::string answer;
std::thread inputThread{ [&]()
{
std::cin >> answer;
answered = true;
}
};
for (int i = 0; i < 5; ++i)
{
print(".");
std::this_thread::sleep_for(std::chrono::seconds(1));
if (answered) break;
}
inputThread.detach();
if (!answered || answer.empty())
{
print(std::format("\ni guess not\n"));
}
if (!answered || answer.empty() || std::tolower(answer[0]) != 'y')
{
return;
}
}
print("Downloading new files...\n\n");
int64_t bytesDownloaded = 0;
for (auto& fileUrl : updateJson["versionFiles"])
{
std::string str = fileUrl;
bytesDownloaded = 0;
std::string hostname;
std::string path;
splitUrl(str, hostname, path);
std::filesystem::path filePath(path);
std::string fileName = filePath.filename().string();
size_t lToCut;
std::string outPath = fileName;
std::string a = std::format("core-files/{}/", vers[1]);
lToCut = path.find(a);
if (lToCut != std::string::npos)
{
outPath = path.substr(lToCut + a.size());
outPath = outPath.substr(outPath.find('/') + 1);
}
else
{
lToCut = path.find("core-files/");
if (lToCut != std::string::npos)
outPath = path.substr(lToCut + 11);
}
print(std::format("Downloading {}\n", outPath));
createDirectories(outPath, "./");
if (DownloadFile(str, outPath, bytesDownloaded))
print(std::format("\tDownloaded ({}).\n", bytesDownloaded));
else
print("\tFailed.\n");
}
}
void AutoUpdate()
{
// check if the server is down or the network is not available
#ifndef SERVER_HTTP
{
if (!InternetCheckConnectionA(SERVER_PHP, FLAG_ICC_FORCE_CONNECTION, 0))
return;
if (!InternetCheckConnectionA(SERVER_FILES, FLAG_ICC_FORCE_CONNECTION, 0))
return;
}
#endif
// don't auto-update if auto-updater option is toggled off
if(std::filesystem::exists("./settings.json"))
{
std::ifstream file("settings.json");
if (file.is_open())
{
nlohmann::json settingsJson = nlohmann::json::parse(file);
file.close();
if (settingsJson.contains("auto-updater") && settingsJson["auto-updater"].is_boolean() && !((bool)settingsJson["auto-updater"]))
return;
if(!settingsJson.contains("auto-updater"))
{
settingsJson["auto-updater"] = true;
std::ofstream outputFile("settings.json");
outputFile << settingsJson.dump() << std::endl;
outputFile.close();
}
}
}
if (updateModLoader())
{
auto pi = startup(thisPath.c_str(), args, false);
if (attachedToConsole)
{
WaitForSingleObject(pi.hProcess, INFINITE);
}
CloseHandle(pi.hProcess);
CloseHandle(pi.hThread);
exit(0);
return;
}
else
{
auto thisPath_ = std::filesystem::path(thisPath);
auto thisPathOld = thisPath_;
thisPathOld.replace_extension(".exe.old");
auto pdbPath_ = thisPath_.parent_path() / "4DModLoader.pdb";
pdbPath_.replace_extension(".pdb");
auto pdbPathOld = pdbPath_;
pdbPathOld.replace_extension(".pdb.old");
if (std::filesystem::exists(thisPathOld))
{
std::filesystem::remove(thisPathOld);
}
if (std::filesystem::exists(pdbPathOld))
{
std::filesystem::remove(pdbPathOld);
}
}
updateCore();
}
void CheckForLibs()
{
// check if the server is down or the network is not available
#ifndef SERVER_HTTP
{
if (!InternetCheckConnectionA(SERVER_PHP, FLAG_ICC_FORCE_CONNECTION, 0))
return;
if (!InternetCheckConnectionA(SERVER_FILES, FLAG_ICC_FORCE_CONNECTION, 0))
return;
}
#endif
std::string result = GET(SERVER_FILES "/core-files/versions.json", "");
if (result.size() <= 6) return;
nlohmann::json versionsJson = nlohmann::json::parse(result);
int64_t bytesDownloaded = 0;
if (versionsJson.contains("libs"))
{
for (auto& lib : versionsJson["libs"])
{
std::string libStr = lib;
if (!std::filesystem::exists(libStr))
{
print(std::format("Missing {}! Downloading it now...\n", libStr));
bytesDownloaded = 0;
if (DownloadFile(std::format(SERVER_FILES "/core-files/{}", libStr), libStr, bytesDownloaded))
print(std::format("\tDownloaded ({}).\n", bytesDownloaded));
else
print("\tFailed.\n");
}
}
}
}