-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBaselineSelector.cs
More file actions
183 lines (158 loc) · 7.45 KB
/
Copy pathBaselineSelector.cs
File metadata and controls
183 lines (158 loc) · 7.45 KB
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
using AET.ModVerify.App.Resources.Baselines;
using AET.ModVerify.App.Settings;
using AET.ModVerify.Reporting.Baseline;
using AnakinRaW.ApplicationBase;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;
using PG.StarWarsGame.Engine;
using System;
using System.Diagnostics;
using System.Diagnostics.CodeAnalysis;
using System.Text;
namespace AET.ModVerify.App.Reporting;
internal sealed class BaselineSelector(AppVerifySettings settings, IServiceProvider services)
{
private readonly ILogger? _logger = services.GetService<ILoggerFactory>()?.CreateLogger(typeof(ModVerifyApplication));
private readonly IBaselineFactory _baselineFactory = services.GetRequiredService<IBaselineFactory>();
public VerificationBaseline SelectBaseline(VerificationTarget verificationTarget, out string? usedBaselinePath)
{
var baselinePath = settings.ReportSettings.BaselinePath;
if (!string.IsNullOrEmpty(baselinePath))
{
try
{
usedBaselinePath = baselinePath;
return _baselineFactory.ParseBaseline(baselinePath);
}
catch (InvalidBaselineException e)
{
using (ConsoleUtilities.HorizontalLineSeparatedBlock('*'))
{
Console.WriteLine($"The baseline '{baselinePath}' is not a valid baseline file: {e.Message}" +
$"{Environment.NewLine}Please generate a new baseline file or download the latest version." +
$"{Environment.NewLine}");
}
// For now, we bubble up this exception because we except users
// to correctly specify their baselines through command line arguments.
throw;
}
}
if (settings.ReportSettings is { SearchBaselineLocally: false, UseDefaultBaseline: false })
{
_logger?.LogDebug(ModVerifyConstants.ConsoleEventId,
"No baseline path specified and local search is not enabled. Using empty baseline.");
usedBaselinePath = null;
return VerificationBaseline.Empty;
}
if (settings.IsInteractive)
return FindBaselineInteractive(verificationTarget, out usedBaselinePath);
// If the application is not interactive, we only use a baseline file present in the directory of the verification target.
return FindBaselineNonInteractive(verificationTarget, out usedBaselinePath);
}
private VerificationBaseline FindBaselineInteractive(VerificationTarget verificationTarget, out string? baselinePath)
{
// The application is in interactive mode. We apply the following lookup:
// 1. Use a baseline found in the directory of the verification target.
// 2. Use a baseline found in the directory ModVerify executable.
// 3. If the verification target is a mod, ask the user to apply the default game's baseline.
// In any case ask the use if they want to use the located baseline file, or they wish to continue using none/empty.
_logger?.LogInformation(ModVerifyConstants.ConsoleEventId, "Searching for local baseline files...");
if (!_baselineFactory.TryFindBaselineInDirectory(
verificationTarget.Location.TargetPath,
b => IsBaselineCompatible(b, verificationTarget),
out var baseline,
out baselinePath))
{
if (!_baselineFactory.TryFindBaselineInDirectory(
Environment.CurrentDirectory,
b => IsBaselineCompatible(b, verificationTarget),
out baseline,
out baselinePath))
{
Console.ForegroundColor = ConsoleColor.Cyan;
Console.WriteLine("No baseline found locally.");
Console.ResetColor();
baselinePath = null;
TryGetDefaultBaseline(verificationTarget.Engine, out baseline);
return baseline ?? VerificationBaseline.Empty;
}
}
Debug.Assert(baselinePath is not null && baseline is not null);
return ShouldUseBaseline(baseline, baselinePath)
? baseline
: VerificationBaseline.Empty;
}
private static bool TryGetDefaultBaseline(
GameEngineType engineType,
[NotNullWhen(true)] out VerificationBaseline? baseline)
{
baseline = null;
if (engineType == GameEngineType.Eaw)
{
// TODO: EAW currently not implemented
return false;
}
if (!ConsoleUtilities.UserYesNoQuestion($"Do you want to load the default baseline for game engine '{engineType}'?"))
return false;
try
{
baseline = LoadEmbeddedBaseline(engineType);
return true;
}
catch (InvalidBaselineException)
{
throw new InvalidOperationException(
"Invalid baseline packed along ModVerify App. Please reach out to the creators. Thanks!");
}
}
internal static VerificationBaseline LoadEmbeddedBaseline(GameEngineType engineType)
{
var baselineFileName = $"baseline-{engineType.ToString().ToLower()}.json";
var resourcePath = $"{typeof(BaselineResources).Namespace}.{baselineFileName}";
using var baselineStream = typeof(BaselineSelector).Assembly.GetManifestResourceStream(resourcePath)!;
return VerificationBaseline.FromJson(baselineStream);
}
private VerificationBaseline FindBaselineNonInteractive(VerificationTarget target, out string? usedPath)
{
if (_baselineFactory.TryFindBaselineInDirectory(
target.Location.TargetPath,
b => IsBaselineCompatible(b, target),
out var baseline,
out usedPath))
{
_logger?.LogInformation(ModVerifyConstants.ConsoleEventId, "Automatically applying local baseline file '{Path}'.", usedPath);
return baseline;
}
_logger?.LogTrace("No baseline file found in taget path '{TargetPath}'.", target.Location.TargetPath);
usedPath = null;
if (settings.ReportSettings.UseDefaultBaseline)
{
try
{
var defaultBaseline = LoadEmbeddedBaseline(target.Engine);
_logger?.LogInformation(ModVerifyConstants.ConsoleEventId, "Automatically applying default embedded baseline for engine '{Engine}'.", target.Engine);
return defaultBaseline;
}
catch (InvalidBaselineException)
{
throw new InvalidOperationException(
"Invalid baseline packed along ModVerify App. Please reach out to the creators. Thanks!");
}
}
return VerificationBaseline.Empty;
}
private static bool IsBaselineCompatible(VerificationBaseline baseline, VerificationTarget target)
{
return baseline.Target?.Engine == target.Engine;
}
private static bool ShouldUseBaseline(VerificationBaseline baseline, string baselinePath)
{
var sb = new StringBuilder("Found baseline ");
if (baseline.Target is not null)
sb.Append($"for '{baseline.Target.Name}' ");
sb.Append($"at '{baselinePath}'.");
Console.ForegroundColor = ConsoleColor.Cyan;
Console.WriteLine(sb.ToString());
return ConsoleUtilities.UserYesNoQuestion("Do you want to use it?");
}
}