mirror of
https://github.com/crskycode/GARbro.git
synced 2026-07-08 01:30:18 +08:00
(Legacy): FD bitmaps.
This commit is contained in:
215
ArcFormats/Key/ArcPAK.cs
Normal file
215
ArcFormats/Key/ArcPAK.cs
Normal file
@@ -0,0 +1,215 @@
|
||||
//! \file ArcPAK.cs
|
||||
//! \date 2018 Nov 23
|
||||
//! \brief Key resource archive.
|
||||
//
|
||||
// Copyright (C) 2018 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.Text;
|
||||
|
||||
namespace GameRes.Formats.Key
|
||||
{
|
||||
[Export(typeof(ArchiveFormat))]
|
||||
public class PakOpener : ArchiveFormat
|
||||
{
|
||||
public override string Tag { get { return "PAK/KEY"; } }
|
||||
public override string Description { get { return "Key resource archive"; } }
|
||||
public override uint Signature { get { return 0; } }
|
||||
public override bool IsHierarchic { get { return false; } }
|
||||
public override bool CanWrite { get { return false; } }
|
||||
|
||||
static Encoding DefaultEncoding = Encoding.UTF8;
|
||||
|
||||
public override ArcFile TryOpen (ArcView file)
|
||||
{
|
||||
int count = file.View.ReadInt32 (4);
|
||||
if (!IsSaneCount (count))
|
||||
return null;
|
||||
uint data_offset = file.View.ReadUInt32 (0);
|
||||
if (data_offset <= 0x24 || data_offset >= file.MaxOffset)
|
||||
return null;
|
||||
uint block_size = file.View.ReadUInt32 (0xC);
|
||||
if (0 == block_size)
|
||||
return null;
|
||||
uint first_offset = data_offset / block_size;
|
||||
if (first_offset * block_size != data_offset)
|
||||
return null;
|
||||
byte flags = file.View.ReadByte (0x21);
|
||||
uint index_offset = 0x24;
|
||||
while (index_offset < data_offset)
|
||||
{
|
||||
if (file.View.ReadUInt32 (index_offset) == first_offset)
|
||||
break;
|
||||
index_offset += 4;
|
||||
}
|
||||
if (index_offset == data_offset)
|
||||
return null;
|
||||
|
||||
string[] names;
|
||||
if ((flags & 2) != 0) // index has names
|
||||
{
|
||||
var encoding = DefaultEncoding;
|
||||
names = new string[count];
|
||||
using (var input = file.CreateStream())
|
||||
{
|
||||
uint names_offset = file.View.ReadUInt32 (index_offset - 4);
|
||||
input.Position = names_offset;
|
||||
for (int i = 0; i < count; ++i)
|
||||
{
|
||||
names[i] = input.ReadCString (encoding);
|
||||
}
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
names = Enumerable.Range (0, count).Select (x => x.ToString ("D5")).ToArray();
|
||||
}
|
||||
var dir = new List<Entry> (count);
|
||||
for (int i = 0; i < count; ++i)
|
||||
{
|
||||
var entry = new Entry { Name = names[i] };
|
||||
entry.Offset = (long)file.View.ReadUInt32 (index_offset) * block_size;
|
||||
entry.Size = file.View.ReadUInt32 (index_offset+4);
|
||||
if (!entry.CheckPlacement (file.MaxOffset))
|
||||
return null;
|
||||
dir.Add (entry);
|
||||
index_offset += 8;
|
||||
}
|
||||
foreach (var entry in dir)
|
||||
{
|
||||
uint signature = file.View.ReadUInt32 (entry.Offset);
|
||||
entry.ChangeType (AutoEntry.DetectFileType (signature));
|
||||
}
|
||||
return new ArcFile (file, this, dir);
|
||||
}
|
||||
}
|
||||
|
||||
/*
|
||||
internal sealed class PakDeserializer : IDisposable
|
||||
{
|
||||
IBinaryStream m_input;
|
||||
int m_data_offset;
|
||||
int m_count1;
|
||||
int m_count2;
|
||||
int m_count3;
|
||||
int field_58;
|
||||
int field_5C;
|
||||
int field_60;
|
||||
int field_64;
|
||||
int field_68;
|
||||
|
||||
public PakDeserializer (IBinaryStream input)
|
||||
{
|
||||
m_input = input;
|
||||
}
|
||||
|
||||
public void Read ()
|
||||
{
|
||||
m_input.Position = 0;
|
||||
ReadHeader();
|
||||
}
|
||||
|
||||
void ReadHeader ()
|
||||
{
|
||||
m_data_offset = ReadInt32();
|
||||
m_count1 = ReadInt32();
|
||||
m_count2 = ReadInt32();
|
||||
m_count3 = ReadInt32();
|
||||
ReadInt32();
|
||||
ReadInt32();
|
||||
ReadInt32();
|
||||
ReadInt32();
|
||||
int n = ReadUInt8();
|
||||
int flag = ReadBit();
|
||||
ReadBit();
|
||||
for (int i = 0; i < n; ++i)
|
||||
{
|
||||
int val1 = ReadUInt8();
|
||||
int val2 = ReadUInt8();
|
||||
int val3 = ReadUInt32();
|
||||
}
|
||||
ReadAlign();
|
||||
field_58 = ReadInt32();
|
||||
field_60 = (int)m_input.Position;
|
||||
if (flag != 0)
|
||||
{
|
||||
field_68 = field_60 + 8 * m_count1;
|
||||
m_input.Position = field_68;
|
||||
}
|
||||
if (field_58 != 0)
|
||||
{
|
||||
field_5C = m_data_offset - field_58;
|
||||
}
|
||||
}
|
||||
|
||||
int m_bit_pos;
|
||||
byte m_bits;
|
||||
|
||||
int ReadInt32 ()
|
||||
{
|
||||
if (m_bit_pos != 0)
|
||||
m_bit_pos = 0;
|
||||
return m_input.ReadInt32();
|
||||
}
|
||||
|
||||
byte ReadUInt8 ()
|
||||
{
|
||||
if (m_bit_pos != 0)
|
||||
m_bit_pos = 0;
|
||||
return m_input.ReadUInt8();
|
||||
}
|
||||
|
||||
int ReadBit ()
|
||||
{
|
||||
if (0 == m_bit_pos)
|
||||
{
|
||||
m_bits = m_input.ReadUInt8();
|
||||
m_bit_pos = 8;
|
||||
}
|
||||
return (m_bits >> --m_bit_pos) & 1;
|
||||
}
|
||||
|
||||
byte[] m_align_buf = new byte[4];
|
||||
|
||||
int ReadAlign ()
|
||||
{
|
||||
int pos_align = (int)m_input.Position & 3;
|
||||
if (pos_align != 0)
|
||||
m_input.Read (m_align_buf, 0, 4 - pos_align);
|
||||
}
|
||||
|
||||
bool m_disposed = false;
|
||||
public void Dispose ()
|
||||
{
|
||||
if (!m_disposed)
|
||||
{
|
||||
m_input.Dispose();
|
||||
m_disposed = true;
|
||||
}
|
||||
}
|
||||
}
|
||||
*/
|
||||
}
|
||||
48
ArcFormats/Key/AudioOGGPAK.cs
Normal file
48
ArcFormats/Key/AudioOGGPAK.cs
Normal file
@@ -0,0 +1,48 @@
|
||||
//! \file AudioOGGPAK.cs
|
||||
//! \date 2019 Mar 29
|
||||
//! \brief Key audio resource.
|
||||
//
|
||||
// Copyright (C) 2019 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;
|
||||
|
||||
namespace GameRes.Formats.Key
|
||||
{
|
||||
[Export(typeof(AudioFormat))]
|
||||
public class OggPakAudio : AudioFormat
|
||||
{
|
||||
public override string Tag { get { return "OGGPAK"; } }
|
||||
public override string Description { get { return "Key audio resource"; } }
|
||||
public override uint Signature { get { return 0x5047474F; } } // 'OGGPAK'
|
||||
public override bool CanWrite { get { return false; } }
|
||||
|
||||
public override SoundInput TryOpen (IBinaryStream file)
|
||||
{
|
||||
var header = file.ReadHeader (0xF);
|
||||
if (!header.AsciiEqual ("OGGPAK"))
|
||||
return null;
|
||||
uint length = header.ToUInt32 (0xB);
|
||||
var input = new StreamRegion (file.AsStream, 0xF, length);
|
||||
return new OggInput (input);
|
||||
}
|
||||
}
|
||||
}
|
||||
262
ArcFormats/Key/ImageCZ.cs
Normal file
262
ArcFormats/Key/ImageCZ.cs
Normal file
@@ -0,0 +1,262 @@
|
||||
//! \file ImageCZ.cs
|
||||
//! \date 2019 Mar 14
|
||||
//! \brief Real Live compressed image format.
|
||||
//
|
||||
// Copyright (C) 2019 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.Windows.Media;
|
||||
using System.Windows.Media.Imaging;
|
||||
using GameRes.Utility;
|
||||
|
||||
namespace GameRes.Formats.Key
|
||||
{
|
||||
internal class CzMetaData : ImageMetaData
|
||||
{
|
||||
public int Version;
|
||||
public uint HeaderLength;
|
||||
}
|
||||
|
||||
[Export(typeof(ImageFormat))]
|
||||
public class CzFormat : ImageFormat
|
||||
{
|
||||
public override string Tag { get { return "CZ"; } }
|
||||
public override string Description { get { return "Key compressed image format"; } }
|
||||
public override uint Signature { get { return 0x315A43; } } // 'CZ1'
|
||||
|
||||
public CzFormat ()
|
||||
{
|
||||
Extensions = new[] { "cz", "cz0", "cz1", "cz3" };
|
||||
Signatures = new uint[] { 0x305A43, 0x315A43, 0x335A43 }; // 'CZ0', 'CZ1', 'CZ3'
|
||||
}
|
||||
|
||||
public override ImageMetaData ReadMetaData (IBinaryStream file)
|
||||
{
|
||||
var header = file.ReadHeader (0x10);
|
||||
var info = new CzMetaData {
|
||||
Width = header.ToUInt16 (8),
|
||||
Height = header.ToUInt16 (10),
|
||||
BPP = header.ToUInt16 (12),
|
||||
HeaderLength = header.ToUInt32 (4),
|
||||
Version = header[2] - '0',
|
||||
};
|
||||
if (info.HeaderLength > 0x18)
|
||||
{
|
||||
info.OffsetX = file.ReadInt16();
|
||||
info.OffsetY = file.ReadInt16();
|
||||
}
|
||||
return info;
|
||||
}
|
||||
|
||||
public override ImageData Read (IBinaryStream file, ImageMetaData info)
|
||||
{
|
||||
var reader = new CzDecoder (file, (CzMetaData)info);
|
||||
return reader.Unpack();
|
||||
}
|
||||
|
||||
public override void Write (Stream file, ImageData image)
|
||||
{
|
||||
throw new System.NotImplementedException ("CzFormat.Write not implemented");
|
||||
}
|
||||
}
|
||||
|
||||
internal class CzDecoder
|
||||
{
|
||||
IBinaryStream m_input;
|
||||
CzMetaData m_info;
|
||||
|
||||
BitmapPalette Palette { get; set; }
|
||||
PixelFormat Format { get; set; }
|
||||
|
||||
public CzDecoder (IBinaryStream input, CzMetaData info)
|
||||
{
|
||||
m_input = input;
|
||||
m_info = info;
|
||||
m_output = new byte[m_info.iWidth * m_info.iHeight * m_info.BPP / 8];
|
||||
Format = 8 == m_info.BPP ? PixelFormats.Indexed8 : PixelFormats.Bgra32;
|
||||
}
|
||||
|
||||
byte[] m_output;
|
||||
int m_dst;
|
||||
|
||||
public ImageData Unpack ()
|
||||
{
|
||||
m_input.Position = m_info.HeaderLength;
|
||||
if (8 == m_info.BPP)
|
||||
Palette = ImageFormat.ReadPalette (m_input.AsStream, 0x100, PaletteFormat.RgbA);
|
||||
switch (m_info.Version)
|
||||
{
|
||||
case 3: UnpackCz3(); break;
|
||||
case 2: UnpackCz2(); break;
|
||||
case 1: UnpackCz1(); break;
|
||||
case 0: UnpackCz0(); break;
|
||||
}
|
||||
if (32 == m_info.BPP)
|
||||
ConvertToBgrA();
|
||||
return ImageData.Create (m_info, Format, Palette, m_output);
|
||||
}
|
||||
|
||||
void UnpackCz0 ()
|
||||
{
|
||||
m_input.Read (m_output, 0, m_output.Length);
|
||||
}
|
||||
|
||||
void UnpackCz1 ()
|
||||
{
|
||||
int part_count = m_input.ReadInt32();
|
||||
var part_sizes = new int[part_count];
|
||||
int total_size = 0;
|
||||
for (int i = 0; i < part_count; ++i)
|
||||
{
|
||||
int part_size = m_input.ReadInt32() * 2;
|
||||
part_sizes[i] = part_size;
|
||||
m_input.ReadInt32(); // unpacked size
|
||||
total_size += part_size;
|
||||
}
|
||||
if (m_input.Position + total_size > m_input.Length)
|
||||
throw new InvalidFormatException();
|
||||
m_dst = 0;
|
||||
for (int i = 0; i < part_count; ++i)
|
||||
{
|
||||
m_chunkCache.Clear();
|
||||
var part = m_input.ReadBytes (part_sizes[i]);
|
||||
for (int j = 0; j < part.Length; j += 2)
|
||||
{
|
||||
byte ctl = part[j+1];
|
||||
if (0 == ctl)
|
||||
{
|
||||
m_output[m_dst++] = part[j];
|
||||
}
|
||||
else
|
||||
{
|
||||
m_dst += CopyRange (part, GetOffset (part, j), m_dst);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void UnpackCz2 ()
|
||||
{
|
||||
UnpackCz1();
|
||||
int stride = m_info.iWidth * m_info.BPP / 8 / 4;
|
||||
var pixels = new uint[m_output.Length / 4];
|
||||
Buffer.BlockCopy (m_output, 0, pixels, 0, m_output.Length);
|
||||
int third = (m_info.iHeight + 2) / 3;
|
||||
for (int y = 0; y < m_info.iHeight; ++y)
|
||||
{
|
||||
int dst = m_info.iWidth * y;
|
||||
if (y % third != 0)
|
||||
{
|
||||
for (int x = 0; x < stride; ++x)
|
||||
{
|
||||
pixels[dst + x] += pixels[dst + x - stride];
|
||||
}
|
||||
}
|
||||
}
|
||||
Buffer.BlockCopy (pixels, 0, m_output, 0, m_output.Length);
|
||||
}
|
||||
|
||||
void UnpackCz3 ()
|
||||
{
|
||||
UnpackCz1();
|
||||
int stride = m_info.iWidth * m_info.BPP / 8;
|
||||
int third = (m_info.iHeight + 2) / 3;
|
||||
for (int y = 0; y < m_info.iHeight; ++y)
|
||||
{
|
||||
int dst = y * stride;
|
||||
if (y % third != 0)
|
||||
{
|
||||
for (int x = 0; x < stride; ++x)
|
||||
{
|
||||
m_output[dst + x] += m_output[dst + x - stride];
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void ConvertToBgrA ()
|
||||
{
|
||||
for (int i = 0; i < m_output.Length; i += 4)
|
||||
{
|
||||
byte r = m_output[i];
|
||||
m_output[i] = m_output[i+2];
|
||||
m_output[i+2] = r;
|
||||
}
|
||||
}
|
||||
|
||||
struct Range
|
||||
{
|
||||
public int Start;
|
||||
public int Length;
|
||||
}
|
||||
|
||||
readonly Dictionary<int, Range> m_chunkCache = new Dictionary<int, Range>();
|
||||
|
||||
static int GetOffset (byte[] input, int src)
|
||||
{
|
||||
return ((input[src] | input[src+1] << 8) - 0x101) * 2;
|
||||
}
|
||||
|
||||
int CopyRange (byte[] input, int src, int dst)
|
||||
{
|
||||
Range range;
|
||||
if (m_chunkCache.TryGetValue (src, out range))
|
||||
{
|
||||
Binary.CopyOverlapped (m_output, range.Start, dst, range.Length);
|
||||
return range.Length;
|
||||
}
|
||||
int start_pos = dst;
|
||||
|
||||
if (input[src+1] == 0)
|
||||
m_output[dst++] = input[src];
|
||||
else if (GetOffset (input, src) == src)
|
||||
m_output[dst++] = 0;
|
||||
else
|
||||
dst += CopyRange (input, GetOffset (input, src), dst);
|
||||
|
||||
if (input[src+3] == 0)
|
||||
m_output[dst++] = input[src+2];
|
||||
else if (GetOffset (input, src+2) == src)
|
||||
m_output[dst++] = m_output[start_pos];
|
||||
else
|
||||
m_output[dst++] = CopyOne (input, GetOffset (input, src+2));
|
||||
|
||||
range.Start = start_pos;
|
||||
range.Length = dst - start_pos;
|
||||
m_chunkCache[src] = range;
|
||||
return range.Length;
|
||||
}
|
||||
|
||||
byte CopyOne (byte[] input, int src)
|
||||
{
|
||||
if (input[src+1] == 0)
|
||||
return input[src];
|
||||
else if (GetOffset (input, src) == src)
|
||||
return 0;
|
||||
else
|
||||
return CopyOne (input, GetOffset (input, src));
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user