forked from schellingb/RDBEd
-
Notifications
You must be signed in to change notification settings - Fork 0
/
RDBEd.cs
2361 lines (2224 loc) · 145 KB
/
RDBEd.cs
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
/* RDBEd - Retro RDB & DAT Editor
* Copyright (C) 2020-2023 - Bernhard Schelling
*
* RDBEd is free software: you can redistribute it and/or modify it under the terms
* of the GNU General Public License as published by the Free Software Found-
* ation, either version 3 of the License, or (at your option) any later version.
*
* RDBEd is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
* without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
* PURPOSE. See the GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License along with RDBEd.
* If not, see <http://www.gnu.org/licenses/>.
*/
using System;
using System.Collections.Generic;
using System.Drawing;
using System.IO;
using System.Windows.Forms;
using System.Text.RegularExpressions;
[assembly: System.Reflection.AssemblyProduct("RDBEd")]
[assembly: System.Reflection.AssemblyTitle("RDBEd - Retro RDB & DAT Editor")]
[assembly: System.Reflection.AssemblyVersion("1.4.0.0")]
[assembly: System.Reflection.AssemblyFileVersion("1.4.0.0")]
[assembly: System.Runtime.InteropServices.ComVisible(false)]
namespace RDBEd { static class About { public const string Text = "RDBEd 1.4 - Retro RDB & DAT Editor\n\nhttps://github.com/schellingb/RDBEd"; } }
namespace RDBEd
{
public enum EProgressState : byte { Ready, Counting, Querying, Applying }
public enum EProgressType : byte { CountInit, CountIncrement, Finish };
public enum EFieldFlag : byte { None = 0, Modified = 0x1, Warning = 0x2 };
public enum ERowFlag : byte { None = 0, Modified = 0x1, FromFile = 0x2, WarningsMask = 0xF0, WarningNotUnique = 0x10, WarningToolUnifyError = 0x20, WarningToolMergeError = 0x40, WarningInvalid = 0x80 };
public enum EFieldIndices : int { Name, Description, Genre, RomName, Size, Users, Release, Rumble, Analog, Coop, EnhancementHW, Franchise,
OriginalTitle, Developer, Publisher, Origin,Region,Tags, CRC, MD5, SHA1, Serial, COUNT };
class Entry
{
public static Color[] CellFieldColors = new Color[]
{
SystemColors.Window, // Default
Color.Orange, // Modified
Color.Red, // Warning
Color.Red, // Modified | Warning
};
public string Name, OrgName; public string ColName { get { return Name; } set { Set(EFieldIndices.Name, ref Name, OrgName, value); } }
public string Description, OrgDescription; public string ColDescription { get { return Description; } set { Set(EFieldIndices.Description, ref Description, OrgDescription, value); } }
public string Genre, OrgGenre; public string ColGenre { get { return Genre; } set { Set(EFieldIndices.Genre, ref Genre, OrgGenre, value); } }
public string RomName, OrgRomName; public string ColRomName { get { return RomName; } set { Set(EFieldIndices.RomName, ref RomName, OrgRomName, value); } }
public UInt64 Size, OrgSize; public UInt64 ColSize { get { return Size; } set { Set(EFieldIndices.Size, ref Size, OrgSize, value); } }
public uint Users, OrgUsers; public uint ColUsers { get { return Users; } set { Set(EFieldIndices.Users, ref Users, OrgUsers, value); } }
public DateTime Release, OrgRelease; public DateTime ColRelease { get { return Release; } set { Set(EFieldIndices.Release, ref Release, OrgRelease, value); } }
public bool Rumble, OrgRumble; public bool ColRumble { get { return Rumble; } set { Set(EFieldIndices.Rumble, ref Rumble, OrgRumble, value); } }
public bool Analog, OrgAnalog; public bool ColAnalog { get { return Analog; } set { Set(EFieldIndices.Analog, ref Analog, OrgAnalog, value); } }
public bool Coop, OrgCoop; public bool ColCoop { get { return Coop; } set { Set(EFieldIndices.Coop, ref Coop, OrgCoop, value); } }
public string EnhancementHW, OrgEnhancementHW; public string ColEnhancementHW { get { return EnhancementHW; } set { Set(EFieldIndices.EnhancementHW, ref EnhancementHW, OrgEnhancementHW, value); } }
public string Franchise, OrgFranchise; public string ColFranchise { get { return Franchise; } set { Set(EFieldIndices.Franchise, ref Franchise, OrgFranchise, value); } }
public string OriginalTitle, OrgOriginalTitle; public string ColOriginalTitle { get { return OriginalTitle; } set { Set(EFieldIndices.OriginalTitle, ref OriginalTitle, OrgOriginalTitle, value); } }
public string Developer, OrgDeveloper; public string ColDeveloper { get { return Developer; } set { Set(EFieldIndices.Developer, ref Developer, OrgDeveloper, value); } }
public string Publisher, OrgPublisher; public string ColPublisher { get { return Publisher; } set { Set(EFieldIndices.Publisher, ref Publisher, OrgPublisher, value); } }
public string Origin, OrgOrigin; public string ColOrigin { get { return Origin; } set { Set(EFieldIndices.Origin, ref Origin, OrgOrigin, value); } }
public string Region, OrgRegion; public string ColRegion { get { return Region; } set { Set(EFieldIndices.Region, ref Region, OrgRegion, value); } }
public string Tags, OrgTags; public string ColTags { get { return Tags; } set { Set(EFieldIndices.Tags, ref Tags, OrgTags, value); } }
public string CRC, OrgCRC; public string ColCRC { get { return CRC; } set { Set(EFieldIndices.CRC, ref CRC, OrgCRC, FixHex(value, 8)); } } //hex (4 byte)
public string MD5, OrgMD5; public string ColMD5 { get { return MD5; } set { Set(EFieldIndices.MD5, ref MD5, OrgMD5, FixHex(value, 32)); } } //hex (16 byte)
public string SHA1, OrgSHA1; public string ColSHA1 { get { return SHA1; } set { Set(EFieldIndices.SHA1, ref SHA1, OrgSHA1, FixHex(value, 40)); } } //hex (20 byte)
public string Serial, OrgSerial; public string ColSerial { get { return Serial; } set { Set(EFieldIndices.Serial, ref Serial, OrgSerial, value); } } //binary/ascii
// Other fields that exist in some data sources, currently not loaded/processed/saved in this program
//uint FamitsuRating;
//uint EdgeRating;
//uint EdgeIssue;
//string EdgeReview;
//string Barcode;
//string EsrbRating;
//string ElspaRating;
//string PegiRating;
//string CeroRating;
//uint TgdbRating;
public EFieldFlag[] FieldFlags = new EFieldFlag[22];
public ERowFlag RowFlags;
string SearchCache = null;
public void FlagModified(EFieldIndices idx, bool f)
{
if (((FieldFlags[(int)idx] & EFieldFlag.Modified) != 0) == f) return;
FieldFlags[(int)idx] = (f ? (FieldFlags[(int)idx] | EFieldFlag.Modified) : (FieldFlags[(int)idx] & ~EFieldFlag.Modified));
if (f) RowFlags |= ERowFlag.Modified;
else
{
bool hasModification = false;
foreach (EFieldFlag ff in FieldFlags) { if ((ff & EFieldFlag.Modified) != 0) { hasModification = true; break; } }
if (!hasModification) RowFlags &= ~ERowFlag.Modified;
}
Data.CountModification(f ? 1 : -1);
}
public void FlagWarning(EFieldIndices idx, bool f, ERowFlag rowFlag = ERowFlag.WarningsMask)
{
EFieldFlag mod = (rowFlag == ERowFlag.Modified ? EFieldFlag.Modified : EFieldFlag.Warning);
if (((FieldFlags[(int)idx] & mod) != 0) == f) return;
FieldFlags[(int)idx] = (f ? (FieldFlags[(int)idx] | mod) : (FieldFlags[(int)idx] & ~mod));
if (rowFlag == ERowFlag.Modified)
{
if (f) RowFlags |= ERowFlag.Modified;
else
{
bool hasModification = false;
foreach (EFieldFlag ff in FieldFlags) { if ((ff & EFieldFlag.Modified) != 0) { hasModification = true; break; } }
if (!hasModification) RowFlags &= ~ERowFlag.Modified;
}
Data.CountModification(f ? 1 : -1);
}
else
{
RowFlags = (f ? (RowFlags | rowFlag) : (RowFlags & ~rowFlag));
Data.CountWarning(f ? 1 : -1);
}
}
void Set(EFieldIndices idx, ref string val, string orgval, string newval)
{
if (String.IsNullOrWhiteSpace(newval)) newval = null;
FlagModified(idx, !String.Equals(newval, orgval, StringComparison.Ordinal));
FlagWarning(idx, false);
val = newval;
SearchCache = null;
}
void Set(EFieldIndices idx, ref uint val, uint orgval, uint newval)
{
FlagModified(idx, (newval != orgval));
FlagWarning(idx, false);
val = newval;
SearchCache = null;
}
void Set(EFieldIndices idx, ref UInt64 val, UInt64 orgval, UInt64 newval)
{
FlagModified(idx, (newval != orgval));
FlagWarning(idx, false);
val = newval;
SearchCache = null;
}
void Set(EFieldIndices idx, ref bool val, bool orgval, bool newval)
{
FlagModified(idx, (newval != orgval));
FlagWarning(idx, false);
val = newval;
SearchCache = null;
}
void Set(EFieldIndices idx, ref DateTime val, DateTime orgval, DateTime newval)
{
FlagModified(idx, (newval != orgval));
FlagWarning(idx, false);
val = newval;
SearchCache = null;
}
static string FixHex(string hex, int len)
{
if (String.IsNullOrWhiteSpace(hex)) return null;
hex = hex.ToUpperInvariant();
for (int i = 0; i != hex.Length; i++)
if (hex[i] < '0' || (hex[i] > '9' && hex[i] < 'A') || hex[i] > 'F')
hex = hex.Remove(i--, 1);
if (hex.Length > len) return hex.Substring(0, len);
while (hex.Length < len) hex = hex.Insert(0, "0");
return hex;
}
public void ClearWarnings()
{
if ((RowFlags & ERowFlag.WarningsMask) != 0)
for (int i = 0; i != (int)EFieldIndices.COUNT; i++)
FlagWarning((EFieldIndices)i, false);
}
public void SetOrg()
{
OrgName = Name = (string.IsNullOrWhiteSpace(Name ) ? null : Name );
OrgDescription = Description = (string.IsNullOrWhiteSpace(Description ) ? null : Description );
OrgGenre = Genre = (string.IsNullOrWhiteSpace(Genre ) ? null : Genre );
OrgRomName = RomName = (string.IsNullOrWhiteSpace(RomName ) ? null : RomName );
OrgSize = Size;
OrgUsers = Users;
OrgRelease = Release;
OrgRumble = Rumble;
OrgAnalog = Analog;
OrgCoop = Coop;
OrgEnhancementHW = EnhancementHW = (string.IsNullOrWhiteSpace(EnhancementHW ) ? null : EnhancementHW );
OrgFranchise = Franchise = (string.IsNullOrWhiteSpace(Franchise ) ? null : Franchise );
OrgOriginalTitle = OriginalTitle = (string.IsNullOrWhiteSpace(OriginalTitle ) ? null : OriginalTitle );
OrgDeveloper = Developer = (string.IsNullOrWhiteSpace(Developer ) ? null : Developer );
OrgPublisher = Publisher = (string.IsNullOrWhiteSpace(Publisher ) ? null : Publisher );
OrgOrigin = Origin = (string.IsNullOrWhiteSpace(Origin ) ? null : Origin );
OrgRegion = Region = (string.IsNullOrWhiteSpace(Region ) ? null : Region );
OrgTags = Tags = (string.IsNullOrWhiteSpace(Tags ) ? null : Tags );
OrgCRC = CRC = (string.IsNullOrWhiteSpace(CRC ) ? null : FixHex(CRC, 8) );
OrgMD5 = MD5 = (string.IsNullOrWhiteSpace(MD5 ) ? null : FixHex(MD5, 32));
OrgSHA1 = SHA1 = (string.IsNullOrWhiteSpace(SHA1 ) ? null : FixHex(SHA1, 40));
OrgSerial = Serial = (string.IsNullOrWhiteSpace(Serial ) ? null : Serial );
RowFlags |= ERowFlag.FromFile;
}
void Cache()
{
SearchCache =
(Name != null ? Name : "") + "\0" +
(Description != null ? Description : "") + "\0" +
(Genre != null ? Genre : "") + "\0" +
(RomName != null ? RomName : "") + "\0" +
(Size != 0 ? Size.ToString() : "") + "\0" +
(Users != 0 ? Users.ToString() : "") + "\0" +
(Release.ToBinary() != 0 ? Release.ToString() : "") + "\0" +
(Rumble ? "Rumble" : "") + "\0" +
(Analog ? "Analog" : "") + "\0" +
(Coop ? "Coop" : "") + "\0" +
(EnhancementHW != null ? EnhancementHW : "") + "\0" +
(Franchise != null ? Franchise : "") + "\0" +
(OriginalTitle != null ? OriginalTitle : "") + "\0" +
(Developer != null ? Developer : "") + "\0" +
(Publisher != null ? Publisher : "") + "\0" +
(Origin != null ? Origin : "") + "\0" +
(Region != null ? Region : "") + "\0" +
(Tags != null ? Tags : "") + "\0" +
(CRC != null ? CRC : "") + "\0" +
(MD5 != null ? MD5 : "") + "\0" +
(SHA1 != null ? SHA1 : "") + "\0" +
(Serial != null ? Serial : "") + "\0";
}
public bool Filter(string s)
{
if (SearchCache == null) Cache();
return SearchCache.IndexOf(s, StringComparison.OrdinalIgnoreCase) >= 0;
}
public void SetDate(int releaseDay = 0, int releaseMonth = 0, int releaseYear = 0)
{
if (releaseDay != 0 || releaseMonth != 0 || releaseYear != 0)
Release = new DateTime(
releaseYear < 1900 || releaseYear > 2100 ? 1900 : releaseYear,
releaseMonth < 1 || releaseMonth > 12 ? 1 : releaseMonth,
releaseDay < 1 || releaseDay > 31 ? 1 : releaseDay);
}
static EFieldFlag MergeString(ref string str, string other, bool merge)
{
if (string.IsNullOrWhiteSpace(other)) return EFieldFlag.None;
if (string.IsNullOrWhiteSpace(str))
{
str = other;
return EFieldFlag.Modified;
}
if (str.Equals(other, StringComparison.Ordinal)) return EFieldFlag.None;
if (!merge)
{
str = other;
return EFieldFlag.Modified;
}
if (str.IndexOf('|') == -1 && other.IndexOf('|') == -1)
{
str = other + "|" + str;
return EFieldFlag.Modified | EFieldFlag.Warning;
}
string[] strs = str.Split('|'), others = other.Split('|');
bool same = true;
for (int i = 0; i != strs.Length; i++)
if (Array.IndexOf<string>(others, strs[i]) == -1)
{ same = false; break; }
if (same) return EFieldFlag.None;
str = other;
for (int i = 0; i != strs.Length; i++)
if (Array.IndexOf<string>(others, strs[i]) == -1)
str += "|" + strs[i];
return EFieldFlag.Modified | EFieldFlag.Warning;
}
public void Import(Entry b, bool metaOnly, bool merge, ERowFlag warningType)
{
EFieldFlag c, all = EFieldFlag.None;
if (!metaOnly && (c = MergeString(ref Name, b.Name, merge)) != EFieldFlag.None) { FieldFlags[0] |= c; all |= c; }
if ( true && (c = MergeString(ref Description, b.Description, merge)) != EFieldFlag.None) { FieldFlags[1] |= c; all |= c; }
if ( true && (c = MergeString(ref Genre, b.Genre, merge)) != EFieldFlag.None) { FieldFlags[2] |= c; all |= c; }
if (!metaOnly && (c = MergeString(ref RomName, b.RomName, merge)) != EFieldFlag.None) { FieldFlags[3] |= c; all |= c; }
if (!metaOnly && b.Size != 0 && Size != b.Size)
{
c = EFieldFlag.Modified | (merge && Size != 0 ? EFieldFlag.Warning : EFieldFlag.None);
Size = b.Size;
FieldFlags[4] |= c; all |= c;
}
if (b.Users != 0 && Users != b.Users)
{
c = EFieldFlag.Modified | (merge && Users != 0 ? EFieldFlag.Warning : EFieldFlag.None);
Users = b.Users;
FieldFlags[5] |= c; all |= c;
}
if (b.Release.ToBinary() != 0 && Release.ToBinary() != b.Release.ToBinary())
{
c = EFieldFlag.Modified | (merge && Release.ToBinary() != 0 ? EFieldFlag.Warning : EFieldFlag.None);
Release = b.Release;
FieldFlags[6] |= c; all |= c;
}
if (!Rumble && b.Rumble) { Rumble = b.Rumble; c = EFieldFlag.Modified; FieldFlags[7] |= c; all |= c; }
if (!Analog && b.Analog) { Analog = b.Analog; c = EFieldFlag.Modified; FieldFlags[8] |= c; all |= c; }
if (!Coop && b.Coop ) { Coop = b.Coop; c = EFieldFlag.Modified; FieldFlags[9] |= c; all |= c; }
if ( true && (c = MergeString(ref EnhancementHW, b.EnhancementHW, merge)) != EFieldFlag.None) { FieldFlags[10] |= c; all |= c; }
if ( true && (c = MergeString(ref Franchise, b.Franchise, merge)) != EFieldFlag.None) { FieldFlags[11] |= c; all |= c; }
if ( true && (c = MergeString(ref OriginalTitle, b.OriginalTitle, merge)) != EFieldFlag.None) { FieldFlags[12] |= c; all |= c; }
if ( true && (c = MergeString(ref Developer, b.Developer, merge)) != EFieldFlag.None) { FieldFlags[13] |= c; all |= c; }
if ( true && (c = MergeString(ref Publisher, b.Publisher, merge)) != EFieldFlag.None) { FieldFlags[14] |= c; all |= c; }
if ( true && (c = MergeString(ref Origin, b.Origin, merge)) != EFieldFlag.None) { FieldFlags[15] |= c; all |= c; }
if ( true && (c = MergeString(ref Region, b.Region, merge)) != EFieldFlag.None) { FieldFlags[16] |= c; all |= c; }
if ( true && (c = MergeString(ref Tags, b.Tags, merge)) != EFieldFlag.None) { FieldFlags[17] |= c; all |= c; }
if (!metaOnly && (c = MergeString(ref CRC, b.CRC, merge)) != EFieldFlag.None) { FieldFlags[18] |= c; all |= c; }
if (!metaOnly && (c = MergeString(ref MD5, b.MD5, merge)) != EFieldFlag.None) { FieldFlags[19] |= c; all |= c; }
if (!metaOnly && (c = MergeString(ref SHA1, b.SHA1, merge)) != EFieldFlag.None) { FieldFlags[20] |= c; all |= c; }
if (!metaOnly && (c = MergeString(ref Serial, b.Serial, merge)) != EFieldFlag.None) { FieldFlags[21] |= c; all |= c; }
if ((all & EFieldFlag.Modified) != 0) RowFlags |= ERowFlag.Modified;
if ((all & EFieldFlag.Warning) != 0) RowFlags |= warningType;
if ((b.RowFlags & warningType) != 0)
{
RowFlags |= warningType;
for (int i = 0; i != FieldFlags.Length; i++)
FieldFlags[i] |= (b.FieldFlags[i] & EFieldFlag.Warning);
}
}
public void SetRegionAndTags(string region, string tags)
{
EFieldFlag c, all = EFieldFlag.None;
if (region != null && (c = (MergeString(ref Region, region, true) )) != EFieldFlag.None) { FieldFlags[(int)EFieldIndices.Region] |= c; all |= c; }
if (tags != null && (c = (MergeString(ref Tags, tags, true) & EFieldFlag.Modified)) != EFieldFlag.None) { FieldFlags[(int)EFieldIndices.Tags ] |= c; all |= c; }
if ((all & EFieldFlag.Modified) != 0) RowFlags |= ERowFlag.Modified;
if ((all & EFieldFlag.Warning) != 0) RowFlags |= ERowFlag.WarningToolMergeError;
}
public void Revert(string propertyName)
{
System.Reflection.PropertyInfo piVal = typeof(Entry).GetProperty(propertyName);
System.Reflection.FieldInfo fiOrg = typeof(Entry).GetField("Org" + propertyName.Substring(3));
piVal.SetValue(this, fiOrg.GetValue(this));
}
}
static class DAT
{
public static string HeaderName, HeaderDescription, HeaderVersion, HeaderHomepage;
public static bool SerialInRom;
}
class EntryList : List<Entry>, System.ComponentModel.IBindingList
{
public class SortPropDesc : System.ComponentModel.PropertyDescriptor
{
public SortPropDesc(string name) : base(name, null) { }
public override bool CanResetValue(object c) { return false; }
public override Type ComponentType { get { return null; } }
public override object GetValue(object c) { return null; }
public override bool IsReadOnly { get { return false; } }
public override Type PropertyType { get { return null; } }
public override void ResetValue(object c) { }
public override void SetValue(object c, object v) { }
public override bool ShouldSerializeValue(object c) { return false; }
}
public SortPropDesc SortProp;
public bool SortDesc;
public bool AllowEdit { get { return true; } }
public bool IsSorted { get { return (SortProp != null); } }
public bool SupportsChangeNotification { get { return true; } }
public bool SupportsSorting { get { return true; } }
public bool SupportsSearching { get { return false; } }
public bool AllowNew { get { return false; } }
public bool AllowRemove { get { return false; } }
public System.ComponentModel.ListSortDirection SortDirection { get { return (SortDesc ? System.ComponentModel.ListSortDirection.Descending : System.ComponentModel.ListSortDirection.Ascending); } }
public System.ComponentModel.PropertyDescriptor SortProperty { get { return SortProp; } }
public void AddIndex(System.ComponentModel.PropertyDescriptor property) { }
public object AddNew() { return null; }
public void ApplySort(System.ComponentModel.PropertyDescriptor property, System.ComponentModel.ListSortDirection direction) { }
public int Find(System.ComponentModel.PropertyDescriptor property, object key) { return 0; }
public void RemoveIndex(System.ComponentModel.PropertyDescriptor property) { }
public void RemoveSort() { }
public event System.ComponentModel.ListChangedEventHandler ListChanged;
public void BroadcastListChanged() { if (ListChanged != null) ListChanged(this, new System.ComponentModel.ListChangedEventArgs(System.ComponentModel.ListChangedType.Reset, 0)); }
enum MsgPackType : byte
{
FIXMAP = 0x80, FIXARRAY = 0x90, FIXSTR = 0xa0,
NIL = 0xc0, FALSE = 0xc2, TRUE = 0xc3,
BIN8 = 0xc4, BIN16 = 0xc5, BIN32 = 0xc6,
UINT8 = 0xcc, UINT16 = 0xcd, UINT32 = 0xce, UINT64 = 0xcf,
INT8 = 0xd0, INT16 = 0xd1, INT32 = 0xd2, INT64 = 0xd3,
STR8 = 0xd9, STR16 = 0xda, STR32 = 0xdb,
ARRAY16 = 0xdc, ARRAY32 = 0xdd,
MAP16 = 0xde, MAP32 = 0xdf,
};
enum MsgPackMode : byte { Map, Array, String, Bin, Boolean, Signed, Unsigned, Nil };
[System.Runtime.InteropServices.StructLayout(System.Runtime.InteropServices.LayoutKind.Explicit)] struct MsgPackValue
{
[System.Runtime.InteropServices.FieldOffset(0)] public MsgPackMode mode;
[System.Runtime.InteropServices.FieldOffset(8)] public UInt64 uint64;
[System.Runtime.InteropServices.FieldOffset(8)] public Int64 int64;
[System.Runtime.InteropServices.FieldOffset(8)] public bool b;
[System.Runtime.InteropServices.FieldOffset(8)] public int offset;
[System.Runtime.InteropServices.FieldOffset(8)] public int obj;
[System.Runtime.InteropServices.FieldOffset(12)] public int len;
public string UTF8String(byte[] rdb) { return System.Text.Encoding.UTF8.GetString(rdb, offset, len); }
public string ASCIIString(byte[] rdb) { return System.Text.Encoding.ASCII.GetString(rdb, offset, len); }
public string HEXString(byte[] rdb) { return BitConverter.ToString(rdb, offset, len).Replace("-", ""); }
}
static MsgPackValue rmsgpack_read(byte[] rdb, List<object> objs, ref int off)
{
byte type = rdb[off++];
MsgPackValue res = default(MsgPackValue);
UInt64 len;
switch (type)
{
case (byte)MsgPackType.NIL: res.mode = MsgPackMode.Nil; break;
case (byte)MsgPackType.FALSE: res.mode = MsgPackMode.Boolean; res.b = false; break;
case (byte)MsgPackType.TRUE: res.mode = MsgPackMode.Boolean; res.b = true; break;
case (byte)MsgPackType.STR8: len = rdb[off++]; goto READ_STRING;
case (byte)MsgPackType.STR16: len = BitConverter.ToUInt16(rdb, off).FromBigEndian(); off += 2; goto READ_STRING;
case (byte)MsgPackType.STR32: len = BitConverter.ToUInt32(rdb, off).FromBigEndian(); off += 4; goto READ_STRING;
READ_STRING:
res.mode = MsgPackMode.String;
res.offset = off;
res.len = (int)len;
off += (int)len;
break;
case (byte)MsgPackType.BIN8: len = rdb[off++]; goto READ_BIN;
case (byte)MsgPackType.BIN16: len = BitConverter.ToUInt16(rdb, off).FromBigEndian(); off += 2; goto READ_BIN;
case (byte)MsgPackType.BIN32: len = BitConverter.ToUInt32(rdb, off).FromBigEndian(); off += 4; goto READ_BIN;
READ_BIN:
res.mode = MsgPackMode.Bin;
res.offset = off;
res.len = (int)len;
off += (int)len;
break;
case (byte)MsgPackType.UINT8: res.mode = MsgPackMode.Unsigned; res.uint64 = rdb[off++]; break;
case (byte)MsgPackType.UINT16: res.mode = MsgPackMode.Unsigned; res.uint64 = BitConverter.ToUInt16(rdb, off).FromBigEndian(); off += 2; break;
case (byte)MsgPackType.UINT32: res.mode = MsgPackMode.Unsigned; res.uint64 = BitConverter.ToUInt32(rdb, off).FromBigEndian(); off += 4; break;
case (byte)MsgPackType.UINT64: res.mode = MsgPackMode.Unsigned; res.uint64 = BitConverter.ToUInt64(rdb, off).FromBigEndian(); off += 8; break;
case (byte)MsgPackType.INT8: res.mode = MsgPackMode.Signed; res.int64 = (sbyte)rdb[off++]; break;
case (byte)MsgPackType.INT16: res.mode = MsgPackMode.Signed; res.int64 = BitConverter.ToInt16(rdb, off).FromBigEndian(); off += 2; break;
case (byte)MsgPackType.INT32: res.mode = MsgPackMode.Signed; res.int64 = BitConverter.ToInt32(rdb, off).FromBigEndian(); off += 4; break;
case (byte)MsgPackType.INT64: res.mode = MsgPackMode.Signed; res.int64 = BitConverter.ToInt64(rdb, off).FromBigEndian(); off += 8; break;
case (byte)MsgPackType.ARRAY16: len = BitConverter.ToUInt16(rdb, off).FromBigEndian(); off += 2; goto READ_ARRAY;
case (byte)MsgPackType.ARRAY32: len = BitConverter.ToUInt32(rdb, off).FromBigEndian(); off += 4; goto READ_ARRAY;
READ_ARRAY:
MsgPackValue[] arr = new MsgPackValue[len];
res.mode = MsgPackMode.Array;
res.obj = objs.Count;
objs.Add(arr);
for (UInt64 i = 0; i != len; i++)
arr[i] = rmsgpack_read(rdb, objs, ref off);
break;
case (byte)MsgPackType.MAP16: len = BitConverter.ToUInt16(rdb, off).FromBigEndian(); off += 2; goto READ_MAP;
case (byte)MsgPackType.MAP32: len = BitConverter.ToUInt32(rdb, off).FromBigEndian(); off += 4; goto READ_MAP;
READ_MAP:
Dictionary<string, MsgPackValue> map = new Dictionary<string, MsgPackValue>((int)len);
res.mode = MsgPackMode.Map;
res.obj = objs.Count;
objs.Add(map);
for (UInt64 i = 0; i != len; i++)
{
MsgPackValue key = rmsgpack_read(rdb, objs, ref off);
if (key.mode != MsgPackMode.String) throw new Exception("RDB Key must be a string");
MsgPackValue val = rmsgpack_read(rdb, objs, ref off);
try { map[string.Intern(key.UTF8String(rdb))] = val; } catch { }
}
break;
default:
if (type < (byte)MsgPackType.FIXMAP) { res.mode = MsgPackMode.Signed; res.int64 = type; }
else if (type < (byte)MsgPackType.FIXARRAY) { len = (UInt64)(type - (byte)MsgPackType.FIXMAP); goto READ_MAP; }
else if (type < (byte)MsgPackType.FIXSTR) { len = (UInt64)(type - (byte)MsgPackType.FIXARRAY); goto READ_ARRAY; }
else if (type < (byte)MsgPackType.NIL) { len = (UInt64)(type - (byte)MsgPackType.FIXSTR); goto READ_STRING; }
else if (type > (byte)MsgPackType.MAP32) { res.mode = MsgPackMode.Signed; res.int64 = (int)type - 0xff - 1; }
break;
}
return res;
}
static MsgPackMode rmsgpack_dump(byte[] rdb, System.Text.StringBuilder sb, ref int off)
{
byte type = rdb[off++];
UInt64 len;
switch (type)
{
case (byte)MsgPackType.NIL: sb.AppendLine("[NIL]"); return MsgPackMode.Nil;
case (byte)MsgPackType.FALSE: sb.AppendLine("[BOOL] FALSE"); return MsgPackMode.Boolean;
case (byte)MsgPackType.TRUE: sb.AppendLine("[BOOL] TRUE"); return MsgPackMode.Boolean;
case (byte)MsgPackType.STR8: sb.Append("[STR8]"); len = rdb[off++]; goto READ_STRING;
case (byte)MsgPackType.STR16: sb.Append("[STR16]"); len = BitConverter.ToUInt16(rdb, off).FromBigEndian(); off += 2; goto READ_STRING;
case (byte)MsgPackType.STR32: sb.Append("[STR32]"); len = BitConverter.ToUInt32(rdb, off).FromBigEndian(); off += 4; goto READ_STRING;
READ_STRING:
sb.AppendLine(" [" + len.ToString() + "] " + System.Text.Encoding.UTF8.GetString(rdb, off, (int)len));
off += (int)len;
return MsgPackMode.String;
case (byte)MsgPackType.BIN8: sb.Append("[BIN8]"); len = rdb[off++]; goto READ_BIN;
case (byte)MsgPackType.BIN16: sb.Append("[BIN16]"); len = BitConverter.ToUInt16(rdb, off).FromBigEndian(); off += 2; goto READ_BIN;
case (byte)MsgPackType.BIN32: sb.Append("[BIN32]"); len = BitConverter.ToUInt32(rdb, off).FromBigEndian(); off += 4; goto READ_BIN;
READ_BIN:
sb.AppendLine(" [" + len.ToString() + "] " + BitConverter.ToString(rdb, off, (int)len).Replace("-", ""));
off += (int)len;
return MsgPackMode.Bin;
case (byte)MsgPackType.UINT8: sb.AppendLine("[UINT8] " + (rdb[off++] ).ToString()); return MsgPackMode.Unsigned;
case (byte)MsgPackType.UINT16: sb.AppendLine("[UINT16] " + (BitConverter.ToUInt16(rdb, off).FromBigEndian()).ToString()); off += 2; return MsgPackMode.Unsigned;
case (byte)MsgPackType.UINT32: sb.AppendLine("[UINT32] " + (BitConverter.ToUInt32(rdb, off).FromBigEndian()).ToString()); off += 4; return MsgPackMode.Unsigned;
case (byte)MsgPackType.UINT64: sb.AppendLine("[UINT64] " + (BitConverter.ToUInt64(rdb, off).FromBigEndian()).ToString()); off += 8; return MsgPackMode.Unsigned;
case (byte)MsgPackType.INT8: sb.AppendLine("[INT8] " + ((sbyte)rdb[off++] ).ToString()); return MsgPackMode.Signed;
case (byte)MsgPackType.INT16: sb.AppendLine("[INT16] " + (BitConverter.ToInt16(rdb, off).FromBigEndian() ).ToString()); off += 2; return MsgPackMode.Signed;
case (byte)MsgPackType.INT32: sb.AppendLine("[INT32] " + (BitConverter.ToInt32(rdb, off).FromBigEndian() ).ToString()); off += 4; return MsgPackMode.Signed;
case (byte)MsgPackType.INT64: sb.AppendLine("[INT64] " + (BitConverter.ToInt64(rdb, off).FromBigEndian() ).ToString()); off += 8; return MsgPackMode.Signed;
case (byte)MsgPackType.ARRAY16: sb.Append("[ARRAY16]"); len = BitConverter.ToUInt16(rdb, off).FromBigEndian(); off += 2; goto READ_ARRAY;
case (byte)MsgPackType.ARRAY32: sb.Append("[ARRAY32]"); len = BitConverter.ToUInt32(rdb, off).FromBigEndian(); off += 4; goto READ_ARRAY;
READ_ARRAY:
sb.AppendLine(" [" + len.ToString() + "]");
for (UInt64 i = 0; i != len; i++)
{
sb.Append(" [" + i.ToString() + "] ");
rmsgpack_dump(rdb, sb, ref off);
}
return MsgPackMode.Array;
case (byte)MsgPackType.MAP16: sb.Append("[MAP16]"); len = BitConverter.ToUInt16(rdb, off).FromBigEndian(); off += 2; goto READ_MAP;
case (byte)MsgPackType.MAP32: sb.Append("[MAP32]"); len = BitConverter.ToUInt32(rdb, off).FromBigEndian(); off += 4; goto READ_MAP;
READ_MAP:
sb.AppendLine(" [" + len.ToString() + "]");
for (UInt64 i = 0; i != len; i++)
{
sb.Append(" [KEY] ");
if (rmsgpack_dump(rdb, sb, ref off) != MsgPackMode.String) throw new Exception("RDB Key must be a string");
sb.Append(" [VAL] ");
rmsgpack_dump(rdb, sb, ref off);
}
return MsgPackMode.Map;
default:
if (type < (byte)MsgPackType.FIXMAP) { sb.AppendLine("[SMALLPOSNUM] " + type.ToString()); return MsgPackMode.Signed; }
else if (type < (byte)MsgPackType.FIXARRAY) { sb.Append("[FIXMAP]"); len = (UInt64)(type - (byte)MsgPackType.FIXMAP); goto READ_MAP; }
else if (type < (byte)MsgPackType.FIXSTR) { sb.Append("[FIXARRAY]"); len = (UInt64)(type - (byte)MsgPackType.FIXARRAY); goto READ_ARRAY; }
else if (type < (byte)MsgPackType.NIL) { sb.Append("[FIXSTR]"); len = (UInt64)(type - (byte)MsgPackType.FIXSTR); goto READ_STRING; }
else if (type > (byte)MsgPackType.MAP32) { sb.AppendLine("[SMALLNEGNUM] " + ((int)type - 0xff - 1).ToString()); return MsgPackMode.Signed; }
break;
}
throw new Exception("Unknown MSGPACK type");
}
public static void DumpRDB(string rdbPath, string outPath)
{
byte[] rdb = File.ReadAllBytes(rdbPath);
UInt64 magic = (rdb.Length >= 8 ? BitConverter.ToUInt64(rdb, 0).FromBigEndian() : 0);
UInt64 metaoffset = (rdb.Length >= 16 ? BitConverter.ToUInt64(rdb, 8).FromBigEndian() : 0);
System.Text.StringBuilder sb = new System.Text.StringBuilder();
sb.AppendLine("RDB IDENTIFIER: " + magic.ToString() + (magic == 0x5241524348444200 ? " (VALID \"RARCHDB\\0\")" : " (INVALID)"));
sb.AppendLine("META DATA OFFSET: " + metaoffset.ToString());
int off = (int)metaoffset, dataend = (metaoffset > 16 ? (int)metaoffset : (int)rdb.LongLength);
if (metaoffset > 16)
{
sb.AppendLine("-------------------- META START --------------------");
while (off < rdb.LongLength) rmsgpack_dump(rdb, sb, ref off);
sb.AppendLine("-------------------- META END --------------------");
}
off = 16;
while (off < dataend)
{
sb.AppendLine("");
rmsgpack_dump(rdb, sb, ref off);
}
File.WriteAllText(outPath, sb.ToString());
}
void LoadRDB(string rdbPath, bool isInit)
{
List<object> objs = new List<object>();
byte[] rdb = File.ReadAllBytes(rdbPath);
UInt64 magic = BitConverter.ToUInt64(rdb, 0).FromBigEndian();
if (magic != 0x5241524348444200) throw new Exception("File is not in RDB format");
UInt64 offset = BitConverter.ToUInt64(rdb, 8).FromBigEndian();
Int64 count = Int64.MaxValue;
int off = (int)offset;
if (offset != 0)
{
int obj = rmsgpack_read(rdb, objs, ref off).obj;
Dictionary<string, MsgPackValue> meta = (Dictionary<string, MsgPackValue>)objs[obj];
count = meta["count"].int64;
}
string rdbKeyName = string.Intern("name" );
string rdbKeyDescription = string.Intern("description" );
string rdbKeyGenre = string.Intern("genre" );
string rdbKeyRomName = string.Intern("rom_name" );
string rdbKeySize = string.Intern("size" );
string rdbKeyUsers = string.Intern("users" );
string rdbKeyReleaseDay = string.Intern("releaseday" );
string rdbKeyReleaseMonth = string.Intern("releasemonth" );
string rdbKeyReleaseYear = string.Intern("releaseyear" );
string rdbKeyRumble = string.Intern("rumble" );
string rdbKeyAnalog = string.Intern("analog" );
string rdbKeyCoop = string.Intern("coop" );
string rdbKeyEnhancementHW = string.Intern("enhancement_hw");
string rdbKeyFranchise = string.Intern("franchise" );
string rdbKeyOriginalTitle = string.Intern("original_title");
string rdbKeyDeveloper = string.Intern("developer" );
string rdbKeyPublisher = string.Intern("publisher" );
string rdbKeyOrigin = string.Intern("origin" );
string rdbKeyRegion = string.Intern("region" );
string rdbKeyTags = string.Intern("tags" );
string rdbKeyCRC = string.Intern("crc" );
string rdbKeyMD5 = string.Intern("md5" );
string rdbKeySHA1 = string.Intern("sha1" );
string rdbKeySerial = string.Intern("serial" );
off = 16;
MsgPackValue v;
for (Int64 i = 0; i != count && off != rdb.LongLength; i++)
{
MsgPackValue mpv = rmsgpack_read(rdb, objs, ref off);
if (mpv.mode != MsgPackMode.Map) continue;
Dictionary<string, MsgPackValue> row = (Dictionary<string, MsgPackValue>)objs[mpv.obj];
if (off == rdb.LongLength && row.Count == 1 && row.ContainsKey("count")) continue; // skip count that couldn't be found via offset
Entry e = new Entry();
int releaseDay = 0, releaseMonth = 0, releaseYear = 0;
if (row.TryGetValue(rdbKeyName , out v) && v.mode == MsgPackMode.String ) e.Name = v.UTF8String(rdb);
if (row.TryGetValue(rdbKeyDescription , out v) && v.mode == MsgPackMode.String ) e.Description = v.UTF8String(rdb);
if (row.TryGetValue(rdbKeyGenre , out v) && v.mode == MsgPackMode.String ) e.Genre = v.UTF8String(rdb);
if (row.TryGetValue(rdbKeyRomName , out v) && v.mode == MsgPackMode.String ) e.RomName = v.UTF8String(rdb);
if (row.TryGetValue(rdbKeySize , out v) && v.mode == MsgPackMode.Unsigned) e.Size = (UInt64)v.uint64;
if (row.TryGetValue(rdbKeyUsers , out v) && v.mode == MsgPackMode.Unsigned) e.Users = (uint)v.uint64;
if (row.TryGetValue(rdbKeyReleaseDay , out v) && v.mode == MsgPackMode.Unsigned) releaseDay = (int)v.uint64;
if (row.TryGetValue(rdbKeyReleaseMonth , out v) && v.mode == MsgPackMode.Unsigned) releaseMonth = (int)v.uint64;
if (row.TryGetValue(rdbKeyReleaseYear , out v) && v.mode == MsgPackMode.Unsigned) releaseYear = (int)v.uint64;
if (row.TryGetValue(rdbKeyRumble , out v) && v.mode == MsgPackMode.Unsigned) e.Rumble = v.uint64 != 0;
if (row.TryGetValue(rdbKeyAnalog , out v) && v.mode == MsgPackMode.Unsigned) e.Analog = v.uint64 != 0;
if (row.TryGetValue(rdbKeyCoop , out v) && v.mode == MsgPackMode.Unsigned) e.Coop = v.uint64 != 0;
if (row.TryGetValue(rdbKeyEnhancementHW, out v) && v.mode == MsgPackMode.String ) e.EnhancementHW = v.UTF8String(rdb);
if (row.TryGetValue(rdbKeyFranchise , out v) && v.mode == MsgPackMode.String ) e.Franchise = v.UTF8String(rdb);
if (row.TryGetValue(rdbKeyOriginalTitle, out v) && v.mode == MsgPackMode.String ) e.OriginalTitle = v.UTF8String(rdb);
if (row.TryGetValue(rdbKeyDeveloper , out v) && v.mode == MsgPackMode.String ) e.Developer = v.UTF8String(rdb);
if (row.TryGetValue(rdbKeyPublisher , out v) && v.mode == MsgPackMode.String ) e.Publisher = v.UTF8String(rdb);
if (row.TryGetValue(rdbKeyOrigin , out v) && v.mode == MsgPackMode.String ) e.Origin = v.UTF8String(rdb);
if (row.TryGetValue(rdbKeyRegion , out v) && v.mode == MsgPackMode.String ) e.Region = v.UTF8String(rdb);
if (row.TryGetValue(rdbKeyTags , out v) && v.mode == MsgPackMode.String ) e.Tags = v.UTF8String(rdb);
if (row.TryGetValue(rdbKeyCRC , out v) && v.mode == MsgPackMode.Bin ) e.CRC = v.HEXString(rdb);
if (row.TryGetValue(rdbKeyMD5 , out v) && v.mode == MsgPackMode.Bin ) e.MD5 = v.HEXString(rdb);
if (row.TryGetValue(rdbKeySHA1 , out v) && v.mode == MsgPackMode.Bin ) e.SHA1 = v.HEXString(rdb);
if (row.TryGetValue(rdbKeySerial , out v) && v.mode == MsgPackMode.Bin ) e.Serial = v.ASCIIString(rdb);
e.SetDate(releaseDay, releaseMonth, releaseYear);
if (isInit) e.SetOrg();
Add(e);
}
}
void LoadDAT(string datPath, bool isInit)
{
string dat = File.ReadAllText(datPath, System.Text.Encoding.UTF8).Replace("\r", "");
Regex rexCombineSpace = new Regex(@"[\r\n\s]+", RegexOptions.Singleline);
Regex rexNextKey = new Regex(@"\G\s*(\)|[a-z0-9_]*)");
Regex rexNextHex = new Regex(@"\G\s*([0-9A-Fa-f]+) ");
Regex rexNextString = new Regex(@"\G\s*\""(.*?(?<!\\)(?:(\\\\)*))[\""]");
Regex rexNextNumeric = new Regex(@"\G\s*(\d+)");
Regex rexNextObject = new Regex(@"\G\s*\(");
string datKeyName = string.Intern("name");
string datKeyDescription = string.Intern("description");
string datKeyGenre = string.Intern("genre");
string datKeySize = string.Intern("size");
string datKeyUsers = string.Intern("users");
string datKeyReleaseDay = string.Intern("releaseday");
string datKeyReleaseMonth = string.Intern("releasemonth");
string datKeyReleaseYear = string.Intern("releaseyear");
string datKeyRumble = string.Intern("rumble");
string datKeyAnalog = string.Intern("analog");
string datKeyEnhancementHW = string.Intern("enhancement_hw");
string datKeyFranchise = string.Intern("franchise");
string datKeyOriginalTitle = string.Intern("original_title");
string datKeyDeveloper = string.Intern("developer");
string datKeyPublisher = string.Intern("publisher");
string datKeyOrigin = string.Intern("origin");
string datKeyRegion = string.Intern("region");
string datKeyTags = string.Intern("tags");
string datKeyCoop = string.Intern("coop");
string datKeyCRC = string.Intern("crc");
string datKeyMD5 = string.Intern("md5");
string datKeySHA1 = string.Intern("sha1");
string datKeySerial = string.Intern("serial");
string datKeyVersion = string.Intern("version");
string datKeyHomepage = string.Intern("homepage");
string game = null;
int off = 0;
List<Dictionary<string, string>> roms = new List<Dictionary<string,string>>();
System.Func<int, Dictionary<string, string>> ParseDATObject = null;
ParseDATObject = (int depth) =>
{
Dictionary<string, string> obj = new Dictionary<string,string>();
for (;;)
{
Match mKey = rexNextKey.Match(game, off), mVal;
off += mKey.Groups[0].Length;
string key = String.Intern(mKey.Groups[1].Value);
if (key.Equals(")") || key.Equals(""))
{
if (depth == 0 && off != game.Length) { throw new Exception("Invalid string in DAT entry [" + game + "]"); }
return obj;
}
else if (key.Equals("crc") || key.Equals("md5") || key.Equals("sha1"))
{
Match mHex = rexNextHex.Match(game, off);
off += mHex.Groups[0].Length;
obj.Add(key, mHex.Groups[1].Value);
}
else if ((mVal = rexNextString.Match(game, off)).Success)
{
off += mVal.Groups[0].Length;
obj.Add(key, mVal.Groups[1].Value);
}
else if ((mVal = rexNextNumeric.Match(game, off)).Success)
{
off += mVal.Groups[0].Length;
obj.Add(key, mVal.Groups[1].Value);
}
else if ((mVal = rexNextObject.Match(game, off)).Success && key.Equals("rom"))
{
off += mVal.Groups[0].Length;
roms.Add(ParseDATObject(depth + 1));
}
else
{
throw new Exception("Could not parse value of key " + key + " in DAT entry [" + game + "]");
}
}
};
if (isInit)
{
foreach (Match m in Regex.Matches(dat, @"clrmamepro\s*\((.*?)\n+\)", RegexOptions.Singleline))
{
game = rexCombineSpace.Replace(m.Groups[1].Value, " ");
off = 0;
Dictionary<string, string> obj = ParseDATObject(0);
string v;
if (obj.TryGetValue(datKeyName, out v)) DAT.HeaderName = v;
if (obj.TryGetValue(datKeyDescription, out v)) DAT.HeaderDescription = v;
if (obj.TryGetValue(datKeyVersion, out v)) DAT.HeaderVersion = v;
if (obj.TryGetValue(datKeyHomepage, out v)) DAT.HeaderHomepage = v;
}
if (game == null) throw new Exception("DAT file is missing clrmamepro header");
}
foreach (Match m in Regex.Matches(dat, @"\ngame\s*\((.*?)\n+\)", RegexOptions.Singleline))
{
game = rexCombineSpace.Replace(m.Groups[1].Value, " ");
roms.Clear();
off = 0;
Dictionary<string, string> obj = ParseDATObject(0);
if (roms.Count == 0) roms.Add(new Dictionary<string, string>());
foreach (Dictionary<string, string> rom in roms)
{
Entry e = new Entry();
int releaseDay = 0, releaseMonth = 0, releaseYear = 0;
string v;
if (obj.TryGetValue(datKeyName , out v)) e.Name = v;
if (obj.TryGetValue(datKeyDescription , out v)) e.Description = v;
if (obj.TryGetValue(datKeyGenre , out v)) e.Genre = v;
if (rom.TryGetValue(datKeyName , out v)) e.RomName = v;
if (rom.TryGetValue(datKeySize , out v)) e.Size = Convert.ToUInt64(v);
if (obj.TryGetValue(datKeyUsers , out v)) e.Users = Convert.ToUInt32(v);
if (obj.TryGetValue(datKeyReleaseDay , out v)) releaseDay = Convert.ToInt32(v);
if (obj.TryGetValue(datKeyReleaseMonth , out v)) releaseMonth = Convert.ToInt32(v);
if (obj.TryGetValue(datKeyReleaseYear , out v)) releaseYear = Convert.ToInt32(v);
if (obj.TryGetValue(datKeyRumble , out v)) e.Rumble = Convert.ToInt32(v) != 0;
if (obj.TryGetValue(datKeyAnalog , out v)) e.Analog = Convert.ToInt32(v) != 0;
if (obj.TryGetValue(datKeyCoop , out v)) e.Coop = Convert.ToInt32(v) != 0;
if (obj.TryGetValue(datKeyEnhancementHW, out v)) e.EnhancementHW = v;
if (obj.TryGetValue(datKeyFranchise , out v)) e.Franchise = v;
if (obj.TryGetValue(datKeyOriginalTitle, out v)) e.OriginalTitle = v;
if (obj.TryGetValue(datKeyDeveloper , out v)) e.Developer = v;
if (obj.TryGetValue(datKeyPublisher , out v)) e.Publisher = v;
if (obj.TryGetValue(datKeyOrigin , out v)) e.Origin = v;
if (obj.TryGetValue(datKeyRegion , out v)) e.Region = v;
if (obj.TryGetValue(datKeyTags , out v)) e.Tags = v;
if (rom.TryGetValue(datKeyCRC , out v)) e.CRC = v;
if (rom.TryGetValue(datKeyMD5 , out v)) e.MD5 = v;
if (rom.TryGetValue(datKeySHA1 , out v)) e.SHA1 = v;
if (rom.TryGetValue(datKeySerial , out v)) e.Serial = v;
if (obj.TryGetValue(datKeySerial , out v)){e.Serial = v; if (isInit) DAT.SerialInRom = true; }
e.SetDate(releaseDay, releaseMonth, releaseYear);
if (isInit) e.SetOrg();
Add(e);
}
}
}
public void SaveRDB(string path)
{
BinaryWriter f = new BinaryWriter(File.Create(path), System.Text.Encoding.UTF8, false);
f.Write(new byte[] { (byte)'R',(byte)'A',(byte)'R',(byte)'C',(byte)'H',(byte)'D',(byte)'B',0 });
f.Write((ulong)0); //offset
byte[] writeBuf = new byte[1024];
Action<string> MsgPackWriteString = (string s) =>
{
int len = System.Text.Encoding.UTF8.GetByteCount(s);
if (len < ((int)MsgPackType.NIL - (int)MsgPackType.FIXSTR)) f.Write((byte)(len + (int)MsgPackType.FIXSTR));
else if (len < 256) { f.Write((byte)MsgPackType.STR8); f.Write((byte)len); }
else if (len < 65536) { f.Write((byte)MsgPackType.STR16); f.Write(((ushort)len).ToBigEndian()); }
else { f.Write((byte)MsgPackType.STR32); f.Write(((uint)len).ToBigEndian()); }
if (len > writeBuf.Length) writeBuf = new byte[len];
System.Text.Encoding.UTF8.GetBytes(s, 0, s.Length, writeBuf, 0);
f.Write(writeBuf, 0, len);
};
Action<string> MsgPackWriteBinAscii = (string s) =>
{
int len = System.Text.Encoding.UTF8.GetByteCount(s);
if (len < 256) { f.Write((byte)MsgPackType.BIN8); f.Write((byte)len); }
else if (len < 65536) { f.Write((byte)MsgPackType.BIN16); f.Write(((ushort)len).ToBigEndian()); }
else { f.Write((byte)MsgPackType.BIN32); f.Write(((uint)len).ToBigEndian()); }
if (len > writeBuf.Length) writeBuf = new byte[len];
System.Text.Encoding.UTF8.GetBytes(s, 0, s.Length, writeBuf, 0);
f.Write(writeBuf, 0, len);
};
Action<string> MsgPackWriteBinHex = (string hex) =>
{
int len = hex.Length / 2;
if (len < 256) { f.Write((byte)MsgPackType.BIN8); f.Write((byte)len); }
else if (len < 65536) { f.Write((byte)MsgPackType.BIN16); f.Write(((ushort)len).ToBigEndian()); }
else { f.Write((byte)MsgPackType.BIN32); f.Write(((uint)len).ToBigEndian()); }
if (len > writeBuf.Length) writeBuf = new byte[len];
for (int i = 0; i < len; i++)
{
int c = hex[i*2], d = hex[i*2+1];
writeBuf[i] = (byte)(((c <= (int)'9' ? c - (int)'0' : (c <= (int)'F' ? c - ((int)'A' - 10) : c - ((int)'a' - 10))) << 4)
| ((d <= (int)'9' ? d - (int)'0' : (d <= (int)'F' ? d - ((int)'A' - 10) : d - ((int)'a' - 10))) ));
}
f.Write(writeBuf, 0, len);
};
Action<int> MsgPackWriteMap = (int count) =>
{
if (count < 16) f.Write((byte)((byte)MsgPackType.FIXMAP + count));
else { f.Write((byte)MsgPackType.MAP16); f.Write(((ushort)count).ToBigEndian()); }
};
//Action<int> MsgPackWriteInt = (int i) =>
//{
// if (i >= 0 && i < (int)MsgPackType.FIXMAP) { f.Write((byte)i); }
// else if (i < 0 && i >= (0xff - (int)MsgPackType.MAP32)) { f.Write((byte)(256+i)); }
// else if (i >= -128 && i < 128) { f.Write((byte)MsgPackType.INT8); f.Write((sbyte)i); }
// else if (i >= -65536 && i < 65536) { f.Write((byte)MsgPackType.INT16); f.Write(((short)i).ToBigEndian()); }
// else { f.Write((byte)MsgPackType.INT32); f.Write(((int)i).ToBigEndian()); }
//};
Action<uint> MsgPackWriteUint = (uint i) =>
{
if (i < 256) { f.Write((byte)MsgPackType.UINT8); f.Write((byte)i); }
else if (i < 65536) { f.Write((byte)MsgPackType.UINT16); f.Write(((ushort)i).ToBigEndian()); }
else { f.Write((byte)MsgPackType.UINT32); f.Write(((uint)i).ToBigEndian()); }
};
Action<UInt64> MsgPackWriteUint64 = (UInt64 i) =>
{
if (i < 256) { f.Write((byte)MsgPackType.UINT8); f.Write((byte)i); }
else if (i < 65536) { f.Write((byte)MsgPackType.UINT16); f.Write(((ushort)i).ToBigEndian()); }
else if (i < 4294967295) { f.Write((byte)MsgPackType.UINT32); f.Write(((uint)i).ToBigEndian()); }
else { f.Write((byte)MsgPackType.UINT64); f.Write(((UInt64)i).ToBigEndian()); }
};
uint entryCount = 0;
foreach (Entry e in this)
{
int count = 0;
if (!string.IsNullOrWhiteSpace(e.Name )) count++;
if (!string.IsNullOrWhiteSpace(e.Description )) count++;
if (!string.IsNullOrWhiteSpace(e.Genre )) count++;
if (!string.IsNullOrWhiteSpace(e.RomName )) count++;
if (e.Size != 0) count++;
if (e.Users != 0) count++;
if (e.Release.ToBinary() != 0) count += 3;
if (e.Rumble ) count++;
if (e.Analog ) count++;
if (e.Coop ) count++;
if (!string.IsNullOrWhiteSpace(e.EnhancementHW)) count++;
if (!string.IsNullOrWhiteSpace(e.Franchise )) count++;
if (!string.IsNullOrWhiteSpace(e.OriginalTitle)) count++;
if (!string.IsNullOrWhiteSpace(e.Developer )) count++;
if (!string.IsNullOrWhiteSpace(e.Publisher )) count++;
if (!string.IsNullOrWhiteSpace(e.Origin )) count++;
if (!string.IsNullOrWhiteSpace(e.Region )) count++;
if (!string.IsNullOrWhiteSpace(e.Tags )) count++;
if (!string.IsNullOrWhiteSpace(e.CRC )) count++;
if (!string.IsNullOrWhiteSpace(e.MD5 )) count++;
if (!string.IsNullOrWhiteSpace(e.SHA1 )) count++;
if (!string.IsNullOrWhiteSpace(e.Serial )) count++;
if (count == 0) continue;
MsgPackWriteMap(count);
if (!string.IsNullOrWhiteSpace(e.Name )) { MsgPackWriteString("name" ); MsgPackWriteString(e.Name ); }
if (!string.IsNullOrWhiteSpace(e.Description )) { MsgPackWriteString("description" ); MsgPackWriteString(e.Description ); }
if (!string.IsNullOrWhiteSpace(e.Genre )) { MsgPackWriteString("genre" ); MsgPackWriteString(e.Genre ); }
if (!string.IsNullOrWhiteSpace(e.RomName )) { MsgPackWriteString("rom_name" ); MsgPackWriteString(e.RomName ); }
if (e.Size != 0) { MsgPackWriteString("size" ); MsgPackWriteUint64(e.Size ); }
if (e.Users != 0) { MsgPackWriteString("users" ); MsgPackWriteUint(e.Users ); }
if (e.Release.ToBinary() != 0) { MsgPackWriteString("releaseday" ); MsgPackWriteUint((uint)e.Release.Day ); }
if (e.Release.ToBinary() != 0) { MsgPackWriteString("releasemonth" ); MsgPackWriteUint((uint)e.Release.Month); }
if (e.Release.ToBinary() != 0) { MsgPackWriteString("releaseyear" ); MsgPackWriteUint((uint)e.Release.Year ); }
if (e.Rumble ) { MsgPackWriteString("rumble" ); MsgPackWriteUint(1); }
if (e.Analog ) { MsgPackWriteString("analog" ); MsgPackWriteUint(1); }
if (e.Coop ) { MsgPackWriteString("coop" ); MsgPackWriteUint(1); }
if (!string.IsNullOrWhiteSpace(e.EnhancementHW)) { MsgPackWriteString("enhancement_hw"); MsgPackWriteString(e.EnhancementHW ); }
if (!string.IsNullOrWhiteSpace(e.Franchise )) { MsgPackWriteString("franchise" ); MsgPackWriteString(e.Franchise ); }
if (!string.IsNullOrWhiteSpace(e.OriginalTitle)) { MsgPackWriteString("original_title"); MsgPackWriteString(e.OriginalTitle ); }
if (!string.IsNullOrWhiteSpace(e.Developer )) { MsgPackWriteString("developer" ); MsgPackWriteString(e.Developer ); }
if (!string.IsNullOrWhiteSpace(e.Publisher )) { MsgPackWriteString("publisher" ); MsgPackWriteString(e.Publisher ); }
if (!string.IsNullOrWhiteSpace(e.Origin )) { MsgPackWriteString("origin" ); MsgPackWriteString(e.Origin ); }
if (!string.IsNullOrWhiteSpace(e.Region )) { MsgPackWriteString("region" ); MsgPackWriteString(e.Region ); }
if (!string.IsNullOrWhiteSpace(e.Tags )) { MsgPackWriteString("tags" ); MsgPackWriteString(e.Tags ); }
if (!string.IsNullOrWhiteSpace(e.CRC )) { MsgPackWriteString("crc" ); MsgPackWriteBinHex(e.CRC ); }
if (!string.IsNullOrWhiteSpace(e.MD5 )) { MsgPackWriteString("md5" ); MsgPackWriteBinHex(e.MD5 ); }
if (!string.IsNullOrWhiteSpace(e.SHA1 )) { MsgPackWriteString("sha1" ); MsgPackWriteBinHex(e.SHA1 ); }
if (!string.IsNullOrWhiteSpace(e.Serial )) { MsgPackWriteString("serial" ); MsgPackWriteBinAscii(e.Serial ); }
entryCount++;
}
f.Write((byte)MsgPackType.NIL); //sentinel eof marker
ulong metaPos = (ulong)f.Seek(0, SeekOrigin.Current);
MsgPackWriteMap(1);
MsgPackWriteString("count");
MsgPackWriteUint(entryCount);
f.Seek(8, SeekOrigin.Begin);
f.Write(metaPos.ToBigEndian());
f.Dispose();
}
public void SaveDAT(string path, bool modificationsOnly = false, string key = null, bool emptyHeader = false)
{
StreamWriter w = new StreamWriter(path, false, new System.Text.UTF8Encoding(false));
w.NewLine = "\n";
w.WriteLine("clrmamepro (");
if (!emptyHeader)
{
if (!string.IsNullOrWhiteSpace(DAT.HeaderName )) { w.WriteLine(" name \"" + DAT.HeaderName .Replace('"', '\'') + "\""); }
if (!string.IsNullOrWhiteSpace(DAT.HeaderDescription)) { w.WriteLine(" description \"" + DAT.HeaderDescription .Replace('"', '\'') + "\""); }
if (!string.IsNullOrWhiteSpace(DAT.HeaderVersion )) { w.WriteLine(" version \"" + DAT.HeaderVersion .Replace('"', '\'') + "\""); }
if (!string.IsNullOrWhiteSpace(DAT.HeaderHomepage )) { w.WriteLine(" homepage \"" + DAT.HeaderHomepage .Replace('"', '\'') + "\""); }
}
w.WriteLine(")");
w.WriteLine("");
if (modificationsOnly)
{
EFieldIndices keyIndex = (EFieldIndices)Enum.Parse(typeof(EFieldIndices), key);
foreach (Entry e in this)
{
if ((e.RowFlags & ERowFlag.Modified) == 0) continue;
string ln = "game (" + "\n";
Action<EFieldIndices, string, string, bool> WriteString = (EFieldIndices idx, string field, string val, bool nl) =>
{ if (keyIndex == idx || (e.FieldFlags[(int)idx] & EFieldFlag.Modified) != 0) ln += (nl ? " " : " ") + field + " \"" + val.Replace('"', '\'') + "\"" + (nl ? "\n" : ""); };
Action<EFieldIndices, string, string, bool> WriteRaw = (EFieldIndices idx, string field, string val, bool nl) =>
{ if (keyIndex == idx || (e.FieldFlags[(int)idx] & EFieldFlag.Modified) != 0) ln += (nl ? " " : " ") + field + " " + val + (nl ? "\n" : ""); };
WriteString(EFieldIndices.Name , "name" , e.Name , true);
WriteString(EFieldIndices.Description , "description" , e.Description , true);
WriteString(EFieldIndices.Genre , "genre" , e.Genre , true);
WriteRaw (EFieldIndices.Users , "users " , e.Users .ToString(), true);
WriteRaw (EFieldIndices.Release , "releaseday " , e.Release.Day .ToString(), true);
WriteRaw (EFieldIndices.Release , "releasemonth " , e.Release.Month.ToString(), true);
WriteRaw (EFieldIndices.Release , "releaseyear " , e.Release.Year .ToString(), true);
WriteRaw (EFieldIndices.Rumble , "rumble" , (e.Rumble ? "1" : "0") , true);
WriteRaw (EFieldIndices.Analog , "analog" , (e.Analog ? "1" : "0") , true);
WriteRaw (EFieldIndices.Coop , "coop" , (e.Coop ? "1" : "0") , true);
WriteString(EFieldIndices.EnhancementHW, "enhancement_hw" , e.EnhancementHW , true);
WriteString(EFieldIndices.Franchise , "franchise" , e.Franchise , true);
WriteString(EFieldIndices.OriginalTitle, "original_title" , e.OriginalTitle , true);
WriteString(EFieldIndices.Developer , "developer" , e.Developer , true);
WriteString(EFieldIndices.Publisher , "publisher" , e.Publisher , true);
WriteString(EFieldIndices.Origin , "origin" , e.Origin , true);
WriteString(EFieldIndices.Region , "region" , e.Region , true);
WriteString(EFieldIndices.Tags , "tags" , e.Tags , true);
if (!DAT.SerialInRom)
WriteString(EFieldIndices.Serial , "serial" , e.Serial , true);
ln += " rom (";
WriteString(EFieldIndices.RomName , "name" , e.RomName , false);
WriteRaw (EFieldIndices.Size , "size" , e.Size .ToString(), false);
WriteRaw (EFieldIndices.MD5 , "md5" , e.MD5 , false);
WriteRaw (EFieldIndices.CRC , "crc" , e.CRC , false);
WriteRaw (EFieldIndices.SHA1 , "sha1" , e.SHA1 , false);
if (DAT.SerialInRom)
WriteString(EFieldIndices.Serial , "serial" , e.Serial , false);
ln += " )\n)\n";
w.Write(ln);
}
}
else
{
foreach (Entry e in this)