From e1538701822209e2967b022d4316d63348000216 Mon Sep 17 00:00:00 2001 From: John Michael Gross Date: Mon, 19 Sep 2016 16:00:38 -0700 Subject: [PATCH] Refactor a bit --- Piston/{ObservableType.cs => ViewModel.cs} | 2 +- PistonServer/ChatItemInfo.cs | 38 ++++++++++++++++++ PistonServer/ConnectionState.cs | 18 +++++++++ PistonServer/PistonServer.cs | 14 ++++--- .../{MainWindow.xaml => PistonUI.xaml} | 0 .../{MainWindow.xaml.cs => PistonUI.xaml.cs} | 22 +++++------ PistonServer/PlayerInfo.cs | 39 +++++++++++++++++++ PistonServer/Program.cs | 3 +- PistonServer/ServerManager.cs | 14 +++++-- 9 files changed, 127 insertions(+), 23 deletions(-) rename Piston/{ObservableType.cs => ViewModel.cs} (89%) create mode 100644 PistonServer/ChatItemInfo.cs create mode 100644 PistonServer/ConnectionState.cs rename PistonServer/{MainWindow.xaml => PistonUI.xaml} (100%) rename PistonServer/{MainWindow.xaml.cs => PistonUI.xaml.cs} (81%) create mode 100644 PistonServer/PlayerInfo.cs diff --git a/Piston/ObservableType.cs b/Piston/ViewModel.cs similarity index 89% rename from Piston/ObservableType.cs rename to Piston/ViewModel.cs index ab2cdbc..9cc7186 100644 --- a/Piston/ObservableType.cs +++ b/Piston/ViewModel.cs @@ -8,7 +8,7 @@ using System.Threading.Tasks; namespace Piston { - public class ObservableType : INotifyPropertyChanged + public class ViewModel : INotifyPropertyChanged { public event PropertyChangedEventHandler PropertyChanged; diff --git a/PistonServer/ChatItemInfo.cs b/PistonServer/ChatItemInfo.cs new file mode 100644 index 0000000..33fdcfc --- /dev/null +++ b/PistonServer/ChatItemInfo.cs @@ -0,0 +1,38 @@ +using System; + +namespace Piston.Server +{ + 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; + } + } +} \ No newline at end of file diff --git a/PistonServer/ConnectionState.cs b/PistonServer/ConnectionState.cs new file mode 100644 index 0000000..e242906 --- /dev/null +++ b/PistonServer/ConnectionState.cs @@ -0,0 +1,18 @@ +using System; + +namespace Piston.Server +{ + /// + /// Identifies a player's current connection state. + /// + [Flags] + public enum ConnectionState + { + Unknown, + Connected = 1, + Left = 2, + Disconnected = 4, + Kicked = 8, + Banned = 16, + } +} \ No newline at end of file diff --git a/PistonServer/PistonServer.cs b/PistonServer/PistonServer.cs index 0b8dc26..bf6ca12 100644 --- a/PistonServer/PistonServer.cs +++ b/PistonServer/PistonServer.cs @@ -12,10 +12,10 @@ namespace Piston.Server /// public static class PistonServer { - public static MainWindow UI; - public static ServerManager Server; - public static MultiplayerManager Multiplayer; - public static PluginManager Plugins; + public static ServerManager Server { get; private set; } + public static MultiplayerManager Multiplayer { get; private set; } + public static PluginManager Plugins { get; private set; } + public static PistonUI UI { get; private set; } private static bool _init; @@ -28,16 +28,20 @@ namespace Piston.Server Server = new ServerManager(); Multiplayer = new MultiplayerManager(Server); Plugins = new PluginManager(); - UI = new MainWindow(); + UI = new PistonUI(); } } public static void Reset() { Logger.Write("Resetting Piston"); + Server.Dispose(); + UI.Close(); + Server = null; Multiplayer = null; Plugins = null; + UI = null; _init = false; } } diff --git a/PistonServer/MainWindow.xaml b/PistonServer/PistonUI.xaml similarity index 100% rename from PistonServer/MainWindow.xaml rename to PistonServer/PistonUI.xaml diff --git a/PistonServer/MainWindow.xaml.cs b/PistonServer/PistonUI.xaml.cs similarity index 81% rename from PistonServer/MainWindow.xaml.cs rename to PistonServer/PistonUI.xaml.cs index b32e0e5..1b23e64 100644 --- a/PistonServer/MainWindow.xaml.cs +++ b/PistonServer/PistonUI.xaml.cs @@ -20,22 +20,22 @@ using System.Windows.Shapes; namespace Piston.Server { /// - /// Interaction logic for MainWindow.xaml + /// Interaction logic for PistonUI.xaml /// - public partial class MainWindow : Window + public partial class PistonUI : Window { - private DateTime startTime; - private Timer uiUpdate = new Timer + private DateTime _startTime; + private readonly Timer _uiUpdate = new Timer { Interval = 1000, AutoReset = true, }; - public MainWindow() + public PistonUI() { InitializeComponent(); - startTime = DateTime.Now; - uiUpdate.Elapsed += UiUpdate_Elapsed; + _startTime = DateTime.Now; + _uiUpdate.Elapsed += UiUpdate_Elapsed; TabControl.Items.Add(new TabItem()); } @@ -48,19 +48,19 @@ namespace Piston.Server private void UpdateUptime() { var currentTime = DateTime.Now; - var uptime = currentTime - startTime; + var uptime = currentTime - _startTime; Dispatcher.Invoke(() => LabelUptime.Content = $"Uptime: {uptime.Days}d {uptime.Hours}h {uptime.Minutes}m"); } private void BtnStart_Click(object sender, RoutedEventArgs e) { - startTime = DateTime.Now; + _startTime = DateTime.Now; Chat.IsEnabled = true; PlayerList.IsEnabled = true; ((Button) sender).IsEnabled = false; BtnStop.IsEnabled = true; - uiUpdate.Start(); + _uiUpdate.Start(); PistonServer.Server.StartServerThread(); } @@ -71,7 +71,7 @@ namespace Piston.Server ((Button) sender).IsEnabled = false; //HACK: Uncomment when restarting is possible. //BtnStart.IsEnabled = true; - uiUpdate.Stop(); + _uiUpdate.Stop(); PistonServer.Server.StopServer(); } diff --git a/PistonServer/PlayerInfo.cs b/PistonServer/PlayerInfo.cs new file mode 100644 index 0000000..6db3e52 --- /dev/null +++ b/PistonServer/PlayerInfo.cs @@ -0,0 +1,39 @@ +using Sandbox.Engine.Multiplayer; + +namespace Piston.Server +{ + /// + /// Stores player information in an observable format. + /// + public class PlayerInfo : ViewModel + { + private ulong _steamId; + private string _name; + private ConnectionState _state; + + public ulong SteamId + { + get { return _steamId; } + set { _steamId = value; OnPropertyChanged(); } + } + + public string Name + { + get { return _name; } + set { _name = value; OnPropertyChanged(); } + } + + public ConnectionState State + { + get { return _state; } + set { _state = value; OnPropertyChanged(); } + } + + public PlayerInfo(ulong steamId) + { + _steamId = steamId; + _name = MyMultiplayer.Static.GetMemberName(steamId); + _state = ConnectionState.Unknown; + } + } +} \ No newline at end of file diff --git a/PistonServer/Program.cs b/PistonServer/Program.cs index ef74d2e..6f1530c 100644 --- a/PistonServer/Program.cs +++ b/PistonServer/Program.cs @@ -21,12 +21,11 @@ namespace Piston.Server PistonServer.Server.RunArgs = new[] { "-console" }; if (args.Contains("-nogui")) - PistonServer.Server.StartServer(); else StartUI(); - if (args.Contains("-autostart") && !PistonServer.Server.Running) + if (args.Contains("-autostart") && !PistonServer.Server.IsRunning) PistonServer.Server.StartServerThread(); Dispatcher.Run(); diff --git a/PistonServer/ServerManager.cs b/PistonServer/ServerManager.cs index 16c1f6b..b2aad47 100644 --- a/PistonServer/ServerManager.cs +++ b/PistonServer/ServerManager.cs @@ -15,11 +15,11 @@ using VRage.Profiler; namespace Piston.Server { - public class ServerManager + public class ServerManager : IDisposable { public Thread ServerThread { get; private set; } public string[] RunArgs { get; set; } = new string[0]; - public bool Running { get; private set; } + public bool IsRunning { get; private set; } public event Action SessionLoading; public event Action SessionLoaded; @@ -70,7 +70,7 @@ namespace Piston.Server /// public void StartServer() { - Running = true; + IsRunning = true; Logger.Write("Starting server."); if (MySandboxGame.Log.LogEnabled) @@ -115,7 +115,7 @@ namespace Piston.Server Logger.Write("Server stopped."); _stopHandle.Set(); - Running = false; + IsRunning = false; } private void CleanupProfilers() @@ -124,5 +124,11 @@ namespace Piston.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).Clear(); } + + public void Dispose() + { + if (IsRunning) + StopServer(); + } } }