2023-11-12 01:59:17 +08:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
2017-09-21 18:33:05 +08:00
|
|
|
|
Copyright(c) 2015 Neodymium
|
|
|
|
|
|
|
|
|
|
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
|
|
|
of this software and associated documentation files (the "Software"), to deal
|
|
|
|
|
in the Software without restriction, including without limitation the rights
|
|
|
|
|
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
|
|
|
copies of the Software, and to permit persons to whom the Software is
|
|
|
|
|
furnished to do so, subject to the following conditions:
|
|
|
|
|
|
|
|
|
|
The above copyright notice and this permission notice shall be included in
|
|
|
|
|
all copies or substantial portions of the Software.
|
|
|
|
|
|
|
|
|
|
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
|
|
|
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
|
|
|
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
|
|
|
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
|
|
|
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
|
|
|
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
|
|
|
|
THE SOFTWARE.
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
//shamelessly stolen
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
using SharpDX;
|
|
|
|
|
using System;
|
|
|
|
|
using System.IO;
|
2023-10-28 03:31:09 +08:00
|
|
|
|
using System.Runtime.CompilerServices;
|
2017-09-21 18:33:05 +08:00
|
|
|
|
using System.Text;
|
2023-11-12 01:59:17 +08:00
|
|
|
|
using CodeWalker.Core.Utils;
|
|
|
|
|
using System.Buffers.Binary;
|
|
|
|
|
using System.Buffers;
|
|
|
|
|
using System.Runtime.InteropServices;
|
|
|
|
|
using System.Threading.Tasks;
|
|
|
|
|
using System.Threading;
|
2024-01-07 02:41:10 +08:00
|
|
|
|
using SharpDX.Win32;
|
|
|
|
|
using CommunityToolkit.HighPerformance;
|
|
|
|
|
using System.Linq;
|
2017-09-21 18:33:05 +08:00
|
|
|
|
|
|
|
|
|
namespace CodeWalker.GameFiles
|
|
|
|
|
{
|
|
|
|
|
public enum Endianess
|
|
|
|
|
{
|
2023-11-14 23:16:59 +08:00
|
|
|
|
LittleEndian = 0,
|
|
|
|
|
BigEndian = 1,
|
2017-09-21 18:33:05 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public enum DataType
|
|
|
|
|
{
|
|
|
|
|
Byte = 0,
|
|
|
|
|
Int16 = 1,
|
|
|
|
|
Int32 = 2,
|
|
|
|
|
Int64 = 3,
|
|
|
|
|
Uint16 = 4,
|
|
|
|
|
Uint32 = 5,
|
|
|
|
|
Uint64 = 6,
|
|
|
|
|
Float = 7,
|
|
|
|
|
Double = 8,
|
|
|
|
|
String = 9,
|
|
|
|
|
}
|
|
|
|
|
|
2024-01-07 02:41:10 +08:00
|
|
|
|
[SkipLocalsInit]
|
2023-10-28 03:31:09 +08:00
|
|
|
|
public class DataReader : IDisposable
|
2017-09-21 18:33:05 +08:00
|
|
|
|
{
|
|
|
|
|
private Stream baseStream;
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Gets or sets the endianess of the underlying stream.
|
|
|
|
|
/// </summary>
|
2024-01-07 02:41:10 +08:00
|
|
|
|
public readonly Endianess Endianess = Endianess.LittleEndian;
|
2017-09-21 18:33:05 +08:00
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Gets the length of the underlying stream.
|
|
|
|
|
/// </summary>
|
2024-01-07 02:41:10 +08:00
|
|
|
|
public virtual long Length => baseStream.Length;
|
2017-09-21 18:33:05 +08:00
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Gets or sets the position within the underlying stream.
|
|
|
|
|
/// </summary>
|
|
|
|
|
public virtual long Position
|
|
|
|
|
{
|
2024-01-07 02:41:10 +08:00
|
|
|
|
get => baseStream.Position;
|
|
|
|
|
set => baseStream.Position = value;
|
2017-09-21 18:33:05 +08:00
|
|
|
|
}
|
|
|
|
|
|
2023-11-14 23:16:59 +08:00
|
|
|
|
public DataReader(Stream stream)
|
2017-09-21 18:33:05 +08:00
|
|
|
|
{
|
2023-11-12 01:59:17 +08:00
|
|
|
|
if (stream is not null)
|
|
|
|
|
{
|
|
|
|
|
this.baseStream = Stream.Synchronized(stream);
|
|
|
|
|
}
|
2023-11-14 23:16:59 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Initializes a new data reader for the specified stream.
|
|
|
|
|
/// </summary>
|
2024-01-07 02:41:10 +08:00
|
|
|
|
public DataReader(Stream stream, Endianess endianess)
|
|
|
|
|
: this(stream)
|
2023-11-14 23:16:59 +08:00
|
|
|
|
{
|
2017-09-21 18:33:05 +08:00
|
|
|
|
this.Endianess = endianess;
|
|
|
|
|
}
|
|
|
|
|
|
2024-01-07 02:41:10 +08:00
|
|
|
|
public DataReader(byte[] data, Endianess endianess = Endianess.LittleEndian)
|
|
|
|
|
: this(new MemoryStream(data))
|
2023-11-12 01:59:17 +08:00
|
|
|
|
{
|
2024-01-07 02:41:10 +08:00
|
|
|
|
this.Endianess = endianess;
|
2023-11-12 01:59:17 +08:00
|
|
|
|
}
|
|
|
|
|
|
2024-01-07 02:41:10 +08:00
|
|
|
|
public virtual Stream GetStream() => baseStream;
|
2023-11-12 01:59:17 +08:00
|
|
|
|
internal virtual void SetPositionAfterRead(Stream stream)
|
|
|
|
|
{
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2024-01-07 02:41:10 +08:00
|
|
|
|
public virtual void ReadFromStream(Span<byte> buffer, bool ignoreEndianess = false)
|
2023-11-14 23:16:59 +08:00
|
|
|
|
{
|
|
|
|
|
var stream = GetStream();
|
|
|
|
|
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
stream.Read(buffer);
|
|
|
|
|
}
|
|
|
|
|
finally
|
|
|
|
|
{
|
|
|
|
|
SetPositionAfterRead(stream);
|
|
|
|
|
}
|
|
|
|
|
|
2024-01-07 02:41:10 +08:00
|
|
|
|
if (Endianess == Endianess.BigEndian && !ignoreEndianess)
|
2023-11-14 23:16:59 +08:00
|
|
|
|
{
|
|
|
|
|
buffer.Reverse();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2017-09-21 18:33:05 +08:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// Reads data from the underlying stream. This is the only method that directly accesses
|
|
|
|
|
/// the data in the underlying stream.
|
|
|
|
|
/// </summary>
|
2024-01-07 02:41:10 +08:00
|
|
|
|
protected virtual byte[] ReadFromStream(int count, bool ignoreEndianess = false, byte[]? buffer = null)
|
2017-09-21 18:33:05 +08:00
|
|
|
|
{
|
2023-11-12 01:59:17 +08:00
|
|
|
|
var stream = GetStream();
|
2024-01-07 02:41:10 +08:00
|
|
|
|
buffer ??= GC.AllocateUninitializedArray<byte>(count);
|
2023-11-12 01:59:17 +08:00
|
|
|
|
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
stream.Read(buffer, 0, count);
|
|
|
|
|
}
|
|
|
|
|
finally
|
|
|
|
|
{
|
|
|
|
|
SetPositionAfterRead(stream);
|
|
|
|
|
}
|
2017-09-21 18:33:05 +08:00
|
|
|
|
|
|
|
|
|
// handle endianess
|
2024-01-07 02:41:10 +08:00
|
|
|
|
if (Endianess == Endianess.BigEndian && !ignoreEndianess)
|
2017-09-21 18:33:05 +08:00
|
|
|
|
{
|
2023-10-28 03:31:09 +08:00
|
|
|
|
Array.Reverse(buffer, 0, count);
|
2017-09-21 18:33:05 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return buffer;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Reads a byte.
|
|
|
|
|
/// </summary>
|
2023-10-28 03:31:09 +08:00
|
|
|
|
public virtual byte ReadByte()
|
2017-09-21 18:33:05 +08:00
|
|
|
|
{
|
2023-11-12 01:59:17 +08:00
|
|
|
|
var stream = GetStream();
|
|
|
|
|
int result;
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
result = stream.ReadByte();
|
|
|
|
|
}
|
|
|
|
|
finally
|
|
|
|
|
{
|
|
|
|
|
SetPositionAfterRead(stream);
|
|
|
|
|
}
|
|
|
|
|
|
2023-10-28 03:31:09 +08:00
|
|
|
|
if (result == -1)
|
|
|
|
|
{
|
|
|
|
|
throw new InvalidOperationException("Tried to read from stream beyond end!");
|
|
|
|
|
}
|
2023-11-12 01:59:17 +08:00
|
|
|
|
|
2023-10-28 03:31:09 +08:00
|
|
|
|
return (byte) result;
|
2017-09-21 18:33:05 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Reads a sequence of bytes.
|
|
|
|
|
/// </summary>
|
2023-10-28 03:31:09 +08:00
|
|
|
|
public byte[] ReadBytes(int count, byte[] buffer = null)
|
2017-09-21 18:33:05 +08:00
|
|
|
|
{
|
2023-10-28 03:31:09 +08:00
|
|
|
|
return ReadFromStream(count, true, buffer);
|
2017-09-21 18:33:05 +08:00
|
|
|
|
}
|
|
|
|
|
|
2024-01-07 02:41:10 +08:00
|
|
|
|
public void ReadBytes(Span<byte> buffer)
|
|
|
|
|
{
|
|
|
|
|
ReadFromStream(buffer, true);
|
|
|
|
|
}
|
|
|
|
|
|
2017-09-21 18:33:05 +08:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// Reads a signed 16-bit value.
|
|
|
|
|
/// </summary>
|
2024-01-07 02:41:10 +08:00
|
|
|
|
[SkipLocalsInit]
|
2017-09-21 18:33:05 +08:00
|
|
|
|
public short ReadInt16()
|
|
|
|
|
{
|
2023-11-14 23:16:59 +08:00
|
|
|
|
Span<byte> _buffer = stackalloc byte[sizeof(short)];
|
|
|
|
|
ReadFromStream(_buffer, true);
|
|
|
|
|
|
|
|
|
|
if (Endianess == Endianess.LittleEndian)
|
|
|
|
|
{
|
|
|
|
|
return BinaryPrimitives.ReadInt16LittleEndian(_buffer);
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
return BinaryPrimitives.ReadInt16BigEndian(_buffer);
|
|
|
|
|
}
|
2017-09-21 18:33:05 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Reads a signed 32-bit value.
|
|
|
|
|
/// </summary>
|
2024-01-07 02:41:10 +08:00
|
|
|
|
[SkipLocalsInit]
|
2017-09-21 18:33:05 +08:00
|
|
|
|
public int ReadInt32()
|
|
|
|
|
{
|
2023-11-14 23:16:59 +08:00
|
|
|
|
Span<byte> _buffer = stackalloc byte[sizeof(int)];
|
|
|
|
|
ReadFromStream(_buffer, true);
|
|
|
|
|
|
|
|
|
|
if (Endianess == Endianess.LittleEndian)
|
|
|
|
|
{
|
|
|
|
|
return BinaryPrimitives.ReadInt32LittleEndian(_buffer);
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
return BinaryPrimitives.ReadInt32BigEndian(_buffer);
|
|
|
|
|
}
|
2017-09-21 18:33:05 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Reads a signed 64-bit value.
|
|
|
|
|
/// </summary>
|
2024-01-07 02:41:10 +08:00
|
|
|
|
[SkipLocalsInit]
|
2017-09-21 18:33:05 +08:00
|
|
|
|
public long ReadInt64()
|
|
|
|
|
{
|
2023-11-14 23:16:59 +08:00
|
|
|
|
Span<byte> _buffer = stackalloc byte[sizeof(long)];
|
|
|
|
|
ReadFromStream(_buffer, true);
|
|
|
|
|
|
|
|
|
|
if (Endianess == Endianess.LittleEndian)
|
|
|
|
|
{
|
|
|
|
|
return BinaryPrimitives.ReadInt64LittleEndian(_buffer);
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
return BinaryPrimitives.ReadInt64BigEndian(_buffer);
|
|
|
|
|
}
|
2017-09-21 18:33:05 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Reads an unsigned 16-bit value.
|
|
|
|
|
/// </summary>
|
2024-01-07 02:41:10 +08:00
|
|
|
|
[SkipLocalsInit]
|
2017-09-21 18:33:05 +08:00
|
|
|
|
public ushort ReadUInt16()
|
|
|
|
|
{
|
2023-11-14 23:16:59 +08:00
|
|
|
|
Span<byte> _buffer = stackalloc byte[sizeof(ushort)];
|
|
|
|
|
ReadFromStream(_buffer, true);
|
|
|
|
|
|
|
|
|
|
if (Endianess == Endianess.LittleEndian)
|
|
|
|
|
{
|
|
|
|
|
return BinaryPrimitives.ReadUInt16LittleEndian(_buffer);
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
return BinaryPrimitives.ReadUInt16BigEndian(_buffer);
|
|
|
|
|
}
|
2017-09-21 18:33:05 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Reads an unsigned 32-bit value.
|
|
|
|
|
/// </summary>
|
2024-01-07 02:41:10 +08:00
|
|
|
|
[SkipLocalsInit]
|
2017-09-21 18:33:05 +08:00
|
|
|
|
public uint ReadUInt32()
|
|
|
|
|
{
|
2023-11-14 23:16:59 +08:00
|
|
|
|
Span<byte> _buffer = stackalloc byte[sizeof(uint)];
|
|
|
|
|
ReadFromStream(_buffer, true);
|
|
|
|
|
|
|
|
|
|
if (Endianess == Endianess.LittleEndian)
|
|
|
|
|
{
|
|
|
|
|
return BinaryPrimitives.ReadUInt32LittleEndian(_buffer);
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
return BinaryPrimitives.ReadUInt32BigEndian(_buffer);
|
|
|
|
|
}
|
2017-09-21 18:33:05 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Reads an unsigned 64-bit value.
|
|
|
|
|
/// </summary>
|
2024-01-07 02:41:10 +08:00
|
|
|
|
[SkipLocalsInit]
|
2017-09-21 18:33:05 +08:00
|
|
|
|
public ulong ReadUInt64()
|
|
|
|
|
{
|
2023-11-14 23:16:59 +08:00
|
|
|
|
Span<byte> _buffer = stackalloc byte[sizeof(ulong)];
|
|
|
|
|
ReadFromStream(_buffer, true);
|
|
|
|
|
|
|
|
|
|
if (Endianess == Endianess.LittleEndian)
|
|
|
|
|
{
|
|
|
|
|
return BinaryPrimitives.ReadUInt64LittleEndian(_buffer);
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
return BinaryPrimitives.ReadUInt64BigEndian(_buffer);
|
|
|
|
|
}
|
|
|
|
|
//return BitConverter.ToUInt64(ReadFromStream(_buffer), 0);
|
2017-09-21 18:33:05 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Reads a single precision floating point value.
|
|
|
|
|
/// </summary>
|
2024-01-07 02:41:10 +08:00
|
|
|
|
[SkipLocalsInit]
|
2017-09-21 18:33:05 +08:00
|
|
|
|
public float ReadSingle()
|
|
|
|
|
{
|
2023-11-14 23:16:59 +08:00
|
|
|
|
Span<byte> _buffer = stackalloc byte[sizeof(float)];
|
|
|
|
|
ReadFromStream(_buffer, true);
|
|
|
|
|
|
|
|
|
|
if (Endianess == Endianess.LittleEndian)
|
|
|
|
|
{
|
|
|
|
|
return BinaryPrimitives.ReadSingleLittleEndian(_buffer);
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
return BinaryPrimitives.ReadSingleBigEndian(_buffer);
|
|
|
|
|
}
|
2017-09-21 18:33:05 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Reads a double precision floating point value.
|
|
|
|
|
/// </summary>
|
2024-01-07 02:41:10 +08:00
|
|
|
|
[SkipLocalsInit]
|
2017-09-21 18:33:05 +08:00
|
|
|
|
public double ReadDouble()
|
|
|
|
|
{
|
2023-11-14 23:16:59 +08:00
|
|
|
|
Span<byte> _buffer = stackalloc byte[sizeof(double)];
|
|
|
|
|
ReadFromStream(_buffer, true);
|
|
|
|
|
|
|
|
|
|
if (Endianess == Endianess.LittleEndian)
|
|
|
|
|
{
|
|
|
|
|
return BinaryPrimitives.ReadDoubleLittleEndian(_buffer);
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
return BinaryPrimitives.ReadDoubleBigEndian(_buffer);
|
|
|
|
|
}
|
2017-09-21 18:33:05 +08:00
|
|
|
|
}
|
|
|
|
|
|
2023-11-12 01:59:17 +08:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// Reads a string.
|
|
|
|
|
/// </summary>
|
2023-11-14 23:16:59 +08:00
|
|
|
|
[SkipLocalsInit]
|
2023-11-12 01:59:17 +08:00
|
|
|
|
unsafe public string ReadStringLength(int length)
|
|
|
|
|
{
|
|
|
|
|
if (length == 0)
|
|
|
|
|
{
|
|
|
|
|
return string.Empty;
|
|
|
|
|
}
|
2024-01-07 02:41:10 +08:00
|
|
|
|
Span<byte> bytes = stackalloc byte[length];
|
2023-11-12 01:59:17 +08:00
|
|
|
|
for (int i = 0; i < length; i++)
|
|
|
|
|
{
|
|
|
|
|
bytes[i] = ReadByte();
|
|
|
|
|
}
|
|
|
|
|
|
2024-01-07 02:41:10 +08:00
|
|
|
|
return Encoding.UTF8.GetString(bytes);
|
2023-11-12 01:59:17 +08:00
|
|
|
|
//return Encoding.UTF8.GetString(bytes, Math.Min(charsRead, maxLength));
|
|
|
|
|
}
|
|
|
|
|
|
2017-09-21 18:33:05 +08:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// Reads a string.
|
|
|
|
|
/// </summary>
|
2023-11-14 23:16:59 +08:00
|
|
|
|
[SkipLocalsInit]
|
2023-10-28 03:31:09 +08:00
|
|
|
|
unsafe public string ReadString(int maxLength = 1024)
|
|
|
|
|
{
|
2023-11-14 23:16:59 +08:00
|
|
|
|
Span<byte> bytes = stackalloc byte[Math.Min(maxLength, 1024)];
|
2023-10-28 03:31:09 +08:00
|
|
|
|
var temp = ReadByte();
|
2023-11-14 23:16:59 +08:00
|
|
|
|
var bytesRead = 0;
|
|
|
|
|
var length = Length;
|
|
|
|
|
while (temp != 0 && (length == -1 || Position <= length))
|
2023-10-28 03:31:09 +08:00
|
|
|
|
{
|
2023-11-14 23:16:59 +08:00
|
|
|
|
if (bytesRead < maxLength && bytesRead < 1024)
|
2023-10-28 03:31:09 +08:00
|
|
|
|
{
|
2023-11-14 23:16:59 +08:00
|
|
|
|
bytes[bytesRead] = temp;
|
2023-10-28 03:31:09 +08:00
|
|
|
|
}
|
|
|
|
|
temp = ReadByte();
|
2023-11-14 23:16:59 +08:00
|
|
|
|
bytesRead++;
|
2023-10-28 03:31:09 +08:00
|
|
|
|
}
|
|
|
|
|
|
2024-01-07 02:41:10 +08:00
|
|
|
|
return Encoding.UTF8.GetString(bytes.Slice(0, bytesRead));
|
2023-11-12 01:59:17 +08:00
|
|
|
|
//return Encoding.UTF8.GetString(bytes, Math.Min(charsRead, maxLength));
|
2023-10-28 03:31:09 +08:00
|
|
|
|
}
|
|
|
|
|
|
2024-01-07 02:41:10 +08:00
|
|
|
|
[SkipLocalsInit]
|
2017-09-21 18:33:05 +08:00
|
|
|
|
public Vector3 ReadVector3()
|
|
|
|
|
{
|
|
|
|
|
Vector3 v = new Vector3();
|
|
|
|
|
v.X = ReadSingle();
|
|
|
|
|
v.Y = ReadSingle();
|
|
|
|
|
v.Z = ReadSingle();
|
|
|
|
|
return v;
|
|
|
|
|
}
|
2024-01-07 02:41:10 +08:00
|
|
|
|
|
|
|
|
|
[SkipLocalsInit]
|
2017-09-21 18:33:05 +08:00
|
|
|
|
public Vector4 ReadVector4()
|
|
|
|
|
{
|
|
|
|
|
Vector4 v = new Vector4();
|
|
|
|
|
v.X = ReadSingle();
|
|
|
|
|
v.Y = ReadSingle();
|
|
|
|
|
v.Z = ReadSingle();
|
|
|
|
|
v.W = ReadSingle();
|
|
|
|
|
return v;
|
|
|
|
|
}
|
|
|
|
|
|
2024-01-07 02:41:10 +08:00
|
|
|
|
[SkipLocalsInit]
|
2017-09-21 18:33:05 +08:00
|
|
|
|
public Matrix ReadMatrix()
|
|
|
|
|
{
|
|
|
|
|
Matrix m = new Matrix();
|
|
|
|
|
m.M11 = ReadSingle();
|
|
|
|
|
m.M21 = ReadSingle();
|
|
|
|
|
m.M31 = ReadSingle();
|
|
|
|
|
m.M41 = ReadSingle();
|
|
|
|
|
m.M12 = ReadSingle();
|
|
|
|
|
m.M22 = ReadSingle();
|
|
|
|
|
m.M32 = ReadSingle();
|
|
|
|
|
m.M42 = ReadSingle();
|
|
|
|
|
m.M13 = ReadSingle();
|
|
|
|
|
m.M23 = ReadSingle();
|
|
|
|
|
m.M33 = ReadSingle();
|
|
|
|
|
m.M43 = ReadSingle();
|
|
|
|
|
m.M14 = ReadSingle();
|
|
|
|
|
m.M24 = ReadSingle();
|
|
|
|
|
m.M34 = ReadSingle();
|
|
|
|
|
m.M44 = ReadSingle();
|
|
|
|
|
return m;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
//TODO: put this somewhere else...
|
|
|
|
|
public static uint SizeOf(DataType type)
|
|
|
|
|
{
|
|
|
|
|
switch (type)
|
|
|
|
|
{
|
|
|
|
|
default:
|
|
|
|
|
case DataType.Byte: return 1;
|
|
|
|
|
case DataType.Int16: return 2;
|
|
|
|
|
case DataType.Int32: return 4;
|
|
|
|
|
case DataType.Int64: return 8;
|
|
|
|
|
case DataType.Uint16: return 2;
|
|
|
|
|
case DataType.Uint32: return 4;
|
|
|
|
|
case DataType.Uint64: return 8;
|
|
|
|
|
case DataType.Float: return 4;
|
|
|
|
|
case DataType.Double: return 8;
|
|
|
|
|
case DataType.String: return 0; //how long is a string..?
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2023-10-28 03:31:09 +08:00
|
|
|
|
public virtual void Dispose()
|
|
|
|
|
{
|
|
|
|
|
baseStream?.Dispose();
|
|
|
|
|
}
|
2017-09-21 18:33:05 +08:00
|
|
|
|
}
|
|
|
|
|
|
2023-10-28 03:31:09 +08:00
|
|
|
|
public class DataWriter : IDisposable
|
2017-09-21 18:33:05 +08:00
|
|
|
|
{
|
|
|
|
|
private Stream baseStream;
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Gets or sets the endianess of the underlying stream.
|
|
|
|
|
/// </summary>
|
|
|
|
|
public Endianess Endianess
|
|
|
|
|
{
|
|
|
|
|
get;
|
2024-01-07 02:41:10 +08:00
|
|
|
|
init;
|
2017-09-21 18:33:05 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Gets the length of the underlying stream.
|
|
|
|
|
/// </summary>
|
|
|
|
|
public virtual long Length
|
|
|
|
|
{
|
|
|
|
|
get
|
|
|
|
|
{
|
|
|
|
|
return baseStream.Length;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Gets or sets the position within the underlying stream.
|
|
|
|
|
/// </summary>
|
|
|
|
|
public virtual long Position
|
|
|
|
|
{
|
|
|
|
|
get
|
|
|
|
|
{
|
|
|
|
|
return baseStream.Position;
|
|
|
|
|
}
|
|
|
|
|
set
|
|
|
|
|
{
|
|
|
|
|
baseStream.Position = value;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2023-11-12 01:59:17 +08:00
|
|
|
|
internal virtual Stream GetStream()
|
|
|
|
|
{
|
|
|
|
|
return baseStream;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
internal virtual void SetPositionAfterWrite(Stream stream)
|
|
|
|
|
{ }
|
|
|
|
|
|
2017-09-21 18:33:05 +08:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// Initializes a new data writer for the specified stream.
|
|
|
|
|
/// </summary>
|
|
|
|
|
public DataWriter(Stream stream, Endianess endianess = Endianess.LittleEndian)
|
|
|
|
|
{
|
2023-11-12 01:59:17 +08:00
|
|
|
|
if (stream is not null)
|
|
|
|
|
{
|
|
|
|
|
this.baseStream = Stream.Synchronized(stream);
|
|
|
|
|
}
|
2017-09-21 18:33:05 +08:00
|
|
|
|
this.Endianess = endianess;
|
|
|
|
|
}
|
|
|
|
|
|
2024-01-07 02:41:10 +08:00
|
|
|
|
protected bool ShouldReverse(bool ignoreEndianness) => !ignoreEndianness && Endianess == Endianess.BigEndian;
|
|
|
|
|
|
2017-09-21 18:33:05 +08:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// Writes data to the underlying stream. This is the only method that directly accesses
|
|
|
|
|
/// the data in the underlying stream.
|
|
|
|
|
/// </summary>
|
2023-11-12 01:59:17 +08:00
|
|
|
|
protected virtual void WriteToStream(byte[] value, bool ignoreEndianess = false, int count = -1, int offset = 0)
|
2017-09-21 18:33:05 +08:00
|
|
|
|
{
|
2023-11-12 01:59:17 +08:00
|
|
|
|
var stream = GetStream();
|
|
|
|
|
if (count == -1)
|
|
|
|
|
{
|
|
|
|
|
count = value.Length;
|
|
|
|
|
}
|
2024-01-07 02:41:10 +08:00
|
|
|
|
if (ShouldReverse(ignoreEndianess))
|
2017-09-21 18:33:05 +08:00
|
|
|
|
{
|
2023-11-12 01:59:17 +08:00
|
|
|
|
var buffer = ArrayPool<byte>.Shared.Rent(count);
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
Array.Copy(value, offset, buffer, 0, count);
|
|
|
|
|
Array.Reverse(buffer, 0, count);
|
|
|
|
|
stream.Write(buffer, 0, count);
|
|
|
|
|
}
|
|
|
|
|
finally
|
|
|
|
|
{
|
|
|
|
|
ArrayPool<byte>.Shared.Return(buffer);
|
|
|
|
|
}
|
2017-09-21 18:33:05 +08:00
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
2023-11-12 01:59:17 +08:00
|
|
|
|
stream.Write(value, offset, count);
|
|
|
|
|
}
|
|
|
|
|
SetPositionAfterWrite(stream);
|
|
|
|
|
}
|
|
|
|
|
|
2024-01-07 02:41:10 +08:00
|
|
|
|
protected virtual void WriteToStream(ReadOnlySpan<byte> value, bool ignoreEndianess = false)
|
2023-11-12 01:59:17 +08:00
|
|
|
|
{
|
2024-01-07 02:41:10 +08:00
|
|
|
|
if (!ShouldReverse(ignoreEndianess))
|
|
|
|
|
{
|
|
|
|
|
var stream = GetStream();
|
|
|
|
|
stream.Write(value);
|
|
|
|
|
SetPositionAfterWrite(stream);
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2023-11-12 01:59:17 +08:00
|
|
|
|
byte[] sharedBuffer = ArrayPool<byte>.Shared.Rent(value.Length);
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
value.CopyTo(sharedBuffer);
|
2024-01-07 02:41:10 +08:00
|
|
|
|
Array.Reverse(sharedBuffer, 0, value.Length);
|
|
|
|
|
var stream = GetStream();
|
|
|
|
|
|
|
|
|
|
stream.Write(sharedBuffer.AsSpan());
|
|
|
|
|
|
|
|
|
|
SetPositionAfterWrite(stream);
|
2023-11-12 01:59:17 +08:00
|
|
|
|
}
|
|
|
|
|
finally
|
|
|
|
|
{
|
|
|
|
|
ArrayPool<byte>.Shared.Return(sharedBuffer);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2024-01-07 02:41:10 +08:00
|
|
|
|
protected virtual async ValueTask WriteToStreamAsync(ReadOnlyMemory<byte> buffer, bool ignoreEndianess = false)
|
2023-11-12 01:59:17 +08:00
|
|
|
|
{
|
2024-01-07 02:41:10 +08:00
|
|
|
|
if (!ShouldReverse(ignoreEndianess))
|
2023-11-12 01:59:17 +08:00
|
|
|
|
{
|
2024-01-07 02:41:10 +08:00
|
|
|
|
var stream = GetStream();
|
|
|
|
|
await stream.WriteAsync(buffer);
|
|
|
|
|
SetPositionAfterWrite(stream);
|
2023-11-12 01:59:17 +08:00
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2024-01-07 02:41:10 +08:00
|
|
|
|
var arr = ArrayPool<byte>.Shared.Rent(buffer.Length);
|
2023-11-12 01:59:17 +08:00
|
|
|
|
try
|
|
|
|
|
{
|
2024-01-07 02:41:10 +08:00
|
|
|
|
buffer.CopyTo(arr);
|
|
|
|
|
Array.Reverse(arr, 0, buffer.Length);
|
|
|
|
|
|
|
|
|
|
var stream = GetStream();
|
|
|
|
|
|
|
|
|
|
await stream.WriteAsync(arr.AsMemory(0, buffer.Length));
|
|
|
|
|
|
|
|
|
|
SetPositionAfterWrite(stream);
|
2023-11-12 01:59:17 +08:00
|
|
|
|
}
|
|
|
|
|
finally
|
|
|
|
|
{
|
2024-01-07 02:41:10 +08:00
|
|
|
|
ArrayPool<byte>.Shared.Return(arr);
|
2017-09-21 18:33:05 +08:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Writes a byte.
|
|
|
|
|
/// </summary>
|
|
|
|
|
public void Write(byte value)
|
|
|
|
|
{
|
|
|
|
|
WriteToStream(new byte[] { value });
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Writes a sequence of bytes.
|
|
|
|
|
/// </summary>
|
|
|
|
|
public void Write(byte[] value)
|
|
|
|
|
{
|
|
|
|
|
WriteToStream(value, true);
|
|
|
|
|
}
|
|
|
|
|
|
2024-01-07 02:41:10 +08:00
|
|
|
|
public void Write(ReadOnlySpan<byte> value)
|
2023-11-12 01:59:17 +08:00
|
|
|
|
{
|
|
|
|
|
WriteToStream(value, true);
|
|
|
|
|
}
|
|
|
|
|
|
2024-01-07 02:41:10 +08:00
|
|
|
|
public ValueTask WriteAsync(Memory<byte> value)
|
2023-11-12 01:59:17 +08:00
|
|
|
|
{
|
2024-01-07 02:41:10 +08:00
|
|
|
|
return WriteToStreamAsync(value, true);
|
2023-11-12 01:59:17 +08:00
|
|
|
|
}
|
|
|
|
|
|
2017-09-21 18:33:05 +08:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// Writes a signed 16-bit value.
|
|
|
|
|
/// </summary>
|
|
|
|
|
public void Write(short value)
|
|
|
|
|
{
|
|
|
|
|
WriteToStream(BitConverter.GetBytes(value));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Writes a signed 32-bit value.
|
|
|
|
|
/// </summary>
|
|
|
|
|
public void Write(int value)
|
|
|
|
|
{
|
|
|
|
|
WriteToStream(BitConverter.GetBytes(value));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Writes a signed 64-bit value.
|
|
|
|
|
/// </summary>
|
|
|
|
|
public void Write(long value)
|
|
|
|
|
{
|
|
|
|
|
WriteToStream(BitConverter.GetBytes(value));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Writes an unsigned 16-bit value.
|
|
|
|
|
/// </summary>
|
|
|
|
|
public void Write(ushort value)
|
|
|
|
|
{
|
|
|
|
|
WriteToStream(BitConverter.GetBytes(value));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Writes an unsigned 32-bit value.
|
|
|
|
|
/// </summary>
|
|
|
|
|
public void Write(uint value)
|
|
|
|
|
{
|
|
|
|
|
WriteToStream(BitConverter.GetBytes(value));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Writes an unsigned 64-bit value.
|
|
|
|
|
/// </summary>
|
|
|
|
|
public void Write(ulong value)
|
|
|
|
|
{
|
|
|
|
|
WriteToStream(BitConverter.GetBytes(value));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Writes a single precision floating point value.
|
|
|
|
|
/// </summary>
|
|
|
|
|
public void Write(float value)
|
|
|
|
|
{
|
|
|
|
|
WriteToStream(BitConverter.GetBytes(value));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Writes a double precision floating point value.
|
|
|
|
|
/// </summary>
|
|
|
|
|
public void Write(double value)
|
|
|
|
|
{
|
|
|
|
|
WriteToStream(BitConverter.GetBytes(value));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Writes a string.
|
|
|
|
|
/// </summary>
|
|
|
|
|
public void Write(string value)
|
|
|
|
|
{
|
|
|
|
|
foreach (var c in value)
|
|
|
|
|
Write((byte)c);
|
|
|
|
|
Write((byte)0);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public void Write(Vector3 value)
|
|
|
|
|
{
|
|
|
|
|
Write(value.X);
|
|
|
|
|
Write(value.Y);
|
|
|
|
|
Write(value.Z);
|
|
|
|
|
}
|
|
|
|
|
public void Write(Vector4 value)
|
|
|
|
|
{
|
|
|
|
|
Write(value.X);
|
|
|
|
|
Write(value.Y);
|
|
|
|
|
Write(value.Z);
|
|
|
|
|
Write(value.W);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void Write(Matrix value)
|
|
|
|
|
{
|
|
|
|
|
Write(value.M11);
|
|
|
|
|
Write(value.M21);
|
|
|
|
|
Write(value.M31);
|
|
|
|
|
Write(value.M41);
|
|
|
|
|
Write(value.M12);
|
|
|
|
|
Write(value.M22);
|
|
|
|
|
Write(value.M32);
|
|
|
|
|
Write(value.M42);
|
|
|
|
|
Write(value.M13);
|
|
|
|
|
Write(value.M23);
|
|
|
|
|
Write(value.M33);
|
|
|
|
|
Write(value.M43);
|
|
|
|
|
Write(value.M14);
|
|
|
|
|
Write(value.M24);
|
|
|
|
|
Write(value.M34);
|
|
|
|
|
Write(value.M44);
|
|
|
|
|
}
|
|
|
|
|
|
2023-10-28 03:31:09 +08:00
|
|
|
|
public virtual void Dispose()
|
|
|
|
|
{
|
2023-11-12 01:59:17 +08:00
|
|
|
|
baseStream?.Dispose();
|
2023-10-28 03:31:09 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public virtual void Close()
|
|
|
|
|
{
|
2023-11-12 01:59:17 +08:00
|
|
|
|
baseStream?.Close();
|
2023-10-28 03:31:09 +08:00
|
|
|
|
}
|
2017-09-21 18:33:05 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
}
|