From 30b0e37fffc0f24b8ad149f9d08a323eaf2ebce3 Mon Sep 17 00:00:00 2001 From: John Michael Gross Date: Mon, 19 Sep 2016 12:45:44 -0700 Subject: [PATCH] Finish chat/players functionality, refactor logic to non-static classes with a single static entry point, add data binding --- Piston.sln | 6 +- Piston/MTObservableCollection.cs | 37 ++++ Piston/ObservableType.cs | 20 ++ Piston/Piston.csproj | 7 +- Piston/PluginManager.cs | 5 +- PistonAPI/IEnvironmentInfo.cs | 2 +- PistonAPI/IPistonPlugin.cs | 2 +- PistonAPI/IServerControls.cs | 2 +- .../{PistonAPI.csproj => Piston.API.csproj} | 0 PistonAPI/PistonAPI.cs | 2 +- ...stonClient.csproj => Piston.Client.csproj} | 0 PistonServer/ChatControl.xaml | 18 +- PistonServer/ChatControl.xaml.cs | 48 +---- PistonServer/MainWindow.xaml | 11 +- PistonServer/MainWindow.xaml.cs | 17 +- PistonServer/MultiplayerManager.cs | 187 ++++++++++++++++++ ...stonServer.csproj => Piston.Server.csproj} | 8 +- PistonServer/PistonServer.cs | 42 ++++ ...ersControl.xaml => PlayerListControl.xaml} | 17 +- PistonServer/PlayerListControl.xaml.cs | 53 +++++ PistonServer/PlayersControl.xaml.cs | 114 ----------- PistonServer/Program.cs | 23 ++- PistonServer/Properties/Resources.Designer.cs | 2 +- PistonServer/Properties/Settings.Designer.cs | 2 +- PistonServer/ServerManager.cs | 36 ++-- 25 files changed, 449 insertions(+), 212 deletions(-) create mode 100644 Piston/MTObservableCollection.cs create mode 100644 Piston/ObservableType.cs rename PistonAPI/{PistonAPI.csproj => Piston.API.csproj} (100%) rename PistonClient/{PistonClient.csproj => Piston.Client.csproj} (100%) create mode 100644 PistonServer/MultiplayerManager.cs rename PistonServer/{PistonServer.csproj => Piston.Server.csproj} (96%) create mode 100644 PistonServer/PistonServer.cs rename PistonServer/{PlayersControl.xaml => PlayerListControl.xaml} (50%) create mode 100644 PistonServer/PlayerListControl.xaml.cs delete mode 100644 PistonServer/PlayersControl.xaml.cs diff --git a/Piston.sln b/Piston.sln index 6831dd3..e629baa 100644 --- a/Piston.sln +++ b/Piston.sln @@ -5,11 +5,11 @@ VisualStudioVersion = 15.0.25618.0 MinimumVisualStudioVersion = 10.0.40219.1 Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Piston", "Piston\Piston.csproj", "{7E01635C-3B67-472E-BCD6-C5539564F214}" EndProject -Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "PistonAPI", "PistonAPI\PistonAPI.csproj", "{FBA5D932-6254-4A1E-BAF4-E229FA94E3C2}" +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Piston.API", "PistonAPI\Piston.API.csproj", "{FBA5D932-6254-4A1E-BAF4-E229FA94E3C2}" EndProject -Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "PistonClient", "PistonClient\PistonClient.csproj", "{E36DF745-260B-4956-A2E8-09F08B2E7161}" +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Piston.Client", "PistonClient\Piston.Client.csproj", "{E36DF745-260B-4956-A2E8-09F08B2E7161}" EndProject -Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "PistonServer", "PistonServer\PistonServer.csproj", "{CA50886B-7B22-4CD8-93A0-C06F38D4F77D}" +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Piston.Server", "PistonServer\Piston.Server.csproj", "{CA50886B-7B22-4CD8-93A0-C06F38D4F77D}" EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution diff --git a/Piston/MTObservableCollection.cs b/Piston/MTObservableCollection.cs new file mode 100644 index 0000000..ae14bf0 --- /dev/null +++ b/Piston/MTObservableCollection.cs @@ -0,0 +1,37 @@ +using System; +using System.Collections.Generic; +using System.Collections.ObjectModel; +using System.Collections.Specialized; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using System.Windows.Threading; + +namespace Piston +{ + public class MTObservableCollection : ObservableCollection + { + public override event NotifyCollectionChangedEventHandler CollectionChanged; + protected override void OnCollectionChanged(NotifyCollectionChangedEventArgs e) + { + NotifyCollectionChangedEventHandler collectionChanged = CollectionChanged; + if (collectionChanged != null) + foreach (NotifyCollectionChangedEventHandler nh in collectionChanged.GetInvocationList()) + { + var dispObj = nh.Target as DispatcherObject; + + Dispatcher dispatcher = dispObj?.Dispatcher; + if (dispatcher != null && !dispatcher.CheckAccess()) + { + dispatcher.BeginInvoke( + (Action)(() => nh.Invoke(this, + new NotifyCollectionChangedEventArgs(NotifyCollectionChangedAction.Reset))), + DispatcherPriority.DataBind); + continue; + } + + nh.Invoke(this, e); + } + } + } +} diff --git a/Piston/ObservableType.cs b/Piston/ObservableType.cs new file mode 100644 index 0000000..ab2cdbc --- /dev/null +++ b/Piston/ObservableType.cs @@ -0,0 +1,20 @@ +using System; +using System.Collections.Generic; +using System.ComponentModel; +using System.Linq; +using System.Runtime.CompilerServices; +using System.Text; +using System.Threading.Tasks; + +namespace Piston +{ + public class ObservableType : INotifyPropertyChanged + { + public event PropertyChangedEventHandler PropertyChanged; + + protected void OnPropertyChanged([CallerMemberName] string propName = "") + { + PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propName)); + } + } +} diff --git a/Piston/Piston.csproj b/Piston/Piston.csproj index cece99f..72d318d 100644 --- a/Piston/Piston.csproj +++ b/Piston/Piston.csproj @@ -111,17 +111,20 @@ C:\Program Files (x86)\Steam\steamapps\common\SpaceEngineers\Bin64\VRage.Scripting.dll + + + - + {fba5d932-6254-4a1e-baf4-e229fa94e3c2} - PistonAPI + Piston.API diff --git a/Piston/PluginManager.cs b/Piston/PluginManager.cs index 76a3e9d..8449991 100644 --- a/Piston/PluginManager.cs +++ b/Piston/PluginManager.cs @@ -5,7 +5,7 @@ using System.Linq; using System.Reflection; using System.Text; using System.Threading.Tasks; -using PistonAPI; +using Piston.API; using Sandbox; using VRage.Plugins; using VRage.Collections; @@ -17,13 +17,12 @@ namespace Piston { //TODO: Disable reloading if the plugin has static elements because they prevent a full reload. - public static PluginManager Static { get; } = new PluginManager(); public ListReader Plugins => MyPlugins.Plugins; private List _plugins; public const string PluginDir = "Plugins"; - private PluginManager() + public PluginManager() { if (!Directory.Exists(PluginDir)) Directory.CreateDirectory(PluginDir); diff --git a/PistonAPI/IEnvironmentInfo.cs b/PistonAPI/IEnvironmentInfo.cs index 4a227fa..3ea80a6 100644 --- a/PistonAPI/IEnvironmentInfo.cs +++ b/PistonAPI/IEnvironmentInfo.cs @@ -1,4 +1,4 @@ -namespace PistonAPI +namespace Piston.API { public interface IEnvironmentInfo { diff --git a/PistonAPI/IPistonPlugin.cs b/PistonAPI/IPistonPlugin.cs index 97f547c..751531b 100644 --- a/PistonAPI/IPistonPlugin.cs +++ b/PistonAPI/IPistonPlugin.cs @@ -6,7 +6,7 @@ using System.Text; using System.Threading.Tasks; using VRage.Plugins; -namespace PistonAPI +namespace Piston.API { public interface IPistonPlugin : IPlugin { diff --git a/PistonAPI/IServerControls.cs b/PistonAPI/IServerControls.cs index a07ae50..5e13089 100644 --- a/PistonAPI/IServerControls.cs +++ b/PistonAPI/IServerControls.cs @@ -1,6 +1,6 @@ using System.Windows.Controls; -namespace PistonAPI +namespace Piston.API { public interface IServerControls { diff --git a/PistonAPI/PistonAPI.csproj b/PistonAPI/Piston.API.csproj similarity index 100% rename from PistonAPI/PistonAPI.csproj rename to PistonAPI/Piston.API.csproj diff --git a/PistonAPI/PistonAPI.cs b/PistonAPI/PistonAPI.cs index 4d558f8..6fd51c7 100644 --- a/PistonAPI/PistonAPI.cs +++ b/PistonAPI/PistonAPI.cs @@ -5,7 +5,7 @@ using System.Text; using System.Threading.Tasks; using System.Windows.Controls; -namespace PistonAPI +namespace Piston.API { public static class PistonAPI { diff --git a/PistonClient/PistonClient.csproj b/PistonClient/Piston.Client.csproj similarity index 100% rename from PistonClient/PistonClient.csproj rename to PistonClient/Piston.Client.csproj diff --git a/PistonServer/ChatControl.xaml b/PistonServer/ChatControl.xaml index 7f8727a..98da407 100644 --- a/PistonServer/ChatControl.xaml +++ b/PistonServer/ChatControl.xaml @@ -1,15 +1,27 @@ - - + + + + + + + + + + + + + diff --git a/PistonServer/ChatControl.xaml.cs b/PistonServer/ChatControl.xaml.cs index f8434a8..c10f20a 100644 --- a/PistonServer/ChatControl.xaml.cs +++ b/PistonServer/ChatControl.xaml.cs @@ -19,45 +19,17 @@ using Sandbox.Engine.Multiplayer; using Sandbox.Game.World; using SteamSDK; -namespace PistonServer +namespace Piston.Server { /// /// Interaction logic for ChatControl.xaml /// public partial class ChatControl : UserControl { - public event Action MessageEntered; public ChatControl() { InitializeComponent(); - ServerManager.Static.SessionReady += InitChatHandler; - } - - public void InitChatHandler() - { - MyMultiplayer.Static.ChatMessageReceived += MessageReceived; - } - - public void MessageReceived(ulong steamId, string message, ChatEntryTypeEnum chatType) - { - //Messages sent from server loop back around. - if (steamId == MyMultiplayer.Static.ServerId) - return; - - var name = MyMultiplayer.Static.GetMemberName(steamId); - Dispatcher.Invoke(() => AddMessage(name, message), DispatcherPriority.Normal); - } - - public void AddMessage(string sender, string message) - { - Chat.Text += $"{DateTime.Now.ToLongTimeString()} | {sender}: {message}\n"; - Program.UserInterface.Players.RefreshNames(); - } - - public void SendMessage(string message) - { - MyMultiplayer.Static.SendChatMessage(message); - Dispatcher.Invoke(() => AddMessage("Server", message)); + ChatItems.ItemsSource = PistonServer.Multiplayer.ChatView; } private void SendButton_Click(object sender, RoutedEventArgs e) @@ -65,18 +37,18 @@ namespace PistonServer OnMessageEntered(); } - private void OnMessageEntered() - { - var text = Message.Text; - SendMessage(text); - MessageEntered?.Invoke(Message.Text); - Message.Text = ""; - } - private void Message_OnKeyDown(object sender, KeyEventArgs e) { if (e.Key == Key.Enter) OnMessageEntered(); } + + private void OnMessageEntered() + { + //Can't use Message.Text directly because of object ownership in WPF. + var text = Message.Text; + PistonServer.Multiplayer.SendMessage(text); + Message.Text = ""; + } } } diff --git a/PistonServer/MainWindow.xaml b/PistonServer/MainWindow.xaml index 69e6342..7e3d4a7 100644 --- a/PistonServer/MainWindow.xaml +++ b/PistonServer/MainWindow.xaml @@ -1,25 +1,25 @@ -