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

NesGame.cs « Apps - github.com/ClusterM/hakchi2.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 3f1a1877a1fa4a71204f4489f88660600c71af6d (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
#pragma warning disable 0108
using com.clusterrr.Famicom;
using com.clusterrr.hakchi_gui.Properties;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text.RegularExpressions;
using System.Windows.Forms;
using System.Xml.XPath;

namespace com.clusterrr.hakchi_gui
{
    public class NesGame : NesMiniApplication, ICloverAutofill, ISupportsGameGenie
    {
        public const char Prefix = 'H';
        public static bool? IgnoreMapper;
        const string DefaultArgs = "--guest-overscan-dimensions 0,0,9,3 --initial-fadein-durations 10,2 --volume 75 --enable-armet";
        private static Dictionary<uint, CachedGameInfo> gameInfoCache = null;

        private static byte[] supportedMappers = new byte[] { 0, 1, 2, 3, 4, 5, 7, 9, 10, 86, 87, 184 };

        public override string GoogleSuffix
        {
            get
            {
                return "(nes | famicom)";
            }
        }

        public NesGame(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 byte saveCount, ref uint crc32)
        {
            // Try to patch before mapper check, maybe it will patch mapper
            var patched = FindPatch(ref rawRomData, inputFileName, crc32);
            NesFile nesFile;
            try
            {
                nesFile = new NesFile(rawRomData);
            }
            catch
            {
                application = "/bin/nes";
                return true;
            }
            crc32 = nesFile.CRC32;
            // Also search for patch using internal CRC32
            if (!patched)
            {
                if (FindPatch(ref rawRomData, inputFileName, crc32))
                    nesFile = new NesFile(rawRomData);
            }
            nesFile.CorrectRom();

            if (ConfigIni.ConsoleType == MainForm.ConsoleType.NES || ConfigIni.ConsoleType == MainForm.ConsoleType.Famicom)
            {
                application = "/bin/clover-kachikachi-wr";
                args = DefaultArgs;
            }
            else
            {
                application = "/bin/nes";
            }

            //if (nesFile.Mapper == 71) nesFile.Mapper = 2; // games by Codemasters/Camerica - this is UNROM clone. One exception - Fire Hawk
            //if (nesFile.Mapper == 88) nesFile.Mapper = 4; // Compatible with MMC3... sometimes
            //if (nesFile.Mapper == 95) nesFile.Mapper = 4; // Compatible with MMC3
            //if (nesFile.Mapper == 206) nesFile.Mapper = 4; // Compatible with MMC3
            if (!supportedMappers.Contains(nesFile.Mapper) && 
                (ConfigIni.ConsoleType == MainForm.ConsoleType.NES || ConfigIni.ConsoleType == MainForm.ConsoleType.Famicom)
                && (IgnoreMapper != true))
            {
                if (IgnoreMapper != false)
                {
                    var r = WorkerForm.MessageBoxFromThread(ParentForm,
                        string.Format(Resources.MapperNotSupported, System.IO.Path.GetFileName(inputFileName), nesFile.Mapper),
                            Resources.AreYouSure,
                            MessageBoxButtons.AbortRetryIgnore,
                            MessageBoxIcon.Warning, MessageBoxDefaultButton.Button2, true);
                    if (r == DialogResult.Abort)
                        IgnoreMapper = true;
                    if (r == DialogResult.Ignore)
                        return false;
                }
                else return false;
            }
            if ((nesFile.Mirroring == NesFile.MirroringType.FourScreenVram) &&
                (ConfigIni.ConsoleType == MainForm.ConsoleType.NES || ConfigIni.ConsoleType == MainForm.ConsoleType.Famicom) &&
                (IgnoreMapper != true))
            {
                var r = WorkerForm.MessageBoxFromThread(ParentForm,
                    string.Format(Resources.FourScreenNotSupported, System.IO.Path.GetFileName(inputFileName)),
                        Resources.AreYouSure,
                        MessageBoxButtons.AbortRetryIgnore,
                        MessageBoxIcon.Warning, MessageBoxDefaultButton.Button2, true);
                if (r == DialogResult.Abort)
                    IgnoreMapper = true;
                if (r == DialogResult.No)
                    return false;
            }

            // TODO: Make trainer check. I think that the NES Mini doesn't support it.
            rawRomData = nesFile.GetRaw();
            if (inputFileName.Contains("(J)")) cover = Resources.blank_jp;

            if (nesFile.Battery)
                saveCount = 3;

            return true;
        }

        public bool TryAutofill(uint crc32)
        {
            CachedGameInfo gameinfo;
            if (gameInfoCache != null && gameInfoCache.TryGetValue(crc32, out gameinfo))
            {
                Name = gameinfo.Name;
                Name = Name.Replace("_", " ").Replace("  ", " ").Trim();
                Players = gameinfo.Players;
                if (Players > 1) Simultaneous = true; // actually unknown...
                ReleaseDate = gameinfo.ReleaseDate;
                if (ReleaseDate.Length == 4) ReleaseDate += "-01";
                if (ReleaseDate.Length == 7) ReleaseDate += "-01";
                Publisher = gameinfo.Publisher.ToUpper();
                return true;
            }
            return false;
        }

        private struct CachedGameInfo
        {
            public string Name;
            public byte Players;
            public string ReleaseDate;
            public string Publisher;
            public string Region;
        }

        public static void LoadCache()
        {
            try
            {
                var xmlDataBasePath = Path.Combine(System.IO.Path.Combine(Program.BaseDirectoryInternal, "data"), "nescarts.xml");
                Debug.WriteLine("Loading " + xmlDataBasePath);

                if (File.Exists(xmlDataBasePath))
                {
                    var xpath = new XPathDocument(xmlDataBasePath);
                    var navigator = xpath.CreateNavigator();
                    var iterator = navigator.Select("/database/game");
                    gameInfoCache = new Dictionary<uint, CachedGameInfo>();
                    while (iterator.MoveNext())
                    {
                        XPathNavigator game = iterator.Current;
                        var cartridges = game.Select("cartridge");
                        while (cartridges.MoveNext())
                        {
                            var cartridge = cartridges.Current;
                            try
                            {
                                var crc = Convert.ToUInt32(cartridge.GetAttribute("crc", ""), 16);
                                gameInfoCache[crc] = new CachedGameInfo
                                {
                                    Name = game.GetAttribute("name", ""),
                                    Players = (byte)((game.GetAttribute("players", "") != "1") ? 2 : 1),
                                    ReleaseDate = game.GetAttribute("date", ""),
                                    Publisher = game.GetAttribute("publisher", ""),
                                    Region = game.GetAttribute("region", "")
                                };
                            }
                            catch { }
                        };
                    }
                }
                Debug.WriteLine(string.Format("NES XML loading done, {0} roms total", gameInfoCache.Count));
            }
            catch (Exception ex)
            {
                Debug.WriteLine(ex.Message + ex.StackTrace);
            }
        }

        public void ApplyGameGenie()
        {
            if (!string.IsNullOrEmpty(GameGenie))
            {
                var codes = GameGenie.Split(new char[] { ',', '\t', ' ', ';' }, StringSplitOptions.RemoveEmptyEntries);
                var nesFiles = Directory.GetFiles(this.GamePath, "*.nes", SearchOption.TopDirectoryOnly);
                foreach (var f in nesFiles)
                {
                    var nesFile = new NesFile(f);
                    foreach (var code in codes)
                    {
                        nesFile.PRG = GameGeniePatcherNes.Patch(nesFile.PRG, code.Trim());
                    }
                    nesFile.Save(f);
                }
            }
        }
    }
}