This repository was archived by the owner on Jul 19, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathDownloadUtil.h
More file actions
448 lines (388 loc) · 13.5 KB
/
DownloadUtil.h
File metadata and controls
448 lines (388 loc) · 13.5 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
#pragma once
#include <direct.h>
struct progInfo {
HWND hWnd;
uint32_t time;
curl_off_t size;
};
LONG canceled = FALSE;
int curlProgress(void *p, curl_off_t dltotal, curl_off_t dlnow, curl_off_t ultotal, curl_off_t ulnow)
{
// canceled_old = canceled; if (canceled == TRUE) canceled = FALSE; return canceled_old;
if (InterlockedCompareExchange(&canceled, FALSE, TRUE) == TRUE) // Cancel
return 1;
if (dltotal != 0 && ultotal != 0)
{
progInfo *info = (progInfo *)p;
uint32_t now = GetTickCount(), span_ms = now - info->time;
if (span_ms >= 500) // Update every 0.5s
{
info->time = now;
curl_off_t dlsize = dlnow > ulnow ? dlnow : ulnow;
curl_off_t current_speed = (dlsize - info->size) / span_ms * CURL_OFF_T_C(1000);
info->size = dlsize;
PostMessage(info->hWnd, WM_PROGRESS, (long)(dlnow * CURL_OFF_T_C(100) / dltotal), (long)(current_speed));
}
}
return 0;
}
bool Extract7z(const char *fileName)
{
#define kInputBufSize ((size_t)1 << 18)
static const ISzAlloc szAlloc = { SzAlloc, SzFree };
struct mycompare { bool operator()(const wchar_t *a, const wchar_t *b) const { return _wcsicmp(a, b) < 0; } };
static const std::set<const wchar_t*, mycompare> excludeFilenames = { L"Thumbs.db" };
CFileInStream archiveStream;
if (InFile_Open(&archiveStream.file, fileName) == 0)
{
CLookToRead2 lookStream;
FileInStream_CreateVTable(&archiveStream);
LookToRead2_CreateVTable(&lookStream, False);
lookStream.buf = NULL;
SRes res = SZ_OK;
{
lookStream.buf = (Byte *)ISzAlloc_Alloc(&szAlloc, kInputBufSize);
if (!lookStream.buf)
res = SZ_ERROR_MEM;
else
{
lookStream.bufSize = kInputBufSize;
lookStream.realStream = &archiveStream.vt;
LookToRead2_Init(&lookStream);
}
}
static bool crcTableGenerated = false;
if (!crcTableGenerated)
{
CrcGenerateTable();
crcTableGenerated = true;
}
CSzArEx db;
SzArEx_Init(&db);
if (res == SZ_OK)
{
res = SzArEx_Open(&db, &lookStream.vt, &szAlloc, &szAlloc);
if (res == SZ_OK)
{
uint32_t blockIndex = 0xFFFFFFFF;
Byte *outBuffer = 0;
size_t outBufferSize = 0;
size_t buflen = 32;
wchar_t *nameBuf = (wchar_t *)malloc(buflen * sizeof(wchar_t));
for (size_t i = 0; i < db.NumFiles; i++)
{
size_t namelen = SzArEx_GetFileNameUtf16(&db, i, NULL);
if (namelen > buflen)
{
free(nameBuf);
buflen = namelen;
nameBuf = (wchar_t *)malloc(buflen * sizeof(wchar_t));
if (nameBuf == nullptr)
{
res = SZ_ERROR_MEM;
break;
}
}
SzArEx_GetFileNameUtf16(&db, i, (UInt16 *)nameBuf);
if (!PathIsRelativeW(nameBuf) || wcsstr(nameBuf, L"../") != nullptr)
continue;
/*for (size_t j = 0; nameBuf[j] != 0; j++)
if (nameBuf[j] == L'/')
{
nameBuf[j] = 0;
_wmkdir(nameBuf);
nameBuf[j] = L'\\';
}*/
bool isDir = SzArEx_IsDir(&db, i);
if (isDir)
{
CreateDirectoryW(nameBuf, nullptr);
continue;
}
else
{
wchar_t *filename = wcsrchr(nameBuf, L'/');
if (filename)
filename++;
else
filename = nameBuf;
if (excludeFilenames.find(filename) != excludeFilenames.cend())
continue;
size_t offset = 0;
size_t outSizeProcessed = 0;
res = SzArEx_Extract(&db, &lookStream.vt, i, &blockIndex, &outBuffer, &outBufferSize, &offset, &outSizeProcessed, &szAlloc, &szAlloc);
if (res != SZ_OK)
break;
FILE *outFile = _wfopen(nameBuf, L"wb+");
if (outFile == nullptr)
{
res = SZ_ERROR_WRITE;
break;
}
size_t outSize = fwrite(outBuffer + offset, 1, outSizeProcessed, outFile);
fclose(outFile);
if (outSize != outSizeProcessed)
{
res = SZ_ERROR_WRITE;
break;
}
}
}
ISzAlloc_Free(&szAlloc, outBuffer);
free(nameBuf);
SzArEx_Free(&db, &szAlloc);
}
ISzAlloc_Free(&szAlloc, lookStream.buf);
}
File_Close(&archiveStream.file);
return res == SZ_OK;
}
return false;
}
using DownloadFunc = void(*)(HWND hTDWnd, HWND hMsgWnd);
HRESULT CALLBACK TaskDialogCallback(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam, LONG_PTR lpRefData)
{
switch (msg)
{
case TDN_CREATED:
PostMessageW(hWnd, TDM_SET_PROGRESS_BAR_MARQUEE, TRUE, 0);
HWND hMsgWnd = CreateWindowW(WC_STATICW, nullptr, WS_CHILD, 0, 0, 0, 0, hWnd, nullptr, g_hInst, nullptr);
SetWindowLongPtrW(hMsgWnd, GWLP_USERDATA, reinterpret_cast<LONG_PTR>(hWnd));
static bool progBarStatus;
progBarStatus = false;
static LONG_PTR prevWndProc;
prevWndProc = SetWindowLongPtrW(hMsgWnd, GWLP_WNDPROC, reinterpret_cast<LONG_PTR>(static_cast<WNDPROC>(
[](HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam) -> LRESULT {
if (message == WM_PROGRESS)
{
HWND hTDWnd = reinterpret_cast<HWND>(GetWindowLongPtrW(hWnd, GWLP_USERDATA));
if (wParam != -1)
{
if (!progBarStatus)
{
SendMessageW(hTDWnd, TDM_SET_MARQUEE_PROGRESS_BAR, FALSE, 0);
progBarStatus = true;
}
SendMessageW(hTDWnd, TDM_SET_PROGRESS_BAR_POS, wParam, 0);
const wchar_t *unit = L"B/s";
float speed = static_cast<float>(lParam);
if (speed > 1024)
{
speed = speed / 1024; // KB
unit = L"KB/s";
if (speed > 1024)
{
speed = speed / 1024; // MB
unit = L"MB/s";
}
}
wchar_t speedText[32];
swprintf_s(speedText, L"%.1f %s", speed, unit);
SendMessageW(hTDWnd, TDM_SET_ELEMENT_TEXT, TDE_CONTENT, reinterpret_cast<LPARAM>(speedText));
}
else
{
SendMessageW(hTDWnd, TDM_SET_PROGRESS_BAR_STATE, PBST_ERROR, 0);
SendMessageW(hTDWnd, TDM_UPDATE_ICON, TDIE_ICON_MAIN, reinterpret_cast<LPARAM>(TD_ERROR_ICON));
SendMessageW(hTDWnd, TDM_SET_ELEMENT_TEXT, TDE_CONTENT, reinterpret_cast<LPARAM>(L"Download failed."));
}
return 0;
}
return CallWindowProcW(reinterpret_cast<WNDPROC>(prevWndProc), hWnd, message, wParam, lParam);
})));
canceled = FALSE;
reinterpret_cast<DownloadFunc>(lpRefData)(hWnd, hMsgWnd);
break;
}
return S_OK;
}
bool DownloadVCMPGame(const char *version, const char *password)
{
struct downloadInfo {
const char *version;
const char *password;
};
downloadInfo dlInfo = { version, password };
static downloadInfo* pdlInfo;
pdlInfo = &dlInfo;
static bool success;
success = false;
TASKDIALOGCONFIG tdConfig = { sizeof(tdConfig) };
tdConfig.hwndParent = g_hMainWnd;
tdConfig.hInstance = g_hInst;
tdConfig.dwFlags = TDF_ALLOW_DIALOG_CANCELLATION | TDF_SHOW_MARQUEE_PROGRESS_BAR | TDF_POSITION_RELATIVE_TO_WINDOW;
tdConfig.dwCommonButtons = TDCBF_CANCEL_BUTTON;
tdConfig.pszWindowTitle = L"Downloading";
tdConfig.pfCallback = TaskDialogCallback;
tdConfig.lpCallbackData = reinterpret_cast<LONG_PTR>(static_cast<DownloadFunc>([](HWND hTDWnd, HWND hMsgWnd) {
std::thread([](HWND hTDWnd, HWND hMsgWnd) {
CURL *curl = curl_easy_init();
if (curl)
{
std::string url = g_browserSettings.gameUpdateURL + "/download";
curl_easy_setopt(curl, CURLOPT_URL, url.c_str());
curl_easy_setopt(curl, CURLOPT_PROXY, g_browserSettings.proxy.empty() ? nullptr : g_browserSettings.proxy.c_str());
SetCurrentDirectoryW(g_exePath.c_str());
char fileName[16];
srand(GetTickCount());
snprintf(fileName, sizeof(fileName), "tmp%.4X.7z", rand());
FILE *file = fopen(fileName, "wb");
if (file == nullptr)
{
curl_easy_cleanup(curl);
PostMessageW(hMsgWnd, WM_PROGRESS, -1, 0);
return 0;
}
curl_easy_setopt(curl, CURLOPT_WRITEDATA, file);
progInfo progress = { hMsgWnd, GetTickCount(), 0 };
curl_easy_setopt(curl, CURLOPT_NOPROGRESS, false);
curl_easy_setopt(curl, CURLOPT_XFERINFODATA, &progress);
curl_easy_setopt(curl, CURLOPT_XFERINFOFUNCTION, curlProgress);
rapidjson::StringBuffer json;
rapidjson::Writer<rapidjson::StringBuffer> writer(json);
writer.StartObject();
writer.Key("password");
writer.String(pdlInfo->password);
writer.Key("version");
writer.String(pdlInfo->version);
writer.EndObject();
struct curl_httppost* post = nullptr;
struct curl_httppost* last = nullptr;
curl_formadd(&post, &last, CURLFORM_COPYNAME, "json", CURLFORM_PTRCONTENTS, json.GetString(), CURLFORM_END);
curl_easy_setopt(curl, CURLOPT_HTTPPOST, post);
CURLcode ret = curl_easy_perform(curl);
curl_easy_cleanup(curl);
fclose(file);
if (ret == CURLE_OK)
{
_mkdir(pdlInfo->version);
SetCurrentDirectoryA(pdlInfo->version);
char fileName_[16];
strcpy(fileName_, "../");
strcat(fileName_, fileName);
success = Extract7z(fileName_);
SetCurrentDirectoryA("..");
}
remove(fileName);
}
if (success)
PostMessageW(hTDWnd, WM_CLOSE, 0, 0);
else
PostMessageW(hMsgWnd, WM_PROGRESS, -1, 0);
return 0;
}, hTDWnd, hMsgWnd).detach();
}));
TaskDialogIndirect(&tdConfig, nullptr, nullptr, nullptr);
InterlockedExchange(&canceled, TRUE); // canceled = TRUE;
return success;
}
struct updateInfo
{
std::string version;
std::string date;
std::string downloadLink;
};
updateInfo g_updateInfo;
void CheckBrowserUpdate()
{
bool result = false;
std::string data;
data.reserve(256);
CURLcode curlRet = CurlRequset("https://ysc3839.github.io/VCMPBrowser/update/version.json", data, "VCMP/0.4");
if (curlRet == CURLE_OK)
{
do {
rapidjson::Document dom;
if (dom.Parse(data).HasParseError() || !dom.IsObject())
break;
auto version = dom.FindMember("version");
if (version == dom.MemberEnd() || !version->value.IsString())
break;
g_updateInfo.version = std::string(version->value.GetString(), version->value.GetStringLength());
auto date = dom.FindMember("date");
if (date == dom.MemberEnd() || !date->value.IsString())
break;
g_updateInfo.date = std::string(date->value.GetString(), date->value.GetStringLength());
auto downloadLink = dom.FindMember("downloadLink");
if (downloadLink == dom.MemberEnd() || !downloadLink->value.IsString())
break;
g_updateInfo.downloadLink = std::string(downloadLink->value.GetString(), downloadLink->value.GetStringLength());
result = true;
} while (0);
}
PostMessage(g_hMainWnd, WM_UPDATE, result, 0);
}
void UpdateBrowser()
{
if (g_updateInfo.version != VERSION) // Version not equal
{
SendMessage(g_hWndStatusBar, SB_SETTEXT, 0, (LPARAM)LoadStr(L"Found new browser version!", IDS_FOUNDUPDATE));
wchar_t message[512];
swprintf_s(message, L"A new version of VCMPBrowser is available!\nVersion: %hs\nRelease Date: %hs\nDo you want to update?", g_updateInfo.version.c_str(), g_updateInfo.date.c_str());
if (TDMessageBox(g_hMainWnd, message, LoadStr(L"Information", IDS_INFORMATION), MB_YESNO | MB_ICONINFORMATION) != IDYES)
return;
TASKDIALOGCONFIG tdConfig = { sizeof(tdConfig) };
tdConfig.hwndParent = g_hMainWnd;
tdConfig.hInstance = g_hInst;
tdConfig.dwFlags = TDF_ALLOW_DIALOG_CANCELLATION | TDF_SHOW_MARQUEE_PROGRESS_BAR | TDF_POSITION_RELATIVE_TO_WINDOW;
tdConfig.dwCommonButtons = TDCBF_CANCEL_BUTTON;
tdConfig.pszWindowTitle = L"Downloading";
tdConfig.pfCallback = TaskDialogCallback;
tdConfig.lpCallbackData = reinterpret_cast<LONG_PTR>(static_cast<DownloadFunc>([](HWND hTDWnd, HWND hMsgWnd) {
std::thread([](HWND hTDWnd, HWND hMsgWnd) {
CURL *curl = curl_easy_init();
if (curl)
{
curl_easy_setopt(curl, CURLOPT_URL, g_updateInfo.downloadLink.c_str());
curl_easy_setopt(curl, CURLOPT_PROXY, g_browserSettings.proxy.empty() ? nullptr : g_browserSettings.proxy.c_str());
SetCurrentDirectoryW(g_exePath.c_str());
char fileName[16];
srand(GetTickCount());
snprintf(fileName, sizeof(fileName), "tmp%.4X.7z", rand());
FILE *file = fopen(fileName, "wb");
if (file == nullptr)
{
curl_easy_cleanup(curl);
PostMessageW(hMsgWnd, WM_PROGRESS, -1, 0);
return 0;
}
curl_easy_setopt(curl, CURLOPT_WRITEDATA, file);
progInfo progress = { hMsgWnd, GetTickCount(), 0 };
curl_easy_setopt(curl, CURLOPT_NOPROGRESS, false);
curl_easy_setopt(curl, CURLOPT_XFERINFODATA, &progress);
curl_easy_setopt(curl, CURLOPT_XFERINFOFUNCTION, curlProgress);
CURLcode ret = curl_easy_perform(curl);
curl_easy_cleanup(curl);
fclose(file);
if (ret == CURLE_OK)
{
wchar_t newFile[MAX_PATH];
wcscpy_s(newFile, _wpgmptr);
wcscat_s(newFile, L".old");
MoveFileEx(_wpgmptr, newFile, MOVEFILE_REPLACE_EXISTING);
Extract7z(fileName);
remove(fileName);
STARTUPINFO si = { sizeof(si) };
GetStartupInfo(&si);
PROCESS_INFORMATION pi;
if (CreateProcess(_wpgmptr, nullptr, nullptr, nullptr, FALSE, 0, nullptr, nullptr, &si, &pi))
{
CloseHandle(pi.hThread);
CloseHandle(pi.hProcess);
PostMessageW(hTDWnd, WM_CLOSE, 0, 0);
PostMessageW(g_hMainWnd, WM_CLOSE, 0, 0);
return 0;
}
}
remove(fileName);
}
PostMessageW(hMsgWnd, WM_PROGRESS, -1, 0);
return 0;
}, hTDWnd, hMsgWnd).detach();
}));
TaskDialogIndirect(&tdConfig, nullptr, nullptr, nullptr);
InterlockedExchange(&canceled, TRUE); // canceled = TRUE;
}
else
SendMessage(g_hWndStatusBar, SB_SETTEXT, 0, (LPARAM)LoadStr(L"No update found.", IDS_NOUPDATE));
}