/// <summary>
 /// Performs a final task for the worker.  Outputting a report, for example.
 /// </summary>
 protected virtual void DoFinalTask(IVgmtWorkerStruct pTaskStruct)
 {
 }
Exemple #2
0
        protected override void DoTaskForFile(string pPath, IVgmtWorkerStruct pTaskStruct, DoWorkEventArgs e)
        {
            IsoExtractorStruct taskStruct = (IsoExtractorStruct)pTaskStruct;
            Dictionary <string, FileStream> streamCache = new Dictionary <string, FileStream>();

            try
            {
                foreach (IDirectoryStructure d in taskStruct.Directories)
                {
                    if (!CancellationPending)
                    {
                        // open the stream and cache it
                        if (!streamCache.ContainsKey(d.SourceFilePath))
                        {
                            streamCache[d.SourceFilePath] = File.Open(d.SourceFilePath, FileMode.Open, FileAccess.Read, FileShare.Read);
                        }

                        d.Extract(ref streamCache, taskStruct.DestinationFolder, taskStruct.ExtractAsRaw);
                    }
                    else
                    {
                        e.Cancel = false;
                        break;
                    }
                }

                foreach (IFileStructure f in taskStruct.Files)
                {
                    if (!CancellationPending)
                    {
                        // open the stream and cache it
                        if (!streamCache.ContainsKey(f.SourceFilePath))
                        {
                            streamCache[f.SourceFilePath] = File.OpenRead(f.SourceFilePath);
                        }

                        f.Extract(ref streamCache, taskStruct.DestinationFolder, taskStruct.ExtractAsRaw);
                    }
                    else
                    {
                        e.Cancel = false;
                        break;
                    }
                }
            }
            catch (Exception ex)
            {
                throw new Exception(String.Format("Error extracting file: {0}", ex.Message));
            }
            finally
            {
                // close any open streams
                foreach (string key in streamCache.Keys)
                {
                    if (streamCache[key].CanRead)
                    {
                        streamCache[key].Close();
                        streamCache[key].Dispose();
                    }
                }
            }
        }
        protected virtual void DoTaskForDirectory(string pPath, IVgmtWorkerStruct pTaskStruct,
                                                  DoWorkEventArgs e)
        {
            foreach (string d in Directory.GetDirectories(pPath))
            {
                if (!CancellationPending)
                {
                    this.DoTaskForDirectory(d, pTaskStruct, e);
                }
                else
                {
                    e.Cancel = true;
                    break;
                }
            }

            foreach (string f in Directory.GetFiles(pPath))
            {
                if (!CancellationPending)
                {
                    // perform task
                    try
                    {
                        this.DoTaskForFile(f, pTaskStruct, e);
                    }
                    catch (Exception ex)
                    {
                        this.progressStruct.Clear();
                        this.progressStruct.ErrorMessage =
                            String.Format(CultureInfo.CurrentCulture, "Error processing <{0}>.  Error received: ", f) + ex.Message + Environment.NewLine;
                        ReportProgress(progress, this.progressStruct);
                    }
                    finally
                    {
                        this.DoFinally();

                        fileCount += 1;

                        // Report Progress
                        if ((fileCount == maxFiles) ||
                            (((fileCount * 100) / maxFiles) > this.progressCounter))
                        {
                            this.progressCounter += this.progressCounterIncrementer;

                            // output info
                            if (this.outputBuffer.Length > 0)
                            {
                                this.progressStruct.Clear();
                                progressStruct.GenericMessage = this.outputBuffer.ToString();
                                ReportProgress(this.Progress, progressStruct);

                                // clear out old info
                                this.outputBuffer.Length = 0;
                            }

                            // output progress
                            this.progress = (fileCount * 100) / maxFiles;
                            this.progressStruct.Clear();
                            this.progressStruct.FileName = f;
                            ReportProgress(this.progress, progressStruct);
                        }
                    }
                }
                else
                {
                    e.Cancel = true;
                    break;
                }
            }
        }
        protected override void DoTaskForFile(string pPath,
                                              IVgmtWorkerStruct pZlibExtractorStruct, DoWorkEventArgs e)
        {
            ZlibExtractorStruct zlibExtractorStruct =
                (ZlibExtractorStruct)pZlibExtractorStruct;

            this.progressStruct.Clear();

            if (zlibExtractorStruct.DoDecompress)
            {
                progressStruct.GenericMessage = String.Format("Decompressing <{0}>{1}", pPath, Environment.NewLine);
            }
            else
            {
                progressStruct.GenericMessage = String.Format("Compressing <{0}>{1}", pPath, Environment.NewLine);
            }
            ReportProgress(this.Progress, progressStruct);

            try
            {
                string outputFileName;

                if (zlibExtractorStruct.DoDecompress)
                {
                    outputFileName = Path.ChangeExtension(pPath, CompressionUtil.ZlibDecompressOutputExtension);
                }
                else
                {
                    outputFileName = Path.ChangeExtension(pPath, CompressionUtil.ZlibCompressOutputExtension);
                }

                using (FileStream fs = File.OpenRead(pPath))
                {
                    if (zlibExtractorStruct.StartingOffset > fs.Length)
                    {
                        throw new ArgumentOutOfRangeException("Starting Offset", "Offset cannot be greater than the file size.");
                    }

                    if (zlibExtractorStruct.DoDecompress)
                    {
                        CompressionUtil.DecompressZlibStreamToFile(fs, outputFileName, zlibExtractorStruct.StartingOffset);
                    }
                    else
                    {
                        CompressionUtil.CompressStreamToZlibFile(fs, outputFileName, zlibExtractorStruct.StartingOffset);
                    }
                }

                this.progressStruct.Clear();
                if (zlibExtractorStruct.DoDecompress)
                {
                    progressStruct.GenericMessage = String.Format("    {0} decompressed.{1}", Path.GetFileName(outputFileName), Environment.NewLine);
                }
                else
                {
                    progressStruct.GenericMessage = String.Format("    {0} compressed.{1}", Path.GetFileName(outputFileName), Environment.NewLine);
                }

                ReportProgress(this.Progress, progressStruct);
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
 protected override void DoTaskForFile(string pPath, IVgmtWorkerStruct pTaskStruct,
                                       DoWorkEventArgs e)
 {
 }
        protected override void DoTaskForFile(string pPath, IVgmtWorkerStruct pPsf2TimerStruct,
                                              DoWorkEventArgs e)
        {
            string outputSqFileName = null;

            string[] libPaths;
            string[] sqFiles;
            string[] iniFiles;
            Psf2.Psf2IniSqIrxStruct psf2IniStruct;

            string filePath;
            string fileDir;
            string fileName;
            string outputDir;
            string libOutputDir;

            Ps2SequenceData.Ps2SqTimingStruct psf2Time;
            int    minutes;
            double seconds;
            int    sequenceNumber = 0;

            StringBuilder batchFile = new StringBuilder();
            string        batchFilePath;

            string formatString = XsfUtil.GetXsfFormatString(pPath);

            if (!String.IsNullOrEmpty(formatString) && (formatString.Equals(Xsf.FormatNamePsf2)))
            {
                filePath = Path.GetFullPath(pPath);
                fileDir  = Path.GetDirectoryName(filePath);
                fileName = Path.GetFileNameWithoutExtension(filePath);

                outputDir = XsfUtil.UnpackPsf2(filePath);

                // parse ini
                iniFiles = Directory.GetFiles(outputDir, "PSF2.INI", SearchOption.AllDirectories);

                if (iniFiles.Length > 0)
                {
                    using (FileStream iniFs = File.Open(iniFiles[0], FileMode.Open, FileAccess.Read))
                    {
                        // parse ini file to get SQ info
                        psf2IniStruct = Psf2.ParseClsIniFile(iniFs);
                    }

                    using (FileStream fs = File.OpenRead(pPath))
                    {
                        Psf2 psf2File = new Psf2();
                        psf2File.Initialize(fs, pPath);

                        // check for libs
                        libPaths = psf2File.GetLibPathArray();
                    }

                    // copy the SQ file out (should only be one)
                    sqFiles = Directory.GetFiles(outputDir, psf2IniStruct.SqFileName, SearchOption.AllDirectories);

                    if (sqFiles.Length > 0)
                    {
                        if (!String.IsNullOrEmpty(psf2IniStruct.SequenceNumber))
                        {
                            sequenceNumber   = int.Parse(psf2IniStruct.SequenceNumber);
                            outputSqFileName = String.Format("{0}_n={1}.SQ", outputDir, psf2IniStruct.SequenceNumber);
                        }
                        else
                        {
                            outputSqFileName = String.Format("{0}.SQ", outputDir);
                        }
                        File.Copy(sqFiles[0], outputSqFileName, true);
                    }
                    else // miniPSF2
                    {
                        // unpack each lib, looking for the needed file
                        foreach (string libPath in libPaths)
                        {
                            fileDir      = Path.GetDirectoryName(libPath);
                            fileName     = Path.GetFileNameWithoutExtension(libPath);
                            libOutputDir = Path.Combine(fileDir, fileName);

                            if (!extractedLibHash.ContainsKey(libPath))
                            {
                                libOutputDir = XsfUtil.UnpackPsf2(libPath);
                                extractedLibHash.Add(libPath, libOutputDir.ToUpper());
                            }

                            // look for the file in this lib
                            sqFiles = Directory.GetFiles(libOutputDir, psf2IniStruct.SqFileName, SearchOption.AllDirectories);

                            if (sqFiles.Length > 0)
                            {
                                if (!String.IsNullOrEmpty(psf2IniStruct.SequenceNumber))
                                {
                                    sequenceNumber   = int.Parse(psf2IniStruct.SequenceNumber);
                                    outputSqFileName = String.Format("{0}_n={1}.SQ", outputDir, psf2IniStruct.SequenceNumber);
                                }
                                else
                                {
                                    outputSqFileName = String.Format("{0}.SQ", outputDir);
                                }

                                File.Copy(sqFiles[0], outputSqFileName, true);
                                break;
                            }

                            // delete the unpkpsf2 output folder
                            if (Directory.Exists(libOutputDir))
                            {
                                Directory.Delete(libOutputDir, true);
                            }
                        } // foreach (string libPath in libPaths)
                    }

                    // get time and add to script
                    if (!String.IsNullOrEmpty(outputSqFileName))
                    {
                        psf2Time = XsfUtil.GetTimeForPsf2File(outputSqFileName, sequenceNumber);

                        File.Delete(outputSqFileName);  // delete SQ file

                        minutes = (int)(psf2Time.TimeInSeconds / 60d);
                        seconds = (psf2Time.TimeInSeconds - (minutes * 60));
                        // seconds = Math.Ceiling(seconds);

                        // shouldn't be needed without Math.Ceiling call, but whatever
                        if (seconds >= 60)
                        {
                            minutes++;
                            seconds -= 60d;
                        }

                        batchFile.AppendFormat("psfpoint.exe -length=\"{0}:{1}\" -fade=\"{2}\" \"{3}\"",
                                               minutes.ToString(), seconds.ToString().PadLeft(2, '0'),
                                               psf2Time.FadeInSeconds.ToString(), Path.GetFileName(pPath));
                        batchFile.Append(Environment.NewLine);

                        batchFilePath = Path.Combine(Path.GetDirectoryName(pPath), BATCH_FILE_NAME);

                        if (!File.Exists(batchFilePath))
                        {
                            using (FileStream cfs = File.Create(batchFilePath)) { };
                        }

                        using (StreamWriter sw = new StreamWriter(File.Open(batchFilePath, FileMode.Append, FileAccess.Write)))
                        {
                            sw.Write(batchFile.ToString());
                        }

                        // report warnings
                        if (!String.IsNullOrEmpty(psf2Time.Warnings))
                        {
                            this.progressStruct.Clear();
                            progressStruct.GenericMessage = String.Format("{0}{1}  WARNINGS{2}    {3}", pPath, Environment.NewLine, Environment.NewLine, psf2Time.Warnings);
                            ReportProgress(this.Progress, progressStruct);
                        }
                    }
                } // if (iniFiles.Length > 0)

                // delete the unpkpsf2 output folder
                if ((Directory.Exists(outputDir)) &&
                    (!extractedLibHash.ContainsValue(outputDir.ToUpper())))
                {
                    Directory.Delete(outputDir, true);
                }
            } // if (psf2File.getFormat().Equals(Xsf.FORMAT_NAME_PSF2) && (!psf2File.IsFileLibrary()))
        }
        protected override void DoTaskForFile(string pPath, IVgmtWorkerStruct pPsf2DataFinderStruct, DoWorkEventArgs e)
        {
            Psf2DataFinderStruct psf2Struct = (Psf2DataFinderStruct)pPsf2DataFinderStruct;

            long offset;

            uint   sqLength;
            string sqName;
            int    sqNumber = 0;

            Psf2.ProbableItemStruct sqEntry;
            ArrayList sqFiles = new ArrayList();
            bool      sqNamingMessageDisplayed = false;

            uint   hdLength;
            string hdName;
            int    hdNumber = 0;

            HdStruct  hdObject;
            ArrayList hdArrayList = new ArrayList();

            ArrayList emptyRowList = new ArrayList();

            Psf2.ProbableItemStruct   potentialBd;
            Psf2.ProbableItemStruct[] potentialBdList;
            byte[] bdRow = new byte[0x10];

            // display file name
            this.progressStruct.Clear();
            this.progressStruct.GenericMessage = String.Format("[{0}]{1}", pPath, Environment.NewLine);
            this.ReportProgress(Constants.ProgressMessageOnly, this.progressStruct);

            using (FileStream fs = File.OpenRead(pPath))
            {
                string destinationFolder = Path.Combine(Path.GetDirectoryName(pPath), Path.GetFileNameWithoutExtension(pPath));

                // get HD Files
                #region HD EXTRACT
                this.progressStruct.Clear();
                this.progressStruct.GenericMessage = String.Format("  Extracting HD{0}", Environment.NewLine);
                this.ReportProgress(Constants.ProgressMessageOnly, this.progressStruct);

                offset = 0;

                while ((offset = ParseFile.GetNextOffset(fs, offset, Psf2.HD_SIGNATURE)) > -1)
                {
                    try
                    {
                        hdLength = BitConverter.ToUInt32(ParseFile.ParseSimpleOffset(fs, offset + 0xC, 4), 0);

                        hdName = String.Format("{0}_{1}.HD", Path.GetFileNameWithoutExtension(pPath), hdNumber++.ToString("X4"));
                        ParseFile.ExtractChunkToFile(fs, offset - 0x10, (int)hdLength,
                                                     Path.Combine(destinationFolder, hdName), true, true);

                        // get info
                        hdObject                  = new HdStruct();
                        hdObject.FileName         = hdName;
                        hdObject.startingOffset   = offset - 0x10;
                        hdObject.length           = BitConverter.ToUInt32(ParseFile.ParseSimpleOffset(fs, offset + 0xC, 4), 0);
                        hdObject.expectedBdLength = BitConverter.ToUInt32(ParseFile.ParseSimpleOffset(fs, offset + 0x10, 4), 0);
                        hdObject.vagSectionOffset = hdObject.startingOffset + BitConverter.ToUInt32(ParseFile.ParseSimpleOffset(fs, offset + 0x20, 4), 0);
                        hdObject.maxVagInfoNumber = BitConverter.ToUInt32(ParseFile.ParseSimpleOffset(fs, hdObject.vagSectionOffset + 0xC, 4), 0);

                        hdObject.vagInfoOffsetAddr    = new long[hdObject.maxVagInfoNumber + 1];
                        hdObject.vagOffset            = new long[hdObject.maxVagInfoNumber + 1];
                        hdObject.vagLengths           = new long[hdObject.maxVagInfoNumber];
                        hdObject.IsSmallSamplePresent = false;

                        for (int i = 0; i <= hdObject.maxVagInfoNumber; i++)
                        {
                            hdObject.vagInfoOffsetAddr[i] = BitConverter.ToUInt32(ParseFile.ParseSimpleOffset(fs, hdObject.vagSectionOffset + 0x10 + (i * 4), 4), 0);
                            hdObject.vagOffset[i]         = BitConverter.ToUInt32(ParseFile.ParseSimpleOffset(fs, hdObject.vagSectionOffset + hdObject.vagInfoOffsetAddr[i], 4), 0);

                            if (i > 0)
                            {
                                hdObject.vagLengths[i - 1] = hdObject.vagOffset[i] - hdObject.vagOffset[i - 1];

                                if (hdObject.vagLengths[i - 1] < Psf2.MIN_ADPCM_ROW_SIZE)
                                {
                                    hdObject.IsSmallSamplePresent = true;
                                }
                            }
                        }

                        // add to array
                        hdArrayList.Add(hdObject);
                    }
                    catch (Exception hdEx)
                    {
                        this.progressStruct.Clear();
                        this.progressStruct.ErrorMessage = String.Format("      Error extracting HD at offset 0x{0}: {1}{2}", offset.ToString("X8"), hdEx.Message, Environment.NewLine);
                        ReportProgress(progress, this.progressStruct);
                    }

                    // increment offset
                    offset += 1;
                }
                #endregion

                // get SQ Files
                #region SQ EXTRACT
                this.progressStruct.Clear();
                this.progressStruct.GenericMessage = String.Format("  Extracting SQ{0}", Environment.NewLine);
                this.ReportProgress(Constants.ProgressMessageOnly, this.progressStruct);

                sqEntry = new Psf2.ProbableItemStruct();
                offset  = 0;

                // build file list
                while ((offset = ParseFile.GetNextOffset(fs, offset, Ps2SequenceData.SIGNATURE_BYTES)) > -1)
                {
                    sqLength = BitConverter.ToUInt32(ParseFile.ParseSimpleOffset(fs, offset + 0xC, 4), 0);

                    if ((!psf2Struct.UseSeqMinimumSize) || ((psf2Struct.UseSeqMinimumSize) &&
                                                            (sqLength >= psf2Struct.MinimumSize)))
                    {
                        sqEntry.offset = offset - 0x10;
                        sqEntry.length = sqLength;
                        sqFiles.Add(sqEntry);
                    }

                    offset += 1;
                }

                foreach (Psf2.ProbableItemStruct sq in sqFiles)
                {
                    if (psf2Struct.ReorderSqFiles)
                    {
                        if (hdArrayList.Count < sqFiles.Count)
                        {
                            if (!sqNamingMessageDisplayed)
                            {
                                this.progressStruct.Clear();
                                this.progressStruct.ErrorMessage = String.Format(
                                    "Warning, cannot reorder SQ files, there are less HD files than SQ files.{0}", Environment.NewLine);
                                this.ReportProgress(this.progress, this.progressStruct);
                                sqNamingMessageDisplayed = true;
                            }

                            sqName = String.Format("{0}_{1}.SQ", Path.GetFileNameWithoutExtension(pPath), sqNumber++.ToString("X4"));
                        }
                        else
                        {
                            hdObject = (HdStruct)hdArrayList[hdArrayList.Count - sqFiles.Count + sqNumber++];
                            sqName   = Path.ChangeExtension(hdObject.FileName, ".SQ");
                        }
                    }
                    else
                    {
                        sqName = String.Format("{0}_{1}.SQ", Path.GetFileNameWithoutExtension(pPath), sqNumber++.ToString("X4"));
                    }

                    ParseFile.ExtractChunkToFile(fs, sq.offset, (int)sq.length,
                                                 Path.Combine(destinationFolder, sqName), true, true);
                }


                #endregion

                // get BD files
                #region BD EXTRACT
                this.progressStruct.Clear();
                this.progressStruct.GenericMessage = String.Format("  Extracting BD...WARNING, THIS CAN TAKE A LONG TIME...{0}", Environment.NewLine);
                this.ReportProgress(Constants.ProgressMessageOnly, this.progressStruct);

                offset = 0;

                // build list of potential adpcm start indexes
                potentialBd = new Psf2.ProbableItemStruct();

                while ((offset = ParseFile.GetNextOffset(fs, offset, Psf2.VB_START_BYTES, false)) > -1)
                {
                    //if (offset == 0x1DD3800)
                    //{
                    //    int fff = 1;
                    //}

                    if ((psf2Struct.UseZeroOffsetForBd) && (offset % 0x10 == 0) ||
                        (!psf2Struct.UseZeroOffsetForBd))
                    {
                        try
                        {
                            bdRow = ParseFile.ParseSimpleOffset(fs, offset, bdRow.Length);

                            if (Psf2.IsPotentialAdpcm(fs, offset + 0x10, Psf2.MIN_ADPCM_ROW_COUNT, false))
                            {
                                potentialBd.offset = offset;
                                emptyRowList.Add(potentialBd);
                            }
                        }
                        catch (Exception bdEx)
                        {
                            this.progressStruct.Clear();
                            this.progressStruct.ErrorMessage = String.Format(" ERROR finding BD for <{0}> at Offset 0x{1}: {2}{3}", pPath, offset.ToString("X8"), bdEx.Message, Environment.NewLine);
                            this.ReportProgress(this.progress, this.progressStruct);
                        }
                    }

                    offset += 1;
                }

                potentialBdList = (Psf2.ProbableItemStruct[])emptyRowList.ToArray(typeof(Psf2.ProbableItemStruct));

                // set probable lengths
                for (int i = 0; i < potentialBdList.Length; i++)
                {
                    if (i > 0)
                    {
                        potentialBdList[i - 1].length = (uint)(potentialBdList[i].offset - potentialBdList[i - 1].offset);
                    }
                }

                // compare HD sample sizes to potential adpcm sizes/indexes
                hdObject.startingOffset   = 0;
                hdObject.length           = 0;
                hdObject.bdStartingOffset = 0;
                hdObject.bdLength         = 0;

                string   bdName;
                string   newFileName;
                string[] dupeFileNames;

                for (int i = 0; i < hdArrayList.Count; i++)
                {
                    hdObject = (HdStruct)hdArrayList[i];
                    if (hdObject.vagLengths.Length < 1)
                    {
                        this.progressStruct.Clear();
                        this.progressStruct.ErrorMessage = String.Format(" ERROR building BD for <{0}>: {1} refers to a single VAG, cannot determine proper BD.  Skipping...{2}", pPath, hdObject.FileName, Environment.NewLine);
                        this.ReportProgress(this.progress, this.progressStruct);
                    }
                    else
                    {
                        for (int j = 0; j < potentialBdList.Length; j++)
                        {
                            // we have a potential match or are at the last item.
                            if ((hdObject.vagLengths[0] <= potentialBdList[j].length) ||
                                (potentialBdList[j].length == 0))
                            {
                                try
                                {
                                    hdObject = PopulateBdOffsetLength(fs, potentialBdList, j, hdObject);

                                    if (hdObject.bdLength > 0)
                                    {
                                        // check for other BD files that matched and rename accordingly
                                        dupeFileNames = Directory.GetFiles(destinationFolder, Path.GetFileNameWithoutExtension(hdObject.FileName) + "*.BD");

                                        if (dupeFileNames.Length >= 1)
                                        {
                                            bdName = String.Format("{0}_{1}.BD", Path.GetFileNameWithoutExtension(hdObject.FileName), (dupeFileNames.Length).ToString("X4"));

                                            if (dupeFileNames.Length == 1)
                                            {
                                                // rename existing
                                                newFileName = String.Format("{0}_{1}.BD", Path.GetFileNameWithoutExtension(hdObject.FileName), (dupeFileNames.Length - 1).ToString("X4"));
                                                File.Move(dupeFileNames[0], Path.Combine(Path.GetDirectoryName(dupeFileNames[0]), newFileName));
                                            }
                                        }
                                        else
                                        {
                                            bdName = Path.ChangeExtension(hdObject.FileName, ".BD");
                                        }


                                        ParseFile.ExtractChunkToFile(fs, hdObject.bdStartingOffset, (int)hdObject.bdLength,
                                                                     Path.Combine(destinationFolder, bdName), true, true);
                                    }
                                }
                                catch (Exception ex)
                                {
                                    this.progressStruct.Clear();
                                    this.progressStruct.ErrorMessage = String.Format("     ERROR building BD for <{0}>: {1}{2}", pPath, ex.Message, Environment.NewLine);
                                    this.ReportProgress(this.progress, this.progressStruct);
                                }
                            }
                        }
                    }
                }
                #endregion
            }
        }
        protected override void DoTaskForFile(string pPath, IVgmtWorkerStruct pTaskStruct, DoWorkEventArgs e)
        {
            ExtractOggStruct extractOggStruct = (ExtractOggStruct)pTaskStruct;

            long offset = 0;

            byte pageType;
            long pageSize;
            uint bitstreamSerialNumber;

            byte segmentCount;
            uint sizeOfAllSegments;
            byte i;

            string outputPath;
            string outputFileName;

            byte[] rawPageBytes;
            bool   pageWrittenToFile = false;

            Dictionary <uint, FileStream> outputStreams = new Dictionary <uint, FileStream>();

            using (FileStream fs = File.Open(pPath, FileMode.Open, FileAccess.Read))
            {
                try
                {
                    outputPath = Path.Combine(Path.GetDirectoryName(pPath), String.Format("VGMT_EXTRACTED_OGGs", Path.GetFileNameWithoutExtension(pPath)));

                    while ((offset = ParseFile.GetNextOffset(fs, offset, XiphOrgOggContainer.MAGIC_BYTES)) > -1)
                    {
                        if (!this.CancellationPending)
                        {
                            pageWrittenToFile = false;

                            //------------------
                            // get page details
                            //------------------
                            pageType = ParseFile.ParseSimpleOffset(fs, offset + 5, 1)[0];
                            bitstreamSerialNumber = BitConverter.ToUInt32(ParseFile.ParseSimpleOffset(fs, offset + 0xE, 4), 0);
                            segmentCount          = ParseFile.ParseSimpleOffset(fs, offset + 0x1A, 1)[0];

                            sizeOfAllSegments = 0;

                            for (i = 0; i < segmentCount; i++)
                            {
                                sizeOfAllSegments += ParseFile.ParseSimpleOffset(fs, offset + 0x1B + i, 1)[0];
                            }

                            pageSize = 0x1B + segmentCount + sizeOfAllSegments;

                            //-----------------------------
                            // write page
                            //-----------------------------
                            rawPageBytes = ParseFile.ParseSimpleOffset(fs, offset, (int)pageSize);

                            // open stream
                            if ((pageType & XiphOrgOggContainer.PAGE_TYPE_BEGIN_STREAM) ==
                                XiphOrgOggContainer.PAGE_TYPE_BEGIN_STREAM)
                            {
                                if (outputStreams.ContainsKey(bitstreamSerialNumber))
                                {
                                    if (extractOggStruct.StopParsingOnFormatError)
                                    {
                                        throw new FormatException(
                                                  String.Format("Beginning of Stream page found multiple times without End of Stream page, for serial number: {1}.{2}",
                                                                pPath,
                                                                bitstreamSerialNumber.ToString("X8"),
                                                                Environment.NewLine));
                                    }
                                    else
                                    {
                                        //this.progressStruct.Clear();
                                        //progressStruct.GenericMessage = String.Format("Warning, for file <{0}>, Beginning of Stream page found multiple times without End of Stream page, for serial number: {1}.{2}",
                                        //                                    pPath,
                                        //                                    bitstreamSerialNumber.ToString("X8"),
                                        //                                    Environment.NewLine);
                                        //ReportProgress(Constants.ProgressMessageOnly, progressStruct);
                                    }
                                }
                                else
                                {
                                    // create the output stream
                                    outputFileName = Path.Combine(outputPath, String.Format("{0}_0x{1}_{2}.ogg", Path.GetFileNameWithoutExtension(pPath), offset.ToString("X8"), bitstreamSerialNumber.ToString("X8")));
                                    outputFileName = FileUtil.GetNonDuplicateFileName(outputFileName);

                                    if (!Directory.Exists(outputPath))
                                    {
                                        Directory.CreateDirectory(outputPath);
                                    }

                                    outputStreams[bitstreamSerialNumber] = File.Open(outputFileName, FileMode.CreateNew, FileAccess.Write);
                                    outputStreams[bitstreamSerialNumber].Write(rawPageBytes, 0, rawPageBytes.Length);
                                    pageWrittenToFile = true;
                                }
                            }

                            // write page otherwise
                            if (outputStreams.ContainsKey(bitstreamSerialNumber))
                            {
                                if (!pageWrittenToFile)
                                {
                                    outputStreams[bitstreamSerialNumber].Write(rawPageBytes, 0, rawPageBytes.Length);
                                    pageWrittenToFile = true;
                                }
                            }
                            else
                            {
                                if (extractOggStruct.StopParsingOnFormatError)
                                {
                                    throw new FormatException(
                                              String.Format("Stream data page found without Beginning of Stream page, for serial number: {1}.{2}",
                                                            pPath,
                                                            bitstreamSerialNumber.ToString("X8"),
                                                            Environment.NewLine));
                                }
                                else
                                {
                                    //this.progressStruct.Clear();
                                    //progressStruct.GenericMessage = String.Format("Warning, for file <{0}>, Stream data page found without Beginning of Stream page, for serial number: {1}.{2}",
                                    //                                    pPath,
                                    //                                    bitstreamSerialNumber.ToString("X8"),
                                    //                                    Environment.NewLine);
                                    //ReportProgress(Constants.ProgressMessageOnly, progressStruct);
                                }
                            }


                            // close stream if needed
                            if ((pageType & XiphOrgOggContainer.PAGE_TYPE_END_STREAM) ==
                                XiphOrgOggContainer.PAGE_TYPE_END_STREAM)
                            {
                                if (outputStreams.ContainsKey(bitstreamSerialNumber))
                                {
                                    if (!pageWrittenToFile)
                                    {
                                        outputStreams[bitstreamSerialNumber].Write(rawPageBytes, 0, rawPageBytes.Length);
                                        pageWrittenToFile = true;
                                    }

                                    outputStreams[bitstreamSerialNumber].Close();
                                    outputStreams[bitstreamSerialNumber].Dispose();

                                    // remove stream from dictionary
                                    outputStreams.Remove(bitstreamSerialNumber);
                                }
                                else
                                {
                                    if (extractOggStruct.StopParsingOnFormatError)
                                    {
                                        throw new FormatException(
                                                  String.Format("End of Stream page found without a Beginning of Stream page, for serial number: {1}{2}.",
                                                                pPath,
                                                                bitstreamSerialNumber.ToString("X8"),
                                                                Environment.NewLine));
                                    }
                                    else
                                    {
                                        //this.progressStruct.Clear();
                                        //progressStruct.GenericMessage = String.Format("Warning, for file <{0}>, End of Stream page found without a Beginning of Stream page, for serial number: {1}.{2}",
                                        //                                    pPath,
                                        //                                    bitstreamSerialNumber.ToString("X8"),
                                        //                                    Environment.NewLine);
                                        //ReportProgress(Constants.ProgressMessageOnly, progressStruct);
                                    }
                                }
                            }
                            //} if (pageType > 0)

                            offset += pageSize;
                        }
                        else
                        {
                            e.Cancel = true;
                            return;
                        }
                    } // while
                }
                catch (Exception ex)
                {
                    throw ex;
                }
                finally
                {
                    // close any unfinished streams
                    foreach (uint k in outputStreams.Keys)
                    {
                        outputStreams[k].Close();
                        outputStreams[k].Dispose();
                    }
                }
            }
        }
Exemple #9
0
 protected override void DoTaskForFile(string pPath, IVgmtWorkerStruct pSdatFinderStruct,
                                       DoWorkEventArgs e)
 {
     string[] outputPaths = SdatUtil.ExtractSdatsFromFile(pPath, null);
 }
Exemple #10
0
        protected override void DoTaskForFile(string path, IVgmtWorkerStruct pMpegDemuxStruct, DoWorkEventArgs e)
        {
            MpegDemuxStruct demuxStruct = (MpegDemuxStruct)pMpegDemuxStruct;

            MpegStream.DemuxOptionsStruct demuxOptions = new MpegStream.DemuxOptionsStruct();

            demuxOptions.ExtractAudio = demuxStruct.ExtractAudio;
            demuxOptions.ExtractVideo = demuxStruct.ExtractVideo;

            demuxOptions.AddHeader         = demuxStruct.AddHeader;
            demuxOptions.SplitAudioStreams = demuxStruct.SplitAudioTracks;
            demuxOptions.AddPlaybackHacks  = demuxStruct.AddPlaybackHacks;

            switch (demuxStruct.SourceFormat)
            {
            case "ASF (MS Advanced Systems Format)":
            case "WMV (MS Advanced Systems Format)":
                MicrosoftAsfContainer asfStream = new MicrosoftAsfContainer(path);
                asfStream.DemultiplexStreams(demuxOptions);
                break;

            case "BIK (Bink Video Container)":
                BinkStream binkStream = new BinkStream(path);
                binkStream.DemultiplexStreams(demuxOptions);
                break;

            case "DSI (Racjin/Racdym PS2 Video)":
                RacjinDsiStream dsiStream = new RacjinDsiStream(path);
                dsiStream.DemultiplexStreams(demuxOptions);
                break;

            case "DVD Video (VOB)":
                DvdVideoStream dvdStream = new DvdVideoStream(path);
                dvdStream.DemultiplexStreams(demuxOptions);
                break;

            case "Electronic Arts VP6 (VP6)":
                ElectronicArtsVp6Stream vp6Stream = new ElectronicArtsVp6Stream(path);
                vp6Stream.DemultiplexStreams(demuxOptions);
                break;

            case "Electronic Arts MPC (MPC)":
                ElectronicArtsMpcStream mpcStream = new ElectronicArtsMpcStream(path);
                mpcStream.DemultiplexStreams(demuxOptions);
                break;

            case "H4M (Hudson GameCube Video)":
                HudsonHvqm4VideoStream hvqmStream = new HudsonHvqm4VideoStream(path);
                hvqmStream.DemultiplexStreams(demuxOptions);
                break;

            case "MO (Mobiclip)":
                MobiclipStream.MovieType movieType = MobiclipStream.GetMobiclipStreamType(path);

                switch (movieType)
                {
                //case MobiclipStream.MovieType.NintendoDs:
                //    MobiclipNdsStream mobiclipNdsStream = new MobiclipNdsStream(path);
                //    mobiclipNdsStream.DemultiplexStreams(demuxOptions);
                //    break;
                case MobiclipStream.MovieType.Wii:
                    MobiclipWiiStream mobiclipWiiStream = new MobiclipWiiStream(path);
                    mobiclipWiiStream.DemultiplexStreams(demuxOptions);
                    break;

                default:
                    throw new FormatException(String.Format("Unsupported Mobiclip type, for file: {0}", Path.GetFileName(path)));
                }

                break;

            case "MPEG":
                int mpegType = MpegStream.GetMpegStreamType(path);

                switch (mpegType)
                {
                case 1:
                    Mpeg1Stream mpeg1Stream = new Mpeg1Stream(path);
                    mpeg1Stream.DemultiplexStreams(demuxOptions);
                    break;

                case 2:
                    Mpeg2Stream mpeg2Stream = new Mpeg2Stream(path);
                    mpeg2Stream.DemultiplexStreams(demuxOptions);
                    break;

                default:
                    throw new FormatException(String.Format("Unsupported MPEG type, for file: {0}", Path.GetFileName(path)));
                }
                break;

            case "MPS (PSP UMD Movie)":
                SonyPspMpsStream mpsStream = new SonyPspMpsStream(path);
                mpsStream.DemultiplexStreams(demuxOptions);
                break;

            case "PAM (PlayStation Advanced Movie)":
                SonyPamStream pamStream = new SonyPamStream(path);
                pamStream.DemultiplexStreams(demuxOptions);
                break;

            case "PMF (PSP Movie Format)":
                SonyPmfStream pmfStream = new SonyPmfStream(path);
                pmfStream.DemultiplexStreams(demuxOptions);
                break;

            case "PSS (PlayStation Stream)":
                SonyPssStream sps = new SonyPssStream(path);
                sps.DemultiplexStreams(demuxOptions);
                break;

            case "SFD (CRI Sofdec Video)":
                SofdecStream ss = new SofdecStream(path);
                ss.DemultiplexStreams(demuxOptions);
                break;

            case "THP":
                NintendoThpMovieStream thp = new NintendoThpMovieStream(path);
                thp.DemultiplexStreams(demuxOptions);
                break;

            case "USM (CRI Movie 2)":
                CriUsmStream cus = new CriUsmStream(path);
                cus.DemultiplexStreams(demuxOptions);
                break;

            case "XMV (Xbox Media Video)":
                XmvStream xmv = new XmvStream(path);
                xmv.DemultiplexStreams(demuxOptions);
                break;

            default:
                throw new FormatException("Source format not defined.");
            }

            this.outputBuffer.Append(Path.GetFileName(path) + Environment.NewLine);
        }
 protected override void DoTaskForFile(string pPath, IVgmtWorkerStruct pSdatExtractorStruct,
                                       DoWorkEventArgs e)
 {
     string outputDir = SdatUtil.ExtractSdat(pPath);
 }
Exemple #12
0
        protected override void DoTaskForFile(string pPath, IVgmtWorkerStruct pMk2sfStruct, DoWorkEventArgs e)
        {
            this.progressStruct.Clear();
            this.progressStruct.GenericMessage = String.Format("Processing: [{0}]{1}", pPath, Environment.NewLine);
            this.ReportProgress(this.progress, this.progressStruct);

            NdsTo2sfStruct workingStruct      = (NdsTo2sfStruct)pMk2sfStruct;
            string         ndsTo2sfOutputPath = Path.Combine(Path.GetDirectoryName(pPath), String.Format("{0}{1}", Path.GetFileNameWithoutExtension(pPath), XsfUtil.NDSTO2SF_FOLDER_SUFFIX));

            // Build Nds object
            Nds ndsFile = new Nds();

            using (FileStream ndsStream = File.OpenRead(pPath))
            {
                ndsFile.Initialize(ndsStream, pPath);
            }

            //------------------------
            // extract 2SFs and STRMs
            //------------------------
            bool filesWereRipped = XsfUtil.NdsTo2sf(pPath, TESTPACK_FULL_PATH, workingStruct.UseSmapNames);

            // output info
            this.progressStruct.Clear();

            if (!filesWereRipped)
            {
                this.progressStruct.GenericMessage = String.Format("  No extractable SSEQ/STRM/SWAR data found.{0}", Environment.NewLine);
            }
            else
            {
                this.progressStruct.GenericMessage = String.Format("  SSEQ/STRM/SWAR Data extracted.{0}", Environment.NewLine);
            }

            this.ReportProgress(this.progress, this.progressStruct);

            //---------------
            // extract SWAVs
            //---------------
            this.progressStruct.Clear();
            this.progressStruct.GenericMessage = String.Format("  Searching for SWAVs{0}", Environment.NewLine);
            this.ReportProgress(this.progress, this.progressStruct);

            string swavMessages = String.Empty;

            FindOffsetStruct findOffsetStruct = new FindOffsetStruct();

            findOffsetStruct.CutFile                = true;
            findOffsetStruct.SearchString           = "53574156FFFE";
            findOffsetStruct.TreatSearchStringAsHex = true;
            findOffsetStruct.SearchStringOffset     = "0";
            findOffsetStruct.OutputFileExtension    = ".swav";
            findOffsetStruct.IsCutSizeAnOffset      = true;
            findOffsetStruct.CutSize                = "8";
            findOffsetStruct.CutSizeOffsetSize      = "4";
            findOffsetStruct.IsLittleEndian         = true;
            findOffsetStruct.OutputFolder           = Path.Combine(ndsTo2sfOutputPath, "SWAV");

            ParseFile.FindOffsetAndCutFile(pPath, findOffsetStruct, out swavMessages, false, false);

            //--------------
            // extract ADXs
            //--------------
            this.progressStruct.Clear();
            this.progressStruct.GenericMessage = String.Format("  Searching for ADXs{0}", Environment.NewLine);
            this.ReportProgress(this.progress, this.progressStruct);

            string adxMessages = String.Empty;

            CriUtil.ExtractAdxStreams(pPath, ndsFile.GameSerial, Path.Combine(ndsTo2sfOutputPath, "ADX"), out adxMessages);
        }
Exemple #13
0
        protected override void DoTaskForFile(string pPath,
                                              IVgmtWorkerStruct pExamineChecksumGeneratorStruct, DoWorkEventArgs e)
        {
            ExamineChecksumGeneratorStruct examineChecksumGeneratorStruct =
                (ExamineChecksumGeneratorStruct)pExamineChecksumGeneratorStruct;

            string crc32;
            string md5;
            string sha1;

            string vgmtCrc32 = "Not implemented for this format.";
            string vgmtMd5   = "Not implemented for this format.";
            string vgmtSha1  = "Not implemented for this format.";

            string checksumKey;

            Type    formatType = null;
            IFormat vgmData    = null;

            using (FileStream fs = File.OpenRead(pPath))
            {
                crc32 = ChecksumUtil.GetCrc32OfFullFile(fs);
                md5   = ChecksumUtil.GetMd5OfFullFile(fs);
                sha1  = ChecksumUtil.GetSha1OfFullFile(fs);

                if (examineChecksumGeneratorStruct.CheckForDuplicates)
                {
                    checksumKey = String.Format("{0}/{1}/{2}", crc32, md5, sha1);
                    this.addChecksumToHash(checksumKey, pPath, true);
                }

                if (examineChecksumGeneratorStruct.DoVgmtChecksums)
                {
                    formatType = FormatUtil.getObjectType(fs);
                    if (formatType != null)
                    {
                        vgmData = (IFormat)Activator.CreateInstance(formatType);
                        vgmData.Initialize(fs, pPath);
                    }
                }
            }

            if (vgmData != null)
            {
                Crc32 crc32Generator = new Crc32();

                MD5CryptoServiceProvider md5Hash = new MD5CryptoServiceProvider();
                FileStream   md5FileStream       = new FileStream(Path.GetTempFileName(), FileMode.Create, FileAccess.Write);
                CryptoStream md5CryptoStream     = new CryptoStream(md5FileStream, md5Hash, CryptoStreamMode.Write);

                SHA1CryptoServiceProvider sha1Hash = new SHA1CryptoServiceProvider();
                FileStream   sha1FileStream        = new FileStream(Path.GetTempFileName(), FileMode.Create, FileAccess.Write);
                CryptoStream sha1CryptoStream      = new CryptoStream(sha1FileStream, sha1Hash, CryptoStreamMode.Write);

                vgmData.GetDatFileChecksums(ref crc32Generator, ref md5CryptoStream,
                                            ref sha1CryptoStream);
                md5CryptoStream.FlushFinalBlock();
                sha1CryptoStream.FlushFinalBlock();

                vgmtCrc32 = crc32Generator.Value.ToString("X8");
                vgmtMd5   = ParseFile.ByteArrayToString(md5Hash.Hash);
                vgmtSha1  = ParseFile.ByteArrayToString(sha1Hash.Hash);

                if (examineChecksumGeneratorStruct.CheckForDuplicates)
                {
                    checksumKey = String.Format("{0}/{1}/{2}", vgmtCrc32, vgmtMd5, vgmtSha1);
                    this.addChecksumToHash(checksumKey, pPath, false);
                }

                md5FileStream.Close();
                md5FileStream.Dispose();
                sha1FileStream.Close();
                sha1FileStream.Dispose();

                md5CryptoStream.Close();
                md5CryptoStream.Dispose();
                sha1CryptoStream.Close();
                sha1CryptoStream.Dispose();
            }

            this.outputBuffer.AppendFormat("<{0}>{1}", pPath, Environment.NewLine);
            this.outputBuffer.AppendFormat("CRC32: {0}{1}", crc32, Environment.NewLine);
            this.outputBuffer.AppendFormat("MD5: {0}{1}", md5, Environment.NewLine);
            this.outputBuffer.AppendFormat("SHA1: {0}{1}", sha1, Environment.NewLine);

            if (examineChecksumGeneratorStruct.DoVgmtChecksums)
            {
                this.outputBuffer.AppendFormat("CRC32 (VGMT): {0}{1}", vgmtCrc32, Environment.NewLine);
                this.outputBuffer.AppendFormat("MD5 (VGMT): {0}{1}", vgmtMd5, Environment.NewLine);
                this.outputBuffer.AppendFormat("SHA1 (VGMT): {0}{1}", vgmtSha1, Environment.NewLine);
            }
            this.outputBuffer.AppendLine();
        }
Exemple #14
0
        protected override void DoTaskForDirectory(string pPath, IVgmtWorkerStruct pTaskStruct,
                                                   DoWorkEventArgs e)
        {
            XsfRecompressDataStruct xsfRecompressDataStruct = (XsfRecompressDataStruct)pTaskStruct;

            foreach (string d in Directory.GetDirectories(pPath))
            {
                if (!CancellationPending)
                {
                    this.DoTaskForDirectory(d, pTaskStruct, e);
                }
                else
                {
                    e.Cancel = true;
                    break;
                }
            }
            foreach (string f in Directory.GetFiles(pPath))
            {
                if (!CancellationPending)
                {
                    // Report Progress
                    base.progress = (++base.fileCount * 100) / base.maxFiles;
                    base.progressStruct.Clear();
                    base.progressStruct.FileName = f;
                    ReportProgress(base.progress, base.progressStruct);

                    // perform task
                    try
                    {
                        this.DoTaskForFile(f, pTaskStruct, e);
                    }
                    catch (Exception ex)
                    {
                        base.progressStruct.Clear();
                        base.progressStruct.ErrorMessage =
                            String.Format(CultureInfo.CurrentCulture, "Error processing <{0}>.  Error received: ", f) + ex.Message + Environment.NewLine;
                        ReportProgress(base.progress, base.progressStruct);
                    }
                    finally
                    {
                        this.DoFinally();
                    }
                }
                else
                {
                    e.Cancel = true;
                    break;
                }
            }

            // recompress folder if requested
            if (xsfRecompressDataStruct.RecompressFolders &&
                (((IList <string>)xsfRecompressDataStruct.SourcePaths)).Contains(pPath))
            {
                string recompressedFolder = Path.Combine(pPath, XsfUtil.RecompressedSubfolderName);

                if (Directory.Exists(recompressedFolder))
                {
                    string[] sourceFiles = Directory.GetFiles(pPath, "*.*", SearchOption.TopDirectoryOnly);
                    string   recompressedFolderFileName;

                    foreach (string f in sourceFiles)
                    {
                        recompressedFolderFileName = Path.Combine(recompressedFolder, Path.GetFileName(f));

                        if (!File.Exists(recompressedFolderFileName))
                        {
                            File.Copy(f, recompressedFolderFileName);
                        }
                    }

                    string destinationArchive = Path.Combine(pPath, String.Format("{0}.{1}", Path.GetFileName(pPath), "7z"));

                    if (File.Exists(destinationArchive))
                    {
                        destinationArchive = Path.Combine(pPath, String.Format("{0}_{1}.{2}", Path.GetFileName(pPath), new Random().Next(0xFF).ToString("X2"), "7z"));
                    }

                    CompressionUtil.CompressFolderWith7zip(recompressedFolder, destinationArchive);
                }
            }
        }