Welcome to mirror list, hosted at ThFree Co, Russian Federation.

UnifFile.cs - github.com/ClusterM/nes-containers.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: f2ad1e6d20a4c444d7712f5b091217b1ad4be0ed (plain)
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
using System;
using System.Collections;
using System.Collections.Generic;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Runtime.ConstrainedExecution;
using System.Security.Cryptography;
using System.Text;
using System.Xml.Linq;

namespace com.clusterrr.Famicom.Containers
{
    /// <summary>
    /// UNIF file container for NES/Famicom games
    /// </summary>
    public class UnifFile : IEnumerable<KeyValuePair<string, byte[]>>
    {
        private const string FIELD_CTRL = "CTRL";
        private const string FIELD_TVCI = "TVCI";
        private const string FIELD_DINF = "DINF";
        private const string FIELD_NAME = "NAME";
        private const string FIELD_BATR = "BATR";
        private const string FIELD_MAPR = "MAPR";
        private const string FIELD_MIRR = "MIRR";
        private const string FIELD_PRG0 = "PRG0";
        private const string FIELD_PRG1 = "PRG1";
        private const string FIELD_PRG2 = "PRG2";
        private const string FIELD_PRG3 = "PRG3";
        private const string FIELD_CHR0 = "CHR0";
        private const string FIELD_CHR1 = "CHR1";
        private const string FIELD_CHR2 = "CHR2";
        private const string FIELD_CHR3 = "CHR3";
        private const string PREFIX_PRG = "PRG";
        private const string PREFIX_CHR = "CHR";

        /// <summary>
        /// UNIF fields
        /// </summary>
        private Dictionary<string, byte[]> fields = new Dictionary<string, byte[]>();

        /// <summary>
        /// UNIF version
        /// </summary>
        public uint Version { get; set; } = 5;

        /// <summary>
        /// Get/set UNIF field
        /// </summary>
        /// <param name="key">UNIF data block key</param>
        /// <returns></returns>
        public byte[] this[string key]
        {
            get
            {
                if (key.Length != 4) throw new ArgumentException("UNIF data block key must be 4 characters long");
                if (!ContainsField(key)) throw new IndexOutOfRangeException($"There is no {key} field");
                return fields[key];
            }
            set
            {
                if (key.Length != 4) throw new ArgumentException("UNIF data block key must be 4 characters long");
                if (value == null)
                    this.RemoveField(key);
                else
                    fields[key] = value;
            }
        }

        /// <summary>
        /// Returns true if field exists in the UNIF
        /// </summary>
        /// <param name="fieldName">Field code</param>
        /// <returns>True if field exists in the UNIF</returns>
        public bool ContainsField(string fieldName) => fields.ContainsKey(fieldName);

        /// <summary>
        /// Remove field from the UNIF
        /// </summary>
        /// <param name="fieldName"></param>
        public void RemoveField(string fieldName) => fields.Remove(fieldName);

        /// <summary>
        /// Returns enumerator that iterates throught fields
        /// </summary>
        /// <returns>IEnumerable object</returns>
        public IEnumerator<KeyValuePair<string, byte[]>> GetEnumerator()
            => fields.Select(kv => new KeyValuePair<string, byte[]>(kv.Key, kv.Value)).GetEnumerator();

        IEnumerator IEnumerable.GetEnumerator() => this.GetEnumerator();

        /// <summary>
        /// Constructor to create empty UnifFile object
        /// </summary>
        public UnifFile()
        {
            DumpDate = DateTime.Now;
        }

        /// <summary>
        /// Create UnifFile object from raw .unf file contents
        /// </summary>
        /// <param name="data">Raw UNIF data</param>
        public UnifFile(byte[] data)
        {
            var header = new byte[32];
            Array.Copy(data, header, 32);
            if (header[0] != 'U' || header[1] != 'N' || header[2] != 'I' || header[3] != 'F')
                throw new InvalidDataException("Invalid UNIF header");
            Version = BitConverter.ToUInt32(header, 4);
            int pos = 32;
            while (pos < data.Length)
            {
                var type = Encoding.UTF8.GetString(data, pos, 4);
                pos += 4;
                int length = BitConverter.ToInt32(data, pos);
                pos += 4;
                var fieldData = new byte[length];
                Array.Copy(data, pos, fieldData, 0, length);
                this[type] = fieldData;
                pos += length;
            }
        }

        /// <summary>
        /// Create UnifFile object from specified file
        /// </summary>
        /// <param name="fileName">Path to the .unf file</param>
        public UnifFile(string fileName) : this(File.ReadAllBytes(fileName))
        {
        }

        /// <summary>
        /// Create UnifFile object from raw .unf file contents
        /// </summary>
        /// <param name="data"></param>
        /// <returns>UnifFile object</returns>
        public static UnifFile FromBytes(byte[] data) => new UnifFile(data);

        /// <summary>
        /// Create UnifFile object from specified file
        /// </summary>
        /// <param name="filename">Path to the .unf file</param>
        /// <returns>UnifFile object</returns>
        public static UnifFile FromFile(string filename) => new UnifFile(filename);

        /// <summary>
        /// Returns .unf file contents
        /// </summary>
        /// <returns></returns>
        public byte[] ToBytes()
        {
            // Some checks
            if (ContainsField(FIELD_CTRL) && Version < 7)
                throw new InvalidDataException("CTRL (controllers) field requires UNIF version 7 or greater");
            if (ContainsField(FIELD_TVCI) && Version < 6)
                throw new InvalidDataException("TVCI (controllers) field requires UNIF version 6 or greater");

            var data = new List<byte>();
            // Header
            data.AddRange(Encoding.UTF8.GetBytes("UNIF"));
            data.AddRange(BitConverter.GetBytes(Version));
            data.AddRange(Enumerable.Repeat<byte>(0, 24).ToArray());
            // Fields
            foreach (var kv in this)
            {
                data.AddRange(Encoding.UTF8.GetBytes(kv.Key));
                var value = kv.Value;
                int len = value.Length;
                data.AddRange(BitConverter.GetBytes(len));
                data.AddRange(value);
            }
            return data.ToArray();
        }

        /// <summary>
        /// Save as .unf file
        /// </summary>
        /// <param name="filename">Target filename</param>
        public void Save(string filename) => File.WriteAllBytes(filename, ToBytes());

        /// <summary>
        /// Convert string to null-terminated UTF string
        /// </summary>
        /// <param name="text">Input text</param>
        /// <returns>Output byte[] array</returns>
        private static byte[] StringToUTF8N(string text)
        {
            var str = Encoding.UTF8.GetBytes(text);
            var result = new byte[str.Length + 1];
            Array.Copy(str, result, str.Length);
            return result;
        }

        /// <summary>
        /// Convert null-terminated UTF string to string
        /// </summary>
        /// <param name="data">Input array of bytes</param>
        /// <param name="maxLength">Maximum number of bytes to parse</param>
        /// <param name="offset">Start offset</param>
        /// <returns></returns>
        private static string UTF8NToString(byte[] data, int maxLength = int.MaxValue, int offset = 0)
        {
            int length = 0;
            while ((data[length + offset] != 0) && (length + offset < data.Length) && (length < maxLength))
                length++;
            return Encoding.UTF8.GetString(data, offset, length);
        }

        /// <summary>
        /// Mapper name (null if none)
        /// </summary>
        public string? Mapper
        {
            get => ContainsField(FIELD_MAPR) ? UTF8NToString(this[FIELD_MAPR]) : null;
            set
            {
                if (value == null)
                    RemoveField(FIELD_MAPR);
                else
                    this[FIELD_MAPR] = StringToUTF8N(value);
            }
        }

        /// <summary>
        /// The dumper name (null if none)
        /// </summary>
        /// 
        public string? DumperName
        {
            get
            {
                if (!ContainsField(FIELD_DINF))
                    return null;
                var data = this[FIELD_DINF];
                if (data.Length >= 204 && data[0] != 0)
                    return UTF8NToString(data, 100);
                else
                    return null;
            }
            set
            {
                if (value != null || DumpingSoftware != null || DumpDate != null)
                {
                    if (!ContainsField(FIELD_DINF))
                        this[FIELD_DINF] = new byte[204];
                    var data = this[FIELD_DINF];
                    for (int i = 0; i < 100; i++)
                        data[i] = 0;
                    if (value != null)
                    {
                        var name = StringToUTF8N(value);
                        Array.Copy(name, 0, data, 0, Math.Min(100, name!.Length));
                    }
                    this[FIELD_DINF] = data;
                }
                else RemoveField(FIELD_DINF);
            }
        }

        /// <summary>
        /// The name of the dumping software or mechanism (null if none)
        /// </summary>
        public string? DumpingSoftware
        {
            get
            {
                if (!ContainsField(FIELD_DINF))
                    return null;
                var data = this[FIELD_DINF];
                if (data.Length >= 204 && data[0] != 0)
                    return UTF8NToString(data, 100, 104);
                else
                    return null;
            }
            set
            {
                if (value != null || DumperName != null || DumpDate != null)
                {
                    if (!ContainsField(FIELD_DINF))
                        this[FIELD_DINF] = new byte[204];
                    var data = this[FIELD_DINF];
                    for (int i = 104; i < 104 + 100; i++)
                        data[i] = 0;
                    if (value != null)
                    {
                        var name = StringToUTF8N(value);
                        Array.Copy(name, 0, data, 104, Math.Min(100, name!.Length));
                    }
                    this[FIELD_DINF] = data;
                }
                else RemoveField(FIELD_DINF);
            }
        }

        /// <summary>
        /// Date of the dump (null if none)
        /// </summary>
        public DateTime? DumpDate
        {
            get
            {
                if (!ContainsField(FIELD_DINF)) return null;
                var data = this[FIELD_DINF];
                if (data[0] == 0 && data[1] == 0 && data[2] == 0 && data[3] == 0)
                    return null;
                return new DateTime(
                        year: data[102] | (data[103] << 8),
                        month: data[101],
                        day: data[100]
                    );
            }
            set
            {
                if (value != null || DumperName != null || DumpingSoftware != null)
                {
                    if (!ContainsField(FIELD_DINF))
                        this[FIELD_DINF] = new byte[204];
                    var data = this[FIELD_DINF];
                    if (value != null)
                    {
                        data[100] = (byte)value.Value.Day;
                        data[101] = (byte)value.Value.Month;
                        data[102] = (byte)(value.Value.Year & 0xFF);
                        data[103] = (byte)(value.Value.Year >> 8);
                    }
                    else
                    {
                        // Is it valid?
                        data[100] = data[101] = data[102] = data[103] = 0;
                    }
                    this[FIELD_DINF] = data;
                }
                else RemoveField(FIELD_DINF);
            }
        }

        /// <summary>
        /// Name of the game (null if none)
        /// </summary>
        public string? GameName
        {
            get => ContainsField(FIELD_NAME) ? UTF8NToString(this[FIELD_NAME]) : null;
            set
            {
                if (value == null)
                    RemoveField(FIELD_NAME);
                else
                    this[FIELD_NAME] = StringToUTF8N(value!);
            }
        }

        /// <summary>
        /// For non-homebrew NES/Famicom games, this field's value is always a function of the region in which a game was released (null if none)
        /// </summary>
        public Timing? Region
        {
            get
            {
                if (ContainsField(FIELD_TVCI) && this[FIELD_TVCI].Any())
                    return (Timing)this[FIELD_TVCI].First();
                else
                    return null;
            }
            set
            {
                if (value != null)
                    this[FIELD_TVCI] = new byte[] { (byte)value };
                else
                    RemoveField(FIELD_TVCI);
            }
        }

        /// <summary>
        /// Controllers usable by this game, bitmask (null if none)
        /// </summary>
        public Controller? Controllers
        {
            get
            {
                if (ContainsField(FIELD_CTRL) && this[FIELD_CTRL].Any())
                    return (Controller)this[FIELD_CTRL].First();
                else
                    return Controller.None;
            }
            set
            {
                if (value != null)
                    fields[FIELD_CTRL] = new byte[] { (byte)value };
                else
                    RemoveField(FIELD_CTRL);
            }
        }

        /// <summary>
        /// Battery-backed (or other non-volatile memory) memory is present (null if none)
        /// </summary>
        public bool? Battery
        {
            get
            {
                if (ContainsField(FIELD_BATR) && this[FIELD_BATR].Any())
                    return this[FIELD_BATR].First() != 0;
                else
                    return false;
            }
            set
            {
                if (value != null)
                    fields[FIELD_BATR] = new byte[] { (byte)((bool)value ? 1 : 0) };
                else
                    RemoveField(FIELD_BATR);
            }
        }

        /// <summary>
        /// Mirroring type (null if none)
        /// </summary>
        public MirroringType? Mirroring
        {
            get
            {
                if (ContainsField(FIELD_MIRR) && this[FIELD_MIRR].Any())
                    return (MirroringType)this[FIELD_MIRR].First();
                else
                    return null;
            }
            set
            {
                if (value != null && value != MirroringType.Unknown)
                    this[FIELD_MIRR] = new byte[] { (byte)value };
                else
                    this.RemoveField(FIELD_MIRR);
            }
        }

        /// <summary>
        /// PRG0 field (null if none)
        /// </summary>
        public byte[]? PRG0
        {
            get
            {
                if (ContainsField(FIELD_PRG0))
                    return this[FIELD_PRG0];
                else
                    return null;
            }
            set
            {
                if (value != null)
                    this[FIELD_PRG0] = value;
                else
                    RemoveField(FIELD_PRG0);
            }
        }

        /// <summary>
        /// PRG1 field (null if none)
        /// </summary>
        public byte[]? PRG1
        {
            get
            {
                if (ContainsField(FIELD_PRG1))
                    return this[FIELD_PRG1];
                else
                    return null;
            }
            set
            {
                if (value != null)
                    this[FIELD_PRG1] = value;
                else
                    RemoveField(FIELD_PRG1);
            }
        }

        /// <summary>
        /// PRG2 field (null if none)
        /// </summary>
        public byte[]? PRG2
        {
            get
            {
                if (ContainsField(FIELD_PRG2))
                    return this[FIELD_PRG2];
                else
                    return null;
            }
            set
            {
                if (value != null)
                    this[FIELD_PRG2] = value;
                else
                    RemoveField(FIELD_PRG2);
            }
        }

        /// <summary>
        /// PRG3 field (null if none)
        /// </summary>
        public byte[]? PRG3
        {
            get
            {
                if (ContainsField(FIELD_PRG3))
                    return this[FIELD_PRG3];
                else
                    return null;
            }
            set
            {
                if (value != null)
                    this[FIELD_PRG3] = value;
                else
                    RemoveField(FIELD_PRG3);
            }
        }

        /// <summary>
        /// CHR0 field (null if none)
        /// </summary>
        public byte[]? CHR0
        {
            get
            {
                if (ContainsField(FIELD_CHR0))
                    return this[FIELD_CHR0];
                else
                    return null;
            }
            set
            {
                if (value != null)
                    this[FIELD_CHR0] = value;
                else
                    RemoveField(FIELD_CHR0);
            }
        }

        /// <summary>
        /// CHR1 field (null if none)
        /// </summary>
        public byte[]? CHR1
        {
            get
            {
                if (ContainsField(FIELD_CHR1))
                    return this[FIELD_CHR1];
                else
                    return null;
            }
            set
            {
                if (value != null)
                    this[FIELD_CHR1] = value;
                else
                    RemoveField(FIELD_CHR1);
            }
        }

        /// <summary>
        /// CHR2 field (null if none)
        /// </summary>
        public byte[]? CHR2
        {
            get
            {
                if (ContainsField(FIELD_CHR2))
                    return this[FIELD_CHR2];
                else
                    return null;
            }
            set
            {
                if (value != null)
                    this[FIELD_CHR2] = value;
                else
                    RemoveField(FIELD_CHR2);
            }
        }

        /// <summary>
        /// CHR3 field (null if none)
        /// </summary>
        public byte[]? CHR3
        {
            get
            {
                if (ContainsField(FIELD_CHR3))
                    return this[FIELD_CHR3];
                else
                    return null;
            }
            set
            {
                if (value != null)
                    this[FIELD_CHR3] = value;
                else
                    RemoveField(FIELD_CHR3);
            }
        }

        /// <summary>
        /// Calculate CRC32 for PRG and CHR fields and store it into PCKx and CCKx fields
        /// </summary>
        public void CalculateAndStoreCRCs()
        {
            foreach (var kv in this.Where(kv => kv.Key.StartsWith(PREFIX_PRG)))
            {
                var num = kv.Key[3];
                using var crc32 = new Crc32();
                var crc32sum = crc32.ComputeHash(kv.Value);
                this[$"PCK{num}"] = crc32sum;
            }
            foreach (var kv in this.Where(kv => kv.Key.StartsWith(PREFIX_CHR)))
            {
                var num = kv.Key[3];
                using var crc32 = new Crc32();
                var crc32sum = crc32.ComputeHash(kv.Value);
                this[$"CCK{num}"] = crc32sum;
            }
        }

        /// <summary>
        /// Calculate MD5 checksum of ROM (all PRG fields + all CHR fields)
        /// </summary>
        /// <returns>MD5 checksum for all PRG and CHR data</returns>
        public byte[] CalculateMD5()
        {
            using var md5 = MD5.Create();
            foreach(var kv in fields.Where(k => k.Key.StartsWith(PREFIX_PRG)).OrderBy(k => k.Key)
                .Concat(fields.Where(k => k.Key.StartsWith(PREFIX_CHR)).OrderBy(k => k.Key)))
            {
                var v = kv.Value;
                int sizeUpPow2 = 0;
                if (v.Length > 0)
                {
                    sizeUpPow2 = 1;
                    while (sizeUpPow2 < v.Length) sizeUpPow2 <<= 1;
                }
                md5.TransformBlock(v, 0, v.Length, null, 0);
                md5.TransformBlock(Enumerable.Repeat<byte>(byte.MaxValue, sizeUpPow2 - v.Length).ToArray(), 0, sizeUpPow2 - v.Length, null, 0);
            }
            md5.TransformFinalBlock(new byte[0], 0, 0);
            return md5.Hash;
        }

        /// <summary>
        /// Calculate CRC32 checksum of ROM (all PRG fields + all CHR fields)
        /// </summary>
        /// <returns>CRC32 checksum for all PRG and CHR data</returns>
        public uint CalculateCRC32()
        {
            using var crc32 = new Crc32();
            foreach (var kv in fields.Where(k => k.Key.StartsWith(PREFIX_PRG)).OrderBy(k => k.Key)
                .Concat(fields.Where(k => k.Key.StartsWith(PREFIX_CHR)).OrderBy(k => k.Key)))
            {
                var v = kv.Value;
                int sizeUpPow2 = 0;
                if (v.Length > 0)
                {
                    sizeUpPow2 = 1;
                    while (sizeUpPow2 < v.Length) sizeUpPow2 <<= 1;
                }
                crc32.TransformBlock(v, 0, v.Length, null, 0);
                crc32.TransformBlock(Enumerable.Repeat(byte.MaxValue, sizeUpPow2 - v.Length).ToArray(), 0, sizeUpPow2 - v.Length, null, 0);
            }
            crc32.TransformFinalBlock(new byte[0], 0, 0);
            return BitConverter.ToUInt32(crc32.Hash.Reverse().ToArray(), 0);
        }

        /// <summary>
        /// Default game controller(s)
        /// </summary>
        [Flags]
        public enum Controller
        {
            /// <summary>
            /// None
            /// </summary>
            None = 0,
            /// <summary>
            /// Standatd Controller
            /// </summary>
            StandardController = 1,
            /// <summary>
            /// Zapper
            /// </summary>
            Zapper = 2,
            /// <summary>
            /// R.O.B.
            /// </summary>
            ROB = 4,
            /// <summary>
            /// Arkanoid Controller
            /// </summary>
            ArkanoidController = 8,
            /// <summary>
            /// Power Pad
            /// </summary>
            PowerPad = 16,
            /// <summary>
            /// Four Score
            /// </summary>
            FourScore = 32,
        }
    }
}