From 9d46a11796df5ccd1cf53d2ceaa8c8fe8c58cb14 Mon Sep 17 00:00:00 2001 From: turtle-insect Date: Mon, 18 Nov 2024 07:13:10 +0900 Subject: [PATCH] refactor --- LINKDATA/ViewModel.cs | 58 +++++++++++++++++-------------------------- 1 file changed, 23 insertions(+), 35 deletions(-) diff --git a/LINKDATA/ViewModel.cs b/LINKDATA/ViewModel.cs index a78650a..b12cb0e 100644 --- a/LINKDATA/ViewModel.cs +++ b/LINKDATA/ViewModel.cs @@ -363,36 +363,28 @@ private void PackIDXzrc(object? param) Byte[] Buffer = System.IO.File.ReadAllBytes(path); Int32 packCount = (Buffer.Length + PackSplitSize - 1) / PackSplitSize; - List Packed = new List(); + using var ms = new MemoryStream(); // split size. - foreach (Byte b in BitConverter.GetBytes(PackSplitSize)) - { - Packed.Add(b); - } + ms.Write(BitConverter.GetBytes(PackSplitSize), 0, 4); // split count. - foreach (Byte b in BitConverter.GetBytes(packCount)) - { - Packed.Add(b); - } + ms.Write(BitConverter.GetBytes(packCount), 0, 4); // unpack file size. - foreach (Byte b in BitConverter.GetBytes(Buffer.Length)) - { - Packed.Add(b); - } + ms.Write(BitConverter.GetBytes(Buffer.Length), 0, 4); // chunk size. // reserve. + // overwrite when writing data for (int index = 0; index < packCount * 4; index++) { - Packed.Add(0); + ms.WriteByte(0); } // padding. - int count = 0x80 - (Packed.Count % 0x80); + int count = 0x80 - ((int)ms.Length % 0x80); if(count == 0x80)count = 0; for (int index = 0; index < count; index++) { - Packed.Add(0); + ms.WriteByte(0); } // data @@ -406,26 +398,22 @@ private void PackIDXzrc(object? param) int index = 0; int size = tmp.Length; - foreach (Byte b in BitConverter.GetBytes(size + 4)) - { - Packed[0x0C + pack * 4 + index] = b; - index++; - } - foreach (Byte b in BitConverter.GetBytes(size)) - { - Packed.Add(b); - } - foreach (Byte b in tmp) - { - Packed.Add(b); - } + + // rewrite chunk size + ms.Seek(0x0C + pack * 4, SeekOrigin.Begin); + ms.Write(BitConverter.GetBytes(size + 4), 0, 4); + // chunk data + // size & bytes + ms.Seek(0, SeekOrigin.End); + ms.Write(BitConverter.GetBytes(size), 0, 4); + ms.Write(tmp, 0, tmp.Length); // padding. - count = 0x80 - (Packed.Count % 0x80); + count = 0x80 - ((int)ms.Length % 0x80); if (count == 0x80) count = 0; for (index = 0; index < count; index++) { - Packed.Add(0); + ms.WriteByte(0); } } @@ -434,17 +422,17 @@ private void PackIDXzrc(object? param) dlg.InitialDirectory = WorkPath; dlg.FileName = System.IO.Path.GetFileNameWithoutExtension(System.IO.Path.GetFileName(path)); if (dlg.ShowDialog() == false) return; - System.IO.File.WriteAllBytes(dlg.FileName, Packed.ToArray()); + System.IO.File.WriteAllBytes(dlg.FileName, ms.ToArray()); } private Byte[] Comp(Byte[] data) { - Byte[] result = null; + Byte[] result = []; using (var input = new MemoryStream(data)) { using (var output = new MemoryStream()) { - using (var zlib = new System.IO.Compression.ZLibStream(output, System.IO.Compression.CompressionLevel.SmallestSize)) + using (var zlib = new System.IO.Compression.ZLibStream(output, System.IO.Compression.CompressionLevel.Fastest)) { input.CopyTo(zlib); } @@ -456,7 +444,7 @@ private Byte[] Comp(Byte[] data) private Byte[] Decomp(Byte[] data) { - Byte[] result = null; + Byte[] result = []; using (var input = new MemoryStream(data)) { using (var zlib = new System.IO.Compression.ZLibStream(input, System.IO.Compression.CompressionMode.Decompress))