-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstartup_update_shared.cpp
More file actions
369 lines (311 loc) · 11 KB
/
startup_update_shared.cpp
File metadata and controls
369 lines (311 loc) · 11 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
#include "pch.h"
#include "startup_update_internal.h"
#include "http_client.h"
#include <cctype>
#include <cwctype>
#include <filesystem>
#include <iomanip>
#include <iostream>
#include <optional>
#include <sstream>
#include <string>
#include <string_view>
#include <vector>
namespace mapupdater::startup {
std::wstring SafeUtf8ToWide(const std::string &value) {
const std::wstring wide = runtime::Utf8ToWide(value);
if (!wide.empty() || value.empty()) {
return wide;
}
std::wstring fallback;
fallback.reserve(value.size());
for (unsigned char ch : value) {
fallback.push_back(static_cast<wchar_t>(ch));
}
return fallback;
}
std::string WideToAsciiLossy(const std::wstring &value) {
std::string out;
out.reserve(value.size());
for (wchar_t ch : value) {
out.push_back((ch >= 0 && ch <= 0x7F) ? static_cast<char>(ch) : '?');
}
return out;
}
void DebugLog(const AppConfig &config, const std::string &message) {
if (config.debug_enabled) {
std::cout << message << std::endl;
}
}
namespace {
bool CaseInsensitiveEquals(std::wstring_view a, std::wstring_view b) {
if (a.size() != b.size()) {
return false;
}
for (size_t i = 0; i < a.size(); ++i) {
if (std::towlower(a[i]) != std::towlower(b[i])) {
return false;
}
}
return true;
}
bool CaseInsensitiveContains(std::wstring_view haystack, std::wstring_view needle) {
if (needle.empty()) {
return true;
}
if (haystack.size() < needle.size()) {
return false;
}
for (size_t i = 0; i + needle.size() <= haystack.size(); ++i) {
if (CaseInsensitiveEquals(haystack.substr(i, needle.size()), needle)) {
return true;
}
}
return false;
}
bool PathEqualsCaseInsensitive(const std::filesystem::path &a,
const std::filesystem::path &b) {
return CaseInsensitiveEquals(a.wstring(), b.wstring());
}
uint32_t OwnerFlagForChannel(MapChannel channel) {
return (channel == MapChannel::Dota) ? kDownloadOwnerDota : kDownloadOwnerLod;
}
std::wstring ChannelLabel(MapChannel channel) {
return (channel == MapChannel::Dota) ? L"Dota" : L"LoD";
}
bool HasW3xExtension(const std::filesystem::path &path) {
const std::wstring ext = path.extension().wstring();
return CaseInsensitiveEquals(ext, L".w3x");
}
bool SizeMatches(const std::filesystem::path &path, uint64_t expected_size,
const AppConfig &config) {
if (!config.checksize_enabled || expected_size == 0) {
return true;
}
std::error_code ec;
const uint64_t actual = std::filesystem::file_size(path, ec);
if (ec) {
return false;
}
return actual >= expected_size;
}
bool FileExistsExactInRoot(const std::filesystem::path &root,
const std::wstring &file_name, uint64_t expected_size,
const AppConfig &config) {
if (file_name.empty()) {
return false;
}
std::error_code ec;
if (!std::filesystem::exists(root, ec) || ec) {
return false;
}
for (const auto &entry : std::filesystem::directory_iterator(root, ec)) {
if (ec) {
return false;
}
if (!entry.is_regular_file(ec) || ec) {
continue;
}
if (!CaseInsensitiveEquals(entry.path().filename().wstring(), file_name)) {
continue;
}
if (SizeMatches(entry.path(), expected_size, config)) {
return true;
}
}
return false;
}
std::filesystem::path ResolveMapDownloadDir(const std::filesystem::path &module_dir) {
const std::vector<std::filesystem::path> candidates = {
module_dir / L"Maps" / L"Donwload", module_dir / L"Maps" / L"Download",
module_dir / L"maps" / L"download", module_dir / L"maps" / L"Download"};
for (const auto &dir : candidates) {
std::error_code ec;
if (std::filesystem::exists(dir, ec) && !ec) {
return dir;
}
}
return module_dir / L"Maps" / L"Donwload";
}
std::vector<std::filesystem::path> ResolveMapSearchRoots(
const std::filesystem::path &module_dir) {
return {module_dir / L"Maps", module_dir / L"maps"};
}
bool MapExistsRecursive(const std::filesystem::path &module_dir,
const std::string &version_utf8, uint64_t expected_size,
const AppConfig &config) {
const std::wstring version = SafeUtf8ToWide(version_utf8);
if (version.empty()) {
return false;
}
for (const auto &root : ResolveMapSearchRoots(module_dir)) {
std::error_code ec;
if (!std::filesystem::exists(root, ec) || ec) {
continue;
}
for (std::filesystem::recursive_directory_iterator it(root, ec), end; it != end;
it.increment(ec)) {
if (ec) {
break;
}
if (!it->is_regular_file(ec) || ec) {
continue;
}
const auto &path = it->path();
if (!HasW3xExtension(path)) {
continue;
}
const std::wstring file_name = path.filename().wstring();
if (!CaseInsensitiveContains(file_name, version)) {
continue;
}
if (SizeMatches(path, expected_size, config)) {
return true;
}
}
}
return false;
}
std::string UrlEncodeAsciiBytes(const std::string &value) {
auto is_safe = [](unsigned char ch) {
return (ch >= 'A' && ch <= 'Z') || (ch >= 'a' && ch <= 'z') ||
(ch >= '0' && ch <= '9') || ch == '-' || ch == '_' || ch == '.' ||
ch == '~';
};
std::ostringstream out;
out << std::uppercase << std::hex;
for (unsigned char ch : value) {
if (is_safe(ch)) {
out << static_cast<char>(ch);
} else {
out << '%' << std::setw(2) << std::setfill('0') << static_cast<int>(ch);
out << std::setfill(' ');
}
}
return out.str();
}
void AddOrMergeDownloadItem(std::vector<DownloadItem> &items, DownloadItem item) {
for (auto &existing : items) {
if (existing.kind != item.kind) {
continue;
}
if (!PathEqualsCaseInsensitive(existing.target_path, item.target_path)) {
continue;
}
existing.owner_flags |= item.owner_flags;
existing.expected_size = (std::max)(existing.expected_size, item.expected_size);
if (existing.url.empty()) {
existing.url = item.url;
}
if (existing.display_name.empty()) {
existing.display_name = item.display_name;
}
if (existing.counter_map_name.empty()) {
existing.counter_map_name = item.counter_map_name;
}
return;
}
items.push_back(std::move(item));
}
void AppendMissingItemsForMap(std::vector<DownloadItem> &items,
const runtime::RuntimeState &state,
const std::filesystem::path &module_dir,
const MapInfo &info, MapChannel channel) {
const uint32_t owner_flag = OwnerFlagForChannel(channel);
const bool map_exists =
MapExistsRecursive(module_dir, info.version, info.data.size, state.config);
if (!map_exists && !info.data.name.empty()) {
DownloadItem map_item;
map_item.kind = DownloadKind::Map;
map_item.owner_flags = owner_flag;
map_item.display_name = SafeUtf8ToWide(info.data.name);
map_item.url = SafeUtf8ToWide(info.map_link);
map_item.target_path = ResolveMapDownloadDir(module_dir) / map_item.display_name;
map_item.expected_size = info.data.size;
map_item.counter_map_name = info.data.name;
AddOrMergeDownloadItem(items, std::move(map_item));
DebugLog(state.config, "[Debug] Missing " + std::string(channel == MapChannel::Dota ? "dota" : "lod") +
" map file: " + WideToAsciiLossy(SafeUtf8ToWide(info.data.name)));
}
if (info.data.mpq.has_value() && !info.data.mpq->name.empty()) {
const std::wstring mpq_name = SafeUtf8ToWide(info.data.mpq->name);
const bool mpq_exists = FileExistsExactInRoot(module_dir, mpq_name, info.data.mpq->size,
state.config);
if (!mpq_exists) {
DownloadItem mpq_item;
mpq_item.kind = DownloadKind::Mpq;
mpq_item.owner_flags = owner_flag;
mpq_item.display_name = mpq_name;
mpq_item.url = SafeUtf8ToWide(info.mpq_link);
mpq_item.target_path = module_dir / mpq_name;
mpq_item.expected_size = info.data.mpq->size;
AddOrMergeDownloadItem(items, std::move(mpq_item));
DebugLog(state.config,
"[Debug] Missing " + std::string(channel == MapChannel::Dota ? "dota" : "lod") +
" mpq file: " + WideToAsciiLossy(mpq_name));
}
}
}
} // namespace
void IncrementDownloadCounter(const MapInfo &info, const AppConfig &config) {
IncrementDownloadCounter(info.data.name, config);
}
void IncrementDownloadCounter(const std::string &map_name_utf8, const AppConfig &config) {
if (map_name_utf8.empty()) {
return;
}
const std::string encoded_map = UrlEncodeAsciiBytes(map_name_utf8);
const std::wstring url =
L"https://d1map.net/api/maps/downloaded?map=" + SafeUtf8ToWide(encoded_map);
if (config.debug_enabled) {
std::cout << "[Debug] Incrementing download counter: "
<< runtime::WideToUtf8(url) << std::endl;
}
const auto status = net::HttpPostNoBody(url);
if (!status.has_value()) {
DebugLog(config, "[Debug] Failed to increment download counter (network error)");
return;
}
if (*status >= 200 && *status < 300) {
DebugLog(config, "[Debug] Download counter incremented successfully");
} else {
DebugLog(config, "[Debug] Download counter request returned HTTP " +
std::to_string(*status));
}
}
std::vector<DownloadItem> BuildMissingDownloadItems(const runtime::RuntimeState &state,
const LatestMapsInfo &maps_info) {
std::vector<DownloadItem> items;
const std::filesystem::path module_dir =
!state.module_dir.empty() ? state.module_dir : state.config_path.parent_path();
if (state.config.dota_enabled && maps_info.dota.has_value()) {
AppendMissingItemsForMap(items, state, module_dir, *maps_info.dota, MapChannel::Dota);
}
if (state.config.lod_enabled && maps_info.lod.has_value()) {
AppendMissingItemsForMap(items, state, module_dir, *maps_info.lod, MapChannel::Lod);
}
std::stable_sort(items.begin(), items.end(), [](const DownloadItem &a, const DownloadItem &b) {
if (a.kind != b.kind) {
return a.kind == DownloadKind::Map; // maps first, mpq second
}
const bool a_dota = (a.owner_flags & kDownloadOwnerDota) != 0;
const bool b_dota = (b.owner_flags & kDownloadOwnerDota) != 0;
if (a_dota != b_dota) {
return a_dota;
}
return CaseInsensitiveEquals(a.display_name, b.display_name) ? false
: a.display_name < b.display_name;
});
if (state.config.debug_enabled && !items.empty()) {
std::ostringstream ss;
ss << "[Debug] Missing files queued: " << items.size();
DebugLog(state.config, ss.str());
for (const auto &item : items) {
DebugLog(state.config, "[Debug] - [" +
std::string(item.kind == DownloadKind::Map ? "map" : "mpq") +
"] " + WideToAsciiLossy(item.display_name));
}
}
return items;
}
} // namespace mapupdater::startup