Skip to content
0.2.0-dev Download ZIP
130 lines ·6.0 KB Raw
1using System.Globalization;
2using System.Reflection;
3using System.Text;
4using System.Text.RegularExpressions;
5using EvilEngine.Conversion;
6using EvilEngine.Formats;
7using EvilEngine.Formats.Records;
8
9namespace EvilEngine.Host.Tests;
10
11public sealed class GarrysModPlayermodelTests(ITestOutputHelper output)
12{
13 private static readonly string Installed = typeof(GarrysModPlayermodelTests).Assembly
14 .GetCustomAttributes<AssemblyMetadataAttribute>()
15 .Single(attribute => attribute.Key == "AdapterOutputRoot").Value!;
16
17 [Fact]
18 public void ListsGarrysModsPlayermodelsFromEveryMountedAutorunFile()
19 {
20 var gmod = Environment.GetEnvironmentVariable("EVIL_GMOD");
21 Assert.SkipWhen(gmod is null, "Set EVIL_GMOD to a Garry's Mod install to list its playermodels.");
22 var host = new EngineHost();
23 host.Adapters.LoadAll([Installed]);
24 var mount = host.Mounts.Open(Assert.Single(host.Mounts.Probe(gmod!)));
25 host.Conversion.AddNamespace(ContentNamespace.For(mount));
26
27 var registry = LocalRegistry.Build([mount], host.Conversion);
28
29 foreach (var problem in registry.Problems)
30 output.WriteLine($"{problem.Subject}: {problem.Reason}");
31 Assert.DoesNotContain(registry.Problems,
32 problem => problem.Subject is not ("gmod:records/sounds" or "gmod:records/soundscapes"));
33 var people = registry.Playermodels().ToArray();
34 output.WriteLine($"{people.Length} playermodels");
35 Assert.Equal(87, people.Length);
36 Assert.Equal(new PersonRecord(ContentId.Parse("gmod:kleiner"),
37 new ModelRef(ContentId.Parse("gmod:models/player/kleiner")))
38 {
39 Hands = new ModelRef(ContentId.Parse("gmod:models/weapons/c_arms_citizen")) { Body = "0000000" },
40 Motion = Motion(ContentId.Parse("gmod:models/player/kleiner")),
41 Player = new PlayerTable { Title = "#npc_kleiner", Category = "Half-Life 2", Fallback = true }
42 }, registry.Person(ContentId.Parse("gmod:kleiner")));
43 }
44
45 [Fact]
46 public void MatchesWhatGarrysModsOwnPlayerManagerHolds()
47 {
48 var gmod = Environment.GetEnvironmentVariable("EVIL_GMOD");
49 var dump = Environment.GetEnvironmentVariable("EVIL_GMOD_PLAYERMODELS");
50 Assert.SkipWhen(gmod is null || dump is null,
51 "Set EVIL_GMOD to a Garry's Mod install and EVIL_GMOD_PLAYERMODELS to its zz_evil_playermodel_oracle.lua dump.");
52 var host = new EngineHost();
53 host.Adapters.LoadAll([Installed]);
54 var mount = host.Mounts.Open(Assert.Single(host.Mounts.Probe(gmod!)));
55 host.Conversion.AddNamespace(ContentNamespace.For(mount));
56 var registry = LocalRegistry.Build([mount], host.Conversion);
57 var ours = registry.Playermodels().ToDictionary(person => person.Id);
58 var differences = new List<string>();
59 var dumped = 0;
60
61 foreach (var line in File.ReadLines(dump!, Encoding.UTF8).Where(line => line.Length > 0))
62 {
63 var fields = line.Split('\t').Select(Value).ToArray();
64 Assert.Equal(9, fields.Length);
65 if (fields[0] is not string name || fields[1] is not string model)
66 {
67 differences.Add($"Garry's Mod holds an entry the records can't: {line}");
68 continue;
69 }
70
71 dumped++;
72 var id = ContentId.Create("gmod", name);
73 var expected = new PersonRecord(id, new ModelRef(Model(model)))
74 {
75 Hands = new ModelRef(Model((string)fields[5]!))
76 {
77 Skin = Convert.ToInt32(fields[6], CultureInfo.InvariantCulture),
78 Body = (string?)fields[7]
79 },
80 Motion = Motion(Model(model)),
81 Player = new PlayerTable
82 {
83 Title = (string?)fields[2], Category = (string?)fields[3],
84 Fallback = string.Equals(model, "models/player/kleiner.mdl", StringComparison.OrdinalIgnoreCase)
85 }
86 };
87 if (Equals(fields[8], true))
88 differences.Add($"{name}'s hands match the body's skin, which a person can't say yet");
89 if (!ours.Remove(id, out var person))
90 differences.Add($"{name} is missing: {expected}");
91 else if (person != expected)
92 differences.Add($"{name} differs:\n ours {person}\n theirs {expected}");
93 }
94
95 differences.AddRange(ours.Keys.Select(id => $"{id} isn't in Garry's Mod's list"));
96 output.WriteLine($"{dumped} playermodels in the dump");
97 Assert.True(differences.Count == 0, string.Join('\n', differences));
98 }
99
100 private static Dictionary<string, ContentId> Motion(ContentId body)
101 {
102 return new Dictionary<string, string>
103 {
104 ["idle"] = "act_hl2mp_idle", ["walk"] = "act_hl2mp_walk", ["run"] = "act_hl2mp_run",
105 ["crouch.idle"] = "act_hl2mp_idle_crouch", ["crouch.walk"] = "act_hl2mp_walk_crouch",
106 ["jump"] = "act_hl2mp_jump_slam", ["swim"] = "act_hl2mp_swim", ["gesture.land"] = "act_land",
107 ["gesture.noclip"] = "act_gmod_noclip_layer"
108 }.ToDictionary(pair => pair.Key, pair => ContentId.Create(body.Namespace, $"{body.Name}/anim/@{pair.Value}"));
109 }
110
111 private static ContentId Model(string path)
112 {
113 return ContentId.Create("gmod", path.EndsWith(".mdl", StringComparison.OrdinalIgnoreCase) ? path[..^4] : path);
114 }
115
116 private static object? Value(string field)
117 {
118 var text = Regex.Replace(field.Length > 2 ? field[2..] : "", @"\\(\d{3})",
119 match => ((char)int.Parse(match.Groups[1].Value, CultureInfo.InvariantCulture)).ToString());
120 return field switch
121 {
122 "nil" => null,
123 _ when field.StartsWith("s:", StringComparison.Ordinal) => text,
124 _ when field.StartsWith("n:", StringComparison.Ordinal) => double.Parse(text, CultureInfo.InvariantCulture),
125 "boolean:true" => true,
126 "boolean:false" => false,
127 _ => field
128 };
129 }
130}