-
Notifications
You must be signed in to change notification settings - Fork 80
Expand file tree
/
Copy pathServerManager.cs
More file actions
209 lines (188 loc) · 6.67 KB
/
ServerManager.cs
File metadata and controls
209 lines (188 loc) · 6.67 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
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows;
using Torch;
using Sandbox;
using Sandbox.Engine.Multiplayer;
using Sandbox.Game;
using Sandbox.Game.World;
using SpaceEngineers.Game;
using Torch.API;
using VRage.Dedicated;
using VRage.Game;
using VRage.Game.SessionComponents;
using VRage.Profiler;
namespace Torch.Server
{
public class TorchServer : ITorchServer
{
public Thread ServerThread { get; private set; }
public string[] RunArgs { get; set; } = new string[0];
public bool IsRunning { get; private set; }
public event Action SessionLoading;
public event Action SessionLoaded;
private readonly ManualResetEvent _stopHandle = new ManualResetEvent(false);
internal TorchServer()
{
MySession.OnLoading += OnSessionLoading;
}
public void Init()
{
SpaceEngineersGame.SetupBasicGameInfo();
SpaceEngineersGame.SetupPerGameSettings();
MyPerGameSettings.SendLogToKeen = false;
MyPerServerSettings.GameName = MyPerGameSettings.GameName;
MyPerServerSettings.GameNameSafe = MyPerGameSettings.GameNameSafe;
MyPerServerSettings.GameDSName = MyPerServerSettings.GameNameSafe + "Dedicated";
MyPerServerSettings.GameDSDescription = "Your place for space engineering, destruction and exploring.";
MySessionComponentExtDebug.ForceDisable = true;
MyPerServerSettings.AppId = 244850u;
ConfigForm<MyObjectBuilder_SessionSettings>.GameAttributes = Game.SpaceEngineers;
ConfigForm<MyObjectBuilder_SessionSettings>.OnReset = delegate
{
SpaceEngineersGame.SetupBasicGameInfo();
SpaceEngineersGame.SetupPerGameSettings();
};
var gameVersion = MyPerGameSettings.BasicGameInfo.GameVersion;
MyFinalBuildConstants.APP_VERSION = gameVersion ?? 0;
}
/// <summary>
/// Invokes an action on the game thread and blocks until completion
/// </summary>
/// <param name="action"></param>
public void GameAction(Action action)
{
try
{
if (Thread.CurrentThread == MySandboxGame.Static.UpdateThread)
{
action();
}
else
{
AutoResetEvent e = new AutoResetEvent(false);
MySandboxGame.Static.Invoke(() =>
{
try
{
action();
}
catch (Exception ex)
{
//log
}
finally
{
e.Set();
}
});
//timeout so we don't accidentally hang the server
e.WaitOne(60000);
}
}
catch (Exception ex)
{
//we need a logger :(
}
}
/// <summary>
/// Queues an action for invocation on the game thread and optionally runs a callback on completion
/// </summary>
/// <param name="action"></param>
/// <param name="callback"></param>
/// <param name="state"></param>
public void BeginGameAction(Action action, Action<object> callback = null, object state = null)
{
try
{
if (Thread.CurrentThread == MySandboxGame.Static.UpdateThread)
{
action();
}
else
{
Task.Run(() =>
{
GameAction(action);
callback?.Invoke(state);
});
}
}
catch (Exception ex)
{
// log
}
}
private void OnSessionLoading()
{
SessionLoading?.Invoke();
MySession.Static.OnReady += OnSessionReady;
}
private void OnSessionReady()
{
SessionLoaded?.Invoke();
}
/// <summary>
/// Start server on a new thread.
/// </summary>
public void StartServerThread()
{
if (ServerThread?.IsAlive ?? false)
{
Logger.Write("Cannot start the server because it's already running.");
return;
}
ServerThread = new Thread(Start);
ServerThread.Start();
}
/// <summary>
/// Start server on the current thread.
/// </summary>
public void Start()
{
IsRunning = true;
Logger.Write("Starting server.");
if (MySandboxGame.Log.LogEnabled)
MySandboxGame.Log.Close();
DedicatedServer.Run<MyObjectBuilder_SessionSettings>(RunArgs);
}
/// <summary>
/// Stop the server.
/// </summary>
public void Stop()
{
if (Thread.CurrentThread.ManagedThreadId != ServerThread?.ManagedThreadId)
{
Logger.Write("Requesting server stop.");
MySandboxGame.Static.Invoke(Stop);
_stopHandle.WaitOne();
return;
}
Logger.Write("Stopping server.");
MySession.Static.Save();
MySession.Static.Unload();
MySandboxGame.Static.Exit();
//Unload all the static junk.
//TODO: Finish unloading all server data so it's in a completely clean state.
VRage.FileSystem.MyFileSystem.Reset();
VRage.Input.MyGuiGameControlsHelpers.Reset();
VRage.Input.MyInput.UnloadData();
CleanupProfilers();
Logger.Write("Server stopped.");
_stopHandle.Set();
IsRunning = false;
}
private void CleanupProfilers()
{
typeof(MyRenderProfiler).GetField("m_threadProfiler", BindingFlags.Static | BindingFlags.NonPublic).SetValue(null, null);
typeof(MyRenderProfiler).GetField("m_gpuProfiler", BindingFlags.Static | BindingFlags.NonPublic).SetValue(null, null);
(typeof(MyRenderProfiler).GetField("m_threadProfilers", BindingFlags.Static | BindingFlags.NonPublic).GetValue(null) as List<MyProfiler>).Clear();
}
}
}