-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathProgram.cs
256 lines (240 loc) · 5.92 KB
/
Program.cs
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
using System;
using System.Diagnostics;
using System.IO;
using System.Net.Sockets;
using System.Threading;
using OpenHellion;
using ZeroGravity;
using ZeroGravity.Network;
public static class Program
{
public static readonly CancellationTokenSource CancelToken = new CancellationTokenSource();
private static void ProcessExit(object sender, EventArgs e)
{
Debug.Log("Exiting safely...");
CancelToken.Cancel();
Server.IsRunning = false;
if (Server.PersistenceSaveInterval > 0.0)
{
Server.SavePersistenceDataOnShutdown = true;
}
Server.MainLoopEnded.WaitOne(10000);
}
private static void Main(string[] args)
{
// Exit handlers.
AppDomain.CurrentDomain.ProcessExit += new EventHandler(ProcessExit);
Console.CancelKeyPress += new ConsoleCancelEventHandler(ProcessExit);
// Parse arguments.
Server.InitProperties(args);
// Enable debugger.
Debug.OutputDir = Server.ConfigDir;
Debug.Initialize();
Debug.Log("Starting server with args:", string.Join(" ", args));
bool shutdown = false;
for (int i = 0; i < args.Length; i++)
{
if (args[i].ToLower() == "-shutdown")
{
shutdown = true;
}
else if (args[i].ToLower() == "-scan")
{
ScanInstances();
Environment.Exit(0);
}
else if (args[i].ToLower() == "-cleanup")
{
CleanupAllInstances();
Environment.Exit(0);
}
}
if (shutdown)
{
ShutdownServerInstance();
return;
}
if (!File.Exists(Server.ConfigDir + "GameServer.ini"))
{
Debug.Info("GameServer.ini not found in folder " + Server.ConfigDir);
return;
}
CheckIniFields();
AppDomain.CurrentDomain.UnhandledException += UnhandledExceptionHandler;
try
{
// Start the server.
Server server = new Server();
HiResTime.Start();
server.MainLoop();
HiResTime.Stop();
}
catch (Exception ex)
{
HiResTime.Stop();
Debug.UnformattedMessage("******************** MAIN EXCEPTION ********************");
Debug.Exception(ex);
}
}
private static void CheckIniFields()
{
try
{
Server.Properties.GetProperty<int>("game_port");
}
catch
{
Debug.Error("Invalid 'game_port' field.");
Environment.Exit(0);
}
try
{
Server.Properties.GetProperty<int>("status_port");
}
catch
{
Debug.Error("Invalid 'status_port' field.");
Environment.Exit(0);
}
try
{
Server.Properties.GetProperty<string>("auth_key");
}
catch
{
Debug.Error("Invalid 'auth_key' field.");
Environment.Exit(0);
}
try
{
Server.Properties.GetProperty<string>("http_key");
}
catch
{
Debug.Error("Invalid 'http_key' field.");
Environment.Exit(0);
}
}
private static void CleanupAllInstances()
{
DirectoryInfo dinfo = new DirectoryInfo(".");
foreach (FileInfo file2 in dinfo.EnumerateFiles("*.save"))
{
file2.Delete();
}
string[] directories = Directory.GetDirectories(".");
foreach (string dir in directories)
{
string configDir = Path.GetFileName(dir);
string path = configDir + "/GameServer.ini";
if (!File.Exists(path))
{
continue;
}
dinfo = new DirectoryInfo(configDir);
foreach (FileInfo file in dinfo.EnumerateFiles("*.save"))
{
file.Delete();
}
}
}
private static void ScanInstances()
{
DirectoryInfo dinfo = new DirectoryInfo(".");
foreach (FileInfo file2 in dinfo.EnumerateFiles("Start_*.bat"))
{
file2.Delete();
}
foreach (FileInfo file in dinfo.EnumerateFiles("Stop_*.bat"))
{
file.Delete();
}
string exe = Path.GetFileName(Process.GetCurrentProcess().MainModule.FileName);
string startAll = "start cmd /c call \"Start_DEFAULT.bat\"\r\n";
string stopAll = "start cmd /c call \"Stop_DEFAULT.bat\"\r\n";
File.WriteAllText("Start_DEFAULT.bat", "@TITLE Game Server: DEFAULT \r\n@" + exe);
File.WriteAllText("Stop_DEFAULT.bat", exe + " -shutdown");
string[] directories = Directory.GetDirectories(".");
foreach (string dir in directories)
{
string configDir = Path.GetFileName(dir);
string path = configDir + "/GameServer.ini";
if (File.Exists(path))
{
string start = "@TITLE Game Server: " + configDir + "\r\n@" + exe + " -configdir \"" + configDir + "\"";
string stop = start + " -shutdown";
string startBatFile = "Start_" + configDir + ".bat";
string stopBatFile = "Stop_" + configDir + ".bat";
File.WriteAllText(startBatFile, start);
File.WriteAllText(stopBatFile, stop);
startAll = startAll + "start cmd /c call \"" + startBatFile + "\"\r\n";
stopAll = stopAll + "start cmd /c call \"" + stopBatFile + "\"\r\n";
}
}
File.WriteAllText("Start_ALL.bat", startAll);
File.WriteAllText("Stop_ALL.bat", stopAll);
}
private static void UnhandledExceptionHandler(object sender, UnhandledExceptionEventArgs args)
{
try
{
Debug.Exception((Exception)args.ExceptionObject);
}
catch
{
File.WriteAllText(Server.ConfigDir + "unhandled_exception.txt", ((Exception)args.ExceptionObject).Message + ", " + ((Exception)args.ExceptionObject).StackTrace);
}
finally
{
Environment.Exit(1);
}
}
// Sends a request to the status listener that we should shutdown.
private static void ShutdownServerInstance()
{
int StatusPort = Server.Properties.GetProperty<int>("status_port");
if (StatusPort <= 0)
{
return;
}
try
{
TcpClient tcpClient = new TcpClient();
IAsyncResult ar = tcpClient.BeginConnect("127.0.0.1", StatusPort, null, null);
WaitHandle wh = ar.AsyncWaitHandle;
try
{
if (!ar.AsyncWaitHandle.WaitOne(TimeSpan.FromSeconds(2.0), exitContext: false))
{
tcpClient.Close();
throw new TimeoutException();
}
tcpClient.EndConnect(ar);
}
finally
{
wh.Close();
}
NetworkStream ns = null;
try
{
ns = tcpClient.GetStream();
}
catch
{
return;
}
ns.ReadTimeout = 1000;
ns.WriteTimeout = 1000;
ServerShutDownMessage msg = new ServerShutDownMessage();
byte[] buffer = Serializer.Package(msg);
ns.Write(buffer, 0, buffer.Length);
ns.Flush();
ns.Close();
tcpClient.Close();
}
catch
{
}
}
}