From 666c8f4f14167e97f109a70ee3822bfd721b0499 Mon Sep 17 00:00:00 2001 From: morkt Date: Fri, 21 Sep 2018 00:14:34 +0400 Subject: [PATCH] (Legacy): Lazycrew archives. --- Legacy/Lazycrew/ArcDAT.cs | 212 ++++++++++++++++++++++++++++++++++++ Legacy/Lazycrew/ImageDAT.cs | 201 ++++++++++++++++++++++++++++++++++ Legacy/Legacy.csproj | 4 +- 3 files changed, 416 insertions(+), 1 deletion(-) create mode 100644 Legacy/Lazycrew/ArcDAT.cs create mode 100644 Legacy/Lazycrew/ImageDAT.cs diff --git a/Legacy/Lazycrew/ArcDAT.cs b/Legacy/Lazycrew/ArcDAT.cs new file mode 100644 index 00000000..59b2580e --- /dev/null +++ b/Legacy/Lazycrew/ArcDAT.cs @@ -0,0 +1,212 @@ +//! \file ArcDAT.cs +//! \date 2018 Sep 20 +//! \brief Lazycrew 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; +using System.Collections.Generic; +using System.ComponentModel.Composition; +using System.IO; +using System.Text.RegularExpressions; +using GameRes.Utility; + +// [031024][Lazycrew] Sister Mermaid + +namespace GameRes.Formats.Lazycrew +{ + internal class DirEntry + { + public string Name; + public int Offset; + public int Count; + } + + [Export(typeof(ArchiveFormat))] + public class DatOpener : ArchiveFormat + { + public override string Tag { get { return "DAT/LAZYCREW"; } } + public override string Description { get { return "Lazycrew resource archive"; } } + public override uint Signature { get { return 0; } } + public override bool IsHierarchic { get { return true; } } + public override bool CanWrite { get { return false; } } + + public DatOpener () + { + Signatures = new uint[] { 2, 0 }; + } + + static readonly Regex NamePattern = new Regex (@"^(0\d\d\d)\.dat$", RegexOptions.IgnoreCase); + + const uint DefaultPcmKey = 0x4B5AB4A5; + + public override ArcFile TryOpen (ArcView file) + { + var name = Path.GetFileName (file.Name); + if (!NamePattern.IsMatch (name)) + return null; + var match = NamePattern.Match (name); + int name_id = Int32.Parse (match.Groups[1].Value); + if (name_id < 1) + return null; + ArcView index = file; + try + { + if (name_id != 1) + { + var index_name = VFS.ChangeFileName (file.Name, "0001.dat"); + if (!VFS.FileExists (index_name)) + return null; + index = VFS.OpenView (index_name); + } + var dir = ReadIndex (index, name_id, file.MaxOffset); + if (null == dir || 0 == dir.Count) + return null; + return new ArcFile (file, this, dir); + } + finally + { + if (index != file) + index.Dispose(); + } + } + + List ReadIndex (ArcView index, int arc_id, long arc_length) + { + int dir_count = index.View.ReadInt32 (0); + if (dir_count <= 0 || dir_count > 20) + return null; + var dir_list = new List (dir_count); + int dir_offset = 4; + int first_offset = dir_count * 0x10 + 4; + int file_count = 0; + for (int i = 0; i < dir_count; ++i) + { + var dir_entry = new DirEntry { + Name = index.View.ReadString (dir_offset, 8), + Offset = index.View.ReadInt32 (dir_offset+8), + Count = index.View.ReadInt32 (dir_offset+12), + }; + if (dir_entry.Offset < first_offset || dir_entry.Offset >= index.MaxOffset + || !IsSaneCount (dir_entry.Count)) + return null; + file_count += dir_entry.Count; + dir_list.Add (dir_entry); + dir_offset += 16; + } + var file_list = new List (file_count); + foreach (var dir in dir_list) + { + dir_offset = dir.Offset; + string type = ""; + if (dir.Name.Equals ("image", StringComparison.OrdinalIgnoreCase)) + type = "image"; + else if (dir.Name.Equals ("sound", StringComparison.OrdinalIgnoreCase)) + type = "audio"; + for (int i = 0; i < dir.Count; ++i) + { + int id = index.View.ReadUInt16 (dir_offset); + if (id == arc_id) + { + var entry = new Entry { + Name = Path.Combine (dir.Name, i.ToString ("D5")), + Type = type, + Offset = index.View.ReadUInt32 (dir_offset+2), + Size = index.View.ReadUInt32 (dir_offset+6), + }; + if (!entry.CheckPlacement (arc_length)) + return null; + file_list.Add (entry); + } + dir_offset += 10; + } + } + return file_list; + } + + public override Stream OpenEntry (ArcFile arc, Entry entry) + { + if (entry.Type == "audio" && arc.File.View.ReadUInt32 (entry.Offset) == 0) + return OpenAudio (arc, entry); + return base.OpenEntry (arc, entry); + } + + Stream OpenAudio (ArcFile arc, Entry entry) + { + var riff_header = new byte[0x2C]; + LittleEndian.Pack (AudioFormat.Wav.Signature, riff_header, 0); + LittleEndian.Pack (0x45564157, riff_header, 8); // 'WAVE' + LittleEndian.Pack (0x20746d66, riff_header, 12); // 'fmt ' + LittleEndian.Pack (0x10, riff_header, 16); + arc.File.View.Read (entry.Offset+4, riff_header, 20, 0x10); + LittleEndian.Pack (0x61746164, riff_header, 0x24); // 'data' + uint data_size = arc.File.View.ReadUInt32 (entry.Offset+0x16); + LittleEndian.Pack (data_size + 0x24u, riff_header, 4); + LittleEndian.Pack (data_size, riff_header, 0x28); + var pcm = arc.File.View.ReadBytes (entry.Offset+0x1A, data_size); + DecryptData (pcm, DefaultPcmKey); + var riff_data = new MemoryStream (pcm); + return new PrefixStream (riff_header, riff_data); + } + + public override IImageDecoder OpenImage (ArcFile arc, Entry entry) + { + var input = arc.OpenBinaryEntry (entry); + int fmt = (int)input.Signature & 0xFFFF; + if (fmt > 4) + return ImageFormatDecoder.Create (input); + var header = input.ReadHeader (14); + input.Position = 0; + int hpos = 2; + if (1 == fmt || 3 == fmt) + hpos = 8; + int cmp = header.ToUInt16 (hpos); + int bpp = 0; + switch (cmp & 0xFF) + { + case 2: bpp = 4; break; + case 3: bpp = 8; break; + case 5: bpp = 24; break; + default: + return ImageFormatDecoder.Create (input); + } + var info = new LcImageMetaData { + Width = header.ToUInt16 (hpos+2), + Height = header.ToUInt16 (hpos+4), + BPP = bpp, + Format = fmt, + Compression = cmp, + DataOffset = hpos + 6, + }; + return new LcImageDecoder (input, info); + } + + void DecryptData (byte[] data, uint key) + { + for (int i = 0; i < data.Length; ++i) + { + data[i] ^= (byte)key; + key = data[i] ^ ((key << 9) | (key >> 23) & 0x1F0); + } + } + } +} diff --git a/Legacy/Lazycrew/ImageDAT.cs b/Legacy/Lazycrew/ImageDAT.cs new file mode 100644 index 00000000..958ffaa3 --- /dev/null +++ b/Legacy/Lazycrew/ImageDAT.cs @@ -0,0 +1,201 @@ +//! \file ImageDAT.cs +//! \date 2018 Sep 20 +//! \brief Lazycrew 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.ComponentModel.Composition; +using System.IO; +using System.Windows.Media; +using System.Windows.Media.Imaging; + +namespace GameRes.Formats.Lazycrew +{ + internal class LcImageMetaData : ImageMetaData + { + public int Format; + public int Compression; + public int DataOffset; + + public bool IsCompressed { get { return (Compression & 0x8000) != 0; } } + public bool HasAlpha { get { return Format == 2 || Format == 3; } } + } + + internal class LcImageDecoder : BinaryImageDecoder + { + LcImageMetaData m_info; + int m_stride; + BitmapPalette m_palette; + + public LcImageDecoder (IBinaryStream input, LcImageMetaData info) : base (input, info) + { + m_info = info; + } + + protected override ImageData GetImageData () + { + m_input.Position = m_info.DataOffset; + if (m_info.BPP <= 8) + { + int colors = m_input.ReadUInt16(); + if (m_info.Format != 4) + m_palette = ImageFormat.ReadPalette (m_input.AsStream, colors); + else // grayscale image - ignore palette + m_input.Seek (colors * 4, SeekOrigin.Current); + } + m_stride = (int)m_info.Width * m_info.BPP / 8; + var pixels = new byte[m_stride * (int)m_info.Height]; + + if (24 == m_info.BPP) + Unpack24bpp (pixels); + else if (m_info.IsCompressed) + UnpackRle (pixels); + else + m_input.Read (pixels, 0, pixels.Length); + + if (m_info.HasAlpha && m_info.BPP != 4) + { + var header = m_input.ReadBytes (8); + bool is_compressed = (header[1] & 0x80) != 0; + int colors = header.ToUInt16 (6); + m_input.Seek (colors * 4, SeekOrigin.Current); + var alpha = new byte[(int)m_info.Width * (int)m_info.Height]; + if (is_compressed) + UnpackRle (alpha); + else + m_input.Read (alpha, 0, pixels.Length); + return ApplyAlpha (pixels, alpha); + } + return ImageData.Create (m_info, GetPixelFormat(), m_palette, pixels, m_stride); + } + + ImageData ApplyAlpha (byte[] rgb, byte[] alpha) + { + int stride = (int)m_info.Width * 4; + var output = new byte[stride * (int)m_info.Height]; + int dst = 0; + int src = 0; + int asrc = 0; + if (8 == m_info.BPP) + { + var color_map = m_palette.Colors; + while (dst < output.Length) + { + var color = color_map[rgb[src++]]; + output[dst++] = color.B; + output[dst++] = color.G; + output[dst++] = color.R; + output[dst++] = alpha[asrc++]; + } + } + else + { + while (dst < output.Length) + { + output[dst++] = rgb[src++]; + output[dst++] = rgb[src++]; + output[dst++] = rgb[src++]; + output[dst++] = alpha[asrc++]; + } + } + m_info.BPP = 32; + return ImageData.Create (m_info, PixelFormats.Bgra32, null, output, stride); + } + + void UnpackRle (byte[] output) + { + int dst = 0; + while (dst < output.Length) + { + int count = m_input.ReadByte(); + if (-1 == count) + break; + if (count > 0x7F) + { + count = (count & 0x7F) + 1; + byte v = m_input.ReadUInt8(); + for (int i = 0; i < count; ++i) + output[dst+i] = v; + } + else + { + m_input.Read (output, dst, ++count); + } + dst += count; + } + } + + void Unpack24bpp (byte[] output) + { + var color = new byte[6]; + int dst = 0; + while (dst < output.Length) + { + int dst1 = dst + m_stride; + for (int x = 0; x < m_stride; x += 6) + { + if (m_input.Read (color, 0, 6) < 6) + return; + int r = (10638 * (sbyte)color[1] + 18651 * (sbyte)color[0]) >> 14; + int b = (29145 * (sbyte)color[1] - 21601 * (sbyte)color[0]) >> 14; + int g = (-5312 * (sbyte)color[0] - 11083 * (sbyte)color[1]) >> 14; + output[dst++] = RgbClamp (color[2] + b); + output[dst++] = RgbClamp (color[2] + g); + output[dst++] = RgbClamp (color[2] + r); + output[dst++] = RgbClamp (color[3] + b); + output[dst++] = RgbClamp (color[3] + g); + output[dst++] = RgbClamp (color[3] + r); + output[dst1++] = RgbClamp (color[4] + b); + output[dst1++] = RgbClamp (color[4] + g); + output[dst1++] = RgbClamp (color[4] + r); + output[dst1++] = RgbClamp (color[5] + b); + output[dst1++] = RgbClamp (color[5] + g); + output[dst1++] = RgbClamp (color[5] + r); + } + dst += m_stride; + } + } + + static byte RgbClamp (int color) + { + if (color < 0) + return 0; + else if (color > 0xFF) + return 0xFF; + else + return (byte)color; + } + + PixelFormat GetPixelFormat () + { + if (4 == m_info.Format) + return PixelFormats.Gray8; + switch (m_info.BPP) + { + case 4: return PixelFormats.Indexed4; + case 8: return PixelFormats.Indexed8; + case 24: return PixelFormats.Bgr24; + default: throw new InvalidFormatException(); + } + } + } +} diff --git a/Legacy/Legacy.csproj b/Legacy/Legacy.csproj index 6c29b91f..50470d80 100644 --- a/Legacy/Legacy.csproj +++ b/Legacy/Legacy.csproj @@ -70,11 +70,14 @@ + + + @@ -208,7 +211,6 @@ -