-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathProgram.cs
More file actions
194 lines (163 loc) · 8.1 KB
/
Program.cs
File metadata and controls
194 lines (163 loc) · 8.1 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
using System.Net;
using System.Security.Cryptography.X509Certificates;
using Blaze3SDK;
using BlazeCommon;
using NLog;
using NLog.Layouts;
using Tdf;
using YamlDotNet.Serialization;
using YamlDotNet.Serialization.NamingConventions;
using Zamboni14Legacy.Components.Blaze;
using ZamboniCommonComponents;
using ZamboniCommonComponents.Structs.TdfTagged;
using ZamboniUltimateTeam;
using LogLevel = NLog.LogLevel;
namespace Zamboni14Legacy;
internal class Program
{
public const string Name = "Zamboni14Legacy 1.1";
public const int RedirectorPort = 42127;
private static readonly Logger Logger = LogManager.GetCurrentClassLogger();
public static ZamboniConfig ZamboniConfig;
public static Database Database;
public static readonly string PublicIp = new HttpClient().GetStringAsync("https://checkip.amazonaws.com/").GetAwaiter().GetResult().Trim();
public static string GameServerIp;
private static ZamboniCoreServer core;
private static async Task Main(string[] args)
{
InitConfig();
StartLogger();
InitDatabase();
GameServerIp = ZamboniConfig.GameServerIp.Equals("auto") ? PublicIp : ZamboniConfig.GameServerIp;
var tasks = new List<Task>();
tasks.Add(Task.Run(StartCommandListener));
tasks.Add(StartCoreServer());
tasks.Add(new Api().StartAsync());
if (ZamboniConfig.HostRedirectorInstance) tasks.Add(StartRedirectorServer());
await RelayCommunicator.ResetAllInstances([ZamboniConfig.TargetProtocol]);
Logger.Warn(Name + " started");
await Task.WhenAll(tasks);
}
private static void StartLogger()
{
var logLevel = LogLevel.FromString(ZamboniConfig.LogLevel);
var layout = new SimpleLayout("[${longdate}][${callsite-filename:includeSourcePath=false}(${callsite-linenumber})][${level:uppercase=true}]: ${message:withexception=true}");
LogManager.Setup().LoadConfiguration(builder =>
{
builder.ForLogger().FilterMinLevel(logLevel)
.WriteToConsole(layout)
.WriteToFile("logs/server-${shortdate}.log", layout);
});
}
private static async Task StartRedirectorServer()
{
var certBytes = File.ReadAllBytes("gosredirector_mod.pfx");
X509Certificate cert = new X509Certificate2(certBytes, "123456");
var redirector = Blaze3.CreateBlazeServer("RedirectorServer", new IPEndPoint(IPAddress.Any, RedirectorPort), cert);
redirector.AddComponent<RedirectorComponent>();
await redirector.Start(-1).ConfigureAwait(false);
}
private static void InitConfig()
{
const string configFile = "zamboni-config.yml";
var deserializer = new DeserializerBuilder()
.WithNamingConvention(PascalCaseNamingConvention.Instance)
.Build();
var serializer = new SerializerBuilder()
.WithNamingConvention(PascalCaseNamingConvention.Instance)
.Build();
if (!File.Exists(configFile))
{
ZamboniConfig = new ZamboniConfig();
var yaml = serializer.Serialize(ZamboniConfig);
var comments = "# GameServerIp: 'auto' = automatically detect public IP or specify a manual IP address, where GameServer is run on\n" +
"# GameServerPort: Port for GameServer to listen on. (Redirector server lives on " + RedirectorPort + ", clients request there)\n" +
"# LogLevel: Valid values: Trace, Debug, Info, Warn, Error, Fatal, Off.\n" +
"# DatabaseConnectionString: Connection string to PostgreSQL, for saving data. (Not required)\n" +
"# HostRedirectorInstance: Whether this program should host a Redirector instance\n" +
"# ApiServerIdentifier and ApiServerPort: identifier and port where status is\n\n";
File.WriteAllText(configFile, comments + yaml);
Logger.Warn("Config file created: " + configFile);
return;
}
var yamlText = File.ReadAllText(configFile);
ZamboniConfig = deserializer.Deserialize<ZamboniConfig>(yamlText);
}
private static void InitDatabase()
{
Database = new Database();
}
private static async Task StartCoreServer()
{
var tdfFactory = new TdfFactory();
var tdfDecoder = tdfFactory.CreateDecoder(true);
var config = new BlazeServerConfiguration("CoreServer", new IPEndPoint(IPAddress.Any, ZamboniConfig.GameServerPort), tdfFactory.CreateEncoder(true), tdfDecoder);
core = new ZamboniCoreServer(config);
core.AddComponent<UtilComponent>();
core.AddComponent<AuthenticationComponent>();
core.AddComponent<UserSessionsComponent>();
core.AddComponent<RoomsComponent>();
core.AddComponent<MessagingComponent>();
core.AddComponent<CensusDataComponent>();
core.AddComponent<AssociationListsComponent>();
core.AddComponent<ClubsComponent>();
core.AddComponent<StatsComponent>();
core.AddComponent<GameManager>();
core.AddComponent<GameReportingComponent>();
core.AddComponent<GameReportingLegacyComponent>();
core.AddComponent<LeagueComponent>();
core.AddComponent<OSDKSeasonalPlayComponent>();
core.AddComponent<OsdkDynamicMessagingComponent>();
core.AddComponent<OsdkWebOfferSurveyComponent>();
core.AddComponent<OSDKSettingsComponent>();
core.AddComponent<OsdkOnlinePassComponent>();
core.AddComponent<OsdkTicker2Component>();
UltimateTeam.Initialize(Database.ConnectionString, new ServerProviderBridge(), "packs.yml");
core.AddComponent<CardHouseComponent>();
tdfFactory.RegisterTdfType(typeof(Report));
tdfFactory.RegisterTdfType(typeof(ClubReportVersusGame));
tdfFactory.RegisterTdfType(typeof(CustomPlayerReportVersusGame));
tdfFactory.RegisterTdfType(typeof(ClubReportSoGame));
tdfFactory.RegisterTdfType(typeof(CustomPlayerReportSoGame));
await core.Start(-1).ConfigureAwait(false);
}
private static void StartCommandListener()
{
Logger.Info("Type 'help' or 'status'.");
while (true)
{
var input = ReadLine.Read();
if (string.IsNullOrWhiteSpace(input))
continue;
switch (input.Trim().ToLowerInvariant())
{
case "help":
Logger.Warn("Available commands: help, status, reload");
break;
case "status":
Logger.Info(Name);
Logger.Info("Server running on ip: " + GameServerIp + " (" + PublicIp + ")");
Logger.Info("GameServerPort port: " + ZamboniConfig.GameServerPort);
Logger.Info("Redirector port: " + RedirectorPort);
Logger.Info("Online Players: " + ServerManager.GetServerPlayers().Count);
foreach (var serverPlayer in ServerManager.GetServerPlayers().Values)
Logger.Info(
serverPlayer.UserIdentification.mName + " "
+ serverPlayer.UserIdentification.mAccountId + " "
+ serverPlayer.BlazeServerConnection.ProtoFireConnection.ID);
Logger.Info("Queued Total Players: " + ServerManager.GetQueuedPlayers().Count);
foreach (var queuedPlayer in ServerManager.GetQueuedPlayers().Values) Logger.Info(queuedPlayer.ServerPlayer.UserIdentification.mName);
Logger.Info("Server Games: " + ServerManager.GetServerGames().Count);
foreach (var serverGame in ServerManager.GetServerGames()) Logger.Info(serverGame);
break;
case "reload":
UltimateTeam.ReloadPackConfig();
Logger.Warn("Reloaded configs");
break;
default:
Logger.Info($"Unknown command: {input}");
break;
}
}
}
}