-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProgram.cs
More file actions
318 lines (270 loc) · 11.6 KB
/
Program.cs
File metadata and controls
318 lines (270 loc) · 11.6 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
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
using AET.ModVerify.App.Settings;
using AET.ModVerify.App.Settings.CommandLine;
using AET.ModVerify.App.Updates;
using AET.ModVerify.App.Utilities;
using AET.SteamAbstraction;
using AnakinRaW.ApplicationBase;
using AnakinRaW.ApplicationBase.Environment;
using AnakinRaW.ApplicationBase.Update;
using AnakinRaW.ApplicationBase.Update.Options;
using AnakinRaW.AppUpdaterFramework.Json;
using AnakinRaW.CommonUtilities.Hashing;
using AnakinRaW.CommonUtilities.Registry;
using AnakinRaW.CommonUtilities.Registry.Windows;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;
using PG.Commons;
using PG.StarWarsGame.Engine;
using PG.StarWarsGame.Files.ALO;
using PG.StarWarsGame.Files.MEG;
using PG.StarWarsGame.Files.MTD;
using PG.StarWarsGame.Files.XML;
using PG.StarWarsGame.Files.XML.Parsers;
using PG.StarWarsGame.Infrastructure;
using PG.StarWarsGame.Infrastructure.Services.Detection;
using PG.StarWarsGame.Infrastructure.Services.Name;
using Serilog;
using Serilog.Events;
using Serilog.Expressions;
using Serilog.Filters;
using Serilog.Sinks.SystemConsole.Themes;
using System;
using System.Collections.Generic;
using System.IO.Abstractions;
using System.Runtime.InteropServices;
using System.Threading.Tasks;
using AET.ModVerify.App.Reporting;
using PG.StarWarsGame.Engine.Xml;
using Testably.Abstractions;
using ILogger = Serilog.ILogger;
namespace AET.ModVerify.App;
internal class MainClass
{
// Fody/Costura application with .NET Core apparently don't work well when the class containing the Main method are derived by a type in an embedded assembly.
private static Task<int> Main(string[] args)
{
return new Program().StartAsync(args);
}
}
internal class Program : SelfUpdateableAppLifecycle
{
private static readonly string EngineParserNamespace = typeof(PetroglyphStarWarsGameXmlParser).Namespace!;
private static readonly string ParserNamespace = typeof(XmlFileParser<>).Namespace!;
private static readonly string ModVerifyRootNameSpace = typeof(Program).Namespace!;
private static readonly CompiledExpression PrintToConsoleExpression = SerilogExpression.Compile($"EventId.Id = {ModVerifyConstants.ConsoleEventIdValue}");
private AppSettingsBase? _modVerifyAppSettings;
private ApplicationUpdateOptions _updateOptions = new();
private bool _offlineMode;
private bool _verboseMode;
private bool _isLaunchedWithoutArguments;
protected override async Task<int> InitializeAppAsync(IReadOnlyList<string> args, IServiceProvider bootstrapServices)
{
await base.InitializeAppAsync(args, bootstrapServices);
ModVerifyConsoleUtilities.WriteHeader(ApplicationEnvironment.AssemblyInfo.InformationalVersion);
ModVerifyOptionsContainer parsedOptions;
try
{
parsedOptions = new ModVerifyOptionsParser(ApplicationEnvironment, BootstrapLoggerFactory).Parse(args);
}
catch (Exception e)
{
Logger?.LogCritical(e, "Failed to parse commandline arguments: {Message}", e.Message);
ConsoleUtilities.WriteApplicationFatalError(ModVerifyConstants.AppNameString, e);
return e.HResult;
}
if (!parsedOptions.HasOptions)
return ModVerifyConstants.ErrorBadArguments;
if (parsedOptions.UpdateOptions is not null)
_updateOptions = parsedOptions.UpdateOptions;
if (parsedOptions.ModVerifyOptions?.Verbose is true || parsedOptions.UpdateOptions?.Verbose is true)
_verboseMode = true;
if (parsedOptions.ModVerifyOptions is null)
return ModVerifyConstants.Success;
_offlineMode = parsedOptions.ModVerifyOptions.OfflineMode;
_isLaunchedWithoutArguments = parsedOptions.ModVerifyOptions.LaunchedWithoutArguments();
try
{
_modVerifyAppSettings = new SettingsBuilder(bootstrapServices)
.BuildSettings(parsedOptions.ModVerifyOptions);
}
catch (AppArgumentException e)
{
Logger?.LogCritical(e, "Invalid arguments specified by the user: {Message}", e.Message);
ConsoleUtilities.WriteApplicationFatalError(ModVerifyConstants.AppNameString, e.Message);
return e.HResult;
}
catch (Exception e)
{
Logger?.LogCritical(e, "Failed to create settings form commandline arguments: {Message}", e.Message);
ConsoleUtilities.WriteApplicationFatalError(ModVerifyConstants.AppNameString, e);
return e.HResult;
}
return ModVerifyConstants.Success;
}
protected override void CreateAppServices(IServiceCollection services, IReadOnlyList<string> args)
{
base.CreateAppServices(services, args);
services.AddSingleton((ApplicationEnvironment as ModVerifyAppEnvironment)!);
services.AddLogging(ConfigureLogging);
services.AddSingleton<IHashingService>(sp => new HashingService(sp));
if (IsUpdateableApplication)
{
#if NET
throw new NotSupportedException();
#endif
services.MakeAppUpdateable(
UpdatableApplicationEnvironment,
sp => new CosturaApplicationProductService(ApplicationEnvironment, sp),
sp => new JsonManifestLoader(sp));
}
if (_modVerifyAppSettings is null)
return;
SteamAbstractionLayer.InitializeServices(services);
PetroglyphGameInfrastructure.InitializeServices(services);
services.SupportMTD();
services.SupportMEG();
services.SupportALO();
services.SupportXML();
PetroglyphCommons.ContributeServices(services);
PetroglyphEngineServiceContribution.ContributeServices(services);
services.AddModVerify();
services.RegisterVerifierCache();
services.AddSingleton<IBaselineFactory>(sp => new BaselineFactory(sp));
if (_offlineMode)
{
services.AddSingleton<IModNameResolver>(sp => new OfflineModNameResolver(sp));
services.AddSingleton<IModGameTypeResolver>(sp => new OfflineModGameTypeResolver(sp));
}
else
{
services.AddSingleton<IModNameResolver>(sp => new OnlineModNameResolver(sp));
services.AddSingleton<IModGameTypeResolver>(sp => new OnlineModGameTypeResolver(sp));
}
}
protected override ApplicationEnvironment CreateAppEnvironment()
{
return new ModVerifyAppEnvironment(typeof(Program).Assembly, FileSystem);
}
protected override IFileSystem CreateFileSystem()
{
return new RealFileSystem();
}
protected override IRegistry CreateRegistry()
{
return !RuntimeInformation.IsOSPlatform(OSPlatform.Windows)
? new InMemoryRegistry(InMemoryRegistryCreationFlags.WindowsLike)
: new WindowsRegistry();
}
protected override async Task<int> RunAppAsync(string[] args, IServiceProvider appServiceProvider)
{
var result = await HandleUpdate(appServiceProvider);
if (result != 0 || _modVerifyAppSettings is null)
return result;
return await new ModVerifyApplication(_modVerifyAppSettings, appServiceProvider).RunAsync().ConfigureAwait(false);
}
private void ConfigureLogging(ILoggingBuilder loggingBuilder)
{
loggingBuilder.ClearProviders();
// ReSharper disable once RedundantAssignment
var logLevel = LogEventLevel.Information;
#if DEBUG
logLevel = LogEventLevel.Debug;
loggingBuilder.AddDebug();
#endif
if (_verboseMode)
{
logLevel = LogEventLevel.Verbose;
loggingBuilder.AddDebug();
}
var fileLogger = SetupFileLogging();
loggingBuilder.AddSerilog(fileLogger);
var consoleLogger = SetupConsoleLogging();
loggingBuilder.AddSerilog(consoleLogger);
return;
ILogger SetupConsoleLogging()
{
return new LoggerConfiguration()
.WriteTo.Console(
logLevel,
theme: AnsiConsoleTheme.Code,
outputTemplate: "[{Level:u3}] {Message:lj}{NewLine}{Exception}")
.MinimumLevel.Is(logLevel)
.Filter.ByIncludingOnly(x =>
{
// Fatal errors are handled by a global exception handler
if (x.Level == LogEventLevel.Fatal)
return false;
// Verbose should print everything we get
if (logLevel == LogEventLevel.Verbose)
return true;
// Debug should print everything that has something to do with ModVerify
if (logLevel == LogEventLevel.Debug)
{
if (!x.Properties.TryGetValue("SourceContext", out var value))
return false;
var source = value.ToString().AsSpan().Trim('\"');
return source.StartsWith(ModVerifyRootNameSpace.AsSpan());
}
// In normal operation, we only print logs, which have the print-to-console EventId set.
return ExpressionResult.IsTrue(PrintToConsoleExpression(x));
})
.CreateLogger();
}
ILogger SetupFileLogging()
{
var logPath = FileSystem.Path.Combine(ApplicationEnvironment.ApplicationLocalPath, "ModVerify_log.txt");
return new LoggerConfiguration()
.Enrich.FromLogContext()
.MinimumLevel.Is(logLevel)
.Filter.ByExcluding(IsXmlParserLogging)
.WriteTo.Async(c =>
{
c.RollingFile(
logPath,
outputTemplate:
"{Timestamp:yyyy-MM-dd HH:mm:ss.fff zzz} [{Level:u3}] [{SourceContext}] {Message}{NewLine}{Exception}");
})
.CreateLogger();
}
static bool IsXmlParserLogging(LogEvent logEvent)
{
return Matching.FromSource(ParserNamespace)(logEvent) || Matching.FromSource(EngineParserNamespace)(logEvent);
}
}
private async Task<int> HandleUpdate(IServiceProvider serviceProvider)
{
if (_offlineMode)
{
Logger?.LogTrace("Running in offline mode. There will be nothing to update.");
return ModVerifyConstants.Success;
}
ModVerifyUpdateMode updateMode;
if (_isLaunchedWithoutArguments)
updateMode = ModVerifyUpdateMode.InteractiveUpdate;
else
{
updateMode = _modVerifyAppSettings is not null
? ModVerifyUpdateMode.CheckOnly
: ModVerifyUpdateMode.AutoUpdate;
}
try
{
Logger?.LogDebug("Running update with mode '{ModVerifyUpdateMode}'", updateMode);
var modVerifyUpdater = new ModVerifyUpdater(serviceProvider);
await modVerifyUpdater.RunUpdateProcedure(_updateOptions, updateMode).ConfigureAwait(false);
Logger?.LogDebug("Update procedure completed successfully.");
return ModVerifyConstants.Success;
}
catch (Exception e)
{
Logger?.LogCritical(e, e.Message);
var action = updateMode switch
{
ModVerifyUpdateMode.CheckOnly => "checking for updates",
_ => "updating"
};
ConsoleUtilities.WriteApplicationFatalError(ModVerifyConstants.AppNameString, $"Error while {action}: {e.Message}", e.StackTrace);
return e.HResult;
}
}
}