Ejemplo n.º 1
0
 private void GameGenieForm_FormClosed(object sender, FormClosedEventArgs e)
 {
     if (FGameGenieDataBase.Modified)
     {
         FGameGenieDataBase.Save();
     }
 }
Ejemplo n.º 2
0
        public ICollection <NesMiniApplication> AddGames(string[] files, Form parentForm = null)
        {
            var apps = new List <NesMiniApplication>();

            addedApplications = null;
            //bool NoForAllUnsupportedMappers = false;
            bool YesForAllUnsupportedMappers = false;

            YesForAllPatches = false;
            int count = 0;

            SetStatus(Resources.AddingGames);
            foreach (var file in files)
            {
                NesMiniApplication app = null;
                try
                {
                    var    fileName  = file;
                    var    ext       = Path.GetExtension(file).ToLower();
                    bool?  needPatch = YesForAllPatches ? (bool?)true : null;
                    byte[] rawData   = null;
                    if (ext == ".7z" || ext == ".zip" || ext == ".rar")
                    {
                        SevenZipExtractor.SetLibraryPath(Path.Combine(baseDirectory, IntPtr.Size == 8 ? @"tools\7z64.dll" : @"tools\7z.dll"));
                        using (var szExtractor = new SevenZipExtractor(file))
                        {
                            var filesInArchive    = new List <string>();
                            var nesFilesInArchive = new List <string>();
                            foreach (var f in szExtractor.ArchiveFileNames)
                            {
                                var e = Path.GetExtension(f).ToLower();
                                if (e == ".nes" || e == ".fds" || e == ".unf" || e == ".unif")
                                {
                                    nesFilesInArchive.Add(f);
                                }
                                filesInArchive.Add(f);
                            }
                            if (nesFilesInArchive.Count == 1) // Only one NES file
                            {
                                fileName = nesFilesInArchive[0];
                            }
                            else if (nesFilesInArchive.Count > 1) // Many NES files, need to select
                            {
                                if (SelectFileFromThread(nesFilesInArchive.ToArray()) == DialogResult.OK)
                                {
                                    fileName = selectedFile;
                                }
                                else
                                {
                                    continue;
                                }
                            }
                            else if (filesInArchive.Count == 1) // No NES files but only one another file
                            {
                                fileName = filesInArchive[0];
                            }
                            else // Need to select
                            {
                                if (SelectFileFromThread(filesInArchive.ToArray()) == DialogResult.OK)
                                {
                                    fileName = selectedFile;
                                }
                                else
                                {
                                    continue;
                                }
                            }
                            var o = new MemoryStream();
                            szExtractor.ExtractFile(fileName, o);
                            rawData = new byte[o.Length];
                            o.Seek(0, SeekOrigin.Begin);
                            o.Read(rawData, 0, (int)o.Length);
                        }
                    }
                    if (Path.GetExtension(fileName).ToLower() == ".nes")
                    {
                        try
                        {
                            app = NesGame.Import(fileName, YesForAllUnsupportedMappers ? (bool?)true : null, ref needPatch, needPatchCallback, this, rawData);

                            // Trying to import Game Genie codes
                            var lGameGeniePath = Path.Combine(Path.GetDirectoryName(fileName), Path.GetFileNameWithoutExtension(fileName) + ".xml");
                            if (File.Exists(lGameGeniePath))
                            {
                                GameGenieDataBase lGameGenieDataBase = new GameGenieDataBase(app);
                                lGameGenieDataBase.ImportCodes(lGameGeniePath, true);
                                lGameGenieDataBase.Save();
                            }
                        }
                        catch (Exception ex)
                        {
                            if (ex is UnsupportedMapperException || ex is UnsupportedFourScreenException)
                            {
                                var r = MessageBoxFromThread(this,
                                                             (ex is UnsupportedMapperException)
                                       ? string.Format(Resources.MapperNotSupported, Path.GetFileName(fileName), (ex as UnsupportedMapperException).ROM.Mapper)
                                       : string.Format(Resources.FourScreenNotSupported, Path.GetFileName(fileName)),
                                                             Resources.AreYouSure,
                                                             files.Length <= 1 ? MessageBoxButtons.YesNo : MessageBoxButtons.AbortRetryIgnore,
                                                             MessageBoxIcon.Warning, MessageBoxDefaultButton.Button2, true);
                                while (r == DialogResult.None)
                                {
                                    Thread.Sleep(100);
                                }
                                if (r == DialogResult.Yes || r == DialogResult.Abort || r == DialogResult.Retry)
                                {
                                    app = NesGame.Import(fileName, true, ref needPatch, needPatchCallback, this, rawData);
                                }
                                if (r == DialogResult.Abort)
                                {
                                    YesForAllUnsupportedMappers = true;
                                }
                            }
                            else
                            {
                                throw ex;
                            }
                        }
                    }
                    else
                    {
                        app = NesMiniApplication.Import(fileName, rawData);
                    }
                    ConfigIni.SelectedGames += ";" + app.Code;
                }
                catch (Exception ex)
                {
                    if (ex is ThreadAbortException)
                    {
                        return(null);
                    }
                    Debug.WriteLine(ex.Message + ex.StackTrace);
                    ShowError(ex, true);
                }
                if (app != null)
                {
                    apps.Add(app);
                }
                SetProgress(++count, files.Length);
            }
            return(apps); // Added games/apps
        }