0.2.0-dev Download ZIP
130 lines ·5.5 KB Raw
| 1 | using System.Globalization; |
| 2 | using System.Reflection; |
| 3 | using System.Text; |
| 4 | using EvilEngine.Conversion; |
| 5 | using EvilEngine.Formats; |
| 6 | using EvilEngine.Formats.Materials; |
| 7 | |
| 8 | namespace EvilEngine.Host.Tests; |
| 9 | |
| 10 | public sealed class GarrysModPlayerColourTests(ITestOutputHelper output) |
| 11 | { |
| 12 | private const double ReadBack = 0.006; |
| 13 | |
| 14 | private static readonly string Installed = typeof(GarrysModPlayerColourTests).Assembly |
| 15 | .GetCustomAttributes<AssemblyMetadataAttribute>() |
| 16 | .Single(attribute => attribute.Key == "AdapterOutputRoot").Value!; |
| 17 | |
| 18 | private static readonly MaterialValue Probe = new MaterialValue.Vector([0.9, 0.2, 0.45]); |
| 19 | |
| 20 | [Fact] |
| 21 | public void RunsEveryPlayermodelsColourDriversAsGarrysModDid() |
| 22 | { |
| 23 | var gmod = Environment.GetEnvironmentVariable("EVIL_GMOD"); |
| 24 | var dump = Environment.GetEnvironmentVariable("EVIL_GMOD_TINT"); |
| 25 | Assert.SkipWhen(gmod is null || dump is null, |
| 26 | "Set EVIL_GMOD to a Garry's Mod install and EVIL_GMOD_TINT to the vars.txt zz_evil_tint_oracle.lua wrote."); |
| 27 | var host = new EngineHost(); |
| 28 | host.Adapters.LoadAll([Installed]); |
| 29 | var mount = host.Mounts.Open(Assert.Single(host.Mounts.Probe(gmod!))); |
| 30 | host.Conversion.AddNamespace(ContentNamespace.For(mount)); |
| 31 | |
| 32 | var seen = new Dictionary<(string Tag, string Model, string Path), Dictionary<string, string>>(); |
| 33 | foreach (var line in File.ReadLines(dump!)) |
| 34 | { |
| 35 | var parts = line.Split('\t'); |
| 36 | if (parts.Length != 5) |
| 37 | continue; |
| 38 | var key = (parts[0], parts[1], parts[2]); |
| 39 | if (!seen.TryGetValue(key, out var values)) |
| 40 | seen[key] = values = new Dictionary<string, string>(StringComparer.Ordinal); |
| 41 | values[parts[3]] = parts[4]; |
| 42 | } |
| 43 | |
| 44 | var materials = new Dictionary<string, Material?>(StringComparer.Ordinal); |
| 45 | var compared = 0; |
| 46 | var skipped = 0; |
| 47 | var mismatches = new List<string>(); |
| 48 | foreach (var ((tag, model, path), values) in seen) |
| 49 | { |
| 50 | if (!materials.TryGetValue(path, out var material)) |
| 51 | { |
| 52 | var result = host.Conversion.Get(ContentKind.Material, |
| 53 | ContentId.Create("gmod", "materials/" + path.ToLowerInvariant())); |
| 54 | materials[path] = material = result.Content is { } content |
| 55 | ? MaterialText.Read(Encoding.UTF8.GetString(content.Data.Span), path) |
| 56 | : null; |
| 57 | } |
| 58 | |
| 59 | if (material is null || !material.Drivers.Any(driver => driver.Op == "input")) |
| 60 | { |
| 61 | skipped++; |
| 62 | continue; |
| 63 | } |
| 64 | |
| 65 | var ran = MaterialDriverRun.Run(material, |
| 66 | input => input == "player.colour" && tag == "player" ? Probe : null); |
| 67 | var other = seen.GetValueOrDefault((tag == "player" ? "none" : "player", model, path)); |
| 68 | if (other is not null && ran.Keys.All(name => |
| 69 | values.GetValueOrDefault("$" + name) == other.GetValueOrDefault("$" + name))) |
| 70 | { |
| 71 | skipped++; |
| 72 | continue; |
| 73 | } |
| 74 | |
| 75 | foreach (var (name, ours) in ran) |
| 76 | { |
| 77 | if (!values.TryGetValue("$" + name, out var theirs)) |
| 78 | { |
| 79 | mismatches.Add($"{tag} {model} {path} ${name}: Garry's Mod has no value, ours {Show(ours)}"); |
| 80 | continue; |
| 81 | } |
| 82 | |
| 83 | compared++; |
| 84 | if (!Matches(ours, theirs)) |
| 85 | mismatches.Add($"{tag} {model} {path} ${name}: Garry's Mod {theirs}, ours {Show(ours)}"); |
| 86 | } |
| 87 | } |
| 88 | |
| 89 | output.WriteLine($"{compared} driven values match over {seen.Count - skipped} drawn materials, " + |
| 90 | $"with {skipped} undriven or left undrawn by the model's body groups"); |
| 91 | foreach (var mismatch in mismatches.Take(100)) |
| 92 | output.WriteLine(mismatch); |
| 93 | Assert.NotEqual(0, compared); |
| 94 | Assert.Empty(mismatches); |
| 95 | } |
| 96 | |
| 97 | private static bool Matches(MaterialValue ours, string theirs) |
| 98 | { |
| 99 | var numbers = theirs[2..].Split(' ').Select(text => double.Parse(text, CultureInfo.InvariantCulture)).ToArray(); |
| 100 | return ours switch |
| 101 | { |
| 102 | MaterialValue.Vector vector when theirs[0] == 'v' => vector.Values.Count >= 3 && |
| 103 | vector.Values.Take(3).Select(Linear) |
| 104 | .Zip(numbers) |
| 105 | .All(pair => |
| 106 | Math.Abs(pair.First - pair.Second) <= |
| 107 | ReadBack), |
| 108 | MaterialValue.Number number when theirs[0] == 'n' => Math.Abs(number.Value - numbers[0]) <= 1e-5, |
| 109 | MaterialValue.Integer integer when theirs[0] == 'n' => integer.Value == numbers[0], |
| 110 | _ => false |
| 111 | }; |
| 112 | } |
| 113 | |
| 114 | private static double Linear(double value) |
| 115 | { |
| 116 | return Math.Pow(Math.Clamp(value, 0, 1), 2.2); |
| 117 | } |
| 118 | |
| 119 | private static string Show(MaterialValue value) |
| 120 | { |
| 121 | return value switch |
| 122 | { |
| 123 | MaterialValue.Vector vector => "v " + string.Join(' ', |
| 124 | vector.Values.Select(v => v.ToString("G6", CultureInfo.InvariantCulture))), |
| 125 | MaterialValue.Number number => "n " + number.Value.ToString("G6", CultureInfo.InvariantCulture), |
| 126 | MaterialValue.Integer integer => "n " + integer.Value, |
| 127 | _ => value.ToString() |
| 128 | }; |
| 129 | } |
| 130 | } |