forked from julianperrott/WowClassicGrindBot
-
-
Notifications
You must be signed in to change notification settings - Fork 190
Expand file tree
/
Copy pathProgram.cs
More file actions
143 lines (113 loc) · 4.63 KB
/
Program.cs
File metadata and controls
143 lines (113 loc) · 4.63 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
using CommandLine;
using Core;
using Frontend;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;
using Serilog;
using Serilog.Templates;
using Serilog.Templates.Themes;
using SharedLib.Logging;
namespace HeadlessServer;
public sealed class Program
{
public static void Main(string[] args)
{
var environmentName = Environment.GetEnvironmentVariable("ASPNETCORE_ENVIRONMENT");
IConfiguration configuration = new ConfigurationBuilder()
.SetBasePath(Directory.GetCurrentDirectory())
.AddJsonFile("headless_appsettings.json", optional: true, reloadOnChange: true)
.AddJsonFile($"headless_appsettings.{environmentName}.json", optional: true, reloadOnChange: true)
.AddEnvironmentVariables()
.AddCommandLine(args)
.Build();
IServiceCollection services = new ServiceCollection();
ILoggerFactory logFactory = LoggerFactory.Create(builder =>
{
builder.ClearProviders().AddSerilog();
});
services.AddLogging(builder =>
{
Log.Logger = new LoggerConfiguration()
.ReadFrom.Configuration(configuration)
.Enrich.FromLogContext()
.Enrich.With<ShortSourceContextEnricher>()
.WriteTo.File(new ExpressionTemplate(LogOutputTemplates.Default),
path: "headless_out.log",
rollingInterval: RollingInterval.Day)
.WriteTo.Debug(new ExpressionTemplate(LogOutputTemplates.Default))
.WriteTo.Console(new ExpressionTemplate(LogOutputTemplates.Default, theme: TemplateTheme.Literate))
.CreateLogger();
builder.Services.AddSingleton<Microsoft.Extensions.Logging.ILogger>(logFactory.CreateLogger(string.Empty));
builder.AddSerilog();
});
ILogger<Program> log = logFactory.CreateLogger<Program>();
if (log.IsEnabled(LogLevel.Information))
{
log.LogInformation($"Hosting environment: {environmentName ?? "Production"}");
log.LogInformation(
$"{Thread.CurrentThread.CurrentCulture.TwoLetterISOLanguageName} " +
$"{DateTimeOffset.Now}");
}
ParserResult<RunOptions> options =
Parser.Default.ParseArguments<RunOptions>(args).WithNotParsed(errors =>
{
foreach (Error? e in errors)
{
log.LogError($"{e}");
}
});
if (options.Tag == ParserResultType.NotParsed)
{
goto Exit;
}
services.AddSingleton<RunOptions>(options.Value);
services.AddStartupConfigFactories();
if (!FrameConfig.Exists() || !AddonConfig.Exists())
{
log.LogError($"Unable to run {nameof(HeadlessServer)} as crucial configuration files were missing!");
log.LogWarning($"Please be sure, the following validated configuration files present next to the executable:");
log.LogWarning($"{Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().Location)}");
log.LogWarning($"* {DataConfigMeta.DefaultFileName}");
log.LogWarning($"* {FrameConfigMeta.DefaultFilename}");
log.LogWarning($"* {AddonConfigMeta.DefaultFileName}");
goto Exit;
}
if (!ConfigureServices(log, services))
{
goto Exit;
}
ServiceProvider provider = services
.AddSingleton<HeadlessServer>()
.BuildServiceProvider(new ServiceProviderOptions() { ValidateOnBuild = true });
var logger =
provider.GetRequiredService<Microsoft.Extensions.Logging.ILogger>();
AppDomain.CurrentDomain.UnhandledException += (object sender, UnhandledExceptionEventArgs args) =>
{
Exception e = (Exception)args.ExceptionObject;
logger.LogError(e, e.Message);
};
HeadlessServer headlessServer = provider.GetRequiredService<HeadlessServer>();
if (options.Value.LoadOnly)
{
bool success = headlessServer.RunLoadOnly(options);
Environment.Exit(success ? 0 : 1);
}
else
{
headlessServer.Run(options);
}
Exit:
Console.ReadKey();
}
private static bool ConfigureServices(
Microsoft.Extensions.Logging.ILogger log,
IServiceCollection services)
{
if (!services.AddWoWProcess(log))
return false;
services.AddCoreBase(log);
services.AddCoreNormal(log);
return true;
}
}