Refactor a bit

This commit is contained in:
John Michael Gross
2016-09-19 16:00:38 -07:00
parent 852a5630be
commit e153870182
9 changed files with 127 additions and 23 deletions

View File

@@ -8,7 +8,7 @@ using System.Threading.Tasks;
namespace Piston
{
public class ObservableType : INotifyPropertyChanged
public class ViewModel : INotifyPropertyChanged
{
public event PropertyChangedEventHandler PropertyChanged;

View File

@@ -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;
}
}
}

View File

@@ -0,0 +1,18 @@
using System;
namespace Piston.Server
{
/// <summary>
/// Identifies a player's current connection state.
/// </summary>
[Flags]
public enum ConnectionState
{
Unknown,
Connected = 1,
Left = 2,
Disconnected = 4,
Kicked = 8,
Banned = 16,
}
}

View File

@@ -12,10 +12,10 @@ namespace Piston.Server
/// </summary>
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;
}
}

View File

@@ -20,22 +20,22 @@ using System.Windows.Shapes;
namespace Piston.Server
{
/// <summary>
/// Interaction logic for MainWindow.xaml
/// Interaction logic for PistonUI.xaml
/// </summary>
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();
}

View File

@@ -0,0 +1,39 @@
using Sandbox.Engine.Multiplayer;
namespace Piston.Server
{
/// <summary>
/// Stores player information in an observable format.
/// </summary>
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;
}
}
}

View File

@@ -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();

View File

@@ -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
/// </summary>
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<MyProfiler>).Clear();
}
public void Dispose()
{
if (IsRunning)
StopServer();
}
}
}