-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathModVerifyApplicationAction.cs
More file actions
182 lines (154 loc) · 7.01 KB
/
Copy pathModVerifyApplicationAction.cs
File metadata and controls
182 lines (154 loc) · 7.01 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
using System;
using System.IO.Abstractions;
using System.Linq;
using System.Threading.Tasks;
using AET.ModVerify.App.GameFinder;
using AET.ModVerify.App.Reporting;
using AET.ModVerify.App.Settings;
using AET.ModVerify.App.TargetSelectors;
using AET.ModVerify.Reporting;
using AET.ModVerify.Reporting.Baseline;
using AET.ModVerify.Reporting.Suppressions;
using AnakinRaW.ApplicationBase;
using AnakinRaW.CommonUtilities.SimplePipeline;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;
namespace AET.ModVerify.App;
internal abstract class ModVerifyApplicationAction<T> : IModVerifyAppAction where T : AppSettingsBase
{
private readonly ModVerifyAppEnvironment _appEnvironment;
private readonly IFileSystem _fileSystem;
protected T Settings { get; }
protected IServiceProvider ServiceProvider { get; }
protected ILogger? Logger { get; }
protected ModVerifyApplicationAction(T settings, IServiceProvider serviceProvider)
{
Settings = settings ?? throw new ArgumentNullException(nameof(settings));
ServiceProvider = serviceProvider ?? throw new ArgumentNullException(nameof(serviceProvider));
Logger = serviceProvider.GetService<ILoggerFactory>()?.CreateLogger(GetType());
_appEnvironment = ServiceProvider.GetRequiredService<ModVerifyAppEnvironment>();
_fileSystem = ServiceProvider.GetRequiredService<IFileSystem>();
}
protected virtual void PrintAction(VerificationTarget target)
{
}
public async Task<int> ExecuteAsync()
{
VerificationTarget verificationTarget;
try
{
var targetSettings = Settings.VerificationTargetSettings;
verificationTarget = new VerificationTargetSelectorFactory(ServiceProvider)
.CreateSelector(targetSettings)
.Select(targetSettings);
}
catch (ArgumentException ex)
{
ConsoleUtilities.WriteApplicationFatalError(_appEnvironment.ApplicationName,
$"The specified arguments are not correct: {ex.Message}");
Logger?.LogError(ex, "Invalid application arguments: {Message}", ex.Message);
return ex.HResult;
}
catch (TargetNotFoundException ex)
{
ConsoleUtilities.WriteApplicationFatalError(_appEnvironment.ApplicationName, ex.Message);
Logger?.LogError(ex, ex.Message);
return ex.HResult;
}
catch (GameNotFoundException ex)
{
ConsoleUtilities.WriteApplicationFatalError(_appEnvironment.ApplicationName,
"Unable to find an installation of Empire at War or Forces of Corruption.");
Logger?.LogError(ex, "Game not found: {Message}", ex.Message);
return ex.HResult;
}
PrintAction(verificationTarget);
var verificationResult = await VerifyTargetAsync(verificationTarget)
.ConfigureAwait(false);
Console.WriteLine();
switch (verificationResult.Status)
{
case VerificationCompletionStatus.Cancelled:
return ModVerifyConstants.VerifyCancelled;
case VerificationCompletionStatus.Failed:
return ReportVerificationFailure(verificationResult.Exception!);
}
return await ProcessResult(verificationResult);
}
private int ReportVerificationFailure(Exception verificationException)
{
var exceptionToReport = verificationException switch
{
AggregateException aggregate => aggregate.InnerExceptions.FirstOrDefault() ?? aggregate,
StepFailureException stepFailure => stepFailure.FailedSteps.First().Error!,
_ => verificationException
};
ConsoleUtilities.WriteApplicationFatalError(_appEnvironment.ApplicationName, exceptionToReport);
Logger?.LogError(exceptionToReport, exceptionToReport.Message);
return exceptionToReport.HResult;
}
protected abstract Task<int> ProcessResult(VerificationResult result);
protected abstract VerificationBaseline GetBaseline(VerificationTarget verificationTarget);
private async Task<VerificationResult> VerifyTargetAsync(VerificationTarget verificationTarget)
{
var progressReporter = new VerifyConsoleProgressReporter(verificationTarget.Name, Settings.ReportSettings);
var baseline = GetBaseline(verificationTarget);
var suppressions = GetSuppressions();
try
{
var verifierService = ServiceProvider.GetRequiredService<IGameVerifierService>();
Logger?.LogInformation(ModVerifyConstants.ConsoleEventId, "Verifying '{Target}'...", verificationTarget.Name);
var verificationResult = await verifierService.VerifyAsync(
verificationTarget,
Settings.VerifierServiceSettings,
baseline,
suppressions,
progressReporter,
new EngineInitializeProgressReporter(verificationTarget.Engine));
progressReporter.Report(string.Empty, 1.0);
switch (verificationResult.Status)
{
case VerificationCompletionStatus.CompletedFailFast:
Logger?.LogWarning(ModVerifyConstants.ConsoleEventId, "Verification stopped due to enabled failFast setting.");
break;
case VerificationCompletionStatus.Cancelled:
Logger?.LogWarning(ModVerifyConstants.ConsoleEventId, "Verification was cancelled.");
break;
case VerificationCompletionStatus.Failed:
progressReporter.ReportError("Verification failed!",
$"An unexpected error occurred while verifying '{verificationTarget.Name}'.");
break;
case VerificationCompletionStatus.Completed:
default:
Logger?.LogInformation(ModVerifyConstants.ConsoleEventId, "Verification completed successfully.");
break;
}
return verificationResult;
}
catch (Exception e)
{
progressReporter.ReportError("Verification failed!", e.Message);
Logger?.LogError(e, "Verification failed: {Message}", e.Message);
throw;
}
finally
{
progressReporter.Dispose();
}
}
private SuppressionList GetSuppressions()
{
var suppressionsFile = Settings.ReportSettings.SuppressionsPath;
SuppressionList suppressions;
if (string.IsNullOrEmpty(suppressionsFile))
suppressions = SuppressionList.Empty;
else
{
using var fileStream = _fileSystem.File.OpenRead(suppressionsFile!);
suppressions = SuppressionList.FromJson(fileStream);
if (suppressions.Count > 0)
Logger?.LogInformation(ModVerifyConstants.ConsoleEventId, "Using suppressions from '{Suppressions}'", suppressionsFile);
}
return suppressions;
}
}