mirror of
https://github.com/crskycode/GARbro.git
synced 2026-07-08 01:30:18 +08:00
added various Strikes resource formats.
This commit is contained in:
275
ArcFormats/Strikes/ArcPCK.cs
Normal file
275
ArcFormats/Strikes/ArcPCK.cs
Normal file
@@ -0,0 +1,275 @@
|
||||
//! \file ArcPCK.cs
|
||||
//! \date 2020 Mar 29
|
||||
//! \brief Strikes resource archive.
|
||||
//
|
||||
// Copyright (C) 2020 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.Compression;
|
||||
using GameRes.Utility;
|
||||
|
||||
namespace GameRes.Formats.Strikes
|
||||
{
|
||||
internal class PckEntry : PackedEntry
|
||||
{
|
||||
public bool IsEncrypted { get; set; }
|
||||
}
|
||||
|
||||
[Export(typeof(ArchiveFormat))]
|
||||
public class PckOpener : ArchiveFormat
|
||||
{
|
||||
public override string Tag { get { return "PCK/AVG"; } }
|
||||
public override string Description { get { return "Strikes resource archive"; } }
|
||||
public override uint Signature { get { return 0; } }
|
||||
public override bool IsHierarchic { get { return true; } }
|
||||
public override bool CanWrite { get { return false; } }
|
||||
|
||||
public PckOpener ()
|
||||
{
|
||||
ContainedFormats = new[] { "LAG", "BMP", "OGG", "WAV", "TXT" };
|
||||
}
|
||||
|
||||
public override ArcFile TryOpen (ArcView file)
|
||||
{
|
||||
if (!VFS.IsPathEqualsToFileName (file.Name, "AVGDatas.pck"))
|
||||
return null;
|
||||
int seed = Binary.BigEndian (file.View.ReadInt32 (file.MaxOffset - 104));
|
||||
var header = file.View.ReadBytes (file.MaxOffset - 100, 100);
|
||||
var rnd = new RandomGenerator();
|
||||
rnd.Init (seed);
|
||||
rnd.Decrypt (header, 0, header.Length);
|
||||
|
||||
uint checksum = (uint)(header[1] | header[2] << 8 | header[0] << 16 | header[3] << 24) ^ 0xDEFD32D3;
|
||||
uint length = BigEndian.ToUInt32 (header, 24);
|
||||
if (checksum != length)
|
||||
return null;
|
||||
|
||||
uint idx_pos = BigEndian.ToUInt32 (header, 28);
|
||||
uint idx_size = Binary.BigEndian (file.View.ReadUInt32 (idx_pos));
|
||||
|
||||
uint index_size;
|
||||
var index = ReadChunk (file, 8, idx_pos + 4, out index_size);
|
||||
if (index_size >= 0x80000000)
|
||||
{
|
||||
index_size &= 0x7FFFFFFF;
|
||||
var unpacked = new byte[idx_size];
|
||||
LzssUnpack (index, index.Length, unpacked);
|
||||
index = unpacked;
|
||||
}
|
||||
using (var input = new BinMemoryStream (index))
|
||||
{
|
||||
var dir = new List<Entry>();
|
||||
int dir_count = 0;
|
||||
while (input.PeekByte() != -1)
|
||||
{
|
||||
input.ReadInt32();
|
||||
input.ReadInt32();
|
||||
input.ReadInt32();
|
||||
int count = Binary.BigEndian (input.ReadInt32());
|
||||
var dir_name = dir_count.ToString ("X4");
|
||||
for (int i = 0; i < count; ++i)
|
||||
{
|
||||
var name = input.ReadCString (0x28);
|
||||
name = Path.Combine (dir_name, name);
|
||||
var entry = Create<PckEntry> (name);
|
||||
entry.Offset = Binary.BigEndian (input.ReadUInt32());
|
||||
entry.Size = Binary.BigEndian (input.ReadUInt32());
|
||||
entry.IsEncrypted = input.ReadInt32() != 0;
|
||||
entry.UnpackedSize = input.ReadUInt32();
|
||||
entry.IsPacked = entry.Size != entry.UnpackedSize;
|
||||
if (!entry.CheckPlacement (file.MaxOffset))
|
||||
return null;
|
||||
dir.Add (entry);
|
||||
}
|
||||
++dir_count;
|
||||
}
|
||||
return new ArcFile (file, this, dir);
|
||||
}
|
||||
}
|
||||
|
||||
public override Stream OpenEntry (ArcFile arc, Entry entry)
|
||||
{
|
||||
var pent = entry as PckEntry;
|
||||
if (null == pent || !(pent.IsEncrypted || pent.IsPacked))
|
||||
return base.OpenEntry (arc, entry);
|
||||
arc.File.View.Reserve (pent.Offset, pent.Size);
|
||||
int skip_size = 8;
|
||||
do
|
||||
{
|
||||
uint test_size = Binary.BigEndian (arc.File.View.ReadUInt32 (pent.Offset + skip_size * 4));
|
||||
if (test_size + 4 == pent.Size)
|
||||
break;
|
||||
}
|
||||
while (--skip_size > 0);
|
||||
byte[] data;
|
||||
uint data_size = pent.Size;
|
||||
if (0 == skip_size)
|
||||
{
|
||||
data = arc.File.View.ReadBytes (pent.Offset, pent.Size);
|
||||
}
|
||||
else
|
||||
{
|
||||
data = ReadChunk (arc.File, skip_size, pent.Offset, out data_size);
|
||||
}
|
||||
if (pent.IsEncrypted)
|
||||
{
|
||||
if (data_size >= 0x10)
|
||||
DecryptData (data, 0x10, 0xC53A9A6C);
|
||||
else
|
||||
DecryptData (data, data.Length, 0x6C9A3AC5);
|
||||
}
|
||||
Stream input = new BinMemoryStream (data, pent.Name);
|
||||
if (pent.IsPacked)
|
||||
input = new LzssStream (input);
|
||||
return input;
|
||||
}
|
||||
|
||||
void DecryptData (byte[] data, int length, uint key)
|
||||
{
|
||||
for (int i = 0; i < length; ++i)
|
||||
{
|
||||
data[i] ^= (byte)(key >> ((i & 3) << 3));
|
||||
}
|
||||
}
|
||||
|
||||
byte[] ReadChunk (ArcView file, int skipSize, long offset, out uint chunkSize)
|
||||
{
|
||||
skipSize *= 4;
|
||||
var header = file.View.ReadUInt32 (offset);
|
||||
chunkSize = Binary.BigEndian (file.View.ReadUInt32 (offset + skipSize));
|
||||
uint size = chunkSize & 0x7FFFFFFF;
|
||||
var chunk = file.View.ReadBytes (offset + 4, size);
|
||||
if (skipSize > 0)
|
||||
{
|
||||
System.Buffer.BlockCopy (chunk, 0, chunk, 4, skipSize - 4);
|
||||
LittleEndian.Pack (header, chunk, 0);
|
||||
}
|
||||
return chunk;
|
||||
}
|
||||
|
||||
static internal int LzssUnpack (byte[] input, int in_length, byte[] output)
|
||||
{
|
||||
var frame = new byte[0x1000];
|
||||
int frame_pos = 0xFEE;
|
||||
int src = 0;
|
||||
int dst = 0;
|
||||
while (src < in_length)
|
||||
{
|
||||
int ctl = input[src++];
|
||||
for (int bit = 1; bit != 0x100; bit <<= 1)
|
||||
{
|
||||
if (0 != (ctl & bit))
|
||||
{
|
||||
if (src >= in_length)
|
||||
return dst;
|
||||
byte b = input[src++];
|
||||
frame[frame_pos++ & 0xFFF] = b;
|
||||
output[dst++] = b;
|
||||
}
|
||||
else
|
||||
{
|
||||
if (src + 2 > in_length)
|
||||
return dst;
|
||||
int lo = input[src++];
|
||||
int hi = input[src++];
|
||||
int offset = (hi & 0xF0) << 4 | lo;
|
||||
int count = Math.Min (3 + (hi & 0xF), output.Length - dst);
|
||||
while (count --> 0)
|
||||
{
|
||||
byte b = frame[offset++ & 0xFFF];
|
||||
frame[frame_pos++ & 0xFFF] = b;
|
||||
output[dst++] = b;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
return dst;
|
||||
}
|
||||
}
|
||||
|
||||
internal class RandomGenerator
|
||||
{
|
||||
int m_count;
|
||||
int[] m_state = new int[56];
|
||||
|
||||
public void Init (int seed)
|
||||
{
|
||||
int n = 1;
|
||||
m_state[55] = seed;
|
||||
for (int i = 1; i <= 54; ++i)
|
||||
{
|
||||
int pos = 21 * i % 55;
|
||||
m_state[pos] = n;
|
||||
|
||||
n = seed - n;
|
||||
if (n < 0)
|
||||
n += 1000000000;
|
||||
seed = m_state[pos];
|
||||
}
|
||||
Shuffle();
|
||||
Shuffle();
|
||||
Shuffle();
|
||||
m_count = 55;
|
||||
}
|
||||
|
||||
public uint Rand ()
|
||||
{
|
||||
if (++m_count > 55)
|
||||
{
|
||||
Shuffle();
|
||||
m_count = 1;
|
||||
}
|
||||
return (uint)m_state[m_count];
|
||||
}
|
||||
|
||||
private void Shuffle ()
|
||||
{
|
||||
for (int i = 1; i <= 24; ++i)
|
||||
{
|
||||
m_state[i] = m_state[i] - m_state[i+31];
|
||||
if (m_state[i] < 0)
|
||||
m_state[i] += 1000000000;
|
||||
}
|
||||
for (int i = 25; i <= 55; ++i)
|
||||
{
|
||||
m_state[i] = m_state[i] - m_state[i-24];
|
||||
if (m_state[i] < 0)
|
||||
m_state[i] += 1000000000;
|
||||
}
|
||||
}
|
||||
|
||||
public void Decrypt (byte[] input, int src, int length)
|
||||
{
|
||||
for (int i = src; i < length; i += 4)
|
||||
{
|
||||
uint key = Rand();
|
||||
input[i ] ^= (byte)(key >> 24);
|
||||
input[i+1] ^= (byte)(key >> 16);
|
||||
input[i+2] ^= (byte)(key >> 8);
|
||||
input[i+3] ^= (byte)(key);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
305
ArcFormats/Strikes/ImageLAG.cs
Normal file
305
ArcFormats/Strikes/ImageLAG.cs
Normal file
@@ -0,0 +1,305 @@
|
||||
//! \file ImageLAG.cs
|
||||
//! \date 2020 Mar 29
|
||||
//! \brief Strikes image format.
|
||||
//
|
||||
// Copyright (C) 2020 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;
|
||||
using GameRes.Utility;
|
||||
|
||||
namespace GameRes.Formats.Strikes
|
||||
{
|
||||
internal class LagMetaData : ImageMetaData
|
||||
{
|
||||
public int ScanLineSize;
|
||||
public bool HasPalette;
|
||||
public bool HasAlpha;
|
||||
public int LastChunkSize;
|
||||
public int ChunkCount;
|
||||
}
|
||||
|
||||
[Export(typeof(ImageFormat))]
|
||||
public class LagFormat : ImageFormat
|
||||
{
|
||||
public override string Tag { get { return "LAG"; } }
|
||||
public override string Description { get { return "Strikes image format"; } }
|
||||
public override uint Signature { get { return 0x414C1001; } }
|
||||
|
||||
public override ImageMetaData ReadMetaData (IBinaryStream file)
|
||||
{
|
||||
var header = file.ReadHeader (0x20);
|
||||
int bpp = header[10] & 0x1F;
|
||||
if (!(bpp == 24 || bpp == 16 || bpp == 8))
|
||||
return null;
|
||||
return new LagMetaData
|
||||
{
|
||||
Width = BigEndian.ToUInt16 (header, 4),
|
||||
Height = BigEndian.ToUInt16 (header, 6),
|
||||
BPP = bpp,
|
||||
ScanLineSize = BigEndian.ToUInt16 (header, 8),
|
||||
HasPalette = (header[10] & 0x80) != 0,
|
||||
HasAlpha = (header[10] & 0x20) != 0,
|
||||
LastChunkSize = BigEndian.ToInt32 (header, 0x14),
|
||||
ChunkCount = BigEndian.ToUInt16 (header, 0x18),
|
||||
};
|
||||
}
|
||||
|
||||
public override ImageData Read (IBinaryStream file, ImageMetaData info)
|
||||
{
|
||||
var reader = new LagReader (file, (LagMetaData)info);
|
||||
var pixels = reader.Unpack();
|
||||
return ImageData.Create (info, reader.Format, reader.Palette, pixels);
|
||||
}
|
||||
|
||||
public override void Write (Stream file, ImageData image)
|
||||
{
|
||||
throw new System.NotImplementedException ("LagFormat.Write not implemented");
|
||||
}
|
||||
}
|
||||
|
||||
internal class LagReader
|
||||
{
|
||||
IBinaryStream m_input;
|
||||
byte[] m_output;
|
||||
LagMetaData m_info;
|
||||
|
||||
public PixelFormat Format { get; private set; }
|
||||
public BitmapPalette Palette { get; private set; }
|
||||
|
||||
public LagReader (IBinaryStream input, LagMetaData info)
|
||||
{
|
||||
m_input = input;
|
||||
m_info = info;
|
||||
m_output = new byte[4 * m_info.Width * m_info.Height];
|
||||
|
||||
switch (m_info.BPP)
|
||||
{
|
||||
case 24:
|
||||
if (m_info.HasAlpha)
|
||||
Format = PixelFormats.Bgra32;
|
||||
else
|
||||
Format = PixelFormats.Bgr24;
|
||||
break;
|
||||
|
||||
default:
|
||||
throw new NotImplementedException ("Not supported LAG color depth.");
|
||||
}
|
||||
}
|
||||
|
||||
public byte[] Unpack ()
|
||||
{
|
||||
m_input.Position = 0x20;
|
||||
if (m_info.HasPalette)
|
||||
Palette = ImageFormat.ReadPalette (m_input.AsStream, 0x100, PaletteFormat.Bgr);
|
||||
|
||||
var buffer = ReadChunks();
|
||||
using (var input = new BinMemoryStream (buffer))
|
||||
{
|
||||
int g_pos = m_info.iWidth;
|
||||
int b_pos = m_info.iWidth * 2;
|
||||
int a_pos = m_info.iWidth * 3;
|
||||
var scanline = new byte[m_info.ScanLineSize * 2];
|
||||
var unpack_buffer = new byte[scanline.Length];
|
||||
int dst = 0;
|
||||
for (int y = 0; y < m_info.iHeight; ++y)
|
||||
{
|
||||
int packed_size = Binary.BigEndian (input.ReadInt24() << 8);
|
||||
byte flags = input.ReadUInt8();
|
||||
if (packed_size > scanline.Length)
|
||||
break;
|
||||
input.Read (scanline, 0, packed_size);
|
||||
if ((flags & 4) != 0) // LZSS compression
|
||||
{
|
||||
packed_size = PckOpener.LzssUnpack (scanline, packed_size, unpack_buffer);
|
||||
var swap = scanline;
|
||||
scanline = unpack_buffer;
|
||||
unpack_buffer = swap;
|
||||
}
|
||||
if ((flags & 2) != 0) // RLE compression
|
||||
{
|
||||
packed_size = RleUnpack (scanline, packed_size, unpack_buffer);
|
||||
var swap = scanline;
|
||||
scanline = unpack_buffer;
|
||||
unpack_buffer = swap;
|
||||
}
|
||||
if ((flags & 1) != 0)
|
||||
{
|
||||
RestoreScanline (scanline, packed_size);
|
||||
}
|
||||
switch (m_info.BPP)
|
||||
{
|
||||
case 24:
|
||||
{
|
||||
int src_r = 0;
|
||||
int src_g = g_pos;
|
||||
int src_b = b_pos;
|
||||
int src_a = a_pos;
|
||||
for (int x = 0; x < m_info.iWidth; ++x)
|
||||
{
|
||||
m_output[dst++] = scanline[src_b++];
|
||||
m_output[dst++] = scanline[src_g++];
|
||||
m_output[dst++] = scanline[src_r++];
|
||||
if (m_info.HasAlpha)
|
||||
m_output[dst++] = scanline[src_a++];
|
||||
}
|
||||
break;
|
||||
}
|
||||
default:
|
||||
throw new NotImplementedException ("Not supported LAG color depth.");
|
||||
}
|
||||
}
|
||||
}
|
||||
return m_output;
|
||||
}
|
||||
|
||||
byte[] ReadChunks ()
|
||||
{
|
||||
var buffer = new byte[0x10000 * m_info.ChunkCount + m_info.LastChunkSize];
|
||||
int dst = 0;
|
||||
for (int i = 0; i <= m_info.ChunkCount; ++i)
|
||||
{
|
||||
int length = Binary.BigEndian (m_input.ReadInt32());
|
||||
bool is_compressed = length < 0;
|
||||
length &= 0x7FFFFFFF;
|
||||
if (is_compressed)
|
||||
{
|
||||
int unpacked_size = Math.Min (0x10000, buffer.Length - dst);
|
||||
using (var region = new StreamRegion (m_input.AsStream, m_input.Position, length, true))
|
||||
using (var zinput = new ZLibStream (region, CompressionMode.Decompress, true))
|
||||
{
|
||||
dst += zinput.Read (buffer, dst, unpacked_size);
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
dst += m_input.Read (buffer, dst, length);
|
||||
}
|
||||
}
|
||||
return buffer;
|
||||
}
|
||||
|
||||
int RleUnpack (byte[] input, int in_length, byte[] output)
|
||||
{
|
||||
int dst = 0;
|
||||
int src = 0;
|
||||
while (src < in_length && dst < output.Length)
|
||||
{
|
||||
byte code = input[src++];
|
||||
if (src >= in_length)
|
||||
break;
|
||||
int count = (code & 0x3F) + 1;
|
||||
if (dst + count > output.Length)
|
||||
break;
|
||||
int val;
|
||||
int i;
|
||||
switch ((code & 0xC0) >> 6)
|
||||
{
|
||||
case 0:
|
||||
for (i = 0; i < count; ++i)
|
||||
{
|
||||
int n = i & 3;
|
||||
if (n == 0)
|
||||
val = (input[src] & 0xC0) >> 6;
|
||||
else if (n == 1)
|
||||
val = (input[src] & 0x30) >> 4;
|
||||
else if (n == 2)
|
||||
val = (input[src] & 0xC) >> 2;
|
||||
else
|
||||
val = input[src++] & 3;
|
||||
if ((val & 2) != 0)
|
||||
val |= 0xFC;
|
||||
output[dst++] = (byte)val;
|
||||
}
|
||||
if ((i & 3) != 0)
|
||||
++src;
|
||||
break;
|
||||
|
||||
case 1:
|
||||
for (i = 0; i < count; ++i)
|
||||
{
|
||||
if ((i & 1) != 0)
|
||||
val = input[src++] & 0xF;
|
||||
else
|
||||
val = (input[src] & 0xF0) >> 4;
|
||||
if ((val & 8) != 0)
|
||||
val |= 0xF0;
|
||||
output[dst++] = (byte)val;
|
||||
}
|
||||
if ((i & 1) != 0)
|
||||
++src;
|
||||
break;
|
||||
|
||||
case 2:
|
||||
for (i = 0; i < count; ++i)
|
||||
{
|
||||
int n = i & 3;
|
||||
if (n == 0)
|
||||
{
|
||||
val = (input[src] & 0xFC) >> 2;
|
||||
}
|
||||
else if (n == 1)
|
||||
{
|
||||
byte v = input[src++];
|
||||
val = ((input[src] & 0xF0) >> 4) | (v & 3) << 4;
|
||||
}
|
||||
else if (n == 2)
|
||||
{
|
||||
byte v = input[src++];
|
||||
val = ((input[src] & 0xC0) >> 6) | (v & 0xF) << 2;
|
||||
}
|
||||
else
|
||||
{
|
||||
val = input[src++] & 0x3F;
|
||||
}
|
||||
if ((val & 0x20) != 0)
|
||||
val |= 0xC0;
|
||||
output[dst++] = (byte)val;
|
||||
}
|
||||
if ((i & 3) != 0)
|
||||
++src;
|
||||
break;
|
||||
|
||||
case 3:
|
||||
Buffer.BlockCopy (input, src, output, dst, count);
|
||||
src += count;
|
||||
dst += count;
|
||||
break;
|
||||
}
|
||||
}
|
||||
return dst;
|
||||
|
||||
}
|
||||
|
||||
void RestoreScanline (byte[] input, int in_length)
|
||||
{
|
||||
for (int pos = 1; pos < in_length; ++pos)
|
||||
{
|
||||
input[pos] += input[pos-1];
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user