ace/Source/ACE.DatLoader/UnpackableExtensions.cs
gmriggs b215a34ce7
Improving DatLoader for third-party apps (#1092)
* Improving DatLoader for third-party apps

* Adding some missing filetypes (thanks OptimShi!)

* Adding more enums, using SmartArray

* Re-adding loadCell

* Reverting to UInt32
2018-11-22 01:33:17 -05:00

254 lines
7.9 KiB
C#

using System.Collections.Generic;
using System.IO;
namespace ACE.DatLoader
{
static class UnpackableExtensions
{
/// <summary>
/// A SmartArray uses a Compressed UInt32 for the length.
/// </summary>
public static void UnpackSmartArray(this List<int> value, BinaryReader reader)
{
var totalObjects = reader.ReadCompressedUInt32();
for (int i = 0; i < totalObjects; i++)
{
var item = reader.ReadInt32();
value.Add(item);
}
}
/// <summary>
/// A SmartArray uses a Compressed UInt32 for the length.
/// </summary>
public static void UnpackSmartArray(this List<uint> value, BinaryReader reader)
{
var totalObjects = reader.ReadCompressedUInt32();
for (int i = 0; i < totalObjects; i++)
{
var item = reader.ReadUInt32();
value.Add(item);
}
}
/// <summary>
/// A SmartArray uses a Compressed UInt32 for the length.
/// </summary>
public static void UnpackSmartArray<T>(this List<T> value, BinaryReader reader) where T : IUnpackable, new()
{
var totalObjects = reader.ReadCompressedUInt32();
for (int i = 0; i < totalObjects; i++)
{
var item = new T();
item.Unpack(reader);
value.Add(item);
}
}
/// <summary>
/// A SmartArray uses a Compressed UInt32 for the length.
/// </summary>
public static void UnpackSmartArray<T>(this Dictionary<ushort, T> value, BinaryReader reader) where T : IUnpackable, new()
{
var totalObjects = reader.ReadCompressedUInt32();
for (int i = 0; i < totalObjects; i++)
{
var key = reader.ReadUInt16();
var item = new T();
item.Unpack(reader);
value.Add(key, item);
}
}
/// <summary>
/// A SmartArray uses a Compressed UInt32 for the length.
/// </summary>
public static void UnpackSmartArray<T>(this Dictionary<int, T> value, BinaryReader reader) where T : IUnpackable, new()
{
var totalObjects = reader.ReadCompressedUInt32();
for (int i = 0; i < totalObjects; i++)
{
var key = reader.ReadInt32();
var item = new T();
item.Unpack(reader);
value.Add(key, item);
}
}
/// <summary>
/// A SmartArray uses a Compressed UInt32 for the length.
/// </summary>
public static void UnpackSmartArray<T>(this Dictionary<uint, T> value, BinaryReader reader) where T : IUnpackable, new()
{
var totalObjects = reader.ReadCompressedUInt32();
for (int i = 0; i < totalObjects; i++)
{
var key = reader.ReadUInt32();
var item = new T();
item.Unpack(reader);
value.Add(key, item);
}
}
/// <summary>
/// A PackedHashTable uses a UInt16 for length, and a UInt16 for bucket size.
/// We don't need to worry about the bucket size with C#.
/// </summary>
public static void UnpackPackedHashTable(this Dictionary<uint, uint> value, BinaryReader reader)
{
var totalObjects = reader.ReadUInt16();
/*var bucketSize = */
reader.ReadUInt16();
for (int i = 0; i < totalObjects; i++)
value.Add(reader.ReadUInt32(), reader.ReadUInt32());
}
/// <summary>
/// A PackedHashTable uses a UInt16 for length, and a UInt16 for bucket size.
/// We don't need to worry about the bucket size with C#.
/// </summary>
public static void UnpackPackedHashTable<T>(this Dictionary<uint, T> value, BinaryReader reader) where T : IUnpackable, new()
{
var totalObjects = reader.ReadUInt16();
/*var bucketSize = */reader.ReadUInt16();
for (int i = 0; i < totalObjects; i++)
{
var key = reader.ReadUInt32();
var item = new T();
item.Unpack(reader);
value.Add(key, item);
}
}
/// <summary>
/// A list that uses a Int32 for the length.
/// </summary>
public static void Unpack(this List<uint> value, BinaryReader reader)
{
var totalObjects = reader.ReadInt32();
for (int i = 0; i < totalObjects; i++)
{
var item = reader.ReadUInt32();
value.Add(item);
}
}
/// <summary>
/// A list that uses a UInt32 for the length.
/// </summary>
public static void Unpack<T>(this List<T> value, BinaryReader reader) where T : IUnpackable, new()
{
var totalObjects = reader.ReadUInt32();
for (int i = 0; i < totalObjects; i++)
{
var item = new T();
item.Unpack(reader);
value.Add(item);
}
}
public static void Unpack<T>(this List<T> value, BinaryReader reader, uint fixedQuantity) where T : IUnpackable, new()
{
for (int i = 0; i < fixedQuantity; i++)
{
var item = new T();
item.Unpack(reader);
value.Add(item);
}
}
public static void Unpack<T>(this Dictionary<ushort, T> value, BinaryReader reader, uint fixedQuantity) where T : IUnpackable, new()
{
for (int i = 0; i < fixedQuantity; i++)
{
var key = reader.ReadUInt16();
var item = new T();
item.Unpack(reader);
value.Add(key, item);
}
}
/// <summary>
/// A Dictionary that uses a Int32 for the length.
/// </summary>
public static void Unpack<T>(this Dictionary<int, T> value, BinaryReader reader) where T : IUnpackable, new()
{
var totalObjects = reader.ReadInt32();
for (int i = 0; i < totalObjects; i++)
{
var key = reader.ReadInt32();
var item = new T();
item.Unpack(reader);
value.Add(key, item);
}
}
/// <summary>
/// A Dictionary that uses a Int32 for the length.
/// </summary>
public static void Unpack<T>(this Dictionary<uint, T> value, BinaryReader reader) where T : IUnpackable, new()
{
var totalObjects = reader.ReadUInt32();
for (int i = 0; i < totalObjects; i++)
{
var key = reader.ReadUInt32();
var item = new T();
item.Unpack(reader);
value.Add(key, item);
}
}
public static void Unpack<T>(this Dictionary<uint, T> value, BinaryReader reader, uint fixedQuantity) where T : IUnpackable, new()
{
for (int i = 0; i < fixedQuantity; i++)
{
var key = reader.ReadUInt32();
var item = new T();
item.Unpack(reader);
value.Add(key, item);
}
}
/// <summary>
/// A Dictionary that uses a Int32 for the length.
/// </summary>
public static void Unpack<T>(this Dictionary<uint, Dictionary<uint, T>> value, BinaryReader reader) where T : IUnpackable, new()
{
var totalObjects = reader.ReadUInt32();
for (int i = 0; i < totalObjects; i++)
{
var key = reader.ReadUInt32();
var values = new Dictionary<uint, T>();
values.Unpack(reader);
value.Add(key, values);
}
}
}
}