Refactor and stuff
This commit is contained in:
@@ -1,281 +0,0 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Collections.ObjectModel;
|
||||
using System.ComponentModel;
|
||||
using System.Diagnostics;
|
||||
using System.Linq;
|
||||
using System.Reflection;
|
||||
using System.Runtime.CompilerServices;
|
||||
using System.Text;
|
||||
using System.Threading;
|
||||
using System.Threading.Tasks;
|
||||
using System.Windows;
|
||||
using System.Windows.Data;
|
||||
using System.Windows.Threading;
|
||||
using Torch;
|
||||
using Sandbox;
|
||||
using Sandbox.Engine.Multiplayer;
|
||||
using Sandbox.Game.Multiplayer;
|
||||
using Sandbox.Game.World;
|
||||
using SteamSDK;
|
||||
using Torch.Server.ViewModels;
|
||||
using VRage.Game;
|
||||
using VRage.Library.Collections;
|
||||
using VRage.Network;
|
||||
using VRage.Utils;
|
||||
|
||||
namespace Torch.Server
|
||||
{
|
||||
/// <summary>
|
||||
/// Provides a proxy to the game's multiplayer-related functions.
|
||||
/// </summary>
|
||||
public class MultiplayerManager
|
||||
{
|
||||
public event Action<PlayerInfo> PlayerJoined;
|
||||
public event Action<PlayerInfo> PlayerLeft;
|
||||
public event Action<ChatItemInfo> ChatMessageReceived;
|
||||
|
||||
public MTObservableCollection<PlayerInfo> PlayersView { get; } = new MTObservableCollection<PlayerInfo>();
|
||||
public MTObservableCollection<ChatItemInfo> ChatView { get; } = new MTObservableCollection<ChatItemInfo>();
|
||||
public PlayerInfo LocalPlayer { get; private set; }
|
||||
|
||||
private TorchServer _server;
|
||||
|
||||
internal MultiplayerManager(TorchServer server)
|
||||
{
|
||||
_server = server;
|
||||
_server.Server.SessionLoaded += OnSessionLoaded;
|
||||
}
|
||||
|
||||
public void KickPlayer(ulong steamId) => _server.Server.BeginGameAction(() => MyMultiplayer.Static.KickClient(steamId));
|
||||
|
||||
public void BanPlayer(ulong steamId, bool banned = true)
|
||||
{
|
||||
_server.Server.BeginGameAction(() =>
|
||||
{
|
||||
MyMultiplayer.Static.BanClient(steamId, banned);
|
||||
if (_gameOwnerIds.ContainsKey(steamId))
|
||||
MyMultiplayer.Static.BanClient(_gameOwnerIds[steamId], banned);
|
||||
});
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Send a message in chat.
|
||||
/// </summary>
|
||||
public void SendMessage(string message)
|
||||
{
|
||||
MyMultiplayer.Static.SendChatMessage(message);
|
||||
ChatView.Add(new ChatItemInfo(LocalPlayer, message));
|
||||
}
|
||||
|
||||
private void OnSessionLoaded()
|
||||
{
|
||||
LocalPlayer = new PlayerInfo(MyMultiplayer.Static.ServerId) { Name = "Server", State = ConnectionState.Connected };
|
||||
|
||||
MyMultiplayer.Static.ChatMessageReceived += OnChatMessage;
|
||||
MyMultiplayer.Static.ClientKicked += OnClientKicked;
|
||||
MyMultiplayer.Static.ClientLeft += OnClientLeft;
|
||||
MySession.Static.Players.PlayerRequesting += OnPlayerRequesting;
|
||||
|
||||
//TODO: Move these with the methods?
|
||||
RemoveHandlers();
|
||||
SteamSDK.SteamServerAPI.Instance.GameServer.ValidateAuthTicketResponse += ValidateAuthTicketResponse;
|
||||
SteamSDK.SteamServerAPI.Instance.GameServer.UserGroupStatus += UserGroupStatus;
|
||||
_members = (List<ulong>)typeof(MyDedicatedServerBase).GetField("m_members", BindingFlags.NonPublic | BindingFlags.Instance).GetValue(MyMultiplayer.Static);
|
||||
_waitingForGroup = (HashSet<ulong>)typeof(MyDedicatedServerBase).GetField("m_waitingForGroup", BindingFlags.NonPublic | BindingFlags.Instance).GetValue(MyMultiplayer.Static);
|
||||
}
|
||||
|
||||
private void OnChatMessage(ulong steamId, string message, ChatEntryTypeEnum chatType)
|
||||
{
|
||||
var player = PlayersView.FirstOrDefault(p => p.SteamId == steamId);
|
||||
if (player == null || player == LocalPlayer)
|
||||
return;
|
||||
|
||||
var info = new ChatItemInfo(player, message);
|
||||
ChatView.Add(info);
|
||||
ChatMessageReceived?.Invoke(info);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Invoked when a client logs in and hits the respawn screen.
|
||||
/// </summary>
|
||||
private void OnPlayerRequesting(PlayerRequestArgs args)
|
||||
{
|
||||
var steamId = args.PlayerId.SteamId;
|
||||
var player = new PlayerInfo(steamId) {State = ConnectionState.Connected};
|
||||
PlayersView.Add(player);
|
||||
PlayerJoined?.Invoke(player);
|
||||
}
|
||||
|
||||
private void OnClientKicked(ulong steamId)
|
||||
{
|
||||
OnClientLeft(steamId, ChatMemberStateChangeEnum.Kicked);
|
||||
}
|
||||
|
||||
private void OnClientLeft(ulong steamId, ChatMemberStateChangeEnum stateChange)
|
||||
{
|
||||
var player = PlayersView.FirstOrDefault(p => p.SteamId == steamId);
|
||||
|
||||
if (player == null)
|
||||
return;
|
||||
|
||||
player.State = (ConnectionState)stateChange;
|
||||
PlayersView.Remove(player);
|
||||
PlayerLeft?.Invoke(player);
|
||||
}
|
||||
|
||||
//TODO: Split the following into a new file?
|
||||
//These methods override some Keen code to allow us full control over client authentication.
|
||||
//This lets us have a server set to private (admins only) or friends (friends of all listed admins)
|
||||
private List<ulong> _members;
|
||||
private HashSet<ulong> _waitingForGroup;
|
||||
private HashSet<ulong> _waitingForFriends;
|
||||
private Dictionary<ulong, ulong> _gameOwnerIds = new Dictionary<ulong, ulong>();
|
||||
/// <summary>
|
||||
/// Removes Keen's hooks into some Steam events so we have full control over client authentication
|
||||
/// </summary>
|
||||
private static void RemoveHandlers()
|
||||
{
|
||||
var eventField = typeof(GameServer).GetField("<backing_store>ValidateAuthTicketResponse", BindingFlags.NonPublic | BindingFlags.Instance);
|
||||
var eventDel = eventField?.GetValue(SteamServerAPI.Instance.GameServer) as MulticastDelegate;
|
||||
if (eventDel != null)
|
||||
{
|
||||
foreach (var handle in eventDel.GetInvocationList())
|
||||
{
|
||||
if (handle.Method.Name == "GameServer_ValidateAuthTicketResponse")
|
||||
{
|
||||
SteamServerAPI.Instance.GameServer.ValidateAuthTicketResponse -= handle as ValidateAuthTicketResponse;
|
||||
}
|
||||
}
|
||||
}
|
||||
eventField = typeof(GameServer).GetField("<backing_store>UserGroupStatus", BindingFlags.NonPublic | BindingFlags.Instance);
|
||||
eventDel = eventField?.GetValue(SteamServerAPI.Instance.GameServer) as MulticastDelegate;
|
||||
if (eventDel != null)
|
||||
{
|
||||
foreach (var handle in eventDel.GetInvocationList())
|
||||
{
|
||||
if (handle.Method.Name == "GameServer_UserGroupStatus")
|
||||
{
|
||||
SteamServerAPI.Instance.GameServer.UserGroupStatus -= handle as UserGroupStatus;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
//Largely copied from SE
|
||||
private void ValidateAuthTicketResponse(ulong steamID, AuthSessionResponseEnum response, ulong ownerSteamID)
|
||||
{
|
||||
MyLog.Default.WriteLineAndConsole($"{Logger.Prefix} Server ValidateAuthTicketResponse ({response}), owner: {ownerSteamID}");
|
||||
|
||||
if (steamID != ownerSteamID)
|
||||
{
|
||||
MyLog.Default.WriteLineAndConsole($"User {steamID} is using a game owned by {ownerSteamID}. Tracking...");
|
||||
_gameOwnerIds[steamID] = ownerSteamID;
|
||||
|
||||
if (MySandboxGame.ConfigDedicated.Banned.Contains(ownerSteamID))
|
||||
{
|
||||
MyLog.Default.WriteLineAndConsole($"Game owner {ownerSteamID} is banned. Banning and rejecting client {steamID}...");
|
||||
UserRejected(steamID, JoinResult.BannedByAdmins);
|
||||
BanPlayer(steamID, true);
|
||||
}
|
||||
}
|
||||
|
||||
if (response == AuthSessionResponseEnum.OK)
|
||||
{
|
||||
if (MySession.Static.MaxPlayers > 0 && _members.Count - 1 >= MySession.Static.MaxPlayers)
|
||||
{
|
||||
UserRejected(steamID, JoinResult.ServerFull);
|
||||
}
|
||||
else if (MySandboxGame.ConfigDedicated.Administrators.Contains(steamID.ToString()) || MySandboxGame.ConfigDedicated.Administrators.Contains(MyDedicatedServerBase.ConvertSteamIDFrom64(steamID)))
|
||||
{
|
||||
UserAccepted(steamID);
|
||||
}
|
||||
else if (MySandboxGame.ConfigDedicated.GroupID == 0)
|
||||
{
|
||||
switch (MySession.Static.OnlineMode)
|
||||
{
|
||||
case MyOnlineModeEnum.PUBLIC:
|
||||
UserAccepted(steamID);
|
||||
break;
|
||||
case MyOnlineModeEnum.PRIVATE:
|
||||
UserRejected(steamID, JoinResult.NotInGroup);
|
||||
break;
|
||||
case MyOnlineModeEnum.FRIENDS:
|
||||
//TODO: actually verify friendship
|
||||
UserRejected(steamID, JoinResult.NotInGroup);
|
||||
break;
|
||||
}
|
||||
}
|
||||
else if (SteamServerAPI.Instance.GetAccountType(MySandboxGame.ConfigDedicated.GroupID) != AccountType.Clan)
|
||||
{
|
||||
UserRejected(steamID, JoinResult.GroupIdInvalid);
|
||||
}
|
||||
else if (SteamServerAPI.Instance.GameServer.RequestGroupStatus(steamID, MySandboxGame.ConfigDedicated.GroupID))
|
||||
{
|
||||
// Returns false when there's no connection to Steam
|
||||
_waitingForGroup.Add(steamID);
|
||||
}
|
||||
else
|
||||
{
|
||||
UserRejected(steamID, JoinResult.SteamServersOffline);
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
JoinResult joinResult = JoinResult.TicketInvalid;
|
||||
switch (response)
|
||||
{
|
||||
case AuthSessionResponseEnum.AuthTicketCanceled:
|
||||
joinResult = JoinResult.TicketCanceled;
|
||||
break;
|
||||
case AuthSessionResponseEnum.AuthTicketInvalidAlreadyUsed:
|
||||
joinResult = JoinResult.TicketAlreadyUsed;
|
||||
break;
|
||||
case AuthSessionResponseEnum.LoggedInElseWhere:
|
||||
joinResult = JoinResult.LoggedInElseWhere;
|
||||
break;
|
||||
case AuthSessionResponseEnum.NoLicenseOrExpired:
|
||||
joinResult = JoinResult.NoLicenseOrExpired;
|
||||
break;
|
||||
case AuthSessionResponseEnum.UserNotConnectedToSteam:
|
||||
joinResult = JoinResult.UserNotConnected;
|
||||
break;
|
||||
case AuthSessionResponseEnum.VACBanned:
|
||||
joinResult = JoinResult.VACBanned;
|
||||
break;
|
||||
case AuthSessionResponseEnum.VACCheckTimedOut:
|
||||
joinResult = JoinResult.VACCheckTimedOut;
|
||||
break;
|
||||
}
|
||||
|
||||
UserRejected(steamID, joinResult);
|
||||
}
|
||||
}
|
||||
|
||||
private void UserGroupStatus(ulong userId, ulong groupId, bool member, bool officer)
|
||||
{
|
||||
if (groupId == MySandboxGame.ConfigDedicated.GroupID && _waitingForGroup.Remove(userId))
|
||||
{
|
||||
if (member || officer)
|
||||
{
|
||||
UserAccepted(userId);
|
||||
}
|
||||
else
|
||||
{
|
||||
UserRejected(userId, JoinResult.NotInGroup);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private void UserAccepted(ulong steamId)
|
||||
{
|
||||
//TODO: Raise user joined event here
|
||||
typeof(MyDedicatedServerBase).GetMethod("UserAccepted", BindingFlags.NonPublic | BindingFlags.Instance).Invoke(MyMultiplayer.Static, new object[] {steamId});
|
||||
}
|
||||
|
||||
private void UserRejected(ulong steamId, JoinResult reason)
|
||||
{
|
||||
typeof(MyDedicatedServerBase).GetMethod("UserRejected", BindingFlags.NonPublic | BindingFlags.Instance).Invoke(MyMultiplayer.Static, new object[] {steamId, reason});
|
||||
}
|
||||
}
|
||||
}
|
@@ -9,26 +9,29 @@ using System.Threading.Tasks;
|
||||
using System.Windows;
|
||||
using System.Windows.Threading;
|
||||
using Torch;
|
||||
using Torch.API;
|
||||
|
||||
namespace Torch.Server
|
||||
{
|
||||
public static class Program
|
||||
{
|
||||
private static TorchServer _server = new TorchServer();
|
||||
private static readonly ITorchServer _server = new TorchServer();
|
||||
private static TorchUI _ui;
|
||||
|
||||
[STAThread]
|
||||
public static void Main(string[] args)
|
||||
{
|
||||
_server.Init();
|
||||
_server.Server.RunArgs = new[] { "-console" };
|
||||
_server.RunArgs = new[] { "-console" };
|
||||
_ui = new TorchUI(_server);
|
||||
|
||||
if (args.Contains("-nogui"))
|
||||
_server.Server.StartServer();
|
||||
_server.Start();
|
||||
else
|
||||
StartUI();
|
||||
|
||||
if (args.Contains("-autostart") && !_server.Server.IsRunning)
|
||||
_server.Server.StartServerThread();
|
||||
if (args.Contains("-autostart") && !_server.IsRunning)
|
||||
new Thread(() => _server.Start()).Start();
|
||||
|
||||
Dispatcher.Run();
|
||||
}
|
||||
@@ -36,12 +39,12 @@ namespace Torch.Server
|
||||
public static void StartUI()
|
||||
{
|
||||
Thread.CurrentThread.Name = "UI Thread";
|
||||
_server.UI.Show();
|
||||
_ui.Show();
|
||||
}
|
||||
|
||||
public static void FullRestart()
|
||||
{
|
||||
_server.Server.StopServer();
|
||||
_server.Stop();
|
||||
Process.Start("TorchServer.exe", "-autostart");
|
||||
Environment.Exit(1);
|
||||
}
|
||||
|
@@ -13,6 +13,7 @@ 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;
|
||||
@@ -20,7 +21,7 @@ using VRage.Profiler;
|
||||
|
||||
namespace Torch.Server
|
||||
{
|
||||
public class ServerManager : IDisposable
|
||||
public class TorchServer : ITorchServer
|
||||
{
|
||||
public Thread ServerThread { get; private set; }
|
||||
public string[] RunArgs { get; set; } = new string[0];
|
||||
@@ -31,12 +32,12 @@ namespace Torch.Server
|
||||
|
||||
private readonly ManualResetEvent _stopHandle = new ManualResetEvent(false);
|
||||
|
||||
internal ServerManager()
|
||||
internal TorchServer()
|
||||
{
|
||||
MySession.OnLoading += OnSessionLoading;
|
||||
}
|
||||
|
||||
public void InitSandbox()
|
||||
public void Init()
|
||||
{
|
||||
SpaceEngineersGame.SetupBasicGameInfo();
|
||||
SpaceEngineersGame.SetupPerGameSettings();
|
||||
@@ -53,7 +54,7 @@ namespace Torch.Server
|
||||
SpaceEngineersGame.SetupBasicGameInfo();
|
||||
SpaceEngineersGame.SetupPerGameSettings();
|
||||
};
|
||||
int? gameVersion = MyPerGameSettings.BasicGameInfo.GameVersion;
|
||||
var gameVersion = MyPerGameSettings.BasicGameInfo.GameVersion;
|
||||
MyFinalBuildConstants.APP_VERSION = gameVersion ?? 0;
|
||||
}
|
||||
|
||||
@@ -150,14 +151,14 @@ namespace Torch.Server
|
||||
return;
|
||||
}
|
||||
|
||||
ServerThread = new Thread(StartServer);
|
||||
ServerThread = new Thread(Start);
|
||||
ServerThread.Start();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Start server on the current thread.
|
||||
/// </summary>
|
||||
public void StartServer()
|
||||
public void Start()
|
||||
{
|
||||
IsRunning = true;
|
||||
Logger.Write("Starting server.");
|
||||
@@ -171,12 +172,12 @@ namespace Torch.Server
|
||||
/// <summary>
|
||||
/// Stop the server.
|
||||
/// </summary>
|
||||
public void StopServer()
|
||||
public void Stop()
|
||||
{
|
||||
if (Thread.CurrentThread.ManagedThreadId != ServerThread?.ManagedThreadId)
|
||||
{
|
||||
Logger.Write("Requesting server stop.");
|
||||
MySandboxGame.Static.Invoke(StopServer);
|
||||
MySandboxGame.Static.Invoke(Stop);
|
||||
_stopHandle.WaitOne();
|
||||
return;
|
||||
}
|
||||
@@ -204,11 +205,5 @@ namespace Torch.Server
|
||||
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();
|
||||
}
|
||||
|
||||
public void Dispose()
|
||||
{
|
||||
if (IsRunning)
|
||||
StopServer();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@@ -111,20 +111,17 @@
|
||||
<Compile Include="Views\ChatControl.xaml.cs">
|
||||
<DependentUpon>ChatControl.xaml</DependentUpon>
|
||||
</Compile>
|
||||
<Compile Include="ViewModels\ChatItemInfo.cs" />
|
||||
<Compile Include="Views\ModsControl.xaml.cs">
|
||||
<DependentUpon>ModsControl.xaml</DependentUpon>
|
||||
</Compile>
|
||||
<Compile Include="Views\PistonUI.xaml.cs">
|
||||
<DependentUpon>PistonUI.xaml</DependentUpon>
|
||||
<Compile Include="Views\TorchUI.xaml.cs">
|
||||
<DependentUpon>TorchUI.xaml</DependentUpon>
|
||||
</Compile>
|
||||
<Compile Include="MultiplayerManager.cs" />
|
||||
<Compile Include="TorchServer.cs" />
|
||||
<Compile Include="Views\PlayerListControl.xaml.cs">
|
||||
<DependentUpon>PlayerListControl.xaml</DependentUpon>
|
||||
</Compile>
|
||||
<Compile Include="Program.cs" />
|
||||
<Compile Include="ServerManager.cs" />
|
||||
<Compile Include="TorchServer.cs" />
|
||||
<Compile Include="Views\PropertyGrid.xaml.cs">
|
||||
<DependentUpon>PropertyGrid.xaml</DependentUpon>
|
||||
</Compile>
|
||||
@@ -181,7 +178,7 @@
|
||||
<SubType>Designer</SubType>
|
||||
<Generator>MSBuild:Compile</Generator>
|
||||
</Page>
|
||||
<Page Include="Views\PistonUI.xaml">
|
||||
<Page Include="Views\TorchUI.xaml">
|
||||
<Generator>MSBuild:Compile</Generator>
|
||||
<SubType>Designer</SubType>
|
||||
</Page>
|
||||
|
@@ -1,75 +1,127 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Drawing;
|
||||
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 Torch.Launcher;
|
||||
using VRage.Dedicated;
|
||||
using VRage.Game;
|
||||
using VRage.Game.SessionComponents;
|
||||
using VRage.Profiler;
|
||||
|
||||
namespace Torch.Server
|
||||
{
|
||||
/// <summary>
|
||||
/// Entry point for all Piston server functionality.
|
||||
/// </summary>
|
||||
public class TorchServer : ITorchServer
|
||||
public class TorchServer : TorchBase, ITorchServer
|
||||
{
|
||||
public ServerManager Server { get; private set; }
|
||||
public MultiplayerManager Multiplayer { get; private set; }
|
||||
public PluginManager Plugins { get; private set; }
|
||||
public PistonUI UI { get; private set; }
|
||||
public Thread ServerThread { get; private set; }
|
||||
public string[] RunArgs { get; set; } = new string[0];
|
||||
public bool IsRunning { get; private set; }
|
||||
|
||||
private bool _init;
|
||||
public event Action SessionLoading;
|
||||
|
||||
public void Start()
|
||||
private readonly ManualResetEvent _stopHandle = new ManualResetEvent(false);
|
||||
|
||||
internal TorchServer()
|
||||
{
|
||||
if (!_init)
|
||||
Init();
|
||||
|
||||
Server.StartServer();
|
||||
}
|
||||
|
||||
public void Stop()
|
||||
{
|
||||
Server.StopServer();
|
||||
}
|
||||
|
||||
public void Init()
|
||||
{
|
||||
if (_init)
|
||||
return;
|
||||
|
||||
Logger.Write("Initializing Torch");
|
||||
_init = true;
|
||||
Server = new ServerManager();
|
||||
Multiplayer = new MultiplayerManager(this);
|
||||
MySession.OnLoading += OnSessionLoading;
|
||||
Plugins = new PluginManager();
|
||||
UI = new PistonUI();
|
||||
|
||||
Server.SessionLoaded += Plugins.LoadAllPlugins;
|
||||
Server.InitSandbox();
|
||||
SteamHelper.Init();
|
||||
UI.PropGrid.SetObject(MySandboxGame.ConfigDedicated);
|
||||
}
|
||||
|
||||
public void Reset()
|
||||
public override void Init()
|
||||
{
|
||||
Logger.Write("Resetting Torch");
|
||||
Server.Dispose();
|
||||
UI.Close();
|
||||
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;
|
||||
}
|
||||
|
||||
Server = null;
|
||||
Multiplayer = null;
|
||||
Plugins = null;
|
||||
UI = null;
|
||||
_init = false;
|
||||
private void OnSessionLoading()
|
||||
{
|
||||
SessionLoading?.Invoke();
|
||||
MySession.Static.OnReady += OnSessionReady;
|
||||
}
|
||||
|
||||
private void OnSessionReady()
|
||||
{
|
||||
Plugins.LoadAllPlugins();
|
||||
InvokeSessionLoaded();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Start server on the current thread.
|
||||
/// </summary>
|
||||
public override void Start()
|
||||
{
|
||||
if (IsRunning)
|
||||
throw new InvalidOperationException("Server is already running.");
|
||||
|
||||
IsRunning = true;
|
||||
Logger.Write("Starting server.");
|
||||
|
||||
if (MySandboxGame.Log.LogEnabled)
|
||||
MySandboxGame.Log.Close();
|
||||
|
||||
DedicatedServer.Run<MyObjectBuilder_SessionSettings>(RunArgs);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Stop the server.
|
||||
/// </summary>
|
||||
public override 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();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@@ -1,38 +0,0 @@
|
||||
using System;
|
||||
|
||||
namespace Torch.Server.ViewModels
|
||||
{
|
||||
public class ChatItemInfo : ViewModel
|
||||
{
|
||||
private PlayerInfo _sender;
|
||||
private string _message;
|
||||
private DateTime _timestamp;
|
||||
|
||||
public PlayerInfo Sender
|
||||
{
|
||||
get { return _sender; }
|
||||
set { _sender = value; OnPropertyChanged(); }
|
||||
}
|
||||
|
||||
public string Message
|
||||
{
|
||||
get { return _message; }
|
||||
set { _message = value; OnPropertyChanged(); }
|
||||
}
|
||||
|
||||
public DateTime Timestamp
|
||||
{
|
||||
get { return _timestamp; }
|
||||
set { _timestamp = value; OnPropertyChanged(); }
|
||||
}
|
||||
|
||||
public string Time => Timestamp.ToShortTimeString();
|
||||
|
||||
public ChatItemInfo(PlayerInfo sender, string message)
|
||||
{
|
||||
_sender = sender;
|
||||
_message = message;
|
||||
_timestamp = DateTime.Now;
|
||||
}
|
||||
}
|
||||
}
|
@@ -18,6 +18,7 @@ using Sandbox;
|
||||
using Sandbox.Engine.Multiplayer;
|
||||
using Sandbox.Game.World;
|
||||
using SteamSDK;
|
||||
using Torch.API;
|
||||
|
||||
namespace Torch.Server
|
||||
{
|
||||
@@ -26,10 +27,27 @@ namespace Torch.Server
|
||||
/// </summary>
|
||||
public partial class ChatControl : UserControl
|
||||
{
|
||||
private ITorchServer _server;
|
||||
|
||||
public ChatControl()
|
||||
{
|
||||
InitializeComponent();
|
||||
ChatItems.ItemsSource = TorchServer.Multiplayer.ChatView;
|
||||
}
|
||||
|
||||
public void BindServer(ITorchServer server)
|
||||
{
|
||||
_server = server;
|
||||
server.Multiplayer.MessageReceived += Refresh;
|
||||
Refresh();
|
||||
}
|
||||
|
||||
private void Refresh(IChatItem chatItem = null)
|
||||
{
|
||||
Dispatcher.Invoke(() =>
|
||||
{
|
||||
ChatItems.ItemsSource = null;
|
||||
ChatItems.ItemsSource = _server.Multiplayer.Chat;
|
||||
});
|
||||
}
|
||||
|
||||
private void SendButton_Click(object sender, RoutedEventArgs e)
|
||||
@@ -47,7 +65,7 @@ namespace Torch.Server
|
||||
{
|
||||
//Can't use Message.Text directly because of object ownership in WPF.
|
||||
var text = Message.Text;
|
||||
TorchServer.Multiplayer.SendMessage(text);
|
||||
_server.Multiplayer.SendMessage(text);
|
||||
Message.Text = "";
|
||||
}
|
||||
}
|
||||
|
@@ -18,6 +18,7 @@ using Sandbox.Engine.Multiplayer;
|
||||
using Sandbox.Game.Multiplayer;
|
||||
using Sandbox.Game.World;
|
||||
using SteamSDK;
|
||||
using Torch.API;
|
||||
|
||||
namespace Torch.Server
|
||||
{
|
||||
@@ -26,27 +27,45 @@ namespace Torch.Server
|
||||
/// </summary>
|
||||
public partial class PlayerListControl : UserControl
|
||||
{
|
||||
private ITorchServer _server;
|
||||
|
||||
public PlayerListControl()
|
||||
{
|
||||
InitializeComponent();
|
||||
PlayerList.ItemsSource = TorchServer.Multiplayer.PlayersView;
|
||||
}
|
||||
|
||||
public void BindServer(ITorchServer server)
|
||||
{
|
||||
_server = server;
|
||||
server.Multiplayer.PlayerJoined += Refresh;
|
||||
server.Multiplayer.PlayerLeft += Refresh;
|
||||
Refresh();
|
||||
}
|
||||
|
||||
private void Refresh(IPlayer player = null)
|
||||
{
|
||||
Dispatcher.Invoke(() =>
|
||||
{
|
||||
PlayerList.ItemsSource = null;
|
||||
PlayerList.ItemsSource = _server.Multiplayer.Players;
|
||||
});
|
||||
}
|
||||
|
||||
private void KickButton_Click(object sender, RoutedEventArgs e)
|
||||
{
|
||||
var player = PlayerList.SelectedItem as PlayerInfo;
|
||||
var player = PlayerList.SelectedItem as Player;
|
||||
if (player != null)
|
||||
{
|
||||
TorchServer.Multiplayer.KickPlayer(player.SteamId);
|
||||
_server.Multiplayer.KickPlayer(player.SteamId);
|
||||
}
|
||||
}
|
||||
|
||||
private void BanButton_Click(object sender, RoutedEventArgs e)
|
||||
{
|
||||
var player = PlayerList.SelectedItem as PlayerInfo;
|
||||
var player = PlayerList.SelectedItem as Player;
|
||||
if (player != null)
|
||||
{
|
||||
TorchServer.Multiplayer.BanPlayer(player.SteamId);
|
||||
_server.Multiplayer.BanPlayer(player.SteamId);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@@ -1,4 +1,4 @@
|
||||
<Window x:Class="Torch.Server.PistonUI"
|
||||
<Window x:Class="Torch.Server.TorchUI"
|
||||
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
|
||||
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
|
||||
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
|
@@ -5,6 +5,7 @@ using System.Diagnostics;
|
||||
using System.IO;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading;
|
||||
using System.Threading.Tasks;
|
||||
using System.Timers;
|
||||
using System.Windows;
|
||||
@@ -16,14 +17,17 @@ using System.Windows.Media;
|
||||
using System.Windows.Media.Imaging;
|
||||
using System.Windows.Navigation;
|
||||
using System.Windows.Shapes;
|
||||
using Torch.API;
|
||||
using Timer = System.Timers.Timer;
|
||||
|
||||
namespace Torch.Server
|
||||
{
|
||||
/// <summary>
|
||||
/// Interaction logic for PistonUI.xaml
|
||||
/// Interaction logic for TorchUI.xaml
|
||||
/// </summary>
|
||||
public partial class PistonUI : Window
|
||||
public partial class TorchUI : Window
|
||||
{
|
||||
private ITorchServer _server;
|
||||
private DateTime _startTime;
|
||||
private readonly Timer _uiUpdate = new Timer
|
||||
{
|
||||
@@ -31,13 +35,15 @@ namespace Torch.Server
|
||||
AutoReset = true,
|
||||
};
|
||||
|
||||
public PistonUI()
|
||||
public TorchUI(ITorchServer server)
|
||||
{
|
||||
_server = server;
|
||||
InitializeComponent();
|
||||
_startTime = DateTime.Now;
|
||||
_uiUpdate.Elapsed += UiUpdate_Elapsed;
|
||||
|
||||
TabControl.Items.Add(new TabItem());
|
||||
Chat.BindServer(server);
|
||||
PlayerList.BindServer(server);
|
||||
}
|
||||
|
||||
private void UiUpdate_Elapsed(object sender, ElapsedEventArgs e)
|
||||
@@ -61,7 +67,7 @@ namespace Torch.Server
|
||||
((Button) sender).IsEnabled = false;
|
||||
BtnStop.IsEnabled = true;
|
||||
_uiUpdate.Start();
|
||||
TorchServer.Server.StartServerThread();
|
||||
new Thread(() => _server.Start()).Start();
|
||||
}
|
||||
|
||||
private void BtnStop_Click(object sender, RoutedEventArgs e)
|
||||
@@ -72,12 +78,12 @@ namespace Torch.Server
|
||||
//HACK: Uncomment when restarting is possible.
|
||||
//BtnStart.IsEnabled = true;
|
||||
_uiUpdate.Stop();
|
||||
TorchServer.Server.StopServer();
|
||||
_server.Stop();
|
||||
}
|
||||
|
||||
protected override void OnClosing(CancelEventArgs e)
|
||||
{
|
||||
TorchServer.Reset();
|
||||
_server.Stop();
|
||||
}
|
||||
|
||||
private void BtnRestart_Click(object sender, RoutedEventArgs e)
|
Reference in New Issue
Block a user