-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmapInfo.cpp
More file actions
688 lines (599 loc) · 16.2 KB
/
mapInfo.cpp
File metadata and controls
688 lines (599 loc) · 16.2 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
#include "pch.h"
#include "mapInfo.h"
#include "http_client.h"
#include "runtime.h"
#include <charconv>
#include <cstdint>
#include <iostream>
#include <string_view>
#include <system_error>
#include <utility>
namespace mapupdater {
namespace {
constexpr wchar_t kLatestMapUrl[] = L"https://d1map.net/api/v1/maps/latest";
class JsonParser {
public:
JsonParser(std::string_view input, std::string_view selected_map_key)
: input_(input), selected_map_key_(selected_map_key) {}
bool ParseMapInfo(MapInfo &out) {
SkipWhitespace();
if (!ParseRootObject(out)) {
return false;
}
SkipWhitespace();
if (!AtEnd()) {
return Fail("Trailing characters after JSON object");
}
return true;
}
const std::string &error() const { return error_; }
private:
bool AtEnd() const { return pos_ >= input_.size(); }
bool Fail(const char *message) {
if (error_.empty()) {
error_ = message;
error_ += " at position ";
error_ += std::to_string(pos_);
}
return false;
}
void SkipWhitespace() {
while (!AtEnd()) {
const char ch = input_[pos_];
if (ch != ' ' && ch != '\t' && ch != '\r' && ch != '\n') {
break;
}
++pos_;
}
}
bool Consume(char expected) {
SkipWhitespace();
if (AtEnd() || input_[pos_] != expected) {
return false;
}
++pos_;
return true;
}
bool TryConsume(char value) {
SkipWhitespace();
if (AtEnd() || input_[pos_] != value) {
return false;
}
++pos_;
return true;
}
bool TryLiteral(std::string_view literal) {
SkipWhitespace();
if (pos_ + literal.size() > input_.size()) {
return false;
}
if (input_.substr(pos_, literal.size()) != literal) {
return false;
}
pos_ += literal.size();
return true;
}
bool ParseRootObject(MapInfo &out) {
if (!Consume('{')) {
return Fail("Expected '{'");
}
bool has_data = false;
bool has_selected_nested_map = false;
MapInfo selected_nested_map;
SkipWhitespace();
if (TryConsume('}')) {
return Fail("Missing data object");
}
while (true) {
std::string key;
if (!ParseString(key)) {
return Fail("Expected object key string");
}
if (!Consume(':')) {
return Fail("Expected ':' after key");
}
if (key == "version") {
if (!ParseNullableString(out.version)) {
return Fail("Expected string value for version");
}
} else if (key == "changelog") {
if (!ParseNullableString(out.changelog)) {
return Fail("Expected string value for changelog");
}
} else if (key == "map_link") {
if (!ParseNullableString(out.map_link)) {
return Fail("Expected string value for map_link");
}
} else if (key == "mpq_link") {
if (!ParseNullableString(out.mpq_link)) {
return Fail("Expected string value for mpq_link");
}
} else if (key == "data") {
if (!ParseDataObject(out.data)) {
return false;
}
has_data = true;
} else if ((key == "dota" || key == "lod") && key == selected_map_key_) {
if (!ParseRootObject(selected_nested_map)) {
return false;
}
has_selected_nested_map = true;
} else {
if (!SkipValue()) {
return false;
}
}
SkipWhitespace();
if (TryConsume('}')) {
if (has_selected_nested_map) {
out = std::move(selected_nested_map);
return true;
}
if (!has_data) {
return Fail("Missing data object");
}
return true;
}
if (!Consume(',')) {
return Fail("Expected ',' or '}' in object");
}
}
}
bool ParseDataObject(MapData &out) {
if (!Consume('{')) {
return Fail("Expected '{' for data");
}
SkipWhitespace();
if (TryConsume('}')) {
return true;
}
while (true) {
std::string key;
if (!ParseString(key)) {
return Fail("Expected data key");
}
if (!Consume(':')) {
return Fail("Expected ':' after data key");
}
if (key == "name") {
if (!ParseString(out.name)) {
return Fail("Expected string value for data.name");
}
} else if (key == "size") {
if (!ParseUnsigned(out.size)) {
return Fail("Expected unsigned integer value for data.size");
}
} else if (key == "mpq") {
SkipWhitespace();
if (TryLiteral("null")) {
out.mpq.reset();
} else {
MapMpq mpq;
if (!ParseMpqObject(mpq)) {
return false;
}
out.mpq = std::move(mpq);
}
} else {
if (!SkipValue()) {
return false;
}
}
SkipWhitespace();
if (TryConsume('}')) {
return true;
}
if (!Consume(',')) {
return Fail("Expected ',' or '}' in data object");
}
}
}
bool ParseMpqObject(MapMpq &out) {
if (!Consume('{')) {
return Fail("Expected '{' for mpq");
}
SkipWhitespace();
if (TryConsume('}')) {
return true;
}
while (true) {
std::string key;
if (!ParseString(key)) {
return Fail("Expected mpq key");
}
if (!Consume(':')) {
return Fail("Expected ':' after mpq key");
}
if (key == "name") {
if (!ParseString(out.name)) {
return Fail("Expected string value for mpq.name");
}
} else if (key == "size") {
if (!ParseUnsigned(out.size)) {
return Fail("Expected unsigned integer value for mpq.size");
}
} else {
if (!SkipValue()) {
return false;
}
}
SkipWhitespace();
if (TryConsume('}')) {
return true;
}
if (!Consume(',')) {
return Fail("Expected ',' or '}' in mpq object");
}
}
}
bool SkipValue() {
SkipWhitespace();
if (AtEnd()) {
return Fail("Unexpected end of input");
}
switch (input_[pos_]) {
case '"': {
std::string ignored;
return ParseString(ignored);
}
case '{':
return SkipObject();
case '[':
return SkipArray();
case 't':
return TryLiteral("true") || Fail("Invalid literal");
case 'f':
return TryLiteral("false") || Fail("Invalid literal");
case 'n':
return TryLiteral("null") || Fail("Invalid literal");
default:
return SkipNumber();
}
}
bool SkipObject() {
if (!Consume('{')) {
return Fail("Expected '{'");
}
SkipWhitespace();
if (TryConsume('}')) {
return true;
}
while (true) {
std::string key;
if (!ParseString(key)) {
return Fail("Expected key while skipping object");
}
if (!Consume(':')) {
return Fail("Expected ':' while skipping object");
}
if (!SkipValue()) {
return false;
}
SkipWhitespace();
if (TryConsume('}')) {
return true;
}
if (!Consume(',')) {
return Fail("Expected ',' or '}' while skipping object");
}
}
}
bool SkipArray() {
if (!Consume('[')) {
return Fail("Expected '['");
}
SkipWhitespace();
if (TryConsume(']')) {
return true;
}
while (true) {
if (!SkipValue()) {
return false;
}
SkipWhitespace();
if (TryConsume(']')) {
return true;
}
if (!Consume(',')) {
return Fail("Expected ',' or ']' while skipping array");
}
}
}
bool SkipNumber() {
SkipWhitespace();
const size_t start = pos_;
if (!AtEnd() && input_[pos_] == '-') {
++pos_;
}
if (!ParseDigits()) {
pos_ = start;
return Fail("Invalid number");
}
if (!AtEnd() && input_[pos_] == '.') {
++pos_;
if (!ParseDigits()) {
pos_ = start;
return Fail("Invalid fractional number");
}
}
if (!AtEnd() && (input_[pos_] == 'e' || input_[pos_] == 'E')) {
++pos_;
if (!AtEnd() && (input_[pos_] == '+' || input_[pos_] == '-')) {
++pos_;
}
if (!ParseDigits()) {
pos_ = start;
return Fail("Invalid exponent");
}
}
return true;
}
bool ParseUnsigned(uint64_t &out) {
SkipWhitespace();
const size_t start = pos_;
if (AtEnd() || input_[pos_] == '-') {
return false;
}
if (!ParseDigits()) {
pos_ = start;
return false;
}
const std::string_view token = input_.substr(start, pos_ - start);
uint64_t value = 0;
const auto [ptr, ec] =
std::from_chars(token.data(), token.data() + token.size(), value);
if (ec != std::errc() || ptr != token.data() + token.size()) {
pos_ = start;
return false;
}
out = value;
return true;
}
bool ParseDigits() {
if (AtEnd()) {
return false;
}
if (input_[pos_] == '0') {
++pos_;
return true;
}
if (input_[pos_] < '1' || input_[pos_] > '9') {
return false;
}
++pos_;
while (!AtEnd() && input_[pos_] >= '0' && input_[pos_] <= '9') {
++pos_;
}
return true;
}
bool ParseString(std::string &out) {
SkipWhitespace();
if (AtEnd() || input_[pos_] != '"') {
return false;
}
++pos_;
out.clear();
while (!AtEnd()) {
const char ch = input_[pos_++];
if (ch == '"') {
return true;
}
if (ch == '\\') {
if (AtEnd()) {
return Fail("Invalid escape sequence");
}
const char esc = input_[pos_++];
switch (esc) {
case '"':
case '\\':
case '/':
out.push_back(esc);
break;
case 'b':
out.push_back('\b');
break;
case 'f':
out.push_back('\f');
break;
case 'n':
out.push_back('\n');
break;
case 'r':
out.push_back('\r');
break;
case 't':
out.push_back('\t');
break;
case 'u':
if (!ParseUnicodeEscape(out)) {
return false;
}
break;
default:
return Fail("Unsupported escape sequence");
}
continue;
}
if (static_cast<unsigned char>(ch) < 0x20) {
return Fail("Control character in string");
}
out.push_back(ch);
}
return Fail("Unterminated string");
}
bool ParseNullableString(std::string &out) {
SkipWhitespace();
if (TryLiteral("null")) {
out.clear();
return true;
}
return ParseString(out);
}
bool ParseUnicodeEscape(std::string &out) {
uint32_t code_point = 0;
if (!ParseHex4(code_point)) {
return Fail("Invalid unicode escape");
}
if (code_point >= 0xD800 && code_point <= 0xDBFF) {
if (pos_ + 2 > input_.size() || input_[pos_] != '\\' || input_[pos_ + 1] != 'u') {
return Fail("Missing low surrogate");
}
pos_ += 2;
uint32_t low = 0;
if (!ParseHex4(low)) {
return Fail("Invalid unicode surrogate pair");
}
if (low < 0xDC00 || low > 0xDFFF) {
return Fail("Invalid low surrogate");
}
code_point = 0x10000 + (((code_point - 0xD800) << 10) | (low - 0xDC00));
} else if (code_point >= 0xDC00 && code_point <= 0xDFFF) {
return Fail("Unexpected low surrogate");
}
AppendUtf8(out, code_point);
return true;
}
bool ParseHex4(uint32_t &value) {
if (pos_ + 4 > input_.size()) {
return false;
}
value = 0;
for (size_t i = 0; i < 4; ++i) {
const char ch = input_[pos_++];
value <<= 4;
if (ch >= '0' && ch <= '9') {
value |= static_cast<uint32_t>(ch - '0');
} else if (ch >= 'a' && ch <= 'f') {
value |= static_cast<uint32_t>(10 + (ch - 'a'));
} else if (ch >= 'A' && ch <= 'F') {
value |= static_cast<uint32_t>(10 + (ch - 'A'));
} else {
return false;
}
}
return true;
}
static void AppendUtf8(std::string &out, uint32_t code_point) {
if (code_point <= 0x7F) {
out.push_back(static_cast<char>(code_point));
return;
}
if (code_point <= 0x7FF) {
out.push_back(static_cast<char>(0xC0 | (code_point >> 6)));
out.push_back(static_cast<char>(0x80 | (code_point & 0x3F)));
return;
}
if (code_point <= 0xFFFF) {
out.push_back(static_cast<char>(0xE0 | (code_point >> 12)));
out.push_back(static_cast<char>(0x80 | ((code_point >> 6) & 0x3F)));
out.push_back(static_cast<char>(0x80 | (code_point & 0x3F)));
return;
}
out.push_back(static_cast<char>(0xF0 | (code_point >> 18)));
out.push_back(static_cast<char>(0x80 | ((code_point >> 12) & 0x3F)));
out.push_back(static_cast<char>(0x80 | ((code_point >> 6) & 0x3F)));
out.push_back(static_cast<char>(0x80 | (code_point & 0x3F)));
}
std::string_view input_;
std::string_view selected_map_key_;
size_t pos_ = 0;
std::string error_;
};
std::optional<MapInfo> ParseMapInfoJsonForKey(const std::string &payload,
std::string_view selected_key,
const AppConfig &config) {
JsonParser parser(payload, selected_key);
MapInfo info;
if (!parser.ParseMapInfo(info)) {
if (config.debug_enabled) {
std::cout << "[Error] JSON parse error for key '" << selected_key
<< "': " << parser.error() << std::endl;
}
return std::nullopt;
}
return info;
}
} // namespace
std::optional<MapInfo> FetchLatestMapInfo() {
const auto &state = runtime::GetRuntimeState();
return FetchLatestMapInfo(state.config);
}
std::optional<LatestMapsInfo> FetchLatestMapsInfo() {
const auto &state = runtime::GetRuntimeState();
return FetchLatestMapsInfo(state.config);
}
std::optional<LatestMapsInfo> FetchLatestMapsInfo(const AppConfig &config) {
if (config.debug_enabled) {
std::cout << "[Debug] Fetching map info from "
<< runtime::WideToUtf8(kLatestMapUrl) << std::endl;
std::cout << "[Debug] Requested map types: "
<< (config.dota_enabled ? "dota " : "")
<< (config.lod_enabled ? "lod" : "") << std::endl;
}
const auto response = net::HttpGet(kLatestMapUrl);
if (!response.has_value()) {
if (config.debug_enabled) {
std::cout << "[Error] Failed to fetch map info (network error)." << std::endl;
}
return std::nullopt;
}
if (response->status_code < 200 || response->status_code >= 300) {
if (config.debug_enabled) {
std::cout << "[Error] Failed to fetch map info. HTTP status: "
<< response->status_code << std::endl;
}
return std::nullopt;
}
if (response->body.empty()) {
if (config.debug_enabled) {
std::cout << "[Error] Failed to fetch map info (empty response)." << std::endl;
}
return std::nullopt;
}
LatestMapsInfo maps_info;
bool parsed_any = false;
if (config.dota_enabled) {
maps_info.dota = ParseMapInfoJsonForKey(response->body, "dota", config);
parsed_any = parsed_any || maps_info.dota.has_value();
}
if (config.lod_enabled) {
maps_info.lod = ParseMapInfoJsonForKey(response->body, "lod", config);
parsed_any = parsed_any || maps_info.lod.has_value();
}
if (!parsed_any) {
return std::nullopt;
}
if (config.debug_enabled) {
if (maps_info.dota.has_value()) {
const bool need_mpq = maps_info.dota->data.mpq.has_value();
std::cout << "[Debug] Dota map info: " << maps_info.dota->version
<< ", need_mpq: " << need_mpq << std::endl;
}
if (maps_info.lod.has_value()) {
const bool need_mpq = maps_info.lod->data.mpq.has_value();
std::cout << "[Debug] LoD map info: " << maps_info.lod->version
<< ", need_mpq: " << need_mpq << std::endl;
}
}
return maps_info;
}
std::optional<MapInfo> FetchLatestMapInfo(const AppConfig &config) {
const auto maps_info = FetchLatestMapsInfo(config);
if (!maps_info.has_value()) {
return std::nullopt;
}
if (config.lod_enabled && !config.dota_enabled) {
if (maps_info->lod.has_value()) {
return maps_info->lod;
}
return maps_info->dota;
}
if (config.dota_enabled) {
if (maps_info->dota.has_value()) {
return maps_info->dota;
}
return maps_info->lod;
}
return maps_info->lod;
}
} // namespace mapupdater