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;
}
}

147
LibDgf/Dat/DatBuilder.cs Normal file
View file

@ -0,0 +1,147 @@
using System;
using System.Collections.Generic;
using System.IO;
using System.Text;
namespace LibDgf.Dat
{
public class DatBuilder
{
public class ReplacementEntry
{
public int Index { get; set; }
public DatReader SourceDat { get; set; }
public int SourceIndex { get; set; }
public string SourceFile { get; set; }
}
class NewEntry
{
public DatEntry ArchEntry { get; set; } = new DatEntry();
public DatEntry OrigEntry { get; set; }
public int OrigIndex { get; set; }
public ReplacementEntry ReplacementEntry { get; set; }
}
DatReader sourceDat;
public List<ReplacementEntry> ReplacementEntries { get; } = new List<ReplacementEntry>();
public DatBuilder(DatReader sourceDat = null)
{
this.sourceDat = sourceDat;
}
public void Build(Stream destStream)
{
// Check there are no duplicated indexes
HashSet<int> indexSet = new HashSet<int>();
foreach (var entry in ReplacementEntries)
{
if (entry.Index < 0) throw new InvalidOperationException("Entry with negative index present.");
indexSet.Add(entry.Index);
}
if (indexSet.Count != ReplacementEntries.Count)
{
throw new InvalidOperationException("Replacement entries with non-unique IDs present.");
}
ReplacementEntries.Sort((x, y) => x.Index.CompareTo(y.Index));
List<NewEntry> newEntries = new List<NewEntry>();
// Copy over original entries
if (sourceDat != null)
{
for (int i = 0; i < sourceDat.EntriesCount; ++i)
{
var e = sourceDat.GetEntry(i);
newEntries.Add(new NewEntry { OrigEntry = e, OrigIndex = i });
}
}
// Set replacement entries
foreach (var rep in ReplacementEntries)
{
if (rep.Index > newEntries.Count)
throw new InvalidOperationException("Replacement entries results in incontinuity.");
var newEntry = new NewEntry { ReplacementEntry = rep };
if (rep.Index == newEntries.Count)
newEntries.Add(newEntry);
else
newEntries[rep.Index] = newEntry;
}
// Update size and position
uint dataOffset = 0;
for (int i = 0; i < newEntries.Count; ++i)
{
var newEntry = newEntries[i];
newEntry.ArchEntry.Offset = dataOffset;
var repEntry = newEntry.ReplacementEntry;
if (repEntry == null)
{
newEntry.ArchEntry.Length = newEntry.OrigEntry.Length;
}
else
{
if (repEntry.SourceDat != null)
{
if (repEntry.SourceFile != null)
throw new InvalidOperationException("Replacement entries with both DAT and file source specified exist.");
newEntry.ArchEntry.Length = repEntry.SourceDat.GetEntry(repEntry.SourceIndex).Length;
}
else if (repEntry.SourceFile != null)
{
newEntry.ArchEntry.Length = (uint)new FileInfo(repEntry.SourceFile).Length;
}
else
{
newEntry.ArchEntry.Length = 0;
newEntries.RemoveAt(i);
--i;
}
}
dataOffset += newEntry.ArchEntry.Length;
}
// Write file
BinaryWriter bw = new BinaryWriter(destStream);
bw.Write("DAT\0".ToCharArray());
bw.Write(newEntries.Count);
dataOffset = (uint)(((newEntries.Count + 1) * 8 + 15) & ~15);
foreach (var newEntry in newEntries)
{
newEntry.ArchEntry.Offset += dataOffset;
newEntry.ArchEntry.Write(bw);
}
// Do we need to 16-byte align anything after the header?
if (destStream.Position < dataOffset)
bw.Write(new byte[dataOffset - destStream.Position]);
foreach (var newEntry in newEntries)
{
var repEntry = newEntry.ReplacementEntry;
if (repEntry == null)
{
bw.Write(sourceDat.GetData(newEntry.OrigIndex));
}
else
{
if (repEntry.SourceDat != null)
{
bw.Write(repEntry.SourceDat.GetData(repEntry.SourceIndex));
}
else
{
using (FileStream fs = File.OpenRead(repEntry.SourceFile))
{
fs.CopyTo(destStream);
}
}
}
}
}
}
}

25
LibDgf/Dat/DatEntry.cs Normal file
View file

@ -0,0 +1,25 @@
using System;
using System.Collections.Generic;
using System.IO;
using System.Text;
namespace LibDgf.Dat
{
public class DatEntry
{
public uint Offset { get; set; }
public uint Length { get; set; }
public void Read(BinaryReader br)
{
Offset = br.ReadUInt32();
Length = br.ReadUInt32();
}
public void Write(BinaryWriter bw)
{
bw.Write(Offset);
bw.Write(Length);
}
}
}

89
LibDgf/Dat/DatReader.cs Normal file
View file

@ -0,0 +1,89 @@
using System;
using System.Collections.Generic;
using System.IO;
using System.Text;
namespace LibDgf.Dat
{
public class DatReader : IDisposable
{
Stream stream;
BinaryReader br;
List<DatEntry> entries = new List<DatEntry>();
private bool disposedValue;
public DatReader(Stream stream)
{
this.stream = stream ?? throw new ArgumentNullException(nameof(stream));
br = new BinaryReader(stream);
if (new string(br.ReadChars(4)) != "DAT\0") throw new InvalidDataException("Not a DAT file.");
int numEntries = br.ReadInt32();
for (int i = 0; i < numEntries; ++i)
{
DatEntry entry = new DatEntry();
entry.Read(br);
entries.Add(entry);
}
}
public int EntriesCount
{
get
{
CheckDisposed();
return entries.Count;
}
}
public byte[] GetData(int index)
{
CheckDisposed();
if (index < 0) throw new ArgumentOutOfRangeException(nameof(index), "Index cannot be negative.");
if (index >= EntriesCount) throw new ArgumentOutOfRangeException(nameof(index), "Index cannot be greater than or equal to count.");
var entry = entries[index];
stream.Seek(entry.Offset, SeekOrigin.Begin);
return br.ReadBytes((int)entry.Length);
}
public DatEntry GetEntry(int index)
{
CheckDisposed();
if (index < 0) throw new ArgumentOutOfRangeException(nameof(index), "Index cannot be negative.");
if (index >= EntriesCount) throw new ArgumentOutOfRangeException(nameof(index), "Index cannot be greater than or equal to count.");
var entry = entries[index];
return new DatEntry
{
Offset = entry.Offset,
Length = entry.Length
};
}
void CheckDisposed()
{
if (disposedValue) throw new ObjectDisposedException(GetType().FullName);
}
protected virtual void Dispose(bool disposing)
{
if (!disposedValue)
{
if (disposing)
{
stream.Dispose();
}
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,167 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.IO.Compression;
namespace GMWare.IO
{
/// <summary>
/// Provides some commonly used methods for Stream manipulation.
/// </summary>
public static class StreamUtils
{
/// <summary>
/// Opens a DeflateStream for reading Zlib compressed data from the current position of the <paramref name="stream"/> parameter. Closing this Stream will not close the underlying Stream.
/// </summary>
/// <param name="stream">The Stream to create a DeflateStream from.</param>
/// <returns>The opened DeflateStream.</returns>
public static DeflateStream OpenDeflateDecompressionStreamCheap(Stream stream)
{
return OpenDeflateDecompressionStreamCheap(stream, true);
}
/// <summary>
/// Opens a DeflateStream for reading Zlib compressed data from the current position of the <paramref name="stream"/> parameter.
/// </summary>
/// <param name="stream">The Stream to create a DeflateStream from.</param>
/// <param name="leaveOpen">Specifies whether or not the underlying Stream will be left open when this Stream is closed.</param>
/// <returns>The opened DeflateStream.</returns>
public static DeflateStream OpenDeflateDecompressionStreamCheap(Stream stream, bool leaveOpen)
{
if (stream == null) throw new ArgumentNullException("stream");
stream.ReadByte();
stream.ReadByte();
return new DeflateStream(stream, CompressionMode.Decompress, leaveOpen);
}
/// <summary>
/// Copies a number of bytes from one Stream to the other. The current position of each is used.
/// </summary>
/// <param name="src">The Stream to copy from.</param>
/// <param name="dest">The Stream to copy to.</param>
/// <param name="length">The number of bytes to copy.</param>
/// <returns>Whether or not all requested bytes are copied.</returns>
[Obsolete("For backward compatibility only. Please use StreamCopy().")]
public static bool StreamCopyWithLength(Stream src, Stream dest, int length)
{
return StreamCopy(src, dest, length);
}
/// <summary>
/// Copies a number of bytes from one Stream to the other. The current position of each is used.
/// </summary>
/// <param name="src">The Stream to copy from.</param>
/// <param name="dest">The Stream to copy to.</param>
/// <param name="length">The number of bytes to copy.</param>
/// <returns>Whether or not all requested bytes are copied.</returns>
public static bool StreamCopy(Stream src, Stream dest, long length)
{
return StreamCopy(src, dest, length, null);
}
/// <summary>
/// Copies a number of bytes from one Stream to the other. The current position of each is used.
/// </summary>
/// <param name="src">The Stream to copy from.</param>
/// <param name="dest">The Stream to copy to.</param>
/// <param name="length">The number of bytes to copy.</param>
/// <param name="procDelegate">A delegate to process the read buffer before it's written to the destination.</param>
/// <returns>Whether or not all requested bytes are copied.</returns>
public static bool StreamCopy(Stream src, Stream dest, long length, StreamCopyProcessor procDelegate)
{
if (src == null) throw new ArgumentNullException("src");
if (dest == null) throw new ArgumentNullException("dest");
if (length == 0) return true;
const int BUFFER_SIZE = 4096;
byte[] buffer = new byte[BUFFER_SIZE];
int read;
long left = length;
bool continueProcessing = true;
while (continueProcessing && left / buffer.Length != 0 && (read = src.Read(buffer, 0, buffer.Length)) > 0)
{
if (procDelegate != null)
{
continueProcessing = procDelegate(buffer, read);
}
dest.Write(buffer, 0, read);
left -= read;
}
// Should stop if zero bytes have been read from stream although some should have been read
if (length > BUFFER_SIZE && left == length) return false;
if (src.CanSeek && src.Position == src.Length && left != 0) throw new EndOfStreamException();
while (continueProcessing && left > 0 && (read = src.Read(buffer, 0, (int)left)) > 0)
{
if (procDelegate != null)
{
continueProcessing = procDelegate(buffer, read);
}
dest.Write(buffer, 0, read);
left -= read;
}
return left == 0;
}
/// <summary>
/// Copies one Stream to the other. The current position of each is used.
/// </summary>
/// <param name="src">The Stream to copy from.</param>
/// <param name="dest">The Stream to copy to.</param>
/// <returns>The number of bytes copied.</returns>
public static long StreamCopy(Stream src, Stream dest)
{
return StreamCopy(src, dest, null);
}
/// <summary>
/// Copies one Stream to the other. The current position of each is used.
/// </summary>
/// <param name="src">The Stream to copy from.</param>
/// <param name="dest">The Stream to copy to.</param>
/// <param name="procDelegate">A delegate for processing a read chunk before it is written.</param>
/// <returns>The number of bytes copied.</returns>
public static long StreamCopy(Stream src, Stream dest, StreamCopyProcessor procDelegate)
{
if (src == null) throw new ArgumentNullException("src");
if (dest == null) throw new ArgumentNullException("dest");
// From Stack Overflow, probably
const int BUFFER_SIZE = 4096;
byte[] buffer = new byte[BUFFER_SIZE];
long bytesCopied = 0;
int bytesRead;
bool continueProcessing = true;
do
{
bytesRead = src.Read(buffer, 0, BUFFER_SIZE);
if (procDelegate != null)
{
continueProcessing = procDelegate(buffer, bytesRead);
}
dest.Write(buffer, 0, bytesRead);
bytesCopied += bytesRead;
}
while (continueProcessing && bytesRead != 0);
return bytesCopied;
}
/// <summary>
/// Encapsulates a method for processing bytes that are being copied.
/// </summary>
/// <param name="buffer">The bytes that have been read from the source stream and will be written to the destination stream</param>
/// <param name="bytesRead">The number of bytes that have been read from the source stream stored in <paramref name="buffer"/></param>
/// <returns></returns>
public delegate bool StreamCopyProcessor(byte[] buffer, int bytesRead);
}
}

View file

@ -0,0 +1,62 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
namespace GMWare.IO
{
/// <summary>
/// Collection of methods for reading strings using BinaryReader
/// </summary>
public static class StringReadingHelper
{
/// <summary>
/// Reads a string of a given length.
/// </summary>
/// <param name="reader">BinaryReader to read from</param>
/// <param name="length">Length of string to read</param>
/// <returns>The string that was read.</returns>
public static string ReadLengthedString(BinaryReader reader, int length)
{
return new string(reader.ReadChars(length));
}
/// <summary>
/// Reads a null terminated string.
/// </summary>
/// <param name="reader">BinaryReader to read from</param>
/// <returns>The string that was read.</returns>
public static string ReadNullTerminatedString(BinaryReader reader)
{
StringBuilder sb = new StringBuilder();
for (char ch = reader.ReadChar(); ch != '\0'; ch = reader.ReadChar())
{
sb.Append(ch);
}
return sb.ToString();
}
/// <summary>
/// Reads a null terminated string from within a fixed size block.
/// </summary>
/// <param name="reader">BinaryReader to read from</param>
/// <param name="blockLen">The length of the fixed size block</param>
/// <param name="encoding">The encoding the string is in</param>
/// <returns>The string that was read.</returns>
public static string ReadNullTerminatedStringFromFixedSizeBlock(BinaryReader reader, int blockLen, Encoding encoding)
{
byte[] data = reader.ReadBytes(blockLen);
string str = encoding.GetString(data);
int indNull = str.IndexOf('\0');
if (indNull >= 0)
{
return str.Substring(0, indNull);
}
else
{
return str;
}
}
}
}

11
LibDgf/LibDgf.csproj Normal file
View file

@ -0,0 +1,11 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>netstandard2.0</TargetFramework>
</PropertyGroup>
<ItemGroup>
<Folder Include="Txm\" />
</ItemGroup>
</Project>

68
LibDgf/Se/SeDatReader.cs Normal file
View file

@ -0,0 +1,68 @@
using LibDgf.Dat;
using System;
using System.Collections.Generic;
using System.IO;
using System.Text;
namespace LibDgf.Se
{
public class SeDatReader
{
DatReader dat;
List<SeEntry> entries = new List<SeEntry>();
byte[] seToc;
byte[] seData;
public SeDatReader(DatReader dat)
{
this.dat = dat ?? throw new ArgumentNullException(nameof(dat));
seToc = dat.GetData(0);
seData = dat.GetData(1);
using (MemoryStream ms = new MemoryStream(seToc))
{
BinaryReader br = new BinaryReader(ms);
while (true)
{
SeEntry entry = new SeEntry();
entry.Read(br);
if (entry.DataOffset == -1 && entry.DataLength == -1)
break;
entries.Add(entry);
}
}
}
public int EntriesCount
{
get
{
return entries.Count;
}
}
public byte[] GetData(int index)
{
if (index < 0) throw new ArgumentOutOfRangeException(nameof(index), "Index cannot be negative.");
if (index >= EntriesCount) throw new ArgumentOutOfRangeException(nameof(index), "Index cannot be greater than or equal to count.");
var entry = entries[index];
byte[] data = new byte[entry.DataLength];
Buffer.BlockCopy(seData, (int)entry.DataOffset, data, 0, data.Length);
return data;
}
public string GetDataAsString(int index)
{
var data = GetData(index);
StringBuilder sb = new StringBuilder();
foreach (byte b in data)
{
if (b == 0) break;
sb.Append((char)b);
}
return sb.ToString();
}
}
}

31
LibDgf/Se/SeEntry.cs Normal file
View file

@ -0,0 +1,31 @@
using System;
using System.Collections.Generic;
using System.IO;
using System.Text;
namespace LibDgf.Se
{
public class SeEntry
{
public int DataOffset { get; set; }
public int DataLength { get; set; }
public uint SeLength { get; set; }
public uint Unknown { get; set; }
public void Read(BinaryReader br)
{
DataOffset = br.ReadInt32();
DataLength = br.ReadInt32();
SeLength = br.ReadUInt32();
Unknown = br.ReadUInt32();
}
public void Write(BinaryWriter bw)
{
bw.Write(DataOffset);
bw.Write(DataLength);
bw.Write(SeLength);
bw.Write(Unknown);
}
}
}

80
LibDgf/Txm/TxmHeader.cs Normal file
View file

@ -0,0 +1,80 @@
using System;
using System.Collections.Generic;
using System.IO;
using System.Text;
namespace LibDgf.Txm
{
public class TxmHeader
{
public TxmPixelFormat ImageSourcePixelFormat { get; set; }
public TxmPixelFormat ImageVideoPixelFormat { get; set; }
public short ImageWidth { get; set; }
public short ImageHeight { get; set; }
public short ImageBufferBase { get; set; }
public TxmPixelFormat ClutPixelFormat { get; set; }
public byte Misc { get; set; } // 0x0f = level, 0x70 = count, 0x80 = fast count
public short ClutWidth { get; set; }
public short ClutHeight { get; set; }
public short ClutBufferBase { get; set; }
public void Read(BinaryReader br)
{
ImageSourcePixelFormat = (TxmPixelFormat)br.ReadByte();
ImageVideoPixelFormat = (TxmPixelFormat)br.ReadByte();
ImageWidth = br.ReadInt16();
ImageHeight = br.ReadInt16();
ImageBufferBase = br.ReadInt16();
ClutPixelFormat = (TxmPixelFormat)br.ReadByte();
Misc = br.ReadByte();
ClutWidth = br.ReadInt16();
ClutHeight = br.ReadInt16();
ClutBufferBase = br.ReadInt16();
}
public void Write(BinaryWriter bw)
{
bw.Write((byte)ImageSourcePixelFormat);
bw.Write((byte)ImageVideoPixelFormat);
bw.Write(ImageWidth);
bw.Write(ImageHeight);
bw.Write(ImageBufferBase);
bw.Write((byte)ClutPixelFormat);
bw.Write(Misc);
bw.Write(ClutWidth);
bw.Write(ClutHeight);
bw.Write(ClutBufferBase);
}
public int GetImageByteSize()
{
return GetImageMemSize(ImageSourcePixelFormat, ImageWidth, ImageHeight);
}
public int GetClutByteSize()
{
return GetImageMemSize(ClutPixelFormat, ClutWidth, ClutHeight);
}
int GetImageMemSize(TxmPixelFormat format, int width, int height)
{
switch (format)
{
case TxmPixelFormat.PSMT4:
return width * height / 2;
case TxmPixelFormat.PSMT8:
return width * height;
case TxmPixelFormat.PSMCT32:
return width * height * 4;
default:
return 0;
}
}
public override string ToString()
{
return $"{ImageSourcePixelFormat} {ImageVideoPixelFormat} {ImageWidth}x{ImageHeight} {ImageBufferBase:x4} " +
$"{ClutPixelFormat} {Misc:x2} {ClutWidth}x{ClutHeight} {ClutBufferBase:x4}";
}
}
}

View file

@ -0,0 +1,24 @@
using System;
using System.Collections.Generic;
using System.Text;
namespace LibDgf.Txm
{
public enum TxmPixelFormat : byte
{
PSMCT32 = 0x00,
PSMCT24 = 0x01,
PSMCT16 = 0x02,
PSMCT16S = 0x0a,
PSMT8 = 0x13,
PSMT4 = 0x14,
PSMT8H = 0x1b,
PSMT4HL = 0x24,
PSMT4HH = 0x2c,
PSMZ32 = 0x30,
PSMZ24 = 0x31,
PSMZ16 = 0x32,
PSMZ16S = 0x3a,
None = 0xff
}
}

29
LibDgf/Utils.cs Normal file
View file

@ -0,0 +1,29 @@
using LibDgf.Aqualead;
using System;
using System.Collections.Generic;
using System.IO;
using System.Text;
namespace LibDgf
{
public static class Utils
{
public static Stream CheckDecompress(Stream stream, bool keepOpen = false)
{
BinaryReader br = new BinaryReader(stream);
if (new string(br.ReadChars(4)) == "ALLZ")
{
stream.Seek(-4, SeekOrigin.Current);
var decoder = new AlLzDecoder();
byte[] decoded = decoder.Decode(stream);
if (!keepOpen) stream.Close();
stream = new MemoryStream(decoded);
}
else
{
stream.Seek(-4, SeekOrigin.Current);
}
return stream;
}
}
}