From 140000df55e64952e75e46ae8becf29df9a32f0b Mon Sep 17 00:00:00 2001 From: Westin Miller Date: Thu, 24 Aug 2017 20:24:07 -0700 Subject: [PATCH] Test-time reflected event checker Server UI components work with new system Events for loading and unloading TorchSessions Commands report information again. Catch, log, and rethrow errors that occur in Torch-handled SE events --- Torch.API/Session/ITorchSessionManager.cs | 16 +++++ .../TorchClientReflectionTest.cs | 5 +- .../TorchServerReflectionTest.cs | 5 +- .../Managers/MultiplayerManagerDedicated.cs | 5 +- Torch.Server/Torch.Server.csproj | 3 + Torch.Server/Views/ChatControl.xaml.cs | 65 ++++++++++++++++--- Torch.Server/Views/PlayerListControl.xaml.cs | 20 ++++-- Torch.Tests/ReflectionSystemTest.cs | 2 +- Torch.Tests/TorchReflectionTest.cs | 5 +- Torch/Commands/Command.cs | 2 +- Torch/Commands/CommandManager.cs | 2 +- Torch/Commands/TorchCommands.cs | 28 +++++--- .../Managers/ChatManager/ChatManagerClient.cs | 20 +++--- .../Managers/ChatManager/ChatManagerServer.cs | 13 ++-- Torch/Managers/MultiplayerManagerBase.cs | 2 +- .../Managers/NetworkManager/NetworkManager.cs | 16 ++--- Torch/Managers/PluginManager.cs | 34 ++++++++-- Torch/Session/TorchSessionManager.cs | 20 ++++-- Torch/TorchBase.cs | 40 ++++++++++-- 19 files changed, 230 insertions(+), 73 deletions(-) diff --git a/Torch.API/Session/ITorchSessionManager.cs b/Torch.API/Session/ITorchSessionManager.cs index 08fef9e..8e5a5ed 100644 --- a/Torch.API/Session/ITorchSessionManager.cs +++ b/Torch.API/Session/ITorchSessionManager.cs @@ -17,11 +17,27 @@ namespace Torch.API.Session /// The manager that will live in the session, or null if none. public delegate IManager SessionManagerFactoryDel(ITorchSession session); + /// + /// Fired when the given session has been completely loaded or is unloading. + /// + /// The session + public delegate void TorchSessionLoadDel(ITorchSession session); + /// /// Manages the creation and destruction of instances for each created by Space Engineers. /// public interface ITorchSessionManager : IManager { + /// + /// Fired when a has finished loading. + /// + event TorchSessionLoadDel SessionLoaded; + + /// + /// Fired when a has begun unloading. + /// + event TorchSessionLoadDel SessionUnloading; + /// /// The currently running session /// diff --git a/Torch.Client.Tests/TorchClientReflectionTest.cs b/Torch.Client.Tests/TorchClientReflectionTest.cs index 12a1948..80cb47f 100644 --- a/Torch.Client.Tests/TorchClientReflectionTest.cs +++ b/Torch.Client.Tests/TorchClientReflectionTest.cs @@ -1,4 +1,5 @@ -using System.Collections.Generic; +using System; +using System.Collections.Generic; using Torch.Client; using Torch.Tests; using Torch.Utils; @@ -86,7 +87,7 @@ namespace Torch.Client.Tests return; Assert.True(ReflectedManager.Process(field.Field)); if (field.Field.IsStatic) - Assert.NotNull(field.Field.GetValue(null)); + ((Func)field.Field.GetValue(null)).Invoke(); } #endregion } diff --git a/Torch.Server.Tests/TorchServerReflectionTest.cs b/Torch.Server.Tests/TorchServerReflectionTest.cs index 8897c8d..b9c7904 100644 --- a/Torch.Server.Tests/TorchServerReflectionTest.cs +++ b/Torch.Server.Tests/TorchServerReflectionTest.cs @@ -1,4 +1,5 @@ -using System.Collections.Generic; +using System; +using System.Collections.Generic; using Torch.Tests; using Torch.Utils; using Xunit; @@ -74,7 +75,7 @@ namespace Torch.Server.Tests return; Assert.True(ReflectedManager.Process(field.Field)); if (field.Field.IsStatic) - Assert.NotNull(field.Field.GetValue(null)); + ((Func)field.Field.GetValue(null)).Invoke(); } #endregion } diff --git a/Torch.Server/Managers/MultiplayerManagerDedicated.cs b/Torch.Server/Managers/MultiplayerManagerDedicated.cs index 85399c0..3a9b331 100644 --- a/Torch.Server/Managers/MultiplayerManagerDedicated.cs +++ b/Torch.Server/Managers/MultiplayerManagerDedicated.cs @@ -16,6 +16,7 @@ using Torch.Utils; using Torch.ViewModels; using VRage.GameServices; using VRage.Network; +using VRage.Steam; namespace Torch.Server.Managers { @@ -73,9 +74,9 @@ namespace Torch.Server.Managers #pragma warning disable 649 - [ReflectedEventReplace(typeof(IMyGameServer), nameof(IMyGameServer.ValidateAuthTicketResponse), typeof(MyDedicatedServerBase), "GameServer_ValidateAuthTicketResponse")] + [ReflectedEventReplace(typeof(MySteamGameServer), nameof(MySteamGameServer.ValidateAuthTicketResponse), typeof(MyDedicatedServerBase), "GameServer_ValidateAuthTicketResponse")] private static Func _gameServerValidateAuthTicketFactory; - [ReflectedEventReplace(typeof(IMyGameServer), nameof(IMyGameServer.UserGroupStatusResponse), typeof(MyDedicatedServerBase), "GameServer_UserGroupStatus")] + [ReflectedEventReplace(typeof(MySteamGameServer), nameof(MySteamGameServer.UserGroupStatusResponse), typeof(MyDedicatedServerBase), "GameServer_UserGroupStatus")] private static Func _gameServerUserGroupStatusFactory; private ReflectedEventReplacer _gameServerValidateAuthTicketReplacer; private ReflectedEventReplacer _gameServerUserGroupStatusReplacer; diff --git a/Torch.Server/Torch.Server.csproj b/Torch.Server/Torch.Server.csproj index 5d9bee4..ff2f432 100644 --- a/Torch.Server/Torch.Server.csproj +++ b/Torch.Server/Torch.Server.csproj @@ -182,6 +182,9 @@ ..\GameBinaries\VRage.Scripting.dll False + + ..\GameBinaries\VRage.Steam.dll + diff --git a/Torch.Server/Views/ChatControl.xaml.cs b/Torch.Server/Views/ChatControl.xaml.cs index 2b8e9de..1c3152e 100644 --- a/Torch.Server/Views/ChatControl.xaml.cs +++ b/Torch.Server/Views/ChatControl.xaml.cs @@ -21,6 +21,7 @@ using Sandbox.Game.World; using SteamSDK; using Torch.API; using Torch.API.Managers; +using Torch.API.Session; using Torch.Managers; using Torch.Server.Managers; @@ -42,14 +43,29 @@ namespace Torch.Server { _server = (TorchBase)server; ChatItems.Items.Clear(); - server.SessionLoaded += () => + + var sessionManager = server.Managers.GetManager(); + sessionManager.SessionLoaded += BindSession; + sessionManager.SessionUnloading += UnbindSession; + } + + private void BindSession(ITorchSession session) + { + Dispatcher.Invoke(() => { - var multiplayer = server.CurrentSession.Managers.GetManager(); - DataContext = multiplayer; - // TODO - // if (multiplayer.ChatHistory is INotifyCollectionChanged ncc) - // ncc.CollectionChanged += ChatHistory_CollectionChanged; - }; + var chatMgr = _server?.CurrentSession?.Managers.GetManager(); + if (chatMgr != null) + DataContext = new ChatManagerProxy(chatMgr); + }); + } + + private void UnbindSession(ITorchSession session) + { + Dispatcher.Invoke(() => + { + (DataContext as ChatManagerProxy)?.Dispose(); + DataContext = null; + }); } private void ChatHistory_CollectionChanged(object sender, NotifyCollectionChangedEventArgs e) @@ -86,7 +102,7 @@ namespace Torch.Server var commands = _server.CurrentSession?.Managers.GetManager(); if (commands != null && commands.IsCommand(text)) { - // TODO _multiplayer.ChatHistory.Add(new ChatMessage(DateTime.Now, 0, "Server", text)); + (DataContext as ChatManagerProxy)?.AddMessage(new TorchChatMessage() { Author = "Server", Message = text }); _server.Invoke(() => { var response = commands.HandleCommandFromServer(text); @@ -102,8 +118,37 @@ namespace Torch.Server private void OnMessageEntered_Callback(string response) { - //if (!string.IsNullOrEmpty(response)) - // TODO _multiplayer.ChatHistory.Add(new ChatMessage(DateTime.Now, 0, "Server", response)); + if (!string.IsNullOrEmpty(response)) + (DataContext as ChatManagerProxy)?.AddMessage(new TorchChatMessage() { Author = "Server", Message = response }); + } + + private class ChatManagerProxy : IDisposable + { + private readonly IChatManagerClient _chatMgr; + + public ChatManagerProxy(IChatManagerClient chatMgr) + { + this._chatMgr = chatMgr; + this._chatMgr.MessageRecieved += ChatMgr_MessageRecieved; ; + } + + public IList ChatHistory { get; } = new ObservableList(); + + /// + public void Dispose() + { + _chatMgr.MessageRecieved -= ChatMgr_MessageRecieved; + } + + private void ChatMgr_MessageRecieved(TorchChatMessage msg, ref bool consumed) + { + AddMessage(msg); + } + + internal void AddMessage(TorchChatMessage msg) + { + ChatHistory.Add(new ChatMessage(DateTime.Now, msg.AuthorSteamId ?? 0, msg.Author, msg.Message)); + } } } } diff --git a/Torch.Server/Views/PlayerListControl.xaml.cs b/Torch.Server/Views/PlayerListControl.xaml.cs index 70fbbc3..b76b7c7 100644 --- a/Torch.Server/Views/PlayerListControl.xaml.cs +++ b/Torch.Server/Views/PlayerListControl.xaml.cs @@ -21,6 +21,7 @@ using Sandbox.ModAPI; using SteamSDK; using Torch.API; using Torch.API.Managers; +using Torch.API.Session; using Torch.Managers; using Torch.Server.Managers; using Torch.ViewModels; @@ -43,11 +44,20 @@ namespace Torch.Server public void BindServer(ITorchServer server) { _server = server; - server.SessionLoaded += () => - { - var multiplayer = server.CurrentSession?.Managers.GetManager(); - DataContext = multiplayer; - }; + + var sessionManager = server.Managers.GetManager(); + sessionManager.SessionLoaded += BindSession; + sessionManager.SessionUnloading += UnbindSession; + } + + private void BindSession(ITorchSession session) + { + Dispatcher.Invoke(() => DataContext = _server?.CurrentSession?.Managers.GetManager()); + } + + private void UnbindSession(ITorchSession session) + { + Dispatcher.Invoke(() => DataContext = null); } private void KickButton_Click(object sender, RoutedEventArgs e) diff --git a/Torch.Tests/ReflectionSystemTest.cs b/Torch.Tests/ReflectionSystemTest.cs index 4161fe7..c586a19 100644 --- a/Torch.Tests/ReflectionSystemTest.cs +++ b/Torch.Tests/ReflectionSystemTest.cs @@ -77,7 +77,7 @@ namespace Torch.Tests return; Assert.True(ReflectedManager.Process(field.Field)); if (field.Field.IsStatic) - Assert.NotNull(field.Field.GetValue(null)); + ((Func)field.Field.GetValue(null)).Invoke(); } #endregion diff --git a/Torch.Tests/TorchReflectionTest.cs b/Torch.Tests/TorchReflectionTest.cs index 26014b5..775d100 100644 --- a/Torch.Tests/TorchReflectionTest.cs +++ b/Torch.Tests/TorchReflectionTest.cs @@ -1,4 +1,5 @@ -using System.Collections.Generic; +using System; +using System.Collections.Generic; using Torch.Utils; using Xunit; @@ -84,7 +85,7 @@ namespace Torch.Tests return; Assert.True(ReflectedManager.Process(field.Field)); if (field.Field.IsStatic) - Assert.NotNull(field.Field.GetValue(null)); + ((Func)field.Field.GetValue(null)).Invoke(); } #endregion } diff --git a/Torch/Commands/Command.cs b/Torch/Commands/Command.cs index 7aedcab..cbd08e2 100644 --- a/Torch/Commands/Command.cs +++ b/Torch/Commands/Command.cs @@ -117,7 +117,7 @@ namespace Torch.Commands catch (Exception e) { context.Respond(e.Message, "Error", MyFontEnum.Red); - Log.Error($"Command '{SyntaxHelp}' from '{Plugin.Name ?? "Torch"}' threw an exception. Args: {string.Join(", ", context.Args)}"); + Log.Error($"Command '{SyntaxHelp}' from '{Plugin?.Name ?? "Torch"}' threw an exception. Args: {string.Join(", ", context.Args)}"); Log.Error(e); return true; } diff --git a/Torch/Commands/CommandManager.cs b/Torch/Commands/CommandManager.cs index e1083cc..ff2d9a0 100644 --- a/Torch/Commands/CommandManager.cs +++ b/Torch/Commands/CommandManager.cs @@ -108,7 +108,7 @@ namespace Torch.Commands consumed = true; - var player = Torch.GetManager().GetPlayerBySteamId(steamId); + var player = Torch.CurrentSession.Managers.GetManager().GetPlayerBySteamId(steamId); if (player == null) { _log.Error($"Command {message} invoked by nonexistant player"); diff --git a/Torch/Commands/TorchCommands.cs b/Torch/Commands/TorchCommands.cs index 5e7dbb6..1430587 100644 --- a/Torch/Commands/TorchCommands.cs +++ b/Torch/Commands/TorchCommands.cs @@ -21,7 +21,12 @@ namespace Torch.Commands [Permission(MyPromoteLevel.None)] public void Help() { - var commandManager = ((TorchBase)Context.Torch).CurrentSession.Managers.GetManager(); + var commandManager = Context.Torch.CurrentSession?.Managers.GetManager(); + if (commandManager == null) + { + Context.Respond("Must have an attached session to list commands"); + return; + } commandManager.Commands.GetNode(Context.Args, out CommandTree.CommandNode node); if (node != null) @@ -51,7 +56,12 @@ namespace Torch.Commands [Command("longhelp", "Get verbose help. Will send a long message, check the Comms tab.")] public void LongHelp() { - var commandManager = Context.Torch.Managers.GetManager(); + var commandManager = Context.Torch.CurrentSession?.Managers.GetManager(); + if (commandManager == null) + { + Context.Respond("Must have an attached session to list commands"); + return; + } commandManager.Commands.GetNode(Context.Args, out CommandTree.CommandNode node); if (node != null) @@ -96,7 +106,7 @@ namespace Torch.Commands [Permission(MyPromoteLevel.None)] public void Plugins() { - var plugins = Context.Torch.Plugins.Select(p => p.Name); + var plugins = Context.Torch.Managers.GetManager()?.Plugins.Select(p => p.Name) ?? Enumerable.Empty(); Context.Respond($"Loaded plugins: {string.Join(", ", plugins)}"); } @@ -128,15 +138,13 @@ namespace Torch.Commands { if (i >= 60 && i % 60 == 0) { - // TODO -// Context.Torch.Multiplayer.SendMessage($"Restarting server in {i / 60} minute{Pluralize(i / 60)}."); -// yield return null; + Context.Torch.CurrentSession.Managers.GetManager().SendMessageAsSelf($"Restarting server in {i / 60} minute{Pluralize(i / 60)}."); + yield return null; } else if (i > 0) { - // TODO -// if (i < 11) -// Context.Torch.Multiplayer.SendMessage($"Restarting server in {i} second{Pluralize(i)}."); + if (i < 11) + Context.Torch.CurrentSession.Managers.GetManager().SendMessageAsSelf($"Restarting server in {i} second{Pluralize(i)}."); yield return null; } else @@ -155,7 +163,7 @@ namespace Torch.Commands { return num == 1 ? "" : "s"; } - + /// /// Initializes a save of the game. /// Caller id defaults to 0 in the case of triggering the chat command from server. diff --git a/Torch/Managers/ChatManager/ChatManagerClient.cs b/Torch/Managers/ChatManager/ChatManagerClient.cs index 529154a..f18a494 100644 --- a/Torch/Managers/ChatManager/ChatManagerClient.cs +++ b/Torch/Managers/ChatManager/ChatManagerClient.cs @@ -15,7 +15,6 @@ using Torch.API; using Torch.API.Managers; using Torch.Utils; using VRage.Game; -using Game = Sandbox.Engine.Platform.Game; namespace Torch.Managers.ChatManager { @@ -37,7 +36,7 @@ namespace Torch.Managers.ChatManager { if (MyMultiplayer.Static != null) { - if (Game.IsDedicated) + if (Sandbox.Engine.Platform.Game.IsDedicated) { var scripted = new ScriptedChatMsg() { @@ -51,14 +50,15 @@ namespace Torch.Managers.ChatManager else MyMultiplayer.Static.SendChatMessage(message); } - else if (MyHud.Chat != null) + else if (HasHud) MyHud.Chat.ShowMessage(MySession.Static.LocalHumanPlayer?.DisplayName ?? "Player", message); } /// public void DisplayMessageOnSelf(string author, string message, string font) { - MyHud.Chat.ShowMessage(author, message, font); + if (HasHud) + MyHud.Chat?.ShowMessage(author, message, font); MySession.Static.GlobalChatHistory.GlobalChatHistory.Chat.Enqueue(new MyGlobalChatItem() { Author = author, @@ -91,9 +91,9 @@ namespace Torch.Managers.ChatManager public override void Detach() { MyAPIUtilities.Static.MessageEntered -= OnMessageEntered; - if (_chatMessageRecievedReplacer != null && _chatMessageRecievedReplacer.Replaced) + if (_chatMessageRecievedReplacer != null && _chatMessageRecievedReplacer.Replaced && HasHud) _chatMessageRecievedReplacer.Restore(MyHud.Chat); - if (_scriptedChatMessageRecievedReplacer != null && _scriptedChatMessageRecievedReplacer.Replaced) + if (_scriptedChatMessageRecievedReplacer != null && _scriptedChatMessageRecievedReplacer.Replaced && HasHud) _scriptedChatMessageRecievedReplacer.Restore(MyHud.Chat); MyAPIUtilities.Static.MessageEntered -= OfflineMessageReciever; base.Detach(); @@ -148,11 +148,11 @@ namespace Torch.Managers.ChatManager }; var consumed = false; MessageRecieved?.Invoke(torchMsg, ref consumed); - if (!consumed) + if (!consumed && HasHud) _hudChatMessageReceived.Invoke(MyHud.Chat, steamUserId, message); } - private void Multiplayer_ScriptedChatMessageReceived(string author, string message, string font) + private void Multiplayer_ScriptedChatMessageReceived(string message, string author, string font) { var torchMsg = new TorchChatMessage() { @@ -163,12 +163,14 @@ namespace Torch.Managers.ChatManager }; var consumed = false; MessageRecieved?.Invoke(torchMsg, ref consumed); - if (!consumed) + if (!consumed && HasHud) _hudChatScriptedMessageReceived.Invoke(MyHud.Chat, author, message, font); } private const string _hudChatMessageReceivedName = "Multiplayer_ChatMessageReceived"; private const string _hudChatScriptedMessageReceivedName = "multiplayer_ScriptedChatMessageReceived"; + + protected static bool HasHud => !Sandbox.Engine.Platform.Game.IsDedicated; [ReflectedMethod(Name = _hudChatMessageReceivedName)] private static Action _hudChatMessageReceived; diff --git a/Torch/Managers/ChatManager/ChatManagerServer.cs b/Torch/Managers/ChatManager/ChatManagerServer.cs index 82b9cc9..f71900c 100644 --- a/Torch/Managers/ChatManager/ChatManagerServer.cs +++ b/Torch/Managers/ChatManager/ChatManagerServer.cs @@ -43,7 +43,7 @@ namespace Torch.Managers.ChatManager { if (MyMultiplayer.Static == null) { - if (targetSteamId == MyGameService.UserId || targetSteamId == 0) + if ((targetSteamId == MyGameService.UserId || targetSteamId == 0) && HasHud) MyHud.Chat?.ShowMessage(authorId == MyGameService.UserId ? (MySession.Static.LocalHumanPlayer?.DisplayName ?? "Player") : $"user_{authorId}", message); return; @@ -73,7 +73,7 @@ namespace Torch.Managers.ChatManager { if (MyMultiplayer.Static == null) { - if (targetSteamId == MyGameService.UserId || targetSteamId == 0) + if ((targetSteamId == MyGameService.UserId || targetSteamId == 0) && HasHud) MyHud.Chat?.ShowMessage(author, message, font); return; } @@ -82,7 +82,7 @@ namespace Torch.Managers.ChatManager Author = author, Text = message, Font = font, - Target = (long)targetSteamId + Target = Sync.Players.TryGetIdentityId(targetSteamId) }; MyMultiplayerBase.SendScriptedChatMessage(ref scripted); } @@ -107,7 +107,12 @@ namespace Torch.Managers.ChatManager if (MyMultiplayer.Static != null) { MyMultiplayer.Static.ChatMessageReceived += MpStaticChatMessageReceived; - _log.Warn("Failed to initialize network intercept, we can't discard chat messages! Falling back to another method."); + _log.Warn( + "Failed to initialize network intercept, we can't discard chat messages! Falling back to another method."); + } + else + { + _log.Debug("Using offline message processor"); } } diff --git a/Torch/Managers/MultiplayerManagerBase.cs b/Torch/Managers/MultiplayerManagerBase.cs index eaad039..d6fcd8a 100644 --- a/Torch/Managers/MultiplayerManagerBase.cs +++ b/Torch/Managers/MultiplayerManagerBase.cs @@ -115,7 +115,7 @@ namespace Torch.Managers protected void RaiseClientJoined(ulong steamId) { var vm = new PlayerViewModel(steamId){State=ConnectionState.Connected}; - _log.Info($"Plat {vm.Name} joined ({vm.SteamId}"); + _log.Info($"Player {vm.Name} joined ({vm.SteamId}"); Players.Add(steamId, vm); PlayerJoined?.Invoke(vm); } diff --git a/Torch/Managers/NetworkManager/NetworkManager.cs b/Torch/Managers/NetworkManager/NetworkManager.cs index 743f319..948ef80 100644 --- a/Torch/Managers/NetworkManager/NetworkManager.cs +++ b/Torch/Managers/NetworkManager/NetworkManager.cs @@ -60,15 +60,9 @@ namespace Torch.Managers throw; } } - /// - /// Loads the network intercept system - /// + + /// public override void Attach() - { - Torch.SessionLoaded += OnSessionLoaded; - } - - private void OnSessionLoaded() { if (_init) return; @@ -105,6 +99,12 @@ namespace Torch.Managers _log.Debug("Initialized network intercept"); } + /// + public override void Detach() + { + // TODO reverse what was done in Attach + } + #region Network Intercept /// diff --git a/Torch/Managers/PluginManager.cs b/Torch/Managers/PluginManager.cs index 3f0108d..0b136ab 100644 --- a/Torch/Managers/PluginManager.cs +++ b/Torch/Managers/PluginManager.cs @@ -10,6 +10,7 @@ using NLog; using Torch.API; using Torch.API.Managers; using Torch.API.Plugins; +using Torch.API.Session; using Torch.Commands; using VRage.Collections; @@ -22,8 +23,8 @@ namespace Torch.Managers public readonly string PluginDir = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "Plugins"); [Dependency] private UpdateManager _updateManager; - [Dependency] - private CommandManager _commandManager; + [Dependency(Optional = true)] + private ITorchSessionManager _sessionManager; /// public IList Plugins { get; } = new ObservableList(); @@ -41,6 +42,11 @@ namespace Torch.Managers /// public void UpdatePlugins() { + if (_sessionManager != null) + { + _sessionManager.SessionLoaded += AttachCommandsToSession; + _sessionManager.SessionUnloading += DetachCommandsFromSession; + } foreach (var plugin in Plugins) plugin.Update(); } @@ -50,12 +56,32 @@ namespace Torch.Managers /// public override void Detach() { + if (_sessionManager != null) + { + _sessionManager.SessionLoaded -= AttachCommandsToSession; + _sessionManager.SessionUnloading -= DetachCommandsFromSession; + } foreach (var plugin in Plugins) plugin.Dispose(); Plugins.Clear(); } + private void AttachCommandsToSession(ITorchSession session) + { + var cmdMgr = session.Managers.GetManager(); + foreach (ITorchPlugin plugin in Plugins) + cmdMgr?.RegisterPluginCommands(plugin); + } + + private void DetachCommandsFromSession(ITorchSession session) + { + var cmdMgr = session.Managers.GetManager(); + foreach (ITorchPlugin plugin in Plugins) { + // cmdMgr?.UnregisterPluginCommands(plugin); + } + } + private void DownloadPlugins() { var folders = Directory.GetDirectories(PluginDir); @@ -80,7 +106,7 @@ namespace Torch.Managers foreach (var repository in toDownload) { - var manifest = new PluginManifest {Repository = repository, Version = "0.0"}; + var manifest = new PluginManifest { Repository = repository, Version = "0.0" }; taskList.Add(_updateManager.CheckAndUpdatePlugin(manifest)); } @@ -118,8 +144,6 @@ namespace Torch.Managers _log.Info($"Loading plugin {plugin.Name} ({plugin.Version})"); plugin.StoragePath = Torch.Config.InstancePath; Plugins.Add(plugin); - - _commandManager.RegisterPluginCommands(plugin); } catch (Exception e) { diff --git a/Torch/Session/TorchSessionManager.cs b/Torch/Session/TorchSessionManager.cs index 3e4b1a6..933f61f 100644 --- a/Torch/Session/TorchSessionManager.cs +++ b/Torch/Session/TorchSessionManager.cs @@ -21,6 +21,12 @@ namespace Torch.Session private static readonly Logger _log = LogManager.GetCurrentClassLogger(); private TorchSession _currentSession; + /// + public event TorchSessionLoadDel SessionLoaded; + + /// + public event TorchSessionLoadDel SessionUnloading; + /// public ITorchSession CurrentSession => _currentSession; @@ -46,7 +52,7 @@ namespace Torch.Session return _factories.Remove(factory); } - private void SessionLoaded() + private void LoadSession() { if (_currentSession != null) { @@ -63,12 +69,14 @@ namespace Torch.Session CurrentSession.Managers.AddManager(manager); } (CurrentSession as TorchSession)?.Attach(); + SessionLoaded?.Invoke(_currentSession); } - private void SessionUnloaded() + private void UnloadSession() { if (_currentSession == null) return; + SessionUnloading?.Invoke(_currentSession); _log.Info($"Unloading torch session for {_currentSession.KeenSession.Name}"); _currentSession.Detach(); _currentSession = null; @@ -77,8 +85,8 @@ namespace Torch.Session /// public override void Attach() { - MySession.AfterLoading += SessionLoaded; - MySession.OnUnloaded += SessionUnloaded; + MySession.AfterLoading += LoadSession; + MySession.OnUnloaded += UnloadSession; } /// @@ -86,8 +94,8 @@ namespace Torch.Session { _currentSession?.Detach(); _currentSession = null; - MySession.AfterLoading -= SessionLoaded; - MySession.OnUnloaded -= SessionUnloaded; + MySession.AfterLoading -= LoadSession; + MySession.OnUnloaded -= UnloadSession; } } } diff --git a/Torch/TorchBase.cs b/Torch/TorchBase.cs index cc88c84..f167e26 100644 --- a/Torch/TorchBase.cs +++ b/Torch/TorchBase.cs @@ -273,25 +273,57 @@ namespace Torch private void OnSessionLoading() { Log.Debug("Session loading"); - SessionLoading?.Invoke(); + try + { + SessionLoading?.Invoke(); + } + catch (Exception e) + { + Log.Error(e); + throw; + } } private void OnSessionLoaded() { Log.Debug("Session loaded"); - SessionLoaded?.Invoke(); + try + { + SessionLoaded?.Invoke(); + } + catch (Exception e) + { + Log.Error(e); + throw; + } } private void OnSessionUnloading() { Log.Debug("Session unloading"); - SessionUnloading?.Invoke(); + try + { + SessionUnloading?.Invoke(); + } + catch (Exception e) + { + Log.Error(e); + throw; + } } private void OnSessionUnloaded() { Log.Debug("Session unloaded"); - SessionUnloaded?.Invoke(); + try + { + SessionUnloaded?.Invoke(); + } + catch (Exception e) + { + Log.Error(e); + throw; + } } ///