diff --git a/LibBSP/LibBSP.csproj b/LibBSP/LibBSP.csproj index eb7e73c..282ac8f 100644 --- a/LibBSP/LibBSP.csproj +++ b/LibBSP/LibBSP.csproj @@ -45,12 +45,11 @@ - + - - + @@ -72,19 +71,18 @@ - + - - + diff --git a/LibBSP/Source/Attributes/CountAttribute.cs b/LibBSP/Source/Attributes/CountAttribute.cs index c05a172..ca023a4 100644 --- a/LibBSP/Source/Attributes/CountAttribute.cs +++ b/LibBSP/Source/Attributes/CountAttribute.cs @@ -9,6 +9,7 @@ namespace LibBSP { /// range of objects from the specified lump through the /// method. /// + [AttributeUsage(AttributeTargets.Field | AttributeTargets.Property)] public class CountAttribute : Attribute { public string lumpName; diff --git a/LibBSP/Source/Attributes/IndexAttribute.cs b/LibBSP/Source/Attributes/IndexAttribute.cs index 8c0d7ab..c28f84d 100644 --- a/LibBSP/Source/Attributes/IndexAttribute.cs +++ b/LibBSP/Source/Attributes/IndexAttribute.cs @@ -4,11 +4,12 @@ using System.Collections.Generic; namespace LibBSP { /// /// Custom Attribute class to mark a member of a struct as an index into another lump. The - /// member this Attribute is applied to should always be paired with a member with a - /// applied to it. The two attributes can the be used to grab a range of objects + /// member this Attribute is applied to can be paired with a member with a + /// applied to it. The two attributes can the be used to grab a range of objects /// from the specified lump through the /// method. /// + [AttributeUsage(AttributeTargets.Field | AttributeTargets.Property)] public class IndexAttribute : Attribute { public string lumpName; diff --git a/LibBSP/Source/Extensions/Color32Extensions.cs b/LibBSP/Source/Extensions/ColorExtensions.cs similarity index 56% rename from LibBSP/Source/Extensions/Color32Extensions.cs rename to LibBSP/Source/Extensions/ColorExtensions.cs index 826ccd8..6aa4fa3 100644 --- a/LibBSP/Source/Extensions/Color32Extensions.cs +++ b/LibBSP/Source/Extensions/ColorExtensions.cs @@ -1,21 +1,18 @@ -#if (UNITY_2_6 || UNITY_2_6_1 || UNITY_3_0 || UNITY_3_0_0 || UNITY_3_1 || UNITY_3_2 || UNITY_3_3 || UNITY_3_4 || UNITY_3_5 || UNITY_4_0 || UNITY_4_0_1 || UNITY_4_2 || UNITY_4_3 || UNITY_4_5 || UNITY_4_6 || UNITY_5 || UNITY_5_3_OR_NEWER) +#if UNITY_2_6 || UNITY_2_6_1 || UNITY_3_0 || UNITY_3_0_0 || UNITY_3_1 || UNITY_3_2 || UNITY_3_3 || UNITY_3_4 || UNITY_3_5 || UNITY_4_0 || UNITY_4_0_1 || UNITY_4_2 || UNITY_4_3 || UNITY_4_5 || UNITY_4_6 || UNITY_5 || UNITY_5_3_OR_NEWER #define UNITY #endif +namespace LibBSP { #if UNITY -using UnityEngine; + using Color = UnityEngine.Color32; #else -using System.Drawing; + using Color = System.Drawing.Color; #endif -namespace LibBSP { -#if !UNITY - using Color32 = Color; -#endif /// /// Static class containing helper methods for Color objects. /// - public static class Color32Extensions { + public static class ColorExtensions { /// /// Constructs a new Color from the passed values. @@ -25,9 +22,9 @@ namespace LibBSP { /// Green component of the color. /// Blue component of the color. /// The resulting Color object. - public static Color32 FromArgb(int a, int r, int g, int b) { + public static Color FromArgb(int a, int r, int g, int b) { #if UNITY - return new Color32((byte)r, (byte)g, (byte)b, (byte)a); + return new Color((byte)r, (byte)g, (byte)b, (byte)a); #else return Color.FromArgb(a, r, g, b); #endif diff --git a/LibBSP/Source/Extensions/CustomAttributeExtensions.cs b/LibBSP/Source/Extensions/CustomAttributeExtensions.cs index c9e5005..cee9249 100644 --- a/LibBSP/Source/Extensions/CustomAttributeExtensions.cs +++ b/LibBSP/Source/Extensions/CustomAttributeExtensions.cs @@ -2,6 +2,7 @@ using System; using System.Reflection; namespace LibBSP { + /// /// Contains static methods for retrieving custom attributes. /// diff --git a/LibBSP/Source/Extensions/PlaneExtensions.cs b/LibBSP/Source/Extensions/PlaneExtensions.cs index 8b4bd98..58766d7 100644 --- a/LibBSP/Source/Extensions/PlaneExtensions.cs +++ b/LibBSP/Source/Extensions/PlaneExtensions.cs @@ -1,4 +1,4 @@ -#if (UNITY_2_6 || UNITY_2_6_1 || UNITY_3_0 || UNITY_3_0_0 || UNITY_3_1 || UNITY_3_2 || UNITY_3_3 || UNITY_3_4 || UNITY_3_5 || UNITY_4_0 || UNITY_4_0_1 || UNITY_4_2 || UNITY_4_3 || UNITY_4_5 || UNITY_4_6 || UNITY_5 || UNITY_5_3_OR_NEWER) +#if UNITY_2_6 || UNITY_2_6_1 || UNITY_3_0 || UNITY_3_0_0 || UNITY_3_1 || UNITY_3_2 || UNITY_3_3 || UNITY_3_4 || UNITY_3_5 || UNITY_4_0 || UNITY_4_0_1 || UNITY_4_2 || UNITY_4_3 || UNITY_4_5 || UNITY_4_6 || UNITY_5 || UNITY_5_3_OR_NEWER #define UNITY #endif @@ -9,146 +9,129 @@ using UnityEngine; #endif namespace LibBSP { -#if !UNITY - using Vector3 = Vector3d; +#if UNITY + using Vector3d = Vector3; #endif + /// /// Static class containing helper methods for objects. /// public static class PlaneExtensions { + /// - /// Intersects three s at a . Returns NaN for all components if two or more s are parallel. + /// Intersects three s at a . Returns NaN for all components if two or more s are parallel. /// /// to intersect. /// to intersect. /// to intersect. /// Point of intersection if all three s meet at a point, (NaN, NaN, NaN) otherwise. - public static Vector3 Intersection(Plane p1, Plane p2, Plane p3) { - Vector3 aN = p1.normal; - Vector3 bN = p2.normal; - Vector3 cN = p3.normal; + public static Vector3d Intersection(Plane p1, Plane p2, Plane p3) { + Vector3d aN = p1.normal; + Vector3d bN = p2.normal; + Vector3d cN = p3.normal; -#if UNITY - float partSolx1 = (bN.y * cN.z) - (bN.z * cN.y); - float partSoly1 = (bN.z * cN.x) - (bN.x * cN.z); - float partSolz1 = (bN.x * cN.y) - (bN.y * cN.x); - float det = (aN.x * partSolx1) + (aN.y * partSoly1) + (aN.z * partSolz1); -#else - double partSolx1 = (bN.y * cN.z) - (bN.z * cN.y); - double partSoly1 = (bN.z * cN.x) - (bN.x * cN.z); - double partSolz1 = (bN.x * cN.y) - (bN.y * cN.x); - double det = (aN.x * partSolx1) + (aN.y * partSoly1) + (aN.z * partSolz1); -#endif + var partSolx1 = (bN.y * cN.z) - (bN.z * cN.y); + var partSoly1 = (bN.z * cN.x) - (bN.x * cN.z); + var partSolz1 = (bN.x * cN.y) - (bN.y * cN.x); + var det = (aN.x * partSolx1) + (aN.y * partSoly1) + (aN.z * partSolz1); if (det == 0) { - return new Vector3(System.Single.NaN, System.Single.NaN, System.Single.NaN); + return new Vector3d(float.NaN, float.NaN, float.NaN); } - return new Vector3((p1.distance * partSolx1 + p2.distance * (cN.y * aN.z - cN.z * aN.y) + p3.distance * (aN.y * bN.z - aN.z * bN.y)) / det, - (p1.distance * partSoly1 + p2.distance * (aN.x * cN.z - aN.z * cN.x) + p3.distance * (bN.x * aN.z - bN.z * aN.x)) / det, - (p1.distance * partSolz1 + p2.distance * (cN.x * aN.y - cN.y * aN.x) + p3.distance * (aN.x * bN.y - aN.y * bN.x)) / det); + return new Vector3d((p1.distance * partSolx1 + p2.distance * (cN.y * aN.z - cN.z * aN.y) + p3.distance * (aN.y * bN.z - aN.z * bN.y)) / det, + (p1.distance * partSoly1 + p2.distance * (aN.x * cN.z - aN.z * cN.x) + p3.distance * (bN.x * aN.z - bN.z * aN.x)) / det, + (p1.distance * partSolz1 + p2.distance * (cN.x * aN.y - cN.y * aN.x) + p3.distance * (aN.x * bN.y - aN.y * bN.x)) / det); } /// - /// Intersects this with two other s at a . Returns NaN for all components if two or more s are parallel. + /// Intersects this with two other s at a . Returns NaN for all components if two or more s are parallel. /// /// This . /// to intersect. /// to intersect. /// Point of intersection if all three s meet at a point, (NaN, NaN, NaN) otherwise. - public static Vector3 Intersect(this Plane p1, Plane p2, Plane p3) { + public static Vector3d Intersect(this Plane p1, Plane p2, Plane p3) { return Intersection(p1, p2, p3); } /// - /// Intersects a "" with a "" at a . Returns NaN for all components if they do not intersect. + /// Intersects a "" with a "" at a . Returns NaN for all components if they do not intersect. /// /// to intersect with. /// to intersect. /// Point of intersection if "" intersects "", (NaN, NaN, NaN) otherwise. - public static Vector3 Intersection(Plane p, Ray r) { + public static Vector3d Intersection(Plane p, Ray r) { #if UNITY float enter; #else double enter; #endif bool intersected = p.Raycast(r, out enter); - if(enter != 0 || intersected) { + if (intersected || enter != 0) { return r.GetPoint(enter); } else { - return new Vector3(System.Single.NaN, System.Single.NaN, System.Single.NaN); + return new Vector3d(float.NaN, float.NaN, float.NaN); } } /// - /// Intersects this with a "" at a . Returns NaN for all components if they do not intersect. + /// Intersects this with a "" at a . Returns NaN for all components if they do not intersect. /// /// This . /// to intersect. /// Point of intersection if "" intersects this , (NaN, NaN, NaN) otherwise. - public static Vector3 Intersect(this Plane p, Ray r) { + public static Vector3d Intersect(this Plane p, Ray r) { return Intersection(p, r); } /// - /// Intersects a "" with this at a . Returns NaN for all components if they do not intersect. + /// Intersects a "" with this at a . Returns NaN for all components if they do not intersect. /// /// This . /// to intersect with. /// Point of intersection if this intersects "", (NaN, NaN, NaN) otherwise. - public static Vector3 Intersect(this Ray r, Plane p) { + public static Vector3d Intersect(this Ray r, Plane p) { return Intersection(p, r); } /// - /// Intersects two s at a . Returns NaN for all components of both s of the if the s are parallel. + /// Intersects two s at a . Returns NaN for all components of both s of the if the s are parallel. /// /// to intersect. /// to intersect. /// Line of intersection where "" intersects "", ((NaN, NaN, NaN) + p(NaN, NaN, NaN)) otherwise. public static Ray Intersection(Plane p1, Plane p2) { - Vector3 direction = Vector3.Cross(p1.normal, p2.normal); - if (direction == Vector3.zero) { - return new Ray(new Vector3(System.Single.NaN, System.Single.NaN, System.Single.NaN), new Vector3(System.Single.NaN, System.Single.NaN, System.Single.NaN)); + Vector3d direction = Vector3d.Cross(p1.normal, p2.normal); + if (direction == Vector3d.zero) { + return new Ray(new Vector3d(float.NaN, float.NaN, float.NaN), new Vector3d(float.NaN, float.NaN, float.NaN)); } // If x == 0, solve for y in terms of z, or z in terms of y - Vector3 origin; + Vector3d origin; - Vector3 sqrDirection = Vector3.Scale(direction, direction); + Vector3d sqrDirection = Vector3d.Scale(direction, direction); if (sqrDirection.x >= sqrDirection.y && sqrDirection.x >= sqrDirection.z) { -#if UNITY - float denom = (p1.normal.y * p2.normal.z) - (p2.normal.y * p1.normal.z); -#else - double denom = (p1.normal.y * p2.normal.z) - (p2.normal.y * p1.normal.z); -#endif - origin = new Vector3(0, - ((p1.normal.z * p2.distance) - (p2.normal.z * p1.distance)) / denom, - ((p2.normal.y * p1.distance) - (p1.normal.y * p2.distance)) / denom); + var denom = (p1.normal.y * p2.normal.z) - (p2.normal.y * p1.normal.z); + origin = new Vector3d(0, + ((p1.normal.z * p2.distance) - (p2.normal.z * p1.distance)) / denom, + ((p2.normal.y * p1.distance) - (p1.normal.y * p2.distance)) / denom); } else if (sqrDirection.y >= sqrDirection.x && sqrDirection.y >= sqrDirection.z) { -#if UNITY - float denom = (p1.normal.x * p2.normal.z) - (p2.normal.x * p1.normal.z); -#else - double denom = (p1.normal.x * p2.normal.z) - (p2.normal.x * p1.normal.z); -#endif - origin = new Vector3(((p1.normal.z * p2.distance) - (p2.normal.z * p1.distance)) / denom, - 0, - ((p2.normal.x * p1.distance) - (p1.normal.x * p2.distance)) / denom); + var denom = (p1.normal.x * p2.normal.z) - (p2.normal.x * p1.normal.z); + origin = new Vector3d(((p1.normal.z * p2.distance) - (p2.normal.z * p1.distance)) / denom, + 0, + ((p2.normal.x * p1.distance) - (p1.normal.x * p2.distance)) / denom); } else { -#if UNITY - float denom = (p1.normal.x * p2.normal.y) - (p2.normal.x * p1.normal.y); -#else - double denom = (p1.normal.x * p2.normal.y) - (p2.normal.x * p1.normal.y); -#endif - origin = new Vector3(((p1.normal.y * p2.distance) - (p2.normal.y * p1.distance)) / denom, - ((p2.normal.x * p1.distance) - (p1.normal.x * p2.distance)) / denom, - 0); + var denom = (p1.normal.x * p2.normal.y) - (p2.normal.x * p1.normal.y); + origin = new Vector3d(((p1.normal.y * p2.distance) - (p2.normal.y * p1.distance)) / denom, + ((p2.normal.x * p1.distance) - (p1.normal.x * p2.distance)) / denom, + 0); } return new Ray(origin, direction); } /// - /// Intersects this with another at a . Returns NaN for all components of both s of the if the s are parallel. + /// Intersects this with another at a . Returns NaN for all components of both s of the if the s are parallel. /// /// This . /// to intersect. @@ -161,65 +144,65 @@ namespace LibBSP { /// Generates three points which can be used to define this . /// /// This . - /// Scale of distance between the generated points. The points will define the same but will be farther apart the larger this value is. May not be zero. + /// Scale of distance between the generated points. The points will define the same but will be farther apart the larger this value is. Must not be zero. /// Three points which define this . - public static Vector3[] GenerateThreePoints(this Plane p, float planePointCoef = 16) { - Vector3[] points = new Vector3[3]; - // Figure out if the plane is parallel to two of the axes. If so it can be reproduced easily + public static Vector3d[] GenerateThreePoints(this Plane p, float scalar = 16) { + Vector3d[] points = new Vector3d[3]; + // Figure out if the plane is parallel to two of the axes. if (p.normal.y == 0 && p.normal.z == 0) { // parallel to plane YZ - points[0] = new Vector3(p.distance / p.normal.x, -planePointCoef, planePointCoef); - points[1] = new Vector3(p.distance / p.normal.x, 0, 0); - points[2] = new Vector3(p.distance / p.normal.x, planePointCoef, planePointCoef); + points[0] = new Vector3d(p.distance / p.normal.x, -scalar, scalar); + points[1] = new Vector3d(p.distance / p.normal.x, 0, 0); + points[2] = new Vector3d(p.distance / p.normal.x, scalar, scalar); if (p.normal.x > 0) { Array.Reverse(points); } } else if (p.normal.x == 0 && p.normal.z == 0) { // parallel to plane XZ - points[0] = new Vector3(planePointCoef, p.distance / p.normal.y, -planePointCoef); - points[1] = new Vector3(0, p.distance / p.normal.y, 0); - points[2] = new Vector3(planePointCoef, p.distance / p.normal.y, planePointCoef); + points[0] = new Vector3d(scalar, p.distance / p.normal.y, -scalar); + points[1] = new Vector3d(0, p.distance / p.normal.y, 0); + points[2] = new Vector3d(scalar, p.distance / p.normal.y, scalar); if (p.normal.y > 0) { Array.Reverse(points); } } else if (p.normal.x == 0 && p.normal.y == 0) { // parallel to plane XY - points[0] = new Vector3(-planePointCoef, planePointCoef, p.distance / p.normal.z); - points[1] = new Vector3(0, 0, p.distance / p.normal.z); - points[2] = new Vector3(planePointCoef, planePointCoef, p.distance / p.normal.z); + points[0] = new Vector3d(-scalar, scalar, p.distance / p.normal.z); + points[1] = new Vector3d(0, 0, p.distance / p.normal.z); + points[2] = new Vector3d(scalar, scalar, p.distance / p.normal.z); if (p.normal.z > 0) { Array.Reverse(points); } } else if (p.normal.x == 0) { // If you reach this point the plane is not parallel to any two-axis plane. // parallel to X axis - points[0] = new Vector3(-planePointCoef, planePointCoef * planePointCoef, (-(planePointCoef * planePointCoef * p.normal.y - p.distance)) / p.normal.z); - points[1] = new Vector3(0, 0, p.distance / p.normal.z); - points[2] = new Vector3(planePointCoef, planePointCoef * planePointCoef, (-(planePointCoef * planePointCoef * p.normal.y - p.distance)) / p.normal.z); + points[0] = new Vector3d(-scalar, scalar * scalar, (-(scalar * scalar * p.normal.y - p.distance)) / p.normal.z); + points[1] = new Vector3d(0, 0, p.distance / p.normal.z); + points[2] = new Vector3d(scalar, scalar * scalar, (-(scalar * scalar * p.normal.y - p.distance)) / p.normal.z); if (p.normal.z > 0) { Array.Reverse(points); } } else if (p.normal.y == 0) { // parallel to Y axis - points[0] = new Vector3((-(planePointCoef * planePointCoef * p.normal.z - p.distance)) / p.normal.x, -planePointCoef, planePointCoef * planePointCoef); - points[1] = new Vector3(p.distance / p.normal.x, 0, 0); - points[2] = new Vector3((-(planePointCoef * planePointCoef * p.normal.z - p.distance)) / p.normal.x, planePointCoef, planePointCoef * planePointCoef); + points[0] = new Vector3d((-(scalar * scalar * p.normal.z - p.distance)) / p.normal.x, -scalar, scalar * scalar); + points[1] = new Vector3d(p.distance / p.normal.x, 0, 0); + points[2] = new Vector3d((-(scalar * scalar * p.normal.z - p.distance)) / p.normal.x, scalar, scalar * scalar); if (p.normal.x > 0) { Array.Reverse(points); } } else if (p.normal.z == 0) { // parallel to Z axis - points[0] = new Vector3(planePointCoef * planePointCoef, (-(planePointCoef * planePointCoef * p.normal.x - p.distance)) / p.normal.y, -planePointCoef); - points[1] = new Vector3(0, p.distance / p.normal.y, 0); - points[2] = new Vector3(planePointCoef * planePointCoef, (-(planePointCoef * planePointCoef * p.normal.x - p.distance)) / p.normal.y, planePointCoef); + points[0] = new Vector3d(scalar * scalar, (-(scalar * scalar * p.normal.x - p.distance)) / p.normal.y, -scalar); + points[1] = new Vector3d(0, p.distance / p.normal.y, 0); + points[2] = new Vector3d(scalar * scalar, (-(scalar * scalar * p.normal.x - p.distance)) / p.normal.y, scalar); if (p.normal.y > 0) { Array.Reverse(points); } } else { // If you reach this point the plane is not parallel to any axis. Therefore, any two coordinates will give a third. - points[0] = new Vector3(-planePointCoef, planePointCoef * planePointCoef, -(-planePointCoef * p.normal.x + planePointCoef * planePointCoef * p.normal.y - p.distance) / p.normal.z); - points[1] = new Vector3(0, 0, p.distance / p.normal.z); - points[2] = new Vector3(planePointCoef, planePointCoef * planePointCoef, -(planePointCoef * p.normal.x + planePointCoef * planePointCoef * p.normal.y - p.distance) / p.normal.z); + points[0] = new Vector3d(-scalar, scalar * scalar, -(-scalar * p.normal.x + scalar * scalar * p.normal.y - p.distance) / p.normal.z); + points[1] = new Vector3d(0, 0, p.distance / p.normal.z); + points[2] = new Vector3d(scalar, scalar * scalar, -(scalar * p.normal.x + scalar * scalar * p.normal.y - p.distance) / p.normal.z); if (p.normal.z > 0) { Array.Reverse(points); } @@ -237,17 +220,10 @@ namespace LibBSP { /// use "Ax + By + Cz = D". The distance equation needs to be evaluated differently from /// Unity's default implementation to properly apply to planes read from BSPs. #if UNITY - public static float GetBSPDistanceToPoint(this Plane p, Vector3 to) { -#else - public static double GetBSPDistanceToPoint(this Plane p, Vector3 to) { -#endif -#if UNITY - float normLength = Mathf.Pow(p.normal.x, 2) + Mathf.Pow(p.normal.y, 2) + Mathf.Pow(p.normal.z, 2); - if (Mathf.Abs(normLength - 1.00f) > 0.01) { - normLength = Mathf.Sqrt(normLength); - } - return (p.normal.x * to.x + p.normal.y * to.y + p.normal.z * to.z - p.distance) / normLength; + public static float GetBSPDistanceToPoint(this Plane p, Vector3d to) { + return (p.normal.x * to.x + p.normal.y * to.y + p.normal.z * to.z - p.distance) / p.normal.magnitude; #else + public static double GetBSPDistanceToPoint(this Plane p, Vector3d to) { return p.GetDistanceToPoint(to); #endif } @@ -261,19 +237,19 @@ namespace LibBSP { /// Unity uses the plane equation "Ax + By + Cz + D = 0" while Quake-based engines /// use "Ax + By + Cz = D". The distance equation needs to be evaluated differently from /// Unity's default implementation to properly apply to planes read from BSPs. - public static bool GetBSPSide(this Plane p, Vector3 v) { + public static bool GetBSPSide(this Plane p, Vector3d v) { return p.GetBSPDistanceToPoint(v) > 0; } /// - /// Determines whether the given is contained in this . + /// Determines whether the given is contained in this . /// /// Point. - /// true if the is contained in this . + /// true if the is contained in this . /// Unity uses the plane equation "Ax + By + Cz + D = 0" while Quake-based engines /// use "Ax + By + Cz = D". The distance equation needs to be evaluated differently from /// Unity's default implementation to properly apply to planes read from BSPs. - public static bool BSPContains(this Plane p, Vector3 v) { + public static bool BSPContains(this Plane p, Vector3d v) { var distanceTo = p.GetBSPDistanceToPoint(v); return distanceTo < 0.001 && distanceTo > -0.001; } @@ -329,12 +305,13 @@ namespace LibBSP { break; } default: { - throw new ArgumentException("Map type " + type + " isn't supported by the Plane class."); + throw new ArgumentException("Map type " + type + " doesn't use a Plane lump or the lump is unknown."); } } - List lump = new List(data.Length / structLength); - for (int i = 0; i < data.Length / structLength; ++i) { - Vector3 normal = new Vector3(BitConverter.ToSingle(data, structLength * i), BitConverter.ToSingle(data, (structLength * i) + 4), BitConverter.ToSingle(data, (structLength * i) + 8)); + int numObjects = data.Length / structLength; + List lump = new List(numObjects); + for (int i = 0; i < numObjects; ++i) { + Vector3d normal = new Vector3d(BitConverter.ToSingle(data, structLength * i), BitConverter.ToSingle(data, (structLength * i) + 4), BitConverter.ToSingle(data, (structLength * i) + 8)); float distance = BitConverter.ToSingle(data, (structLength * i) + 12); lump.Add(new Plane(normal, distance)); } diff --git a/LibBSP/Source/Extensions/RectExtensions.cs b/LibBSP/Source/Extensions/RectExtensions.cs deleted file mode 100644 index 0b19f45..0000000 --- a/LibBSP/Source/Extensions/RectExtensions.cs +++ /dev/null @@ -1,57 +0,0 @@ -#if (UNITY_2_6 || UNITY_2_6_1 || UNITY_3_0 || UNITY_3_0_0 || UNITY_3_1 || UNITY_3_2 || UNITY_3_3 || UNITY_3_4 || UNITY_3_5 || UNITY_4_0 || UNITY_4_0_1 || UNITY_4_2 || UNITY_4_3 || UNITY_4_5 || UNITY_4_6 || UNITY_5 || UNITY_5_3_OR_NEWER) -#define UNITY -#endif - -#if UNITY -using UnityEngine; -#endif - -namespace LibBSP { -#if !UNITY - using Vector2 = Vector2d; -#endif - /// - /// Static class containing helper methods for objects. - /// - public static class RectExtensions { - - /// - /// Determines if this is intersected by the line segment defined by and . - /// - /// - /// Adapted from top answer at - /// http://stackoverflow.com/questions/99353/how-to-test-if-a-line-segment-intersects-an-axis-aligned-rectange-in-2d - /// - /// This . - /// First point defining the line segment. - /// The change and x and y for the coordinates of the tail. - /// true if the line segment intersects this at any point. - public static bool IntersectsSegment(Rect rect, Vector2 head, Vector2 tail) { - // Compute the signed distance from the line to each corner of the box - double[] dist = new double[4]; - double x1 = head.x; - double x2 = head.x + tail.x; - double y1 = head.y; - double y2 = head.y + tail.y; - dist[0] = tail.y * rect.min.x + tail.x * rect.min.y + (x2 * y1 - x1 * y2); - dist[1] = tail.y * rect.min.x + tail.x * rect.max.y + (x2 * y1 - x1 * y2); - dist[2] = tail.y * rect.max.x + tail.x * rect.min.y + (x2 * y1 - x1 * y2); - dist[3] = tail.y * rect.max.x + tail.x * rect.max.y + (x2 * y1 - x1 * y2); - if (dist[0] >= 0 && dist[1] >= 0 && dist[2] >= 0 && dist[3] >= 0) { - return false; - } else { - if (dist[0] <= 0 && dist[1] <= 0 && dist[2] <= 0 && dist[3] <= 0) { - return false; - } else { - // If we get to this point, the line intersects the box. Figure out if the line SEGMENT actually cuts it. - if ((x1 > rect.max.x && x2 > rect.max.x) || (x1 < rect.min.x && x2 < rect.min.x) || (y1 > rect.max.y && y2 > rect.max.y) || (y1 < rect.min.y && y2 < rect.min.y)) { - return false; - } else { - return true; - } - } - } - } - - } -} diff --git a/LibBSP/Source/Extensions/StringExtensions.cs b/LibBSP/Source/Extensions/StringExtensions.cs index 3961600..6553841 100644 --- a/LibBSP/Source/Extensions/StringExtensions.cs +++ b/LibBSP/Source/Extensions/StringExtensions.cs @@ -4,10 +4,12 @@ using System.Linq; using System.Text; namespace LibBSP { + /// /// Static class containing helper methods for string objects. /// public static class StringExtensions { + /// /// Splits a string using a Unicode character, unless that character is between two instances of a container. /// @@ -139,7 +141,7 @@ namespace LibBSP { } /// - /// Parses the bytes in a byte array into an ASCII string. + /// Parses the bytes in a byte array into an ASCII string. /// /// bytes to parse. /// The resulting string. @@ -150,6 +152,6 @@ namespace LibBSP { } return sb.ToString(); } - } + } } diff --git a/LibBSP/Source/Extensions/UIVertexExtensions.cs b/LibBSP/Source/Extensions/VertexExtensions.cs similarity index 59% rename from LibBSP/Source/Extensions/UIVertexExtensions.cs rename to LibBSP/Source/Extensions/VertexExtensions.cs index c449317..c0cdbd1 100644 --- a/LibBSP/Source/Extensions/UIVertexExtensions.cs +++ b/LibBSP/Source/Extensions/VertexExtensions.cs @@ -1,53 +1,55 @@ -#if (UNITY_2_6 || UNITY_2_6_1 || UNITY_3_0 || UNITY_3_0_0 || UNITY_3_1 || UNITY_3_2 || UNITY_3_3 || UNITY_3_4 || UNITY_3_5 || UNITY_4_0 || UNITY_4_0_1 || UNITY_4_2 || UNITY_4_3 || UNITY_4_5 || UNITY_4_6 || UNITY_5 || UNITY_5_3_OR_NEWER) +#if UNITY_2_6 || UNITY_2_6_1 || UNITY_3_0 || UNITY_3_0_0 || UNITY_3_1 || UNITY_3_2 || UNITY_3_3 || UNITY_3_4 || UNITY_3_5 || UNITY_4_0 || UNITY_4_0_1 || UNITY_4_2 || UNITY_4_3 || UNITY_4_5 || UNITY_4_6 || UNITY_5 || UNITY_5_3_OR_NEWER #define UNITY +#if !(UNITY_4_6 || UNITY_5 || UNITY_5_3_OR_NEWER) +#define OLDUNITY +#endif #endif using System; using System.Collections.Generic; #if UNITY using UnityEngine; -#else -using System.Drawing; #endif namespace LibBSP { -#if !UNITY - using Color32 = Color; - using Vector2 = Vector2d; - using Vector3 = Vector3d; +#if UNITY + using Vector2d = Vector2; + using Vector3d = Vector3; +#if !OLDUNITY + using Vertex = UIVertex; #endif +#endif + /// - /// Static class containing helper methods for objects. + /// Static class containing helper methods for objects. /// - public static class UIVertexExtensions { + public static class VertexExtensions { #if !UNITY /// - /// Scales the position of this by a number. + /// Scales the position of this by a scalar. /// - /// This . + /// This . /// Scalar value. - public static void Scale(this ref UIVertex v1, float scalar) { + public static void Scale(this ref Vertex v1, float scalar) { v1.position *= scalar; } /// - /// Adds the position of this to another . + /// Adds the position of this to another . /// - /// This . - /// The other . - public static void Add(this ref UIVertex v1, UIVertex v2) { + /// This . + /// The other . + public static void Add(this ref Vertex v1, Vertex v2) { v1.position += v2.position; - v1.uv0 += v2.uv0; - v1.uv1 += v2.uv1; } /// - /// Adds the position of a Vector3 to this . + /// Adds the position of a Vector3d to this . /// - /// This . - /// The . - public static void Translate(this ref UIVertex v1, Vector3 v2) { + /// This . + /// The . + public static void Translate(this ref Vertex v1, Vector3d v2) { v1.position += v2; } #endif @@ -56,62 +58,60 @@ namespace LibBSP { // Instead use these vanilla static methods in any code intended to be used with Unity. /// - /// Scales the position of a by a number and returns the result. + /// Scales the position of a by a scalar and returns the result. /// - /// The to scale. + /// The to scale. /// Scalar value. - /// The resulting of this scaling operation. - public static UIVertex Scale(UIVertex v1, float scalar) { + /// The resulting of this scaling operation. + public static Vertex Scale(Vertex v1, float scalar) { v1.position *= scalar; return v1; } /// - /// Adds the position of a to another and returns the result. + /// Adds the position of a to another and returns the result. /// - /// A to be added to another. - /// A to be added to another. - /// The resulting of this addition. - public static UIVertex Add(UIVertex v1, UIVertex v2) { + /// A to be added to another. + /// A to be added to another. + /// The resulting of this addition. + public static Vertex Add(Vertex v1, Vertex v2) { v1.position += v2.position; - v1.uv0 += v2.uv0; - v1.uv1 += v2.uv1; return v1; } /// - /// Adds the position of a Vector3 to a and returns the result. + /// Adds the position of a Vector3d to a and returns the result. /// - /// The to translate. - /// The to translate by. - /// The resulting translated by . - public static UIVertex Translate(UIVertex v1, Vector3 v2) { + /// The to translate. + /// The to translate by. + /// The resulting translated by . + public static Vertex Translate(Vertex v1, Vector3d v2) { v1.position += v2; return v1; } /// - /// Creates a new object from a byte array. + /// Creates a new object from a byte array. /// /// byte array to parse. /// The map type. /// The version of this lump. - /// The resulting object. + /// The resulting object. /// was null. /// This structure is not implemented for the given maptype. - /// has no constructor, so the object must be initialized field-by-field. Even if it + /// has no constructor, so the object must be initialized field-by-field. Even if it /// did have a constructor, the way data needs to be read wouldn't allow use of it. - public static UIVertex CreateVertex(byte[] data, MapType type, int version = 0) { + public static Vertex CreateVertex(byte[] data, MapType type, int version = 0) { if (data == null) { throw new ArgumentNullException(); } - UIVertex result = new UIVertex(); + Vertex result = new Vertex(); switch (type) { case MapType.CoD: { if (version == 0) { goto case MapType.Quake3; } else if (version == 1) { - result.color = Color32Extensions.FromArgb(255, 255, 255, 255); + result.color = ColorExtensions.FromArgb(255, 255, 255, 255); goto case MapType.DMoMaM; } break; @@ -121,19 +121,19 @@ namespace LibBSP { case MapType.CoD2: case MapType.CoD4: case MapType.FAKK: { - result.uv0 = new Vector2(BitConverter.ToSingle(data, 12), BitConverter.ToSingle(data, 16)); - result.color = Color32Extensions.FromArgb(data[43], data[40], data[41], data[42]); + result.uv0 = new Vector2d(BitConverter.ToSingle(data, 12), BitConverter.ToSingle(data, 16)); + result.color = ColorExtensions.FromArgb(data[43], data[40], data[41], data[42]); goto case MapType.DMoMaM; } case MapType.Raven: { - result.uv0 = new Vector2(BitConverter.ToSingle(data, 12), BitConverter.ToSingle(data, 16)); - result.color = Color32Extensions.FromArgb(data[67], data[64], data[65], data[66]); + result.uv0 = new Vector2d(BitConverter.ToSingle(data, 12), BitConverter.ToSingle(data, 16)); + result.color = ColorExtensions.FromArgb(data[67], data[64], data[65], data[66]); goto case MapType.DMoMaM; } case MapType.STEF2: case MapType.STEF2Demo: { - result.uv0 = new Vector2(BitConverter.ToSingle(data, 12), BitConverter.ToSingle(data, 16)); - result.color = Color32Extensions.FromArgb(data[35], data[32], data[33], data[34]); + result.uv0 = new Vector2d(BitConverter.ToSingle(data, 12), BitConverter.ToSingle(data, 16)); + result.color = ColorExtensions.FromArgb(data[35], data[32], data[33], data[34]); goto case MapType.DMoMaM; } case MapType.Quake: @@ -154,28 +154,28 @@ 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 = new Vector3d(BitConverter.ToSingle(data, 0), BitConverter.ToSingle(data, 4), BitConverter.ToSingle(data, 8)); break; } default: { - throw new ArgumentException("Map type " + type + " isn't supported by the UIVertex class factory."); + throw new ArgumentException("Map type " + type + " doesn't use Vertex objects, or reading them isn't implemented."); } } return result; } /// - /// Factory method to parse a byte array into a List of objects. + /// Factory method to parse a byte array into a List of objects. /// /// The data to parse. /// The map type. /// The version of this lump. - /// A List of objects. + /// A List of objects. /// was null. /// This structure is not implemented for the given maptype. /// This function goes here since I can't put it into Unity's UIVertex class, and so I can't /// depend on having a constructor taking a byte array. - public static List LumpFactory(byte[] data, MapType type, int version = 0) { + public static List LumpFactory(byte[] data, MapType type, int version = 0) { if (data == null) { throw new ArgumentNullException(); } @@ -226,12 +226,13 @@ namespace LibBSP { break; } default: { - throw new ArgumentException("Map type " + type + " isn't supported by the Vertex lump factory."); + throw new ArgumentException("Map type " + type + " doesn't use a Vertex lump or the lump is unknown."); } } - List lump = new List(data.Length / structLength); + int numObjects = data.Length / structLength; + List lump = new List(numObjects); byte[] bytes = new byte[structLength]; - for (int i = 0; i < data.Length / structLength; ++i) { + for (int i = 0; i < numObjects; ++i) { Array.Copy(data, i * structLength, bytes, 0, structLength); lump.Add(CreateVertex(bytes, type, version)); } diff --git a/LibBSP/Source/Structs/BSP/BSP.cs b/LibBSP/Source/Structs/BSP/BSP.cs index d5645a4..6181198 100644 --- a/LibBSP/Source/Structs/BSP/BSP.cs +++ b/LibBSP/Source/Structs/BSP/BSP.cs @@ -1,5 +1,8 @@ -#if (UNITY_2_6 || UNITY_2_6_1 || UNITY_3_0 || UNITY_3_0_0 || UNITY_3_1 || UNITY_3_2 || UNITY_3_3 || UNITY_3_4 || UNITY_3_5 || UNITY_4_0 || UNITY_4_0_1 || UNITY_4_2 || UNITY_4_3 || UNITY_4_5 || UNITY_4_6 || UNITY_5 || UNITY_5_3_OR_NEWER) +#if UNITY_2_6 || UNITY_2_6_1 || UNITY_3_0 || UNITY_3_0_0 || UNITY_3_1 || UNITY_3_2 || UNITY_3_3 || UNITY_3_4 || UNITY_3_5 || UNITY_4_0 || UNITY_4_0_1 || UNITY_4_2 || UNITY_4_3 || UNITY_4_5 || UNITY_4_6 || UNITY_5 || UNITY_5_3_OR_NEWER #define UNITY +#if !(UNITY_4_6 || UNITY_5 || UNITY_5_3_OR_NEWER) +#define OLDUNITY +#endif #endif using System; @@ -11,9 +14,10 @@ using UnityEngine; #endif namespace LibBSP { -#if !UNITY - using Vector3 = Vector3d; +#if UNITY && !OLDUNITY + using Vertex = UIVertex; #endif + /// /// Enum of the known different map formats. /// @@ -52,7 +56,7 @@ namespace LibBSP { } /// - /// Struct containing basic information for a lump in a BSP file. + /// Class containing basic information for a lump in a BSP file. /// public class LumpInfo { public int ident; @@ -64,7 +68,7 @@ namespace LibBSP { } /// - /// Holds data for any and all BSP formats. Any unused lumps in a given format + /// Holds data for any and all supported BSP formats. Any unused lumps in a given format /// will be left as null. /// public class BSP : Dictionary { @@ -76,7 +80,7 @@ namespace LibBSP { private Entities _entities; private List _planes; private Textures _textures; - private List _vertices; + private List _vertices; private List _nodes; private List _texInfo; private List _faces; @@ -95,7 +99,7 @@ namespace LibBSP { private List _staticModels; // CoD private List _patches; - private List _patchVerts; + private List _patchVerts; private NumList _patchIndices; // Nightfire private Textures _materials; @@ -183,14 +187,14 @@ namespace LibBSP { } /// - /// A List of objects in the BSP file representing the vertices of the BSP, if available. + /// A List of objects in the BSP file representing the vertices of the BSP, if available. /// - public List vertices { + public List vertices { get { if (_vertices == null) { - int index = UIVertexExtensions.GetIndexForLump(version); + int index = VertexExtensions.GetIndexForLump(version); if (index >= 0) { - _vertices = UIVertexExtensions.LumpFactory(reader.ReadLump(this[index]), version, this[index].version); + _vertices = VertexExtensions.LumpFactory(reader.ReadLump(this[index]), version, this[index].version); } } return _vertices; @@ -501,14 +505,14 @@ namespace LibBSP { } /// - /// A List of objects in the BSP file representing the patch vertices of the BSP, if available. + /// A List of objects in the BSP file representing the patch vertices of the BSP, if available. /// - public List patchVerts { + public List patchVerts { get { if (_patchVerts == null) { - int index = UIVertexExtensions.GetIndexForPatchVertsLump(version); + int index = VertexExtensions.GetIndexForPatchVertsLump(version); if (index >= 0) { - _patchVerts = UIVertexExtensions.LumpFactory(reader.ReadLump(this[index]), version, 1); + _patchVerts = VertexExtensions.LumpFactory(reader.ReadLump(this[index]), version, 1); } } return _patchVerts; @@ -699,7 +703,7 @@ namespace LibBSP { /// A reference to the .BSP file. public BSP(FileInfo file) : base(16) { reader = new BSPReader(file); - this.filePath = file.FullName; + filePath = file.FullName; } /// @@ -830,5 +834,6 @@ namespace LibBSP { return theLump.GetRange(index, count); } + } } diff --git a/LibBSP/Source/Structs/BSP/Brush.cs b/LibBSP/Source/Structs/BSP/Brush.cs index a9870bb..eaa118e 100644 --- a/LibBSP/Source/Structs/BSP/Brush.cs +++ b/LibBSP/Source/Structs/BSP/Brush.cs @@ -2,6 +2,7 @@ using System; using System.Collections.Generic; namespace LibBSP { + /// /// Holds the data used by the brush structures of all formats of BSP. /// @@ -136,9 +137,10 @@ namespace LibBSP { throw new ArgumentException("Map type " + type + " isn't supported by the Brush lump factory."); } } - List lump = new List(data.Length / structLength); + int numObjects = data.Length / structLength; + List lump = new List(numObjects); byte[] bytes = new byte[structLength]; - for (int i = 0; i < data.Length / structLength; ++i) { + for (int i = 0; i < numObjects; ++i) { Array.Copy(data, (i * structLength), bytes, 0, structLength); lump.Add(new Brush(bytes, type, version)); } diff --git a/LibBSP/Source/Structs/BSP/BrushSide.cs b/LibBSP/Source/Structs/BSP/BrushSide.cs index ffc3777..1298cae 100644 --- a/LibBSP/Source/Structs/BSP/BrushSide.cs +++ b/LibBSP/Source/Structs/BSP/BrushSide.cs @@ -2,6 +2,7 @@ using System; using System.Collections.Generic; namespace LibBSP { + /// /// Holds the data used by the brush side structures of all formats of BSP. /// @@ -161,9 +162,10 @@ namespace LibBSP { throw new ArgumentException("Map type " + type + " isn't supported by the BrushSide lump factory."); } } - List lump = new List(data.Length / structLength); + int numObjects = data.Length / structLength; + List lump = new List(numObjects); byte[] bytes = new byte[structLength]; - for (int i = 0; i < data.Length / structLength; i++) { + for (int i = 0; i < numObjects; i++) { Array.Copy(data, (i * structLength), bytes, 0, structLength); lump.Add(new BrushSide(bytes, type, version)); } diff --git a/LibBSP/Source/Structs/BSP/Cubemap.cs b/LibBSP/Source/Structs/BSP/Cubemap.cs index c234f48..1bef803 100644 --- a/LibBSP/Source/Structs/BSP/Cubemap.cs +++ b/LibBSP/Source/Structs/BSP/Cubemap.cs @@ -9,15 +9,16 @@ using UnityEngine; #endif namespace LibBSP { -#if !UNITY - using Vector3 = Vector3d; +#if UNITY + using Vector3d = Vector3; #endif + /// /// Holds all data for a Cubemap from Source engine. /// public struct Cubemap { - public Vector3 origin { get; private set; } + public Vector3d origin { get; private set; } public int size { get; private set; } /// @@ -45,7 +46,7 @@ namespace LibBSP { case MapType.L4D2: case MapType.Vindictus: case MapType.DMoMaM: { - origin = new Vector3(BitConverter.ToInt32(data, 0), BitConverter.ToInt32(data, 4), BitConverter.ToInt32(data, 8)); + origin = new Vector3d(BitConverter.ToInt32(data, 0), BitConverter.ToInt32(data, 4), BitConverter.ToInt32(data, 8)); size = BitConverter.ToInt32(data, 12); break; } @@ -89,13 +90,12 @@ namespace LibBSP { throw new ArgumentException("Map type " + type + " isn't supported by the SourceCubemap lump factory."); } } - int offset = 0; - List lump = new List(data.Length / structLength); + int numObjects = data.Length / structLength; + List lump = new List(numObjects); byte[] bytes = new byte[structLength]; - for (int i = 0; i < data.Length / structLength; ++i) { + for (int i = 0; i < numObjects; ++i) { Array.Copy(data, (i * structLength), bytes, 0, structLength); lump.Add(new Cubemap(bytes, type, version)); - offset += structLength; } return lump; } diff --git a/LibBSP/Source/Structs/BSP/DisplacementInfo.cs b/LibBSP/Source/Structs/BSP/DisplacementInfo.cs index 2de5719..7adc55c 100644 --- a/LibBSP/Source/Structs/BSP/DisplacementInfo.cs +++ b/LibBSP/Source/Structs/BSP/DisplacementInfo.cs @@ -9,15 +9,16 @@ using UnityEngine; #endif namespace LibBSP { -#if !UNITY - using Vector3 = Vector3d; +#if UNITY + using Vector3d = Vector3; #endif + /// /// Holds all data for a Displacement from Source engine. /// public struct DisplacementInfo { - public Vector3 startPosition { get; private set; } + public Vector3d startPosition { get; private set; } public int dispVertStart { get; private set; } public int dispTriStart { get; private set; } public int power { get; private set; } @@ -41,7 +42,7 @@ namespace LibBSP { if (data == null) { throw new ArgumentNullException(); } - startPosition = new Vector3(BitConverter.ToSingle(data, 0), BitConverter.ToSingle(data, 4), BitConverter.ToSingle(data, 8)); + startPosition = new Vector3d(BitConverter.ToSingle(data, 0), BitConverter.ToSingle(data, 4), BitConverter.ToSingle(data, 8)); dispVertStart = BitConverter.ToInt32(data, 12); dispTriStart = BitConverter.ToInt32(data, 16); power = BitConverter.ToInt32(data, 20); @@ -130,13 +131,12 @@ namespace LibBSP { throw new ArgumentException("Map type " + type + " isn't supported by the SourceDispInfo lump factory."); } } - int offset = 0; - List lump = new List(data.Length / structLength); + int numObjects = data.Length / structLength; + List lump = new List(numObjects); byte[] bytes = new byte[structLength]; - for (int i = 0; i < data.Length / structLength; ++i) { + for (int i = 0; i < numObjects; ++i) { Array.Copy(data, (i * structLength), bytes, 0, structLength); lump.Add(new DisplacementInfo(bytes, type, version)); - offset += structLength; } return lump; } diff --git a/LibBSP/Source/Structs/BSP/DisplacementVertex.cs b/LibBSP/Source/Structs/BSP/DisplacementVertex.cs index 110c02d..3e37f3d 100644 --- a/LibBSP/Source/Structs/BSP/DisplacementVertex.cs +++ b/LibBSP/Source/Structs/BSP/DisplacementVertex.cs @@ -3,21 +3,21 @@ #endif using System; -using System.Collections.Generic; #if UNITY using UnityEngine; #endif namespace LibBSP { -#if !UNITY - using Vector3 = Vector3d; +#if UNITY + using Vector3d = Vector3; #endif + /// /// Holds all the data for a displacement in a Source map. /// public struct DisplacementVertex { - public Vector3 normal { get; private set; } // The normalized vector direction this vertex points from "flat" + public Vector3d normal { get; private set; } // The normalized vector direction this vertex points from "flat" public float dist { get; private set; } // Magnitude of normal, before normalization public float alpha { get; private set; } // Alpha value of texture at this vertex @@ -32,9 +32,9 @@ namespace LibBSP { if (data == null) { throw new ArgumentNullException(); } - this.normal = new Vector3(BitConverter.ToSingle(data, 0), BitConverter.ToSingle(data, 4), BitConverter.ToSingle(data, 8)); - this.dist = BitConverter.ToSingle(data, 12); - this.alpha = BitConverter.ToSingle(data, 16); + normal = new Vector3d(BitConverter.ToSingle(data, 0), BitConverter.ToSingle(data, 4), BitConverter.ToSingle(data, 8)); + dist = BitConverter.ToSingle(data, 12); + alpha = BitConverter.ToSingle(data, 16); } /// diff --git a/LibBSP/Source/Structs/BSP/Edge.cs b/LibBSP/Source/Structs/BSP/Edge.cs index ebdc9bd..aa838b4 100644 --- a/LibBSP/Source/Structs/BSP/Edge.cs +++ b/LibBSP/Source/Structs/BSP/Edge.cs @@ -2,6 +2,7 @@ using System; using System.Collections.Generic; namespace LibBSP { + /// /// Holds all the data for an edge in a BSP map. /// @@ -96,9 +97,10 @@ namespace LibBSP { throw new ArgumentException("Map type " + type + " isn't supported by the Edge lump factory."); } } - List lump = new List(data.Length / structLength); + int numObjects = data.Length / structLength; + List lump = new List(numObjects); byte[] bytes = new byte[structLength]; - for (int i = 0; i < data.Length / structLength; ++i) { + for (int i = 0; i < numObjects; ++i) { Array.Copy(data, (i * structLength), bytes, 0, structLength); lump.Add(new Edge(bytes, type, version)); } @@ -138,5 +140,6 @@ namespace LibBSP { } } } + } } diff --git a/LibBSP/Source/Structs/BSP/Face.cs b/LibBSP/Source/Structs/BSP/Face.cs index a214c65..dcfe569 100644 --- a/LibBSP/Source/Structs/BSP/Face.cs +++ b/LibBSP/Source/Structs/BSP/Face.cs @@ -9,8 +9,8 @@ using UnityEngine; #endif namespace LibBSP { -#if !UNITY - using Vector2 = Vector2d; +#if UNITY + using Vector2d = Vector2; #endif /// @@ -41,7 +41,7 @@ namespace LibBSP { public int unknown { get; private set; } public int lightStyles { get; private set; } public int lightMaps { get; private set; } - public Vector2 patchSize { get; private set; } + public Vector2d patchSize { get; private set; } /// /// Creates a new object from a byte array. @@ -72,7 +72,7 @@ namespace LibBSP { unknown = -1; lightStyles = -1; lightMaps = -1; - patchSize = new Vector2(Single.NaN, Single.NaN); + patchSize = new Vector2d(Single.NaN, Single.NaN); switch (type) { case MapType.CoD: { texture = BitConverter.ToInt16(data, 0); @@ -106,7 +106,7 @@ namespace LibBSP { numVertices = BitConverter.ToInt32(data, 16); firstIndex = BitConverter.ToInt32(data, 20); numIndices = BitConverter.ToInt32(data, 24); - patchSize = new Vector2(BitConverter.ToInt32(data, 96), BitConverter.ToInt32(data, 100)); + patchSize = new Vector2d(BitConverter.ToInt32(data, 96), BitConverter.ToInt32(data, 100)); break; } case MapType.Source17: { @@ -257,9 +257,10 @@ namespace LibBSP { throw new ArgumentException("Map type " + type + " isn't supported by the Face lump factory."); } } - List lump = new List(data.Length / structLength); + int numObjects = data.Length / structLength; + List lump = new List(numObjects); byte[] bytes = new byte[structLength]; - for (int i = 0; i < data.Length / structLength; ++i) { + for (int i = 0; i < numObjects; ++i) { Array.Copy(data, (i * structLength), bytes, 0, structLength); lump.Add(new Face(bytes, type, version)); } @@ -342,5 +343,6 @@ namespace LibBSP { } } } + } } diff --git a/LibBSP/Source/Structs/BSP/LODTerrain.cs b/LibBSP/Source/Structs/BSP/LODTerrain.cs index 8c5438f..1c4f9d8 100644 --- a/LibBSP/Source/Structs/BSP/LODTerrain.cs +++ b/LibBSP/Source/Structs/BSP/LODTerrain.cs @@ -2,6 +2,7 @@ using System; using System.Collections.Generic; namespace LibBSP { + /// /// Holds the data for a terrain object in MoHAA. /// @@ -92,9 +93,10 @@ namespace LibBSP { throw new ArgumentException("Map type " + type + " isn't supported by the LODTerrain lump factory."); } } - List lump = new List(data.Length / structLength); + int numObjects = data.Length / structLength; + List lump = new List(numObjects); byte[] bytes = new byte[structLength]; - for (int i = 0; i < data.Length / structLength; ++i) { + for (int i = 0; i < numObjects; ++i) { Array.Copy(data, (i * structLength), bytes, 0, structLength); lump.Add(new LODTerrain(bytes, type, version)); } diff --git a/LibBSP/Source/Structs/BSP/Leaf.cs b/LibBSP/Source/Structs/BSP/Leaf.cs index f66c141..e82dfb7 100644 --- a/LibBSP/Source/Structs/BSP/Leaf.cs +++ b/LibBSP/Source/Structs/BSP/Leaf.cs @@ -2,6 +2,7 @@ using System; using System.Collections.Generic; namespace LibBSP { + /// /// Holds data for a leaf structure in a BSP map. /// @@ -168,9 +169,10 @@ namespace LibBSP { throw new ArgumentException("Map type " + type + " isn't supported by the Leaf lump factory."); } } - List lump = new List(data.Length / structLength); + int numObjects = data.Length / structLength; + List lump = new List(numObjects); byte[] bytes = new byte[structLength]; - for (int i = 0; i < data.Length / structLength; ++i) { + for (int i = 0; i < numObjects; ++i) { Array.Copy(data, (i * structLength), bytes, 0, structLength); lump.Add(new Leaf(bytes, type, version)); } @@ -224,5 +226,6 @@ namespace LibBSP { } } } + } } diff --git a/LibBSP/Source/Structs/BSP/Lumps/DisplacementVertices.cs b/LibBSP/Source/Structs/BSP/Lumps/DisplacementVertices.cs index 5926091..acd783a 100644 --- a/LibBSP/Source/Structs/BSP/Lumps/DisplacementVertices.cs +++ b/LibBSP/Source/Structs/BSP/Lumps/DisplacementVertices.cs @@ -2,6 +2,7 @@ using System; using System.Collections.Generic; namespace LibBSP { + /// /// Class representing a group of objects. Contains helpful methods to handle Displacement Vertices in the List. /// @@ -20,7 +21,8 @@ namespace LibBSP { } int structLength = 20; byte[] bytes = new byte[structLength]; - for (int i = 0; i < data.Length / structLength; ++i) { + int numObjects = data.Length / structLength; + for (int i = 0; i < numObjects; ++i) { Array.Copy(data, (i * structLength), bytes, 0, structLength); Add(new DisplacementVertex(bytes, type, version)); } diff --git a/LibBSP/Source/Structs/BSP/Lumps/GameLump.cs b/LibBSP/Source/Structs/BSP/Lumps/GameLump.cs index d084061..153e263 100644 --- a/LibBSP/Source/Structs/BSP/Lumps/GameLump.cs +++ b/LibBSP/Source/Structs/BSP/Lumps/GameLump.cs @@ -2,6 +2,7 @@ using System; using System.Collections.Generic; namespace LibBSP { + /// /// Enum containing known game lumps. /// @@ -10,12 +11,11 @@ namespace LibBSP { tlpd = 1685089396, prpd = 1685090928, sprp = 1936749168, - } /// /// Class containing the identification and information for the various Game Lumps in Source - /// engine BSPs. The only one we're really concerned with is the Static Props. + /// engine BSPs. /// public class GameLump : Dictionary { @@ -76,14 +76,14 @@ namespace LibBSP { break; } default: { - throw new ArgumentException("Map type " + type + " isn't supported by the GameLump class."); + throw new ArgumentException("Map type " + type + " doesn't use a Game lump or the lump is unknown."); } } int numGameLumps = BitConverter.ToInt32(data, 0); if (numGameLumps > 0) { int headerLength = 4 + (numGameLumps * structLength); - int lowestLumpOffset = Int32.MaxValue; + int lowestLumpOffset = int.MaxValue; for (int i = 0; i < numGameLumps; ++i) { int lumpIdent = BitConverter.ToInt32(data, (i * structLength) + 4); diff --git a/LibBSP/Source/Structs/BSP/Lumps/StaticProps.cs b/LibBSP/Source/Structs/BSP/Lumps/StaticProps.cs index 4b2e472..be8912d 100644 --- a/LibBSP/Source/Structs/BSP/Lumps/StaticProps.cs +++ b/LibBSP/Source/Structs/BSP/Lumps/StaticProps.cs @@ -2,6 +2,7 @@ using System; using System.Collections.Generic; namespace LibBSP { + /// /// List of objects containing data relevant to Static Props, like the dictionary of actual model paths. /// diff --git a/LibBSP/Source/Structs/BSP/Lumps/Textures.cs b/LibBSP/Source/Structs/BSP/Lumps/Textures.cs index 0e52d31..a7e1e95 100644 --- a/LibBSP/Source/Structs/BSP/Lumps/Textures.cs +++ b/LibBSP/Source/Structs/BSP/Lumps/Textures.cs @@ -2,6 +2,7 @@ using System; using System.Collections.Generic; namespace LibBSP { + /// /// List<> with some useful methods for manipulating objects, /// especially when handling them as a group. @@ -64,13 +65,13 @@ namespace LibBSP { case MapType.Vindictus: case MapType.DMoMaM: { int offset = 0; - byte[] bytes; + byte[] myBytes; for (int i = 0; i < data.Length; ++i) { if (data[i] == (byte)0x00) { // They are null-terminated strings, of non-constant length (not padded) - bytes = new byte[i - offset]; - Array.Copy(data, offset, bytes, 0, i - offset); - Add(new Texture(bytes, type, version)); + myBytes = new byte[i - offset]; + Array.Copy(data, offset, myBytes, 0, i - offset); + Add(new Texture(myBytes, type, version)); offset = i + 1; } } @@ -79,24 +80,23 @@ namespace LibBSP { case MapType.Quake: { int numElements = BitConverter.ToInt32(data, 0); structLength = 40; - byte[] bytes = new byte[structLength]; + byte[] myBytes = new byte[structLength]; for (int i = 0; i < numElements; ++i) { - Array.Copy(data, BitConverter.ToInt32(data, (i + 1) * 4), bytes, 0, structLength); - Add(new Texture(bytes, type, version)); + Array.Copy(data, BitConverter.ToInt32(data, (i + 1) * 4), myBytes, 0, structLength); + Add(new Texture(myBytes, type, version)); } return; } default: { - throw new ArgumentException("Map type " + type + " isn't supported by the Node class."); + throw new ArgumentException("Map type " + type + " doesn't use a Texture lump or the lump is unknown."); } } - { - byte[] bytes = new byte[structLength]; - for (int i = 0; i < data.Length / structLength; ++i) { - Array.Copy(data, (i * structLength), bytes, 0, structLength); - Add(new Texture(bytes, type, version)); - } + int numObjects = data.Length / structLength; + byte[] bytes = new byte[structLength]; + for (int i = 0; i < numObjects; ++i) { + Array.Copy(data, (i * structLength), bytes, 0, structLength); + Add(new Texture(bytes, type, version)); } } @@ -136,5 +136,6 @@ namespace LibBSP { // If we get here, the requested texture didn't exist. return -1; } + } } diff --git a/LibBSP/Source/Structs/BSP/Model.cs b/LibBSP/Source/Structs/BSP/Model.cs index 40ff8ba..5eb0655 100644 --- a/LibBSP/Source/Structs/BSP/Model.cs +++ b/LibBSP/Source/Structs/BSP/Model.cs @@ -2,6 +2,7 @@ using System; using System.Collections.Generic; namespace LibBSP { + /// /// A class containing all data needed for the models lump in any given BSP. /// @@ -178,13 +179,12 @@ namespace LibBSP { throw new ArgumentException("Map type " + type + " isn't supported by the Leaf lump factory."); } } - int offset = 0; - List lump = new List(data.Length / structLength); + int numObjects = data.Length / structLength; + List lump = new List(numObjects); byte[] bytes = new byte[structLength]; - for (int i = 0; i < data.Length / structLength; ++i) { + for (int i = 0; i < numObjects; ++i) { Array.Copy(data, (i * structLength), bytes, 0, structLength); lump.Add(new Model(bytes, type, version)); - offset += structLength; } return lump; } @@ -242,5 +242,6 @@ namespace LibBSP { } } } + } } diff --git a/LibBSP/Source/Structs/BSP/Node.cs b/LibBSP/Source/Structs/BSP/Node.cs index 2f6561e..a8419d7 100644 --- a/LibBSP/Source/Structs/BSP/Node.cs +++ b/LibBSP/Source/Structs/BSP/Node.cs @@ -2,6 +2,7 @@ using System; using System.Collections.Generic; namespace LibBSP { + /// /// Contains all data needed for a node in a BSP tree. /// @@ -10,7 +11,6 @@ namespace LibBSP { public int plane { get; private set; } public int child1 { get; private set; } // Negative values are valid here. However, the child can never be zero, public int child2 { get; private set; } // since that would reference the head node causing an infinite loop. - // TODO: Other things in Node structure /// /// Creates a new object from a byte array. @@ -125,13 +125,12 @@ namespace LibBSP { throw new ArgumentException("Map type " + type + " isn't supported by the Node lump factory."); } } - int offset = 0; - List lump = new List(data.Length / structLength); + int numObjects = data.Length / structLength; + List lump = new List(numObjects); byte[] bytes = new byte[structLength]; - for (int i = 0; i < data.Length / structLength; ++i) { + for (int i = 0; i < numObjects; ++i) { Array.Copy(data, (i * structLength), bytes, 0, structLength); lump.Add(new Node(bytes, type, version)); - offset += structLength; } return lump; } @@ -187,5 +186,6 @@ namespace LibBSP { } } } + } } diff --git a/LibBSP/Source/Structs/BSP/Patch.cs b/LibBSP/Source/Structs/BSP/Patch.cs index 980e961..2229b6c 100644 --- a/LibBSP/Source/Structs/BSP/Patch.cs +++ b/LibBSP/Source/Structs/BSP/Patch.cs @@ -9,8 +9,8 @@ using UnityEngine; #endif namespace LibBSP { -#if !UNITY - using Vector2 = Vector2d; +#if UNITY + using Vector2d = Vector2; #endif /// @@ -20,7 +20,7 @@ namespace LibBSP { public short shader { get; private set; } public short type { get; private set; } - public Vector2 dimensions { get; private set; } + public Vector2d dimensions { get; private set; } public int flags { get; private set; } [Index("patchVerts")] public int firstVertex { get; private set; } [Count("patchVerts")] public int numVertices { get; private set; } @@ -46,7 +46,7 @@ namespace LibBSP { if (this.type == 0) { // Patch short x = BitConverter.ToInt16(data, 4); short y = BitConverter.ToInt16(data, 6); - dimensions = new Vector2(x, y); + dimensions = new Vector2d(x, y); flags = BitConverter.ToInt32(data, 8); firstVertex = BitConverter.ToInt32(data, 12); numVertices = x * y; @@ -87,9 +87,10 @@ namespace LibBSP { throw new ArgumentException("Map type " + type + " isn't supported by the Patch lump factory."); } } - List lump = new List(data.Length / structLength); + int numObjects = data.Length / structLength; + List lump = new List(numObjects); byte[] bytes = new byte[structLength]; - for (int i = 0; i < data.Length / structLength; ++i) { + for (int i = 0; i < numObjects; ++i) { Array.Copy(data, (i * structLength), bytes, 0, structLength); lump.Add(new Patch(bytes, type, version)); } diff --git a/LibBSP/Source/Structs/BSP/StaticModel.cs b/LibBSP/Source/Structs/BSP/StaticModel.cs index 1e9fef4..bf422aa 100644 --- a/LibBSP/Source/Structs/BSP/StaticModel.cs +++ b/LibBSP/Source/Structs/BSP/StaticModel.cs @@ -9,17 +9,18 @@ using UnityEngine; #endif namespace LibBSP { -#if !UNITY - using Vector3 = Vector3d; +#if UNITY + using Vector3d = Vector3; #endif + /// /// Handles the data needed for a static model object from MoHAA. /// public struct StaticModel { public string name { get; private set; } - public Vector3 origin { get; private set; } - public Vector3 angles { get; private set; } + public Vector3d origin { get; private set; } + public Vector3d angles { get; private set; } public float scale { get; private set; } [Index("vertices")] public int firstVertex { get; private set; } [Count("vertices")] public short numVertices { get; private set; } @@ -37,16 +38,16 @@ namespace LibBSP { throw new ArgumentNullException(); } name = ""; - origin = Vector3.zero; - angles = Vector3.zero; + origin = Vector3d.zero; + angles = Vector3d.zero; scale = 0; switch (type) { case MapType.MOHAA: { byte[] nameBytes = new byte[128]; Array.Copy(data, 0, nameBytes, 0, 128); name = nameBytes.ToNullTerminatedString(); - origin = new Vector3(BitConverter.ToSingle(data, 128), BitConverter.ToSingle(data, 132), BitConverter.ToSingle(data, 136)); - angles = new Vector3(BitConverter.ToSingle(data, 140), BitConverter.ToSingle(data, 144), BitConverter.ToSingle(data, 148)); + origin = new Vector3d(BitConverter.ToSingle(data, 128), BitConverter.ToSingle(data, 132), BitConverter.ToSingle(data, 136)); + angles = new Vector3d(BitConverter.ToSingle(data, 140), BitConverter.ToSingle(data, 144), BitConverter.ToSingle(data, 148)); scale = BitConverter.ToSingle(data, 152); firstVertex = BitConverter.ToInt32(data, 156); numVertices = BitConverter.ToInt16(data, 160); @@ -81,9 +82,10 @@ namespace LibBSP { throw new ArgumentException("Map type " + type + " isn't supported by the StaticModel lump factory."); } } - List lump = new List(data.Length / structLength); + int numObjects = data.Length / structLength; + List lump = new List(numObjects); byte[] bytes = new byte[structLength]; - for (int i = 0; i < data.Length / structLength; ++i) { + for (int i = 0; i < numObjects; ++i) { Array.Copy(data, (i * structLength), bytes, 0, structLength); lump.Add(new StaticModel(bytes, type, version)); } @@ -105,5 +107,6 @@ namespace LibBSP { } } } + } } diff --git a/LibBSP/Source/Structs/BSP/StaticProp.cs b/LibBSP/Source/Structs/BSP/StaticProp.cs index 0a0e67c..ad464b1 100644 --- a/LibBSP/Source/Structs/BSP/StaticProp.cs +++ b/LibBSP/Source/Structs/BSP/StaticProp.cs @@ -3,22 +3,22 @@ #endif using System; -using System.Collections.Generic; #if UNITY using UnityEngine; #endif namespace LibBSP { -#if !UNITY - using Vector3 = Vector3d; +#if UNITY + using Vector3d = Vector3; #endif + /// /// Handles the data needed for a static prop object. /// public struct StaticProp { - public Vector3 origin { get; private set; } - public Vector3 angles { get; private set; } + public Vector3d origin { get; private set; } + public Vector3d angles { get; private set; } public short dictionaryEntry { get; private set; } public byte solidity { get; private set; } public byte flags { get; private set; } @@ -40,8 +40,8 @@ namespace LibBSP { if (data == null) { throw new ArgumentNullException(); } - origin = Vector3.zero; - angles = Vector3.zero; + origin = Vector3d.zero; + angles = Vector3d.zero; dictionaryEntry = 0; solidity = 0; flags = 0; @@ -85,8 +85,8 @@ namespace LibBSP { goto case 4; } case 4: { - origin = new Vector3(BitConverter.ToSingle(data, 0), BitConverter.ToSingle(data, 4), BitConverter.ToSingle(data, 8)); - angles = new Vector3(BitConverter.ToSingle(data, 12), BitConverter.ToSingle(data, 16), BitConverter.ToSingle(data, 20)); + origin = new Vector3d(BitConverter.ToSingle(data, 0), BitConverter.ToSingle(data, 4), BitConverter.ToSingle(data, 8)); + angles = new Vector3d(BitConverter.ToSingle(data, 12), BitConverter.ToSingle(data, 16), BitConverter.ToSingle(data, 20)); dictionaryEntry = BitConverter.ToInt16(data, 24); solidity = data[30]; flags = data[31]; @@ -114,5 +114,6 @@ namespace LibBSP { public static StaticProps LumpFactory(byte[] data, MapType type, int version = 0) { return new StaticProps(data, type, version); } + } } diff --git a/LibBSP/Source/Structs/BSP/Texture.cs b/LibBSP/Source/Structs/BSP/Texture.cs index 59a5c26..2896816 100644 --- a/LibBSP/Source/Structs/BSP/Texture.cs +++ b/LibBSP/Source/Structs/BSP/Texture.cs @@ -3,14 +3,14 @@ #endif using System; -using System.Collections.Generic; #if UNITY using UnityEngine; #endif namespace LibBSP { -#if !UNITY - using Vector3 = Vector3d; +#if UNITY + using Vector2d = Vector2; + using Vector3d = Vector3; #endif /// @@ -56,7 +56,11 @@ namespace LibBSP { case MapType.Quake2: case MapType.SoF: case MapType.Daikatana: { - texAxes = new TextureInfo(new Vector3(BitConverter.ToSingle(data, 0), BitConverter.ToSingle(data, 4), BitConverter.ToSingle(data, 8)), BitConverter.ToSingle(data, 12), 1, new Vector3(BitConverter.ToSingle(data, 16), BitConverter.ToSingle(data, 20), BitConverter.ToSingle(data, 24)), BitConverter.ToSingle(data, 28), 1, -1, -1); + texAxes = new TextureInfo(new Vector3d(BitConverter.ToSingle(data, 0), BitConverter.ToSingle(data, 4), BitConverter.ToSingle(data, 8)), + new Vector3d(BitConverter.ToSingle(data, 16), BitConverter.ToSingle(data, 20), BitConverter.ToSingle(data, 24)), + new Vector2d(BitConverter.ToSingle(data, 12), BitConverter.ToSingle(data, 28)), + Vector2d.one, + -1, -1, 0); flags = BitConverter.ToInt32(data, 32); name = data.ToNullTerminatedString(40, 32); break; @@ -94,7 +98,11 @@ namespace LibBSP { break; } case MapType.SiN: { - texAxes = new TextureInfo(new Vector3(BitConverter.ToSingle(data, 0), BitConverter.ToSingle(data, 4), BitConverter.ToSingle(data, 8)), BitConverter.ToSingle(data, 12), 1, new Vector3(BitConverter.ToSingle(data, 16), BitConverter.ToSingle(data, 20), BitConverter.ToSingle(data, 24)), BitConverter.ToSingle(data, 28), 1, -1, -1); + texAxes = new TextureInfo(new Vector3d(BitConverter.ToSingle(data, 0), BitConverter.ToSingle(data, 4), BitConverter.ToSingle(data, 8)), + new Vector3d(BitConverter.ToSingle(data, 16), BitConverter.ToSingle(data, 20), BitConverter.ToSingle(data, 24)), + new Vector2d(BitConverter.ToSingle(data, 12), BitConverter.ToSingle(data, 28)), + Vector2d.one, + -1, -1, 0); flags = BitConverter.ToInt32(data, 32); name = data.ToNullTerminatedString(36, 64); break; @@ -181,5 +189,6 @@ namespace LibBSP { } } } + } } diff --git a/LibBSP/Source/Structs/BSP/TextureData.cs b/LibBSP/Source/Structs/BSP/TextureData.cs index af132c2..65cdac5 100644 --- a/LibBSP/Source/Structs/BSP/TextureData.cs +++ b/LibBSP/Source/Structs/BSP/TextureData.cs @@ -9,8 +9,8 @@ using UnityEngine; #endif namespace LibBSP { -#if !UNITY - using Vector3 = Vector3d; +#if UNITY + using Vector3d = Vector3; #endif /// @@ -18,7 +18,7 @@ namespace LibBSP { /// public struct TextureData { - public Vector3 reflectivity { get; private set; } + public Vector3d reflectivity { get; private set; } public int stringTableIndex { get; private set; } public int width { get; private set; } public int height { get; private set; } @@ -36,7 +36,7 @@ namespace LibBSP { if (data == null) { throw new ArgumentNullException(); } - reflectivity = new Vector3(BitConverter.ToSingle(data, 0), BitConverter.ToSingle(data, 4), BitConverter.ToSingle(data, 8)); + reflectivity = new Vector3d(BitConverter.ToSingle(data, 0), BitConverter.ToSingle(data, 4), BitConverter.ToSingle(data, 8)); stringTableIndex = BitConverter.ToInt32(data, 12); width = BitConverter.ToInt32(data, 16); height = BitConverter.ToInt32(data, 20); @@ -57,9 +57,10 @@ namespace LibBSP { throw new ArgumentNullException(); } int structLength = 32; - List lump = new List(data.Length / structLength); + int numObjects = data.Length / structLength; + List lump = new List(numObjects); byte[] bytes = new byte[structLength]; - for (int i = 0; i < data.Length / structLength; i++) { + for (int i = 0; i < numObjects; i++) { Array.Copy(data, (i * structLength), bytes, 0, structLength); lump.Add(new TextureData(bytes, type, version)); } @@ -92,5 +93,6 @@ namespace LibBSP { } } } + } } diff --git a/LibBSP/Source/Structs/Common/Entity.cs b/LibBSP/Source/Structs/Common/Entity.cs index 50928d6..ea9c788 100644 --- a/LibBSP/Source/Structs/Common/Entity.cs +++ b/LibBSP/Source/Structs/Common/Entity.cs @@ -3,7 +3,6 @@ #endif using System; -using System.Collections; using System.Collections.Generic; using System.Text; using System.Runtime.Serialization; @@ -13,10 +12,11 @@ using UnityEngine; #endif namespace LibBSP { -#if !UNITY - using Vector3 = Vector3d; - using Vector4 = Vector4d; +#if UNITY + using Vector3d = Vector3; + using Vector4d = Vector4; #endif + /// /// Class containing all data for a single , including attributes, Source Entity I/O connections and solids. /// @@ -41,7 +41,7 @@ namespace LibBSP { get { try { if (ContainsKey("spawnflags")) { - return System.UInt32.Parse(this["spawnflags"]); + return uint.Parse(this["spawnflags"]); } else { return 0; } @@ -55,7 +55,7 @@ namespace LibBSP { /// /// Wrapper for the "origin" attribute. /// - public Vector3 origin { + public Vector3d origin { get { return GetVector("origin"); } set { this["origin"] = value.x + " " + value.y + " " + value.z; } } @@ -63,7 +63,7 @@ namespace LibBSP { /// /// Wrapper for the "angles" attribute. /// - public Vector3 angles { + public Vector3d angles { get { return GetVector("angles"); } set { this["angles"] = value.x + " " + value.y + " " + value.z; } } @@ -114,7 +114,7 @@ namespace LibBSP { string st = this["model"]; if (st[0] == '*') { int ret = -1; - if (Int32.TryParse(st.Substring(1), out ret)) { + if (int.TryParse(st.Substring(1), out ret)) { return ret; } else { return -1; @@ -351,8 +351,8 @@ namespace LibBSP { target = connection[0], action = connection[1], param = connection[2], - delay = Double.Parse(connection[3], _format), - fireOnce = Int32.Parse(connection[4]), + delay = double.Parse(connection[3], _format), + fireOnce = int.Parse(connection[4]), unknown0 = connection.Length > 5 ? connection[5] : "", unknown1 = connection.Length > 6 ? connection[6] : "", }); @@ -444,7 +444,7 @@ namespace LibBSP { /// The numeric value of the value corresponding to . public float GetFloat(string key, float? failDefault = null) { try { - return Single.Parse(this[key], _format); + return float.Parse(this[key], _format); } catch (Exception e) { if (!failDefault.HasValue) { throw e; @@ -462,7 +462,7 @@ namespace LibBSP { /// The numeric value of the value corresponding to . public int GetInt(string key, int? failDefault = null) { try { - return Int32.Parse(this[key], _format); + return int.Parse(this[key], _format); } catch (Exception e) { if (!failDefault.HasValue) { throw e; @@ -472,23 +472,23 @@ namespace LibBSP { } /// - /// Gets a Vector attribute as a . This will only read as many values as are in the attribute. + /// Gets a Vector attribute as a . This will only read as many values as are in the attribute. /// /// Name of the attribute to retrieve. /// Vector representation of the components of the attribute. - public Vector4 GetVector(string key) { + public Vector4d GetVector(string key) { float[] results = new float[4]; if (ContainsKey(key) && !string.IsNullOrEmpty(this[key])) { string[] nums = this[key].Split(' '); for (int i = 0; i < results.Length && i < nums.Length; ++i) { try { - results[i] = Single.Parse(nums[i], _format); + results[i] = float.Parse(nums[i], _format); } catch { results[i] = 0; } } } - return new Vector4(results[0], results[1], results[2], results[3]); + return new Vector4d(results[0], results[1], results[2], results[3]); } #region IComparable diff --git a/LibBSP/Source/Structs/Common/Lumps/Entities.cs b/LibBSP/Source/Structs/Common/Lumps/Entities.cs index 68d2b8f..3a2c6ff 100644 --- a/LibBSP/Source/Structs/Common/Lumps/Entities.cs +++ b/LibBSP/Source/Structs/Common/Lumps/Entities.cs @@ -1,8 +1,10 @@ using System; using System.IO; using System.Collections.Generic; +using System.Text; namespace LibBSP { + /// /// Class representing a group of objects. Contains helpful methods to handle Entities in the List. /// @@ -49,7 +51,7 @@ namespace LibBSP { // all text between them. char currentChar; // This will be the resulting entity, fed into Entity.FromString - System.Text.StringBuilder current = new System.Text.StringBuilder(); + StringBuilder current = new StringBuilder(); for (int offset = 0; offset < data.Length; ++offset) { currentChar = (char)data[offset]; @@ -88,7 +90,7 @@ namespace LibBSP { if (offset == 0 || (char)data[offset - 1] == '\n' || (char)data[offset - 1] == '\t' || (char)data[offset - 1] == ' ' || (char)data[offset - 1] == '\r') { --braceCount; if (braceCount == 0) { - this.Add(Entity.FromString(current.ToString())); + Add(Entity.FromString(current.ToString())); // Reset StringBuilder current.Length = 0; } @@ -108,7 +110,7 @@ namespace LibBSP { /// Attribute to match. /// Desired value of attribute. public void RemoveAllWithAttribute(string key, string value) { - this.RemoveAll(entity => { return entity[key].Equals(value, StringComparison.InvariantCultureIgnoreCase); }); + RemoveAll(entity => { return entity[key].Equals(value, StringComparison.InvariantCultureIgnoreCase); }); } /// diff --git a/LibBSP/Source/Structs/Common/Lumps/NumList.cs b/LibBSP/Source/Structs/Common/Lumps/NumList.cs index 2f9a0c5..04676e5 100644 --- a/LibBSP/Source/Structs/Common/Lumps/NumList.cs +++ b/LibBSP/Source/Structs/Common/Lumps/NumList.cs @@ -2,6 +2,7 @@ using System; using System.Collections.Generic; namespace LibBSP { + /// /// List class for numbers. Can handle any integer data type except ulong. /// diff --git a/LibBSP/Source/Structs/Common/Plane.cs b/LibBSP/Source/Structs/Common/Plane.cs index 57572c2..0fe4e37 100644 --- a/LibBSP/Source/Structs/Common/Plane.cs +++ b/LibBSP/Source/Structs/Common/Plane.cs @@ -1,8 +1,8 @@ -#if !(UNITY || UNITY_2_6 || UNITY_2_6_1 || UNITY_3_0 || UNITY_3_0_0 || UNITY_3_1 || UNITY_3_2 || UNITY_3_3 || UNITY_3_4 || UNITY_3_5 || UNITY_4_0 || UNITY_4_0_1 || UNITY_4_2 || UNITY_4_3 || UNITY_4_5 || UNITY_4_6 || UNITY_5 || UNITY_5_3_OR_NEWER) +#if !(UNITY_2_6 || UNITY_2_6_1 || UNITY_3_0 || UNITY_3_0_0 || UNITY_3_1 || UNITY_3_2 || UNITY_3_3 || UNITY_3_4 || UNITY_3_5 || UNITY_4_0 || UNITY_4_0_1 || UNITY_4_2 || UNITY_4_3 || UNITY_4_5 || UNITY_4_6 || UNITY_5 || UNITY_5_3_OR_NEWER) using System; -using System.Collections.Generic; namespace LibBSP { + /// /// Holds the data for a plane in 3D space in Hesse Normal Form. /// @@ -96,9 +96,9 @@ namespace LibBSP { /// Normal of this . /// Distance from the origin to this . public Plane(Vector3d normal, double dist) { - this._normal = new Vector3d(normal); + _normal = new Vector3d(normal); _normal.Normalize(); - this.distance = dist; + distance = dist; } /// @@ -123,9 +123,9 @@ namespace LibBSP { /// Normal of this . /// A point on this . public Plane(Vector3d normal, Vector3d point) { - this._normal = normal; + _normal = normal; _normal.Normalize(); - this.distance = point * normal; + distance = point * normal; } /// @@ -153,7 +153,7 @@ namespace LibBSP { /// object to compare to. /// Whether is a and is equal to this . public override bool Equals(object obj) { - if (object.ReferenceEquals(obj, null) || !GetType().IsAssignableFrom(obj.GetType())) { return false; } + if (ReferenceEquals(obj, null) || !GetType().IsAssignableFrom(obj.GetType())) { return false; } return Equals((Plane)obj); } @@ -210,9 +210,9 @@ namespace LibBSP { /// Signed distance from this to the given point. public double GetDistanceToPoint(Vector3d to) { // Ax + By + Cz - d = DISTANCE = normDOTpoint - d - double normLength = System.Math.Pow(normal.x, 2) + System.Math.Pow(normal.y, 2) + System.Math.Pow(normal.z, 2); - if (System.Math.Abs(normLength - 1.00) > 0.01) { - normLength = System.Math.Sqrt(normLength); + double normLength = Math.Pow(normal.x, 2) + Math.Pow(normal.y, 2) + Math.Pow(normal.z, 2); + if (Math.Abs(normLength - 1.00) > 0.01) { + normLength = Math.Sqrt(normLength); } return (normal.x * to.x + normal.y * to.y + normal.z * to.z - distance) / normLength; } diff --git a/LibBSP/Source/Structs/Common/Ray.cs b/LibBSP/Source/Structs/Common/Ray.cs index e6b62a1..e83a564 100644 --- a/LibBSP/Source/Structs/Common/Ray.cs +++ b/LibBSP/Source/Structs/Common/Ray.cs @@ -1,7 +1,8 @@ -#if !(UNITY || UNITY_2_6 || UNITY_2_6_1 || UNITY_3_0 || UNITY_3_0_0 || UNITY_3_1 || UNITY_3_2 || UNITY_3_3 || UNITY_3_4 || UNITY_3_5 || UNITY_4_0 || UNITY_4_0_1 || UNITY_4_2 || UNITY_4_3 || UNITY_4_5 || UNITY_4_6 || UNITY_5 || UNITY_5_3_OR_NEWER) +#if !(UNITY_2_6 || UNITY_2_6_1 || UNITY_3_0 || UNITY_3_0_0 || UNITY_3_1 || UNITY_3_2 || UNITY_3_3 || UNITY_3_4 || UNITY_3_5 || UNITY_4_0 || UNITY_4_0_1 || UNITY_4_2 || UNITY_4_3 || UNITY_4_5 || UNITY_4_6 || UNITY_5 || UNITY_5_3_OR_NEWER) using System; namespace LibBSP { + /// /// A struct for a defined by a starting point and a direction vector. /// @@ -11,7 +12,9 @@ namespace LibBSP { private Vector3d _direction; public Vector3d direction { - get { return _direction; } + get { + return _direction; + } set { _direction = value.normalized; } @@ -24,8 +27,7 @@ namespace LibBSP { /// Direction vector of this . public Ray(Vector3d origin, Vector3d direction) { this.origin = origin; - this._direction = direction; - _direction.Normalize(); + _direction = direction.normalized; } /// @@ -81,7 +83,7 @@ namespace LibBSP { /// The object to compare to. /// true if is a and this and have the same and . public override bool Equals(object obj) { - if (object.ReferenceEquals(obj, null) || !GetType().IsAssignableFrom(obj.GetType())) { return false; } + if (ReferenceEquals(obj, null) || !GetType().IsAssignableFrom(obj.GetType())) { return false; } return Equals((Ray)obj); } diff --git a/LibBSP/Source/Structs/Common/Rect.cs b/LibBSP/Source/Structs/Common/Rect.cs deleted file mode 100644 index 0056bdd..0000000 --- a/LibBSP/Source/Structs/Common/Rect.cs +++ /dev/null @@ -1,302 +0,0 @@ -#if !(UNITY || UNITY_2_6 || UNITY_2_6_1 || UNITY_3_0 || UNITY_3_0_0 || UNITY_3_1 || UNITY_3_2 || UNITY_3_3 || UNITY_3_4 || UNITY_3_5 || UNITY_4_0 || UNITY_4_0_1 || UNITY_4_2 || UNITY_4_3 || UNITY_4_5 || UNITY_4_6 || UNITY_5 || UNITY_5_3_OR_NEWER) -using System; -using System.Collections.Generic; - -namespace LibBSP { - /// - /// Struct defining a rectangle in 2D space. - /// - public struct Rect : IEquatable { - - public Vector2d position; - public Vector2d size; - - /// - /// Gets the center point of this . - /// - public Vector2d center { - get { - return position + (size / 2.0); - } - } - - /// - /// Gets the width of this . - /// - public double width { - get { - return size.x; - } - set { - size.x = value; - } - } - - /// - /// Gets the height of this . - /// - public double height { - get { - return size.y; - } - set { - size.y = value; - } - } - - /// - /// Gets the bottom-left coordinate of this . - /// - public Vector2d min { - get { - return new Vector2d(position.x, position.y + size.y); - } - set { - position.x = value.x; - size.y = value.y - position.y; - } - } - - /// - /// Gets the top-right coordinate of this . - /// - public Vector2d max { - get { - return new Vector2d(position.x + size.x, position.y); - } - set { - position.y = value.y; - size.x = value.x - position.x; - } - } - - /// - /// Gets the left bound of this . - /// - public double x { - get { - return position.x; - } - set { - position.x = value; - } - } - - /// - /// Gets the left bound of this . - /// - public double xMin { - get { - return position.x; - } - set { - position.x = value; - } - } - - /// - /// Gets the right bound of this . - /// - public double xMax { - get { - return position.x + size.x; - } - set { - size.x = value - position.x; - } - } - - /// - /// Gets the upper bound of this . - /// - public double y { - get { - return position.y; - } - set { - position.y = value; - } - } - - /// - /// Gets the upper bound of this . - /// - public double yMin { - get { - return position.y; - } - set { - position.y = value; - } - } - - /// - /// Gets the lower bound of this . - /// - public double yMax { - get { - return position.y + size.y; - } - set { - size.y = value - position.y; - } - } - - /// - /// Creates a new object. - /// - /// Left bound. - /// Upper bound. - /// Width. - /// Height. - public Rect(double left, double top, double width, double height) : this() { - position = new Vector2d(left, top); - size = new Vector2d(width, height); - } - - /// - /// Creates a new object from the specified bounds. - /// - /// Left bound. - /// Upper bound. - /// Right bound. - /// Lower bound. - /// The resulting - public static Rect MinMaxRect(double left, double top, double right, double bottom) { - return new Rect(left, top, right - left, bottom - top); - } - - /// - /// Gets whether this contains the specified point. - /// - /// The point to check. - /// If the 's size is negative, should the check allow this? - /// true if the point is contained in the . - public bool Contains(Vector2d point, bool allowInverse = false) { - if (allowInverse) { - return point.x > position.x && point.x < position.x + size.x && point.y > position.y && point.y < position.y + size.y; - } else { - double xmax = Math.Max(x, xMax); - double ymax = Math.Max(y, yMax); - - return MinMaxRect(x, y, xmax, ymax).Contains(point, true); - } - } - - /// - /// Gets whether this overlaps the specified . - /// - /// The to check. - /// If either 's size is negative, should the check allow this? - /// true if the other overlaps this . - public bool Overlaps(Rect other, bool allowInverse = false) { - if (allowInverse) { - return Contains(other.position, true) || Contains(other.position + other.size, true) || Contains(other.min, true) || Contains(other.max, true) || - other.Contains(position, true) || other.Contains(position + size, true) || other.Contains(min, true) || other.Contains(max, true); - } else { - double xmax = Math.Max(x, xMax); - double ymax = Math.Max(y, yMax); - double otherXmax = Math.Max(other.x, other.xMax); - double otherYmax = Math.Max(other.y, other.yMax); - - return MinMaxRect(x, y, xmax, ymax).Overlaps(MinMaxRect(other.x, other.y, otherXmax, otherYmax), true); - } - } - - /// - /// Sets this 's components. - /// - /// The left bound to set. - /// The upper bound to set. - /// The width to set. - /// The height to set. - public void Set(double left, double top, double width, double height) { - position.x = left; - position.y = top; - size.x = width; - size.y = height; - } - - /// - /// Given a normalized point, returns a coordinate point inside the . - /// - /// The . - /// The normalized coordinates. - /// The denormalized coordinates. - public static Vector2d NormalizedToPoint(Rect rect, Vector2d normalizedCoordinates) { - return new Vector2d(rect.x + (rect.width * normalizedCoordinates.x), rect.y + (rect.height * normalizedCoordinates.y)); - } - - /// - /// Given a point, returns a normalized point relative to the . - /// - /// The . - /// The coordinates. - /// The normalized coordinates. - public static Vector2d PointToNormalized(Rect rect, Vector2d point) { - if (rect.width != 0 && rect.height != 0) { - return new Vector2d((point.x - rect.x) / rect.width, (point.y - rect.y) / rect.height); - } else { - return new Vector2d(Double.NaN, Double.NaN); - } - } - - /// - /// Gives a nicely formatted string for this . - /// - /// Nicely formatted string for this . - public override string ToString() { - return string.Format("(x:{1}, y:{2}, width:{3}, height:{4})", x, y, width, height); - } - - #region IEquatable - /// - /// Determines whether this equals another. - /// - /// The to compare to. - /// true if the s are equal. - public bool Equals(Rect other) { - return position == other.position && size == other.size; - } - - /// - /// Determines whether this equals another object. - /// - /// The object to compare to. - /// true if the other object is not null and a , and the s are equal. - public override bool Equals(object obj) { - if (object.ReferenceEquals(obj, null)) { return false; } - if (!(obj is Rect)) { return false; } - return Equals((Rect)obj); - } - - /// - /// Determines whether this equals another. - /// - /// The to compare to. - /// true if the s are equal. - public static bool operator ==(Rect r1, Rect r2) { - return r1.Equals(r2); - } - - /// - /// Determines whether this does not equal another. - /// - /// The to compare to. - /// true if the s are not equal. - public static bool operator !=(Rect r1, Rect r2) { - return !r1.Equals(r2); - } - - /// - /// Generates a hash code for this instance based on instance data. - /// - /// The hash code for this instance. - public override int GetHashCode() { - return position.GetHashCode() ^ size.GetHashCode(); - } - #endregion - - } -} -#endif diff --git a/LibBSP/Source/Structs/BSP/TextureInfo.cs b/LibBSP/Source/Structs/Common/TextureInfo.cs similarity index 69% rename from LibBSP/Source/Structs/BSP/TextureInfo.cs rename to LibBSP/Source/Structs/Common/TextureInfo.cs index caca927..07f99b3 100644 --- a/LibBSP/Source/Structs/BSP/TextureInfo.cs +++ b/LibBSP/Source/Structs/Common/TextureInfo.cs @@ -9,8 +9,9 @@ using UnityEngine; #endif namespace LibBSP { -#if !UNITY - using Vector3 = Vector3d; +#if UNITY + using Vector2d = Vector2; + using Vector3d = Vector3; #endif /// @@ -25,20 +26,33 @@ namespace LibBSP { /// /// Array of base texture axes. When referenced properly, provides a good default texture axis for any given plane. /// - public static readonly Vector3[] baseAxes = new Vector3[] { - new Vector3(0, 0, 1), new Vector3(1, 0, 0), new Vector3(0, -1, 0), - new Vector3(0, 0, -1), new Vector3(1, 0, 0), new Vector3(0, -1, 0), - new Vector3(1, 0, 0), new Vector3(0, 1, 0), new Vector3(0, 0, -1), - new Vector3(-1, 0, 0), new Vector3(0, 1, 0), new Vector3(0, 0, -1), - new Vector3(0, 1, 0), new Vector3(1, 0, 0), new Vector3(0, 0, -1), - new Vector3(0, -1, 0), new Vector3(1, 0, 0), new Vector3(0, 0, -1) + public static readonly Vector3d[] baseAxes = new Vector3d[] { + new Vector3d(0, 0, 1), new Vector3d(1, 0, 0), new Vector3d(0, -1, 0), + new Vector3d(0, 0, -1), new Vector3d(1, 0, 0), new Vector3d(0, -1, 0), + new Vector3d(1, 0, 0), new Vector3d(0, 1, 0), new Vector3d(0, 0, -1), + new Vector3d(-1, 0, 0), new Vector3d(0, 1, 0), new Vector3d(0, 0, -1), + new Vector3d(0, 1, 0), new Vector3d(1, 0, 0), new Vector3d(0, 0, -1), + new Vector3d(0, -1, 0), new Vector3d(1, 0, 0), new Vector3d(0, 0, -1) }; - public Vector3[] axes { get; private set; } - public float[] shifts { get; private set; } - public float[] scales { get; private set; } - public int flags { get; private set; } - public int texture { get; private set; } + public Vector3d[] axes { get; set; } + public Vector2d translation { get; set; } + public Vector2d scale { get; set; } + public int flags { get; set; } + public int texture { get; set; } + public double rotation { get; set; } + + /// + /// Creates a new object with sensible defaults. + /// + public TextureInfo() { + axes = new Vector3d[2]; + translation = Vector2d.zero; + scale = Vector2d.one; + flags = 0; + texture = 0; + rotation = 0; + } /// /// Creates a new object from a byte array. @@ -54,14 +68,13 @@ namespace LibBSP { } texture = -1; flags = -1; - axes = new Vector3[2]; - shifts = new float[2]; - axes[S] = new Vector3(BitConverter.ToSingle(data, 0), BitConverter.ToSingle(data, 4), BitConverter.ToSingle(data, 8)); - shifts[S] = BitConverter.ToSingle(data, 12); - axes[T] = new Vector3(BitConverter.ToSingle(data, 16), BitConverter.ToSingle(data, 20), BitConverter.ToSingle(data, 24)); - shifts[T] = BitConverter.ToSingle(data, 28); + axes = new Vector3d[2]; + translation = new Vector2d(BitConverter.ToSingle(data, 12), BitConverter.ToSingle(data, 28)); + axes[S] = new Vector3d(BitConverter.ToSingle(data, 0), BitConverter.ToSingle(data, 4), BitConverter.ToSingle(data, 8)); + axes[T] = new Vector3d(BitConverter.ToSingle(data, 16), BitConverter.ToSingle(data, 20), BitConverter.ToSingle(data, 24)); // Texture scaling information is compiled into the axes by changing their length. - scales = new float[] { 1, 1 }; + scale = Vector2d.one; + rotation = 0; switch (type) { // Excluded engines: Quake 2-based, Quake 3-based case MapType.Source17: @@ -102,48 +115,42 @@ namespace LibBSP { /// Creates a new object using the passed data. /// /// The S texture axis. - /// The texture shift on the S axis. - /// The texture scale on the S axis. /// The T texture axis. - /// The texture shift on the T axis. - /// The texture scale on the T axis. + /// Texture translation along both axes (in pixels). + /// Texture scale along both axes. /// The flags for this . /// Index into the texture list for the texture this uses. - public TextureInfo(Vector3 s, float sShift, float sScale, Vector3 t, float tShift, float tScale, int flags, int texture) { - axes = new Vector3[2]; + /// Rotation of the texutre axes. + public TextureInfo(Vector3d s, Vector3d t, Vector2d translation, Vector2d scale, int flags, int texture, double rotation) { + axes = new Vector3d[2]; axes[S] = s; axes[T] = t; - shifts = new float[2]; - scales = new float[2]; - shifts[S] = sShift; - shifts[T] = tShift; - scales[S] = sScale; - scales[T] = tScale; + this.translation = translation; + this.scale = scale; this.flags = flags; this.texture = texture; + this.rotation = rotation; } /// - /// Adapted from code in the Quake III Arena source code. Stolen without permission - /// because it falls under the terms of the GPL v2 license, and because I'm not making - /// any money, just awesome tools. + /// Given a p, return an optimal set of texture axes for it. /// /// of the surface. /// The best matching texture axes for the given . - public static Vector3[] TextureAxisFromPlane(Plane p) { + public static Vector3d[] TextureAxisFromPlane(Plane p) { int bestaxis = 0; double best = 0; // "Best" dot product so far for (int i = 0; i < 6; ++i) { // For all possible axes, positive and negative - double dot = Vector3.Dot(p.normal, new Vector3(baseAxes[i * 3][0], baseAxes[i * 3][1], baseAxes[i * 3][2])); + double dot = Vector3d.Dot(p.normal, new Vector3d(baseAxes[i * 3][0], baseAxes[i * 3][1], baseAxes[i * 3][2])); if (dot > best) { best = dot; bestaxis = i; } } - Vector3[] newAxes = new Vector3[2]; - newAxes[0] = new Vector3(baseAxes[bestaxis * 3 + 1][0], baseAxes[bestaxis * 3 + 1][1], baseAxes[bestaxis * 3 + 1][2]); - newAxes[1] = new Vector3(baseAxes[bestaxis * 3 + 2][0], baseAxes[bestaxis * 3 + 2][1], baseAxes[bestaxis * 3 + 2][2]); + Vector3d[] newAxes = new Vector3d[2]; + newAxes[0] = new Vector3d(baseAxes[bestaxis * 3 + 1][0], baseAxes[bestaxis * 3 + 1][1], baseAxes[bestaxis * 3 + 1][2]); + newAxes[1] = new Vector3d(baseAxes[bestaxis * 3 + 2][0], baseAxes[bestaxis * 3 + 2][1], baseAxes[bestaxis * 3 + 2][2]); return newAxes; } @@ -192,9 +199,10 @@ namespace LibBSP { throw new ArgumentException("Map type " + type + " isn't supported by the Leaf lump factory."); } } - List lump = new List(data.Length / structLength); + int numObjects = data.Length / structLength; + List lump = new List(numObjects); byte[] bytes = new byte[structLength]; - for (int i = 0; i < data.Length / structLength; ++i) { + for (int i = 0; i < numObjects; ++i) { Array.Copy(data, (i * structLength), bytes, 0, structLength); lump.Add(new TextureInfo(bytes, type, version)); } @@ -231,5 +239,6 @@ namespace LibBSP { } } } + } } diff --git a/LibBSP/Source/Structs/Common/UIVertex.cs b/LibBSP/Source/Structs/Common/UIVertex.cs deleted file mode 100644 index b90dbab..0000000 --- a/LibBSP/Source/Structs/Common/UIVertex.cs +++ /dev/null @@ -1,48 +0,0 @@ -#if (UNITY_2_6 || UNITY_2_6_1 || UNITY_3_0 || UNITY_3_0_0 || UNITY_3_1 || UNITY_3_2 || UNITY_3_3 || UNITY_3_4 || UNITY_3_5 || UNITY_4_0 || UNITY_4_0_1 || UNITY_4_2 || UNITY_4_3 || UNITY_4_5 || UNITY_4_6 || UNITY_5 || UNITY_5_3_OR_NEWER) -#define UNITY -#endif -#if !(UNITY_4_6 || UNITY_5 || UNITY_5_3_OR_NEWER) -#if UNITY -using UnityEngine; -#else -using System.Drawing; -#endif -using System; - -namespace LibBSP { -#if !UNITY - using Color32 = Color; - using Vector2 = Vector2d; - using Vector3 = Vector3d; - using Vector4 = Vector4d; -#endif - /// - /// Replacement UIVertex struct for Unity versions before 4.6, and standalone projects. - /// - /// Unity 4.5 had UIVertex but the implementation was incomplete. - [Serializable] public struct UIVertex { - public Color32 color; - public Vector3 normal; - public Vector3 position; - public Vector4 tangent; - public Vector2 uv0; - public Vector2 uv1; - - /// - /// Simple UIVertex with sensible settings. - /// - public static UIVertex simpleVert { - get { - return new UIVertex { - color = Color32Extensions.FromArgb(255, 255, 255, 255), - normal = new Vector3(0, 0, -1), - position = new Vector3(0, 0, 0), - tangent = new Vector4(1, 0, 0, -1), - uv0 = new Vector2(0, 0), - uv1 = new Vector2(0, 0) - }; - } - } - } -} -#endif diff --git a/LibBSP/Source/Structs/Common/Vector2d.cs b/LibBSP/Source/Structs/Common/Vector2d.cs index 55b32f8..cf29224 100644 --- a/LibBSP/Source/Structs/Common/Vector2d.cs +++ b/LibBSP/Source/Structs/Common/Vector2d.cs @@ -4,6 +4,7 @@ using System.Collections; using System.Collections.Generic; namespace LibBSP { + /// /// Holds two doubles representing a 2-dimensional vector. /// @@ -287,7 +288,7 @@ namespace LibBSP { return Scale(v1, 1.0 / divisor); } - #region IEquatable +#region IEquatable /// /// Equivalency. Returns true if the components of two vectors are equal or approximately equal. /// @@ -336,7 +337,7 @@ namespace LibBSP { public override int GetHashCode() { return x.GetHashCode() ^ y.GetHashCode(); } - #endregion +#endregion /// /// Calculates the distance from this vector to another. @@ -390,7 +391,7 @@ namespace LibBSP { return 4.0 * a * a * b * b - Math.Pow((a * a) + (b * b) - (c * c), 2); } - #region IEnumerable +#region IEnumerable /// /// Allows enumeration through the components of a using a foreach loop. /// @@ -411,7 +412,7 @@ namespace LibBSP { yield return x; yield return y; } - #endregion +#endregion /// /// Implicitly converts this into a . This will be called when Vector3d v3 = v2 is used. diff --git a/LibBSP/Source/Structs/Common/Vector3d.cs b/LibBSP/Source/Structs/Common/Vector3d.cs index ad96391..f038d30 100644 --- a/LibBSP/Source/Structs/Common/Vector3d.cs +++ b/LibBSP/Source/Structs/Common/Vector3d.cs @@ -5,7 +5,7 @@ using System.Collections.Generic; using System.Drawing; namespace LibBSP { - using Color32 = Color; + /// /// Holds three doubles representing a 3-dimensional vector. /// @@ -337,7 +337,7 @@ namespace LibBSP { return Scale(v1, 1.0 / divisor); } - #region IEquatable +#region IEquatable /// /// Equivalency. Returns true if the components of two vectors are equal or approximately equal. /// @@ -386,7 +386,7 @@ namespace LibBSP { public override int GetHashCode() { return x.GetHashCode() ^ y.GetHashCode() ^ z.GetHashCode(); } - #endregion +#endregion /// /// Calculates the distance from this vector to another. @@ -441,7 +441,7 @@ namespace LibBSP { return 4.0 * a * a * b * b - Math.Pow((a * a) + (b * b) - (c * c), 2); } - #region IEnumerable +#region IEnumerable /// /// Allows enumeration through the components of a using a foreach loop. /// @@ -464,7 +464,7 @@ namespace LibBSP { yield return y; yield return z; } - #endregion +#endregion /// /// Implicitly converts this into a . This will be called when Vector2d v2 = v3 is used. @@ -489,8 +489,8 @@ namespace LibBSP { /// /// to convert. /// This in a Color object interpreted as RGB. - public static implicit operator Color32(Vector3d v) { - return Color32Extensions.FromArgb(255, (int)Math.Max(v.x, 255), (int)Math.Max(v.y, 255), (int)Math.Max(v.z, 255)); + public static implicit operator Color(Vector3d v) { + return ColorExtensions.FromArgb(255, (int)Math.Max(v.x, 255), (int)Math.Max(v.y, 255), (int)Math.Max(v.z, 255)); } } } diff --git a/LibBSP/Source/Structs/Common/Vector4d.cs b/LibBSP/Source/Structs/Common/Vector4d.cs index 15cbf04..e0446d2 100644 --- a/LibBSP/Source/Structs/Common/Vector4d.cs +++ b/LibBSP/Source/Structs/Common/Vector4d.cs @@ -5,7 +5,7 @@ using System.Collections.Generic; using System.Drawing; namespace LibBSP { - using Color32 = Color; + /// /// Holds four doubles representing a 4-dimensional vector. /// @@ -332,7 +332,7 @@ namespace LibBSP { return Scale(v1, 1.0 / divisor); } - #region IEquatable +#region IEquatable /// /// Equivalency. Returns true if the components of two vectors are equal or approximately equal. /// @@ -437,7 +437,7 @@ namespace LibBSP { return 4.0 * a * a * b * b - Math.Pow((a * a) + (b * b) - (c * c), 2); } - #region IEnumerable +#region IEnumerable /// /// Allows enumeration through the components of a using a foreach loop. /// @@ -462,7 +462,7 @@ namespace LibBSP { yield return z; yield return w; } - #endregion +#endregion /// /// Implicitly converts this into a . This will be called when Vector2d v2 = v4 is used. @@ -488,8 +488,8 @@ namespace LibBSP { /// /// to convert. /// This in a Color object interpreted as RGBA. - public static implicit operator Color32(Vector4d v) { - return Color32Extensions.FromArgb((int)Math.Max(v.w, 255), (int)Math.Max(v.x, 255), (int)Math.Max(v.y, 255), (int)Math.Max(v.z, 255)); + public static implicit operator Color(Vector4d v) { + return ColorExtensions.FromArgb((int)Math.Max(v.w, 255), (int)Math.Max(v.x, 255), (int)Math.Max(v.y, 255), (int)Math.Max(v.z, 255)); } } } diff --git a/LibBSP/Source/Structs/Common/Vertex.cs b/LibBSP/Source/Structs/Common/Vertex.cs new file mode 100644 index 0000000..e215c49 --- /dev/null +++ b/LibBSP/Source/Structs/Common/Vertex.cs @@ -0,0 +1,55 @@ +#if UNITY_2_6 || UNITY_2_6_1 || UNITY_3_0 || UNITY_3_0_0 || UNITY_3_1 || UNITY_3_2 || UNITY_3_3 || UNITY_3_4 || UNITY_3_5 || UNITY_4_0 || UNITY_4_0_1 || UNITY_4_2 || UNITY_4_3 || UNITY_4_5 || UNITY_4_6 || UNITY_5 || UNITY_5_3_OR_NEWER +#define UNITY +#if !(UNITY_4_6 || UNITY_5 || UNITY_5_3_OR_NEWER) +// Unity 4.5 had UIVertex but the implementation was incomplete. +#define OLDUNITY +#endif +#endif + +#if !UNITY || OLDUNITY + +using System; + +namespace LibBSP { +#if UNITY + using Color = UnityEngine.Color; + using Vector2d = UnityEngine.Vector2; + using Vector3d = UnityEngine.Vector3; + using Vector4d = UnityEngine.Vector4; +#else + using Color = System.Drawing.Color; +#endif + + /// + /// Vertex struct, including fields for normal, tangent and four sets of UVs. + /// + [Serializable] public struct Vertex { + public Color color; + public Vector3d normal; + public Vector3d position; + public Vector4d tangent; + public Vector2d uv0; + public Vector2d uv1; + public Vector2d uv2; + public Vector2d uv3; + + /// + /// Simple UIVertex with sensible settings. + /// + public static Vertex simpleVert { + get { + return new Vertex { + color = ColorExtensions.FromArgb(255, 255, 255, 255), + normal = new Vector3d(0, 0, -1), + position = new Vector3d(0, 0, 0), + tangent = new Vector4d(1, 0, 0, -1), + uv0 = new Vector2d(0, 0), + uv1 = new Vector2d(0, 0), + uv2 = new Vector2d(0, 0), + uv3 = new Vector2d(0, 0), + }; + } + } + } +} +#endif diff --git a/LibBSP/Source/Structs/MAP/MAPBrush.cs b/LibBSP/Source/Structs/MAP/MAPBrush.cs index 777d26b..10c16a0 100644 --- a/LibBSP/Source/Structs/MAP/MAPBrush.cs +++ b/LibBSP/Source/Structs/MAP/MAPBrush.cs @@ -1,11 +1,8 @@ -#if (UNITY_2_6 || UNITY_2_6_1 || UNITY_3_0 || UNITY_3_0_0 || UNITY_3_1 || UNITY_3_2 || UNITY_3_3 || UNITY_3_4 || UNITY_3_5 || UNITY_4_0 || UNITY_4_0_1 || UNITY_4_2 || UNITY_4_3 || UNITY_4_5 || UNITY_4_6 || UNITY_5 || UNITY_5_3_OR_NEWER) -#define UNITY -#endif - using System; using System.Collections.Generic; namespace LibBSP { + /// /// Class containing all data for a single brush, including side definitions or a patch definition. /// diff --git a/LibBSP/Source/Structs/MAP/MAPBrushSide.cs b/LibBSP/Source/Structs/MAP/MAPBrushSide.cs index cd0b305..b9bdda1 100644 --- a/LibBSP/Source/Structs/MAP/MAPBrushSide.cs +++ b/LibBSP/Source/Structs/MAP/MAPBrushSide.cs @@ -1,4 +1,4 @@ -#if (UNITY_2_6 || UNITY_2_6_1 || UNITY_3_0 || UNITY_3_0_0 || UNITY_3_1 || UNITY_3_2 || UNITY_3_3 || UNITY_3_4 || UNITY_3_5 || UNITY_4_0 || UNITY_4_0_1 || UNITY_4_2 || UNITY_4_3 || UNITY_4_5 || UNITY_4_6 || UNITY_5 || UNITY_5_3_OR_NEWER) +#if UNITY_2_6 || UNITY_2_6_1 || UNITY_3_0 || UNITY_3_0_0 || UNITY_3_1 || UNITY_3_2 || UNITY_3_3 || UNITY_3_4 || UNITY_3_5 || UNITY_4_0 || UNITY_4_0_1 || UNITY_4_2 || UNITY_4_3 || UNITY_4_5 || UNITY_4_6 || UNITY_5 || UNITY_5_3_OR_NEWER #define UNITY #endif @@ -10,9 +10,11 @@ using UnityEngine; #endif namespace LibBSP { -#if !UNITY - using Vector3 = Vector3d; +#if UNITY + using Vector2d = Vector2; + using Vector3d = Vector3; #endif + /// /// Class containing data for a brush side. Please note vertices must be set manually or generated through CSG. /// @@ -20,17 +22,10 @@ namespace LibBSP { private static IFormatProvider _format = CultureInfo.CreateSpecificCulture("en-US"); - public Vector3[] vertices; + public Vector3d[] vertices; public Plane plane; public string texture; - public Vector3 textureS; - public double textureShiftS; - public Vector3 textureT; - public double textureShiftT; - public float texRot; - public double texScaleX; - public double texScaleY; - public int flags; + public TextureInfo textureInfo; public string material; public double lgtScale; public double lgtRot; @@ -53,42 +48,43 @@ namespace LibBSP { float dist = 0; // If this succeeds, assume brushDef3 - if (Single.TryParse(tokens[4], out dist)) { - plane = new Plane(new Vector3(Single.Parse(tokens[1], _format), Single.Parse(tokens[2], _format), Single.Parse(tokens[3], _format)), dist); - textureS = new Vector3(Single.Parse(tokens[8], _format), Single.Parse(tokens[9], _format), Single.Parse(tokens[10], _format)); - textureT = new Vector3(Single.Parse(tokens[13], _format), Single.Parse(tokens[14], _format), Single.Parse(tokens[15], _format)); + if (float.TryParse(tokens[4], out dist)) { + plane = new Plane(new Vector3d(float.Parse(tokens[1], _format), float.Parse(tokens[2], _format), float.Parse(tokens[3], _format)), dist); + textureInfo = new TextureInfo(new Vector3d(float.Parse(tokens[8], _format), float.Parse(tokens[9], _format), float.Parse(tokens[10], _format)), + new Vector3d(float.Parse(tokens[13], _format), float.Parse(tokens[14], _format), float.Parse(tokens[15], _format)), + Vector2d.zero, + Vector2d.one, + 0, 0, 0); texture = tokens[18]; } else { - Vector3 v1 = new Vector3(Single.Parse(tokens[1], _format), Single.Parse(tokens[2], _format), Single.Parse(tokens[3], _format)); - Vector3 v2 = new Vector3(Single.Parse(tokens[6], _format), Single.Parse(tokens[7], _format), Single.Parse(tokens[8], _format)); - Vector3 v3 = new Vector3(Single.Parse(tokens[11], _format), Single.Parse(tokens[12], _format), Single.Parse(tokens[13], _format)); - vertices = new Vector3[] { v1, v2, v3 }; + Vector3d v1 = new Vector3d(float.Parse(tokens[1], _format), float.Parse(tokens[2], _format), float.Parse(tokens[3], _format)); + Vector3d v2 = new Vector3d(float.Parse(tokens[6], _format), float.Parse(tokens[7], _format), float.Parse(tokens[8], _format)); + Vector3d v3 = new Vector3d(float.Parse(tokens[11], _format), float.Parse(tokens[12], _format), float.Parse(tokens[13], _format)); + vertices = new Vector3d[] { v1, v2, v3 }; plane = new Plane(v1, v2, v3); texture = tokens[15]; // GearCraft if (tokens[16] == "[") { - textureS = new Vector3(Single.Parse(tokens[17], _format), Single.Parse(tokens[18], _format), Single.Parse(tokens[19], _format)); - textureShiftS = Double.Parse(tokens[20], _format); - textureT = new Vector3(Single.Parse(tokens[23], _format), Single.Parse(tokens[24], _format), Single.Parse(tokens[25], _format)); - textureShiftT = Double.Parse(tokens[26], _format); - texRot = Single.Parse(tokens[28], _format); - texScaleX = Double.Parse(tokens[29], _format); - texScaleY = Double.Parse(tokens[30], _format); - flags = Int32.Parse(tokens[31]); + textureInfo = new TextureInfo(new Vector3d(float.Parse(tokens[17], _format), float.Parse(tokens[18], _format), float.Parse(tokens[19], _format)), + new Vector3d(float.Parse(tokens[23], _format), float.Parse(tokens[24], _format), float.Parse(tokens[25], _format)), + new Vector2d(float.Parse(tokens[20], _format), float.Parse(tokens[26], _format)), + new Vector2d(float.Parse(tokens[29], _format), float.Parse(tokens[30], _format)), + int.Parse(tokens[31]), 0, double.Parse(tokens[28], _format)); material = tokens[32]; } else { // - textureShiftS = Single.Parse(tokens[16], _format); - textureShiftT = Single.Parse(tokens[17], _format); - texRot = Single.Parse(tokens[18], _format); - texScaleX = Double.Parse(tokens[19], _format); - texScaleY = Double.Parse(tokens[20], _format); - flags = Int32.Parse(tokens[22]); + Vector3d[] axes = TextureInfo.TextureAxisFromPlane(plane); + textureInfo = new TextureInfo(axes[0], + axes[1], + new Vector2d(float.Parse(tokens[16], _format), float.Parse(tokens[17], _format)), + new Vector2d(float.Parse(tokens[19], _format), float.Parse(tokens[20], _format)), + int.Parse(tokens[22]), 0, double.Parse(tokens[18], _format)); } } } else { bool inDispInfo = false; int braceCount = 0; + textureInfo = new TextureInfo(); List child = new List(37); foreach (string line in lines) { if (line == "{") { @@ -116,32 +112,32 @@ namespace LibBSP { case "plane": { string[] points = tokens[1].SplitUnlessBetweenDelimiters(' ', '(', ')', StringSplitOptions.RemoveEmptyEntries); string[] components = points[0].Split(new char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries); - Vector3 v1 = new Vector3(Single.Parse(components[0], _format), Single.Parse(components[1], _format), Single.Parse(components[2], _format)); + Vector3d v1 = new Vector3d(float.Parse(components[0], _format), float.Parse(components[1], _format), float.Parse(components[2], _format)); components = points[1].Split(new char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries); - Vector3 v2 = new Vector3(Single.Parse(components[0], _format), Single.Parse(components[1], _format), Single.Parse(components[2], _format)); + Vector3d v2 = new Vector3d(float.Parse(components[0], _format), float.Parse(components[1], _format), float.Parse(components[2], _format)); components = points[2].Split(new char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries); - Vector3 v3 = new Vector3(Single.Parse(components[0], _format), Single.Parse(components[1], _format), Single.Parse(components[2], _format)); + Vector3d v3 = new Vector3d(float.Parse(components[0], _format), float.Parse(components[1], _format), float.Parse(components[2], _format)); plane = new Plane(v1, v2, v3); break; } case "uaxis": { string[] split = tokens[1].SplitUnlessBetweenDelimiters(' ', '[', ']', StringSplitOptions.RemoveEmptyEntries); - texScaleX = Single.Parse(split[1], _format); + textureInfo.scale = new Vector2d(float.Parse(split[1], _format), textureInfo.scale.y); split = split[0].Split(new char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries); - textureS = new Vector3(Single.Parse(split[0], _format), Single.Parse(split[1], _format), Single.Parse(split[2], _format)); - textureShiftS = Single.Parse(split[3], _format); + textureInfo.axes[0] = new Vector3d(float.Parse(split[0], _format), float.Parse(split[1], _format), float.Parse(split[2], _format)); + textureInfo.translation = new Vector2d(float.Parse(split[3], _format), textureInfo.translation.y); break; } case "vaxis": { string[] split = tokens[1].SplitUnlessBetweenDelimiters(' ', '[', ']', StringSplitOptions.RemoveEmptyEntries); - texScaleY = Single.Parse(split[1], _format); + textureInfo.scale = new Vector2d(textureInfo.scale.x, float.Parse(split[1], _format)); split = split[0].Split(new char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries); - textureT = new Vector3(Single.Parse(split[0], _format), Single.Parse(split[1], _format), Single.Parse(split[2], _format)); - textureShiftT = Single.Parse(split[3], _format); + textureInfo.axes[1] = new Vector3d(float.Parse(split[0], _format), float.Parse(split[1], _format), float.Parse(split[2], _format)); + textureInfo.translation = new Vector2d(textureInfo.translation.x, float.Parse(split[3], _format)); break; } case "rotation": { - texRot = Single.Parse(tokens[1], _format); + textureInfo.rotation = double.Parse(tokens[1], _format); break; } } diff --git a/LibBSP/Source/Structs/MAP/MAPDisplacement.cs b/LibBSP/Source/Structs/MAP/MAPDisplacement.cs index 058d28f..86044c0 100644 --- a/LibBSP/Source/Structs/MAP/MAPDisplacement.cs +++ b/LibBSP/Source/Structs/MAP/MAPDisplacement.cs @@ -1,19 +1,19 @@ -#if (UNITY_2_6 || UNITY_2_6_1 || UNITY_3_0 || UNITY_3_0_0 || UNITY_3_1 || UNITY_3_2 || UNITY_3_3 || UNITY_3_4 || UNITY_3_5 || UNITY_4_0 || UNITY_4_0_1 || UNITY_4_2 || UNITY_4_3 || UNITY_4_5 || UNITY_4_6 || UNITY_5 || UNITY_5_3_OR_NEWER) +#if UNITY_2_6 || UNITY_2_6_1 || UNITY_3_0 || UNITY_3_0_0 || UNITY_3_1 || UNITY_3_2 || UNITY_3_3 || UNITY_3_4 || UNITY_3_5 || UNITY_4_0 || UNITY_4_0_1 || UNITY_4_2 || UNITY_4_3 || UNITY_4_5 || UNITY_4_6 || UNITY_5 || UNITY_5_3_OR_NEWER #define UNITY #endif using System; using System.Collections.Generic; -using System.Linq; using System.Globalization; #if UNITY using UnityEngine; #endif namespace LibBSP { -#if !UNITY - using Vector3 = Vector3d; +#if UNITY + using Vector3d = Vector3; #endif + /// /// Class containing all data necessary to render a displacement from Source engine. /// @@ -22,10 +22,10 @@ namespace LibBSP { private static IFormatProvider _format = CultureInfo.CreateSpecificCulture("en-US"); public int power; - public Vector3 start; - public Vector3[][] normals; - public float[][] distances; - public float[][] alphas; + public Vector3d start; + public Vector3d[,] normals; + public float[,] distances; + public float[,] alphas; /// /// Creates a new empty object. Internal data will have to be set manually. @@ -71,38 +71,33 @@ namespace LibBSP { string[] tokens = line.SplitUnlessInContainer(' ', '\"', StringSplitOptions.RemoveEmptyEntries); switch (tokens[0]) { case "power": { - power = Int32.Parse(tokens[1]); + power = int.Parse(tokens[1]); int side = (int)Math.Pow(2, power) + 1; - normals = new Vector3[side][]; - distances = new float[side][]; - alphas = new float[side][]; - for (int i = 0; i < side; ++i) { - normals[i] = new Vector3[side]; - distances[i] = new float[side]; - alphas[i] = new float[side]; - } + normals = new Vector3d[side, side]; + distances = new float[side, side]; + alphas = new float[side, side]; break; } case "startposition": { string[] point = tokens[1].Substring(1, tokens[1].Length - 2).Split(new char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries); - start = new Vector3(Single.Parse(point[0], _format), Single.Parse(point[1], _format), Single.Parse(point[2], _format)); + start = new Vector3d(float.Parse(point[0], _format), float.Parse(point[1], _format), float.Parse(point[2], _format)); break; } } } else if (braceCount > 1) { if (inNormals) { string[] tokens = line.SplitUnlessInContainer(' ', '\"', StringSplitOptions.RemoveEmptyEntries); - int row = Int32.Parse(tokens[0].Substring(3)); + int row = int.Parse(tokens[0].Substring(3)); string[] points = tokens[1].Split(new char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries); normalsTokens[row] = points; } else if (inDistances) { string[] tokens = line.SplitUnlessInContainer(' ', '\"', StringSplitOptions.RemoveEmptyEntries); - int row = Int32.Parse(tokens[0].Substring(3)); + int row = int.Parse(tokens[0].Substring(3)); string[] nums = tokens[1].Split(new char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries); distancesTokens[row] = nums; } else if (inAlphas) { string[] tokens = line.SplitUnlessInContainer(' ', '\"', StringSplitOptions.RemoveEmptyEntries); - int row = Int32.Parse(tokens[0].Substring(3)); + int row = int.Parse(tokens[0].Substring(3)); string[] nums = tokens[1].Split(new char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries); alphasTokens[row] = nums; } @@ -113,18 +108,17 @@ namespace LibBSP { throw new ArgumentException("Bad data given to MAPDisplacement, no power specified!"); } - if (start.x == Single.NaN) { + if (start.x == float.NaN) { throw new ArgumentException("Bad data given to MAPDisplacement, no starting point specified!"); } foreach (int i in normalsTokens.Keys) { for (int j = 0; j < normalsTokens[i].Length / 3; j++) { - normals[i][j] = new Vector3(Single.Parse(normalsTokens[i][j * 3], _format), Single.Parse(normalsTokens[i][(j * 3) + 1], _format), Single.Parse(normalsTokens[i][(j * 3) + 2], _format)); - distances[i][j] = Single.Parse(distancesTokens[i][j], _format); - alphas[i][j] = Single.Parse(alphasTokens[i][j], _format); + normals[i, j] = new Vector3d(float.Parse(normalsTokens[i][j * 3], _format), float.Parse(normalsTokens[i][(j * 3) + 1], _format), float.Parse(normalsTokens[i][(j * 3) + 2], _format)); + distances[i, j] = float.Parse(distancesTokens[i][j], _format); + alphas[i, j] = float.Parse(alphasTokens[i][j], _format); } } - } } diff --git a/LibBSP/Source/Structs/MAP/MAPPatch.cs b/LibBSP/Source/Structs/MAP/MAPPatch.cs index 47f5639..4b46234 100644 --- a/LibBSP/Source/Structs/MAP/MAPPatch.cs +++ b/LibBSP/Source/Structs/MAP/MAPPatch.cs @@ -1,21 +1,26 @@ -#if (UNITY_2_6 || UNITY_2_6_1 || UNITY_3_0 || UNITY_3_0_0 || UNITY_3_1 || UNITY_3_2 || UNITY_3_3 || UNITY_3_4 || UNITY_3_5 || UNITY_4_0 || UNITY_4_0_1 || UNITY_4_2 || UNITY_4_3 || UNITY_4_5 || UNITY_4_6 || UNITY_5 || UNITY_5_3_OR_NEWER) +#if UNITY_2_6 || UNITY_2_6_1 || UNITY_3_0 || UNITY_3_0_0 || UNITY_3_1 || UNITY_3_2 || UNITY_3_3 || UNITY_3_4 || UNITY_3_5 || UNITY_4_0 || UNITY_4_0_1 || UNITY_4_2 || UNITY_4_3 || UNITY_4_5 || UNITY_4_6 || UNITY_5 || UNITY_5_3_OR_NEWER #define UNITY +#if !(UNITY_4_6 || UNITY_5 || UNITY_5_3_OR_NEWER) +#define OLDUNITY #endif +#endif + using System; using System.Collections.Generic; using System.Globalization; -#if UNITY -using UnityEngine; -#else -using System.Drawing; -#endif namespace LibBSP { -#if !UNITY - using Vector2 = Vector2d; - using Vector3 = Vector3d; - using Color32 = Color; +#if UNITY + using Vector2d = UnityEngine.Vector2; + using Vector3d = UnityEngine.Vector3; + using Color = UnityEngine.Color32; +#if !OLDUNITY + using Vertex = UnityEngine.UIVertex; #endif +#else + using Color = System.Drawing.Color; +#endif + /// /// Class containing all data necessary to render a Bezier patch. /// @@ -23,8 +28,8 @@ namespace LibBSP { private static IFormatProvider _format = CultureInfo.CreateSpecificCulture("en-US"); - public UIVertex[] points; - public Vector2 dims; + public Vertex[] points; + public Vector2d dims; public string texture; /// @@ -39,21 +44,22 @@ namespace LibBSP { public MAPPatch(string[] lines) { texture = lines[2]; - List vertices = new List(9); + List vertices = new List(9); switch (lines[0]) { case "patchDef3": case "patchDef2": { string[] line = lines[3].Split(new char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries); - dims = new Vector2(Single.Parse(line[1], _format), Single.Parse(line[2], _format)); + dims = new Vector2d(float.Parse(line[1], _format), float.Parse(line[2], _format)); for (int i = 0; i < dims.x; ++i) { line = lines[i + 5].Split(new char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries); for (int j = 0; j < dims.y; ++j) { - Vector3 point = new Vector3(Single.Parse(line[2 + (j * 7)], _format), Single.Parse(line[3 + (j * 7)], _format), Single.Parse(line[4 + (j * 7)], _format)); - Vector2 uv = new Vector2(Single.Parse(line[5 + (j * 7)], _format), Single.Parse(line[6 + (j * 7)], _format)); - UIVertex vertex = new UIVertex() { + Vector3d point = new Vector3d(float.Parse(line[2 + (j * 7)], _format), float.Parse(line[3 + (j * 7)], _format), float.Parse(line[4 + (j * 7)], _format)); + Vector2d uv = new Vector2d(float.Parse(line[5 + (j * 7)], _format), float.Parse(line[6 + (j * 7)], _format)); + Vertex vertex = new Vertex() { position = point, - uv0 = uv + uv0 = uv, + color = ColorExtensions.FromArgb(255, 255, 255, 255), }; vertices.Add(vertex); } @@ -62,17 +68,17 @@ namespace LibBSP { } case "patchTerrainDef3": { string[] line = lines[3].Split(new char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries); - dims = new Vector2(Single.Parse(line[1], _format), Single.Parse(line[2], _format)); + dims = new Vector2d(float.Parse(line[1], _format), float.Parse(line[2], _format)); for (int i = 0; i < dims.x; ++i) { line = lines[i + 5].Split(new char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries); for (int j = 0; j < dims.y; ++j) { - Vector3 point = new Vector3(Single.Parse(line[2 + (j * 12)], _format), Single.Parse(line[3 + (j * 12)], _format), Single.Parse(line[4 + (j * 12)], _format)); - Vector2 uv = new Vector2(Single.Parse(line[5 + (j * 12)], _format), Single.Parse(line[6 + (j * 12)], _format)); - Color32 color = Color32Extensions.FromArgb(Byte.Parse(line[7 + (j * 12)]), Byte.Parse(line[8 + (j * 12)]), Byte.Parse(line[9 + (j * 12)]), Byte.Parse(line[10 + (j * 12)])); - UIVertex vertex = new UIVertex() { + Vector3d point = new Vector3d(float.Parse(line[2 + (j * 12)], _format), float.Parse(line[3 + (j * 12)], _format), float.Parse(line[4 + (j * 12)], _format)); + Vector2d uv = new Vector2d(float.Parse(line[5 + (j * 12)], _format), float.Parse(line[6 + (j * 12)], _format)); + Color color = ColorExtensions.FromArgb(byte.Parse(line[7 + (j * 12)]), byte.Parse(line[8 + (j * 12)]), byte.Parse(line[9 + (j * 12)]), byte.Parse(line[10 + (j * 12)])); + Vertex vertex = new Vertex() { position = point, uv0 = uv, - color = color + color = color, }; vertices.Add(vertex); } @@ -83,7 +89,6 @@ namespace LibBSP { throw new ArgumentException(string.Format("Unknown patch type {0}! Call a scientist! ", lines[0])); } } - } } diff --git a/LibBSP/Source/Structs/MAP/MAPTerrainEF2.cs b/LibBSP/Source/Structs/MAP/MAPTerrainEF2.cs index 79174da..a9cfb44 100644 --- a/LibBSP/Source/Structs/MAP/MAPTerrainEF2.cs +++ b/LibBSP/Source/Structs/MAP/MAPTerrainEF2.cs @@ -1,20 +1,19 @@ -#if (UNITY_2_6 || UNITY_2_6_1 || UNITY_3_0 || UNITY_3_0_0 || UNITY_3_1 || UNITY_3_2 || UNITY_3_3 || UNITY_3_4 || UNITY_3_5 || UNITY_4_0 || UNITY_4_0_1 || UNITY_4_2 || UNITY_4_3 || UNITY_4_5 || UNITY_4_6 || UNITY_5 || UNITY_5_3_OR_NEWER) +#if UNITY_2_6 || UNITY_2_6_1 || UNITY_3_0 || UNITY_3_0_0 || UNITY_3_1 || UNITY_3_2 || UNITY_3_3 || UNITY_3_4 || UNITY_3_5 || UNITY_4_0 || UNITY_4_0_1 || UNITY_4_2 || UNITY_4_3 || UNITY_4_5 || UNITY_4_6 || UNITY_5 || UNITY_5_3_OR_NEWER #define UNITY #endif + using System; -using System.Collections.Generic; using System.Globalization; #if UNITY using UnityEngine; -#else -using System.Drawing; #endif namespace LibBSP { -#if !UNITY - using Vector4 = Vector4d; - using Vector3 = Vector3d; +#if UNITY + using Vector4d = Vector4; + using Vector3d = Vector3; #endif + /// /// Class containing all data necessary to render a Terrain from Star Trek EF2. /// @@ -31,11 +30,11 @@ namespace LibBSP { public double texScaleY; public int flags; public double sideLength; - public Vector3 start; - public Vector4 IF; - public Vector4 LF; - public float[][] heightMap; - public float[][] alphaMap; + public Vector3d start; + public Vector4d IF; + public Vector4d LF; + public float[,] heightMap; + public float[,] alphaMap; /// /// Creates a new empty object. Internal data will have to be set manually. @@ -57,25 +56,25 @@ namespace LibBSP { switch (line[0]) { case "TEX(": { texture = line[1]; - textureShiftS = Single.Parse(line[2], _format); - textureShiftT = Single.Parse(line[3], _format); - texRot = Single.Parse(line[4], _format); - texScaleX = Double.Parse(line[5], _format); - texScaleY = Double.Parse(line[6], _format); - flags = Int32.Parse(line[8]); + textureShiftS = float.Parse(line[2], _format); + textureShiftT = float.Parse(line[3], _format); + texRot = float.Parse(line[4], _format); + texScaleX = double.Parse(line[5], _format); + texScaleY = double.Parse(line[6], _format); + flags = int.Parse(line[8]); break; } case "TD(": { - sideLength = Int32.Parse(line[1], _format); - start = new Vector3(Single.Parse(line[2], _format), Single.Parse(line[3], _format), Single.Parse(line[4], _format)); + sideLength = int.Parse(line[1], _format); + start = new Vector3d(float.Parse(line[2], _format), float.Parse(line[3], _format), float.Parse(line[4], _format)); break; } case "IF(": { - IF = new Vector4(Single.Parse(line[1], _format), Single.Parse(line[2], _format), Single.Parse(line[3], _format), Single.Parse(line[4], _format)); + IF = new Vector4d(float.Parse(line[1], _format), float.Parse(line[2], _format), float.Parse(line[3], _format), float.Parse(line[4], _format)); break; } case "LF(": { - LF = new Vector4(Single.Parse(line[1], _format), Single.Parse(line[2], _format), Single.Parse(line[3], _format), Single.Parse(line[4], _format)); + LF = new Vector4d(float.Parse(line[1], _format), float.Parse(line[2], _format), float.Parse(line[3], _format), float.Parse(line[4], _format)); break; } case "V(": { @@ -84,11 +83,10 @@ namespace LibBSP { if (side == 0) { side = line.Length; } - heightMap = new float[side][]; + heightMap = new float[side, side]; for (int j = 0; j < side; ++j) { - heightMap[j] = new float[side]; for (int k = 0; k < side; ++k) { - heightMap[j][k] = Single.Parse(line[k], _format); + heightMap[j, k] = float.Parse(line[k], _format); } ++i; line = lines[i].Split(new char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries); @@ -101,11 +99,10 @@ namespace LibBSP { if (side == 0) { side = line.Length; } - alphaMap = new float[side][]; + alphaMap = new float[side, side]; for (int j = 0; j < side; ++j) { - alphaMap[j] = new float[side]; for (int k = 0; k < side; ++k) { - alphaMap[j][k] = Single.Parse(line[k], _format); + alphaMap[j, k] = float.Parse(line[k], _format); } ++i; line = lines[i].Split(new char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries); diff --git a/LibBSP/Source/Structs/MAP/MAPTerrainMoHAA.cs b/LibBSP/Source/Structs/MAP/MAPTerrainMoHAA.cs index fd047c0..e88bb4a 100644 --- a/LibBSP/Source/Structs/MAP/MAPTerrainMoHAA.cs +++ b/LibBSP/Source/Structs/MAP/MAPTerrainMoHAA.cs @@ -1,26 +1,27 @@ -#if (UNITY_2_6 || UNITY_2_6_1 || UNITY_3_0 || UNITY_3_0_0 || UNITY_3_1 || UNITY_3_2 || UNITY_3_3 || UNITY_3_4 || UNITY_3_5 || UNITY_4_0 || UNITY_4_0_1 || UNITY_4_2 || UNITY_4_3 || UNITY_4_5 || UNITY_4_6 || UNITY_5 || UNITY_5_3_OR_NEWER) +#if UNITY_2_6 || UNITY_2_6_1 || UNITY_3_0 || UNITY_3_0_0 || UNITY_3_1 || UNITY_3_2 || UNITY_3_3 || UNITY_3_4 || UNITY_3_5 || UNITY_4_0 || UNITY_4_0_1 || UNITY_4_2 || UNITY_4_3 || UNITY_4_5 || UNITY_4_6 || UNITY_5 || UNITY_5_3_OR_NEWER #define UNITY #endif + using System; using System.Collections.Generic; -using System.Text; -using System.Globalization; +#if UNITY +using UnityEngine; +#endif namespace LibBSP { -#if !UNITY - using Vector2 = Vector2d; - using Vector3 = Vector3d; +#if UNITY + using Vector2d = Vector2; + using Vector3d = Vector3; #endif + /// /// Class containing all data necessary to render a Terrain from MoHAA. /// [Serializable] public class MAPTerrainMoHAA { - private static IFormatProvider _format = CultureInfo.CreateSpecificCulture("en-US"); - - public Vector2 size; + public Vector2d size; public int flags; - public Vector3 origin; + public Vector3d origin; public List partitions; public List vertices; @@ -40,6 +41,9 @@ namespace LibBSP { // TODO: Constructor to parse text } + /// + /// Class containing the data for a partition of a . + /// [Serializable] public class Partition { public int unknown1; public int unknown2; @@ -68,6 +72,9 @@ namespace LibBSP { } } + /// + /// Class containing the data for a vertex of a . + /// [Serializable] public class Vertex { public int height; public string unknown1; @@ -79,5 +86,6 @@ namespace LibBSP { unknown2 = ""; } } + } } diff --git a/LibBSP/Source/Util/BSPReader.cs b/LibBSP/Source/Util/BSPReader.cs index 58ce277..06d279e 100644 --- a/LibBSP/Source/Util/BSPReader.cs +++ b/LibBSP/Source/Util/BSPReader.cs @@ -4,10 +4,12 @@ using System.IO; using System.Collections.Generic; namespace LibBSP { + /// /// Handles reading of a BSP file on-demand. /// public class BSPReader { + private FileInfo bspFile; private Dictionary lumpFiles = null; @@ -31,7 +33,7 @@ namespace LibBSP { if (!File.Exists(file.FullName)) { throw new FileNotFoundException("Unable to open BSP file; file " + file.FullName + " not found."); } else { - this.bspFile = file; + bspFile = file; } } @@ -232,8 +234,8 @@ namespace LibBSP { int startIndex = bspFile.Name.Length - 1; int f1EndIndex = f1.Name.LastIndexOf('.'); int f2EndIndex = f2.Name.LastIndexOf('.'); - int f1Position = Int32.Parse(f1.Name.Substring(startIndex, f1EndIndex - startIndex)); - int f2Position = Int32.Parse(f2.Name.Substring(startIndex, f2EndIndex - startIndex)); + int f1Position = int.Parse(f1.Name.Substring(startIndex, f1EndIndex - startIndex)); + int f2Position = int.Parse(f2.Name.Substring(startIndex, f2EndIndex - startIndex)); return f1Position - f2Position; }); // Read the files in order. The last file in the list for a specific lump will replace that lump.