using System.Numerics; using System.Text; using EvilEngine.Adapters.Source.Maps; using EvilEngine.Adapters.Tests.Support; using EvilEngine.Mounts; namespace EvilEngine.Adapters.Tests; public sealed class BspTests { [Fact] public void ReadsWhatTheWriterWrote() { var bsp = BspFile.Read(BoxMap.Write(), "maps/box.bsp"); Assert.Equal((20, 7), (bsp.Version, bsp.Revision)); Assert.Equal(BoxMap.Faces.Length, bsp.Faces.Count); Assert.Equal(BoxMap.Floor, bsp.TexDataName(0)); Assert.Equal(2, bsp.Faces[0].StyleCount); Assert.Equal((9, 9), (bsp.Faces[0].LightmapWidth, bsp.Faces[0].LightmapHeight)); Assert.Equal(BoxMap.Vertices[1], bsp.Vertices[bsp.FaceVertex(bsp.Faces[0], 1)]); Assert.Equal(6, bsp.Entities.Count); Assert.Equal(["a,Kill", "b,Kill"], bsp.Entities[2].Properties.Where(pair => pair.Key == "OnUser1").Select(pair => pair.Value)); Assert.Equal(new Vector3(100, 0, 0), bsp.Entities[2].Vector("origin")); Assert.Equal(UnityFsTests.Text(), bsp.GameLump("zzzz", out var version).ToArray()); Assert.Equal(1, version); Assert.Equal(1, bsp.FindLeaf(new Vector3(0, 0, 1500))); Assert.Equal([0, 1], bsp.ModelLeafs(0).Order()); var props = BspStaticProps.Read(bsp); Assert.Equal(2, props.Count); Assert.Equal(("models/props_c17/oildrum001.mdl", new Vector3(0, 90, 0)), (props[0].Model, props[0].Angles)); Assert.Equal([1], props[1].Leafs); } [Fact] public void ReadsAPakfileStoredDeflatedAndLzma() { var pakfile = BspFile.ReadPakfile(BoxMap.Write(), "maps/box.bsp"); Assert.Equal(["materials/maps/box/c0_0_0.vtf", $"materials/{BoxMap.Floor}.vmt", "scripts/notes.txt"], pakfile.Paths.Select(path => path.Value)); Assert.True(pakfile.TryRead(VirtualPath.Parse($"MATERIALS/{BoxMap.Floor}.vmt"), out var patch)); Assert.Equal(BoxMap.Patch, Encoding.ASCII.GetString(patch.Span)); Assert.True(pakfile.TryRead(VirtualPath.Parse("materials/maps/box/c0_0_0.vtf"), out var deflated)); Assert.Equal("not really a vtf", Encoding.ASCII.GetString(deflated.Span)); Assert.True(pakfile.TryRead(VirtualPath.Parse("scripts/notes.txt"), out var lzma)); Assert.Equal(UnityFsTests.Text(), lzma.ToArray()); } [Theory] [InlineData(0, 0x42, "not VBSP")] [InlineData(4, 21, "version 21")] [InlineData(8 + BspLump.Faces * 16 + 4, 0x7F, "outside the file")] public void RefusesBrokenBsps(int offset, byte value, string expected) { var bytes = BoxMap.Write(); bytes[offset] = value; if (offset > 4) bytes[offset + 3] = value; var error = Assert.Throws(() => BspFile.Read(bytes, "maps/box.bsp")); Assert.Contains(expected, error.Message); } [Fact] public void RefusesFacesAndPropsThatPointOutside() { var bytes = BoxMap.Write(); var faces = BitConverter.ToInt32(bytes, 8 + BspLump.Faces * 16); BitConverter.GetBytes(999).CopyTo(bytes, faces + 4); var bsp = BspFile.Read(BoxMap.Write(5), "maps/box.bsp"); Assert.Contains("face 0 first edge 999", Assert.Throws(() => BspFile.Read(bytes, "maps/box.bsp")).Message); Assert.Contains("has 128 bytes for 2 version 5 static props, not 120", Assert.Throws(() => BspStaticProps.Read(bsp)).Message); } }