Initial commit

This commit is contained in:
Yukai Li 2021-02-16 01:04:29 -07:00
commit 0f86b0434b
38 changed files with 2370 additions and 0 deletions

View file

@ -0,0 +1,114 @@
using System;
using System.Collections.Generic;
using System.IO;
using System.Text;
namespace LibDgf.Aqualead
{
public class AlLzDecoder
{
uint bitBuffer;
int numBitsRemaining;
BinaryReader br;
public byte[] Decode(Stream inStream)
{
br = new BinaryReader(inStream);
numBitsRemaining = 0;
if (new string(br.ReadChars(4)) != "ALLZ")
throw new InvalidDataException("Not an Aqualead LZ compressed file.");
byte version = br.ReadByte();
if (version > 1)
throw new InvalidDataException("Version too new.");
byte minLookbackLengthBits = br.ReadByte();
byte minLookbackPosBits = br.ReadByte();
byte minLiteralLengthBits = br.ReadByte();
uint decompressedLength = br.ReadUInt32();
byte[] output = new byte[decompressedLength];
int outPos = 0;
if (version == 0) ReadBits(1); // Consume literal bit if v0
bool hasLiteral = true;
while (true)
{
if (hasLiteral)
{
if (outPos >= decompressedLength) break;
int literalLength = (int)ReadEncodedNum(minLiteralLengthBits) + 1;
if (inStream.Read(output, outPos, literalLength) != literalLength)
throw new IOException("Could not read all bytes requested.");
outPos += literalLength;
//Console.WriteLine("Did literal");
}
if (outPos >= decompressedLength) break;
int lookbackPos = (int)ReadEncodedNum(minLookbackPosBits) + 1;
int lookbackLength = (int)ReadEncodedNum(minLookbackLengthBits) + 3;
for (int i = 0; i < lookbackLength; ++i)
{
output[outPos] = output[outPos - lookbackPos];
++outPos;
}
//Console.WriteLine("Did lookback");
if (outPos >= decompressedLength) break;
hasLiteral = ReadBits(1) == 0;
//Console.WriteLine("Has literal: " + hasLiteral);
}
br = null;
return output;
}
uint ReadEncodedNum(int minNumBits)
{
int numExtBits = CountBits();
uint num = ReadBits(minNumBits);
if (numExtBits > 0)
{
// Length encoding
// e e e e 0 | xxxx | yy
// Number of es indicate exponent and number of bits for mantissa
// Subsequently, ((2 ^ count(e)) - 1 + xxxx) | yy
num |= (uint)(ReadBits(numExtBits) + (1 << numExtBits) - 1) << minNumBits;
}
return num;
}
void EnsureBits(int numBits)
{
while (numBitsRemaining < numBits)
{
if (numBitsRemaining + 8 > 32) throw new ArgumentOutOfRangeException(nameof(numBits));
bitBuffer |= (uint)br.ReadByte() << numBitsRemaining;
numBitsRemaining += 8;
}
}
uint ReadBits(int numBits)
{
EnsureBits(numBits);
uint output = (uint)(bitBuffer & ((1 << numBits) - 1));
bitBuffer >>= numBits;
numBitsRemaining -= numBits;
return output;
}
int CountBits()
{
int i = 0;
while (ReadBits(1) != 0) ++i;
return i;
}
void WriteBits()
{
}
}
}

View file

@ -0,0 +1,14 @@
using System;
using System.Collections.Generic;
using System.Text;
namespace LibDgf.Aqualead.Archive
{
[Flags]
public enum AlAarArchiveFlags : byte
{
IsValid = 1 << 0,
IsSorted = 1 << 5,
IsUseNameHash = 1 << 6
}
}

View file

@ -0,0 +1,15 @@
using System;
using System.Collections.Generic;
using System.Text;
namespace LibDgf.Aqualead.Archive
{
[Flags]
public enum AlAarEntryFlags : byte
{
IsResident = 1 << 0,
IsPrepare = 1 << 1,
Unknown2 = 1 << 6,
IsUseName = 1 << 7
}
}

View file

@ -0,0 +1,49 @@
using System;
using System.Collections.Generic;
using System.IO;
using System.Text;
namespace LibDgf.Aqualead.Archive
{
public class AlAarEntryV2
{
uint rest;
public uint Id { get; set; }
public uint Offset { get; set; }
public uint Length { get; set; }
public uint Range
{
get
{
return rest & 0xffffff;
}
set
{
if (value > 0xffffff) throw new ArgumentOutOfRangeException(nameof(value));
rest = value | (rest & 0xff000000);
}
}
public AlAarEntryFlags Flags
{
get
{
return (AlAarEntryFlags)((rest >> 24) & 0xff);
}
set
{
if ((uint)value > 0xff) throw new ArgumentOutOfRangeException(nameof(value));
rest = ((uint)value << 24) | (rest & 0xffffff);
}
}
public string Name { get; set; }
public void Read(BinaryReader br)
{
Id = br.ReadUInt32();
Offset = br.ReadUInt32();
Length = br.ReadUInt32();
rest = br.ReadUInt32();
}
}
}

View file

@ -0,0 +1,24 @@
using System;
using System.Collections.Generic;
using System.IO;
using System.Text;
namespace LibDgf.Aqualead.Archive
{
public class AlAarHeaderV2
{
// Magic and version omitted, handled in archive class
public AlAarArchiveFlags Flags { get; set; }
public ushort Count { get; set; }
public uint LowId { get; set; }
public uint HighId { get; set; }
public void Read(BinaryReader br)
{
Flags = (AlAarArchiveFlags)br.ReadByte();
Count = br.ReadUInt16();
LowId = br.ReadUInt32();
HighId = br.ReadUInt32();
}
}
}

View file

@ -0,0 +1,108 @@
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Diagnostics;
using System.IO;
using System.Text;
using GMWare.IO;
namespace LibDgf.Aqualead.Archive
{
public class AlArchiveV2 : IDisposable
{
AlAarHeaderV2 header;
List<AlAarEntryV2> entries;
Stream stream;
private bool disposedValue;
public AlArchiveV2(Stream stream)
{
this.stream = stream ?? throw new ArgumentNullException(nameof(stream));
Load();
}
public IList<AlAarEntryV2> Entries
{
get
{
CheckDisposed();
return new ReadOnlyCollection<AlAarEntryV2>(entries);
}
}
void Load()
{
BinaryReader br = new BinaryReader(stream);
if (new string(br.ReadChars(4)) != "ALAR")
throw new InvalidDataException("Not an AquaLead archive.");
if (br.ReadByte() != 2)
throw new NotSupportedException("Not version 2 archive.");
header = new AlAarHeaderV2();
header.Read(br);
entries = new List<AlAarEntryV2>();
for (int i = 0; i < header.Count; ++i)
{
var entry = new AlAarEntryV2();
entry.Read(br);
entries.Add(entry);
}
foreach (var entry in entries)
{
if ((entry.Flags & AlAarEntryFlags.IsUseName) != 0)
{
stream.Seek(entry.Offset - 0x22, SeekOrigin.Begin);
entry.Name = StringReadingHelper.ReadNullTerminatedStringFromFixedSizeBlock(br, 0x20, Encoding.UTF8);
}
if ((entry.Flags & ~AlAarEntryFlags.IsUseName) != 0)
{
Console.WriteLine($"Entry {entry.Name} has other flags set: {entry.Flags}");
//Debugger.Break();
}
}
}
public Stream GetFile(AlAarEntryV2 entry)
{
if (!entries.Contains(entry))
throw new ArgumentException("Entry not from this archive.", nameof(entry));
if (stream == null) throw new InvalidOperationException("Archive is not opened from existing.");
CheckDisposed();
stream.Seek(entry.Offset, SeekOrigin.Begin);
MemoryStream ms = new MemoryStream();
StreamUtils.StreamCopy(stream, ms, entry.Length);
ms.Seek(0, SeekOrigin.Begin);
return Utils.CheckDecompress(ms);
}
void CheckDisposed()
{
if (disposedValue) throw new ObjectDisposedException(GetType().FullName);
}
protected virtual void Dispose(bool disposing)
{
if (!disposedValue)
{
if (disposing)
{
stream.Close();
}
entries = null;
disposedValue = true;
}
}
public void Dispose()
{
// Do not change this code. Put cleanup code in 'Dispose(bool disposing)' method
Dispose(disposing: true);
GC.SuppressFinalize(this);
}
}
}

View file

@ -0,0 +1,95 @@
using System;
using System.Collections.Generic;
using System.IO;
using System.Text;
namespace LibDgf.Aqualead.Image
{
public class AlImage
{
string pixelFormat;
public AlImageMipmapType MipmapType { get; set; }
public AlImageFlags Flags { get; set; }
public string PixelFormat
{
get
{
return pixelFormat;
}
set
{
if (value == null) throw new ArgumentNullException(nameof(value));
if (value.Length > 4) throw new ArgumentException("String length too long.", nameof(value));
pixelFormat = value;
}
}
public uint Width { get; set; }
public uint Height { get; set; }
public byte[] PlatformWork { get; set; }
public uint[] PaletteColors { get; set; }
public List<byte[]> Mipmaps { get; set; }
public void Read(BinaryReader br)
{
var baseOffset = br.BaseStream.Position;
if (new string(br.ReadChars(4)) != "ALIG")
throw new InvalidDataException("Not an AquaLead image.");
MipmapType = (AlImageMipmapType)br.ReadByte();
Flags = (AlImageFlags)br.ReadByte();
int numPaletteColors = br.ReadUInt16();
if ((Flags & AlImageFlags.PaletteAdd64K) != 0) numPaletteColors += 0x10000;
if ((Flags & AlImageFlags.PaletteAdd128K) != 0) numPaletteColors += 0x20000;
PixelFormat = new string(br.ReadChars(4));
br.ReadUInt32();
if (MipmapType != AlImageMipmapType.Platform)
{
Width = br.ReadUInt32();
Height = br.ReadUInt32();
}
else
{
Width = br.ReadUInt16();
Height = br.ReadUInt16();
}
int numMipmaps = MipmapType == AlImageMipmapType.NoMipmap ? 1 : br.ReadUInt16();
ushort paletteOffset = br.ReadUInt16();
ushort platformWorkLength = MipmapType == AlImageMipmapType.Platform ? br.ReadUInt16() : (ushort)0;
uint[] mipmapOffsets = new uint[numMipmaps + 1];
if (MipmapType == AlImageMipmapType.NoMipmap)
{
mipmapOffsets[0] = br.ReadUInt16();
}
else
{
for (int i = 0; i < numMipmaps; ++i)
{
mipmapOffsets[i] = br.ReadUInt32();
}
}
mipmapOffsets[numMipmaps] = (uint)(br.BaseStream.Length - baseOffset);
PaletteColors = new uint[numPaletteColors];
br.BaseStream.Seek(baseOffset + paletteOffset, SeekOrigin.Begin);
for (int i = 0; i < PaletteColors.Length; ++i)
{
PaletteColors[i] = br.ReadUInt32();
}
if (MipmapType == AlImageMipmapType.Platform)
{
br.BaseStream.Seek(baseOffset + 0x20, SeekOrigin.Begin);
PlatformWork = br.ReadBytes(platformWorkLength);
}
Mipmaps = new List<byte[]>();
for (int i = 0; i < numMipmaps; ++i)
{
br.BaseStream.Seek(baseOffset + mipmapOffsets[i], SeekOrigin.Begin);
Mipmaps.Add(br.ReadBytes((int)(mipmapOffsets[i + 1] - mipmapOffsets[i])));
}
}
}
}

View file

@ -0,0 +1,14 @@
using System;
using System.Collections.Generic;
using System.Text;
namespace LibDgf.Aqualead.Image
{
[Flags]
public enum AlImageFlags
{
IsUseAlpha = 1 << 0,
PaletteAdd64K = 1 << 4,
PaletteAdd128K = 1 << 5,
}
}

View file

@ -0,0 +1,13 @@
using System;
using System.Collections.Generic;
using System.Text;
namespace LibDgf.Aqualead.Image
{
public enum AlImageMipmapType
{
NoMipmap,
HasMipmap,
Platform
}
}

View file

@ -0,0 +1,12 @@
using System;
using System.Collections.Generic;
using System.Text;
namespace LibDgf.Aqualead.Texture
{
public struct AlPoint
{
public short X;
public short Y;
}
}

View file

@ -0,0 +1,63 @@
using System;
using System.Collections.Generic;
using System.IO;
using System.Text;
namespace LibDgf.Aqualead.Texture
{
public class AlTexture
{
public AlTextureFlags Flags { get; set; }
public List<AlTextureEntry> ChildTextures { get; set; } = new List<AlTextureEntry>();
public short Width { get; set; }
public short Height { get; set; }
public uint IndirectReference { get; set; }
public byte[] ImageData { get; set; }
public void Read(BinaryReader br)
{
var baseOffset = br.BaseStream.Position;
if (new string(br.ReadChars(4)) != "ALTX")
throw new InvalidDataException("Not an Aqualead texture.");
bool isMultitexture = br.ReadBoolean();
Flags = (AlTextureFlags)br.ReadByte();
ushort numTextures = br.ReadUInt16();
uint imgOffset = br.ReadUInt32();
ushort[] entryOffsets = new ushort[numTextures];
for (int i = 0; i < numTextures; ++i)
{
entryOffsets[i] = br.ReadUInt16();
}
ChildTextures.Clear();
uint entryOffset = 0;
for (int i = 0; i < numTextures; ++i)
{
entryOffset += entryOffsets[i];
br.BaseStream.Seek(baseOffset + entryOffset, SeekOrigin.Begin);
AlTextureEntry entry = new AlTextureEntry();
entry.Read(br, isMultitexture);
ChildTextures.Add(entry);
}
if ((Flags & AlTextureFlags.IsSpecial) != 0)
{
if ((Flags & AlTextureFlags.IsIndirect) != 0)
{
IndirectReference = br.ReadUInt32();
return;
}
else if ((Flags & AlTextureFlags.IsOverrideDimensions) != 0)
{
Width = br.ReadInt16();
Height = br.ReadInt16();
}
}
br.BaseStream.Seek(baseOffset + imgOffset, SeekOrigin.Begin);
ImageData = br.ReadBytes((int)(br.BaseStream.Length - br.BaseStream.Position));
}
}
}

View file

@ -0,0 +1,53 @@
using GMWare.IO;
using System;
using System.Collections.Generic;
using System.IO;
using System.Text;
namespace LibDgf.Aqualead.Texture
{
public class AlTextureEntry
{
public uint Id { get; set; }
public AlTextureEntryFlags Flags { get; set; }
public string Name { get; set; }
public List<AlXYWH> Bounds { get; set; } = new List<AlXYWH>();
public AlPoint CenterPoint { get; set; }
public void Read(BinaryReader br, bool isMultiTexture)
{
var baseOffset = br.BaseStream.Position;
Id = br.ReadUInt32();
ushort mipsCount = br.ReadUInt16();
Flags = (AlTextureEntryFlags)br.ReadByte();
br.ReadByte(); // Alignment
if (!isMultiTexture) return;
for (int i = 0; i < mipsCount; ++i)
{
Bounds.Add(new AlXYWH
{
X = br.ReadInt16(),
Y = br.ReadInt16(),
W = br.ReadUInt16(),
H = br.ReadUInt16()
});
}
if ((Flags & AlTextureEntryFlags.IsHasCenterPoint) != 0)
{
CenterPoint = new AlPoint
{
X = br.ReadInt16(),
Y = br.ReadInt16()
};
}
if ((Flags & AlTextureEntryFlags.IsHasName) != 0)
{
br.BaseStream.Seek(baseOffset - 0x20, SeekOrigin.Begin);
Name = StringReadingHelper.ReadNullTerminatedStringFromFixedSizeBlock(br, 0x20, Encoding.UTF8);
}
}
}
}

View file

@ -0,0 +1,14 @@
using System;
using System.Collections.Generic;
using System.Text;
namespace LibDgf.Aqualead.Texture
{
[Flags]
public enum AlTextureEntryFlags : byte
{
IsHasName = 1 << 0,
IsHasCenterPoint = 1 << 1,
IsFiltered = 1 << 2
}
}

View file

@ -0,0 +1,14 @@
using System;
using System.Collections.Generic;
using System.Text;
namespace LibDgf.Aqualead.Texture
{
[Flags]
public enum AlTextureFlags : byte
{
IsSpecial = 1 << 1,
IsIndirect = 1 << 2,
IsOverrideDimensions = 1 << 3
}
}

View file

@ -0,0 +1,14 @@
using System;
using System.Collections.Generic;
using System.Text;
namespace LibDgf.Aqualead.Texture
{
public struct AlXYWH
{
public short X;
public short Y;
public ushort W;
public ushort H;
}
}