From fef6dd5bb10a1e55f781e3eb420cf4b485d32fd6 Mon Sep 17 00:00:00 2001 From: morkt Date: Tue, 5 Dec 2017 22:30:53 +0400 Subject: [PATCH] implemented CAF archives and CFP images. --- ArcFormats/Tail/ArcCAF.cs | 128 ++++++++++++++++++++++++++++++++++++ ArcFormats/Tail/ImageCFP.cs | 86 ++++++++++++++++++++++++ 2 files changed, 214 insertions(+) create mode 100644 ArcFormats/Tail/ArcCAF.cs create mode 100644 ArcFormats/Tail/ImageCFP.cs diff --git a/ArcFormats/Tail/ArcCAF.cs b/ArcFormats/Tail/ArcCAF.cs new file mode 100644 index 00000000..6c9f1a28 --- /dev/null +++ b/ArcFormats/Tail/ArcCAF.cs @@ -0,0 +1,128 @@ +//! \file ArcCAF.cs +//! \date 2017 Nov 30 +//! \brief Tail resource archive. +// +// Copyright (C) 2017 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 GameRes.Utility; + +namespace GameRes.Formats.Tail +{ + [Export(typeof(ArchiveFormat))] + public class CafOpener : ArchiveFormat + { + public override string Tag { get { return "CAF"; } } + public override string Description { get { return "Tail resource archive"; } } + public override uint Signature { get { return 0x30464143; } } // 'CAF0' + public override bool IsHierarchic { get { return true; } } + public override bool CanWrite { get { return false; } } + + public override ArcFile TryOpen (ArcView file) + { + int count = file.View.ReadInt32 (8); + if (!IsSaneCount (count)) + return null; + uint index_offset = file.View.ReadUInt32 (0xC); + uint index_size = file.View.ReadUInt32 (0x10); + uint names_offset = file.View.ReadUInt32 (0x14); + uint names_size = file.View.ReadUInt32 (0x18); + var names = file.View.ReadBytes (names_offset, names_size); + if (names.Length != names_size) + return null; + if (index_size > file.View.Reserve (index_offset, index_size)) + return null; + var dir_map = new Dictionary(); + long data_offset = names_offset + names_size; + var dir = new List (count); + for (int i = 0; i < count; ++i) + { + int dir_name_offset = file.View.ReadInt32 (index_offset+4); + int name_offset = file.View.ReadInt32 (index_offset+8); + var name = Binary.GetCString (names, name_offset); + if (dir_name_offset >= 0) + { + string dir_name; + if (!dir_map.TryGetValue (dir_name_offset, out dir_name)) + { + dir_name = Binary.GetCString (names, dir_name_offset); + dir_map[dir_name_offset] = dir_name; + } + name = Path.Combine (dir_name, name); + } + var entry = FormatCatalog.Instance.Create (name); + entry.Offset = file.View.ReadUInt32 (index_offset+0xC) + data_offset; + entry.Size = file.View.ReadUInt32 (index_offset+0x10); + if (!entry.CheckPlacement (file.MaxOffset)) + return null; + dir.Add (entry); + index_offset += 0x14; + } + return new ArcFile (file, this, dir); + } + + public override Stream OpenEntry (ArcFile arc, Entry entry) + { + if (!arc.File.View.AsciiEqual (entry.Offset, "PREN")) + return base.OpenEntry (arc, entry); + using (var input = arc.File.CreateStream (entry.Offset, entry.Size)) + { + var data = UnpackPren (input); + return new BinMemoryStream (data, entry.Name); + } + } + + byte[] UnpackPren (IBinaryStream input) + { + input.Position = 8; + int unpacked_size = input.ReadInt32(); + byte rle_code = input.ReadUInt8(); + input.Seek (3, SeekOrigin.Current); + var output = new byte[unpacked_size]; + int dst = 0; + while (dst < output.Length) + { + int v = input.ReadByte(); + if (-1 == v) + break; + if (rle_code == v) + { + byte count = input.ReadUInt8(); + byte x = rle_code; + if (count > 2) + x = input.ReadUInt8(); + + while (count --> 0) + output[dst++] = x; + } + else + { + output[dst++] = (byte)v; + } + } + return output; + } + } +} diff --git a/ArcFormats/Tail/ImageCFP.cs b/ArcFormats/Tail/ImageCFP.cs new file mode 100644 index 00000000..a0d31b7c --- /dev/null +++ b/ArcFormats/Tail/ImageCFP.cs @@ -0,0 +1,86 @@ +//! \file ImageCFP.cs +//! \date 2017 Nov 30 +//! \brief Tail image format. +// +// Copyright (C) 2017 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; + +namespace GameRes.Formats.Tail +{ + [Export(typeof(ImageFormat))] + public class CfpFormat : ImageFormat + { + public override string Tag { get { return "CFP"; } } + public override string Description { get { return "Tail image format"; } } + public override uint Signature { get { return 0x20424552; } } // 'REB ' + + public override ImageMetaData ReadMetaData (IBinaryStream file) + { + var header = file.ReadHeader (0x1C); + return new ImageMetaData { + Width = header.ToUInt32 (0x14), + Height = header.ToUInt32 (0x18), + BPP = 24, + }; + } + + public override ImageData Read (IBinaryStream file, ImageMetaData info) + { + file.Position = 0x1C; + int stride = ((int)info.Width * 3 + 3) & -4; + int width = ((int)info.Width + 1) & -2; + int height = (int)info.Height; + int total = width * height; + var input = file.ReadBytes (total * 3); + var pixels = new byte[stride*height]; + int src1 = 0; + int src2 = total / 2; + int src3 = total; + int src4 = src2 + total; + int src5 = total * 2; + int src6 = src5 + total / 2; + for (int dst_row = stride * (height - 1); dst_row >= 0; dst_row -= stride) + { + int dst = dst_row; + for (int x = 0; x < width; x += 2) + { + pixels[dst ] = (byte)(input[src2 ] & 0x0F | input[src1 ] << 4); + pixels[dst+3] = (byte)(input[src1++] & 0xF0 | input[src2++] >> 4); + pixels[dst+1] = (byte)(input[src4 ] & 0x0F | input[src3 ] << 4); + pixels[dst+4] = (byte)(input[src3++] & 0xF0 | input[src4++] >> 4); + pixels[dst+2] = (byte)(input[src6 ] & 0x0F | input[src5 ] << 4); + pixels[dst+5] = (byte)(input[src5++] & 0xF0 | input[src6++] >> 4); + dst += 6; + } + } + return ImageData.Create (info, PixelFormats.Bgr24, null, pixels, stride); + } + + public override void Write (Stream file, ImageData image) + { + throw new System.NotImplementedException ("CfpFormat.Write not implemented"); + } + } +}