-
Notifications
You must be signed in to change notification settings - Fork 13
Expand file tree
/
Copy pathMain.cpp
More file actions
708 lines (586 loc) · 21 KB
/
Copy pathMain.cpp
File metadata and controls
708 lines (586 loc) · 21 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
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
#define _PHYSX_DEBUG
#include <winsock2.h>
#include <windows.h>
#include <Overlay/Overlay.h>
#include <Common/Data.h>
#include <Utils/Utils.h>
#include <Hack/Hack.h>
#include <Common/Offset.h>
#include <thread>
#include <DMALibrary/Memory/Memory.h>
#include <cstdint>
#include <Utils/w3c/w3c.h>
#include <Utils/MachineCodeGenerator.h>
#include <CommCtrl.h>
#pragma comment(lib, "comctl32.lib")
#pragma comment(lib, "Msimg32.lib")
//#include "QPVerify.h"
FGameData GameData;
using namespace std;
// 进度窗口全局句柄
HWND g_progressWindow = NULL;
HWND g_progressBar = NULL;
HWND g_progressLabel = NULL;
HWND g_speedLabel = NULL;
HWND g_customProgress = NULL;
// 自定义绘制的进度条状态
struct ProgressBarState {
int percent;
double downloadedMB;
double totalMB;
double speed; // KB/s
DWORD lastUpdateTime;
DWORD lastDownloadedBytes;
bool indeterminate;
int animationOffset;
} g_progressState = { 0 };
// 自定义进度条窗口过程
LRESULT CALLBACK CustomProgressWndProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam) {
switch (msg) {
case WM_CREATE: {
// 初始化进度条状态
g_progressState.percent = 0;
g_progressState.downloadedMB = 0;
g_progressState.totalMB = 0;
g_progressState.speed = 0;
g_progressState.lastUpdateTime = GetTickCount();
g_progressState.lastDownloadedBytes = 0;
g_progressState.indeterminate = false;
g_progressState.animationOffset = 0;
// 创建定时器用于动画
SetTimer(hwnd, 1, 30, NULL);
return 0;
}
case WM_TIMER: {
// 更新动画偏移量
g_progressState.animationOffset = (g_progressState.animationOffset + 3) % 100;
InvalidateRect(hwnd, NULL, TRUE);
return 0;
}
case WM_PAINT: {
PAINTSTRUCT ps;
HDC hdc = BeginPaint(hwnd, &ps);
RECT clientRect;
GetClientRect(hwnd, &clientRect);
// 绘制背景
HBRUSH bgBrush = CreateSolidBrush(RGB(240, 240, 240));
FillRect(hdc, &clientRect, bgBrush);
DeleteObject(bgBrush);
// 绘制边框
HPEN borderPen = CreatePen(PS_SOLID, 1, RGB(220, 220, 220));
SelectObject(hdc, borderPen);
HBRUSH nullBrush = (HBRUSH)GetStockObject(NULL_BRUSH);
SelectObject(hdc, nullBrush);
RoundRect(hdc, clientRect.left, clientRect.top, clientRect.right, clientRect.bottom, 5, 5);
DeleteObject(borderPen);
// 绘制进度条轨道
RECT trackRect = clientRect;
InflateRect(&trackRect, -2, -2);
HBRUSH trackBrush = CreateSolidBrush(RGB(230, 230, 230));
FillRect(hdc, &trackRect, trackBrush);
DeleteObject(trackBrush);
// 绘制进度条
if (g_progressState.indeterminate) {
// 不确定状态的进度条 - 动画效果
RECT progressRect = trackRect;
progressRect.right = progressRect.left + (trackRect.right - trackRect.left) * 30 / 100;
progressRect.left = progressRect.left + g_progressState.animationOffset * (trackRect.right - trackRect.left) / 100;
if (progressRect.left > trackRect.right - (trackRect.right - trackRect.left) * 30 / 100) {
progressRect.left = trackRect.left;
progressRect.right = progressRect.left + (trackRect.right - trackRect.left) * 30 / 100;
}
HBRUSH progressBrush = CreateSolidBrush(RGB(0, 120, 215)); // Windows 10 蓝色
FillRect(hdc, &progressRect, progressBrush);
DeleteObject(progressBrush);
}
else {
// 确定状态的进度条
RECT progressRect = trackRect;
progressRect.right = progressRect.left + (trackRect.right - trackRect.left) * g_progressState.percent / 100;
// 创建渐变画刷
TRIVERTEX vertex[2];
vertex[0].x = progressRect.left;
vertex[0].y = progressRect.top;
vertex[0].Red = 0x0000;
vertex[0].Green = 0xC000;
vertex[0].Blue = 0xF000;
vertex[0].Alpha = 0x0000;
vertex[1].x = progressRect.right;
vertex[1].y = progressRect.bottom;
vertex[1].Red = 0x0000;
vertex[1].Green = 0x8000;
vertex[1].Blue = 0xD700;
vertex[1].Alpha = 0x0000;
GRADIENT_RECT gRect = { 0, 1 };
GradientFill(hdc, vertex, 2, &gRect, 1, GRADIENT_FILL_RECT_H);
}
EndPaint(hwnd, &ps);
return 0;
}
case WM_DESTROY:
KillTimer(hwnd, 1);
return 0;
}
return DefWindowProc(hwnd, msg, wParam, lParam);
}
// 注册自定义进度条窗口类
bool RegisterCustomProgressClass() {
WNDCLASSEXA wcex = { 0 };
wcex.cbSize = sizeof(WNDCLASSEXA);
wcex.style = CS_HREDRAW | CS_VREDRAW;
wcex.lpfnWndProc = CustomProgressWndProc;
wcex.hInstance = GetModuleHandle(NULL);
wcex.hCursor = LoadCursor(NULL, IDC_ARROW);
wcex.hbrBackground = (HBRUSH)(COLOR_WINDOW + 1);
wcex.lpszClassName = "CustomProgressClass";
return RegisterClassExA(&wcex) != 0;
}
// 创建一个简单的进度窗口函数
HWND CreateProgressWindow(const char* title) {
// 初始化Common Controls
INITCOMMONCONTROLSEX icex;
icex.dwSize = sizeof(INITCOMMONCONTROLSEX);
icex.dwICC = ICC_PROGRESS_CLASS;
InitCommonControlsEx(&icex);
// 注册自定义进度条窗口类
RegisterCustomProgressClass();
// 获取屏幕尺寸
int screenWidth = GetSystemMetrics(SM_CXSCREEN);
int screenHeight = GetSystemMetrics(SM_CYSCREEN);
// 设置窗口尺寸
int windowWidth = 480;
int windowHeight = 200;
// 计算窗口位置 (屏幕中央)
int posX = (screenWidth - windowWidth) / 2;
int posY = (screenHeight - windowHeight) / 2;
// 创建窗口
HWND hwndProgress = CreateWindowExA(
WS_EX_TOPMOST, // 窗口始终位于顶层
"Static",
title,
WS_OVERLAPPED | WS_CAPTION | WS_SYSMENU | WS_VISIBLE,
posX, posY, windowWidth, windowHeight,
NULL, NULL, GetModuleHandle(NULL), NULL);
// 添加图标到窗口
HICON hIcon = LoadIcon(NULL, IDI_APPLICATION);
SendMessage(hwndProgress, WM_SETICON, ICON_BIG, (LPARAM)hIcon);
// 创建标题标签
HWND hTitleLabel = CreateWindowExA(0, "Static", "正在下载更新文件...",
WS_CHILD | WS_VISIBLE | SS_CENTER,
20, 20, windowWidth - 40, 20, hwndProgress, NULL, GetModuleHandle(NULL), NULL);
// 创建自定义进度条
HWND hCustomProgress = CreateWindowExA(0, "CustomProgressClass", NULL,
WS_CHILD | WS_VISIBLE,
20, 60, windowWidth - 40, 30, hwndProgress, NULL, GetModuleHandle(NULL), NULL);
// 创建进度标签
HWND hLabel = CreateWindowExA(0, "Static", "准备下载...",
WS_CHILD | WS_VISIBLE | SS_CENTER,
20, 100, windowWidth - 40, 20, hwndProgress, NULL, GetModuleHandle(NULL), NULL);
// 创建速度标签
HWND hSpeedLabel = CreateWindowExA(0, "Static", "",
WS_CHILD | WS_VISIBLE | SS_CENTER,
20, 125, windowWidth - 40, 20, hwndProgress, NULL, GetModuleHandle(NULL), NULL);
// 保存窗口句柄到全局变量
g_progressWindow = hwndProgress;
g_customProgress = hCustomProgress;
g_progressLabel = hLabel;
g_speedLabel = hSpeedLabel;
// 设置窗口为前台窗口
SetForegroundWindow(hwndProgress);
return hwndProgress;
}
// 更新进度窗口
void UpdateProgressWindow(int percent, double downloadedMB, double totalMB, DWORD bytesDownloaded) {
if (!g_progressWindow || !IsWindow(g_progressWindow))
return;
// 更新进度状态
g_progressState.percent = percent;
g_progressState.downloadedMB = downloadedMB;
g_progressState.totalMB = totalMB;
g_progressState.indeterminate = (percent < 0);
// 计算下载速度
DWORD currentTime = GetTickCount();
DWORD elapsedTime = currentTime - g_progressState.lastUpdateTime;
if (elapsedTime >= 1000) { // 每秒更新一次速度
double bytesPerSec = ((double)(bytesDownloaded - g_progressState.lastDownloadedBytes) * 1000) / elapsedTime;
g_progressState.speed = bytesPerSec / 1024; // KB/s
g_progressState.lastUpdateTime = currentTime;
g_progressState.lastDownloadedBytes = bytesDownloaded;
// 更新速度标签
char speedBuffer[64];
if (g_progressState.speed > 1024) {
sprintf_s(speedBuffer, "下载速度: %.2f MB/s", g_progressState.speed / 1024);
}
else {
sprintf_s(speedBuffer, "下载速度: %.2f KB/s", g_progressState.speed);
}
SetWindowTextA(g_speedLabel, speedBuffer);
}
// 更新进度标签
char buffer[256];
if (totalMB > 0) {
sprintf_s(buffer, "下载进度: %.1f%% (%.2f MB / %.2f MB)",
(float)percent, downloadedMB, totalMB);
}
else {
sprintf_s(buffer, "已下载: %.2f MB", downloadedMB);
}
SetWindowTextA(g_progressLabel, buffer);
// 强制重绘自定义进度条
InvalidateRect(g_customProgress, NULL, TRUE);
// 处理消息
MSG msg;
while (PeekMessage(&msg, NULL, 0, 0, PM_REMOVE)) {
TranslateMessage(&msg);
DispatchMessage(&msg);
}
}
void SetConsoleStyle()
{
SetConsoleOutputCP(CP_UTF8);
SetConsoleTitle(L"KAKA漏打");
HANDLE hOut = GetStdHandle(STD_OUTPUT_HANDLE);
DWORD dwMode = 0;
GetConsoleMode(hOut, &dwMode);
if (!(dwMode & ENABLE_VIRTUAL_TERMINAL_PROCESSING))
{
dwMode |= ENABLE_VIRTUAL_TERMINAL_PROCESSING;
SetConsoleMode(hOut, dwMode);
}
}
int DENGLU;
static int Refresh()
{
while (true)
{
mem.RefreshAll();
Sleep(1000 * 60 * 15);
}
}
BOOL WINAPI ConsoleHandler(DWORD event) {
if (event == CTRL_CLOSE_EVENT) {
HWND Progman = FindWindowA("Progman", NULL);
HWND TrayWnd = FindWindowA("Shell_TrayWnd", NULL);
ShowWindow(Progman, SW_SHOW);
ShowWindow(TrayWnd, SW_SHOW);
return TRUE;
}
return FALSE;
}
std::string WideStringToMultiByte(const std::wstring& wideString) {
int size_needed = WideCharToMultiByte(CP_ACP, 0, &wideString[0], (int)wideString.size(), NULL, 0, NULL, NULL);
std::string multiByteString(size_needed, 0);
WideCharToMultiByte(CP_ACP, 0, &wideString[0], (int)wideString.size(), &multiByteString[0], size_needed, NULL, NULL);
return multiByteString;
}
// 设置控制台文本颜色
void SetConsoleColor(WORD color) {
HANDLE hConsoleOutput = GetStdHandle(STD_OUTPUT_HANDLE);
SetConsoleTextAttribute(hConsoleOutput, color);
}
// 显示菜单并高亮选中项
void DisplayMenu(const std::vector<std::string>& menuItems, int selectedItem, const std::string g) {
system("cls"); // 清除屏幕
printf("%s\n", g.c_str());
for (int i = 0; i < menuItems.size(); ++i) {
if (i == selectedItem) {
SetConsoleColor(FOREGROUND_RED | FOREGROUND_INTENSITY); // 设置红色高亮
}
else {
SetConsoleColor(FOREGROUND_GREEN | FOREGROUND_BLUE | FOREGROUND_RED); // 设置白色
}
printf("%s\n", menuItems[i].c_str());
}
}
bool DownloadFileWithProgress(const std::string& url, const std::string& output_file) {
// 检查当前目录写入权限
std::string testFile = "test_write_permission.tmp";
std::ofstream testWrite(testFile);
if (!testWrite.is_open()) {
std::cout << U8("错误:当前目录没有写入权限!") << std::endl;
return false;
}
testWrite.close();
DeleteFileA(testFile.c_str()); // 删除测试文件
// 如果目标文件已存在,尝试删除
if (GetFileAttributesA(output_file.c_str()) != INVALID_FILE_ATTRIBUTES) {
std::cout << U8("临时文件已存在,正在删除...") << std::endl;
if (!DeleteFileA(output_file.c_str())) {
std::cout << U8("警告:无法删除现有临时文件,错误代码: ") << GetLastError() << std::endl;
}
}
HINTERNET hSession = InternetOpenW(L"Updater/1.0", INTERNET_OPEN_TYPE_DIRECT, NULL, NULL, 0);
if (!hSession) {
std::cout << U8("Failed to open internet session, 错误代码: ") << GetLastError() << std::endl;
return false;
}
HINTERNET hConnect = InternetOpenUrlA(hSession, url.c_str(), NULL, 0, INTERNET_FLAG_RELOAD, 0);
if (!hConnect) {
std::cout << U8("Failed to open URL: ") << GetLastError() << std::endl;
InternetCloseHandle(hSession);
return false;
}
// 获取文件大小
DWORD dwFileSize = 0;
DWORD dwBufferSize = sizeof(dwFileSize);
HttpQueryInfo(hConnect, HTTP_QUERY_CONTENT_LENGTH | HTTP_QUERY_FLAG_NUMBER, &dwFileSize, &dwBufferSize, NULL);
// 创建进度窗口
CreateProgressWindow("下载更新");
double totalMB = dwFileSize / (1024.0 * 1024.0);
// 设置初始状态
g_progressState.indeterminate = (dwFileSize == 0);
g_progressState.totalMB = totalMB;
UpdateProgressWindow(0, 0, totalMB, 0);
std::cout << U8("文件大小: ") << std::fixed << std::setprecision(2) << totalMB << U8(" MB") << std::endl;
// 使用二进制模式和更兼容的方式创建文件
FILE* pFile = nullptr;
errno_t err = fopen_s(&pFile, output_file.c_str(), "wb");
if (err != 0 || !pFile) {
std::cout << U8("创建输出文件失败!") << std::endl;
std::cout << U8("文件路径: ") << output_file << std::endl;
std::cout << U8("fopen错误代码: ") << err << std::endl;
std::cout << U8("系统错误代码: ") << GetLastError() << std::endl;
// 获取当前工作目录
char currentDir[MAX_PATH];
GetCurrentDirectoryA(MAX_PATH, currentDir);
std::cout << U8("当前目录: ") << currentDir << std::endl;
InternetCloseHandle(hConnect);
InternetCloseHandle(hSession);
// 销毁进度窗口
if (g_progressWindow && IsWindow(g_progressWindow)) {
DestroyWindow(g_progressWindow);
g_progressWindow = NULL;
}
return false;
}
std::cout << U8("输出文件创建成功,开始下载...") << std::endl;
DWORD dwRead = 0;
std::vector<char> buffer(8192);
DWORD dwDownloaded = 0;
while (InternetReadFile(hConnect, buffer.data(), buffer.size(), &dwRead) && dwRead > 0) {
size_t written = fwrite(buffer.data(), 1, dwRead, pFile);
if (written != dwRead) {
std::cout << U8("写入文件失败! 期望写入: ") << dwRead << U8(", 实际写入: ") << written << std::endl;
fclose(pFile);
InternetCloseHandle(hConnect);
InternetCloseHandle(hSession);
// 销毁进度窗口
if (g_progressWindow && IsWindow(g_progressWindow)) {
DestroyWindow(g_progressWindow);
g_progressWindow = NULL;
}
return false;
}
dwDownloaded += dwRead;
double downloadedMB = dwDownloaded / (1024.0 * 1024.0);
if (dwFileSize > 0) {
double progress = (double)dwDownloaded / dwFileSize * 100.0;
// 更新进度窗口
UpdateProgressWindow((int)progress, downloadedMB, totalMB, dwDownloaded);
std::cout << "\r" << U8("下载进度: ") << std::fixed << std::setprecision(1) << progress << "% ("
<< std::setprecision(2) << downloadedMB << U8(" MB)");
}
else {
// 未知大小的情况
UpdateProgressWindow(-1, downloadedMB, 0, dwDownloaded);
std::cout << "\r" << U8("已下载: ") << std::fixed << std::setprecision(2)
<< downloadedMB << U8(" MB");
}
std::cout.flush();
}
fclose(pFile);
InternetCloseHandle(hConnect);
InternetCloseHandle(hSession);
std::cout << std::endl;
// 检查下载的文件大小
std::ifstream checkFile(output_file, std::ios::binary | std::ios::ate);
if (checkFile.is_open()) {
std::streamsize fileSize = checkFile.tellg();
std::cout << U8("下载完成,文件大小: ") << std::fixed << std::setprecision(2) << (fileSize / (1024.0 * 1024.0)) << U8(" MB") << std::endl;
checkFile.close();
if (fileSize == 0) {
std::cout << U8("错误:下载的文件为空!") << std::endl;
// 销毁进度窗口
if (g_progressWindow && IsWindow(g_progressWindow)) {
DestroyWindow(g_progressWindow);
g_progressWindow = NULL;
}
return false;
}
// 下载成功,销毁进度窗口
if (g_progressWindow && IsWindow(g_progressWindow)) {
// 更新进度为100%
UpdateProgressWindow(100, g_progressState.downloadedMB, g_progressState.totalMB, dwDownloaded);
// 短暂显示完成状态
Sleep(1000);
DestroyWindow(g_progressWindow);
g_progressWindow = NULL;
}
return true;
}
else {
std::cout << U8("无法验证下载文件!") << std::endl;
// 销毁进度窗口
if (g_progressWindow && IsWindow(g_progressWindow)) {
DestroyWindow(g_progressWindow);
g_progressWindow = NULL;
}
return false;
}
}
// 替换和启动新版本函数
bool ReplaceAndLaunchNewVersion(const std::string& newExePath) {
// 获取当前程序路径
char currentPath[MAX_PATH] = { 0 };
GetModuleFileNameA(NULL, currentPath, MAX_PATH);
std::string currentExe = currentPath;
// 获取程序的文件名部分
std::string exeName = currentExe.substr(currentExe.find_last_of("\\/") + 1);
std::cout << "准备替换当前程序..." << std::endl;
std::cout << "当前程序路径: " << currentExe << std::endl;
std::cout << "当前程序文件名: " << exeName << std::endl;
std::cout << "更新文件路径: " << newExePath << std::endl;
// 创建批处理文件
std::string batchPath = "update_program.bat";
std::ofstream batchFile(batchPath);
if (!batchFile.is_open()) {
std::cout << "创建更新脚本失败!" << std::endl;
return false;
}
batchFile << "@echo off" << std::endl;
batchFile << "echo " << "正在执行自动更新,等待即可,不要进行任何操作..." << std::endl;
batchFile << "timeout /t 3 /nobreak > NUL" << std::endl;
// 强制结束进程
batchFile << "taskkill /F /IM \"" << exeName << "\" > NUL 2>&1" << std::endl;
batchFile << "timeout /t 1 /nobreak > NUL" << std::endl;
// 尝试10次删除文件和移动新文件
batchFile << "set COUNTER=0" << std::endl;
batchFile << ":retry" << std::endl;
batchFile << "set /a COUNTER+=1" << std::endl;
batchFile << "if %COUNTER% GTR 10 goto failed" << std::endl;
batchFile << "echo " << "尝试替换文件 (尝试 %COUNTER%/10)..." << std::endl;
// 删除原始文件
batchFile << "del \"" << currentExe << "\" > NUL 2>&1" << std::endl;
batchFile << "if exist \"" << currentExe << "\" (" << std::endl;
batchFile << " echo " << "无法删除原始文件,等待2秒后重试..." << std::endl;
batchFile << " timeout /t 2 /nobreak > NUL" << std::endl;
batchFile << " goto retry" << std::endl;
batchFile << ")" << std::endl;
// 移动新文件到原始位置
batchFile << "move /Y \"" << newExePath << "\" \"" << currentExe << "\" > NUL 2>&1" << std::endl;
batchFile << "if not exist \"" << currentExe << "\" (" << std::endl;
batchFile << " echo " << "移动新文件失败,等待2秒后重试..." << std::endl;
batchFile << " timeout /t 2 /nobreak > NUL" << std::endl;
batchFile << " goto retry" << std::endl;
batchFile << ")" << std::endl;
// 成功更新
batchFile << "echo " << "更新成功!" << std::endl;
batchFile << "echo " << "正在启动新版本..." << std::endl;
batchFile << "start \"\" \"" << currentExe << "\"" << std::endl;
batchFile << "goto cleanup" << std::endl;
// 更新失败
batchFile << ":failed" << std::endl;
batchFile << "echo " << "更新失败!" << std::endl;
batchFile << "if exist \"" << newExePath << "\" (" << std::endl;
batchFile << " echo " << "尝试直接运行更新文件..." << std::endl;
batchFile << " start \"\" \"" << newExePath << "\"" << std::endl;
batchFile << ") else (" << std::endl;
batchFile << " echo " << "尝试启动原始程序..." << std::endl;
batchFile << " start \"\" \"" << currentExe << "\"" << std::endl;
batchFile << ")" << std::endl;
// 清理
batchFile << ":cleanup" << std::endl;
batchFile << "timeout /t 2 /nobreak > NUL" << std::endl;
batchFile << "del \"%~f0\"" << std::endl;
batchFile.close();
// 执行批处理文件
SHELLEXECUTEINFOA sei = { sizeof(sei) };
sei.fMask = SEE_MASK_NOCLOSEPROCESS;
sei.lpVerb = "open";
sei.lpFile = "cmd.exe";
sei.lpParameters = ("/c " + batchPath).c_str(); // 使用cmd.exe来执行批处理,确保窗口可见
sei.nShow = SW_SHOW; // 显示窗口
// 获取屏幕尺寸
int screenWidth = GetSystemMetrics(SM_CXSCREEN);
int screenHeight = GetSystemMetrics(SM_CYSCREEN);
// 计算CMD窗口位置 (屏幕中央)
HWND cmdWindow = NULL;
if (ShellExecuteExA(&sei)) {
std::cout << "更新程序已启动,当前程序即将退出..." << std::endl;
// 等待CMD窗口出现并居中它
Sleep(500); // 给CMD窗口一点时间启动
cmdWindow = FindWindowA("ConsoleWindowClass", NULL);
if (cmdWindow) {
RECT rect;
GetWindowRect(cmdWindow, &rect);
int width = rect.right - rect.left;
int height = rect.bottom - rect.top;
int posX = (screenWidth - width) / 2;
int posY = (screenHeight - height) / 2;
SetWindowPos(cmdWindow, HWND_TOP, posX, posY, width, height, SWP_SHOWWINDOW);
}
if (sei.hProcess) {
CloseHandle(sei.hProcess);
}
return true;
}
else {
std::cout << "启动更新程序失败,错误代码: " << GetLastError() << std::endl;
return false;
}
}
bool SaveVersionToFile(const std::string& version) {
std::ofstream versionFile("version.txt");
if (versionFile.is_open()) {
versionFile << version;
versionFile.close();
return true;
}
return false;
}
// 版本比较函数
bool IsVersionNewer(const std::string& serverVersion, const std::string& currentVersion) {
// 简单的版本号比较,您可以根据实际版本号格式调整此函数
try {
// 如果是纯数字版本号
int serverVer = std::stoi(serverVersion);
int currentVer = std::stoi(currentVersion);
return serverVer > currentVer;
}
catch (...) {
// 如果转换失败,使用字符串比较
return serverVersion > currentVersion;
}
}
bool DeleteFile(const std::string& file_path) {
return DeleteFileA(file_path.c_str()) != 0;
}
int main() {
if (!SetConsoleCtrlHandler(ConsoleHandler, TRUE))
{
return -1;
}
HWND hwnd = GetConsoleWindow();
if (hwnd != NULL)
{
ShowWindow(hwnd, SW_SHOW);//SW_SHOW显示 SW_HIDE隐藏
}
std::string HWID = MachineCodeGenerator::GenerateMachineCode();
SetConsoleStyle();
if (HWID == "") exit(0);
//Utils::Log(0, U8("KAKA漏打-----助手加载成功......"));
GameData.Config.Window.IsLogin = true;//
ShowWindow(hwnd, SW_SHOW);//SW_SHOW显示 SW_HIDE隐藏
Config::Load();
std::thread HackThread(Hack::Init);
Overlay::Init(0, DrawMain);
while (true)
{
if (GameData.Config.Window.Main) {
Sleep(500);
continue;
}
break;
}
return 0;
}