157 lines
4.9 KiB
C#
157 lines
4.9 KiB
C#
using System;
|
|
using System.Collections.Generic;
|
|
using System.IO;
|
|
using System.Text;
|
|
using SharpDX;
|
|
|
|
namespace AssetStudio
|
|
{
|
|
public static class BinaryReaderExtensions
|
|
{
|
|
public static void AlignStream(this BinaryReader reader)
|
|
{
|
|
reader.AlignStream(4);
|
|
}
|
|
|
|
public static void AlignStream(this BinaryReader reader, int alignment)
|
|
{
|
|
var pos = reader.BaseStream.Position;
|
|
var mod = pos % alignment;
|
|
if (mod != 0)
|
|
{
|
|
reader.BaseStream.Position += alignment - mod;
|
|
}
|
|
}
|
|
|
|
public static string ReadAlignedString(this BinaryReader reader)
|
|
{
|
|
var length = reader.ReadInt32();
|
|
if (length > 0 && length < (reader.BaseStream.Length - reader.BaseStream.Position))
|
|
{
|
|
var stringData = reader.ReadBytes(length);
|
|
var result = Encoding.UTF8.GetString(stringData);
|
|
reader.AlignStream(4);
|
|
return result;
|
|
}
|
|
return "";
|
|
}
|
|
|
|
public static string ReadStringToNull(this BinaryReader reader)
|
|
{
|
|
var bytes = new List<byte>();
|
|
byte b;
|
|
while (reader.BaseStream.Position != reader.BaseStream.Length && (b = reader.ReadByte()) != 0)
|
|
bytes.Add(b);
|
|
return Encoding.UTF8.GetString(bytes.ToArray());
|
|
}
|
|
|
|
public static Quaternion ReadQuaternion(this BinaryReader reader)
|
|
{
|
|
return new Quaternion(reader.ReadSingle(), reader.ReadSingle(), reader.ReadSingle(), reader.ReadSingle());
|
|
}
|
|
|
|
public static Vector2 ReadVector2(this BinaryReader reader)
|
|
{
|
|
return new Vector2(reader.ReadSingle(), reader.ReadSingle());
|
|
}
|
|
|
|
public static Vector3 ReadVector3(this BinaryReader reader)
|
|
{
|
|
return new Vector3(reader.ReadSingle(), reader.ReadSingle(), reader.ReadSingle());
|
|
}
|
|
|
|
public static Vector4 ReadVector4(this BinaryReader reader)
|
|
{
|
|
return new Vector4(reader.ReadSingle(), reader.ReadSingle(), reader.ReadSingle(), reader.ReadSingle());
|
|
}
|
|
|
|
public static System.Drawing.RectangleF ReadRectangleF(this BinaryReader reader)
|
|
{
|
|
return new System.Drawing.RectangleF(reader.ReadSingle(), reader.ReadSingle(), reader.ReadSingle(), reader.ReadSingle());
|
|
}
|
|
|
|
public static Color4 ReadColor4(this BinaryReader reader)
|
|
{
|
|
return new Color4(reader.ReadSingle(), reader.ReadSingle(), reader.ReadSingle(), reader.ReadSingle());
|
|
}
|
|
|
|
public static Matrix ReadMatrix(this BinaryReader reader)
|
|
{
|
|
var m = new Matrix();
|
|
for (int i = 0; i < 4; i++)
|
|
{
|
|
for (int j = 0; j < 4; j++)
|
|
{
|
|
m[i, j] = reader.ReadSingle();
|
|
}
|
|
}
|
|
return m;
|
|
}
|
|
|
|
private static T[] ReadArray<T>(Func<T> del, int length)
|
|
{
|
|
var array = new T[length];
|
|
for (int i = 0; i < length; i++)
|
|
{
|
|
array[i] = del();
|
|
}
|
|
return array;
|
|
}
|
|
|
|
public static bool[] ReadBooleanArray(this BinaryReader reader)
|
|
{
|
|
return ReadArray(reader.ReadBoolean, reader.ReadInt32());
|
|
}
|
|
|
|
public static ushort[] ReadUInt16Array(this BinaryReader reader)
|
|
{
|
|
return ReadArray(reader.ReadUInt16, reader.ReadInt32());
|
|
}
|
|
|
|
public static int[] ReadInt32Array(this BinaryReader reader)
|
|
{
|
|
return ReadArray(reader.ReadInt32, reader.ReadInt32());
|
|
}
|
|
|
|
public static int[] ReadInt32Array(this BinaryReader reader, int length)
|
|
{
|
|
return ReadArray(reader.ReadInt32, length);
|
|
}
|
|
|
|
public static uint[] ReadUInt32Array(this BinaryReader reader)
|
|
{
|
|
return ReadArray(reader.ReadUInt32, reader.ReadInt32());
|
|
}
|
|
|
|
public static float[] ReadSingleArray(this BinaryReader reader)
|
|
{
|
|
return ReadArray(reader.ReadSingle, reader.ReadInt32());
|
|
}
|
|
|
|
public static float[] ReadSingleArray(this BinaryReader reader, int length)
|
|
{
|
|
return ReadArray(reader.ReadSingle, length);
|
|
}
|
|
|
|
public static string[] ReadStringArray(this BinaryReader reader)
|
|
{
|
|
return ReadArray(reader.ReadAlignedString, reader.ReadInt32());
|
|
}
|
|
|
|
public static Vector2[] ReadVector2Array(this BinaryReader reader)
|
|
{
|
|
return ReadArray(reader.ReadVector2, reader.ReadInt32());
|
|
}
|
|
|
|
public static Vector4[] ReadVector4Array(this BinaryReader reader)
|
|
{
|
|
return ReadArray(reader.ReadVector4, reader.ReadInt32());
|
|
}
|
|
|
|
public static Matrix[] ReadMatrixArray(this BinaryReader reader)
|
|
{
|
|
return ReadArray(reader.ReadMatrix, reader.ReadInt32());
|
|
}
|
|
}
|
|
}
|