Example #1
0
 public LazyPsarcLoader(string fileName, bool useMemory = true, bool lazy = true)
 {
     _filePath   = fileName;
     _archive    = new PSARC(true);
     _fileStream = File.OpenRead(_filePath);
     _archive.Read(_fileStream, lazy);
 }
Example #2
0
        public LazyPsarcLoader(FileInfo fileInfo, bool useMemory = true, bool lazy = true)
        {
            _filePath = fileInfo.FullName;
            _archive  = new PSARC(useMemory);

            //Try to open the file over 10 seconds
            int tries = 0;

            while (_fileStream == null)
            {
                try
                {
                    _fileStream = fileInfo.OpenRead();
                }
                catch (Exception e)
                {
                    //Throw the exception if we tried 10 times
                    if (tries > 10)
                    {
                        throw e;
                    }

                    Thread.Sleep(1000);
                }
                tries++;
            }

            _archive.Read(_fileStream, lazy);
        }
Example #3
0
        /// <summary>
        /// Decompress a .Pak file
        /// </summary>
        /// <param name="p_strFileName">The file's name</param>
        private void DecompressPAKFile(string p_strFileName)
        {
            //Throw exection if the file isn't a .pak
            if (!p_strFileName.Contains(".pak"))
            {
                throw new Exception(string.Format("The file {0} isn't a .pak file", p_strFileName));
            }

            using (PSARC psaDecompresser = new PSARC(p_strFileName))
            {
                psaDecompresser.ReadManifest();

                if (psaDecompresser.TOC.Count != psaDecompresser.m_hdrPSHeader.TocEntries)
                {
                    throw new Exception("TOC Count does not meet expected value");
                }

                foreach (TOCEntry tocEntry in psaDecompresser.TOC)
                {
                    //Skip the manifest files
                    if (tocEntry.FileName.Contains("manifest"))
                    {
                        continue;
                    }
                    WriteFile(psaDecompresser.DecompressFile(tocEntry.FileName));
                }
            }
            Console.WriteLine(string.Format(" Files: {0} is successfully uncompress", p_strFileName));
        }
Example #4
0
 public ArcFileWrapper(string archiveFile)
 {
     _psarc       = new PSARC();
     _archiveFile = archiveFile;
     using (var inputStream = System.IO.File.OpenRead(archiveFile))
     {
         _psarc.Read(inputStream);
     }
 }
Example #5
0
        void Run(string[] args)
        {
            UnpackMultipleFiles(m_strTestDirectories);

            //Test reading archive from disk directly via ReadManifest
            pSarc = new PSARC();
            pSarc.ReadManifest(fileName);
            if (pSarc.TOC.Count != pSarc.m_hdrPSHeader.TocEntries)
            {
                throw new Exception("TOC Count does not meet expected value");
            }

            // Test reading archive from disk directly via constructor
            pSarc = new PSARC(fileName);
            pSarc.ReadManifest();
            if (pSarc.TOC.Count != pSarc.m_hdrPSHeader.TocEntries)
            {
                throw new Exception("TOC Count does not meet expected value");
            }

            // Test reading archive from FileStream
            pSarc = new PSARC(new FileStream(fileName, FileMode.Open, FileAccess.Read));
            pSarc.ReadManifest();
            if (pSarc.TOC.Count != pSarc.m_hdrPSHeader.TocEntries)
            {
                throw new Exception("TOC Count does not meet expected value");
            }

            int index    = 0;
            var manifest = new List <string>();

            foreach (TOCEntry entry in pSarc.TOC)
            {
                string msg = string.Format("Index: {0}, Name: {1}, \toriginalSize: {2}, \tstartOffset: {3}, \tblockListStart: {4}, \tblocks: {5}",
                                           index,
                                           entry.FileName,
                                           entry.OriginalSize,
                                           entry.StartOffset,
                                           entry.BlockListStart,
                                           Math.Ceiling(entry.OriginalSize / (double)pSarc.m_hdrPSHeader.BlockSize)
                                           );
                manifest.Add(msg);
                Console.WriteLine(msg);

                index++;
            }
            File.WriteAllLines(m_strTestDirectories + @"\manifestdetails.txt", manifest);
            Console.Write("Press any key to continue ...");
            Console.ReadKey();

            TestDecompression();
            TestCompression();
        }
Example #6
0
        public void Dispose()
        {
            if (_fileStream != null)
            {
                _fileStream.Dispose();
                _fileStream = null;
            }
            if (_archive != null)
            {
                _archive.Dispose();
                _archive = null;
            }

            GC.SuppressFinalize(this);
        }
Example #7
0
        public void SaveAllToPak(string pakPath)
        {
            foreach (var lazy in _lazyMBINs)
            {
                var mem = this[lazy.Key];
            }

            PSARC pak = new PSARC();

            foreach (var mbin in _loadedMBINs)
            {
                mbin.Value.mbin.Save();
                var bytes = mbin.Value.mem.GetBuffer();
                pak.CompressFile(mbin.Key, bytes);
            }

            // TODO: Fix PSArcHandler to handle creating brand new PAK files
        }
Example #8
0
        public void LoadMBINsFromPak(string pakPath)
        {
            var pak = new PSARC(pakPath);

            _paks.Add(pak);
            foreach (var entry in pak.TOC)
            {
                if (Path.GetExtension(entry.FileName)?.ToLower() == ".mbin")
                {
                    _lazyMBINs[entry.FileName] = delegate
                    {
                        var decompressed = pak.DecompressFile(entry.FileName);
                        var stream       = new MemoryStream(decompressed.BinaryFile);
                        return(new MBINMemory
                        {
                            mbin = new MBINFile(stream),
                            mem = stream
                        });
                    }
                }
                ;
            }
        }
        public void UpdateAppId(object sender, DoWorkEventArgs e)
        {
            var srcFilePaths = e.Argument as string[];

            errorsFound = new StringBuilder();
            var step     = (int)Math.Round(1.0 / srcFilePaths.Length * 100, 0);
            int progress = 0;

            // show some initial progress if only one song
            if (step > 99)
            {
                progress = 50;
            }

            var tmpDir = Directory.CreateDirectory(Path.Combine(Path.GetTempPath(), Path.GetRandomFileName())).FullName;
            var appId  = NewAppId;

            if (String.IsNullOrEmpty(appId))
            {
                throw new InvalidDataException("<ERROR> AppID is null or empty ...");
            }

            foreach (string srcFilePath in srcFilePaths)
            {
                Application.DoEvents();
                var srcPlatform = srcFilePath.GetPlatform();
                bwRepack.ReportProgress(progress, String.Format("Updating '{0}'", Path.GetFileName(srcFilePath)));

                if (!srcPlatform.IsConsole)
                {
                    NoCloseStream dataStream = new NoCloseStream();
                    try
                    {
                        // use fast PsarcLoader memory methods (respect processing order/grouping)
                        using (PSARC p = new PSARC(true))
                        {
                            // write the new appid.appid
                            using (var fs = File.OpenRead(srcFilePath))
                                p.Read(fs);

                            dataStream = p.ReplaceData(x => x.Name.Equals("appid.appid"), appId);

                            using (var fs = File.Create(srcFilePath))
                                p.Write(fs, true);

                            // update toolkit.version
                            var tkStream = p.GetData(x => x.Name.Equals("toolkit.version"));
                            if (tkStream != null)
                            {
                                using (var tkReader = new StreamReader(tkStream))
                                {
                                    var tkInfo         = GeneralExtension.GetToolkitInfo(tkReader);
                                    var packageComment = tkInfo.PackageComment;
                                    if (String.IsNullOrEmpty(packageComment))
                                    {
                                        packageComment = TKI_APPID;
                                    }
                                    else if (!packageComment.Contains(TKI_APPID))
                                    {
                                        packageComment = packageComment + " " + TKI_APPID;
                                    }

                                    var toolkitVersion = ToolkitVersion.RSTKGuiVersion;
                                    if (!tkInfo.ToolkitVersion.Contains(toolkitVersion))
                                    {
                                        toolkitVersion = String.Format("{0} ({1})", toolkitVersion, tkInfo.ToolkitVersion);
                                    }

                                    using (var tkInfoStream = new MemoryStream())
                                    {
                                        PackageCreator.GenerateToolkitVersion(tkInfoStream, tkInfo.PackageAuthor, tkInfo.PackageVersion, packageComment, tkInfo.PackageRating, toolkitVersion);
                                        PsarcExtensions.InjectArchiveEntry(srcFilePath, "toolkit.version", tkInfoStream);
                                        tkInfoStream.Dispose(); // CRITICAL
                                    }
                                }
                            }
                        }
                    }
                    catch (Exception ex)
                    {
                        errorsFound.AppendLine(String.Format("Error trying repack file '{0}': {1}", Path.GetFileName(srcFilePath), ex.Message));
                    }

                    if (dataStream != null)
                    {
                        dataStream.CloseEx();
                    }

                    progress += step;
                    bwRepack.ReportProgress(progress);
                }
                else
                {
                    errorsFound.AppendLine(String.Format("File '{0}' is not a valid desktop platform package.", Path.GetFileName(srcFilePath)));
                }
            }

            bwRepack.ReportProgress(100);
            e.Result = "repack";
        }