Add write extensions
This commit is contained in:
parent
8af9740eb6
commit
5c345d3277
|
@ -1,4 +1,3 @@
|
||||||
using System.IO;
|
|
||||||
using System.Numerics;
|
using System.Numerics;
|
||||||
using System.Text;
|
using System.Text;
|
||||||
|
|
||||||
|
@ -12,16 +11,37 @@ public static class Extensions
|
||||||
return raw * 360 / (ushort.MaxValue + 1);
|
return raw * 360 / (ushort.MaxValue + 1);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public static void WriteRotation(this BinaryWriter writer, Vector3 rotation)
|
||||||
|
{
|
||||||
|
var raw = rotation * (ushort.MaxValue + 1) / 360;
|
||||||
|
writer.Write((ushort)raw.X);
|
||||||
|
writer.Write((ushort)raw.Y);
|
||||||
|
writer.Write((ushort)raw.Z);
|
||||||
|
}
|
||||||
|
|
||||||
public static Vector3 ReadVec3(this BinaryReader reader)
|
public static Vector3 ReadVec3(this BinaryReader reader)
|
||||||
{
|
{
|
||||||
return new Vector3(reader.ReadSingle(), reader.ReadSingle(), reader.ReadSingle());
|
return new Vector3(reader.ReadSingle(), reader.ReadSingle(), reader.ReadSingle());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public static void WriteVec3(this BinaryWriter writer, Vector3 vec)
|
||||||
|
{
|
||||||
|
writer.Write(vec.X);
|
||||||
|
writer.Write(vec.Y);
|
||||||
|
writer.Write(vec.Z);
|
||||||
|
}
|
||||||
|
|
||||||
public static Vector2 ReadVec2(this BinaryReader reader)
|
public static Vector2 ReadVec2(this BinaryReader reader)
|
||||||
{
|
{
|
||||||
return new Vector2(reader.ReadSingle(), reader.ReadSingle());
|
return new Vector2(reader.ReadSingle(), reader.ReadSingle());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public static void WriteVec2(this BinaryWriter writer, Vector2 vec)
|
||||||
|
{
|
||||||
|
writer.Write(vec.X);
|
||||||
|
writer.Write(vec.Y);
|
||||||
|
}
|
||||||
|
|
||||||
public static string ReadNullString(this BinaryReader reader, int length)
|
public static string ReadNullString(this BinaryReader reader, int length)
|
||||||
{
|
{
|
||||||
var tmpName = Encoding.UTF8.GetString(reader.ReadBytes(length));
|
var tmpName = Encoding.UTF8.GetString(reader.ReadBytes(length));
|
||||||
|
@ -29,4 +49,12 @@ public static class Extensions
|
||||||
if (idx >= 0) tmpName = tmpName[..idx];
|
if (idx >= 0) tmpName = tmpName[..idx];
|
||||||
return tmpName;
|
return tmpName;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public static void WriteNullString(this BinaryWriter writer, string nullString, int length)
|
||||||
|
{
|
||||||
|
var writeBytes = new byte[length];
|
||||||
|
var stringBytes = Encoding.UTF8.GetBytes(nullString);
|
||||||
|
stringBytes[..Math.Min(length, stringBytes.Length)].CopyTo(writeBytes, 0);
|
||||||
|
writer.Write(writeBytes);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue