forked from RPCS3/rpcs3
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbin_patch.cpp
1112 lines (931 loc) · 30.8 KB
/
bin_patch.cpp
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
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#include "bin_patch.h"
#include "File.h"
#include "Config.h"
#include "version.h"
#include "Emu/System.h"
#include "util/types.hpp"
#include "util/endian.hpp"
LOG_CHANNEL(patch_log, "PAT");
template <>
void fmt_class_string<YAML::NodeType::value>::format(std::string& out, u64 arg)
{
format_enum(out, arg, [](YAML::NodeType::value value)
{
switch (value)
{
case YAML::NodeType::Undefined: return "Undefined";
case YAML::NodeType::Null: return "Null";
case YAML::NodeType::Scalar: return "Scalar";
case YAML::NodeType::Sequence: return "Sequence";
case YAML::NodeType::Map: return "Map";
}
return unknown;
});
}
template <>
void fmt_class_string<patch_type>::format(std::string& out, u64 arg)
{
format_enum(out, arg, [](patch_type value)
{
switch (value)
{
case patch_type::invalid: return "invalid";
case patch_type::load: return "load";
case patch_type::byte: return "byte";
case patch_type::le16: return "le16";
case patch_type::le32: return "le32";
case patch_type::le64: return "le64";
case patch_type::bef32: return "bef32";
case patch_type::bef64: return "bef64";
case patch_type::be16: return "be16";
case patch_type::be32: return "be32";
case patch_type::bd32: return "bd32";
case patch_type::be64: return "be64";
case patch_type::bd64: return "bd64";
case patch_type::lef32: return "lef32";
case patch_type::lef64: return "lef64";
case patch_type::utf8: return "utf8";
}
return unknown;
});
}
patch_engine::patch_engine()
{
const std::string patches_path = get_patches_path();
if (!fs::create_path(patches_path))
{
patch_log.fatal("Failed to create path: %s (%s)", patches_path, fs::g_tls_error);
}
}
std::string patch_engine::get_patch_config_path()
{
#ifdef _WIN32
const std::string config_dir = fs::get_config_dir() + "config/";
const std::string patch_path = config_dir + "patch_config.yml";
if (!fs::create_path(config_dir))
{
patch_log.error("Could not create path: %s (%s)", patch_path, fs::g_tls_error);
}
return patch_path;
#else
return fs::get_config_dir() + "patch_config.yml";
#endif
}
std::string patch_engine::get_patches_path()
{
return fs::get_config_dir() + "patches/";
}
std::string patch_engine::get_imported_patch_path()
{
return get_patches_path() + "imported_patch.yml";
}
static void append_log_message(std::stringstream* log_messages, const std::string& message)
{
if (log_messages)
*log_messages << message << std::endl;
};
bool patch_engine::load(patch_map& patches_map, const std::string& path, std::string content, bool importing, std::stringstream* log_messages)
{
if (content.empty())
{
// Load patch file
fs::file file{path};
if (!file)
{
// Do nothing
return true;
}
content = file.to_string();
}
// Interpret yaml nodes
auto [root, error] = yaml_load(content);
if (!error.empty() || !root)
{
append_log_message(log_messages, "Fatal Error: Failed to load file!");
patch_log.fatal("Failed to load patch file %s:\n%s", path, error);
return false;
}
// Load patch config to determine which patches are enabled
patch_map patch_config;
if (!importing)
{
patch_config = load_config();
}
std::string version;
if (const auto version_node = root[patch_key::version])
{
version = version_node.Scalar();
if (version != patch_engine_version)
{
append_log_message(log_messages, fmt::format("Error: File version %s does not match patch engine target version %s (file: %s)", version, patch_engine_version, path));
patch_log.error("File version %s does not match patch engine target version %s (file: %s)", version, patch_engine_version, path);
return false;
}
// We don't need the Version node in local memory anymore
root.remove(patch_key::version);
}
else
{
append_log_message(log_messages, fmt::format("Error: No '%s' entry found. Patch engine version = %s (file: %s)", patch_key::version, patch_engine_version, path));
patch_log.error("No '%s' entry found. Patch engine version = %s (file: %s)", patch_key::version, patch_engine_version, path);
return false;
}
bool is_valid = true;
// Go through each main key in the file
for (auto pair : root)
{
const auto& main_key = pair.first.Scalar();
if (const auto yml_type = pair.second.Type(); yml_type != YAML::NodeType::Map)
{
append_log_message(log_messages, fmt::format("Error: Skipping key %s: expected Map, found %s", main_key, yml_type));
patch_log.error("Skipping key %s: expected Map, found %s (file: %s)", main_key, yml_type, path);
is_valid = false;
continue;
}
// Skip Anchors
if (main_key == patch_key::anchors)
{
continue;
}
// Find or create an entry matching the key/hash in our map
auto& container = patches_map[main_key];
container.hash = main_key;
container.version = version;
// Go through each patch
for (auto patches_entry : pair.second)
{
// Each key in "Patches" is also the patch description
const std::string& description = patches_entry.first.Scalar();
// Compile patch information
if (const auto yml_type = patches_entry.second.Type(); yml_type != YAML::NodeType::Map)
{
append_log_message(log_messages, fmt::format("Error: Skipping Patch key %s: expected Map, found %s (key: %s)", description, yml_type, main_key));
patch_log.error("Skipping Patch key %s: expected Map, found %s (key: %s, file: %s)", description, yml_type, main_key, path);
is_valid = false;
continue;
}
struct patch_info info {};
info.description = description;
info.hash = main_key;
info.version = version;
info.source_path = path;
if (const auto games_node = patches_entry.second[patch_key::games])
{
if (const auto yml_type = games_node.Type(); yml_type != YAML::NodeType::Map)
{
append_log_message(log_messages, fmt::format("Error: Skipping Games key: expected Map, found %s (patch: %s, key: %s)", yml_type, description, main_key));
patch_log.error("Skipping Games key: expected Map, found %s (patch: %s, key: %s, file: %s)", yml_type, description, main_key, path);
is_valid = false;
continue;
}
for (const auto game_node : games_node)
{
const std::string& title = game_node.first.Scalar();
if (const auto yml_type = game_node.second.Type(); yml_type != YAML::NodeType::Map)
{
append_log_message(log_messages, fmt::format("Error: Skipping %s: expected Map, found %s (patch: %s, key: %s)", title, yml_type, description, main_key));
patch_log.error("Skipping %s: expected Map, found %s (patch: %s, key: %s, file: %s)", title, yml_type, description, main_key, path);
is_valid = false;
continue;
}
const bool title_is_all_key = title == patch_key::all;
for (const auto serial_node : game_node.second)
{
const std::string& serial = serial_node.first.Scalar();
if (serial == patch_key::all)
{
if (!title_is_all_key)
{
append_log_message(log_messages, fmt::format("Error: Using '%s' as serial is not allowed for titles other than '%s' (title: %s, patch: %s, key: %s)", patch_key::all, patch_key::all, title, description, main_key));
patch_log.error("Error: Using '%s' as serial is not allowed for titles other than '%s' (title: %s, patch: %s, key: %s, file: %s)", patch_key::all, patch_key::all, title, description, main_key, path);
is_valid = false;
continue;
}
}
else if (title_is_all_key)
{
append_log_message(log_messages, fmt::format("Error: Only '%s' is allowed as serial if the title is '%s' (serial: %s, patch: %s, key: %s)", patch_key::all, patch_key::all, serial, description, main_key));
patch_log.error("Error: Only '%s' is allowed as serial if the title is '%s' (serial: %s, patch: %s, key: %s, file: %s)", patch_key::all, patch_key::all, serial, description, main_key, path);
is_valid = false;
continue;
}
if (const auto yml_type = serial_node.second.Type(); yml_type != YAML::NodeType::Sequence)
{
append_log_message(log_messages, fmt::format("Error: Skipping %s: expected Sequence, found %s (title: %s, patch: %s, key: %s)", serial, title, yml_type, description, main_key));
patch_log.error("Skipping %s: expected Sequence, found %s (title: %s, patch: %s, key: %s, file: %s)", serial, title, yml_type, description, main_key, path);
is_valid = false;
continue;
}
patch_engine::patch_app_versions app_versions;
for (const auto version : serial_node.second)
{
const auto& app_version = version.Scalar();
// Find out if this patch was enabled in the patch config
const bool enabled = patch_config[main_key].patch_info_map[description].titles[title][serial][app_version];
app_versions.emplace(version.Scalar(), enabled);
}
if (app_versions.empty())
{
append_log_message(log_messages, fmt::format("Error: Skipping %s: empty Sequence (title: %s, patch: %s, key: %s)", serial, title, description, main_key));
patch_log.error("Skipping %s: empty Sequence (title: %s, patch: %s, key: %s, file: %s)", serial, title, description, main_key, path);
is_valid = false;
}
else
{
info.titles[title][serial] = app_versions;
}
}
}
}
if (const auto author_node = patches_entry.second[patch_key::author])
{
info.author = author_node.Scalar();
}
if (const auto patch_version_node = patches_entry.second[patch_key::patch_version])
{
info.patch_version = patch_version_node.Scalar();
}
if (const auto notes_node = patches_entry.second[patch_key::notes])
{
if (notes_node.IsSequence())
{
for (const auto note : notes_node)
{
if (note && note.IsScalar())
{
info.notes += note.Scalar();
}
else
{
append_log_message(log_messages, fmt::format("Error: Skipping sequenced Note (patch: %s, key: %s)", description, main_key));
patch_log.error("Skipping sequenced Note (patch: %s, key: %s, file: %s)", description, main_key, path);
is_valid = false;
}
}
}
else
{
info.notes = notes_node.Scalar();
}
}
if (const auto patch_group_node = patches_entry.second[patch_key::group])
{
info.patch_group = patch_group_node.Scalar();
}
if (const auto patch_node = patches_entry.second[patch_key::patch])
{
if (!read_patch_node(info, patch_node, root, log_messages))
{
is_valid = false;
}
}
// Skip this patch if a higher patch version already exists
if (container.patch_info_map.find(description) != container.patch_info_map.end())
{
bool ok;
const auto existing_version = container.patch_info_map[description].patch_version;
const bool version_is_bigger = utils::compare_versions(info.patch_version, existing_version, ok) > 0;
if (!ok || !version_is_bigger)
{
patch_log.warning("A higher or equal patch version already exists ('%s' vs '%s') for %s: %s (in file %s)", info.patch_version, existing_version, main_key, description, path);
append_log_message(log_messages, fmt::format("A higher or equal patch version already exists ('%s' vs '%s') for %s: %s (in file %s)", info.patch_version, existing_version, main_key, description, path));
continue;
}
else if (!importing)
{
patch_log.warning("A lower patch version was found ('%s' vs '%s') for %s: %s (in file %s)", existing_version, info.patch_version, main_key, description, container.patch_info_map[description].source_path);
}
}
// Insert patch information
container.patch_info_map[description] = info;
}
}
return is_valid;
}
patch_type patch_engine::get_patch_type(YAML::Node node)
{
u64 type_val = 0;
if (!node || !node.IsScalar() || !cfg::try_to_enum_value(&type_val, &fmt_class_string<patch_type>::format, node.Scalar()))
{
return patch_type::invalid;
}
return static_cast<patch_type>(type_val);
}
bool patch_engine::add_patch_data(YAML::Node node, patch_info& info, u32 modifier, const YAML::Node& root, std::stringstream* log_messages)
{
if (!node || !node.IsSequence())
{
append_log_message(log_messages, fmt::format("Skipping invalid patch node %s. (key: %s)", info.description, info.hash));
patch_log.error("Skipping invalid patch node %s. (key: %s)", info.description, info.hash);
return false;
}
const auto type_node = node[0];
auto addr_node = node[1];
const auto value_node = node[2];
const auto type = get_patch_type(type_node);
if (type == patch_type::invalid)
{
const auto type_str = type_node && type_node.IsScalar() ? type_node.Scalar() : "";
append_log_message(log_messages, fmt::format("Skipping patch node %s: type '%s' is invalid. (key: %s)", info.description, type_str, info.hash));
patch_log.error("Skipping patch node %s: type '%s' is invalid. (key: %s)", info.description, type_str, info.hash);
return false;
}
if (type == patch_type::load)
{
// Special syntax: anchors (named sequence)
// Check if the anchor was resolved.
if (const auto yml_type = addr_node.Type(); yml_type != YAML::NodeType::Sequence)
{
append_log_message(log_messages, fmt::format("Skipping sequence: expected Sequence, found %s (key: %s)", yml_type, info.hash));
patch_log.error("Skipping sequence: expected Sequence, found %s (key: %s)", yml_type, info.hash);
return false;
}
// Address modifier (optional)
const u32 mod = value_node.as<u32>(0);
bool is_valid = true;
for (const auto& item : addr_node)
{
if (!add_patch_data(item, info, mod, root, log_messages))
{
is_valid = false;
}
}
return is_valid;
}
struct patch_data p_data{};
p_data.type = type;
p_data.offset = addr_node.as<u32>(0) + modifier;
p_data.original_value = value_node.IsScalar() ? value_node.Scalar() : "";
std::string error_message;
switch (p_data.type)
{
case patch_type::utf8:
{
break;
}
case patch_type::bef32:
case patch_type::lef32:
case patch_type::bef64:
case patch_type::lef64:
{
p_data.value.double_value = get_yaml_node_value<f64>(value_node, error_message);
break;
}
default:
{
p_data.value.long_value = get_yaml_node_value<u64>(value_node, error_message);
break;
}
}
if (!error_message.empty())
{
error_message = fmt::format("Skipping patch data entry: [ %s, 0x%.8x, %s ] (key: %s) %s",
p_data.type, p_data.offset, p_data.original_value.empty() ? "?" : p_data.original_value, info.hash, error_message);
append_log_message(log_messages, error_message);
patch_log.error("%s", error_message);
return false;
}
info.data_list.emplace_back(p_data);
return true;
}
bool patch_engine::read_patch_node(patch_info& info, YAML::Node node, const YAML::Node& root, std::stringstream* log_messages)
{
if (!node)
{
append_log_message(log_messages, fmt::format("Skipping invalid patch node %s. (key: %s)", info.description, info.hash));
patch_log.error("Skipping invalid patch node %s. (key: %s)", info.description, info.hash);
return false;
}
if (const auto yml_type = node.Type(); yml_type != YAML::NodeType::Sequence)
{
append_log_message(log_messages, fmt::format("Skipping patch node %s: expected Sequence, found %s (key: %s)", info.description, yml_type, info.hash));
patch_log.error("Skipping patch node %s: expected Sequence, found %s (key: %s)", info.description, yml_type, info.hash);
return false;
}
bool is_valid = true;
for (auto patch : node)
{
if (!add_patch_data(patch, info, 0, root, log_messages))
{
is_valid = false;
}
}
return is_valid;
}
void patch_engine::append_global_patches()
{
// Regular patch.yml
load(m_map, get_patches_path() + "patch.yml");
// Imported patch.yml
load(m_map, get_imported_patch_path());
}
void patch_engine::append_title_patches(const std::string& title_id)
{
if (title_id.empty())
{
return;
}
// Regular patch.yml
load(m_map, get_patches_path() + title_id + "_patch.yml");
}
static std::basic_string<u32> apply_modification(const patch_engine::patch_info& patch, u8* dst, u32 filesz, u32 min_addr)
{
std::basic_string<u32> applied;
for (const auto& p : patch.data_list)
{
u32 offset = p.offset;
if (offset < min_addr || offset - min_addr >= filesz)
{
// This patch is out of range for this segment
continue;
}
offset -= min_addr;
auto ptr = dst + offset;
u32 resval = umax;
switch (p.type)
{
case patch_type::invalid:
case patch_type::load:
{
// Invalid in this context
continue;
}
case patch_type::byte:
{
*ptr = static_cast<u8>(p.value.long_value);
break;
}
case patch_type::le16:
{
le_t<u16> val = static_cast<u16>(p.value.long_value);
std::memcpy(ptr, &val, sizeof(val));
break;
}
case patch_type::le32:
{
le_t<u32> val = static_cast<u32>(p.value.long_value);
std::memcpy(ptr, &val, sizeof(val));
break;
}
case patch_type::lef32:
{
le_t<f32> val = static_cast<f32>(p.value.double_value);
std::memcpy(ptr, &val, sizeof(val));
break;
}
case patch_type::le64:
{
le_t<u64> val = static_cast<u64>(p.value.long_value);
std::memcpy(ptr, &val, sizeof(val));
break;
}
case patch_type::lef64:
{
le_t<f64> val = p.value.double_value;
std::memcpy(ptr, &val, sizeof(val));
break;
}
case patch_type::be16:
{
be_t<u16> val = static_cast<u16>(p.value.long_value);
std::memcpy(ptr, &val, sizeof(val));
break;
}
case patch_type::bd32:
{
be_t<u32> val = static_cast<u32>(p.value.long_value);
std::memcpy(ptr, &val, sizeof(val));
break;
}
case patch_type::be32:
{
be_t<u32> val = static_cast<u32>(p.value.long_value);
std::memcpy(ptr, &val, sizeof(val));
if (offset % 4 == 0)
resval = offset;
break;
}
case patch_type::bef32:
{
be_t<f32> val = static_cast<f32>(p.value.double_value);
std::memcpy(ptr, &val, sizeof(val));
break;
}
case patch_type::bd64:
{
be_t<u64> val = static_cast<u64>(p.value.long_value);
std::memcpy(ptr, &val, sizeof(val));
break;
}
case patch_type::be64:
{
be_t<u64> val = static_cast<u64>(p.value.long_value);
std::memcpy(ptr, &val, sizeof(val));
if (offset % 4)
{
break;
}
resval = offset;
applied.push_back((offset + 7) & -4); // Two 32-bit locations
break;
}
case patch_type::bef64:
{
be_t<f64> val = p.value.double_value;
std::memcpy(ptr, &val, sizeof(val));
break;
}
case patch_type::utf8:
{
std::memcpy(ptr, p.original_value.data(), p.original_value.size());
break;
}
}
// Possibly an executable instruction
applied.push_back(resval);
}
return applied;
}
std::basic_string<u32> patch_engine::apply(const std::string& name, u8* dst, u32 filesz, u32 min_addr)
{
if (m_map.find(name) == m_map.cend())
{
return {};
}
std::basic_string<u32> applied_total;
const auto& container = m_map.at(name);
const auto& serial = Emu.GetTitleID();
const auto& app_version = Emu.GetAppVersion();
// Different containers in order to seperate the patches
std::vector<const patch_info*> patches_for_this_serial_and_this_version;
std::vector<const patch_info*> patches_for_this_serial_and_all_versions;
std::vector<const patch_info*> patches_for_all_serials_and_this_version;
std::vector<const patch_info*> patches_for_all_serials_and_all_versions;
// Sort patches into different vectors based on their serial and version
for (const auto& [description, patch] : container.patch_info_map)
{
// Find out if this patch is enabled
for (const auto& [title, serials] : patch.titles)
{
bool is_all_serials = false;
bool is_all_versions = false;
std::string found_serial;
if (serials.find(serial) != serials.end())
{
found_serial = serial;
}
else if (serials.find(patch_key::all) != serials.end())
{
found_serial = patch_key::all;
is_all_serials = true;
}
if (found_serial.empty())
{
continue;
}
const auto& app_versions = serials.at(found_serial);
std::string found_app_version;
if (app_versions.find(app_version) != app_versions.end())
{
found_app_version = app_version;
}
else if (app_versions.find(patch_key::all) != app_versions.end())
{
found_app_version = patch_key::all;
is_all_versions = true;
}
if (!found_app_version.empty() && app_versions.at(found_app_version))
{
// This patch is enabled
if (is_all_serials)
{
if (is_all_versions)
{
patches_for_all_serials_and_all_versions.emplace_back(&patch);
}
else
{
patches_for_all_serials_and_this_version.emplace_back(&patch);
}
}
else if (is_all_versions)
{
patches_for_this_serial_and_all_versions.emplace_back(&patch);
}
else
{
patches_for_this_serial_and_this_version.emplace_back(&patch);
}
break;
}
}
}
// Apply modifications sequentially
auto apply_func = [&](const patch_info& patch)
{
auto applied = apply_modification(patch, dst, filesz, min_addr);
applied_total += applied;
patch_log.success("Applied patch (hash='%s', description='%s', author='%s', patch_version='%s', file_version='%s') (<- %u)", patch.hash, patch.description, patch.author, patch.patch_version, patch.version, applied.size());
};
// Sort specific patches after global patches
// So they will determine the end results
const auto patch_super_list =
{
&patches_for_all_serials_and_all_versions,
&patches_for_all_serials_and_this_version,
&patches_for_this_serial_and_all_versions,
&patches_for_this_serial_and_this_version
};
// Filter by patch group (reverse so specific patches will be prioritized over globals)
for (auto it = std::rbegin(patch_super_list); it != std::rend(patch_super_list); it++)
{
for (auto& patch : *it.operator*())
{
if (!patch->patch_group.empty())
{
if (!m_applied_groups.insert(patch->patch_group).second)
{
patch = nullptr;
}
}
}
}
for (auto patch_list : patch_super_list)
{
for (const patch_info* patch : *patch_list)
{
if (patch)
{
apply_func(*patch);
}
}
}
return applied_total;
}
void patch_engine::save_config(const patch_map& patches_map)
{
const std::string path = get_patch_config_path();
patch_log.notice("Saving patch config file %s", path);
fs::file file(path, fs::rewrite);
if (!file)
{
patch_log.fatal("Failed to open patch config file %s (%s)", path, fs::g_tls_error);
return;
}
YAML::Emitter out;
out << YAML::BeginMap;
// Save 'enabled' state per hash, description, serial and app_version
patch_map config_map;
for (const auto& [hash, container] : patches_map)
{
for (const auto& [description, patch] : container.patch_info_map)
{
for (const auto& [title, serials] : patch.titles)
{
for (const auto& [serial, app_versions] : serials)
{
for (const auto& [app_version, enabled] : app_versions)
{
if (enabled)
{
config_map[hash].patch_info_map[description].titles[title][serial][app_version] = true;
}
}
}
}
}
if (const auto& enabled_patches = config_map[hash].patch_info_map; !enabled_patches.empty())
{
out << hash << YAML::BeginMap;
for (const auto& [description, patch] : enabled_patches)
{
const auto& titles = patch.titles;
out << description << YAML::BeginMap;
for (const auto& [title, serials] : titles)
{
out << title << YAML::BeginMap;
for (const auto& [serial, app_versions] : serials)
{
out << serial << YAML::BeginMap;
for (const auto& [app_version, enabled] : app_versions)
{
out << app_version << enabled;
}
out << YAML::EndMap;
}
out << YAML::EndMap;
}
out << YAML::EndMap;
}
out << YAML::EndMap;
}
}
out << YAML::EndMap;
file.write(out.c_str(), out.size());
}
static void append_patches(patch_engine::patch_map& existing_patches, const patch_engine::patch_map& new_patches, usz& count, usz& total, std::stringstream* log_messages)
{
for (const auto& [hash, new_container] : new_patches)
{
total += new_container.patch_info_map.size();
if (existing_patches.find(hash) == existing_patches.end())
{
existing_patches[hash] = new_container;
count += new_container.patch_info_map.size();
continue;
}
auto& container = existing_patches[hash];
for (const auto& [description, new_info] : new_container.patch_info_map)
{
if (container.patch_info_map.find(description) == container.patch_info_map.end())
{
container.patch_info_map[description] = new_info;
count++;
continue;
}
auto& info = container.patch_info_map[description];
bool ok;
const bool version_is_bigger = utils::compare_versions(new_info.patch_version, info.patch_version, ok) > 0;
if (!ok)
{
patch_log.error("Failed to compare patch versions ('%s' vs '%s') for %s: %s", new_info.patch_version, info.patch_version, hash, description);
append_log_message(log_messages, fmt::format("Failed to compare patch versions ('%s' vs '%s') for %s: %s", new_info.patch_version, info.patch_version, hash, description));
continue;
}
if (!version_is_bigger)
{
patch_log.error("A higher or equal patch version already exists ('%s' vs '%s') for %s: %s", new_info.patch_version, info.patch_version, hash, description);
append_log_message(log_messages, fmt::format("A higher or equal patch version already exists ('%s' vs '%s') for %s: %s", new_info.patch_version, info.patch_version, hash, description));
continue;
}
for (const auto& [title, new_serials] : new_info.titles)
{
for (const auto& [serial, new_app_versions] : new_serials)
{
if (!new_app_versions.empty())
{
info.titles[title][serial].insert(new_app_versions.begin(), new_app_versions.end());
}
}
}
if (!new_info.patch_version.empty()) info.patch_version = new_info.patch_version;
if (!new_info.author.empty()) info.author = new_info.author;
if (!new_info.notes.empty()) info.notes = new_info.notes;
if (!new_info.data_list.empty()) info.data_list = new_info.data_list;
if (!new_info.source_path.empty()) info.source_path = new_info.source_path;
count++;
}
}
}
bool patch_engine::save_patches(const patch_map& patches, const std::string& path, std::stringstream* log_messages)
{
fs::file file(path, fs::rewrite);
if (!file)
{
patch_log.fatal("save_patches: Failed to open patch file %s (%s)", path, fs::g_tls_error);
append_log_message(log_messages, fmt::format("Failed to open patch file %s (%s)", path, fs::g_tls_error));
return false;
}
YAML::Emitter out;
out << YAML::BeginMap;
out << patch_key::version << patch_engine_version;
for (const auto& [hash, container] : patches)
{
if (container.patch_info_map.empty())
{
continue;
}
out << YAML::Newline << YAML::Newline;
out << hash << YAML::BeginMap;
for (const auto& [description, info] : container.patch_info_map)
{
out << description << YAML::BeginMap;
out << patch_key::games << YAML::BeginMap;
for (const auto& [title, serials] : info.titles)
{
out << title << YAML::BeginMap;
for (const auto& [serial, app_versions] : serials)
{
out << serial << YAML::BeginSeq;
for (const auto& app_version : app_versions)
{
out << app_version.first;
}
out << YAML::EndSeq;
}
out << YAML::EndMap;
}
out << YAML::EndMap;
if (!info.author.empty()) out << patch_key::author << info.author;
if (!info.patch_version.empty()) out << patch_key::patch_version << info.patch_version;
if (!info.patch_group.empty()) out << patch_key::group << info.patch_group;
if (!info.notes.empty()) out << patch_key::notes << info.notes;
out << patch_key::patch << YAML::BeginSeq;
for (const auto& data : info.data_list)
{
if (data.type == patch_type::invalid || data.type == patch_type::load)
{
// Unreachable with current logic
continue;
}
out << YAML::Flow;
out << YAML::BeginSeq;
out << fmt::format("%s", data.type);
out << fmt::format("0x%.8x", data.offset);
out << data.original_value;
out << YAML::EndSeq;
}
out << YAML::EndSeq;
out << YAML::EndMap;
}
out << YAML::EndMap;
}