Merge remote-tracking branch 'upstream/master'

This commit is contained in:
Crsky
2022-11-29 18:43:15 +08:00
67 changed files with 2707 additions and 344 deletions

View File

@@ -23,7 +23,6 @@
// IN THE SOFTWARE.
//
using System;
using System.Collections.Generic;
using System.ComponentModel.Composition;
using System.IO;
@@ -42,60 +41,110 @@ namespace GameRes.Formats.Kaguya
}
}
[Export(typeof(ArchiveFormat))]
public class AnmOpener : ArchiveFormat
internal class AnmEntry : Entry
{
public long ImageDataOffset;
public uint ImageDataSize;
}
internal interface IAnmReader
{
List<Entry> GetFramesList (IBinaryStream input);
}
public abstract class AnmOpenerBase : ArchiveFormat, IAnmReader
{
public override string Tag { get { return "ANM/KAGUYA"; } }
public override string Description { get { return "KaGuYa script engine animation resource"; } }
public override uint Signature { get { return 0x30304E41; } } // 'AN00'
public override bool IsHierarchic { get { return false; } }
public override bool CanWrite { get { return false; } }
public AnmOpener ()
public AnmOpenerBase ()
{
Extensions = new string[] { "anm" };
}
public override ArcFile TryOpen (ArcView file)
{
int frame_count = file.View.ReadInt16 (0x14);
uint current_offset = 0x18 + (uint)frame_count * 4;
int count = file.View.ReadInt16 (current_offset);
if (!IsSaneCount (count))
return null;
var base_info = new ImageMetaData
using (var input = file.CreateStream())
{
OffsetX = file.View.ReadInt32 (4),
OffsetY = file.View.ReadInt32 (8),
Width = file.View.ReadUInt32 (0x0C),
Height = file.View.ReadUInt32 (0x10),
BPP = 32,
};
current_offset += 2;
string base_name = Path.GetFileNameWithoutExtension (file.Name);
var dir = new List<Entry> (count);
for (int i = 0; i < count; ++i)
{
uint width = file.View.ReadUInt32 (current_offset+8);
uint height = file.View.ReadUInt32 (current_offset+12);
var entry = new Entry
var dir = GetFramesList (input);
if (null == dir)
return null;
var base_info = GetBaseInfo (input);
string base_name = Path.GetFileNameWithoutExtension (file.Name);
int i = 0;
foreach (var entry in dir)
{
Name = string.Format ("{0}#{1:D2}", base_name, i),
Type = "image",
Offset = current_offset,
Size = 0x10 + 4*width*height,
};
dir.Add (entry);
current_offset += entry.Size;
entry.Name = string.Format ("{0}#{1:D2}", base_name, i++);
entry.Type = "image";
}
return new AnmArchive (file, this, dir, base_info);
}
return new AnmArchive (file, this, dir, base_info);
}
public override IImageDecoder OpenImage (ArcFile arc, Entry entry)
{
var base_info = ((AnmArchive)arc).ImageInfo;
var input = arc.File.CreateStream (entry.Offset, entry.Size);
return new An00Decoder (input, base_info);
return CreateDecoder (input, base_info);
}
internal virtual ImageMetaData GetBaseInfo (IBinaryStream input)
{
input.Position = 4;
return new ImageMetaData
{
OffsetX = input.ReadInt32(),
OffsetY = input.ReadInt32(),
Width = input.ReadUInt32(),
Height = input.ReadUInt32(),
BPP = 32,
};
}
public abstract List<Entry> GetFramesList (IBinaryStream input);
public abstract IImageDecoder CreateDecoder (IBinaryStream input, ImageMetaData info);
}
[Export(typeof(ArchiveFormat))]
public class AnmOpener : AnmOpenerBase
{
public override string Tag { get { return "ANM/KAGUYA"; } }
public override uint Signature { get { return 0x30304E41; } } // 'AN00'
public override List<Entry> GetFramesList (IBinaryStream file)
{
file.Position = 0x14;
int frame_count = file.ReadInt16();
file.Position = 0x18 + frame_count * 4;
int count = file.ReadInt16();
if (!IsSaneCount (count))
return null;
var current_offset = file.Position;
var dir = new List<Entry> (count);
for (int i = 0; i < count; ++i)
{
file.Position = current_offset + 8;
uint width = file.ReadUInt32();
uint height = file.ReadUInt32();
uint image_size = 4*width*height;
var entry = new AnmEntry
{
Offset = current_offset,
Size = 0x10 + image_size,
ImageDataOffset = current_offset + 0x10,
ImageDataSize = image_size,
};
dir.Add (entry);
current_offset += entry.Size;
}
return dir;
}
public override IImageDecoder CreateDecoder (IBinaryStream input, ImageMetaData info)
{
return new An00Decoder (input, info);
}
}
@@ -123,75 +172,146 @@ namespace GameRes.Formats.Kaguya
}
[Export(typeof(ArchiveFormat))]
public class An20Opener : ArchiveFormat
public class An10Opener : AnmOpenerBase, IAnmReader
{
public override string Tag { get { return "AN20/KAGUYA"; } }
public override string Description { get { return "KaGuYa script engine animation resource"; } }
public override uint Signature { get { return 0x30324E41; } } // 'AN20'
public override bool IsHierarchic { get { return false; } }
public override bool CanWrite { get { return false; } }
public override string Tag { get { return "AN10/KAGUYA"; } }
public override uint Signature { get { return 0x30314E41; } } // 'AN10'
public An20Opener ()
public override List<Entry> GetFramesList (IBinaryStream file)
{
Extensions = new string[] { "anm" };
}
public override ArcFile TryOpen (ArcView file)
{
int table_count = file.View.ReadInt16 (4);
uint current_offset = 8;
for (int i = 0; i < table_count; ++i)
{
switch (file.View.ReadByte (current_offset++))
{
case 0: break;
case 1: current_offset += 8; break;
case 2:
case 3:
case 4:
case 5: current_offset += 4; break;
default: return null;
}
}
current_offset += 2 + file.View.ReadUInt16 (current_offset) * 8u;
int count = file.View.ReadInt16 (current_offset);
file.Position = 0x14;
int frame_count = file.ReadInt16();
file.Position = 0x18 + frame_count * 4;
int count = file.ReadInt16();
if (!IsSaneCount (count))
return null;
current_offset += 2;
var base_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),
BPP = 32,
};
current_offset += 0x10;
string base_name = Path.GetFileNameWithoutExtension (file.Name);
var current_offset = file.Position;
var dir = new List<Entry> (count);
for (int i = 0; i < count; ++i)
{
uint width = file.View.ReadUInt32 (current_offset+8);
uint height = file.View.ReadUInt32 (current_offset+0x0C);
uint depth = file.View.ReadUInt32 (current_offset+0x10);
var entry = new Entry
file.Position = current_offset + 8;
uint width = file.ReadUInt32();
uint height = file.ReadUInt32();
uint channels = file.ReadUInt32();
uint image_size = channels*width*height;
var entry = new AnmEntry
{
Name = string.Format ("{0}#{1:D2}", base_name, i),
Type = "image",
Offset = current_offset,
Size = 0x14 + depth*width*height,
Size = 0x14 + image_size,
ImageDataOffset = current_offset + 0x14,
ImageDataSize = image_size,
};
dir.Add (entry);
current_offset += entry.Size;
}
return new AnmArchive (file, this, dir, base_info);
return dir;
}
public override IImageDecoder OpenImage (ArcFile arc, Entry entry)
public override IImageDecoder CreateDecoder (IBinaryStream input, ImageMetaData info)
{
var base_info = ((AnmArchive)arc).ImageInfo;
var input = arc.File.CreateStream (entry.Offset, entry.Size);
return new An20Decoder (input, base_info);
return new An10Decoder (input, info);
}
}
internal class An10Decoder : BinaryImageDecoder
{
public An10Decoder (IBinaryStream input, ImageMetaData base_info) : base (input)
{
Info = new ImageMetaData
{
OffsetX = base_info.OffsetX + m_input.ReadInt32(),
OffsetY = base_info.OffsetY + m_input.ReadInt32(),
Width = m_input.ReadUInt32(),
Height = m_input.ReadUInt32(),
BPP = m_input.ReadInt32() * 8,
};
}
protected override ImageData GetImageData ()
{
m_input.Position = 0x14;
int stride = Info.BPP / 8 * Info.iWidth;
var pixels = m_input.ReadBytes (stride*Info.iHeight);
PixelFormat format = 24 == Info.BPP ? PixelFormats.Bgr24 : PixelFormats.Bgra32;
return ImageData.CreateFlipped (Info, format, null, pixels, stride);
}
}
[Export(typeof(ArchiveFormat))]
public class An20Opener : AnmOpenerBase
{
public override string Tag { get { return "AN20/KAGUYA"; } }
public override uint Signature { get { return 0x30324E41; } } // 'AN20'
public override List<Entry> GetFramesList (IBinaryStream file)
{
if (!SkipFrameTable (file))
return null;
int count = file.ReadInt16();
if (!IsSaneCount (count))
return null;
long current_offset = file.Position + 0x10;
var dir = new List<Entry> (count);
for (int i = 0; i < count; ++i)
{
file.Position = current_offset + 8;
uint width = file.ReadUInt32();
uint height = file.ReadUInt32();
uint depth = file.ReadUInt32();
uint image_size = depth*width*height;
var entry = new AnmEntry
{
Offset = current_offset,
Size = 0x14 + image_size,
ImageDataOffset = current_offset + 0x14,
ImageDataSize = image_size,
};
dir.Add (entry);
current_offset += entry.Size;
}
return dir;
}
internal override ImageMetaData GetBaseInfo (IBinaryStream input)
{
SkipFrameTable (input);
input.ReadInt16();
return new ImageMetaData
{
OffsetX = input.ReadInt32(),
OffsetY = input.ReadInt32(),
Width = input.ReadUInt32(),
Height = input.ReadUInt32(),
BPP = 32,
};
}
bool SkipFrameTable (IBinaryStream file)
{
file.Position = 4;
int table_count = file.ReadInt16();
file.Position = 8;
for (int i = 0; i < table_count; ++i)
{
switch (file.ReadByte())
{
case 0: break;
case 1: file.Seek (8, SeekOrigin.Current); break;
case 2:
case 3:
case 4:
case 5: file.Seek (4, SeekOrigin.Current); break;
default: return false;
}
}
int count = file.ReadUInt16();
file.Seek (count * 8, SeekOrigin.Current);
return true;
}
public override IImageDecoder CreateDecoder (IBinaryStream input, ImageMetaData info)
{
return new An20Decoder (input, info);
}
}

View File

@@ -90,7 +90,7 @@ namespace GameRes.Formats.Kaguya
}
}
byte[] UnpackLzss (IBinaryStream input, uint unpacked_size)
internal static byte[] UnpackLzss (IBinaryStream input, uint unpacked_size)
{
var output = new byte[unpacked_size];
var frame = new byte[0x100];

View File

@@ -65,7 +65,7 @@ namespace GameRes.Formats.Kaguya
{
int version = file.View.ReadByte (4) - '0';
if (version < 3 || version > 6)
return null;
return ReadOldIndex (file);
using (var reader = LinkReader.Create (file, version))
{
@@ -87,7 +87,22 @@ namespace GameRes.Formats.Kaguya
{
var lent = entry as LinkEntry;
if (null == lent || (!lent.IsPacked && !lent.IsEncrypted))
{
if (entry.Size > 8)
{
uint unpacked_size = arc.File.View.ReadUInt32 (entry.Offset);
int id = arc.File.View.ReadUInt16 (entry.Offset+5);
if (id == 0x4D42) // 'BM'
{
using (var input = arc.File.CreateStream (entry.Offset+4, entry.Size-4, entry.Name))
{
var data = Lin2Opener.UnpackLzss (input, unpacked_size);
return new BinMemoryStream (data, entry.Name);
}
}
}
return base.OpenEntry (arc, entry);
}
if (lent.IsEncrypted)
{
var larc = arc as LinkArchive;
@@ -102,6 +117,35 @@ namespace GameRes.Formats.Kaguya
return new BinMemoryStream (bmr.Data, entry.Name);
}
}
internal ArcFile ReadOldIndex (ArcView file)
{
int count = file.View.ReadInt32 (4);
if (!IsSaneCount (count))
return null;
var dir = new List<Entry> (count);
using (var index = file.CreateStream())
{
index.Position = 8;
uint names_size = index.ReadUInt32();
for (int i = 0; i < count; ++i)
{
var name = index.ReadCString();
var entry = FormatCatalog.Instance.Create<Entry> (name);
dir.Add (entry);
}
index.Position = 12 + names_size;
foreach (var entry in dir)
{
entry.Offset = index.ReadUInt32();
entry.Size = index.ReadUInt32();
if (!entry.CheckPlacement (file.MaxOffset))
return null;
}
}
return new ArcFile (file, this, dir);
}
}
internal class LinkReader : IDisposable
@@ -440,9 +484,15 @@ namespace GameRes.Formats.Kaguya
var header = input.ReadHeader (0x11);
if (header.AsciiEqual ("[SCR-PARAMS]v0"))
{
var version = Version.Parse (header.GetCString (13, 4));
Version version;
if ('.' == header[15])
version = Version.Parse (header.GetCString (13, 4));
else
version = new Version (header[14] - '0', 0);
if (2 == version.Major)
return new ParamsV2Deserializer (input, version);
else if (version.Major < 5)
return new ParamsV4Deserializer (input, version);
else if (5 == version.Major && (version.Minor >= 4 && version.Minor <= 7))
return new ParamsV5Deserializer (input, version);
}
@@ -499,6 +549,22 @@ namespace GameRes.Formats.Kaguya
SkipString();
}
}
protected void ReadHeader (int start)
{
m_input.Position = start;
SkipChunk();
m_title = ReadString();
if (m_version.Major < 3)
m_input.ReadCString();
SkipString();
SkipString();
m_input.ReadByte();
SkipString();
SkipString();
SkipDict();
m_input.ReadByte();
}
}
internal class ParamsV2Deserializer : ParamsDeserializer
@@ -515,17 +581,7 @@ namespace GameRes.Formats.Kaguya
public override byte[] GetKey ()
{
m_input.Position = 0x17;
SkipChunk();
m_title = ReadString();
m_input.ReadCString();
SkipString();
SkipString();
m_input.ReadByte();
SkipString();
SkipString();
SkipDict();
m_input.ReadByte();
ReadHeader (0x17);
if ("幼なじみと甘~くエッチに過ごす方法" == m_title)
{
@@ -565,6 +621,37 @@ namespace GameRes.Formats.Kaguya
}
}
internal class ParamsV4Deserializer : ParamsDeserializer
{
public ParamsV4Deserializer (IBinaryStream input, Version version) : base (input, version)
{
}
public override byte[] GetKey ()
{
ReadHeader (0x19);
Skip (m_version.Major < 5 ? 12 : 11);
int count = m_input.ReadUInt8();
for (int i = 0; i < count; ++i)
{
m_input.ReadByte();
SkipChunk();
SkipArray();
SkipArray();
}
SkipDict();
count = m_input.ReadUInt8();
for (int i = 0; i < count; ++i)
{
SkipChunk();
SkipArray();
SkipArray();
}
return ReadKey();
}
}
internal class ParamsV5Deserializer : ParamsDeserializer
{
public ParamsV5Deserializer (IBinaryStream input, Version version) : base (input, version)
@@ -573,18 +660,9 @@ namespace GameRes.Formats.Kaguya
public override byte[] GetKey ()
{
// ハラミタマ
m_input.Position = 0x1B;
SkipChunk();
m_title = ReadString();
SkipString();
SkipString();
m_input.ReadByte();
SkipString();
SkipString();
SkipDict();
ReadHeader (0x1B);
Skip (m_version.Minor <= 4 ? 0x10 : 0x11);
Skip (m_version.Minor <= 4 ? 15 : 16);
for (int i = 0; i < 3; ++i)
{
if (0 != m_input.ReadUInt8())
@@ -633,6 +711,11 @@ namespace GameRes.Formats.Kaguya
delegate Stream Decryptor (LinkArchive arc, LinkEntry entry);
static readonly ResourceInstance<AnmOpener> An00 = new ResourceInstance<AnmOpener> ("ANM/KAGUYA");
static readonly ResourceInstance<An10Opener> An10 = new ResourceInstance<An10Opener> ("AN10/KAGUYA");
static readonly ResourceInstance<An20Opener> An20 = new ResourceInstance<An20Opener> ("AN20/KAGUYA");
static readonly ResourceInstance<Pl00Opener> Pl00 = new ResourceInstance<Pl00Opener> ("PLT/KAGUYA");
public LinkEncryption (byte[] key, bool anm_encrypted = true)
{
if (null == key || 0 == key.Length)
@@ -675,13 +758,12 @@ namespace GameRes.Formats.Kaguya
return new PrefixStream (header, body);
}
Stream DecryptAn00 (LinkArchive arc, LinkEntry entry)
Stream DecryptAnm (LinkArchive arc, LinkEntry entry, IAnmReader reader)
{
var data = arc.File.View.ReadBytes (entry.Offset, entry.Size);
int frame_offset = 0x18 + data.ToUInt16 (0x14) * 4;
int count = data.ToUInt16 (frame_offset);
frame_offset += 10;
for (int i = 0; i < count; ++i)
var input = new BinMemoryStream (data, entry.Name);
var dir = reader.GetFramesList (input);
if (dir != null)
{
int w = data.ToInt32 (frame_offset);
int h = data.ToInt32 (frame_offset+4);
@@ -691,8 +773,8 @@ 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);
@@ -701,13 +783,13 @@ namespace GameRes.Formats.Kaguya
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;
{
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);
}
}
@@ -715,16 +797,16 @@ namespace GameRes.Formats.Kaguya
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;
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);
}
@@ -755,27 +837,27 @@ namespace GameRes.Formats.Kaguya
offset += 12;
DecryptData (data, offset, channels * w * h);
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;
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);

101
ArcFormats/Kaguya/ArcPLT.cs Normal file
View File

@@ -0,0 +1,101 @@
//! \file ArcPLT.cs
//! \date 2022 May 03
//! \brief KaGuYa script engine animation resource.
//
// Copyright (C) 2022 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;
using System.Windows.Media;
namespace GameRes.Formats.Kaguya
{
[Export(typeof(ArchiveFormat))]
public class Pl00Opener : AnmOpenerBase
{
public override string Tag { get { return "PLT/KAGUYA"; } }
public override uint Signature { get { return 0x30304C50; } } // 'PL00'
public Pl00Opener ()
{
Extensions = new string[] { "plt" };
}
public override List<Entry> GetFramesList (IBinaryStream file)
{
file.Position = 4;
int count = file.ReadInt16();
if (!IsSaneCount (count))
return null;
file.Position = 0x16;
var current_offset = file.Position;
var dir = new List<Entry> (count);
for (int i = 0; i < count; ++i)
{
file.Position = current_offset + 8;
uint width = file.ReadUInt32();
uint height = file.ReadUInt32();
uint depth = file.ReadUInt32();
uint image_size = depth*width*height;
var entry = new AnmEntry
{
Offset = current_offset,
Size = 0x14 + image_size,
ImageDataOffset = current_offset + 0x14,
ImageDataSize = image_size,
};
dir.Add (entry);
current_offset += entry.Size;
}
return dir;
}
public override IImageDecoder CreateDecoder (IBinaryStream input, ImageMetaData info)
{
return new Pl00Decoder (input, info);
}
}
internal class Pl00Decoder : BinaryImageDecoder
{
public Pl00Decoder (IBinaryStream input, ImageMetaData base_info) : base (input)
{
Info = new ImageMetaData
{
OffsetX = base_info.OffsetX + m_input.ReadInt32(),
OffsetY = base_info.OffsetY + m_input.ReadInt32(),
Width = m_input.ReadUInt32(),
Height = m_input.ReadUInt32(),
BPP = m_input.ReadInt32() * 8,
};
}
protected override ImageData GetImageData ()
{
m_input.Position = 0x14;
int stride = Info.BPP * Info.iWidth / 8;
var pixels = m_input.ReadBytes (stride*Info.iHeight);
PixelFormat format = 24 == Info.BPP ? PixelFormats.Bgr24 : PixelFormats.Bgra32;
return ImageData.CreateFlipped (Info, format, null, pixels, stride);
}
}
}