Beispiel #1
0
        public static NesMiniApplication ImportNes(string nesFileName, bool?ignoreMapper, ref bool?needPatch, NeedPatchDelegate needPatchCallback = null, Form parentForm = null, byte[] rawRomData = null)
        {
            NesFile nesFile;

            try
            {
                if (rawRomData != null)
                {
                    nesFile = new NesFile(rawRomData);
                }
                else
                {
                    nesFile = new NesFile(nesFileName);
                }
            }
            catch
            {
                return(NesMiniApplication.Import(nesFileName, rawRomData));
            }
            nesFile.CorrectRom();
            var crc32            = nesFile.CRC32;
            var code             = GenerateCode(crc32, Prefix);
            var gamePath         = Path.Combine(GamesDirectory, code);
            var nesPath          = Path.Combine(gamePath, code + ".nes");
            var patchesDirectory = Path.Combine(Path.GetDirectoryName(Application.ExecutablePath), "patches");

            Directory.CreateDirectory(patchesDirectory);
            Directory.CreateDirectory(gamePath);
            var patches = Directory.GetFiles(patchesDirectory, string.Format("{0:X8}*.ips", crc32), SearchOption.AllDirectories);

            if (patches.Length > 0 && needPatch != false)
            {
                if (needPatch == true || ((needPatchCallback != null) && needPatchCallback(parentForm, Path.GetFileName(nesFileName))))
                {
                    needPatch = true;
                    var patch = patches[0];
                    if (rawRomData == null)
                    {
                        rawRomData = File.ReadAllBytes(nesFileName);
                    }
                    Debug.WriteLine(string.Format("Patching {0}", nesFileName));
                    IpsPatcher.Patch(patch, ref rawRomData);
                    nesFile = new NesFile(rawRomData);
                }
                else
                {
                    needPatch = false;
                }
            }

            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) && (ignoreMapper != true))
            {
                Directory.Delete(gamePath, true);
                if (ignoreMapper != false)
                {
                    throw new UnsupportedMapperException(nesFile);
                }
                else
                {
                    Debug.WriteLine(string.Format("Game {0} has mapper #{1}, skipped", nesFileName, nesFile.Mapper));
                    return(null);
                }
            }
            if ((nesFile.Mirroring == NesFile.MirroringType.FourScreenVram) && (ignoreMapper != true))
            {
                Directory.Delete(gamePath, true);
                if (ignoreMapper != false)
                {
                    throw new UnsupportedFourScreenException(nesFile);
                }
                else
                {
                    Debug.WriteLine(string.Format("Game {0} has four-screen mirroring, skipped", nesFileName, nesFile.Mapper));
                    return(null);
                }
            }
            // TODO: Make trainer check. I think that the NES Mini doesn't support it.

            nesFile.Save(nesPath);
            var game = new NesGame(gamePath, true);

            game.Name = Path.GetFileNameWithoutExtension(nesFileName);
            if (game.Name.Contains("(J)"))
            {
                game.region = "Japan";
            }
            game.TryAutofill(crc32);
            game.Name = Regex.Replace(game.Name, @" ?\(.*?\)", string.Empty).Trim();
            game.Name = Regex.Replace(game.Name, @" ?\[.*?\]", string.Empty).Trim();
            game.Name = game.Name.Replace("_", " ").Replace("  ", " ");
            game.FindCover(nesFileName, (game.region == "Japan") ? Resources.blank_jp : Resources.blank_nes, crc32);
            game.Args = DefaultArgs;
            game.Save();
            return(game);
        }
Beispiel #2
0
        public static NesGame Import(string nesFileName, bool?ignoreMapper, ref bool?needPatch, NeedPatchDelegate needPatchCallback, Form parentForm = null, byte[] rawRomData = null)
        {
            NesFile nesFile;

            if (rawRomData != null)
            {
                nesFile = new NesFile(rawRomData);
            }
            else
            {
                nesFile = new NesFile(nesFileName);
            }
            nesFile.CorrectRom();
            var crc32            = nesFile.CRC32;
            var code             = GenerateCode(crc32, prefixCode);
            var gamePath         = Path.Combine(GamesDirectory, code);
            var nesPath          = Path.Combine(gamePath, code + ".nes");
            var patchesDirectory = Path.Combine(Path.GetDirectoryName(Application.ExecutablePath), "patches");

            Directory.CreateDirectory(patchesDirectory);
            Directory.CreateDirectory(gamePath);
            var patches = Directory.GetFiles(patchesDirectory, string.Format("{0:X8}*.ips", crc32), SearchOption.AllDirectories);

            if (patches.Length > 0 && needPatch != false)
            {
                if (needPatch == true || ((needPatchCallback != null) && needPatchCallback(parentForm, Path.GetFileName(nesFileName)))) /*MessageBox.Show(parentForm, string.Format(Resources.PatchQ, Path.GetFileName(nesFileName)), Resources.PatchAvailable, MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes*/
                {
                    needPatch = true;
                    var patch = patches[0];
                    if (rawRomData == null)
                    {
                        rawRomData = File.ReadAllBytes(nesFileName);
                    }
                    Debug.WriteLine(string.Format("Patching {0}", nesFileName));
                    IpsPatcher.Patch(patch, ref rawRomData);
                    nesFile = new NesFile(rawRomData);
                }
                else
                {
                    needPatch = false;
                }
            }

            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) && (ignoreMapper != true))
            {
                Directory.Delete(gamePath, true);
                if (ignoreMapper != false)
                {
                    throw new UnsupportedMapperException(nesFile);
                }
                else
                {
                    Debug.WriteLine(string.Format("Game {0} has mapper #{1}, skipped", nesFileName, nesFile.Mapper));
                    return(null);
                }
            }
            if ((nesFile.Mirroring == NesFile.MirroringType.FourScreenVram) && (ignoreMapper != true))
            {
                Directory.Delete(gamePath, true);
                if (ignoreMapper != false)
                {
                    throw new UnsupportedFourScreenException(nesFile);
                }
                else
                {
                    Debug.WriteLine(string.Format("Game {0} has four-screen mirroring, skipped", nesFileName, nesFile.Mapper));
                    return(null);
                }
            }
            // TODO: Make trainer check. I think that NES Mini doesn't support it.

            nesFile.Save(nesPath);
            var game = new NesGame(gamePath, true);

            game.Name = Path.GetFileNameWithoutExtension(nesFileName);
            if (game.Name.Contains("(J)"))
            {
                game.region = "Japan";
            }
            game.TryAutofill(crc32);
            game.Name = Regex.Replace(game.Name, @" ?\(.*?\)", string.Empty).Trim();
            game.Name = Regex.Replace(game.Name, @" ?\[.*?\]", string.Empty).Trim();
            game.Name = game.Name.Replace("_", " ").Replace("  ", " ") /*.Replace(", The", "")*/.Trim();

            // Trying to find cover file
            Image cover = null;

            if (!string.IsNullOrEmpty(nesFileName))
            {
                var imagePath = Path.Combine(Path.GetDirectoryName(nesFileName), Path.GetFileNameWithoutExtension(nesFileName) + ".png");
                if (File.Exists(imagePath))
                {
                    cover = LoadBitmap(imagePath);
                }
                imagePath = Path.Combine(Path.GetDirectoryName(nesFileName), Path.GetFileNameWithoutExtension(nesFileName) + ".jpg");
                if (File.Exists(imagePath))
                {
                    cover = LoadBitmap(imagePath);
                }
                var artDirectory = Path.Combine(Path.GetDirectoryName(Application.ExecutablePath), "art");
                Directory.CreateDirectory(artDirectory);
                imagePath = Path.Combine(artDirectory, Path.GetFileNameWithoutExtension(nesFileName) + ".png");
                if (File.Exists(imagePath))
                {
                    cover = LoadBitmap(imagePath);
                }
                imagePath = Path.Combine(artDirectory, Path.GetFileNameWithoutExtension(nesFileName) + ".jpg");
                if (File.Exists(imagePath))
                {
                    cover = LoadBitmap(imagePath);
                }
                var covers = Directory.GetFiles(artDirectory, string.Format("{0:X8}*.*", crc32), SearchOption.AllDirectories);
                if (covers.Length > 0)
                {
                    cover = LoadBitmap(covers[0]);
                }
            }
            if (cover == null)
            {
                if (game.region == "Japan")
                {
                    cover = Resources.blank_jp;
                }
                else
                {
                    cover = Resources.blank;
                }
            }
            game.Image = cover;
            game.Args  = DefaultArgs;
            game.Save();
            return(game);
        }