Add project files.

This commit is contained in:
Razmoth
2022-09-27 17:40:31 +04:00
parent 871d908948
commit a476ace7d7
305 changed files with 71340 additions and 84 deletions

View File

@@ -0,0 +1,192 @@
using System;
using System.Collections.Generic;
using System.IO;
using System.Text;
using System.Linq;
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, int maxLength = 32767)
{
var bytes = new List<byte>();
int count = 0;
while (reader.BaseStream.Position != reader.BaseStream.Length && count < maxLength)
{
var b = reader.ReadByte();
if (b == 0)
{
break;
}
bytes.Add(b);
count++;
}
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 Color ReadColor4(this BinaryReader reader)
{
return new Color(reader.ReadSingle(), reader.ReadSingle(), reader.ReadSingle(), reader.ReadSingle());
}
public static Matrix4x4 ReadMatrix(this BinaryReader reader)
{
return new Matrix4x4(reader.ReadSingleArray(16));
}
public static int ReadMhy0Int1(this BinaryReader reader)
{
var buffer = reader.ReadBytes(7);
return buffer[1] | (buffer[6] << 8) | (buffer[3] << 0x10) | (buffer[2] << 0x18);
}
public static int ReadMhy0Int2(this BinaryReader reader)
{
var buffer = reader.ReadBytes(6);
return buffer[2] | (buffer[4] << 8) | (buffer[0] << 0x10) | (buffer[5] << 0x18);
}
public static string ReadMhy0String(this BinaryReader reader)
{
var bytes = reader.ReadBytes(0x100);
return Encoding.UTF8.GetString(bytes.TakeWhile(b => !b.Equals(0)).ToArray());
}
public static bool ReadMhy0Bool(this BinaryReader reader)
{
var value = reader.ReadMhy0Int2();
var bytes = BitConverter.GetBytes(value);
Array.Reverse(bytes);
return BitConverter.ToBoolean(bytes, 0);
}
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 byte[] ReadUInt8Array(this BinaryReader reader)
{
return reader.ReadBytes(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 uint[][] ReadUInt32ArrayArray(this BinaryReader reader)
{
return ReadArray(reader.ReadUInt32Array, reader.ReadInt32());
}
public static uint[] ReadUInt32Array(this BinaryReader reader, int length)
{
return ReadArray(reader.ReadUInt32, length);
}
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 Matrix4x4[] ReadMatrixArray(this BinaryReader reader)
{
return ReadArray(reader.ReadMatrix, reader.ReadInt32());
}
}
}

View File

@@ -0,0 +1,27 @@
using System;
using System.IO;
using System.Text;
namespace AssetStudio
{
public static class BinaryWriterExtensions
{
public static void AlignStream(this BinaryWriter writer, int alignment)
{
var pos = writer.BaseStream.Position;
var mod = pos % alignment;
if (mod != 0)
{
writer.Write(new byte[alignment - mod]);
}
}
public static void WriteAlignedString(this BinaryWriter writer, string str)
{
var bytes = Encoding.UTF8.GetBytes(str);
writer.Write(bytes.Length);
writer.Write(bytes);
writer.AlignStream(4);
}
}
}

View File

@@ -0,0 +1,107 @@
using System;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
namespace AssetStudio
{
public static class BitConverterExtensions
{
[StructLayout(LayoutKind.Explicit)]
private struct FloatUIntUnion
{
[FieldOffset(0)]
public uint Int;
[FieldOffset(0)]
public float Float;
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static uint ToUInt32(float value)
{
return new FloatUIntUnion { Float = value }.Int;
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static ulong ToUInt64(double value)
{
return unchecked((ulong)BitConverter.DoubleToInt64Bits(value));
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static float ToSingle(uint value)
{
return new FloatUIntUnion { Int = value }.Float;
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static double ToDouble(ulong value)
{
return BitConverter.Int64BitsToDouble(unchecked((long)value));
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static void GetBytes(ushort value, byte[] buffer, int offset)
{
buffer[offset + 0] = unchecked((byte)(value >> 0));
buffer[offset + 1] = unchecked((byte)(value >> 8));
}
public static void GetBytes(uint value, byte[] buffer, int offset)
{
buffer[offset + 0] = unchecked((byte)(value >> 0));
buffer[offset + 1] = unchecked((byte)(value >> 8));
buffer[offset + 2] = unchecked((byte)(value >> 16));
buffer[offset + 3] = unchecked((byte)(value >> 24));
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static short Swap(short value)
{
return unchecked((short)(Swap(unchecked((ushort)value))));
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static ushort Swap(ushort value)
{
return unchecked((ushort)(value >> 8 | value << 8));
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static int Swap(int value)
{
return unchecked((int)(Swap(unchecked((uint)value))));
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static uint Swap(uint value)
{
value = value >> 16 | value << 16;
return ((value & 0xFF00FF00) >> 8) | ((value & 0x00FF00FF) << 8);
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static long Swap(long value)
{
return unchecked((long)(Swap(unchecked((ulong)value))));
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static ulong Swap(ulong value)
{
value = value >> 32 | value << 32;
value = ((value & 0xFFFF0000FFFF0000) >> 16) | ((value & 0x0000FFFF0000FFFF) << 16);
return ((value & 0xFF00FF00FF00FF00) >> 8) | ((value & 0x00FF00FF00FF00FF) << 8);
}
public static int GetDigitsCount(uint value)
{
int count = 0;
while (value != 0)
{
value /= 10;
count++;
}
return count;
}
}
}

View File

@@ -0,0 +1,79 @@
namespace AssetStudio
{
public static class PrimitiveExtensions
{
public static int ParseDigit(this char _this)
{
return _this - '0';
}
public static string ToHexString(this byte _this)
{
return _this.ToString("x2");
}
public static string ToHexString(this short _this)
{
ushort value = unchecked((ushort)_this);
return ToHexString(value);
}
public static string ToHexString(this ushort _this)
{
ushort reverse = unchecked((ushort)(((0xFF00 & _this) >> 8) | ((0x00FF & _this) << 8)));
return reverse.ToString("x4");
}
public static string ToHexString(this int _this)
{
uint value = unchecked((uint)_this);
return ToHexString(value);
}
public static string ToHexString(this uint _this)
{
uint reverse = ((0xFF000000 & _this) >> 24) | ((0x00FF0000 & _this) >> 8) | ((0x0000FF00 & _this) << 8) | ((0x000000FF & _this) << 24);
return reverse.ToString("x8");
}
public static string ToHexString(this long _this)
{
ulong value = unchecked((ulong)_this);
return ToHexString(value);
}
public static string ToHexString(this ulong _this)
{
ulong reverse = (_this & 0x00000000000000FFUL) << 56 | (_this & 0x000000000000FF00UL) << 40 |
(_this & 0x0000000000FF0000UL) << 24 | (_this & 0x00000000FF000000UL) << 8 |
(_this & 0x000000FF00000000UL) >> 8 | (_this & 0x0000FF0000000000UL) >> 24 |
(_this & 0x00FF000000000000UL) >> 40 | (_this & 0xFF00000000000000UL) >> 56;
return reverse.ToString("x16");
}
public static string ToHexString(this float _this)
{
uint value = BitConverterExtensions.ToUInt32(_this);
return ToHexString(value);
}
public static string ToHexString(this double _this)
{
ulong value = BitConverterExtensions.ToUInt64(_this);
return ToHexString(value);
}
public static int ToClosestInt(this long _this)
{
if (_this > int.MaxValue)
{
return int.MaxValue;
}
if (_this < int.MinValue)
{
return int.MinValue;
}
return unchecked((int)_this);
}
}
}

View File

@@ -0,0 +1,24 @@
using System.IO;
namespace AssetStudio
{
public static class StreamExtensions
{
private const int BufferSize = 81920;
public static void CopyTo(this Stream source, Stream destination, long size)
{
var buffer = new byte[BufferSize];
for (var left = size; left > 0; left -= BufferSize)
{
int toRead = BufferSize < left ? BufferSize : (int)left;
int read = source.Read(buffer, 0, toRead);
destination.Write(buffer, 0, read);
if (read != toRead)
{
return;
}
}
}
}
}

View File

@@ -0,0 +1,87 @@
using System.Text;
namespace AssetStudio
{
public static class StringBuilderExtensions
{
static StringBuilderExtensions()
{
for (int i = 0; i <= byte.MaxValue; i++)
{
ByteHexRepresentations[i] = i.ToString("x2");
}
}
public static StringBuilder AppendHex(this StringBuilder _this, byte value)
{
_this.Append(ByteHexRepresentations[value]);
return _this;
}
public static StringBuilder AppendHex(this StringBuilder _this, ushort value)
{
_this.Append(ByteHexRepresentations[(value >> 0) & 0xFF]);
_this.Append(ByteHexRepresentations[(value >> 8) & 0xFF]);
return _this;
}
public static StringBuilder AppendHex(this StringBuilder _this, short value)
{
return AppendHex(_this, unchecked((ushort)value));
}
public static StringBuilder AppendHex(this StringBuilder _this, uint value)
{
_this.Append(ByteHexRepresentations[unchecked((int)(value >> 0) & 0xFF)]);
_this.Append(ByteHexRepresentations[unchecked((int)(value >> 8) & 0xFF)]);
_this.Append(ByteHexRepresentations[unchecked((int)(value >> 16) & 0xFF)]);
_this.Append(ByteHexRepresentations[unchecked((int)(value >> 24) & 0xFF)]);
return _this;
}
public static StringBuilder AppendHex(this StringBuilder _this, int value)
{
return AppendHex(_this, unchecked((uint)value));
}
public static StringBuilder AppendHex(this StringBuilder _this, ulong value)
{
_this.Append(ByteHexRepresentations[unchecked((int)(value >> 0) & 0xFF)]);
_this.Append(ByteHexRepresentations[unchecked((int)(value >> 8) & 0xFF)]);
_this.Append(ByteHexRepresentations[unchecked((int)(value >> 16) & 0xFF)]);
_this.Append(ByteHexRepresentations[unchecked((int)(value >> 24) & 0xFF)]);
_this.Append(ByteHexRepresentations[unchecked((int)(value >> 32) & 0xFF)]);
_this.Append(ByteHexRepresentations[unchecked((int)(value >> 40) & 0xFF)]);
_this.Append(ByteHexRepresentations[unchecked((int)(value >> 48) & 0xFF)]);
_this.Append(ByteHexRepresentations[unchecked((int)(value >> 56) & 0xFF)]);
return _this;
}
public static StringBuilder AppendHex(this StringBuilder _this, long value)
{
return AppendHex(_this, unchecked((ulong)value));
}
public static StringBuilder AppendHex(this StringBuilder _this, float value)
{
return AppendHex(_this, BitConverterExtensions.ToUInt32(value));
}
public static StringBuilder AppendHex(this StringBuilder _this, double value)
{
return AppendHex(_this, BitConverterExtensions.ToUInt64(value));
}
public static StringBuilder AppendIndent(this StringBuilder _this, int count)
{
for (int i = 0; i < count; i++)
{
_this.Append('\t');
}
return _this;
}
public static readonly string HexAlphabet = "0123456789abcdef";
public static readonly string[] ByteHexRepresentations = new string[256];
}
}