From 067d8a5163cb1138360664b313124e4e9c55c0d9 Mon Sep 17 00:00:00 2001 From: morkt Date: Fri, 9 Feb 2018 17:34:30 +0400 Subject: [PATCH] (Legacy): implemented K3 archives and K2 images. --- Legacy/Gsx/ArcK3.cs | 69 +++++++++++++++++++++++++ Legacy/Gsx/ImageK2.cs | 116 ++++++++++++++++++++++++++++++++++++++++++ Legacy/Legacy.csproj | 2 + 3 files changed, 187 insertions(+) create mode 100644 Legacy/Gsx/ArcK3.cs create mode 100644 Legacy/Gsx/ImageK2.cs diff --git a/Legacy/Gsx/ArcK3.cs b/Legacy/Gsx/ArcK3.cs new file mode 100644 index 00000000..00f12ea0 --- /dev/null +++ b/Legacy/Gsx/ArcK3.cs @@ -0,0 +1,69 @@ +//! \file ArcK3.cs +//! \date 2018 Feb 09 +//! \brief Toyo GSX resource archive. +// +// Copyright (C) 2018 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.Collections.Generic; +using System.ComponentModel.Composition; + +// [000225][Light Plan] My Fairink Yousei Byakuya Monogatari + +namespace GameRes.Formats.Gsx +{ + [Export(typeof(ArchiveFormat))] + public class K3Opener : ArchiveFormat + { + public override string Tag { get { return "K3"; } } + public override string Description { get { return "Toyo GSX resource archive"; } } + public override uint Signature { get { return 0; } } + public override bool IsHierarchic { get { return false; } } + public override bool CanWrite { get { return false; } } + + public override ArcFile TryOpen (ArcView file) + { + if (!file.View.AsciiEqual (0, "K3")) + return null; + int count = file.View.ReadInt32 (2); + if (!IsSaneCount (count)) + return null; + uint index_offset = 6; + long base_offset = index_offset + count * 0x40; + var dir = new List (count); + for (int i = 0; i < count; ++i) + { + uint offset = file.View.ReadUInt32 (index_offset); + uint size = file.View.ReadUInt32 (index_offset+4); + int type = file.View.ReadInt32 (index_offset+0xC); + var name = file.View.ReadString (index_offset+0x20, 0x20); + var entry = FormatCatalog.Instance.Create (name); + entry.Offset = base_offset + offset; + entry.Size = size; + if (!entry.CheckPlacement (file.MaxOffset)) + return null; + dir.Add (entry); + index_offset += 0x40; + } + return new ArcFile (file, this, dir); + } + } +} diff --git a/Legacy/Gsx/ImageK2.cs b/Legacy/Gsx/ImageK2.cs new file mode 100644 index 00000000..091cd3d8 --- /dev/null +++ b/Legacy/Gsx/ImageK2.cs @@ -0,0 +1,116 @@ +//! \file ImageK2.cs +//! \date 2018 Feb 09 +//! \brief Toyo GSX image format. +// +// Copyright (C) 2018 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; +using System.ComponentModel.Composition; +using System.IO; +using GameRes.Utility; + +namespace GameRes.Formats.Gsx +{ + [Export(typeof(ImageFormat))] + public class K2Format : ImageFormat + { + public override string Tag { get { return "K2"; } } + public override string Description { get { return "Toyo GSX image format"; } } + public override uint Signature { get { return 0x18324B; } } // 'K2' + + public K2Format () + { + Signatures = new uint[] { 0x18324B, 0x20324B, 0x10324B, 0x0F324B, 0x08324B, 0x04324B, 0x01324B }; + } + + public override ImageMetaData ReadMetaData (IBinaryStream file) + { + var bmp_header = Decompress (file, 0x36); + using (var bmp = new BinMemoryStream (bmp_header)) + return Bmp.ReadMetaData (bmp); + } + + public override ImageData Read (IBinaryStream file, ImageMetaData info) + { + var bmp_data = Decompress (file); + using (var bmp = new BinMemoryStream (bmp_data)) + return Bmp.Read (bmp, info); + } + + public override void Write (Stream file, ImageData image) + { + throw new System.NotImplementedException ("K2Format.Write not implemented"); + } + + internal byte[] Decompress (IBinaryStream input) + { + input.Position = 6; + int unpacked_size = input.ReadInt32(); + return Decompress (input, unpacked_size); + } + + internal byte[] Decompress (IBinaryStream input, int unpacked_size) + { + input.Position = 0x12; + int data_pos = input.ReadInt32(); + int bits_length = Math.Min (data_pos-0x10, (unpacked_size + 7) / 8); + var ctl_bits = input.ReadBytes (bits_length); + input.Position = 6 + data_pos; + var output = new byte[unpacked_size]; + + using (var mem = new MemoryStream (ctl_bits)) + using (var bits = new MsbBitStream (mem)) + using (var data = new MsbBitStream (input.AsStream, true)) + { + int dst = 0; + while (dst < unpacked_size) + { + int ctl = bits.GetNextBit(); + if (-1 == ctl) + break; + if (ctl != 0) + { + output[dst++] = (byte)data.GetBits (8); + } + else + { + int offset, count; + if (bits.GetNextBit() != 0) + { + offset = data.GetBits (14); + count = data.GetBits (4) + 3; + } + else + { + offset = data.GetBits (9); + count = data.GetBits (3) + 2; + } + count = Math.Min (count, output.Length-dst); + Binary.CopyOverlapped (output, dst-offset-1, dst, count); + dst += count; + } + } + return output; + } + } + } +} diff --git a/Legacy/Legacy.csproj b/Legacy/Legacy.csproj index c1f0d020..1cbc5b1a 100644 --- a/Legacy/Legacy.csproj +++ b/Legacy/Legacy.csproj @@ -75,6 +75,8 @@ + +