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); } }
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); } }