From 31c7f59e86413c20f493d1cd171435f8e2f3c47c Mon Sep 17 00:00:00 2001 From: CasualPokePlayer <50538166+CasualPokePlayer@users.noreply.github.com> Date: Mon, 15 Aug 2022 10:59:27 -0700 Subject: [PATCH] fix some edge cases with new zip compression --- .../movie/bk2/Bk2Movie.IO.cs | 14 ++++++-------- .../movie/tasproj/TasBranch.cs | 6 +++--- .../movie/tasproj/TasMovie.IO.cs | 2 +- .../savestates/ZipStateLoader.cs | 13 ------------- src/BizHawk.Common/Extensions/IOExtensions.cs | 16 ++++++++++++++++ 5 files changed, 26 insertions(+), 25 deletions(-) diff --git a/src/BizHawk.Client.Common/movie/bk2/Bk2Movie.IO.cs b/src/BizHawk.Client.Common/movie/bk2/Bk2Movie.IO.cs index 18dd79c5a6..dbcbc26ae1 100644 --- a/src/BizHawk.Client.Common/movie/bk2/Bk2Movie.IO.cs +++ b/src/BizHawk.Client.Common/movie/bk2/Bk2Movie.IO.cs @@ -229,22 +229,20 @@ namespace BizHawk.Client.Common if (StartsFromSavestate) { bl.GetCoreState( - (br, length) => BinarySavestate = br.ReadBytes((int) length), + br => BinarySavestate = br.ReadAllBytes(), tr => TextSavestate = tr.ReadToEnd()); bl.GetLump(BinaryStateLump.Framebuffer, false, - (br, length) => + br => { - SavestateFramebuffer = new int[length / sizeof(int)]; - for (int i = 0; i < SavestateFramebuffer.Length; i++) - { - SavestateFramebuffer[i] = br.ReadInt32(); - } + var fb = br.ReadAllBytes(); + SavestateFramebuffer = new int[fb.Length / sizeof(int)]; + Buffer.BlockCopy(fb, 0, SavestateFramebuffer, 0, fb.Length); }); } else if (StartsFromSaveRam) { bl.GetLump(BinaryStateLump.MovieSaveRam, false, - (br, length) => SaveRam = br.ReadBytes((int) length)); + br => SaveRam = br.ReadAllBytes()); } } } diff --git a/src/BizHawk.Client.Common/movie/tasproj/TasBranch.cs b/src/BizHawk.Client.Common/movie/tasproj/TasBranch.cs index 45f101a2de..ddc5d3f6ed 100644 --- a/src/BizHawk.Client.Common/movie/tasproj/TasBranch.cs +++ b/src/BizHawk.Client.Common/movie/tasproj/TasBranch.cs @@ -4,6 +4,7 @@ using System.IO; using System.Linq; using Newtonsoft.Json; using BizHawk.Bizware.BizwareGL; +using BizHawk.Common.IOExtensions; namespace BizHawk.Client.Common { @@ -223,10 +224,9 @@ namespace BizHawk.Client.Common return; } - bl.GetLump(ncore, abort: true, (Stream s, long length) => + bl.GetLump(ncore, abort: true, (s, _) => { - b.CoreData = new byte[length]; - s.Read(b.CoreData, 0, b.CoreData.Length); + b.CoreData = s.ReadAllBytes(); }); bl.GetLump(ninput, abort: true, tr => diff --git a/src/BizHawk.Client.Common/movie/tasproj/TasMovie.IO.cs b/src/BizHawk.Client.Common/movie/tasproj/TasMovie.IO.cs index 744d4d1fbe..ef3e4e7d1e 100644 --- a/src/BizHawk.Client.Common/movie/tasproj/TasMovie.IO.cs +++ b/src/BizHawk.Client.Common/movie/tasproj/TasMovie.IO.cs @@ -163,7 +163,7 @@ namespace BizHawk.Client.Common } }); - bl.GetLump(BinaryStateLump.StateHistory, abort: false, (br, _) => + bl.GetLump(BinaryStateLump.StateHistory, abort: false, br => { try { diff --git a/src/BizHawk.Client.Common/savestates/ZipStateLoader.cs b/src/BizHawk.Client.Common/savestates/ZipStateLoader.cs index a522abda4f..74848e3adf 100644 --- a/src/BizHawk.Client.Common/savestates/ZipStateLoader.cs +++ b/src/BizHawk.Client.Common/savestates/ZipStateLoader.cs @@ -149,22 +149,9 @@ namespace BizHawk.Client.Common public bool GetLump(BinaryStateLump lump, bool abort, Action callback) => GetLump(lump, abort, (s, _) => callback(new(s))); - public bool GetLump(BinaryStateLump lump, bool abort, Action callback) - => GetLump(lump, abort, (s, length) => callback(new(s), length)); - public bool GetLump(BinaryStateLump lump, bool abort, Action callback) => GetLump(lump, abort, (s, _) => callback(new StreamReader(s)), false); - /// couldn't find Binary or Text savestate - public void GetCoreState(Action callbackBinary, Action callbackText) - { - if (!GetLump(BinaryStateLump.Corestate, false, callbackBinary) - && !GetLump(BinaryStateLump.CorestateText, false, callbackText)) - { - throw new Exception("Couldn't find Binary or Text savestate"); - } - } - /// couldn't find Binary or Text savestate public void GetCoreState(Action callbackBinary, Action callbackText) { diff --git a/src/BizHawk.Common/Extensions/IOExtensions.cs b/src/BizHawk.Common/Extensions/IOExtensions.cs index e066409bbc..cf4c705418 100644 --- a/src/BizHawk.Common/Extensions/IOExtensions.cs +++ b/src/BizHawk.Common/Extensions/IOExtensions.cs @@ -26,6 +26,22 @@ namespace BizHawk.Common.IOExtensions return outStream.ToArray(); } + public static byte[] ReadAllBytes(this BinaryReader br) + { + const int BUFF_SIZE = 4096; + var buffer = new byte[BUFF_SIZE]; + + int bytesRead; + var outStream = new MemoryStream(); + + while ((bytesRead = br.Read(buffer, 0, BUFF_SIZE)) > 0) + { + outStream.Write(buffer, 0, bytesRead); + } + + return outStream.ToArray(); + } + /// /// Read a string from a binary reader using utf8 encoding and known byte length ///