From 4b5059311c0566a12eda7a5762975fa9e8917637 Mon Sep 17 00:00:00 2001 From: morkt Date: Wed, 16 Mar 2016 04:20:52 +0400 Subject: [PATCH] implemented EME archives. --- ArcFormats/ArcFormats.csproj | 2 + ArcFormats/EmonEngine/ArcEME.cs | 253 ++++++++++++++++++++++++++++++ ArcFormats/EmonEngine/ImageBMP.cs | 134 ++++++++++++++++ supported.html | 6 +- 4 files changed, 394 insertions(+), 1 deletion(-) create mode 100644 ArcFormats/EmonEngine/ArcEME.cs create mode 100644 ArcFormats/EmonEngine/ImageBMP.cs diff --git a/ArcFormats/ArcFormats.csproj b/ArcFormats/ArcFormats.csproj index 2fddbec8..b31327ce 100644 --- a/ArcFormats/ArcFormats.csproj +++ b/ArcFormats/ArcFormats.csproj @@ -75,6 +75,8 @@ + + diff --git a/ArcFormats/EmonEngine/ArcEME.cs b/ArcFormats/EmonEngine/ArcEME.cs new file mode 100644 index 00000000..692a0440 --- /dev/null +++ b/ArcFormats/EmonEngine/ArcEME.cs @@ -0,0 +1,253 @@ +//! \file ArcEME.cs +//! \date Tue Mar 15 08:13:00 2016 +//! \brief Emon Engine (えもんエンジン) resource archives. +// +// Copyright (C) 2016 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; +using GameRes.Compression; +using GameRes.Utility; + +namespace GameRes.Formats.EmonEngine +{ + [Export(typeof(ArchiveFormat))] + public class EmeOpener : ArchiveFormat + { + public override string Tag { get { return "EME"; } } + public override string Description { get { return "Emon Engine resource archive"; } } // 'えもんエンジン' + public override uint Signature { get { return 0x44455252; } } // 'RREDATA' + public override bool IsHierarchic { get { return false; } } + public override bool CanCreate { get { return false; } } + + public override ArcFile TryOpen (ArcView file) + { + if (!file.View.AsciiEqual (4, "ATA ")) + return null; + int count = file.View.ReadInt32 (file.MaxOffset-4); + if (!IsSaneCount (count)) + return null; + + uint index_size = (uint)count * 0x60; + var index_offset = file.MaxOffset - 4 - index_size; + var key = file.View.ReadBytes (index_offset - 40, 40); + var index = file.View.ReadBytes (index_offset, index_size); + + int current_offset = 0; + var dir = new List (count); + for (int i = 0; i < count; ++i) + { + Decrypt (index, current_offset, 0x60, key); + var name = Binary.GetCString (index, current_offset, 0x40); + var entry = FormatCatalog.Instance.Create (name); + entry.LzssFrameSize = LittleEndian.ToUInt16 (index, current_offset+0x40); + entry.LzssInitPos = LittleEndian.ToUInt16 (index, current_offset+0x42); + if (entry.LzssFrameSize != 0) + entry.LzssInitPos = (entry.LzssFrameSize - entry.LzssInitPos) % entry.LzssFrameSize; + entry.SubType = LittleEndian.ToInt32 (index, current_offset+0x48); + entry.Size = LittleEndian.ToUInt32 (index, current_offset+0x4C); + entry.UnpackedSize = LittleEndian.ToUInt32 (index, current_offset+0x50); + entry.Offset = LittleEndian.ToUInt32 (index, current_offset+0x54); + if (!entry.CheckPlacement (file.MaxOffset)) + return null; + if (3 == entry.SubType) + entry.Type = "script"; + else if (4 == entry.SubType) + entry.Type = "image"; + dir.Add (entry); + current_offset += 0x60; + } + return new EmeArchive (file, this, dir, key); + } + + public override Stream OpenEntry (ArcFile arc, Entry entry) + { + var ement = entry as EmEntry; + var emarc = arc as EmeArchive; + if (null == ement || null == emarc) + return base.OpenEntry (arc, entry); + if (3 == ement.SubType) + return OpenScript (emarc, ement); + else if (4 == ement.SubType) + return OpenImage (emarc, ement); + else if (5 == ement.SubType && entry.Size > 4) + return OpenT5 (emarc, ement); + else + return base.OpenEntry (arc, entry); + } + + Stream OpenScript (EmeArchive arc, EmEntry entry) + { + var header = arc.File.View.ReadBytes (entry.Offset, 12); + Decrypt (header, 0, 12, arc.Key); + if (0 == entry.LzssFrameSize) + { + var input = arc.File.CreateStream (entry.Offset+12, entry.Size); + return new PrefixStream (header, input); + } + int unpacked_size = LittleEndian.ToInt32 (header, 4); + if (0 != unpacked_size) + { + uint packed_size = LittleEndian.ToUInt32 (header, 0); + int part1_size = (int)entry.UnpackedSize - unpacked_size; + var data = new byte[entry.UnpackedSize]; + using (var input = arc.File.CreateStream (entry.Offset+12+packed_size, entry.Size)) + using (var lzss = new LzssStream (input)) + { + lzss.Config.FrameSize = entry.LzssFrameSize; + lzss.Config.FrameInitPos = entry.LzssInitPos; + lzss.Read (data, 0, part1_size); + } + using (var input = arc.File.CreateStream (entry.Offset+12, packed_size)) + using (var lzss = new LzssStream (input)) + { + lzss.Config.FrameSize = entry.LzssFrameSize; + lzss.Config.FrameInitPos = entry.LzssInitPos; + lzss.Read (data, part1_size, unpacked_size); + } + return new MemoryStream (data); + } + else + { + var input = arc.File.CreateStream (entry.Offset+12, entry.Size); + var lzss = new LzssStream (input); + lzss.Config.FrameSize = entry.LzssFrameSize; + lzss.Config.FrameInitPos = entry.LzssInitPos; + return lzss; + } + } + + Stream OpenImage (EmeArchive arc, EmEntry entry) + { + var header = new byte[40]; + Encoding.ASCII.GetBytes ("EMBM", 0, 4, header, 0); + LittleEndian.Pack ((ushort)entry.LzssFrameSize, header, 4); + LittleEndian.Pack ((ushort)entry.LzssInitPos, header, 6); + arc.File.View.Read (entry.Offset, header, 8, 32); + Decrypt (header, 8, 32, arc.Key); + uint entry_size = entry.Size; + uint colors = LittleEndian.ToUInt16 (header, 14); + if (0 != colors && header[0] != 7) + entry_size += Math.Max (colors, 3u) * 4; + var input = arc.File.CreateStream (entry.Offset+32, entry_size); + return new PrefixStream (header, input); + } + + Stream OpenT5 (EmeArchive arc, EmEntry entry) + { + var header = arc.File.View.ReadBytes (entry.Offset, 4); + Decrypt (header, 0, 4, arc.Key); + var input = arc.File.CreateStream (entry.Offset+4, entry.Size-4); + return new PrefixStream (header, input); + } + + internal static unsafe void Decrypt (byte[] buffer, int offset, int length, byte[] routine) + { + if (null == buffer) + throw new ArgumentNullException ("buffer", "Buffer cannot be null."); + if (offset < 0) + throw new ArgumentOutOfRangeException ("offset", "Buffer offset should be non-negative."); + if (buffer.Length - offset < length) + throw new ArgumentException ("Buffer offset and length are out of bounds."); + fixed (byte* data8 = &buffer[offset]) + { + uint* data32 = (uint*)data8; + int length32 = length / 4; + int key_index = routine.Length; + for (int i = 7; i >= 0; --i) + { + key_index -= 4; + uint key = LittleEndian.ToUInt32 (routine, key_index); + switch (routine[i]) + { + case 1: + for (int j = 0; j < length32; ++j) + data32[j] ^= key; + break; + case 2: + for (int j = 0; j < length32; ++j) + { + uint v = data32[j]; + data32[j] = v ^ key; + key = v; + } + break; + case 4: + for (int j = 0; j < length32; ++j) + data32[j] = ShiftValue (data32[j], key); + break; + case 8: + InitTable (buffer, offset, length, key); + break; + } + } + } + } + + static uint ShiftValue (uint val, uint key) + { + int shift = 0; + uint result = 0; + for (int i = 0; i < 32; ++i) + { + shift += (int)key; + result |= ((val >> i) & 1) << shift; + } + return result; + } + + static void InitTable (byte[] buffer, int offset, int length, uint key) + { + var table = new byte[length]; + int x = 0; + for (int i = 0; i < length; ++i) + { + x += (int)key; + while (x >= length) + x -= length; + table[x] = buffer[offset+i]; + } + Buffer.BlockCopy (table, 0, buffer, offset, length); + } + } + + internal class EmEntry : PackedEntry + { + public int LzssFrameSize; + public int LzssInitPos; + public int SubType; + } + + internal class EmeArchive : ArcFile + { + public readonly byte[] Key; + + public EmeArchive (ArcView arc, ArchiveFormat impl, ICollection dir, byte[] key) + : base (arc, impl, dir) + { + Key = key; + } + } +} diff --git a/ArcFormats/EmonEngine/ImageBMP.cs b/ArcFormats/EmonEngine/ImageBMP.cs new file mode 100644 index 00000000..9487c9e3 --- /dev/null +++ b/ArcFormats/EmonEngine/ImageBMP.cs @@ -0,0 +1,134 @@ +//! \file ImageBMP.cs +//! \date Wed Mar 16 01:08:47 2016 +//! \brief Emon Engine compressed images. +// +// Copyright (C) 2016 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 System.Windows.Media; +using System.Windows.Media.Imaging; +using GameRes.Compression; + +namespace GameRes.Formats.EmonEngine +{ + internal class EmMetaData : ImageMetaData + { + public int Colors; + public int Stride; + public int LzssFrameSize; + public int LzssInitPos; + public int DataOffset; + } + + [Export(typeof(ImageFormat))] + public class EmFormat : ImageFormat + { + public override string Tag { get { return "EM/BMP"; } } + public override string Description { get { return "Emon Engine image format"; } } + // this is an artificial prefix embedded into stream by EmeOpener.OpenImage + public override uint Signature { get { return 0x4D424D45; } } // 'EMBM' + + public EmFormat () + { + Extensions = new string[] { "bmp" }; + } + + public override ImageMetaData ReadMetaData (Stream stream) + { + using (var reader = new ArcView.Reader (stream)) + { + reader.ReadInt32(); + var info = new EmMetaData(); + info.LzssFrameSize = reader.ReadUInt16(); + info.LzssInitPos = reader.ReadUInt16(); + info.BPP = reader.ReadUInt16() & 0xFF; + info.Width = reader.ReadUInt16(); + info.Height = reader.ReadUInt16(); + info.Colors = reader.ReadUInt16(); + info.Stride = reader.ReadInt32(); + info.OffsetX = reader.ReadInt32(); + info.OffsetY = reader.ReadInt32(); + info.DataOffset = 40; + return info; + } + } + + public override ImageData Read (Stream stream, ImageMetaData info) + { + var meta = (EmMetaData)info; + stream.Position = meta.DataOffset; + BitmapPalette palette = null; + if (meta.Colors != 0) + palette = ReadPalette (stream, Math.Max (meta.Colors, 3)); + var pixels = new byte[meta.Stride * (int)info.Height]; + if (meta.LzssFrameSize != 0) + { + using (var lzss = new LzssStream (stream, LzssMode.Decompress, true)) + { + lzss.Config.FrameSize = meta.LzssFrameSize; + lzss.Config.FrameInitPos = meta.LzssInitPos; + if (pixels.Length != lzss.Read (pixels, 0, pixels.Length)) + throw new EndOfStreamException(); + } + } + else + { + if (pixels.Length != stream.Read (pixels, 0, pixels.Length)) + throw new EndOfStreamException(); + } + if (7 == meta.BPP) + return ImageData.Create (info, PixelFormats.Gray8, palette, pixels, meta.Stride); + + PixelFormat format; + if (32 == meta.BPP) + format = PixelFormats.Bgr32; + else if (24 == meta.BPP) + format = PixelFormats.Bgr24; + else + format = PixelFormats.Indexed8; + return ImageData.CreateFlipped (info, format, palette, pixels, meta.Stride); + } + + BitmapPalette ReadPalette (Stream input, int colors) + { + int palette_size = colors * 4; + var palette_data = new byte[palette_size]; + if (palette_size != input.Read (palette_data, 0, palette_size)) + throw new InvalidFormatException(); + var palette = new Color[colors]; + int src = 0; + for (int i = 0; i < palette.Length; ++i) + { + palette[i] = Color.FromRgb (palette_data[src], palette_data[src+1], palette_data[src+2]); + src += 4; + } + return new BitmapPalette (palette); + } + + public override void Write (Stream file, ImageData image) + { + throw new System.NotImplementedException ("EmFormat.Write not implemented"); + } + } +} diff --git a/supported.html b/supported.html index 118ddb6a..d1e30e66 100644 --- a/supported.html +++ b/supported.html @@ -406,9 +406,10 @@ Yatohime Zankikou
*.arc+*.ariWFL1NoKaGuYa Dungeon Crusaderz ~Tales of Demon Eater~
Dungeon Crusaderz 2 ~Eigou no Rakudo~
-Onna Kyoushi
+Inkou Haden Amatsu ~Hakudaku no Juin~
Magical Witch Academy
Medorei ~Okasareta Houkago~
+Onna Kyoushi
Saishuu Chikan Densha
Serina
Ura Nyuugaku ~Ineki ni Nureta Kyoukasho~
@@ -745,6 +746,9 @@ Angenehm Platz -Kleiner Garten Sie Erstellen-
*.arcARCCNo *.binODIONo +*.emeRREDATANoEmon Engine +Ase Nure Shoujo Misaki "Anata no Nioi de Icchau!"
+

1 Non-encrypted only