Exemple #1
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);
        }
Exemple #2
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);
 }
 public ArcFileWrapper(string archiveFile)
 {
     _psarc       = new PSARC();
     _archiveFile = archiveFile;
     using (var inputStream = System.IO.File.OpenRead(archiveFile))
     {
         _psarc.Read(inputStream);
     }
 }
        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";
        }