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

SnesGame.cs « Apps - github.com/ClusterM/hakchi2.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 6b1a13839960ece838d25ea05292c103c1112422 (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
#pragma warning disable 0108
using com.clusterrr.hakchi_gui.Properties;
using System;
using System.Collections.Generic;
using System.Drawing;
using System.IO;
using System.Runtime.InteropServices;
using System.Text;
using System.Windows.Forms;

namespace com.clusterrr.hakchi_gui
{
    public class SnesGame : NesMiniApplication
    {
        const string DefaultArgs = "--volume 100 -rollback-snapshot-period 600";
        static List<byte> SfxTypes = new List<byte>() { 0x13, 0x14, 0x15, 0x1a };
        static List<byte> Dsp1Types = new List<byte>() { 0x03, 0x05 };
        static List<byte> SA1Types = new List<byte>() { 0x34, 0x35 };
        static Dictionary<string, ushort> knownPresets = new Dictionary<string, ushort>()
        {
            { "SUPER MARIOWORLD", 0x1011 },
            { "F-ZERO", 0x1018 },
            { "THE LEGEND OF ZELDA", 0x101D },
            { "SUPER MARIO KART", 0x10BD },
            { "Super Metroid", 0x1040 },
            { "EARTH BOUND", 0x1070 },
            { "Kirby's Dream Course", 0x1058 },
            { "DONKEY KONG COUNTRY", 0x1077 },
            { "KIRBY SUPER DELUXE", 0x109F },
            { "Super Punch-Out!!", 0x10A9 },
            { "MEGAMAN X", 0x1109 },
            { "SUPER GHOULS'N GHOST", 0x1003 },
            { "Street Fighter2 Turb", 0x1065 },
            { "SUPER MARIO RPG", 0x109E },
            { "Secret of MANA", 0x10B0 },
            { "FINAL FANTASY 3", 0x10DC },
            { "SUPER CASTLEVANIA 4", 0x1030 },
            { "CONTRA3 THE ALIEN WA", 0x1036 },
            { "STAR FOX", 0x1242 },
            { "YOSHI'S ISLAND", 0x123D },
            { "STARFOX2", 0x123C },
            { "ZELDANODENSETSU", 0x101F },
            { "SHVC FIREEMBLEM", 0x102B },
            { "SUPER DONKEY KONG", 0x1023 },
            { "Super Street Fighter", 0x1056 },
            { "ROCKMAN X", 0x110A },
            { "CHOHMAKAIMURA", 0x1004 },
            { "SeikenDensetsu 2", 0x10B2 },
            { "FINAL FANTASY 6", 0x10DD },
            { "CONTRA SPIRITS", 0x1037 },
            { "ganbare goemon", 0x1048 },
            { "SUPER FORMATION SOCC", 0x1240 },
            { "YOSSY'S ISLAND", 0x1243 },
            { "FINAL FIGHT", 0x100E },
            { "DIDDY'S KONG QUEST", 0x105D },
            { "KIRBY'S DREAM LAND 3", 0x10A2 },
            { "BREATH OF FIRE 2", 0x1068 },
            { "FINAL FIGHT 2", 0x10E1 },
            { "MEGAMAN X2", 0x1117 },
            { "FINAL FIGHT 3", 0x10E3 },
            { "GENGHIS KHAN 2", 0x10C4 },
            { "CASTLEVANIA DRACULA", 0x1131 },
            { "STREET FIGHTER ALPHA", 0x10DF },
            { "MEGAMAN 7", 0x113A },
            { "MEGAMAN X3", 0x113D },
            { "Breath of Fire", 0x1144 },
        };

        public override string GoogleSuffix
        {
            get
            {
                return "(snes | super nintendo)";
            }
        }

        public SnesGame(string path, bool ignoreEmptyConfig = false)
            : base(path, ignoreEmptyConfig)
        {
        }

        public static bool Patch(string inputFileName, ref byte[] rawRomData, ref char prefix, ref string application, ref string outputFileName, ref string args, ref Image cover, ref uint crc32)
        {
            FindPatch(ref rawRomData, inputFileName, crc32);
            if (inputFileName.Contains("(E)") || inputFileName.Contains("(J)"))
                cover = Resources.blank_snes_eu_jp;
            if (ConfigIni.ConsoleType == MainForm.ConsoleType.SNES || ConfigIni.ConsoleType == MainForm.ConsoleType.SuperFamicom)
            {
                application = "/bin/clover-canoe-shvc-wr -rom";
                args = DefaultArgs;
                var ext = Path.GetExtension(inputFileName);
                if (ext.ToLower() != ".sfrom") // Need to patch for canoe
                {
                    if ((ext.ToLower() == ".smc") && ((rawRomData.Length % 1024) != 0))
                    {
                        var stripped = new byte[rawRomData.Length - 512];
                        Array.Copy(rawRomData, 512, stripped, 0, stripped.Length);
                        rawRomData = stripped;
                    }
                    MakeSfrom(ref rawRomData);
                    outputFileName = Path.GetFileNameWithoutExtension(outputFileName) + ".sfrom";
                }
            }
            else
            {
                application = "/bin/snes";
            }

            return true;
        }

        private static void MakeSfrom(ref byte[] rawRomData)
        {
            var romHeaderLoRom = SnesRomHeader.Read(rawRomData, 0x7FC0);
            var romHeaderHiRom = SnesRomHeader.Read(rawRomData, 0xFFC0);
            SnesRomHeader romHeader;
            bool loRom = true;
            bool hiRom = true;
            if (romHeaderLoRom.GameTitle.Length == 0)
                loRom = false;
            foreach (char c in romHeaderLoRom.GameTitle)
                if (c < 31 || c > 127) loRom = false;
            if (romHeaderHiRom.GameTitle.Length == 0)
                hiRom = false;
            foreach (char c in romHeaderHiRom.GameTitle)
                if (c < 31 || c > 127) hiRom = false;
            SnesRomType romType;
            if (loRom && !hiRom)
            {
                romType = SnesRomType.LoRom;
                romHeader = romHeaderLoRom;
            }
            else if (!loRom && hiRom)
            {
                romType = SnesRomType.HiRom;
                romHeader = romHeaderHiRom;
            }
            else if ((romHeaderLoRom.RomMakeup & 1) == 0)
            {
                romType = SnesRomType.LoRom;
                romHeader = romHeaderLoRom;
            }
            else
            {
                romType = SnesRomType.HiRom;
                romHeader = romHeaderHiRom;
            }

            string gameTitle = romHeader.GameTitle.Trim();
            ushort presetId = 0; // 0x1011;
            ushort chip = 0;
            if (SfxTypes.Contains(romHeader.RomType)) // Super FX chip
                chip = 0x0C;
            if (!knownPresets.TryGetValue(gameTitle, out presetId)) // Known codes
            {
                if (Dsp1Types.Contains(romHeader.RomType))
                    presetId = 0x10BD; // ID from Mario Kard, DSP1
                if (SA1Types.Contains(romHeader.RomType))
                    presetId = 0x109C; // ID from Super Mario RPG, SA1
            }

            var sfromHeader1 = new SfromHeader1((uint)rawRomData.Length);
            var sfromHeader2 = new SfromHeader2((uint)rawRomData.Length, presetId, romType, chip);
            var sfromHeader1Raw = sfromHeader1.GetBytes();
            var sfromHeader2Raw = sfromHeader2.GetBytes();
            var result = new byte[sfromHeader1Raw.Length + rawRomData.Length + sfromHeader2Raw.Length];
            Array.Copy(sfromHeader1Raw, 0, result, 0, sfromHeader1Raw.Length);
            Array.Copy(rawRomData, 0, result, sfromHeader1Raw.Length, rawRomData.Length);
            Array.Copy(sfromHeader2Raw, 0, result, sfromHeader1Raw.Length + rawRomData.Length, sfromHeader2Raw.Length);
            rawRomData = result;
        }

        [StructLayout(LayoutKind.Sequential)]
        private struct SnesRomHeader
        {
            [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 21)]
            public string GameTitle;
            [MarshalAs(UnmanagedType.U1)] // $xFD5
            public byte RomMakeup;
            [MarshalAs(UnmanagedType.U1)] // $xFD6
            public byte RomType;
            [MarshalAs(UnmanagedType.U1)] // $xFD7
            public byte RomSize;
            [MarshalAs(UnmanagedType.U1)] // $xFD8
            public byte SramSize;
            [MarshalAs(UnmanagedType.U2)] // $xFD9
            public ushort LicenseId;
            [MarshalAs(UnmanagedType.U1)] // $xFDB
            public byte Version;
            [MarshalAs(UnmanagedType.U2)] // $xFDC
            public ushort ChecksumComplement;
            [MarshalAs(UnmanagedType.U2)] // $xFDE
            public ushort Checksum;

            public byte[] GetBytes()
            {
                int size = Marshal.SizeOf(this);
                byte[] arr = new byte[size];

                IntPtr ptr = Marshal.AllocHGlobal(size);
                Marshal.StructureToPtr(this, ptr, true);
                Marshal.Copy(ptr, arr, 0, size);
                Marshal.FreeHGlobal(ptr);
                return arr;
            }

            public static SnesRomHeader Read(byte[] buffer, int pos)
            {
                var size = Marshal.SizeOf(typeof(SnesRomHeader));
                IntPtr ptr = Marshal.AllocHGlobal(size);
                Marshal.Copy(buffer, pos, ptr, size);
                var r = (SnesRomHeader)Marshal.PtrToStructure(ptr, typeof(SnesRomHeader));
                Marshal.FreeHGlobal(ptr);
                return r;
            }
        }


        [StructLayout(LayoutKind.Sequential)]
        private struct SfromHeader1
        {
            [MarshalAs(UnmanagedType.U4)]
            public uint Uknown1_0x00000100;
            [MarshalAs(UnmanagedType.U4)]
            public uint FileSize;
            [MarshalAs(UnmanagedType.U4)]
            public uint Uknown2_0x00000030;
            [MarshalAs(UnmanagedType.U4)]
            public uint RomEnd;
            [MarshalAs(UnmanagedType.U4)]
            public uint FooterStart;
            [MarshalAs(UnmanagedType.U4)]
            public uint Header2;
            [MarshalAs(UnmanagedType.U4)]
            public uint FileSize2;
            [MarshalAs(UnmanagedType.U4)]
            public uint Uknown3_0x00000000;
            [MarshalAs(UnmanagedType.U4)]
            public uint Flags;
            [MarshalAs(UnmanagedType.ByValArray, SizeConst = 8)]
            public byte[] VCGameID;
            [MarshalAs(UnmanagedType.U4)]
            public uint Uknown4_0x00000000;

            public SfromHeader1(uint romSize)
            {
                Uknown1_0x00000100 = 0x00000100;
                FileSize = (uint)(48 + romSize + 38);
                Uknown2_0x00000030 = 0x00000030;
                RomEnd = (uint)(48 + romSize);
                FooterStart = FileSize;
                Header2 = RomEnd;
                FileSize2 = FileSize;
                Uknown3_0x00000000 = 0;
                Flags = FileSize - 11;
                VCGameID = new byte[8];
                var VCGameID_s = Encoding.ASCII.GetBytes("WUP-XXXX");
                Array.Copy(VCGameID_s, VCGameID, VCGameID_s.Length);
                Uknown4_0x00000000 = 0;
            }

            public byte[] GetBytes()
            {
                int size = Marshal.SizeOf(this);
                byte[] arr = new byte[size];

                IntPtr ptr = Marshal.AllocHGlobal(size);
                Marshal.StructureToPtr(this, ptr, true);
                Marshal.Copy(ptr, arr, 0, size);
                Marshal.FreeHGlobal(ptr);
                return arr;
            }

            public static SfromHeader1 Read(byte[] buffer, int pos)
            {
                var size = Marshal.SizeOf(typeof(SfromHeader1));
                IntPtr ptr = Marshal.AllocHGlobal(size);
                Marshal.Copy(buffer, pos, ptr, size);
                var r = (SfromHeader1)Marshal.PtrToStructure(ptr, typeof(SfromHeader1));
                Marshal.FreeHGlobal(ptr);
                return r;
            }
        }

        [StructLayout(LayoutKind.Sequential, Pack = 1)]
        private struct SfromHeader2
        {
            [MarshalAs(UnmanagedType.U1)] // 0x00
            public byte FPS;
            [MarshalAs(UnmanagedType.U4)] // 0x01
            public uint RomSize;
            [MarshalAs(UnmanagedType.U4)] // 0x05
            public uint PcmSize;
            [MarshalAs(UnmanagedType.U4)] // 0x09
            public uint FooterSize;
            [MarshalAs(UnmanagedType.U2)] // 0x0D
            public ushort PresetID;
            [MarshalAs(UnmanagedType.U1)] // 0x0F
            public byte Mostly2;
            [MarshalAs(UnmanagedType.U1)] // 0x10
            public byte Volume;
            [MarshalAs(UnmanagedType.U1)] // 0x11
            public byte RomType;
            [MarshalAs(UnmanagedType.U4)] // 0x12
            public uint Chip;
            [MarshalAs(UnmanagedType.U4)] // 0x16
            public uint Unknown1_0x00000000;
            [MarshalAs(UnmanagedType.U4)] // 0x1A
            public uint Unknown2_0x00000100;
            [MarshalAs(UnmanagedType.U4)] // 0x1E
            public uint Unknown3_0x00000100;
            [MarshalAs(UnmanagedType.U4)]
            public uint Unknown4_0x00000000;

            public SfromHeader2(uint romSize, ushort presetId, SnesRomType romType, uint chip)
            {
                FPS = 60;
                RomSize = romSize;
                PcmSize = 0;
                FooterSize = 0;
                PresetID = presetId;
                Mostly2 = 2;
                Volume = 100;
                RomType = (byte)romType;
                Chip = chip;
                Unknown1_0x00000000 = 0x00000000;
                Unknown2_0x00000100 = 0x00000100;
                Unknown3_0x00000100 = 0x00000100;
                Unknown4_0x00000000 = 0x00000000;
            }

            public byte[] GetBytes()
            {
                int size = Marshal.SizeOf(this);
                byte[] arr = new byte[size];

                IntPtr ptr = Marshal.AllocHGlobal(size);
                Marshal.StructureToPtr(this, ptr, true);
                Marshal.Copy(ptr, arr, 0, size);
                Marshal.FreeHGlobal(ptr);
                return arr;
            }

            public static SfromHeader2 Read(byte[] buffer, int pos)
            {
                var size = Marshal.SizeOf(typeof(SfromHeader2));
                IntPtr ptr = Marshal.AllocHGlobal(size);
                Marshal.Copy(buffer, pos, ptr, size);
                var r = (SfromHeader2)Marshal.PtrToStructure(ptr, typeof(SfromHeader2));
                Marshal.FreeHGlobal(ptr);
                return r;
            }
        }

        private enum SnesRomType { LoRom = 0x14, HiRom = 0x15 };
    }
}