using System.Globalization; using System.Reflection; using System.Text; using System.Text.RegularExpressions; using EvilEngine.Conversion; using EvilEngine.Formats; using EvilEngine.Formats.Records; namespace EvilEngine.Host.Tests; public sealed class GarrysModPlayermodelTests(ITestOutputHelper output) { private static readonly string Installed = typeof(GarrysModPlayermodelTests).Assembly .GetCustomAttributes() .Single(attribute => attribute.Key == "AdapterOutputRoot").Value!; [Fact] public void ListsGarrysModsPlayermodelsFromEveryMountedAutorunFile() { var gmod = Environment.GetEnvironmentVariable("EVIL_GMOD"); Assert.SkipWhen(gmod is null, "Set EVIL_GMOD to a Garry's Mod install to list its playermodels."); var host = new EngineHost(); host.Adapters.LoadAll([Installed]); var mount = host.Mounts.Open(Assert.Single(host.Mounts.Probe(gmod!))); host.Conversion.AddNamespace(ContentNamespace.For(mount)); var registry = LocalRegistry.Build([mount], host.Conversion); foreach (var problem in registry.Problems) output.WriteLine($"{problem.Subject}: {problem.Reason}"); Assert.DoesNotContain(registry.Problems, problem => problem.Subject is not ("gmod:records/sounds" or "gmod:records/soundscapes")); var people = registry.Playermodels().ToArray(); output.WriteLine($"{people.Length} playermodels"); Assert.Equal(87, people.Length); Assert.Equal(new PersonRecord(ContentId.Parse("gmod:kleiner"), new ModelRef(ContentId.Parse("gmod:models/player/kleiner"))) { Hands = new ModelRef(ContentId.Parse("gmod:models/weapons/c_arms_citizen")) { Body = "0000000" }, Motion = Motion(ContentId.Parse("gmod:models/player/kleiner")), Player = new PlayerTable { Title = "#npc_kleiner", Category = "Half-Life 2", Fallback = true } }, registry.Person(ContentId.Parse("gmod:kleiner"))); } [Fact] public void MatchesWhatGarrysModsOwnPlayerManagerHolds() { var gmod = Environment.GetEnvironmentVariable("EVIL_GMOD"); var dump = Environment.GetEnvironmentVariable("EVIL_GMOD_PLAYERMODELS"); Assert.SkipWhen(gmod is null || dump is null, "Set EVIL_GMOD to a Garry's Mod install and EVIL_GMOD_PLAYERMODELS to its zz_evil_playermodel_oracle.lua dump."); var host = new EngineHost(); host.Adapters.LoadAll([Installed]); var mount = host.Mounts.Open(Assert.Single(host.Mounts.Probe(gmod!))); host.Conversion.AddNamespace(ContentNamespace.For(mount)); var registry = LocalRegistry.Build([mount], host.Conversion); var ours = registry.Playermodels().ToDictionary(person => person.Id); var differences = new List(); var dumped = 0; foreach (var line in File.ReadLines(dump!, Encoding.UTF8).Where(line => line.Length > 0)) { var fields = line.Split('\t').Select(Value).ToArray(); Assert.Equal(9, fields.Length); if (fields[0] is not string name || fields[1] is not string model) { differences.Add($"Garry's Mod holds an entry the records can't: {line}"); continue; } dumped++; var id = ContentId.Create("gmod", name); var expected = new PersonRecord(id, new ModelRef(Model(model))) { Hands = new ModelRef(Model((string)fields[5]!)) { Skin = Convert.ToInt32(fields[6], CultureInfo.InvariantCulture), Body = (string?)fields[7] }, Motion = Motion(Model(model)), Player = new PlayerTable { Title = (string?)fields[2], Category = (string?)fields[3], Fallback = string.Equals(model, "models/player/kleiner.mdl", StringComparison.OrdinalIgnoreCase) } }; if (Equals(fields[8], true)) differences.Add($"{name}'s hands match the body's skin, which a person can't say yet"); if (!ours.Remove(id, out var person)) differences.Add($"{name} is missing: {expected}"); else if (person != expected) differences.Add($"{name} differs:\n ours {person}\n theirs {expected}"); } differences.AddRange(ours.Keys.Select(id => $"{id} isn't in Garry's Mod's list")); output.WriteLine($"{dumped} playermodels in the dump"); Assert.True(differences.Count == 0, string.Join('\n', differences)); } private static Dictionary Motion(ContentId body) { return new Dictionary { ["idle"] = "act_hl2mp_idle", ["walk"] = "act_hl2mp_walk", ["run"] = "act_hl2mp_run", ["crouch.idle"] = "act_hl2mp_idle_crouch", ["crouch.walk"] = "act_hl2mp_walk_crouch", ["jump"] = "act_hl2mp_jump_slam", ["swim"] = "act_hl2mp_swim", ["gesture.land"] = "act_land", ["gesture.noclip"] = "act_gmod_noclip_layer" }.ToDictionary(pair => pair.Key, pair => ContentId.Create(body.Namespace, $"{body.Name}/anim/@{pair.Value}")); } private static ContentId Model(string path) { return ContentId.Create("gmod", path.EndsWith(".mdl", StringComparison.OrdinalIgnoreCase) ? path[..^4] : path); } private static object? Value(string field) { var text = Regex.Replace(field.Length > 2 ? field[2..] : "", @"\\(\d{3})", match => ((char)int.Parse(match.Groups[1].Value, CultureInfo.InvariantCulture)).ToString()); return field switch { "nil" => null, _ when field.StartsWith("s:", StringComparison.Ordinal) => text, _ when field.StartsWith("n:", StringComparison.Ordinal) => double.Parse(text, CultureInfo.InvariantCulture), "boolean:true" => true, "boolean:false" => false, _ => field }; } }