ThiefLightmapper/KeepersCompound.LGS/Extensions.cs

61 lines
1.8 KiB
C#
Raw Normal View History

2024-09-20 15:28:44 +00:00
using System.Numerics;
using System.Text;
namespace KeepersCompound.LGS;
public static class Extensions
{
public static Vector3 ReadRotation(this BinaryReader reader)
{
var raw = new Vector3(reader.ReadUInt16(), reader.ReadUInt16(), reader.ReadUInt16());
return raw * 360 / (ushort.MaxValue + 1);
}
2024-09-22 10:13:50 +00:00
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);
}
2024-09-20 15:28:44 +00:00
public static Vector3 ReadVec3(this BinaryReader reader)
{
return new Vector3(reader.ReadSingle(), reader.ReadSingle(), reader.ReadSingle());
}
2024-09-22 10:13:50 +00:00
public static void WriteVec3(this BinaryWriter writer, Vector3 vec)
{
writer.Write(vec.X);
writer.Write(vec.Y);
writer.Write(vec.Z);
}
2024-09-20 15:28:44 +00:00
public static Vector2 ReadVec2(this BinaryReader reader)
{
return new Vector2(reader.ReadSingle(), reader.ReadSingle());
}
2024-09-22 10:13:50 +00:00
public static void WriteVec2(this BinaryWriter writer, Vector2 vec)
{
writer.Write(vec.X);
writer.Write(vec.Y);
}
2024-09-20 15:28:44 +00:00
public static string ReadNullString(this BinaryReader reader, int length)
{
var tmpName = Encoding.UTF8.GetString(reader.ReadBytes(length));
var idx = tmpName.IndexOf('\0');
if (idx >= 0) tmpName = tmpName[..idx];
return tmpName;
}
2024-09-22 10:13:50 +00:00
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);
}
2024-09-20 15:28:44 +00:00
}