using System; using System.Collections.Generic; using System.Linq; using System.Threading.Tasks; using Content.IntegrationTests; using Robust.Shared.Prototypes; using Robust.Shared.Serialization.Markdown.Validation; using Robust.Shared.Timing; using Robust.Shared.Utility; using Robust.UnitTesting; namespace Content.YAMLLinter { internal static class Program { private static async Task Main(string[] _) { var stopwatch = new Stopwatch(); stopwatch.Start(); var (errors, fieldErrors) = await RunValidation(); var count = errors.Count + fieldErrors.Count; if (count == 0) { Console.WriteLine($"No errors found in {(int) stopwatch.Elapsed.TotalMilliseconds} ms."); return 0; } foreach (var (file, errorHashset) in errors) { foreach (var errorNode in errorHashset) { Console.WriteLine($"::error file={file},line={errorNode.Node.Start.Line},col={errorNode.Node.Start.Column}::{file}({errorNode.Node.Start.Line},{errorNode.Node.Start.Column}) {errorNode.ErrorReason}"); } } foreach (var error in fieldErrors) { Console.WriteLine(error); } Console.WriteLine($"{count} errors found in {(int) stopwatch.Elapsed.TotalMilliseconds} ms."); return -1; } private static async Task<(Dictionary> YamlErrors, List FieldErrors)> ValidateClient() { await using var pairTracker = await PoolManager.GetServerClient(new PoolSettings { DummyTicker = true, Disconnected = true }); var client = pairTracker.Pair.Client; var result = await ValidateInstance(client); await pairTracker.CleanReturnAsync(); return result; } private static async Task<(Dictionary> YamlErrors, List FieldErrors)> ValidateServer() { await using var pairTracker = await PoolManager.GetServerClient(new PoolSettings { DummyTicker = true, NoClient = true }); var server = pairTracker.Pair.Server; var result = await ValidateInstance(server); await pairTracker.CleanReturnAsync(); return result; } private static async Task<(Dictionary>, List)> ValidateInstance( RobustIntegrationTest.IntegrationInstance instance) { var protoMan = instance.ResolveDependency(); Dictionary> yamlErrors = default!; List fieldErrors = default!; await instance.WaitPost(() => { yamlErrors = protoMan.ValidateDirectory(new ResPath("/Prototypes"), out var prototypes); fieldErrors = protoMan.ValidateFields(prototypes); }); return (yamlErrors, fieldErrors); } public static async Task<(Dictionary> YamlErrors , List FieldErrors)> RunValidation() { var yamlErrors = new Dictionary>(); var serverErrors = await ValidateServer(); var clientErrors = await ValidateClient(); foreach (var (key, val) in serverErrors.YamlErrors) { // Include all server errors marked as always relevant var newErrors = val.Where(n => n.AlwaysRelevant).ToHashSet(); // We include sometimes-relevant errors if they exist both for the client & server if (clientErrors.Item1.TryGetValue(key, out var clientVal)) newErrors.UnionWith(val.Intersect(clientVal)); if (newErrors.Count != 0) yamlErrors[key] = newErrors; } // Next add any always-relevant client errors. foreach (var (key, val) in clientErrors.YamlErrors) { var newErrors = val.Where(n => n.AlwaysRelevant).ToHashSet(); if (newErrors.Count == 0) continue; if (yamlErrors.TryGetValue(key, out var errors)) errors.UnionWith(val.Where(n => n.AlwaysRelevant)); else yamlErrors[key] = newErrors; } // Finally, combine the prototype ID field errors. var fieldErrors = serverErrors.FieldErrors .Concat(clientErrors.FieldErrors) .Distinct() .ToList(); return (yamlErrors, fieldErrors); } } }