-
Notifications
You must be signed in to change notification settings - Fork 87
/
Program.cs
191 lines (159 loc) · 7.17 KB
/
Program.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Threading.Tasks;
using Content.IntegrationTests;
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;
namespace Content.YAMLLinter
{
internal static class Program
{
private static async Task<int> Main(string[] _)
{
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();
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();
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();
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();
}
}
}
}