public static NesMiniApplication CreateEmptyApp() { char prefix = 'X'; uint crc32; string code; string gamePath; do { crc32 = (uint)new Random().Next(); code = GenerateCode(crc32, prefix); gamePath = Path.Combine(GamesDirectory, code); } while (Directory.Exists(gamePath)); Directory.CreateDirectory(gamePath); var game = new NesMiniApplication(gamePath, true); game.Name = "New app"; game.Image = DefaultCover; game.Command = "enter some command here"; game.SaveCount = 0; game.Save(); var app = FromDirectory(gamePath); return(app); }
private static NesMiniApplication Import(string fileName, string zFileName, byte[] rawRomData, char prefixCode, string application, Image defaultCover, bool compress = false) { var crc32 = CRC32(rawRomData); var code = GenerateCode(crc32, prefixCode); var gamePath = Path.Combine(GamesDirectory, code); bool sevenZipped = false; if (compress) { string temp = null; try { if (!File.Exists(fileName)) { temp = Path.Combine(Path.GetTempPath(), Path.GetFileName(fileName)); File.WriteAllBytes(temp, rawRomData); rawRomData = Compress(temp); sevenZipped = true; } else { rawRomData = Compress(fileName); sevenZipped = true; } } catch (Exception ex) { Debug.WriteLine("Compression error: " + ex.Message + ex.Source); } finally { if (!string.IsNullOrEmpty(temp) && File.Exists(temp)) { File.Delete(temp); } } } var romName = Regex.Replace(Path.GetFileName(fileName), @"[^A-Za-z0-9.-]", "_").Trim() + (sevenZipped ? ".7z" : ""); var romPath = Path.Combine(gamePath, romName); if (Directory.Exists(gamePath)) { Directory.Delete(gamePath, true); } Directory.CreateDirectory(gamePath); File.WriteAllBytes(romPath, rawRomData); var game = new NesMiniApplication(gamePath, true); game.Name = Path.GetFileNameWithoutExtension(fileName); game.Name = Regex.Replace(game.Name, @" ?\(.*?\)", string.Empty).Trim(); game.Name = Regex.Replace(game.Name, @" ?\[.*?\]", string.Empty).Trim(); game.Name = game.Name.Replace("_", " ").Replace(" ", " ").Trim(); game.FindCover(fileName, zFileName, defaultCover, crc32); game.Command = string.Format("{0} /usr/share/games/nes/kachikachi/{1}/{2}", application, code, romName); game.Save(); return(NesMiniApplication.FromDirectory(gamePath)); }
public static NesMiniApplication Import(string fileName, byte[] rawRomData = null) { var extension = Path.GetExtension(fileName).ToLower(); char prefixCode; string application; switch (extension) { // For some unusual NES ROM formats case ".fds": return(FdsGame.Import(fileName, rawRomData)); case ".nes": case ".unf": case ".unif": prefixCode = 'U'; application = "/bin/nes"; break; case ".desktop": return(ImportApp(fileName)); case ".gb": case ".gbc": prefixCode = 'C'; application = "/bin/gb"; break; case ".gba": prefixCode = 'A'; application = "/bin/gba"; break; case ".n64": case ".z64": case ".v64": prefixCode = 'F'; application = "/bin/n64"; break; case ".smc": prefixCode = 'E'; application = "/bin/snes"; break; case ".gen": case ".smd": prefixCode = 'G'; application = "/bin/md"; break; case ".sms": prefixCode = 'M'; application = "/bin/sms"; break; default: prefixCode = 'Z'; application = "/bin/path-to-your-app"; break; } if (rawRomData == null) { rawRomData = File.ReadAllBytes(fileName); } var crc32 = CRC32(rawRomData); var code = GenerateCode(crc32, prefixCode); var gamePath = Path.Combine(GamesDirectory, code); var romName = Path.GetFileName(fileName).Replace(" ", "_"); var romPath = Path.Combine(gamePath, romName); Directory.CreateDirectory(gamePath); File.WriteAllBytes(romPath, rawRomData); var game = new NesMiniApplication(gamePath, true); game.Name = Path.GetFileNameWithoutExtension(fileName); game.Name = Regex.Replace(game.Name, @" ?\(.*?\)", string.Empty).Trim(); game.Name = Regex.Replace(game.Name, @" ?\[.*?\]", string.Empty).Trim(); game.Name = game.Name.Replace("_", " ").Replace(" ", " ").Trim(); game.FindCover(fileName, Resources.blank, crc32); game.Command = string.Format("{0} /usr/share/games/nes/kachikachi/{1}/{2}", application, code, romName); game.Save(); return(game); }
public static NesMiniApplication Import(string fileName, byte[] rawRomData = null) { var extension = Path.GetExtension(fileName).ToLower(); char prefixCode; string application; Image defaultCover = Resources.blank_app; switch (extension) { // For some unusual NES ROM formats case ".fds": return(FdsGame.ImportFds(fileName, rawRomData)); case ".nes": case ".unf": case ".unif": prefixCode = NesUGame.Prefix; application = NesUGame.DefaultApp; defaultCover = NesUGame.DefaultCover; // Most of UNIF roms are pirated Famicom games break; case ".desktop": return(ImportApp(fileName)); case ".gb": prefixCode = GbGame.Prefix; application = GbGame.DefaultApp; defaultCover = GbGame.DefaultCover; break; case ".gbc": prefixCode = GbcGame.Prefix; application = GbcGame.DefaultApp; defaultCover = GbcGame.DefaultCover; break; case ".gba": prefixCode = GbaGame.Prefix; application = GbaGame.DefaultApp; defaultCover = GbaGame.DefaultCover; break; case ".n64": case ".z64": case ".v64": prefixCode = N64Game.Prefix; application = N64Game.DefaultApp; defaultCover = N64Game.DefaultCover; break; case ".sfc": case ".smc": prefixCode = SnesGame.Prefix; application = SnesGame.DefaultApp; defaultCover = SnesGame.DefaultCover; break; case ".gen": case ".md": case ".smd": prefixCode = GenesisGame.Prefix; application = GenesisGame.DefaultApp; defaultCover = GenesisGame.DefaultCover; break; case ".sms": prefixCode = SmsGame.Prefix; application = SmsGame.DefaultApp; defaultCover = SmsGame.DefaultCover; break; case ".pce": prefixCode = PceGame.Prefix; application = PceGame.DefaultApp; defaultCover = PceGame.DefaultCover; break; default: prefixCode = Prefix; if (extension.Length > 1) { application = string.Format("/bin/{0}", extension.Substring(1)); } else { application = DefaultApp; } defaultCover = DefaultCover; break; } if (rawRomData == null) { rawRomData = File.ReadAllBytes(fileName); } var crc32 = CRC32(rawRomData); var code = GenerateCode(crc32, prefixCode); var gamePath = Path.Combine(GamesDirectory, code); var romName = Regex.Replace(Path.GetFileName(fileName), @"[^A-Za-z0-9.-]", "_").Trim(); var romPath = Path.Combine(gamePath, romName); Directory.CreateDirectory(gamePath); File.WriteAllBytes(romPath, rawRomData); var game = new NesMiniApplication(gamePath, true); game.Name = Path.GetFileNameWithoutExtension(fileName); game.Name = Regex.Replace(game.Name, @" ?\(.*?\)", string.Empty).Trim(); game.Name = Regex.Replace(game.Name, @" ?\[.*?\]", string.Empty).Trim(); game.Name = game.Name.Replace("_", " ").Replace(" ", " ").Trim(); game.FindCover(fileName, defaultCover, crc32); game.Command = string.Format("{0} /usr/share/games/nes/kachikachi/{1}/{2}", application, code, romName); game.Save(); return(NesMiniApplication.FromDirectory(gamePath)); }
public static NesMiniApplication Import(string inputFileName, string originalFileName = null, byte[] rawRomData = null) { var extension = System.IO.Path.GetExtension(inputFileName).ToLower(); if (extension == ".desktop") { return(ImportApp(inputFileName)); } if (rawRomData == null) // Maybe it's already extracted data? { rawRomData = File.ReadAllBytes(inputFileName); // If not, reading file } if (originalFileName == null) // Original file name from archive { originalFileName = System.IO.Path.GetFileName(inputFileName); } char prefix = DefaultPrefix; string application = extension.Length > 2 ? ("/bin/" + extension.Substring(1)) : DefaultApp; string args = null; Image cover = DefaultCover; uint crc32 = CRC32(rawRomData); string outputFileName = Regex.Replace(System.IO.Path.GetFileName(inputFileName), @"[^A-Za-z0-9()!\[\]\.\-]", "_").Trim(); // Trying to determine file type var appinfo = AppTypeCollection.GetAppByExtension(extension); bool patched = false; if (appinfo != null) { if (appinfo.DefaultApps.Length > 0) { application = appinfo.DefaultApps[0]; } prefix = appinfo.Prefix; cover = appinfo.DefaultCover; var patch = appinfo.Class.GetMethod("Patch"); if (patch != null) { object[] values = new object[] { inputFileName, rawRomData, prefix, application, outputFileName, args, cover, crc32 }; var result = (bool)patch.Invoke(null, values); if (!result) { return(null); } rawRomData = (byte[])values[1]; prefix = (char)values[2]; application = (string)values[3]; outputFileName = (string)values[4]; args = (string)values[5]; cover = (Image)values[6]; crc32 = (uint)values[7]; patched = true; } } if (!patched) { FindPatch(ref rawRomData, inputFileName, crc32); } var code = GenerateCode(crc32, prefix); var gamePath = Path.Combine(GamesDirectory, code); var romPath = Path.Combine(gamePath, outputFileName); if (Directory.Exists(gamePath)) { var files = Directory.GetFiles(gamePath, "*.*", SearchOption.AllDirectories); foreach (var f in files) { try { File.Delete(f); } catch { } } } Directory.CreateDirectory(gamePath); File.WriteAllBytes(romPath, rawRomData); var game = new NesMiniApplication(gamePath, true); game.Name = System.IO.Path.GetFileNameWithoutExtension(inputFileName); game.Name = Regex.Replace(game.Name, @" ?\(.*?\)", string.Empty).Trim(); game.Name = Regex.Replace(game.Name, @" ?\[.*?\]", string.Empty).Trim(); game.Name = game.Name.Replace("_", " ").Replace(" ", " ").Trim(); game.Command = $"{application} {GamesCloverPath}/{code}/{outputFileName}"; if (!string.IsNullOrEmpty(args)) { game.Command += " " + args; } game.FindCover(inputFileName, cover, crc32); game.Save(); var app = NesMiniApplication.FromDirectory(gamePath); if (app is ICloverAutofill) { (app as ICloverAutofill).TryAutofill(crc32); } if (ConfigIni.Compress) { app.Compress(); } return(app); }