Write() public method

public Write ( Stream inputStream, bool encrypt = false, bool seek = true ) : void
inputStream Stream
encrypt bool
seek bool
return void
        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);
                    psarcStream.Dispose(); // CRITICAL

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

                    if (tocEntry == null)
                    {
                        archive.Dispose(); // CRITICAL
                        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);

                    archive.Dispose(); // CRITICAL
                    return(true);
                }
        }
        public static bool InjectArchiveEntry(string psarcPath, string entryName, string sourcePath)
        {
            if (!File.Exists(psarcPath))
            {
                return(false);
            }

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

                        var entryStream = new MemoryStream();

                        using (var sourceStream = File.OpenRead(sourcePath))
                            sourceStream.CopyTo(entryStream);

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

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

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

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

                    archive.Dispose(); // CRITICAL
                    return(true);
                }
        }
Ejemplo n.º 3
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);
                }
        }