From f1439db314804721df467c69dab3233d4b5bfaa0 Mon Sep 17 00:00:00 2001 From: KillzXGaming Date: Mon, 5 Aug 2019 17:04:24 -0400 Subject: [PATCH] Save compressed bytes properly and save data within archives for pak --- .vs/Toolbox/v15/.suo | Bin 914944 -> 914944 bytes .../FileFormats/CrashBandicoot/IGA_PAK.cs | 59 +++++++++++++++++- 2 files changed, 58 insertions(+), 1 deletion(-) diff --git a/.vs/Toolbox/v15/.suo b/.vs/Toolbox/v15/.suo index 239c4fb331d0a6239441c408afbe7f7ac5ab6d9f..bf8c77206fb97dbcb6944cbce41cf1572448fef2 100644 GIT binary patch delta 137 zcmZp8W8Uz_d_xWsE5il$9~U+kGW}*|6-Yhy<3e*GTYDiJBM>tIF*6XeY%gSEJ=iPw zg#iXc4*Uf&wWn`rV%t1j{yisG+f>$VQ`wA`;#FhK-e%3wX3g1V&DCbj-Db_R%$j#M E0JY#N*Z=?k delta 137 zcmZp8W8Uz_d_xWstAKpkq;s1KnSL{~Is{F>f3CTZt-X+q5r~<9m>Gy!wimLo9_$q? zVt@gW1C2nY()0~YY@4Uczvtv?o65RvDx1+#!NV{m3?c`TfNWtTHP-BH)*NltoNd-z PZPwgv);!Csd3OT<+chZ` diff --git a/File_Format_Library/FileFormats/CrashBandicoot/IGA_PAK.cs b/File_Format_Library/FileFormats/CrashBandicoot/IGA_PAK.cs index 9e9c91dd..28e0352f 100644 --- a/File_Format_Library/FileFormats/CrashBandicoot/IGA_PAK.cs +++ b/File_Format_Library/FileFormats/CrashBandicoot/IGA_PAK.cs @@ -121,6 +121,10 @@ namespace FirstPlugin public void Write(FileWriter writer) { + //Save all the data for opened files first and compress if needed + for (int i = 0; i < files.Count; i++) + files[i].SaveOpenedFile(); + writer.WriteSignature("IGA\x01"); writer.Write(Version); //total size of file info and hash section @@ -157,7 +161,7 @@ namespace FirstPlugin writer.Write((uint)dataPos); } - writer.Write(files[i].FileData); + writer.Write(files[i].CompressedBytes); writer.Align(2048); } @@ -234,9 +238,40 @@ namespace FirstPlugin Version = version; } + private byte[] _savedBytes { get; set; } + public uint FileOffset; public uint DecompressedFileSize; + public byte[] CompressedBytes + { + get + { + if (FileOffset == 0) + return new byte[0]; + + if (_savedBytes != null) + return _savedBytes; + + using (var reader = new FileReader(SourceFile)) + { + uint size = 0; + reader.BaseStream.Seek(FileOffset, SeekOrigin.Begin); + if (FileCompressionType != -1) + { + if (Version <= 0x0B) + size = reader.ReadUInt16(); + else + size = reader.ReadUInt32(); + } + else + size = DecompressedFileSize; + + return reader.ReadBytes((int)size); + } + } + } + public override byte[] FileData { get @@ -285,6 +320,28 @@ namespace FirstPlugin } } + public void SaveOpenedFile() + { + if (FileFormat != null) + { + byte[] data = FileFormat.Save(); + DecompressedFileSize = (uint)data.Length; + + if (FileCompressionType != -1) + { + SevenZip.Compression.LZMA.Encoder encode = new SevenZip.Compression.LZMA.Encoder(); + MemoryStream ms = new MemoryStream(data); + MemoryStream otuput = new MemoryStream(data); + encode.Code(ms, otuput, -1, -1, null); + _savedBytes = otuput.ToArray(); + } + else + _savedBytes = data; + + GC.Collect(); + } + } + public void Read(FileReader reader) { FileOffset = reader.ReadUInt32();