Пример #1
0
        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));
        }
Пример #2
0
        private void buttonOk_Click(object sender, EventArgs e)
        {
            if (string.IsNullOrEmpty(textBoxCode.Text.Trim()))
            {
                MessageBox.Show(this, Resources.GGCodeEmpty, Resources.Error, MessageBoxButtons.OK, MessageBoxIcon.Warning);
                return;
            }

            if (FGame != null)
            {
                var tmpPath = Path.Combine(Path.GetTempPath(), FGame.Code);
                try
                {
                    FGame.CopyTo(tmpPath);
                    var lGame = NesMiniApplication.FromDirectory(tmpPath);
                    (lGame as NesMiniApplication).GameGenie = textBoxCode.Text;
                    lGame.Save();
                    (lGame as ISupportsGameGenie).ApplyGameGenie();
                }
                catch (GameGenieFormatException)
                {
                    MessageBox.Show(this, string.Format(Resources.GameGenieFormatError, textBoxCode.Text, FGame.Name), Resources.Error, MessageBoxButtons.OK, MessageBoxIcon.Error);
                    return;
                }
                catch (GameGenieNotFoundException)
                {
                    MessageBox.Show(this, string.Format(Resources.GameGenieNotFound, textBoxCode.Text, FGame.Name), Resources.Error, MessageBoxButtons.OK, MessageBoxIcon.Error);
                    return;
                }
                finally
                {
                    if (Directory.Exists(tmpPath))
                    {
                        Directory.Delete(tmpPath, true);
                    }
                }
            }

            if (string.IsNullOrEmpty(textBoxDescription.Text.Trim()))
            {
                MessageBox.Show(this, Resources.GGDescriptionEmpty, Resources.Error, MessageBoxButtons.OK, MessageBoxIcon.Warning);
                return;
            }
            textBoxCode.Text = textBoxCode.Text.ToUpper().Trim();
            DialogResult     = System.Windows.Forms.DialogResult.OK;
        }
Пример #3
0
        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));
        }
Пример #4
0
        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);
        }