forked from SpaceStation14-Shenanigans/Monolith
a75a27b456
Co-authored-by: Moony <moony@hellomouse.net> Co-authored-by: J <billsmith116@gmail.com> Co-authored-by: metalgearsloth <31366439+metalgearsloth@users.noreply.github.com> Co-authored-by: Tayrtahn <tayrtahn@gmail.com> Co-authored-by: chromiumboy <50505512+chromiumboy@users.noreply.github.com> Co-authored-by: SlamBamActionman <83650252+SlamBamActionman@users.noreply.github.com> Co-authored-by: Kyle Tyo <36606155+VerinSenpai@users.noreply.github.com> Co-authored-by: Nemanja <98561806+EmoGarbage404@users.noreply.github.com> Co-authored-by: Cojoke <83733158+Cojoke-dot@users.noreply.github.com> Co-authored-by: Pieter-Jan Briers <pieterjan.briers+git@gmail.com> Co-authored-by: TemporalOroboros <TemporalOroboros@gmail.com> Co-authored-by: Lordbrandon12 <107556696+Lordbrandon12@users.noreply.github.com> Co-authored-by: Perry Fraser <perryprog@users.noreply.github.com> Co-authored-by: slarticodefast <161409025+slarticodefast@users.noreply.github.com> Co-authored-by: Princess Cheeseballs <66055347+Pronana@users.noreply.github.com> Co-authored-by: ArtisticRoomba <145879011+ArtisticRoomba@users.noreply.github.com> Co-authored-by: Krunklehorn <42424291+Krunklehorn@users.noreply.github.com> Co-authored-by: Leon Friedrich <60421075+ElectroJr@users.noreply.github.com> Co-authored-by: paige404 <59348003+paige404@users.noreply.github.com> Co-authored-by: ElectroJr <leonsfriedrich@gmail.com> Co-authored-by: M4rchy-S <89603088+M4rchy-S@users.noreply.github.com> Co-authored-by: Princess Cheeseballs <66055347+Princess-Cheeseballs@users.noreply.github.com> Co-authored-by: Connor Huffine <chuffine@gmail.com> Co-authored-by: metalgearsloth <comedian_vs_clown@hotmail.com> Co-authored-by: Whatstone <166147148+whatston3@users.noreply.github.com> Co-authored-by: Errant <35878406+Errant-4@users.noreply.github.com> Co-authored-by: Schrödinger <132720404+Schrodinger71@users.noreply.github.com> Co-authored-by: Samuka-C <47865393+Samuka-C@users.noreply.github.com> Co-authored-by: Milon <milonpl.git@proton.me> Co-authored-by: ScarKy0 <scarky0@onet.eu> Co-authored-by: beck-thompson <107373427+beck-thompson@users.noreply.github.com> Co-authored-by: psykana <36602558+psykana@users.noreply.github.com> Co-authored-by: thetuerk <46725294+ThanosDeGraf@users.noreply.github.com> Co-authored-by: ScarKy0 <106310278+ScarKy0@users.noreply.github.com> Co-authored-by: Ed <96445749+TheShuEd@users.noreply.github.com> Co-authored-by: MilenVolf <63782763+MilenVolf@users.noreply.github.com> Co-authored-by: YotaXP <yotaxp+git@gmail.com> Co-authored-by: BarryNorfolk <barrynorfolkman@protonmail.com> Co-authored-by: JesterX666 <32009105+JesterX666@users.noreply.github.com> Co-authored-by: mtrs163 <153596133+mtrs163@users.noreply.github.com> Co-authored-by: IProduceWidgets <107586145+IProduceWidgets@users.noreply.github.com>
198 lines
7.5 KiB
C#
198 lines
7.5 KiB
C#
using System;
|
|
using System.Collections.Generic;
|
|
using System.IO;
|
|
using System.Linq;
|
|
using System.Reflection;
|
|
using System.Threading.Tasks;
|
|
using Content.IntegrationTests;
|
|
using Content.IntegrationTests.Utility;
|
|
using Robust.Shared.Prototypes;
|
|
using Robust.Shared.Reflection;
|
|
using Robust.Shared.Serialization.Markdown.Validation;
|
|
using Robust.Shared.Timing;
|
|
using Robust.Shared.Utility;
|
|
using Robust.UnitTesting;
|
|
using Robust.UnitTesting.Pool;
|
|
|
|
namespace Content.YAMLLinter
|
|
{
|
|
internal static class Program
|
|
{
|
|
private static readonly ExternalTestContext TestContext = new("YAML Linter", StreamWriter.Null);
|
|
|
|
private static async Task<int> Main(string[] _)
|
|
{
|
|
GameDataScrounger.NoScrounging = true; // Ugly hack for YAML Linter.
|
|
PoolManager.Startup();
|
|
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.");
|
|
PoolManager.Shutdown();
|
|
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.");
|
|
PoolManager.Shutdown();
|
|
return -1;
|
|
}
|
|
|
|
private static async Task<(Dictionary<string, HashSet<ErrorNode>> YamlErrors, List<string> FieldErrors)>
|
|
ValidateClient()
|
|
{
|
|
await using var pair = await PoolManager.GetServerClient(testContext: TestContext);
|
|
var client = pair.Client;
|
|
var result = await ValidateInstance(client);
|
|
await pair.CleanReturnAsync();
|
|
return result;
|
|
}
|
|
|
|
private static async Task<(Dictionary<string, HashSet<ErrorNode>> YamlErrors, List<string> FieldErrors)>
|
|
ValidateServer()
|
|
{
|
|
await using var pair = await PoolManager.GetServerClient(testContext: TestContext);
|
|
var server = pair.Server;
|
|
var result = await ValidateInstance(server);
|
|
await pair.CleanReturnAsync();
|
|
return result;
|
|
}
|
|
|
|
private static async Task<(Dictionary<string, HashSet<ErrorNode>>, List<string>)> ValidateInstance(
|
|
RobustIntegrationTest.IntegrationInstance instance)
|
|
{
|
|
var protoMan = instance.ResolveDependency<IPrototypeManager>();
|
|
Dictionary<string, HashSet<ErrorNode>> yamlErrors = default!;
|
|
List<string> fieldErrors = default!;
|
|
|
|
await instance.WaitPost(() =>
|
|
{
|
|
var engineErrors = protoMan.ValidateDirectory(new ResPath("/EnginePrototypes"), out var engPrototypes);
|
|
yamlErrors = protoMan.ValidateDirectory(new ResPath("/Prototypes"), out var prototypes);
|
|
|
|
// Merge engine & content prototypes
|
|
foreach (var (kind, instances) in engPrototypes)
|
|
{
|
|
if (prototypes.TryGetValue(kind, out var existing))
|
|
existing.UnionWith(instances);
|
|
else
|
|
prototypes[kind] = instances;
|
|
}
|
|
|
|
foreach (var (kind, set) in engineErrors)
|
|
{
|
|
if (yamlErrors.TryGetValue(kind, out var existing))
|
|
existing.UnionWith(set);
|
|
else
|
|
yamlErrors[kind] = set;
|
|
}
|
|
|
|
fieldErrors = protoMan.ValidateStaticFields(prototypes);
|
|
});
|
|
|
|
return (yamlErrors, fieldErrors);
|
|
}
|
|
|
|
public static async Task<(Dictionary<string, HashSet<ErrorNode>> YamlErrors, List<string> FieldErrors)>
|
|
RunValidation()
|
|
{
|
|
var (clientAssemblies, serverAssemblies) = await GetClientServerAssemblies();
|
|
var serverTypes = serverAssemblies.SelectMany(n => n.GetTypes()).Select(t => t.Name).ToHashSet();
|
|
var clientTypes = clientAssemblies.SelectMany(n => n.GetTypes()).Select(t => t.Name).ToHashSet();
|
|
|
|
var yamlErrors = new Dictionary<string, HashSet<ErrorNode>>();
|
|
|
|
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.YamlErrors.TryGetValue(key, out var clientVal))
|
|
newErrors.UnionWith(val.Intersect(clientVal));
|
|
|
|
// Include any errors that relate to server-only types
|
|
foreach (var errorNode in val)
|
|
{
|
|
if (errorNode is FieldNotFoundErrorNode fieldNotFoundNode && !clientTypes.Contains(fieldNotFoundNode.FieldType.Name))
|
|
{
|
|
newErrors.Add(errorNode);
|
|
}
|
|
}
|
|
|
|
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;
|
|
|
|
// Include any errors that relate to client-only types
|
|
foreach (var errorNode in val)
|
|
{
|
|
if (errorNode is FieldNotFoundErrorNode fieldNotFoundNode && !serverTypes.Contains(fieldNotFoundNode.FieldType.Name))
|
|
{
|
|
newErrors.Add(errorNode);
|
|
}
|
|
}
|
|
}
|
|
|
|
// Finally, combine the prototype ID field errors.
|
|
var fieldErrors = serverErrors.FieldErrors
|
|
.Concat(clientErrors.FieldErrors)
|
|
.Distinct()
|
|
.ToList();
|
|
|
|
return (yamlErrors, fieldErrors);
|
|
}
|
|
|
|
private static async Task<(Assembly[] clientAssemblies, Assembly[] serverAssemblies)>
|
|
GetClientServerAssemblies()
|
|
{
|
|
await using var pair = await PoolManager.GetServerClient(testContext: TestContext);
|
|
|
|
var result = (GetAssemblies(pair.Client), GetAssemblies(pair.Server));
|
|
|
|
await pair.CleanReturnAsync();
|
|
|
|
return result;
|
|
|
|
Assembly[] GetAssemblies(RobustIntegrationTest.IntegrationInstance instance)
|
|
{
|
|
var refl = instance.ResolveDependency<IReflectionManager>();
|
|
return refl.Assemblies.ToArray();
|
|
}
|
|
}
|
|
}
|
|
}
|