0.2.0-dev Download ZIP
115 lines ·4.8 KB Raw
| 1 | using System.Reflection; |
| 2 | using System.Text; |
| 3 | using EvilEngine.Adapters.Tests.Support; |
| 4 | using EvilEngine.Mounts; |
| 5 | |
| 6 | namespace EvilEngine.Host.Tests; |
| 7 | |
| 8 | public sealed class GarrysModGamemodeTests : IDisposable |
| 9 | { |
| 10 | private static readonly string Installed = typeof(GarrysModGamemodeTests).Assembly |
| 11 | .GetCustomAttributes<AssemblyMetadataAttribute>() |
| 12 | .Single(attribute => attribute.Key == "AdapterOutputRoot").Value!; |
| 13 | |
| 14 | private readonly string _root = Path.Combine(Path.GetTempPath(), $"evil-gamemodes-{Guid.NewGuid():N}"); |
| 15 | |
| 16 | private string Install => Path.Combine(_root, "GarrysMod"); |
| 17 | |
| 18 | private string Garrysmod => Path.Combine(Install, "garrysmod"); |
| 19 | |
| 20 | public void Dispose() |
| 21 | { |
| 22 | if (Directory.Exists(_root)) |
| 23 | Directory.Delete(_root, true); |
| 24 | } |
| 25 | |
| 26 | private static void Write(string path, byte[] data) |
| 27 | { |
| 28 | Directory.CreateDirectory(Path.GetDirectoryName(path)!); |
| 29 | File.WriteAllBytes(path, data); |
| 30 | } |
| 31 | |
| 32 | private static void Write(string path, string text) |
| 33 | { |
| 34 | Write(path, Encoding.UTF8.GetBytes(text)); |
| 35 | } |
| 36 | |
| 37 | private void Library(string sandboxBase = "base") |
| 38 | { |
| 39 | Write(Path.Combine(Garrysmod, "gameinfo.txt"), "\"GameInfo\" {}"); |
| 40 | Write(Path.Combine(Garrysmod, "steam.inf"), "PatchVersion=2026.04.29\nProductName=garrysmod\nappID=4000\n"); |
| 41 | foreach (var (path, data) in new VpkWriter().Add("materials/game.vmt", "garrysmod.vpk") |
| 42 | .Build("garrysmod/garrysmod_dir.vpk")) |
| 43 | Write(Path.Combine(Install, path), data); |
| 44 | Write(Path.Combine(Garrysmod, "gamemodes", "sandbox", "sandbox.txt"), |
| 45 | $"\"sandbox\"\n{{\n\t\"base\"\t\"{sandboxBase}\"\n\t\"title\"\t\"Sandbox\"\n}}\n"); |
| 46 | Write(Path.Combine(Garrysmod, "gamemodes", "base", "base.txt"), |
| 47 | "\"base\"\n{\n\t\"title\"\t\"Base\"\n\t\"base\"\t\"\"\n}\n"); |
| 48 | Write(Path.Combine(Garrysmod, "gamemodes", "base", "content", "materials", "base.vmt"), "base on disk"); |
| 49 | Write(Path.Combine(Garrysmod, "gamemodes", "terrortown", "terrortown.txt"), |
| 50 | "\"terrortown\"\n{\n\t\"base\"\t\"base\"\n}\n"); |
| 51 | Write(Path.Combine(Garrysmod, "gamemodes", "terrortown", "content", "materials", "ttt.vmt"), "terrortown"); |
| 52 | Write(Path.Combine(Garrysmod, "addons", "legacy", "materials", "shared.vmt"), "legacy"); |
| 53 | Write(Path.Combine(Garrysmod, "addons", "fun.gma"), |
| 54 | new GmaWriter().Add("gamemodes/sandbox/content/materials/shared.vmt", "gamemode") |
| 55 | .Add("gamemodes/sandbox/content/materials/second.vmt", "gamemode") |
| 56 | .Add("gamemodes/fungus/fungus.txt", "\"fungus\"\n{\n\t\"base\"\t\"sandbox\"\n}\n") |
| 57 | .Add("gamemodes/fungus/content/materials/spores.vmt", "fungus") |
| 58 | .Add("materials/shared.vmt", "gma") |
| 59 | .Add("materials/second.vmt", "gma") |
| 60 | .Build()); |
| 61 | Write(Path.Combine(Garrysmod, "addons", "plain.gma"), |
| 62 | new GmaWriter().Add("lua/autorun/plain.lua", "plain").Build()); |
| 63 | } |
| 64 | |
| 65 | private Mount Open() |
| 66 | { |
| 67 | var host = new EngineHost(); |
| 68 | host.Adapters.LoadAll([Installed]); |
| 69 | return host.Mounts.Open(Assert.Single(host.Mounts.Probe(Install))); |
| 70 | } |
| 71 | |
| 72 | [Fact] |
| 73 | public void LayersTheStartingGamemodesContentAndItsBasesUnderTheLegacyAddonsAndOverTheGmas() |
| 74 | { |
| 75 | Library(); |
| 76 | var mount = Open(); |
| 77 | |
| 78 | Assert.Equal( |
| 79 | [ |
| 80 | "addons/legacy", "addons/fun.gma/gamemodes/sandbox/content", "gamemodes/base/content", "addons/fun.gma", |
| 81 | "addons/plain.gma", "garrysmod.vpk" |
| 82 | ], mount.Layers.Select(layer => layer.Name).Take(6)); |
| 83 | Assert.Equal(["gamemode", "gamemode"], mount.Layers.Skip(1).Take(2).Select(layer => layer.Role)); |
| 84 | Assert.Equal("legacy", mount.Files.ReadAllText("materials/shared.vmt")); |
| 85 | Assert.Equal("gamemode", mount.Files.ReadAllText("materials/second.vmt")); |
| 86 | Assert.Equal("base on disk", mount.Files.ReadAllText("materials/base.vmt")); |
| 87 | Assert.Equal("gamemodes/base/content", mount.Origin("materials/base.vmt")!.Layer.Name); |
| 88 | } |
| 89 | |
| 90 | [Fact] |
| 91 | public void LeavesOutEveryGamemodeOutsideTheStartingOnesChain() |
| 92 | { |
| 93 | Library(); |
| 94 | var mount = Open(); |
| 95 | |
| 96 | Assert.False(mount.Files.Exists("materials/ttt.vmt")); |
| 97 | Assert.False(mount.Files.Exists("materials/spores.vmt")); |
| 98 | Assert.True(mount.Files.Exists("gamemodes/terrortown/content/materials/ttt.vmt")); |
| 99 | } |
| 100 | |
| 101 | [Fact] |
| 102 | public void FollowsAGamemodesBaseThroughTheGmaThatDefinesIt() |
| 103 | { |
| 104 | Library("fungus"); |
| 105 | var mount = Open(); |
| 106 | |
| 107 | Assert.Equal( |
| 108 | [ |
| 109 | "addons/legacy", "addons/fun.gma/gamemodes/sandbox/content", "addons/fun.gma/gamemodes/fungus/content", |
| 110 | "addons/fun.gma" |
| 111 | ], mount.Layers.Select(layer => layer.Name).Take(4)); |
| 112 | Assert.Equal("fungus", mount.Files.ReadAllText("materials/spores.vmt")); |
| 113 | Assert.Empty(mount.Notes); |
| 114 | } |
| 115 | } |