From e7f411e2d42d9f8d8b5a9f3b1f543b878eb5ad87 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andrei=20M=C3=BCller=20=28Andy=20Miira=29?= Date: Sun, 26 Jul 2026 00:07:56 -0300 Subject: [PATCH 01/15] [Leaf] ArcPAK.cs: Move Leaf LZSS compression logic to a separate class (LeafCompression.cs) --- ArcFormats/ArcFormats.csproj | 1 + ArcFormats/Leaf/ArcPAK.cs | 164 ------------------------ ArcFormats/Leaf/LeafCompression.cs | 193 +++++++++++++++++++++++++++++ 3 files changed, 194 insertions(+), 164 deletions(-) create mode 100644 ArcFormats/Leaf/LeafCompression.cs diff --git a/ArcFormats/ArcFormats.csproj b/ArcFormats/ArcFormats.csproj index c5a51c084..7edf0275a 100644 --- a/ArcFormats/ArcFormats.csproj +++ b/ArcFormats/ArcFormats.csproj @@ -255,6 +255,7 @@ + diff --git a/ArcFormats/Leaf/ArcPAK.cs b/ArcFormats/Leaf/ArcPAK.cs index 2ca92fa4e..baf5b689a 100644 --- a/ArcFormats/Leaf/ArcPAK.cs +++ b/ArcFormats/Leaf/ArcPAK.cs @@ -313,168 +313,4 @@ internal struct EntryDefV2 : IEntryDefinition public bool IsPacked { get { return _is_packed != 0; } } } #pragma warning restore 649,169 - - // --- LEAF LZSS COMPRESSION --- - internal static class LeafLzss - { - const int N = 4096; - const int F = 18; - const int THR = 2; - const int NIL = N; - - public static byte[] Compress (byte[] input) - { - if (input.Length == 0) return new byte[0]; - - using (var outStream = new MemoryStream (input.Length)) - { - // Arrays size = N + 257 to handle Root Nodes (N+1..N+256) safely - int[] lson = new int[N + 257]; - int[] rson = new int[N + 257]; - int[] dad = new int[N + 257]; - byte[] text_buf = new byte[N + F - 1]; - - for (int j = N + 1; j <= N + 256; j++) rson[j] = NIL; - for (int j = 0; j < N; j++) dad[j] = NIL; - - int match_position = 0, match_length = 0; - - void InsertNode (int r_node) - { - int i, p, cmp; - int key_pos = r_node; - - // Root for this character - p = N + 1 + text_buf[key_pos]; - - rson[r_node] = lson[r_node] = NIL; - match_length = 0; - - cmp = 1; // Initial state - - for (; ; ) - { - if (cmp >= 0) - { - if (rson[p] != NIL) p = rson[p]; - else { rson[p] = r_node; dad[r_node] = p; return; } - } - else - { - if (lson[p] != NIL) p = lson[p]; - else { lson[p] = r_node; dad[r_node] = p; return; } - } - - // Compare bytes only after finding a valid child node 'p' (buffer index) - for (i = 1; i < F; i++) - if ((cmp = text_buf[key_pos + i] - text_buf[p + i]) != 0) break; - - if (i > match_length) - { - match_position = p; - match_length = i; - if (match_length >= F) break; - } - } - - // Replace node logic - dad[r_node] = dad[p]; lson[r_node] = lson[p]; rson[r_node] = rson[p]; - dad[lson[p]] = r_node; dad[rson[p]] = r_node; - if (rson[dad[p]] == p) rson[dad[p]] = r_node; - else lson[dad[p]] = r_node; - dad[p] = NIL; - } - - void DeleteNode (int p) - { - int q; - if (dad[p] == NIL) return; - if (rson[p] == NIL) q = lson[p]; - else if (lson[p] == NIL) q = rson[p]; - else - { - q = lson[p]; - if (rson[q] != NIL) - { - do { q = rson[q]; } while (rson[q] != NIL); - rson[dad[q]] = lson[q]; dad[lson[q]] = dad[q]; - lson[q] = lson[p]; dad[lson[p]] = q; - } - rson[q] = rson[p]; dad[rson[p]] = q; - } - dad[q] = dad[p]; - if (rson[dad[p]] == p) rson[dad[p]] = q; - else lson[dad[p]] = q; - dad[p] = NIL; - } - - int code_buf_ptr = 1; - byte[] code_buf = new byte[17]; - byte mask = 1; - int s = 0, r = N - F; - int len = 0; - - for (int j = 0; j < r; j++) text_buf[j] = 0x20; - - int bytes_read = 0; - for (len = 0; len < F && bytes_read < input.Length; len++) - text_buf[r + len] = input[bytes_read++]; - - if (len == 0) return new byte[0]; - - for (int j = 1; j <= F; j++) InsertNode (r - j); - InsertNode (r); - - do - { - if (match_length > len) match_length = len; - if (match_length <= THR) - { - match_length = 1; - code_buf[0] |= mask; - code_buf[code_buf_ptr++] = text_buf[r]; - } - else - { - code_buf[code_buf_ptr++] = (byte)(match_position & 0xFF); - code_buf[code_buf_ptr++] = (byte)(((match_position >> 4) & 0xF0) | (match_length - (THR + 1))); - } - - if ((mask <<= 1) == 0) - { - outStream.Write (code_buf, 0, code_buf_ptr); - code_buf[0] = 0; code_buf_ptr = 1; mask = 1; - } - - int last_match_length = match_length; - - int i; - for (i = 0; i < last_match_length && bytes_read < input.Length; i++) - { - DeleteNode (s); - byte c = input[bytes_read++]; - text_buf[s] = c; - if (s < F - 1) text_buf[s + N] = c; - s = (s + 1) & (N - 1); - r = (r + 1) & (N - 1); - InsertNode (r); - } - - while (bytes_read == input.Length && i++ < last_match_length) - { - DeleteNode (s); - s = (s + 1) & (N - 1); - r = (r + 1) & (N - 1); - if (--len != 0) InsertNode (r); - } - - } while (len > 0); - - if (code_buf_ptr > 1) - outStream.Write (code_buf, 0, code_buf_ptr); - - return outStream.ToArray(); - } - } - } } \ No newline at end of file diff --git a/ArcFormats/Leaf/LeafCompression.cs b/ArcFormats/Leaf/LeafCompression.cs new file mode 100644 index 000000000..e715d217d --- /dev/null +++ b/ArcFormats/Leaf/LeafCompression.cs @@ -0,0 +1,193 @@ +//! \file LeafCompression.cs +//! \date 2026 Jul 25 +//! \brief Leaf LZSS compression implementation (by gopicolo). +// +// Copyright (C) 2018-2019 by morkt +// +// Permission is hereby granted, free of charge, to any person obtaining a copy +// of this software and associated documentation files (the "Software"), to +// deal in the Software without restriction, including without limitation the +// rights to use, copy, modify, merge, publish, distribute, sublicense, and/or +// sell copies of the Software, and to permit persons to whom the Software is +// furnished to do so, subject to the following conditions: +// +// The above copyright notice and this permission notice shall be included in +// all copies or substantial portions of the Software. +// +// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING +// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS +// IN THE SOFTWARE. +// + +using System.IO; + +namespace GameRes.Formats.Leaf +{ + // --- LEAF LZSS COMPRESSION --- + internal static class LeafLzss + { + const int N = 4096; + const int F = 18; + const int THR = 2; + const int NIL = N; + + public static byte[] Compress (byte[] input) + { + if (input.Length == 0) return new byte[0]; + + using (var outStream = new MemoryStream (input.Length)) + { + // Arrays size = N + 257 to handle Root Nodes (N+1..N+256) safely + int[] lson = new int[N + 257]; + int[] rson = new int[N + 257]; + int[] dad = new int[N + 257]; + byte[] text_buf = new byte[N + F - 1]; + + for (int j = N + 1; j <= N + 256; j++) rson[j] = NIL; + for (int j = 0; j < N; j++) dad[j] = NIL; + + int match_position = 0, match_length = 0; + + void InsertNode (int r_node) + { + int i, p, cmp; + int key_pos = r_node; + + // Root for this character + p = N + 1 + text_buf[key_pos]; + + rson[r_node] = lson[r_node] = NIL; + match_length = 0; + + cmp = 1; // Initial state + + for (; ; ) + { + if (cmp >= 0) + { + if (rson[p] != NIL) p = rson[p]; + else { rson[p] = r_node; dad[r_node] = p; return; } + } + else + { + if (lson[p] != NIL) p = lson[p]; + else { lson[p] = r_node; dad[r_node] = p; return; } + } + + // Compare bytes only after finding a valid child node 'p' (buffer index) + for (i = 1; i < F; i++) + if ((cmp = text_buf[key_pos + i] - text_buf[p + i]) != 0) break; + + if (i > match_length) + { + match_position = p; + match_length = i; + if (match_length >= F) break; + } + } + + // Replace node logic + dad[r_node] = dad[p]; lson[r_node] = lson[p]; rson[r_node] = rson[p]; + dad[lson[p]] = r_node; dad[rson[p]] = r_node; + if (rson[dad[p]] == p) rson[dad[p]] = r_node; + else lson[dad[p]] = r_node; + dad[p] = NIL; + } + + void DeleteNode (int p) + { + int q; + if (dad[p] == NIL) return; + if (rson[p] == NIL) q = lson[p]; + else if (lson[p] == NIL) q = rson[p]; + else + { + q = lson[p]; + if (rson[q] != NIL) + { + do { q = rson[q]; } while (rson[q] != NIL); + rson[dad[q]] = lson[q]; dad[lson[q]] = dad[q]; + lson[q] = lson[p]; dad[lson[p]] = q; + } + rson[q] = rson[p]; dad[rson[p]] = q; + } + dad[q] = dad[p]; + if (rson[dad[p]] == p) rson[dad[p]] = q; + else lson[dad[p]] = q; + dad[p] = NIL; + } + + int code_buf_ptr = 1; + byte[] code_buf = new byte[17]; + byte mask = 1; + int s = 0, r = N - F; + int len = 0; + + for (int j = 0; j < r; j++) text_buf[j] = 0x20; + + int bytes_read = 0; + for (len = 0; len < F && bytes_read < input.Length; len++) + text_buf[r + len] = input[bytes_read++]; + + if (len == 0) return new byte[0]; + + for (int j = 1; j <= F; j++) InsertNode (r - j); + InsertNode (r); + + do + { + if (match_length > len) match_length = len; + if (match_length <= THR) + { + match_length = 1; + code_buf[0] |= mask; + code_buf[code_buf_ptr++] = text_buf[r]; + } + else + { + code_buf[code_buf_ptr++] = (byte)(match_position & 0xFF); + code_buf[code_buf_ptr++] = (byte)(((match_position >> 4) & 0xF0) | (match_length - (THR + 1))); + } + + if ((mask <<= 1) == 0) + { + outStream.Write (code_buf, 0, code_buf_ptr); + code_buf[0] = 0; code_buf_ptr = 1; mask = 1; + } + + int last_match_length = match_length; + + int i; + for (i = 0; i < last_match_length && bytes_read < input.Length; i++) + { + DeleteNode (s); + byte c = input[bytes_read++]; + text_buf[s] = c; + if (s < F - 1) text_buf[s + N] = c; + s = (s + 1) & (N - 1); + r = (r + 1) & (N - 1); + InsertNode (r); + } + + while (bytes_read == input.Length && i++ < last_match_length) + { + DeleteNode (s); + s = (s + 1) & (N - 1); + r = (r + 1) & (N - 1); + if (--len != 0) InsertNode (r); + } + + } while (len > 0); + + if (code_buf_ptr > 1) + outStream.Write (code_buf, 0, code_buf_ptr); + + return outStream.ToArray(); + } + } + } +} From 7d0008e07ae49796a37dff9c659cfc0b83ef2681 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andrei=20M=C3=BCller=20=28Andy=20Miira=29?= Date: Sun, 26 Jul 2026 13:16:26 -0300 Subject: [PATCH 02/15] [Leaf] ArcLAC.cs: LAC repack implementation --- ArcFormats/Leaf/ArcLAC.cs | 96 ++++++++++++++++++++++++++++++++++++++- 1 file changed, 95 insertions(+), 1 deletion(-) diff --git a/ArcFormats/Leaf/ArcLAC.cs b/ArcFormats/Leaf/ArcLAC.cs index 114477015..93c5b180f 100644 --- a/ArcFormats/Leaf/ArcLAC.cs +++ b/ArcFormats/Leaf/ArcLAC.cs @@ -23,9 +23,12 @@ // IN THE SOFTWARE. // +using System; using System.Collections.Generic; using System.ComponentModel.Composition; using System.IO; +using System.Linq; +using System.Text; using GameRes.Compression; namespace GameRes.Formats.Leaf @@ -37,7 +40,7 @@ public class LacOpener : ArchiveFormat public override string Description { get { return "Leaf resource archive"; } } public override uint Signature { get { return 0x43414C; } } // 'LAC' public override bool IsHierarchic { get { return true; } } - public override bool CanWrite { get { return false; } } + public override bool CanWrite { get { return true; } } public override ArcFile TryOpen (ArcView file) { @@ -148,5 +151,96 @@ public override Stream OpenEntry (ArcFile arc, Entry entry) Stream input = arc.File.CreateStream (entry.Offset, entry.Size); return new LzssStream (input); } + + // --- REPACK IMPLEMENTATION --- + + public override void Create (Stream output, IEnumerable entries, ResourceOptions options, EntryCallback callback) + { + int count = entries.Count(); + using (var writer = new BinaryWriter (output, Encoding.ASCII, true)) + { + // Write Header (LAC) + writer.Write (Signature); + writer.Write (count); + + long table_offset = 8; + long data_offset = table_offset + (count * 0x28); + + writer.BaseStream.Position = data_offset; + + var entryInfos = new List(); + int i = 0; + + foreach (var entry in entries) + { + if (callback != null) + callback (i + 1, entry, null); + + long currentOffset = writer.BaseStream.Position; + + using (var input = File.OpenRead(entry.Name)) + { + byte[] raw_data = new byte[input.Length]; + input.Read (raw_data, 0, (int)input.Length); + + // Compress + byte[] compressedData = LeafLzss.Compress (raw_data); + + uint totalSize; + bool isPacked; + + // Write Data + if (compressedData.Length < raw_data.Length) + { + isPacked = true; + totalSize = (uint)(compressedData.Length + 4); + writer.Write ((uint)raw_data.Length); + writer.Write (compressedData); + } + else + { + isPacked = false; + totalSize = (uint)raw_data.Length; + writer.Write (raw_data); + } + + entryInfos.Add (new EntryInfo + { + Name = Path.GetFileName (entry.Name), + Offset = (uint)currentOffset, + Size = totalSize, + IsPacked = isPacked + }); + } + i++; + } + + // Write File Table + writer.BaseStream.Position = table_offset; + foreach (var info in entryInfos) + { + byte[] name_buf = new byte[0x20]; + + byte[] name_bytes = Encodings.cp932.GetBytes (info.Name); + int len = Math.Min (name_bytes.Length, 0x1F); + for (int j = 0; j < len; ++j) + name_buf[j] = (byte)(name_bytes[j] ^ 0xFF); + if (info.IsPacked) + name_buf[0x1F] = 1; + + writer.Write (name_buf); + writer.Write (info.Size); + writer.Write (info.Offset); + } + } + } + + struct EntryInfo + { + public string Name; + public uint Offset; + public uint Size; + public bool IsPacked; + } } } From 23d5f3a653660e568e5b0466a4c9e51db3d040fd Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andrei=20M=C3=BCller=20=28Andy=20Miira=29?= Date: Sun, 26 Jul 2026 14:01:19 -0300 Subject: [PATCH 03/15] [Leaf] ArcLAC.cs: Fix "CanWrite" properties --- ArcFormats/Leaf/ArcLAC.cs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/ArcFormats/Leaf/ArcLAC.cs b/ArcFormats/Leaf/ArcLAC.cs index 93c5b180f..f5812ca5d 100644 --- a/ArcFormats/Leaf/ArcLAC.cs +++ b/ArcFormats/Leaf/ArcLAC.cs @@ -40,7 +40,7 @@ public class LacOpener : ArchiveFormat public override string Description { get { return "Leaf resource archive"; } } public override uint Signature { get { return 0x43414C; } } // 'LAC' public override bool IsHierarchic { get { return true; } } - public override bool CanWrite { get { return true; } } + public override bool CanWrite { get { return false; } } public override ArcFile TryOpen (ArcView file) { @@ -89,7 +89,7 @@ public class PakOpener : ArchiveFormat public override string Description { get { return "Leaf resource archive"; } } public override uint Signature { get { return 0x43414C; } } // 'LAC' public override bool IsHierarchic { get { return false; } } - public override bool CanWrite { get { return false; } } + public override bool CanWrite { get { return true; } } public PakOpener () { From 798a3f24805a012741fb3d07f0f4eb279c5030a2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andrei=20M=C3=BCller=20=28Andy=20Miira=29?= Date: Sun, 16 Aug 2026 19:03:22 -0300 Subject: [PATCH 04/15] [Leaf] Add information disclaimer regarding this specific LAC repack implementation (i.e., supports "Kimi ga Yobu, Megiddo no Oka de") --- ArcFormats/Leaf/ArcLAC.cs | 2 ++ 1 file changed, 2 insertions(+) diff --git a/ArcFormats/Leaf/ArcLAC.cs b/ArcFormats/Leaf/ArcLAC.cs index f5812ca5d..fbaeec805 100644 --- a/ArcFormats/Leaf/ArcLAC.cs +++ b/ArcFormats/Leaf/ArcLAC.cs @@ -153,6 +153,8 @@ public override Stream OpenEntry (ArcFile arc, Entry entry) } // --- REPACK IMPLEMENTATION --- + // Note: This implementation was made for the Leaf game "Kimi ga Yobu, Megiddo no Oka de". + // Other games may or may not be supported. public override void Create (Stream output, IEnumerable entries, ResourceOptions options, EntryCallback callback) { From 68e88549d87748574ceaf2c226f3801ec74fc42a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andrei=20M=C3=BCller=20=28Andy=20Miira=29?= Date: Sun, 16 Aug 2026 19:24:04 -0300 Subject: [PATCH 05/15] [Leaf] ArcLAC.cs: Order entries using the same order as the original PAK/LAC archive --- ArcFormats/Leaf/ArcLAC.cs | 46 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 46 insertions(+) diff --git a/ArcFormats/Leaf/ArcLAC.cs b/ArcFormats/Leaf/ArcLAC.cs index fbaeec805..e6d4015dd 100644 --- a/ArcFormats/Leaf/ArcLAC.cs +++ b/ArcFormats/Leaf/ArcLAC.cs @@ -158,6 +158,9 @@ public override Stream OpenEntry (ArcFile arc, Entry entry) public override void Create (Stream output, IEnumerable entries, ResourceOptions options, EntryCallback callback) { + // Order entries using the same order as the original PAK/LAC archive. + entries = entries.OrderBy (x => x, LacEntryComparer.Instance).ToList(); + int count = entries.Count(); using (var writer = new BinaryWriter (output, Encoding.ASCII, true)) { @@ -244,5 +247,48 @@ struct EntryInfo public uint Size; public bool IsPacked; } + + internal sealed class LacEntryComparer : IComparer + { + public static readonly LacEntryComparer Instance = new LacEntryComparer(); + + public int Compare (Entry x, Entry y) + { + string x_name = Path.GetFileName (x.Name); + string y_name = Path.GetFileName (y.Name); + + // LAC names use '_' as a separator. The archive's order puts + // that separator after digits and before letters, which may not be + // the order produced by the current-culture string comparer. + int result = CompareNames (x_name, y_name); + if (result != 0) + return result; + + // Keep the ordering deterministic for names that differ only + // by case or by a path component. + return StringComparer.Ordinal.Compare (x.Name, y.Name); + } + + private static int CompareNames (string x, string y) + { + int length = Math.Min (x.Length, y.Length); + for (int i = 0; i < length; ++i) + { + char xc = char.ToUpperInvariant (x[i]); + char yc = char.ToUpperInvariant (y[i]); + if (xc == yc) + continue; + + if (xc == '_') + return char.IsLetter (yc) ? -1 : 1; + if (yc == '_') + return char.IsLetter (xc) ? 1 : -1; + + return xc < yc ? -1 : 1; + } + + return x.Length.CompareTo (y.Length); + } + } } } From da8b7cd40060bd444e22943c67d0d3a7e706596a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andrei=20M=C3=BCller=20=28Andy=20Miira=29?= Date: Sun, 26 Jul 2026 00:07:56 -0300 Subject: [PATCH 06/15] [Leaf] ArcPAK.cs: Move Leaf LZSS compression logic to a separate class (LeafCompression.cs) --- ArcFormats/ArcFormats.csproj | 1 + ArcFormats/Leaf/ArcPAK.cs | 164 ------------------------ ArcFormats/Leaf/LeafCompression.cs | 193 +++++++++++++++++++++++++++++ 3 files changed, 194 insertions(+), 164 deletions(-) create mode 100644 ArcFormats/Leaf/LeafCompression.cs diff --git a/ArcFormats/ArcFormats.csproj b/ArcFormats/ArcFormats.csproj index 2a28fc930..e294ef7a7 100644 --- a/ArcFormats/ArcFormats.csproj +++ b/ArcFormats/ArcFormats.csproj @@ -256,6 +256,7 @@ + diff --git a/ArcFormats/Leaf/ArcPAK.cs b/ArcFormats/Leaf/ArcPAK.cs index 2ca92fa4e..baf5b689a 100644 --- a/ArcFormats/Leaf/ArcPAK.cs +++ b/ArcFormats/Leaf/ArcPAK.cs @@ -313,168 +313,4 @@ internal struct EntryDefV2 : IEntryDefinition public bool IsPacked { get { return _is_packed != 0; } } } #pragma warning restore 649,169 - - // --- LEAF LZSS COMPRESSION --- - internal static class LeafLzss - { - const int N = 4096; - const int F = 18; - const int THR = 2; - const int NIL = N; - - public static byte[] Compress (byte[] input) - { - if (input.Length == 0) return new byte[0]; - - using (var outStream = new MemoryStream (input.Length)) - { - // Arrays size = N + 257 to handle Root Nodes (N+1..N+256) safely - int[] lson = new int[N + 257]; - int[] rson = new int[N + 257]; - int[] dad = new int[N + 257]; - byte[] text_buf = new byte[N + F - 1]; - - for (int j = N + 1; j <= N + 256; j++) rson[j] = NIL; - for (int j = 0; j < N; j++) dad[j] = NIL; - - int match_position = 0, match_length = 0; - - void InsertNode (int r_node) - { - int i, p, cmp; - int key_pos = r_node; - - // Root for this character - p = N + 1 + text_buf[key_pos]; - - rson[r_node] = lson[r_node] = NIL; - match_length = 0; - - cmp = 1; // Initial state - - for (; ; ) - { - if (cmp >= 0) - { - if (rson[p] != NIL) p = rson[p]; - else { rson[p] = r_node; dad[r_node] = p; return; } - } - else - { - if (lson[p] != NIL) p = lson[p]; - else { lson[p] = r_node; dad[r_node] = p; return; } - } - - // Compare bytes only after finding a valid child node 'p' (buffer index) - for (i = 1; i < F; i++) - if ((cmp = text_buf[key_pos + i] - text_buf[p + i]) != 0) break; - - if (i > match_length) - { - match_position = p; - match_length = i; - if (match_length >= F) break; - } - } - - // Replace node logic - dad[r_node] = dad[p]; lson[r_node] = lson[p]; rson[r_node] = rson[p]; - dad[lson[p]] = r_node; dad[rson[p]] = r_node; - if (rson[dad[p]] == p) rson[dad[p]] = r_node; - else lson[dad[p]] = r_node; - dad[p] = NIL; - } - - void DeleteNode (int p) - { - int q; - if (dad[p] == NIL) return; - if (rson[p] == NIL) q = lson[p]; - else if (lson[p] == NIL) q = rson[p]; - else - { - q = lson[p]; - if (rson[q] != NIL) - { - do { q = rson[q]; } while (rson[q] != NIL); - rson[dad[q]] = lson[q]; dad[lson[q]] = dad[q]; - lson[q] = lson[p]; dad[lson[p]] = q; - } - rson[q] = rson[p]; dad[rson[p]] = q; - } - dad[q] = dad[p]; - if (rson[dad[p]] == p) rson[dad[p]] = q; - else lson[dad[p]] = q; - dad[p] = NIL; - } - - int code_buf_ptr = 1; - byte[] code_buf = new byte[17]; - byte mask = 1; - int s = 0, r = N - F; - int len = 0; - - for (int j = 0; j < r; j++) text_buf[j] = 0x20; - - int bytes_read = 0; - for (len = 0; len < F && bytes_read < input.Length; len++) - text_buf[r + len] = input[bytes_read++]; - - if (len == 0) return new byte[0]; - - for (int j = 1; j <= F; j++) InsertNode (r - j); - InsertNode (r); - - do - { - if (match_length > len) match_length = len; - if (match_length <= THR) - { - match_length = 1; - code_buf[0] |= mask; - code_buf[code_buf_ptr++] = text_buf[r]; - } - else - { - code_buf[code_buf_ptr++] = (byte)(match_position & 0xFF); - code_buf[code_buf_ptr++] = (byte)(((match_position >> 4) & 0xF0) | (match_length - (THR + 1))); - } - - if ((mask <<= 1) == 0) - { - outStream.Write (code_buf, 0, code_buf_ptr); - code_buf[0] = 0; code_buf_ptr = 1; mask = 1; - } - - int last_match_length = match_length; - - int i; - for (i = 0; i < last_match_length && bytes_read < input.Length; i++) - { - DeleteNode (s); - byte c = input[bytes_read++]; - text_buf[s] = c; - if (s < F - 1) text_buf[s + N] = c; - s = (s + 1) & (N - 1); - r = (r + 1) & (N - 1); - InsertNode (r); - } - - while (bytes_read == input.Length && i++ < last_match_length) - { - DeleteNode (s); - s = (s + 1) & (N - 1); - r = (r + 1) & (N - 1); - if (--len != 0) InsertNode (r); - } - - } while (len > 0); - - if (code_buf_ptr > 1) - outStream.Write (code_buf, 0, code_buf_ptr); - - return outStream.ToArray(); - } - } - } } \ No newline at end of file diff --git a/ArcFormats/Leaf/LeafCompression.cs b/ArcFormats/Leaf/LeafCompression.cs new file mode 100644 index 000000000..e715d217d --- /dev/null +++ b/ArcFormats/Leaf/LeafCompression.cs @@ -0,0 +1,193 @@ +//! \file LeafCompression.cs +//! \date 2026 Jul 25 +//! \brief Leaf LZSS compression implementation (by gopicolo). +// +// Copyright (C) 2018-2019 by morkt +// +// Permission is hereby granted, free of charge, to any person obtaining a copy +// of this software and associated documentation files (the "Software"), to +// deal in the Software without restriction, including without limitation the +// rights to use, copy, modify, merge, publish, distribute, sublicense, and/or +// sell copies of the Software, and to permit persons to whom the Software is +// furnished to do so, subject to the following conditions: +// +// The above copyright notice and this permission notice shall be included in +// all copies or substantial portions of the Software. +// +// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING +// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS +// IN THE SOFTWARE. +// + +using System.IO; + +namespace GameRes.Formats.Leaf +{ + // --- LEAF LZSS COMPRESSION --- + internal static class LeafLzss + { + const int N = 4096; + const int F = 18; + const int THR = 2; + const int NIL = N; + + public static byte[] Compress (byte[] input) + { + if (input.Length == 0) return new byte[0]; + + using (var outStream = new MemoryStream (input.Length)) + { + // Arrays size = N + 257 to handle Root Nodes (N+1..N+256) safely + int[] lson = new int[N + 257]; + int[] rson = new int[N + 257]; + int[] dad = new int[N + 257]; + byte[] text_buf = new byte[N + F - 1]; + + for (int j = N + 1; j <= N + 256; j++) rson[j] = NIL; + for (int j = 0; j < N; j++) dad[j] = NIL; + + int match_position = 0, match_length = 0; + + void InsertNode (int r_node) + { + int i, p, cmp; + int key_pos = r_node; + + // Root for this character + p = N + 1 + text_buf[key_pos]; + + rson[r_node] = lson[r_node] = NIL; + match_length = 0; + + cmp = 1; // Initial state + + for (; ; ) + { + if (cmp >= 0) + { + if (rson[p] != NIL) p = rson[p]; + else { rson[p] = r_node; dad[r_node] = p; return; } + } + else + { + if (lson[p] != NIL) p = lson[p]; + else { lson[p] = r_node; dad[r_node] = p; return; } + } + + // Compare bytes only after finding a valid child node 'p' (buffer index) + for (i = 1; i < F; i++) + if ((cmp = text_buf[key_pos + i] - text_buf[p + i]) != 0) break; + + if (i > match_length) + { + match_position = p; + match_length = i; + if (match_length >= F) break; + } + } + + // Replace node logic + dad[r_node] = dad[p]; lson[r_node] = lson[p]; rson[r_node] = rson[p]; + dad[lson[p]] = r_node; dad[rson[p]] = r_node; + if (rson[dad[p]] == p) rson[dad[p]] = r_node; + else lson[dad[p]] = r_node; + dad[p] = NIL; + } + + void DeleteNode (int p) + { + int q; + if (dad[p] == NIL) return; + if (rson[p] == NIL) q = lson[p]; + else if (lson[p] == NIL) q = rson[p]; + else + { + q = lson[p]; + if (rson[q] != NIL) + { + do { q = rson[q]; } while (rson[q] != NIL); + rson[dad[q]] = lson[q]; dad[lson[q]] = dad[q]; + lson[q] = lson[p]; dad[lson[p]] = q; + } + rson[q] = rson[p]; dad[rson[p]] = q; + } + dad[q] = dad[p]; + if (rson[dad[p]] == p) rson[dad[p]] = q; + else lson[dad[p]] = q; + dad[p] = NIL; + } + + int code_buf_ptr = 1; + byte[] code_buf = new byte[17]; + byte mask = 1; + int s = 0, r = N - F; + int len = 0; + + for (int j = 0; j < r; j++) text_buf[j] = 0x20; + + int bytes_read = 0; + for (len = 0; len < F && bytes_read < input.Length; len++) + text_buf[r + len] = input[bytes_read++]; + + if (len == 0) return new byte[0]; + + for (int j = 1; j <= F; j++) InsertNode (r - j); + InsertNode (r); + + do + { + if (match_length > len) match_length = len; + if (match_length <= THR) + { + match_length = 1; + code_buf[0] |= mask; + code_buf[code_buf_ptr++] = text_buf[r]; + } + else + { + code_buf[code_buf_ptr++] = (byte)(match_position & 0xFF); + code_buf[code_buf_ptr++] = (byte)(((match_position >> 4) & 0xF0) | (match_length - (THR + 1))); + } + + if ((mask <<= 1) == 0) + { + outStream.Write (code_buf, 0, code_buf_ptr); + code_buf[0] = 0; code_buf_ptr = 1; mask = 1; + } + + int last_match_length = match_length; + + int i; + for (i = 0; i < last_match_length && bytes_read < input.Length; i++) + { + DeleteNode (s); + byte c = input[bytes_read++]; + text_buf[s] = c; + if (s < F - 1) text_buf[s + N] = c; + s = (s + 1) & (N - 1); + r = (r + 1) & (N - 1); + InsertNode (r); + } + + while (bytes_read == input.Length && i++ < last_match_length) + { + DeleteNode (s); + s = (s + 1) & (N - 1); + r = (r + 1) & (N - 1); + if (--len != 0) InsertNode (r); + } + + } while (len > 0); + + if (code_buf_ptr > 1) + outStream.Write (code_buf, 0, code_buf_ptr); + + return outStream.ToArray(); + } + } + } +} From f8f69e93a65bd9ecff8eba902376fb2d4d98ab1c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andrei=20M=C3=BCller=20=28Andy=20Miira=29?= Date: Sun, 26 Jul 2026 13:16:26 -0300 Subject: [PATCH 07/15] [Leaf] ArcLAC.cs: LAC repack implementation --- ArcFormats/Leaf/ArcLAC.cs | 96 ++++++++++++++++++++++++++++++++++++++- 1 file changed, 95 insertions(+), 1 deletion(-) diff --git a/ArcFormats/Leaf/ArcLAC.cs b/ArcFormats/Leaf/ArcLAC.cs index 114477015..93c5b180f 100644 --- a/ArcFormats/Leaf/ArcLAC.cs +++ b/ArcFormats/Leaf/ArcLAC.cs @@ -23,9 +23,12 @@ // IN THE SOFTWARE. // +using System; using System.Collections.Generic; using System.ComponentModel.Composition; using System.IO; +using System.Linq; +using System.Text; using GameRes.Compression; namespace GameRes.Formats.Leaf @@ -37,7 +40,7 @@ public class LacOpener : ArchiveFormat public override string Description { get { return "Leaf resource archive"; } } public override uint Signature { get { return 0x43414C; } } // 'LAC' public override bool IsHierarchic { get { return true; } } - public override bool CanWrite { get { return false; } } + public override bool CanWrite { get { return true; } } public override ArcFile TryOpen (ArcView file) { @@ -148,5 +151,96 @@ public override Stream OpenEntry (ArcFile arc, Entry entry) Stream input = arc.File.CreateStream (entry.Offset, entry.Size); return new LzssStream (input); } + + // --- REPACK IMPLEMENTATION --- + + public override void Create (Stream output, IEnumerable entries, ResourceOptions options, EntryCallback callback) + { + int count = entries.Count(); + using (var writer = new BinaryWriter (output, Encoding.ASCII, true)) + { + // Write Header (LAC) + writer.Write (Signature); + writer.Write (count); + + long table_offset = 8; + long data_offset = table_offset + (count * 0x28); + + writer.BaseStream.Position = data_offset; + + var entryInfos = new List(); + int i = 0; + + foreach (var entry in entries) + { + if (callback != null) + callback (i + 1, entry, null); + + long currentOffset = writer.BaseStream.Position; + + using (var input = File.OpenRead(entry.Name)) + { + byte[] raw_data = new byte[input.Length]; + input.Read (raw_data, 0, (int)input.Length); + + // Compress + byte[] compressedData = LeafLzss.Compress (raw_data); + + uint totalSize; + bool isPacked; + + // Write Data + if (compressedData.Length < raw_data.Length) + { + isPacked = true; + totalSize = (uint)(compressedData.Length + 4); + writer.Write ((uint)raw_data.Length); + writer.Write (compressedData); + } + else + { + isPacked = false; + totalSize = (uint)raw_data.Length; + writer.Write (raw_data); + } + + entryInfos.Add (new EntryInfo + { + Name = Path.GetFileName (entry.Name), + Offset = (uint)currentOffset, + Size = totalSize, + IsPacked = isPacked + }); + } + i++; + } + + // Write File Table + writer.BaseStream.Position = table_offset; + foreach (var info in entryInfos) + { + byte[] name_buf = new byte[0x20]; + + byte[] name_bytes = Encodings.cp932.GetBytes (info.Name); + int len = Math.Min (name_bytes.Length, 0x1F); + for (int j = 0; j < len; ++j) + name_buf[j] = (byte)(name_bytes[j] ^ 0xFF); + if (info.IsPacked) + name_buf[0x1F] = 1; + + writer.Write (name_buf); + writer.Write (info.Size); + writer.Write (info.Offset); + } + } + } + + struct EntryInfo + { + public string Name; + public uint Offset; + public uint Size; + public bool IsPacked; + } } } From 4309b883f869f31a1668d9a995b9d284ba0aa5b3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andrei=20M=C3=BCller=20=28Andy=20Miira=29?= Date: Sun, 26 Jul 2026 14:01:19 -0300 Subject: [PATCH 08/15] [Leaf] ArcLAC.cs: Fix "CanWrite" properties --- ArcFormats/Leaf/ArcLAC.cs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/ArcFormats/Leaf/ArcLAC.cs b/ArcFormats/Leaf/ArcLAC.cs index 93c5b180f..f5812ca5d 100644 --- a/ArcFormats/Leaf/ArcLAC.cs +++ b/ArcFormats/Leaf/ArcLAC.cs @@ -40,7 +40,7 @@ public class LacOpener : ArchiveFormat public override string Description { get { return "Leaf resource archive"; } } public override uint Signature { get { return 0x43414C; } } // 'LAC' public override bool IsHierarchic { get { return true; } } - public override bool CanWrite { get { return true; } } + public override bool CanWrite { get { return false; } } public override ArcFile TryOpen (ArcView file) { @@ -89,7 +89,7 @@ public class PakOpener : ArchiveFormat public override string Description { get { return "Leaf resource archive"; } } public override uint Signature { get { return 0x43414C; } } // 'LAC' public override bool IsHierarchic { get { return false; } } - public override bool CanWrite { get { return false; } } + public override bool CanWrite { get { return true; } } public PakOpener () { From 153456ba98bd34af73e7697fa0a62648ab754816 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andrei=20M=C3=BCller=20=28Andy=20Miira=29?= Date: Sun, 16 Aug 2026 19:03:22 -0300 Subject: [PATCH 09/15] [Leaf] Add information disclaimer regarding this specific LAC repack implementation (i.e., supports "Kimi ga Yobu, Megiddo no Oka de") --- ArcFormats/Leaf/ArcLAC.cs | 2 ++ 1 file changed, 2 insertions(+) diff --git a/ArcFormats/Leaf/ArcLAC.cs b/ArcFormats/Leaf/ArcLAC.cs index f5812ca5d..fbaeec805 100644 --- a/ArcFormats/Leaf/ArcLAC.cs +++ b/ArcFormats/Leaf/ArcLAC.cs @@ -153,6 +153,8 @@ public override Stream OpenEntry (ArcFile arc, Entry entry) } // --- REPACK IMPLEMENTATION --- + // Note: This implementation was made for the Leaf game "Kimi ga Yobu, Megiddo no Oka de". + // Other games may or may not be supported. public override void Create (Stream output, IEnumerable entries, ResourceOptions options, EntryCallback callback) { From d8023674eef421ad5bebf17d484a8e64a0f22622 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andrei=20M=C3=BCller=20=28Andy=20Miira=29?= Date: Sun, 16 Aug 2026 19:24:04 -0300 Subject: [PATCH 10/15] [Leaf] ArcLAC.cs: Order entries using the same order as the original PAK/LAC archive --- ArcFormats/Leaf/ArcLAC.cs | 46 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 46 insertions(+) diff --git a/ArcFormats/Leaf/ArcLAC.cs b/ArcFormats/Leaf/ArcLAC.cs index fbaeec805..e6d4015dd 100644 --- a/ArcFormats/Leaf/ArcLAC.cs +++ b/ArcFormats/Leaf/ArcLAC.cs @@ -158,6 +158,9 @@ public override Stream OpenEntry (ArcFile arc, Entry entry) public override void Create (Stream output, IEnumerable entries, ResourceOptions options, EntryCallback callback) { + // Order entries using the same order as the original PAK/LAC archive. + entries = entries.OrderBy (x => x, LacEntryComparer.Instance).ToList(); + int count = entries.Count(); using (var writer = new BinaryWriter (output, Encoding.ASCII, true)) { @@ -244,5 +247,48 @@ struct EntryInfo public uint Size; public bool IsPacked; } + + internal sealed class LacEntryComparer : IComparer + { + public static readonly LacEntryComparer Instance = new LacEntryComparer(); + + public int Compare (Entry x, Entry y) + { + string x_name = Path.GetFileName (x.Name); + string y_name = Path.GetFileName (y.Name); + + // LAC names use '_' as a separator. The archive's order puts + // that separator after digits and before letters, which may not be + // the order produced by the current-culture string comparer. + int result = CompareNames (x_name, y_name); + if (result != 0) + return result; + + // Keep the ordering deterministic for names that differ only + // by case or by a path component. + return StringComparer.Ordinal.Compare (x.Name, y.Name); + } + + private static int CompareNames (string x, string y) + { + int length = Math.Min (x.Length, y.Length); + for (int i = 0; i < length; ++i) + { + char xc = char.ToUpperInvariant (x[i]); + char yc = char.ToUpperInvariant (y[i]); + if (xc == yc) + continue; + + if (xc == '_') + return char.IsLetter (yc) ? -1 : 1; + if (yc == '_') + return char.IsLetter (xc) ? 1 : -1; + + return xc < yc ? -1 : 1; + } + + return x.Length.CompareTo (y.Length); + } + } } } From e639c5485fa6444a145b6ef1851524f9275a33d3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andrei=20M=C3=BCller=20=28Andy=20Miira=29?= Date: Mon, 17 Aug 2026 14:44:07 -0300 Subject: [PATCH 11/15] [Leaf] ArcLAC.cs: Adjust compression logic to skip small uncompressed data --- ArcFormats/Leaf/ArcLAC.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/ArcFormats/Leaf/ArcLAC.cs b/ArcFormats/Leaf/ArcLAC.cs index e6d4015dd..7a9c11142 100644 --- a/ArcFormats/Leaf/ArcLAC.cs +++ b/ArcFormats/Leaf/ArcLAC.cs @@ -195,7 +195,7 @@ public override void Create (Stream output, IEnumerable entries, Resource bool isPacked; // Write Data - if (compressedData.Length < raw_data.Length) + if (raw_data.Length > 32 && compressedData.Length < raw_data.Length) { isPacked = true; totalSize = (uint)(compressedData.Length + 4); From c546e7929ffd647e1c0cad3716776c2ecf22db0c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andrei=20M=C3=BCller=20=28Andy=20Miira=29?= Date: Mon, 17 Aug 2026 15:06:13 -0300 Subject: [PATCH 12/15] [Leaf] ArcLAC.cs: Clarify condition for skipping small uncompressed data --- ArcFormats/Leaf/ArcLAC.cs | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/ArcFormats/Leaf/ArcLAC.cs b/ArcFormats/Leaf/ArcLAC.cs index 7a9c11142..1a082e322 100644 --- a/ArcFormats/Leaf/ArcLAC.cs +++ b/ArcFormats/Leaf/ArcLAC.cs @@ -195,7 +195,8 @@ public override void Create (Stream output, IEnumerable entries, Resource bool isPacked; // Write Data - if (raw_data.Length > 32 && compressedData.Length < raw_data.Length) + if (raw_data.Length > 32 // Skip small uncompressed data that should not be compressed. + && compressedData.Length < raw_data.Length) { isPacked = true; totalSize = (uint)(compressedData.Length + 4); From 0d8b1f145c7eea2f9f4775d463b4e9d20980dea8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andrei=20M=C3=BCller=20=28Andy=20Miira=29?= Date: Mon, 17 Aug 2026 15:25:34 -0300 Subject: [PATCH 13/15] [Leaf] ArcLAC.cs: Use variable naming convention consistent with ArcPAK.cs --- ArcFormats/Leaf/ArcLAC.cs | 42 +++++++++++++++++++-------------------- 1 file changed, 21 insertions(+), 21 deletions(-) diff --git a/ArcFormats/Leaf/ArcLAC.cs b/ArcFormats/Leaf/ArcLAC.cs index 1a082e322..401578a12 100644 --- a/ArcFormats/Leaf/ArcLAC.cs +++ b/ArcFormats/Leaf/ArcLAC.cs @@ -168,10 +168,10 @@ public override void Create (Stream output, IEnumerable entries, Resource writer.Write (Signature); writer.Write (count); - long table_offset = 8; - long data_offset = table_offset + (count * 0x28); + long tableOffset = 8; + long dataOffset = tableOffset + (count * 0x28); - writer.BaseStream.Position = data_offset; + writer.BaseStream.Position = dataOffset; var entryInfos = new List(); int i = 0; @@ -185,29 +185,29 @@ public override void Create (Stream output, IEnumerable entries, Resource using (var input = File.OpenRead(entry.Name)) { - byte[] raw_data = new byte[input.Length]; - input.Read (raw_data, 0, (int)input.Length); + byte[] rawData = new byte[input.Length]; + input.Read (rawData, 0, (int)input.Length); // Compress - byte[] compressedData = LeafLzss.Compress (raw_data); + byte[] compressedData = LeafLzss.Compress (rawData); uint totalSize; bool isPacked; // Write Data - if (raw_data.Length > 32 // Skip small uncompressed data that should not be compressed. - && compressedData.Length < raw_data.Length) + if (rawData.Length > 32 // Skip small uncompressed data that should not be compressed. + && compressedData.Length < rawData.Length) { isPacked = true; totalSize = (uint)(compressedData.Length + 4); - writer.Write ((uint)raw_data.Length); + writer.Write ((uint)rawData.Length); writer.Write (compressedData); } else { isPacked = false; - totalSize = (uint)raw_data.Length; - writer.Write (raw_data); + totalSize = (uint)rawData.Length; + writer.Write (rawData); } entryInfos.Add (new EntryInfo @@ -222,19 +222,19 @@ public override void Create (Stream output, IEnumerable entries, Resource } // Write File Table - writer.BaseStream.Position = table_offset; + writer.BaseStream.Position = tableOffset; foreach (var info in entryInfos) { - byte[] name_buf = new byte[0x20]; + byte[] nameBuf = new byte[0x20]; - byte[] name_bytes = Encodings.cp932.GetBytes (info.Name); - int len = Math.Min (name_bytes.Length, 0x1F); + byte[] nameBytes = Encodings.cp932.GetBytes (info.Name); + int len = Math.Min (nameBytes.Length, 0x1F); for (int j = 0; j < len; ++j) - name_buf[j] = (byte)(name_bytes[j] ^ 0xFF); + nameBuf[j] = (byte)(nameBytes[j] ^ 0xFF); if (info.IsPacked) - name_buf[0x1F] = 1; + nameBuf[0x1F] = 1; - writer.Write (name_buf); + writer.Write (nameBuf); writer.Write (info.Size); writer.Write (info.Offset); } @@ -255,13 +255,13 @@ internal sealed class LacEntryComparer : IComparer public int Compare (Entry x, Entry y) { - string x_name = Path.GetFileName (x.Name); - string y_name = Path.GetFileName (y.Name); + string xName = Path.GetFileName (x.Name); + string yName = Path.GetFileName (y.Name); // LAC names use '_' as a separator. The archive's order puts // that separator after digits and before letters, which may not be // the order produced by the current-culture string comparer. - int result = CompareNames (x_name, y_name); + int result = CompareNames (xName, yName); if (result != 0) return result; From 3942d192f6042e315f19d5ea26716418fbbf9f43 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andrei=20M=C3=BCller=20=28Andy=20Miira=29?= Date: Mon, 17 Aug 2026 16:03:21 -0300 Subject: [PATCH 14/15] [Leaf] LeafCompression.cs: Add `frameFill` parameter to LZSS compression - Update ArcPAK/ArcLAC usage of Compress() --- ArcFormats/Leaf/ArcLAC.cs | 2 +- ArcFormats/Leaf/ArcPAK.cs | 2 +- ArcFormats/Leaf/LeafCompression.cs | 4 ++-- 3 files changed, 4 insertions(+), 4 deletions(-) diff --git a/ArcFormats/Leaf/ArcLAC.cs b/ArcFormats/Leaf/ArcLAC.cs index 401578a12..4adaae4bc 100644 --- a/ArcFormats/Leaf/ArcLAC.cs +++ b/ArcFormats/Leaf/ArcLAC.cs @@ -189,7 +189,7 @@ public override void Create (Stream output, IEnumerable entries, Resource input.Read (rawData, 0, (int)input.Length); // Compress - byte[] compressedData = LeafLzss.Compress (rawData); + byte[] compressedData = LeafLzss.Compress (rawData, 0); uint totalSize; bool isPacked; diff --git a/ArcFormats/Leaf/ArcPAK.cs b/ArcFormats/Leaf/ArcPAK.cs index baf5b689a..893bcf5d3 100644 --- a/ArcFormats/Leaf/ArcPAK.cs +++ b/ArcFormats/Leaf/ArcPAK.cs @@ -199,7 +199,7 @@ public override void Create (Stream output, IEnumerable entries, Resource input.Read (rawData, 0, (int)input.Length); // Compress - byte[] compressedData = LeafLzss.Compress (rawData); + byte[] compressedData = LeafLzss.Compress (rawData, 0x20); // Write Data (Prefix + LZSS) writer.Write ((uint)compressedData.Length); diff --git a/ArcFormats/Leaf/LeafCompression.cs b/ArcFormats/Leaf/LeafCompression.cs index e715d217d..7c2e4cabb 100644 --- a/ArcFormats/Leaf/LeafCompression.cs +++ b/ArcFormats/Leaf/LeafCompression.cs @@ -35,7 +35,7 @@ internal static class LeafLzss const int THR = 2; const int NIL = N; - public static byte[] Compress (byte[] input) + public static byte[] Compress (byte[] input, byte frameFill) { if (input.Length == 0) return new byte[0]; @@ -127,7 +127,7 @@ void DeleteNode (int p) int s = 0, r = N - F; int len = 0; - for (int j = 0; j < r; j++) text_buf[j] = 0x20; + for (int j = 0; j < r; j++) text_buf[j] = frameFill; int bytes_read = 0; for (len = 0; len < F && bytes_read < input.Length; len++) From 529b94d736572ecedd619d38435a6d9e58083ee2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andrei=20M=C3=BCller=20=28Andy=20Miira=29?= Date: Tue, 18 Aug 2026 23:17:03 -0300 Subject: [PATCH 15/15] [Leaf] ArcLAC.cs: Simplify entry ordering logic; Remove custom LacEntryComparer --- ArcFormats/Leaf/ArcLAC.cs | 45 +-------------------------------------- 1 file changed, 1 insertion(+), 44 deletions(-) diff --git a/ArcFormats/Leaf/ArcLAC.cs b/ArcFormats/Leaf/ArcLAC.cs index 4adaae4bc..a7a5257b8 100644 --- a/ArcFormats/Leaf/ArcLAC.cs +++ b/ArcFormats/Leaf/ArcLAC.cs @@ -159,7 +159,7 @@ public override Stream OpenEntry (ArcFile arc, Entry entry) public override void Create (Stream output, IEnumerable entries, ResourceOptions options, EntryCallback callback) { // Order entries using the same order as the original PAK/LAC archive. - entries = entries.OrderBy (x => x, LacEntryComparer.Instance).ToList(); + entries = entries.OrderBy (x => x.Name.ToLowerAscii(), StringComparer.Ordinal).ToList(); int count = entries.Count(); using (var writer = new BinaryWriter (output, Encoding.ASCII, true)) @@ -248,48 +248,5 @@ struct EntryInfo public uint Size; public bool IsPacked; } - - internal sealed class LacEntryComparer : IComparer - { - public static readonly LacEntryComparer Instance = new LacEntryComparer(); - - public int Compare (Entry x, Entry y) - { - string xName = Path.GetFileName (x.Name); - string yName = Path.GetFileName (y.Name); - - // LAC names use '_' as a separator. The archive's order puts - // that separator after digits and before letters, which may not be - // the order produced by the current-culture string comparer. - int result = CompareNames (xName, yName); - if (result != 0) - return result; - - // Keep the ordering deterministic for names that differ only - // by case or by a path component. - return StringComparer.Ordinal.Compare (x.Name, y.Name); - } - - private static int CompareNames (string x, string y) - { - int length = Math.Min (x.Length, y.Length); - for (int i = 0; i < length; ++i) - { - char xc = char.ToUpperInvariant (x[i]); - char yc = char.ToUpperInvariant (y[i]); - if (xc == yc) - continue; - - if (xc == '_') - return char.IsLetter (yc) ? -1 : 1; - if (yc == '_') - return char.IsLetter (xc) ? 1 : -1; - - return xc < yc ? -1 : 1; - } - - return x.Length.CompareTo (y.Length); - } - } } }