Ejemplo n.º 1
0
        protected override void DoTaskForFile(string pPath, IVgmtWorkerStruct pPsf2SettingsUpdaterStruct,
                                              DoWorkEventArgs e)
        {
            Psf2SettingsUpdaterStruct iniStruct = (Psf2SettingsUpdaterStruct)pPsf2SettingsUpdaterStruct;
            string workingFolder = String.Empty;

            //////////////////////
            // copy existing tags
            //////////////////////
            string formatString = XsfUtil.GetXsfFormatString(pPath);
            Dictionary <string, string> tagHash;
            Xsf psf2File, newPsf2File;

            if (!String.IsNullOrEmpty(formatString) &&
                formatString.Equals(Xsf.FormatNamePsf2))
            {
                try
                {
                    using (FileStream fs = File.Open(pPath, FileMode.Open, FileAccess.Read))
                    {
                        // initialize file
                        psf2File = new Xsf();
                        psf2File.Initialize(fs, pPath);

                        // copy tags
                        tagHash = psf2File.GetTagHash();
                    }

                    ///////////////
                    // unpack Psf2
                    ///////////////
                    workingFolder = Path.Combine(Path.GetDirectoryName(pPath), Path.GetFileNameWithoutExtension(pPath));
                    string unpackFolder = Path.Combine(workingFolder, "unpack_dir");
                    XsfUtil.UnpackPsf2(pPath, unpackFolder);

                    ///////////////////
                    // parse .ini file
                    ///////////////////
                    Psf2.Psf2IniSqIrxStruct originalIni;
                    string[] originalIniFiles = Directory.GetFiles(unpackFolder, "*.ini");

                    if (originalIniFiles.Length > 0)
                    {
                        using (FileStream iniFs = File.Open(originalIniFiles[0], FileMode.Open, FileAccess.Read))
                        {
                            originalIni = Psf2.ParseClsIniFile(iniFs);
                        }

                        ////////////////////
                        // update .ini file
                        ////////////////////
                        iniStruct.IniSettings = UpdateClsIniFile(iniStruct.IniSettings, originalIni, iniStruct.RemoveEmptySettings);
                        File.Delete(originalIniFiles[0]);
                        Psf2.WriteClsIniFile(iniStruct.IniSettings, originalIniFiles[0]);

                        ///////////////
                        // repack Psf2
                        ///////////////
                        string psf2FileName = Path.GetFileName(pPath);

                        ///////////////////
                        // copy mkpsf2.exe
                        ///////////////////
                        string mkpsf2Destination = Path.Combine(workingFolder, Path.GetFileName(MKPSF2_SOURCE_PATH));
                        File.Copy(MKPSF2_SOURCE_PATH, mkpsf2Destination, true);

                        //////////////////
                        // run mkpsf2.exe
                        //////////////////
                        string  arguments       = String.Format(" \"{0}\" \"{1}\"", psf2FileName, unpackFolder);
                        Process makePsf2Process = new Process();
                        makePsf2Process.StartInfo = new ProcessStartInfo(mkpsf2Destination, arguments);
                        makePsf2Process.StartInfo.UseShellExecute  = false;
                        makePsf2Process.StartInfo.CreateNoWindow   = true;
                        makePsf2Process.StartInfo.WorkingDirectory = workingFolder;
                        bool isSuccess = makePsf2Process.Start();
                        makePsf2Process.WaitForExit();
                        makePsf2Process.Close();
                        makePsf2Process.Dispose();

                        ////////////////
                        // replace tags
                        ////////////////
                        string newPsf2FilePath = Path.Combine(workingFolder, psf2FileName);

                        using (FileStream newFs = File.Open(newPsf2FilePath, FileMode.Open, FileAccess.Read))
                        {
                            // initialize new file
                            newPsf2File = new Xsf();
                            newPsf2File.Initialize(newFs, newPsf2FilePath);
                        }

                        // update to use old tag hash
                        newPsf2File.TagHash = tagHash;
                        newPsf2File.UpdateTags();

                        /////////////////////////
                        // replace original file
                        /////////////////////////
                        File.Copy(newPsf2FilePath, pPath, true);
                    }
                }
                catch (Exception ex)
                {
                    throw ex;
                }
                finally
                {
                    // delete working folder
                    if (Directory.Exists(workingFolder))
                    {
                        Directory.Delete(workingFolder, true);
                    }
                }
            }
        }
Ejemplo n.º 2
0
        private void buildPsf2Lib(Psf2ToPsf2LibStruct pPsf2ToPsf2LibStruct, DoWorkEventArgs e)
        {
            bool isSuccess;

            if (!Directory.Exists(pPsf2ToPsf2LibStruct.sourcePath))
            {
                this.progressStruct              = new VGMToolbox.util.ProgressStruct();
                this.progressStruct.NewNode      = null;
                this.progressStruct.FileName     = null;
                this.progressStruct.ErrorMessage =
                    String.Format("ERROR: Directory <{0}> Not Found.", pPsf2ToPsf2LibStruct.sourcePath);
                ReportProgress(0, this.progressStruct);
            }
            else
            {
                // Get Max Files
                this.maxFiles = Directory.GetFiles(pPsf2ToPsf2LibStruct.sourcePath, "*.psf2").Length;

                pPsf2ToPsf2LibStruct.libraryName =
                    FileUtil.CleanFileName(pPsf2ToPsf2LibStruct.libraryName) +
                    ".psf2lib";

                foreach (string f in
                         Directory.GetFiles(pPsf2ToPsf2LibStruct.sourcePath, "*.psf2"))
                {
                    // report progress
                    int progress = (++this.fileCount * 100) / maxFiles;
                    this.progressStruct          = new VGMToolbox.util.ProgressStruct();
                    this.progressStruct.NewNode  = null;
                    this.progressStruct.FileName = f;
                    ReportProgress(progress, this.progressStruct);

                    using (FileStream fs = File.OpenRead(f))
                    {
                        // check type
                        Type dataType = FormatUtil.getObjectType(fs);

                        if (dataType != null && dataType.Name.Equals("Xsf"))
                        {
                            Xsf psf2File = new Xsf();
                            psf2File.Initialize(fs, f);

                            if (psf2File.GetFormat().Equals(Xsf.FormatNamePsf2))
                            {
                                // copy tags
                                Dictionary <string, string> tagHash = psf2File.GetTagHash();

                                // unpack files
                                string outputDir = Path.Combine(PSF2_WORKING_FOLDER,
                                                                Path.GetFileNameWithoutExtension(f));
                                isSuccess = this.unpackPsf2File(f, outputDir);

                                if (isSuccess)
                                {
                                    // move non.ini files to lib working dir
                                    isSuccess = moveNonIniFiles(outputDir, PSF2LIB_WORKING_SUBFOLDER);

                                    // make simple psf2 with just pointer .ini
                                    isSuccess = makeMiniPsf2(outputDir);

                                    if (isSuccess)
                                    {
                                        // update tag script
                                        addTagsToTagScript(f, tagHash, pPsf2ToPsf2LibStruct.libraryName);
                                    }
                                }
                            } // if (psf2File.getFormat().Equals(Xsf.FORMAT_NAME_PSF2))
                        }     // if (dataType != null && dataType.Name.Equals("Xsf"))

                        fs.Close();
                    } // using (FileStream fs = File.OpenRead(f))
                }     // foreach (string f in Directory.GetFiles(pPsf2ToPsf2LibStruct.sourcePath, "*.psf2"))

                // combine files into lib
                makePsf2Lib(PSF2LIB_WORKING_SUBFOLDER, pPsf2ToPsf2LibStruct.libraryName);

                // execute tagging script
                executeBatchScript();

                // move completed psf2s to rip folder
                doCleanup(pPsf2ToPsf2LibStruct);
            }
        }