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

FdsDiskSide.cs - github.com/ClusterM/nes-containers.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 8ea8e91acad1808aace7a6c902fcafe04ea85496 (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
using System;
using System.Collections.Generic;
using System.Linq;

namespace com.clusterrr.Famicom.Containers
{
    public class FdsDiskSide
    {
        FdsBlockDiskInfo diskInfoBlock;
        /// <summary>
        /// Disk info block
        /// </summary>
        public FdsBlockDiskInfo DiskInfoBlock { get => diskInfoBlock; }
        public string DiskVerification { get => diskInfoBlock.DiskVerification; }
        /// <summary>
        /// Manufacturer code. $00 = Unlicensed, $01 = Nintendo
        /// </summary>
        public FdsBlockDiskInfo.Manufacturer ManufacturerCode { get => diskInfoBlock.ManufacturerCode; set => diskInfoBlock.ManufacturerCode = value; }
        /// <summary>
        /// 3-letter ASCII code per game (e.g. ZEL for The Legend of Zelda)
        /// </summary>
        public string GameName { get => diskInfoBlock.GameName; set => diskInfoBlock.GameName = value; }
        /// <summary>
        /// $20 = " " — Normal disk
        /// $45 = "E" — Event(e.g.Japanese national DiskFax tournaments)
        /// $52 = "R" — Reduction in price via advertising
        /// </summary>
        public char GameType { get => diskInfoBlock.GameType; set => diskInfoBlock.GameType = value; }
        /// <summary>
        /// Game version/revision number. Starts at $00, increments per revision
        /// </summary>
        public byte GameVersion { get => diskInfoBlock.GameVersion; set => diskInfoBlock.GameVersion = value; }
        /// <summary>
        /// Side number. Single-sided disks use A
        /// </summary>
        public FdsBlockDiskInfo.DiskSides DiskSide { get => diskInfoBlock.DiskSide; set => diskInfoBlock.DiskSide = value; }
        /// <summary>
        /// Disk number. First disk is $00, second is $01, etc.
        /// </summary>
        public byte DiskNumber { get => diskInfoBlock.DiskNumber; set => diskInfoBlock.DiskNumber = value; }
        /// <summary>
        /// Disk type. $00 = FMC ("normal card"), $01 = FSC ("card with shutter"). May correlate with FMC and FSC product codes
        /// </summary>
        public FdsBlockDiskInfo.DiskTypes DiskType { get => diskInfoBlock.DiskType; set => diskInfoBlock.DiskType = value; }
        /// <summary>
        /// Boot read file code. Refers to the file code/file number to load upon boot/start-up
        /// </summary>
        public byte BootFile { get => diskInfoBlock.BootFile; set => diskInfoBlock.BootFile = value; }
        /// <summary>
        /// Manufacturing date
        /// </summary>
        public DateTime ManufacturingDate { get => diskInfoBlock.ManufacturingDate; set => diskInfoBlock.ManufacturingDate = value; }
        /// <summary>
        /// Country code. $49 = Japan
        /// </summary>
        public FdsBlockDiskInfo.Country CountryCode { get => diskInfoBlock.CountryCode; set => diskInfoBlock.CountryCode = value; }
        /// <summary>
        /// "Rewritten disk" date. It's speculated this refers to the date the disk was formatted and rewritten by something like a Disk Writer kiosk.
        /// In the case of an original (non-copied) disk, this should be the same as Manufacturing date
        /// </summary>
        public DateTime RewrittenDate { get => diskInfoBlock.RewrittenDate; set => diskInfoBlock.RewrittenDate = value; }
        /// <summary>
        /// Disk Writer serial number
        /// </summary>
        public ushort DiskWriterSerialNumber { get => diskInfoBlock.DiskWriterSerialNumber; set => diskInfoBlock.DiskWriterSerialNumber = value; }
        /// <summary>
        /// Disk rewrite count. $00 = Original (no copies)
        /// </summary>
        public byte DiskRewriteCount { get => diskInfoBlock.DiskRewriteCount; set => diskInfoBlock.DiskRewriteCount = value; }
        /// <summary>
        /// Actual disk side
        /// </summary>
        public FdsBlockDiskInfo.DiskSides ActualDiskSide { get => diskInfoBlock.ActualDiskSide; set => diskInfoBlock.ActualDiskSide = value; }
        /// <summary>
        /// Price code
        /// </summary>
        public byte Price { get => diskInfoBlock.Price; set => diskInfoBlock.Price = value; }

        FdsBlockFileAmount fileAmountBlock;
        /// <summary>
        /// Non-hidden file amount
        /// </summary>
        public byte FileAmount { get => fileAmountBlock.FileAmount; set => fileAmountBlock.FileAmount = value; }

        IList<FdsDiskFile> files;

        /// <summary>
        /// Files on disk
        /// </summary>
        public IList<FdsDiskFile> Files { get => files; }

        public FdsDiskSide()
        {
            diskInfoBlock = new FdsBlockDiskInfo();
            fileAmountBlock = new FdsBlockFileAmount();
            files = new List<FdsDiskFile>();
        }

        public FdsDiskSide(FdsBlockDiskInfo diskInfoBlock, FdsBlockFileAmount fileAmountBlock, IEnumerable<FdsDiskFile> files)
        {
            this.diskInfoBlock = diskInfoBlock;
            this.fileAmountBlock = fileAmountBlock;
            this.files = files.ToList();
            //if (this.fileAmountBlock.FileAmount > this.files.Count)
            //    throw new ArgumentOutOfRangeException("visibleFileAmount", "visibleFileAmount must be less or equal number of files");
        }

        public FdsDiskSide(IEnumerable<IFdsBlock> blocks)
        {
            this.diskInfoBlock = (FdsBlockDiskInfo)blocks.First();
            this.fileAmountBlock = (FdsBlockFileAmount)blocks.Skip(1).First();
            files = new List<FdsDiskFile>();
            var fileBlocks = blocks.Skip(2).ToArray();
            for (int i = 0; i < fileBlocks.Length / 2; i++)
            {
                files.Add(new FdsDiskFile((FdsBlockFileHeader)fileBlocks[i * 2], (FdsBlockFileData)fileBlocks[i * 2 + 1]));
            }
        }

        public FdsDiskSide(byte[] data) : this()
        {
            int pos = 0;
            this.diskInfoBlock = FdsBlockDiskInfo.FromBytes(data.Take(56).ToArray());
            pos += 56;
            this.fileAmountBlock = FdsBlockFileAmount.FromBytes(data.Skip(pos).Take(2).ToArray());
            pos += 2;
            while (pos < data.Length)
            {
                try
                {
                    var fileHeaderBlock = FdsBlockFileHeader.FromBytes(data.Skip(pos).Take(16).ToArray());
                    if (!fileHeaderBlock.IsValid)
                        break;
                    pos += 16;
                    var fileDataBlock = FdsBlockFileData.FromBytes(data.Skip(pos).Take(fileHeaderBlock.FileSize + 1).ToArray());
                    if (!fileDataBlock.IsValid)
                        break;
                    pos += fileHeaderBlock.FileSize + 1;
                    files.Add(new FdsDiskFile(fileHeaderBlock, fileDataBlock));
                }
                catch
                {
                    // just break on out of range
                    break;
                }
            }
        }

        public void FixFileNumbers()
        {
            for (var i = 0; i < files.Count; i++)
                files[i].FileNumber = (byte)i;
        }

        public IEnumerable<IFdsBlock> GetBlocks()
        {
            var blocks = new List<IFdsBlock>();
            blocks.Add(diskInfoBlock);
            blocks.Add(fileAmountBlock);
            blocks.AddRange(files.SelectMany(f => new IFdsBlock[] { f.HeaderBlock, f.DataBlock }));
            return blocks;
        }

        public static FdsDiskSide FromBytes(byte[] data)
        {
            return new FdsDiskSide(data);
        }

        public byte[] ToBytes()
        {
            var data = Enumerable.Concat(Enumerable.Concat(diskInfoBlock.ToBytes(), fileAmountBlock.ToBytes()), files.SelectMany(f => f.ToBytes())).ToArray();
            return Enumerable.Concat(data, new byte[65500 - data.Count()]).ToArray();
        }

        public override string ToString() => $"{GameName} - disk {DiskNumber + 1}, side {DiskSide}";
    }
}