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
|
using System;
using System.IO;
using System.Linq;
using Impostor.Api.Events.Managers;
using Impostor.Api.Games;
using Impostor.Api.Games.Managers;
using Impostor.Api.Net.Manager;
using Impostor.Api.Net.Messages;
using Impostor.Hazel.Extensions;
using Impostor.Server.Config;
using Impostor.Server.Events;
using Impostor.Server.Net;
using Impostor.Server.Net.Factories;
using Impostor.Server.Net.Manager;
using Impostor.Server.Net.Messages;
using Impostor.Server.Net.Redirector;
using Impostor.Server.Plugins;
using Impostor.Server.Recorder;
using Impostor.Server.Utils;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using Microsoft.Extensions.ObjectPool;
using Serilog;
using Serilog.Events;
namespace Impostor.Server
{
internal static class Program
{
private static int Main(string[] args)
{
#if DEBUG
var logLevel = LogEventLevel.Debug;
#else
var logLevel = LogEventLevel.Information;
#endif
if (args.Contains("--verbose"))
{
logLevel = LogEventLevel.Verbose;
}
else if (args.Contains("--errors-only"))
{
logLevel = LogEventLevel.Error;
}
Log.Logger = new LoggerConfiguration()
.MinimumLevel.Is(logLevel)
#if DEBUG
.MinimumLevel.Override("Microsoft", LogEventLevel.Debug)
#else
.MinimumLevel.Override("Microsoft", LogEventLevel.Information)
#endif
.Enrich.FromLogContext()
.WriteTo.Console()
.CreateLogger();
try
{
Log.Information("Starting Impostor v{0}", DotnetUtils.GetVersion());
CreateHostBuilder(args).Build().Run();
return 0;
}
catch (Exception ex)
{
Log.Fatal(ex, "Impostor terminated unexpectedly");
return 1;
}
finally
{
Log.CloseAndFlush();
}
}
private static IConfiguration CreateConfiguration(string[] args)
{
var configurationBuilder = new ConfigurationBuilder();
configurationBuilder.AddJsonFile("config.json", true);
configurationBuilder.AddJsonFile("config.Development.json", true);
configurationBuilder.AddEnvironmentVariables(prefix: "IMPOSTOR_");
configurationBuilder.AddCommandLine(args);
return configurationBuilder.Build();
}
//c 入口
private static IHostBuilder CreateHostBuilder(string[] args)
{
var configuration = CreateConfiguration(args);
var pluginConfig = configuration.GetSection("PluginLoader")
.Get<PluginConfig>() ?? new PluginConfig();
return Host.CreateDefaultBuilder(args)
.UseContentRoot(Directory.GetCurrentDirectory())
#if DEBUG
.UseEnvironment(Environment.GetEnvironmentVariable("IMPOSTOR_ENV") ?? "Development")
#else
.UseEnvironment("Production")
#endif
.ConfigureAppConfiguration(builder =>
{
builder.AddConfiguration(configuration);
})
.ConfigureServices((host, services) =>
{
var debug = host.Configuration
.GetSection(DebugConfig.Section)
.Get<DebugConfig>() ?? new DebugConfig();
var redirector = host.Configuration
.GetSection(ServerRedirectorConfig.Section)
.Get<ServerRedirectorConfig>() ?? new ServerRedirectorConfig();
services.Configure<DebugConfig>(host.Configuration.GetSection(DebugConfig.Section));
services.Configure<AntiCheatConfig>(host.Configuration.GetSection(AntiCheatConfig.Section));
services.Configure<ServerConfig>(host.Configuration.GetSection(ServerConfig.Section));
services.Configure<ServerRedirectorConfig>(host.Configuration.GetSection(ServerRedirectorConfig.Section));
if (redirector.Enabled)
{
if (!string.IsNullOrEmpty(redirector.Locator.Redis))
{
// When joining a game, it retrieves the game server ip from redis.
// When a game has been created on this node, it stores the game code with its ip in redis.
services.AddSingleton<INodeLocator, NodeLocatorRedis>();
// Dependency for the NodeLocatorRedis.
services.AddStackExchangeRedisCache(options =>
{
options.Configuration = redirector.Locator.Redis;
options.InstanceName = "ImpostorRedis";
});
}
else if (!string.IsNullOrEmpty(redirector.Locator.UdpMasterEndpoint))
{
services.AddSingleton<INodeLocator, NodeLocatorUdp>();
if (redirector.Master)
{
services.AddHostedService<NodeLocatorUdpService>();
}
}
else
{
throw new Exception("Missing a valid NodeLocator config.");
}
// Use the configuration as source for the list of nodes to provide
// when creating a game.
services.AddSingleton<INodeProvider, NodeProviderConfig>();
}
else
{
// Redirector is not enabled but the dependency is still required.
// So we provide one that ignores all calls.
services.AddSingleton<INodeLocator, NodeLocatorNoOp>();
}
services.AddSingleton<ClientManager>();
services.AddSingleton<IClientManager>(p => p.GetRequiredService<ClientManager>());
if (redirector.Enabled && redirector.Master)
{
services.AddSingleton<IClientFactory, ClientFactory<ClientRedirector>>();
// For a master server, we don't need a GameManager.
}
else
{
if (debug.GameRecorderEnabled)
{
services.AddSingleton<ObjectPoolProvider>(new DefaultObjectPoolProvider());
services.AddSingleton<ObjectPool<PacketSerializationContext>>(serviceProvider =>
{
var provider = serviceProvider.GetRequiredService<ObjectPoolProvider>();
var policy = new PacketSerializationContextPooledObjectPolicy();
return provider.Create(policy);
});
services.AddSingleton<PacketRecorder>();
services.AddHostedService(sp => sp.GetRequiredService<PacketRecorder>());
services.AddSingleton<IClientFactory, ClientFactory<ClientRecorder>>();
}
else
{
services.AddSingleton<IClientFactory, ClientFactory<Client>>();
}
services.AddSingleton<GameManager>();
services.AddSingleton<IGameManager>(p => p.GetRequiredService<GameManager>());
}
services.AddHazel();
services.AddSingleton<IMessageWriterProvider, MessageWriterProvider>();
services.AddSingleton<IGameCodeFactory, GameCodeFactory>();
services.AddSingleton<IEventManager, EventManager>();
services.AddSingleton<Matchmaker>();
services.AddHostedService<MatchmakerService>();
})
.UseSerilog()
.UseConsoleLifetime()
.UsePluginLoader(pluginConfig);
}
}
}
|