-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSpawningToolImport.cpp
More file actions
443 lines (403 loc) · 12.9 KB
/
SpawningToolImport.cpp
File metadata and controls
443 lines (403 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
#include "SpawningToolImport.h"
#include "BuildOrderPasteParser.h"
#ifndef WIN32_LEAN_AND_MEAN
#define WIN32_LEAN_AND_MEAN
#endif
#include <Windows.h>
#include <winhttp.h>
#pragma comment(lib, "winhttp.lib")
#include <algorithm>
#include <cctype>
#include <cstdio>
#include <sstream>
#include <vector>
#include <cwctype>
namespace
{
bool IsAsciiSpace(unsigned char c)
{
return c == ' ' || c == '\t' || c == '\r' || c == '\n' || c == '\f' || c == '\v';
}
void TrimInPlace(std::string& s)
{
while (!s.empty() && IsAsciiSpace(static_cast<unsigned char>(s.back())))
s.pop_back();
size_t i0 = 0;
while (i0 < s.size() && IsAsciiSpace(static_cast<unsigned char>(s[i0])))
++i0;
if (i0 > 0)
s.erase(0, i0);
}
std::wstring Utf8ToWide(const std::string& sUtf8)
{
if (sUtf8.empty())
return {};
const int n = MultiByteToWideChar(CP_UTF8, MB_ERR_INVALID_CHARS, sUtf8.c_str(), static_cast<int>(sUtf8.size()), nullptr, 0);
if (n <= 0)
return {};
std::wstring w;
w.resize(static_cast<size_t>(n));
if (MultiByteToWideChar(CP_UTF8, MB_ERR_INVALID_CHARS, sUtf8.c_str(), static_cast<int>(sUtf8.size()), w.data(), n) <= 0)
return {};
return w;
}
std::string WideToUtf8(const std::wstring& w)
{
if (w.empty())
return {};
const int n = WideCharToMultiByte(CP_UTF8, 0, w.c_str(), static_cast<int>(w.size()), nullptr, 0, nullptr, nullptr);
if (n <= 0)
return {};
std::string s;
s.resize(static_cast<size_t>(n));
if (WideCharToMultiByte(CP_UTF8, 0, w.c_str(), static_cast<int>(w.size()), s.data(), n, nullptr, nullptr) <= 0)
return {};
return s;
}
bool IsAllowedSpawningToolHost(const std::wstring& hostLower)
{
static const wchar_t kSuffix[] = L".spawningtool.com";
constexpr size_t nSuffix = sizeof(kSuffix) / sizeof(kSuffix[0]) - 1;
if (hostLower.size() < nSuffix)
return false;
return hostLower.compare(hostLower.size() - nSuffix, nSuffix, kSuffix) == 0;
}
bool UrlLooksLikeSpawningToolBuild(const std::string& sUrl)
{
std::string u = sUrl;
TrimInPlace(u);
if (u.size() < 20)
return false;
std::string low;
low.reserve(u.size());
for (unsigned char c : u)
low.push_back(static_cast<char>(std::tolower(c)));
if (low.find("spawningtool.com") == std::string::npos)
return false;
if (low.find("/build/") == std::string::npos)
return false;
return true;
}
bool HttpsGetUtf8(const std::wstring& wUrl, std::string& outUtf8Body, std::wstring& outWinErr)
{
outUtf8Body.clear();
outWinErr.clear();
URL_COMPONENTSW comp{};
comp.dwStructSize = sizeof(comp);
comp.dwSchemeLength = static_cast<DWORD>(-1);
comp.dwHostNameLength = static_cast<DWORD>(-1);
comp.dwUrlPathLength = static_cast<DWORD>(-1);
comp.dwExtraInfoLength = static_cast<DWORD>(-1);
if (!WinHttpCrackUrl(wUrl.c_str(), static_cast<DWORD>(wUrl.size()), 0, &comp))
{
outWinErr = L"URL invalida.";
return false;
}
if (!comp.lpszHostName || comp.dwHostNameLength == 0)
{
outWinErr = L"Sem host na URL.";
return false;
}
std::wstring host(comp.lpszHostName, comp.lpszHostName + comp.dwHostNameLength);
std::wstring path(comp.lpszUrlPath, comp.lpszUrlPath + comp.dwUrlPathLength);
if (comp.lpszExtraInfo && comp.dwExtraInfoLength > 0)
path.append(comp.lpszExtraInfo, comp.lpszExtraInfo + comp.dwExtraInfoLength);
std::wstring hostLower = host;
for (wchar_t& ch : hostLower)
ch = static_cast<wchar_t>(std::towlower(ch));
if (!IsAllowedSpawningToolHost(hostLower))
{
outWinErr = L"Apenas URLs em spawningtool.com (build orders).";
return false;
}
const bool bHttps = comp.nScheme == INTERNET_SCHEME_HTTPS;
const INTERNET_PORT port = bHttps ? INTERNET_DEFAULT_HTTPS_PORT : INTERNET_DEFAULT_HTTP_PORT;
const DWORD flags = bHttps ? WINHTTP_FLAG_SECURE : 0;
HINTERNET hSession = WinHttpOpen(L"SC2Hud/1.0 (import build)", WINHTTP_ACCESS_TYPE_DEFAULT_PROXY, WINHTTP_NO_PROXY_NAME, WINHTTP_NO_PROXY_BYPASS, 0);
if (!hSession)
{
outWinErr = L"WinHttpOpen falhou.";
return false;
}
HINTERNET hConnect = WinHttpConnect(hSession, host.c_str(), port, 0);
if (!hConnect)
{
WinHttpCloseHandle(hSession);
outWinErr = L"WinHttpConnect falhou.";
return false;
}
HINTERNET hRequest = WinHttpOpenRequest(hConnect, L"GET", path.empty() ? L"/" : path.c_str(), nullptr, WINHTTP_NO_REFERER, WINHTTP_DEFAULT_ACCEPT_TYPES, flags);
if (!hRequest)
{
WinHttpCloseHandle(hConnect);
WinHttpCloseHandle(hSession);
outWinErr = L"WinHttpOpenRequest falhou.";
return false;
}
const wchar_t* extraHeaders = L"Accept: text/html,application/xhtml+xml\r\nAccept-Language: en-US,en;q=0.9\r\n";
BOOL bOk = WinHttpSendRequest(hRequest, extraHeaders, static_cast<DWORD>(-1), WINHTTP_NO_REQUEST_DATA, 0, 0, 0);
if (!bOk)
{
WinHttpCloseHandle(hRequest);
WinHttpCloseHandle(hConnect);
WinHttpCloseHandle(hSession);
outWinErr = L"WinHttpSendRequest falhou.";
return false;
}
bOk = WinHttpReceiveResponse(hRequest, nullptr);
if (!bOk)
{
WinHttpCloseHandle(hRequest);
WinHttpCloseHandle(hConnect);
WinHttpCloseHandle(hSession);
outWinErr = L"WinHttpReceiveResponse falhou.";
return false;
}
DWORD dwStatus = 0;
DWORD dwStatusSize = sizeof(dwStatus);
if (!WinHttpQueryHeaders(hRequest, WINHTTP_QUERY_STATUS_CODE | WINHTTP_QUERY_FLAG_NUMBER, WINHTTP_HEADER_NAME_BY_INDEX, &dwStatus, &dwStatusSize, WINHTTP_NO_HEADER_INDEX))
{
WinHttpCloseHandle(hRequest);
WinHttpCloseHandle(hConnect);
WinHttpCloseHandle(hSession);
outWinErr = L"Resposta HTTP sem status.";
return false;
}
if (dwStatus < 200 || dwStatus >= 300)
{
wchar_t wbuf[64];
_snwprintf_s(wbuf, _TRUNCATE, L"HTTP %lu.", static_cast<unsigned long>(dwStatus));
outWinErr = wbuf;
WinHttpCloseHandle(hRequest);
WinHttpCloseHandle(hConnect);
WinHttpCloseHandle(hSession);
return false;
}
std::vector<char> vAcc;
for (;;)
{
DWORD dwAvail = 0;
if (!WinHttpQueryDataAvailable(hRequest, &dwAvail))
break;
if (dwAvail == 0)
break;
const size_t iOld = vAcc.size();
if (iOld + dwAvail > 8 * 1024 * 1024)
{
outWinErr = L"Pagina demasiado grande.";
WinHttpCloseHandle(hRequest);
WinHttpCloseHandle(hConnect);
WinHttpCloseHandle(hSession);
return false;
}
vAcc.resize(iOld + dwAvail);
DWORD dwRead = 0;
if (!WinHttpReadData(hRequest, vAcc.data() + iOld, dwAvail, &dwRead))
break;
vAcc.resize(iOld + dwRead);
}
WinHttpCloseHandle(hRequest);
WinHttpCloseHandle(hConnect);
WinHttpCloseHandle(hSession);
if (vAcc.empty())
{
outWinErr = L"Corpo vazio.";
return false;
}
int nLen = MultiByteToWideChar(CP_UTF8, MB_ERR_INVALID_CHARS, vAcc.data(), static_cast<int>(vAcc.size()), nullptr, 0);
std::wstring wHtml;
if (nLen > 0)
{
wHtml.resize(static_cast<size_t>(nLen));
MultiByteToWideChar(CP_UTF8, MB_ERR_INVALID_CHARS, vAcc.data(), static_cast<int>(vAcc.size()), wHtml.data(), nLen);
}
else
{
nLen = MultiByteToWideChar(CP_ACP, 0, vAcc.data(), static_cast<int>(vAcc.size()), nullptr, 0);
if (nLen <= 0)
{
outWinErr = L"Codificacao da pagina desconhecida.";
return false;
}
wHtml.resize(static_cast<size_t>(nLen));
MultiByteToWideChar(CP_ACP, 0, vAcc.data(), static_cast<int>(vAcc.size()), wHtml.data(), nLen);
}
outUtf8Body = WideToUtf8(wHtml);
return !outUtf8Body.empty();
}
void StripTagsInPlace(std::string& s)
{
std::string out;
out.reserve(s.size());
bool bInTag = false;
for (unsigned char c : s)
{
if (c == '<')
bInTag = true;
else if (c == '>')
bInTag = false;
else if (!bInTag)
out.push_back(static_cast<char>(c));
}
s.swap(out);
}
void DecodeBasicEntities(std::string& s)
{
for (;;)
{
const size_t iAmp = s.find('&');
if (iAmp == std::string::npos)
break;
size_t iSemi = s.find(';', iAmp + 1);
if (iSemi == std::string::npos)
break;
const std::string ent = s.substr(iAmp + 1, iSemi - iAmp - 1);
std::string rep;
if (ent == "amp")
rep = "&";
else if (ent == "lt")
rep = "<";
else if (ent == "gt")
rep = ">";
else if (ent == "quot")
rep = "\"";
else if (ent == "nbsp")
rep = " ";
else
break;
s.replace(iAmp, iSemi - iAmp + 1, rep);
}
}
bool IsAllDigits(const std::string& s)
{
if (s.empty())
return false;
for (unsigned char c : s)
{
if (!std::isdigit(c))
return false;
}
return true;
}
std::string ExtractH1Plain(const std::string& html)
{
const std::string tagOpen = "<h1";
size_t iH = html.find(tagOpen);
if (iH == std::string::npos)
return {};
const size_t iGt = html.find('>', iH + tagOpen.size());
if (iGt == std::string::npos)
return {};
const size_t iEnd = html.find("</h1>", iGt + 1);
if (iEnd == std::string::npos)
return {};
std::string inner = html.substr(iGt + 1, iEnd - iGt - 1);
StripTagsInPlace(inner);
DecodeBasicEntities(inner);
TrimInPlace(inner);
return inner;
}
bool BuildPasteFromSpawningToolHtml(const std::string& html, std::string& outPaste, std::string& outErr)
{
outPaste.clear();
outErr.clear();
const std::string sTitle = ExtractH1Plain(html);
size_t iPos = 0;
std::vector<std::string> vLines;
while (iPos < html.size())
{
const size_t iTr = html.find("<tr", iPos);
if (iTr == std::string::npos)
break;
const size_t iTrEnd = html.find("</tr>", iTr);
if (iTrEnd == std::string::npos)
break;
std::string row = html.substr(iTr, iTrEnd - iTr + 5);
iPos = iTrEnd + 5;
if (row.find("<th") != std::string::npos)
continue;
std::vector<std::string> vCells;
size_t cPos = 0;
while (cPos < row.size())
{
const size_t iTd = row.find("<td", cPos);
if (iTd == std::string::npos)
break;
const size_t iGt = row.find('>', iTd + 3);
if (iGt == std::string::npos)
break;
const size_t iClose = row.find("</td>", iGt + 1);
if (iClose == std::string::npos)
break;
std::string cell = row.substr(iGt + 1, iClose - iGt - 1);
StripTagsInPlace(cell);
DecodeBasicEntities(cell);
TrimInPlace(cell);
vCells.push_back(std::move(cell));
cPos = iClose + 5;
}
if (vCells.size() < 3)
continue;
if (!IsAllDigits(vCells[0]))
continue;
std::ostringstream line;
line << vCells[0] << " " << vCells[1] << " " << vCells[2];
if (vCells.size() >= 4 && !vCells[3].empty())
line << " " << vCells[3];
vLines.push_back(line.str());
}
if (vLines.empty())
{
outErr = "Nao foi encontrada tabela de build order no HTML. Copia manual ou verifica a pagina.";
return false;
}
std::ostringstream paste;
if (!sTitle.empty())
paste << sTitle << "\n\n";
for (const std::string& ln : vLines)
paste << ln << "\n";
outPaste = paste.str();
return true;
}
} // namespace
bool ImportBuildOrderFromSpawningToolUrl(const std::string& sUrlUtf8, BuildOrder& outOrder, std::string& outError)
{
outError.clear();
outOrder.Clear();
std::string sUrl = sUrlUtf8;
TrimInPlace(sUrl);
if (sUrl.empty())
{
outError = "URL vazia.";
return false;
}
if (sUrl.find("://") == std::string::npos)
sUrl.insert(0, "https://");
if (!UrlLooksLikeSpawningToolBuild(sUrl))
{
outError = "Use um link do Spawning Tool, ex.: https://lotv.spawningtool.com/build/123456/";
return false;
}
std::wstring wUrl = Utf8ToWide(sUrl);
if (wUrl.empty())
{
outError = "URL invalida (UTF-8).";
return false;
}
std::string html;
std::wstring wErr;
if (!HttpsGetUtf8(wUrl, html, wErr))
{
if (wErr.empty())
outError = "Falha ao descarregar a pagina.";
else
outError = WideToUtf8(wErr);
return false;
}
std::string paste;
if (!BuildPasteFromSpawningToolHtml(html, paste, outError))
return false;
if (!ParseWebsiteBuildOrderPaste(paste, outOrder, outError))
return false;
return true;
}