From 37fc7c5a26e0d82fad394ce3e04a4955bd3e2a56 Mon Sep 17 00:00:00 2001 From: wfowler Date: Thu, 15 Apr 2021 20:55:44 -0600 Subject: [PATCH] Add extensions to make constructing Vectors from byte arrays easier. --- LibBSP/Source/Extensions/PlaneExtensions.cs | 2 +- LibBSP/Source/Extensions/Vector2Extensions.cs | 10 ++++++ LibBSP/Source/Extensions/Vector3Extensions.cs | 10 ++++++ LibBSP/Source/Extensions/Vector4Extensions.cs | 10 ++++++ LibBSP/Source/Extensions/VertexExtensions.cs | 36 +++++++++---------- 5 files changed, 49 insertions(+), 19 deletions(-) diff --git a/LibBSP/Source/Extensions/PlaneExtensions.cs b/LibBSP/Source/Extensions/PlaneExtensions.cs index b5b7f5e..7dd87c4 100644 --- a/LibBSP/Source/Extensions/PlaneExtensions.cs +++ b/LibBSP/Source/Extensions/PlaneExtensions.cs @@ -279,7 +279,7 @@ namespace LibBSP { int numObjects = data.Length / structLength; Lump lump = new Lump(numObjects, bsp, lumpInfo); for (int i = 0; i < numObjects; ++i) { - Vector3 normal = new Vector3(BitConverter.ToSingle(data, structLength * i), BitConverter.ToSingle(data, (structLength * i) + 4), BitConverter.ToSingle(data, (structLength * i) + 8)); + Vector3 normal = Vector3Extensions.ToVector3(data, structLength * i); float distance = BitConverter.ToSingle(data, (structLength * i) + 12); lump.Add(new Plane(normal, distance)); } diff --git a/LibBSP/Source/Extensions/Vector2Extensions.cs b/LibBSP/Source/Extensions/Vector2Extensions.cs index 7cc66f4..02d0820 100644 --- a/LibBSP/Source/Extensions/Vector2Extensions.cs +++ b/LibBSP/Source/Extensions/Vector2Extensions.cs @@ -137,6 +137,16 @@ namespace LibBSP { return ret; } + /// + /// Returns a converted from eight bytes at a specified position in a byte array. + /// + /// An array of bytes. + /// The starting position within . + /// A representing the converted bytes. + public static Vector2 ToVector2(byte[] value, int startIndex) { + return new Vector2(BitConverter.ToSingle(value, startIndex), BitConverter.ToSingle(value, startIndex + 4)); + } + /// /// Gets the X component of this . /// diff --git a/LibBSP/Source/Extensions/Vector3Extensions.cs b/LibBSP/Source/Extensions/Vector3Extensions.cs index 2118f3b..6ab1407 100644 --- a/LibBSP/Source/Extensions/Vector3Extensions.cs +++ b/LibBSP/Source/Extensions/Vector3Extensions.cs @@ -149,6 +149,16 @@ namespace LibBSP { return ret; } + /// + /// Returns a converted from twelve bytes at a specified position in a byte array. + /// + /// An array of bytes. + /// The starting position within . + /// A representing the converted bytes. + public static Vector3 ToVector3(byte[] value, int startIndex) { + return new Vector3(BitConverter.ToSingle(value, startIndex), BitConverter.ToSingle(value, startIndex + 4), BitConverter.ToSingle(value, startIndex + 8)); + } + /// /// Gets the X component of this . /// diff --git a/LibBSP/Source/Extensions/Vector4Extensions.cs b/LibBSP/Source/Extensions/Vector4Extensions.cs index 4d9d834..369fe7e 100644 --- a/LibBSP/Source/Extensions/Vector4Extensions.cs +++ b/LibBSP/Source/Extensions/Vector4Extensions.cs @@ -151,6 +151,16 @@ namespace LibBSP { return ret; } + /// + /// Returns a converted from twelve bytes at a specified position in a byte array. + /// + /// An array of bytes. + /// The starting position within . + /// A representing the converted bytes. + public static Vector4 ToVector4(byte[] value, int startIndex) { + return new Vector4(BitConverter.ToSingle(value, startIndex), BitConverter.ToSingle(value, startIndex + 4), BitConverter.ToSingle(value, startIndex + 8), BitConverter.ToSingle(value, startIndex + 12)); + } + /// /// Gets the X component of this . /// diff --git a/LibBSP/Source/Extensions/VertexExtensions.cs b/LibBSP/Source/Extensions/VertexExtensions.cs index 2de8588..bb319a3 100644 --- a/LibBSP/Source/Extensions/VertexExtensions.cs +++ b/LibBSP/Source/Extensions/VertexExtensions.cs @@ -92,43 +92,43 @@ namespace LibBSP { } case MapType.CoD2: case MapType.CoD4: { - result.normal = new Vector3(BitConverter.ToSingle(data, 12), BitConverter.ToSingle(data, 16), BitConverter.ToSingle(data, 20)); + result.normal = Vector3Extensions.ToVector3(data, 12); result.color = ColorExtensions.FromArgb(data[27], data[24], data[25], data[26]); - result.uv0 = new Vector2(BitConverter.ToSingle(data, 28), BitConverter.ToSingle(data, 32)); - result.uv1 = new Vector2(BitConverter.ToSingle(data, 36), BitConverter.ToSingle(data, 40)); + result.uv0 = Vector2Extensions.ToVector2(data, 28); + result.uv1 = Vector2Extensions.ToVector2(data, 36); // Use these fields to store additional unknown information - result.tangent = new Vector4(BitConverter.ToSingle(data, 44), BitConverter.ToSingle(data, 48), BitConverter.ToSingle(data, 52), BitConverter.ToSingle(data, 56)); - result.uv3 = new Vector2(BitConverter.ToSingle(data, 60), BitConverter.ToSingle(data, 64)); + result.tangent = Vector4Extensions.ToVector4(data, 44); + result.uv3 = Vector2Extensions.ToVector2(data, 60); goto case MapType.Quake; } case MapType.MOHAA: case MapType.Quake3: case MapType.FAKK: { - result.uv0 = new Vector2(BitConverter.ToSingle(data, 12), BitConverter.ToSingle(data, 16)); - result.uv1 = new Vector2(BitConverter.ToSingle(data, 20), BitConverter.ToSingle(data, 24)); - result.normal = new Vector3(BitConverter.ToSingle(data, 28), BitConverter.ToSingle(data, 32), BitConverter.ToSingle(data, 36)); + result.uv0 = Vector2Extensions.ToVector2(data, 12); + result.uv1 = Vector2Extensions.ToVector2(data, 20); + result.normal = Vector3Extensions.ToVector3(data, 28); result.color = ColorExtensions.FromArgb(data[43], data[40], data[41], data[42]); goto case MapType.Quake; } case MapType.Raven: { - result.uv0 = new Vector2(BitConverter.ToSingle(data, 12), BitConverter.ToSingle(data, 16)); - result.uv1 = new Vector2(BitConverter.ToSingle(data, 20), BitConverter.ToSingle(data, 24)); - result.uv2 = new Vector2(BitConverter.ToSingle(data, 28), BitConverter.ToSingle(data, 32)); - result.uv3 = new Vector2(BitConverter.ToSingle(data, 36), BitConverter.ToSingle(data, 40)); - result.normal = new Vector3(BitConverter.ToSingle(data, 52), BitConverter.ToSingle(data, 56), BitConverter.ToSingle(data, 60)); + result.uv0 = Vector2Extensions.ToVector2(data, 12); + result.uv1 = Vector2Extensions.ToVector2(data, 20); + result.uv2 = Vector2Extensions.ToVector2(data, 28); + result.uv3 = Vector2Extensions.ToVector2(data, 36); + result.normal = Vector3Extensions.ToVector3(data, 52); result.color = ColorExtensions.FromArgb(data[67], data[64], data[65], data[66]); // Use for two more float fields and two more colors. // There's actually another field that seems to be color but I've only ever seen it be 0xFFFFFFFF. - result.tangent = new Vector4(BitConverter.ToSingle(data, 44), BitConverter.ToSingle(data, 48), BitConverter.ToSingle(data, 68), BitConverter.ToSingle(data, 72)); + result.tangent = Vector4Extensions.ToVector4(data, 44); goto case MapType.Quake; } case MapType.STEF2: case MapType.STEF2Demo: { - result.uv0 = new Vector2(BitConverter.ToSingle(data, 12), BitConverter.ToSingle(data, 16)); - result.uv1 = new Vector2(BitConverter.ToSingle(data, 20), BitConverter.ToSingle(data, 24)); + result.uv0 = Vector2Extensions.ToVector2(data, 12); + result.uv1 = Vector2Extensions.ToVector2(data, 20); result.uv2 = new Vector2(BitConverter.ToSingle(data, 28), 0); result.color = ColorExtensions.FromArgb(data[35], data[32], data[33], data[34]); - result.normal = new Vector3(BitConverter.ToSingle(data, 36), BitConverter.ToSingle(data, 40), BitConverter.ToSingle(data, 44)); + result.normal = Vector3Extensions.ToVector3(data, 36); goto case MapType.Quake; } case MapType.Quake: @@ -150,7 +150,7 @@ namespace LibBSP { case MapType.Daikatana: case MapType.Vindictus: case MapType.DMoMaM: { - result.position = new Vector3(BitConverter.ToSingle(data, 0), BitConverter.ToSingle(data, 4), BitConverter.ToSingle(data, 8)); + result.position = Vector3Extensions.ToVector3(data, 0); break; } default: {