示例#1
0
        public static bool RemoveArchiveEntry(string psarcPath, string entryName)
        {
            if (!File.Exists(psarcPath))
            {
                return(false);
            }

            using (PSARC archive = new PSARC(true))
            {
                using (var psarcStream = File.OpenRead(psarcPath))
                    archive.Read(psarcStream);

                var tocEntry = archive.TOC.FirstOrDefault(entry => entry.Name == entryName);

                if (tocEntry == null)
                {
                    return(true);
                }

                archive.TOC.Remove(tocEntry);
                archive.TOC.Insert(0, new Entry()
                {
                    Name = "NamesBlock.bin"
                });

                using (var fs = File.Create(psarcPath))
                    archive.Write(fs, true);

                return(true);
            }
        }
示例#2
0
        public static bool InjectArchiveEntry(string psarcPath, string entryName,
                                              string sourcePath, bool updateToolkitVersion = false,
                                              string packageAuthor = "", string packageVersion = "1", string packageComment = "")
        {
            if (!File.Exists(psarcPath))
            {
                return(false);
            }

            int injectionCount = 2;

            if (!updateToolkitVersion)
            {
                injectionCount = 1;
            }
            else
            {
                if (String.IsNullOrEmpty(packageVersion))
                {
                    packageVersion = "1";
                }
                if (String.IsNullOrEmpty(packageAuthor))
                {
                    packageAuthor = Assembly.GetExecutingAssembly().GetName().ToString();
                }
            }

            using (PSARC archive = new PSARC(true))
                using (var psarcStream = File.OpenRead(psarcPath))
                {
                    try
                    {
                        archive.Read(psarcStream);
                        psarcStream.Dispose();

                        for (int i = 0; i < injectionCount; i++)
                        {
                            var entryStream = new MemoryStream();

                            switch (i)
                            {
                            case 0:
                                using (var sourceStream = File.OpenRead(sourcePath))
                                    sourceStream.CopyTo(entryStream);
                                break;

                            case 1:
                                DLCPackageCreator.GenerateToolkitVersion(entryStream, packageAuthor, packageVersion, packageComment);
                                entryName = "toolkit.version";
                                break;
                            }

                            entryStream.Position = 0;
                            Entry tocEntry = archive.TOC.FirstOrDefault(x => x.Name == entryName);

                            if (tocEntry != null)
                            {
                                tocEntry.Data.Dispose();
                                tocEntry.Data = null;
                                tocEntry.Data = entryStream;
                            }
                            else
                            {
                                archive.AddEntry(entryName, entryStream);

                                // evil genius ... ;) => forces archive update
                                archive.TOC.Insert(0, new Entry()
                                {
                                    Name = "NamesBlock.bin"
                                });
                            }
                        }
                    }
                    catch
                    {
                        return(false);
                    }

                    using (var fs = File.Create(psarcPath))
                        archive.Write(fs, true);

                    return(true);
                }
        }