From debd814596372bebdadd8cc192e1f91b51f4f669 Mon Sep 17 00:00:00 2001 From: rewjx Date: Sat, 12 Feb 2022 21:33:18 +0800 Subject: [PATCH 1/3] update_kaguya --- ArcFormats/Kaguya/ArcLINK.cs | 13 +++ ArcFormats/Kaguya/ArcPL10.cs | 207 +++++++++++++++++++++++++++++++++++ 2 files changed, 220 insertions(+) create mode 100644 ArcFormats/Kaguya/ArcPL10.cs diff --git a/ArcFormats/Kaguya/ArcLINK.cs b/ArcFormats/Kaguya/ArcLINK.cs index 0514d70e..6e3dfbe6 100644 --- a/ArcFormats/Kaguya/ArcLINK.cs +++ b/ArcFormats/Kaguya/ArcLINK.cs @@ -649,6 +649,7 @@ namespace GameRes.Formats.Kaguya { table.Add (new Tuple ("AN00", (a, e) => DecryptAn00 (a, e))); table.Add (new Tuple ("AN21", (a, e) => DecryptAn21 (a, e))); + table.Add (new Tuple ("PL10", (a, e) => DecryptPL10 (a, e))); } m_type_table = table.ToArray(); } @@ -716,6 +717,18 @@ namespace GameRes.Formats.Kaguya offset += 12; DecryptData (data, offset, channels * w * h); return new BinMemoryStream (data, entry.Name); + } + + Stream DecryptPL10(LinkArchive arc, LinkEntry entry) + { + var data = arc.File.View.ReadBytes(entry.Offset, entry.Size); + int offset = 30; + int w = data.ToInt32(offset); + int h = data.ToInt32(offset + 4); + int channels = data.ToInt32(offset + 8); + offset += 12; + DecryptData(data, offset, channels * w * h); + return new BinMemoryStream(data, entry.Name); } void DecryptData (byte[] data, int index, int length) diff --git a/ArcFormats/Kaguya/ArcPL10.cs b/ArcFormats/Kaguya/ArcPL10.cs new file mode 100644 index 00000000..65d60578 --- /dev/null +++ b/ArcFormats/Kaguya/ArcPL10.cs @@ -0,0 +1,207 @@ +//! \file ArcAN21.cs +//! \date Sun Apr 30 21:04:25 2017 +//! \brief KaGuYa script engine animation resource. +// +// 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 System.Linq; +using System.Windows.Media; + +namespace GameRes.Formats.Kaguya +{ + class PL10Entry : PackedEntry + { + public int FrameIndex; + public int RleStep; + } + + [Export(typeof(ArchiveFormat))] + public class PL10Opener : ArchiveFormat + { + public override string Tag { get { return "PL10/KAGUYA"; } } + public override string Description { get { return "KaGuYa script engine animation resource"; } } + public override uint Signature { get { return 0x30314C50; } } // 'PL10' + public override bool IsHierarchic { get { return false; } } + public override bool CanWrite { get { return false; } } + + public PL10Opener() + { + Extensions = new string[] { "plt" }; + } + + public override ArcFile TryOpen(ArcView file) + { + uint current_offset = 4; + int frame_count = file.View.ReadUInt16(current_offset); + if (!IsSaneCount(frame_count)) + return null; + current_offset += 0x12; + string base_name = Path.GetFileNameWithoutExtension(file.Name); + var dir = new List(frame_count); + var info = new ImageMetaData + { + OffsetX = file.View.ReadInt32(current_offset), + OffsetY = file.View.ReadInt32(current_offset + 4), + Width = file.View.ReadUInt32(current_offset + 8), + Height = file.View.ReadUInt32(current_offset + 12), + }; + int channels = file.View.ReadInt32(current_offset + 0x10); + info.BPP = channels * 8; + current_offset += 0x14; + var entry = new PL10Entry + { + FrameIndex = 0, + Name = string.Format("{0}#{1:D2}", base_name, 0), + Type = "image", + Offset = current_offset, + Size = (uint)channels * info.Width * info.Height, + IsPacked = false, + }; + dir.Add(entry); + current_offset += entry.Size; + for (int i = 1; i < frame_count; ++i) + { + int step = file.View.ReadByte(current_offset++); + if (0 == step) + return null; + uint packed_size = file.View.ReadUInt32(current_offset); + uint unpacked_size = (uint)(channels * (info.OffsetX + (int)info.Width) + * (info.OffsetY + (int)info.Height)); + current_offset += 4; + entry = new PL10Entry + { + FrameIndex = i, + Name = string.Format("{0}#{1:D2}", base_name, i), + Type = "image", + Offset = current_offset, + Size = packed_size, + UnpackedSize = unpacked_size, + IsPacked = true, + RleStep = step, + }; + dir.Add(entry); + current_offset += packed_size; + } + return new PL10Archive(file, this, dir, info); + } + + public override Stream OpenEntry(ArcFile arc, Entry entry) + { + var anent = entry as PL10Entry; + var input = arc.File.CreateStream(entry.Offset, entry.Size); + if (null == anent || !anent.IsPacked) + return input; + using (input) + { + var data = DecompressRLE(input, anent.UnpackedSize, anent.RleStep); + return new BinMemoryStream(data); + } + } + + public override IImageDecoder OpenImage(ArcFile arc, Entry entry) + { + var anarc = (PL10Archive)arc; + var anent = (PL10Entry)entry; + var pixels = anarc.GetFrame(anent.FrameIndex); + return new BitmapDecoder(pixels, anarc.ImageInfo); + } + + internal static byte[] DecompressRLE(IBinaryStream input, uint unpacked_size, int rle_step) + { + var output = new byte[unpacked_size]; + for (int i = 0; i < rle_step; ++i) + { + byte v1 = input.ReadUInt8(); + output[i] = v1; + int dst = i + rle_step; + while (dst < output.Length) + { + byte v2 = input.ReadUInt8(); + output[dst] = v2; + dst += rle_step; + if (v2 == v1) + { + int count = input.ReadUInt8(); + if (0 != (count & 0x80)) + count = input.ReadUInt8() + ((count & 0x7F) << 8) + 128; + while (count-- > 0 && dst < output.Length) + { + output[dst] = v2; + dst += rle_step; + } + if (dst < output.Length) + { + v2 = input.ReadUInt8(); + output[dst] = v2; + dst += rle_step; + } + } + v1 = v2; + } + } + return output; + } + } + + class PL10Archive : ArcFile + { + byte[][] Frames; + + public readonly ImageMetaData ImageInfo; + + public PL10Archive(ArcView arc, ArchiveFormat impl, ICollection dir, ImageMetaData base_info) + : base(arc, impl, dir) + { + Frames = new byte[dir.Count][]; + ImageInfo = base_info; + } + + public byte[] GetFrame(int index) + { + if (index >= Frames.Length) + throw new ArgumentException("index"); + if (null != Frames[index]) + return Frames[index]; + + var entry = Dir.ElementAt(index); + byte[] pixels; + using (var stream = OpenEntry(entry)) + { + pixels = new byte[stream.Length]; + stream.Read(pixels, 0, pixels.Length); + } + if (index > 0) + { + var prev_frame = GetFrame(index - 1); + for (int i = 0; i < pixels.Length; ++i) + pixels[i] += prev_frame[i]; + } + Frames[index] = pixels; + return pixels; + } + } + +} From 01a2b34c5ddab1d50d6c3c704299481e40f0440e Mon Sep 17 00:00:00 2001 From: rewjx Date: Sun, 13 Feb 2022 00:55:33 +0800 Subject: [PATCH 2/3] support_kaguya_PL_arc --- ArcFormats/ArcFormats.csproj | 2 + ArcFormats/Kaguya/ArcLINK.cs | 20 ++ ArcFormats/Kaguya/ArcPL00.cs | 126 +++++++++++ ArcFormats/Kaguya/ArcPL10.cs | 414 +++++++++++++++++------------------ 4 files changed, 355 insertions(+), 207 deletions(-) create mode 100644 ArcFormats/Kaguya/ArcPL00.cs diff --git a/ArcFormats/ArcFormats.csproj b/ArcFormats/ArcFormats.csproj index d12363f1..ebf94c3e 100644 --- a/ArcFormats/ArcFormats.csproj +++ b/ArcFormats/ArcFormats.csproj @@ -215,6 +215,8 @@ + + diff --git a/ArcFormats/Kaguya/ArcLINK.cs b/ArcFormats/Kaguya/ArcLINK.cs index 6e3dfbe6..1cb96f84 100644 --- a/ArcFormats/Kaguya/ArcLINK.cs +++ b/ArcFormats/Kaguya/ArcLINK.cs @@ -649,6 +649,7 @@ namespace GameRes.Formats.Kaguya { table.Add (new Tuple ("AN00", (a, e) => DecryptAn00 (a, e))); table.Add (new Tuple ("AN21", (a, e) => DecryptAn21 (a, e))); + table.Add (new Tuple ("PL00", (a, e) => DecryptPL00 (a, e))); table.Add (new Tuple ("PL10", (a, e) => DecryptPL10 (a, e))); } m_type_table = table.ToArray(); @@ -719,6 +720,25 @@ namespace GameRes.Formats.Kaguya return new BinMemoryStream (data, entry.Name); } + Stream DecryptPL00(LinkArchive arc, LinkEntry entry) + { + var data = arc.File.View.ReadBytes(entry.Offset, entry.Size); + int count = data.ToUInt16(4); + int offset = 22; + for(int i = 0; i < count; ++i) + { + offset += 8; + int w = data.ToInt32(offset); + int h = data.ToInt32(offset + 4); + int channels = data.ToInt32(offset + 8); + int size = channels * w * h; + offset += 12; + DecryptData(data, offset, size); + offset += size; + } + return new BinMemoryStream(data, entry.Name); + } + Stream DecryptPL10(LinkArchive arc, LinkEntry entry) { var data = arc.File.View.ReadBytes(entry.Offset, entry.Size); diff --git a/ArcFormats/Kaguya/ArcPL00.cs b/ArcFormats/Kaguya/ArcPL00.cs new file mode 100644 index 00000000..e35e5bce --- /dev/null +++ b/ArcFormats/Kaguya/ArcPL00.cs @@ -0,0 +1,126 @@ +//! \file ArcAN21.cs +//! \date Sun Apr 30 21:04:25 2017 +//! \brief KaGuYa script engine animation resource. +// +// 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 System.Linq; +using System.Windows.Media; + +namespace GameRes.Formats.Kaguya +{ + class PL00Entry : PackedEntry + { + public int FrameIndex; + public ImageMetaData ImageInfo; + } + + [Export(typeof(ArchiveFormat))] + public class PL00Opener : ArchiveFormat + { + public override string Tag { get { return "PL00/KAGUYA"; } } + public override string Description { get { return "KaGuYa script engine animation resource"; } } + public override uint Signature { get { return 0x30304C50; } } // 'PL00' + public override bool IsHierarchic { get { return false; } } + public override bool CanWrite { get { return false; } } + + public PL00Opener() + { + Extensions = new string[] { "plt" }; + } + + public override ArcFile TryOpen(ArcView file) + { + uint current_offset = 4; + int frame_count = file.View.ReadUInt16(current_offset); + if (!IsSaneCount(frame_count)) + return null; + current_offset += 2; + string base_name = Path.GetFileNameWithoutExtension(file.Name); + var dir = new List(frame_count); + var info = new ImageMetaData + { + OffsetX = file.View.ReadInt32(current_offset), + OffsetY = file.View.ReadInt32(current_offset + 4), + Width = file.View.ReadUInt32(current_offset + 8), + Height = file.View.ReadUInt32(current_offset + 12), + }; + int channels = file.View.ReadInt32(38); + info.BPP = channels * 8; + current_offset += 16; + for (int i = 0; i < frame_count; ++i) + { + int offsetx = file.View.ReadInt32(current_offset); + int offsety = file.View.ReadInt32(current_offset + 4); + uint width = file.View.ReadUInt32(current_offset + 8); + uint height = file.View.ReadUInt32(current_offset + 12); + channels = file.View.ReadInt32(current_offset + 16); + uint size = (uint)(width * height * channels); + current_offset += 20; + var entry = new PL00Entry + { + FrameIndex = i, + Name = string.Format("{0}#{1:D2}", base_name, i), + Type = "image", + Offset = current_offset, + Size = size, + IsPacked = false, + ImageInfo = new ImageMetaData + { + OffsetX = offsetx, + OffsetY = offsety, + Width = width, + Height = height, + BPP = channels * 8, + } + }; + dir.Add(entry); + current_offset += size; + } + return new PL00Archive(file, this, dir, info); + } + + public override IImageDecoder OpenImage(ArcFile arc, Entry entry) + { + var anent = (PL00Entry)entry; + var input = arc.File.CreateStream(entry.Offset, entry.Size); + var pixels = input.ReadBytes((int)anent.Size); + return new BitmapDecoder(pixels, anent.ImageInfo); + } + } + + class PL00Archive : ArcFile + { + public readonly ImageMetaData ImageInfo; + + public PL00Archive(ArcView arc, ArchiveFormat impl, ICollection dir, ImageMetaData base_info) + : base(arc, impl, dir) + { + ImageInfo = base_info; + } + } + +} diff --git a/ArcFormats/Kaguya/ArcPL10.cs b/ArcFormats/Kaguya/ArcPL10.cs index 65d60578..5b70bc4b 100644 --- a/ArcFormats/Kaguya/ArcPL10.cs +++ b/ArcFormats/Kaguya/ArcPL10.cs @@ -1,207 +1,207 @@ -//! \file ArcAN21.cs -//! \date Sun Apr 30 21:04:25 2017 -//! \brief KaGuYa script engine animation resource. -// -// 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 System.Linq; -using System.Windows.Media; - -namespace GameRes.Formats.Kaguya -{ - class PL10Entry : PackedEntry - { - public int FrameIndex; - public int RleStep; - } - - [Export(typeof(ArchiveFormat))] - public class PL10Opener : ArchiveFormat - { - public override string Tag { get { return "PL10/KAGUYA"; } } - public override string Description { get { return "KaGuYa script engine animation resource"; } } - public override uint Signature { get { return 0x30314C50; } } // 'PL10' - public override bool IsHierarchic { get { return false; } } - public override bool CanWrite { get { return false; } } - - public PL10Opener() - { - Extensions = new string[] { "plt" }; - } - - public override ArcFile TryOpen(ArcView file) - { - uint current_offset = 4; - int frame_count = file.View.ReadUInt16(current_offset); - if (!IsSaneCount(frame_count)) - return null; - current_offset += 0x12; - string base_name = Path.GetFileNameWithoutExtension(file.Name); - var dir = new List(frame_count); - var info = new ImageMetaData - { - OffsetX = file.View.ReadInt32(current_offset), - OffsetY = file.View.ReadInt32(current_offset + 4), - Width = file.View.ReadUInt32(current_offset + 8), - Height = file.View.ReadUInt32(current_offset + 12), - }; - int channels = file.View.ReadInt32(current_offset + 0x10); - info.BPP = channels * 8; - current_offset += 0x14; - var entry = new PL10Entry - { - FrameIndex = 0, - Name = string.Format("{0}#{1:D2}", base_name, 0), - Type = "image", - Offset = current_offset, - Size = (uint)channels * info.Width * info.Height, - IsPacked = false, - }; - dir.Add(entry); - current_offset += entry.Size; - for (int i = 1; i < frame_count; ++i) - { - int step = file.View.ReadByte(current_offset++); - if (0 == step) - return null; - uint packed_size = file.View.ReadUInt32(current_offset); - uint unpacked_size = (uint)(channels * (info.OffsetX + (int)info.Width) - * (info.OffsetY + (int)info.Height)); - current_offset += 4; - entry = new PL10Entry - { - FrameIndex = i, - Name = string.Format("{0}#{1:D2}", base_name, i), - Type = "image", - Offset = current_offset, - Size = packed_size, - UnpackedSize = unpacked_size, - IsPacked = true, - RleStep = step, - }; - dir.Add(entry); - current_offset += packed_size; - } - return new PL10Archive(file, this, dir, info); - } - - public override Stream OpenEntry(ArcFile arc, Entry entry) - { - var anent = entry as PL10Entry; - var input = arc.File.CreateStream(entry.Offset, entry.Size); - if (null == anent || !anent.IsPacked) - return input; - using (input) - { - var data = DecompressRLE(input, anent.UnpackedSize, anent.RleStep); - return new BinMemoryStream(data); - } - } - - public override IImageDecoder OpenImage(ArcFile arc, Entry entry) - { - var anarc = (PL10Archive)arc; - var anent = (PL10Entry)entry; - var pixels = anarc.GetFrame(anent.FrameIndex); - return new BitmapDecoder(pixels, anarc.ImageInfo); - } - - internal static byte[] DecompressRLE(IBinaryStream input, uint unpacked_size, int rle_step) - { - var output = new byte[unpacked_size]; - for (int i = 0; i < rle_step; ++i) - { - byte v1 = input.ReadUInt8(); - output[i] = v1; - int dst = i + rle_step; - while (dst < output.Length) - { - byte v2 = input.ReadUInt8(); - output[dst] = v2; - dst += rle_step; - if (v2 == v1) - { - int count = input.ReadUInt8(); - if (0 != (count & 0x80)) - count = input.ReadUInt8() + ((count & 0x7F) << 8) + 128; - while (count-- > 0 && dst < output.Length) - { - output[dst] = v2; - dst += rle_step; - } - if (dst < output.Length) - { - v2 = input.ReadUInt8(); - output[dst] = v2; - dst += rle_step; - } - } - v1 = v2; - } - } - return output; - } - } - - class PL10Archive : ArcFile - { - byte[][] Frames; - - public readonly ImageMetaData ImageInfo; - - public PL10Archive(ArcView arc, ArchiveFormat impl, ICollection dir, ImageMetaData base_info) - : base(arc, impl, dir) - { - Frames = new byte[dir.Count][]; - ImageInfo = base_info; - } - - public byte[] GetFrame(int index) - { - if (index >= Frames.Length) - throw new ArgumentException("index"); - if (null != Frames[index]) - return Frames[index]; - - var entry = Dir.ElementAt(index); - byte[] pixels; - using (var stream = OpenEntry(entry)) - { - pixels = new byte[stream.Length]; - stream.Read(pixels, 0, pixels.Length); - } - if (index > 0) - { - var prev_frame = GetFrame(index - 1); - for (int i = 0; i < pixels.Length; ++i) - pixels[i] += prev_frame[i]; - } - Frames[index] = pixels; - return pixels; - } - } - -} +//! \file ArcAN21.cs +//! \date Sun Apr 30 21:04:25 2017 +//! \brief KaGuYa script engine animation resource. +// +// 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 System.Linq; +using System.Windows.Media; + +namespace GameRes.Formats.Kaguya +{ + class PL10Entry : PackedEntry + { + public int FrameIndex; + public int RleStep; + } + + [Export(typeof(ArchiveFormat))] + public class PL10Opener : ArchiveFormat + { + public override string Tag { get { return "PL10/KAGUYA"; } } + public override string Description { get { return "KaGuYa script engine animation resource"; } } + public override uint Signature { get { return 0x30314C50; } } // 'PL10' + public override bool IsHierarchic { get { return false; } } + public override bool CanWrite { get { return false; } } + + public PL10Opener() + { + Extensions = new string[] { "plt" }; + } + + public override ArcFile TryOpen(ArcView file) + { + uint current_offset = 4; + int frame_count = file.View.ReadUInt16(current_offset); + if (!IsSaneCount(frame_count)) + return null; + current_offset += 0x12; + string base_name = Path.GetFileNameWithoutExtension(file.Name); + var dir = new List(frame_count); + var info = new ImageMetaData + { + OffsetX = file.View.ReadInt32(current_offset), + OffsetY = file.View.ReadInt32(current_offset + 4), + Width = file.View.ReadUInt32(current_offset + 8), + Height = file.View.ReadUInt32(current_offset + 12), + }; + int channels = file.View.ReadInt32(current_offset + 0x10); + info.BPP = channels * 8; + current_offset += 0x14; + var entry = new PL10Entry + { + FrameIndex = 0, + Name = string.Format("{0}#{1:D2}", base_name, 0), + Type = "image", + Offset = current_offset, + Size = (uint)channels * info.Width * info.Height, + IsPacked = false, + }; + dir.Add(entry); + current_offset += entry.Size; + for (int i = 1; i < frame_count; ++i) + { + int step = file.View.ReadByte(current_offset++); + if (0 == step) + return null; + uint packed_size = file.View.ReadUInt32(current_offset); + uint unpacked_size = (uint)(channels * (info.OffsetX + (int)info.Width) + * (info.OffsetY + (int)info.Height)); + current_offset += 4; + entry = new PL10Entry + { + FrameIndex = i, + Name = string.Format("{0}#{1:D2}", base_name, i), + Type = "image", + Offset = current_offset, + Size = packed_size, + UnpackedSize = unpacked_size, + IsPacked = true, + RleStep = step, + }; + dir.Add(entry); + current_offset += packed_size; + } + return new PL10Archive(file, this, dir, info); + } + + public override Stream OpenEntry(ArcFile arc, Entry entry) + { + var anent = entry as PL10Entry; + var input = arc.File.CreateStream(entry.Offset, entry.Size); + if (null == anent || !anent.IsPacked) + return input; + using (input) + { + var data = DecompressRLE(input, anent.UnpackedSize, anent.RleStep); + return new BinMemoryStream(data); + } + } + + public override IImageDecoder OpenImage(ArcFile arc, Entry entry) + { + var anarc = (PL10Archive)arc; + var anent = (PL10Entry)entry; + var pixels = anarc.GetFrame(anent.FrameIndex); + return new BitmapDecoder(pixels, anarc.ImageInfo); + } + + internal static byte[] DecompressRLE(IBinaryStream input, uint unpacked_size, int rle_step) + { + var output = new byte[unpacked_size]; + for (int i = 0; i < rle_step; ++i) + { + byte v1 = input.ReadUInt8(); + output[i] = v1; + int dst = i + rle_step; + while (dst < output.Length) + { + byte v2 = input.ReadUInt8(); + output[dst] = v2; + dst += rle_step; + if (v2 == v1) + { + int count = input.ReadUInt8(); + if (0 != (count & 0x80)) + count = input.ReadUInt8() + ((count & 0x7F) << 8) + 128; + while (count-- > 0 && dst < output.Length) + { + output[dst] = v2; + dst += rle_step; + } + if (dst < output.Length) + { + v2 = input.ReadUInt8(); + output[dst] = v2; + dst += rle_step; + } + } + v1 = v2; + } + } + return output; + } + } + + class PL10Archive : ArcFile + { + byte[][] Frames; + + public readonly ImageMetaData ImageInfo; + + public PL10Archive(ArcView arc, ArchiveFormat impl, ICollection dir, ImageMetaData base_info) + : base(arc, impl, dir) + { + Frames = new byte[dir.Count][]; + ImageInfo = base_info; + } + + public byte[] GetFrame(int index) + { + if (index >= Frames.Length) + throw new ArgumentException("index"); + if (null != Frames[index]) + return Frames[index]; + + var entry = Dir.ElementAt(index); + byte[] pixels; + using (var stream = OpenEntry(entry)) + { + pixels = new byte[stream.Length]; + stream.Read(pixels, 0, pixels.Length); + } + if (index > 0) + { + var prev_frame = GetFrame(index - 1); + for (int i = 0; i < pixels.Length; ++i) + pixels[i] += prev_frame[i]; + } + Frames[index] = pixels; + return pixels; + } + } + +} From 726b4161602fbabdcacee787fc273f0089650f25 Mon Sep 17 00:00:00 2001 From: rewjx Date: Sun, 13 Feb 2022 15:55:43 +0800 Subject: [PATCH 3/3] update_kaguya_to_support_AN20_decrypt --- ArcFormats/Kaguya/ArcLINK.cs | 37 ++++++++++++++++++++++++++++++++++++ 1 file changed, 37 insertions(+) diff --git a/ArcFormats/Kaguya/ArcLINK.cs b/ArcFormats/Kaguya/ArcLINK.cs index 1cb96f84..ec0f5294 100644 --- a/ArcFormats/Kaguya/ArcLINK.cs +++ b/ArcFormats/Kaguya/ArcLINK.cs @@ -648,6 +648,7 @@ namespace GameRes.Formats.Kaguya if (anm_encrypted) { table.Add (new Tuple ("AN00", (a, e) => DecryptAn00 (a, e))); + table.Add (new Tuple ("AN20", (a, e) => DecryptAn20 (a, e))); table.Add (new Tuple ("AN21", (a, e) => DecryptAn21 (a, e))); table.Add (new Tuple ("PL00", (a, e) => DecryptPL00 (a, e))); table.Add (new Tuple ("PL10", (a, e) => DecryptPL10 (a, e))); @@ -690,6 +691,42 @@ namespace GameRes.Formats.Kaguya frame_offset += size + 8; } return new BinMemoryStream (data, entry.Name); + } + + Stream DecryptAn20(LinkArchive arc, LinkEntry entry) + { + var data = arc.File.View.ReadBytes(entry.Offset, entry.Size); + int count = data.ToUInt16(4); + int offset = 8; + for (int i = 0; i < count; ++i) + { + switch (data[offset++]) + { + case 0: break; + case 1: offset += 8; break; + case 2: + case 3: + case 4: + case 5: offset += 4; break; + default: return new BinMemoryStream(data, entry.Name); + } + } + count = data.ToUInt16(offset); + offset += 2 + count * 8; + int frame_count = data.ToInt16(offset); + offset += 18; + for(int i = 0; i < frame_count; ++i) + { + offset += 8; + int w = data.ToInt32(offset); + int h = data.ToInt32(offset + 4); + int channels = data.ToInt32(offset + 8); + int frame_size = channels * w * h; + offset += 12; + DecryptData(data, offset, frame_size); + offset += frame_size; + } + return new BinMemoryStream(data, entry.Name); } Stream DecryptAn21 (LinkArchive arc, LinkEntry entry)