From 7aa85c437b4b83e091c73a7e40bb2eb186276367 Mon Sep 17 00:00:00 2001 From: Chev <11602755+chev2@users.noreply.github.com> Date: Sat, 16 Jan 2021 22:51:04 -0800 Subject: [PATCH] Change Yaz0 method ReadFile to FromBytes --- botw-toolset/IO/Yaz0/Yaz0.cs | 39 +++++++++++++++--------------------- 1 file changed, 16 insertions(+), 23 deletions(-) diff --git a/botw-toolset/IO/Yaz0/Yaz0.cs b/botw-toolset/IO/Yaz0/Yaz0.cs index 092a6ef..84f42aa 100644 --- a/botw-toolset/IO/Yaz0/Yaz0.cs +++ b/botw-toolset/IO/Yaz0/Yaz0.cs @@ -32,35 +32,28 @@ namespace BOTWToolset.IO.Yaz0 /// /// The file (full path) to read. /// containing the file's data. - public static Yaz0 ReadFile(string file) + public static Yaz0 FromBytes(byte[] bytes) { - if (File.Exists(file)) + Yaz0 y = new Yaz0(); + + // Use big-endian + using (var r = new BinaryReaderBig(new MemoryStream(bytes))) { - Yaz0 y = new Yaz0(); + y.Magic = new string(r.ReadChars(4)); + if (y.Magic != "Yaz0") + throw new InvalidMagicException("This file is not Yaz0-encoded."); - // Use big-endian - using (var r = new BinaryReaderBig(File.Open(file, FileMode.Open))) - { - y.Magic = new string(r.ReadChars(4)); - if (y.Magic != "Yaz0") - throw new InvalidMagicException("This file is not Yaz0-encoded."); + y.UncompressedDataSize = r.ReadUInt32(); + y.DataAlignment = r.ReadUInt32(); - y.UncompressedDataSize = r.ReadUInt32(); - y.DataAlignment = r.ReadUInt32(); + // Seek back to beginning of file to capture all bytes + r.BaseStream.Seek(0, SeekOrigin.Begin); - // Seek back to beginning of file to capture all bytes - r.BaseStream.Seek(0, SeekOrigin.Begin); - - // Capture all bytes - y.Bytes = r.ReadBytes((int)r.BaseStream.Length); - } - - return y; - } - else - { - throw new FileNotFoundException("Cannot find Yaz0 file to read."); + // Capture all bytes + y.Bytes = r.ReadBytes((int)r.BaseStream.Length); } + + return y; } ///