public NesGame(string gamesDirectory, string nesFileName, bool?ignoreMapper, ref bool?needPatch, NeedPatchDelegate needPatchCallback, Form parentForm = null, byte[] rawRomData = null) { uint crc32; if (!Path.GetExtension(nesFileName).ToLower().Equals(".fds")) { NesFile nesFile; if (rawRomData != null) { nesFile = new NesFile(rawRomData); } else { nesFile = new NesFile(nesFileName); } nesFile.CorrectRom(); crc32 = nesFile.CRC32; Code = GenerateCode(crc32); GamePath = Path.Combine(gamesDirectory, Code); Args = DefaultArgs; Type = GameType.Cartridge; 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; } } 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; } } // TODO: Make trainer check. I think that NES Mini doesn't support it. ConfigPath = Path.Combine(GamePath, Code + ".desktop"); nesFile.Save(NesPath); } else { byte[] fdsData; if (rawRomData != null) { fdsData = rawRomData; } else { fdsData = File.ReadAllBytes(nesFileName); } if (Encoding.ASCII.GetString(fdsData, 0, 3) == "FDS") // header? cut it! { var fdsDataNoHeader = new byte[fdsData.Length - 0x10]; Array.Copy(fdsData, 0x10, fdsDataNoHeader, 0, fdsDataNoHeader.Length); fdsData = fdsDataNoHeader; } crc32 = CRC32(fdsData); Code = GenerateCode(crc32); GamePath = Path.Combine(gamesDirectory, Code); Args = DefaultArgs + " --fds-auto-disk-side-switch-on-keypress"; // seems like need to make it default Type = GameType.FDS; Directory.CreateDirectory(GamePath); ConfigPath = Path.Combine(GamePath, Code + ".desktop"); NesPath = Path.Combine(GamePath, Code + ".fds"); File.WriteAllBytes(NesPath, fdsData); } Name = Path.GetFileNameWithoutExtension(nesFileName); Players = 1; ReleaseDate = DefaultReleaseDate; Publisher = DefaultPublisher; if (nesFileName.Contains("(J)")) { Region = "Japan"; } TryAutofill(crc32); Name = Regex.Replace(Name, @" ?\(.*?\)", string.Empty).Trim(); Name = Regex.Replace(Name, @" ?\[.*?\]", string.Empty).Trim(); Name = Name.Replace("_", " ").Replace(" ", " ") /*.Replace(", The", "")*/.Trim(); IconPath = Path.Combine(GamePath, Code + ".png"); SmallIconPath = Path.Combine(GamePath, Code + "_small.png"); GameGeniePath = Path.Combine(GamePath, GameGenieFileName); // 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) { SetImage(cover, ConfigIni.EightBitPngCompression); } else { SetImage(null, ConfigIni.EightBitPngCompression); } Save(); }
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); }
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); }