0.2.0-dev Download ZIP
80 lines ·3.4 KB Raw
| 1 | using System.Numerics; |
| 2 | using System.Text; |
| 3 | using EvilEngine.Adapters.Source.Maps; |
| 4 | using EvilEngine.Adapters.Tests.Support; |
| 5 | using EvilEngine.Mounts; |
| 6 | |
| 7 | namespace EvilEngine.Adapters.Tests; |
| 8 | |
| 9 | public sealed class BspTests |
| 10 | { |
| 11 | [Fact] |
| 12 | public void ReadsWhatTheWriterWrote() |
| 13 | { |
| 14 | var bsp = BspFile.Read(BoxMap.Write(), "maps/box.bsp"); |
| 15 | |
| 16 | Assert.Equal((20, 7), (bsp.Version, bsp.Revision)); |
| 17 | Assert.Equal(BoxMap.Faces.Length, bsp.Faces.Count); |
| 18 | Assert.Equal(BoxMap.Floor, bsp.TexDataName(0)); |
| 19 | Assert.Equal(2, bsp.Faces[0].StyleCount); |
| 20 | Assert.Equal((9, 9), (bsp.Faces[0].LightmapWidth, bsp.Faces[0].LightmapHeight)); |
| 21 | Assert.Equal(BoxMap.Vertices[1], bsp.Vertices[bsp.FaceVertex(bsp.Faces[0], 1)]); |
| 22 | Assert.Equal(6, bsp.Entities.Count); |
| 23 | Assert.Equal(["a,Kill", "b,Kill"], |
| 24 | bsp.Entities[2].Properties.Where(pair => pair.Key == "OnUser1").Select(pair => pair.Value)); |
| 25 | Assert.Equal(new Vector3(100, 0, 0), bsp.Entities[2].Vector("origin")); |
| 26 | Assert.Equal(UnityFsTests.Text(), bsp.GameLump("zzzz", out var version).ToArray()); |
| 27 | Assert.Equal(1, version); |
| 28 | Assert.Equal(1, bsp.FindLeaf(new Vector3(0, 0, 1500))); |
| 29 | Assert.Equal([0, 1], bsp.ModelLeafs(0).Order()); |
| 30 | var props = BspStaticProps.Read(bsp); |
| 31 | Assert.Equal(2, props.Count); |
| 32 | Assert.Equal(("models/props_c17/oildrum001.mdl", new Vector3(0, 90, 0)), (props[0].Model, props[0].Angles)); |
| 33 | Assert.Equal([1], props[1].Leafs); |
| 34 | } |
| 35 | |
| 36 | [Fact] |
| 37 | public void ReadsAPakfileStoredDeflatedAndLzma() |
| 38 | { |
| 39 | var pakfile = BspFile.ReadPakfile(BoxMap.Write(), "maps/box.bsp"); |
| 40 | |
| 41 | Assert.Equal(["materials/maps/box/c0_0_0.vtf", $"materials/{BoxMap.Floor}.vmt", "scripts/notes.txt"], |
| 42 | pakfile.Paths.Select(path => path.Value)); |
| 43 | Assert.True(pakfile.TryRead(VirtualPath.Parse($"MATERIALS/{BoxMap.Floor}.vmt"), out var patch)); |
| 44 | Assert.Equal(BoxMap.Patch, Encoding.ASCII.GetString(patch.Span)); |
| 45 | Assert.True(pakfile.TryRead(VirtualPath.Parse("materials/maps/box/c0_0_0.vtf"), out var deflated)); |
| 46 | Assert.Equal("not really a vtf", Encoding.ASCII.GetString(deflated.Span)); |
| 47 | Assert.True(pakfile.TryRead(VirtualPath.Parse("scripts/notes.txt"), out var lzma)); |
| 48 | Assert.Equal(UnityFsTests.Text(), lzma.ToArray()); |
| 49 | } |
| 50 | |
| 51 | [Theory] |
| 52 | [InlineData(0, 0x42, "not VBSP")] |
| 53 | [InlineData(4, 21, "version 21")] |
| 54 | [InlineData(8 + BspLump.Faces * 16 + 4, 0x7F, "outside the file")] |
| 55 | public void RefusesBrokenBsps(int offset, byte value, string expected) |
| 56 | { |
| 57 | var bytes = BoxMap.Write(); |
| 58 | bytes[offset] = value; |
| 59 | if (offset > 4) |
| 60 | bytes[offset + 3] = value; |
| 61 | |
| 62 | var error = Assert.Throws<BspFormatException>(() => BspFile.Read(bytes, "maps/box.bsp")); |
| 63 | |
| 64 | Assert.Contains(expected, error.Message); |
| 65 | } |
| 66 | |
| 67 | [Fact] |
| 68 | public void RefusesFacesAndPropsThatPointOutside() |
| 69 | { |
| 70 | var bytes = BoxMap.Write(); |
| 71 | var faces = BitConverter.ToInt32(bytes, 8 + BspLump.Faces * 16); |
| 72 | BitConverter.GetBytes(999).CopyTo(bytes, faces + 4); |
| 73 | var bsp = BspFile.Read(BoxMap.Write(5), "maps/box.bsp"); |
| 74 | |
| 75 | Assert.Contains("face 0 first edge 999", |
| 76 | Assert.Throws<BspFormatException>(() => BspFile.Read(bytes, "maps/box.bsp")).Message); |
| 77 | Assert.Contains("has 128 bytes for 2 version 5 static props, not 120", |
| 78 | Assert.Throws<BspFormatException>(() => BspStaticProps.Read(bsp)).Message); |
| 79 | } |
| 80 | } |