From 7f6a6f9fff0fcbac07ed7dd73672f70a7f0def9e Mon Sep 17 00:00:00 2001 From: Wojtek Figat Date: Tue, 18 May 2021 11:41:49 +0200 Subject: [PATCH] Fix showing Game Plugins in editor plugins window --- Source/Editor/Windows/PluginsWindow.cs | 46 ++++++++++++------- .../Engine/Scripting/Plugins/PluginManager.cs | 35 ++++++++++---- 2 files changed, 56 insertions(+), 25 deletions(-) diff --git a/Source/Editor/Windows/PluginsWindow.cs b/Source/Editor/Windows/PluginsWindow.cs index a14a38774..56b3271ef 100644 --- a/Source/Editor/Windows/PluginsWindow.cs +++ b/Source/Editor/Windows/PluginsWindow.cs @@ -7,7 +7,6 @@ using FlaxEditor.GUI; using FlaxEditor.GUI.Tabs; using FlaxEngine; using FlaxEngine.GUI; -using FlaxEngine.Utilities; namespace FlaxEditor.Windows { @@ -184,16 +183,34 @@ namespace FlaxEditor.Windows Parent = this }; - // Check already loaded plugins - PluginManager.GamePlugins.ForEach(OnPluginLoaded); - PluginManager.EditorPlugins.ForEach(OnPluginLoaded); - - // Register for events - PluginManager.PluginLoaded += OnPluginLoaded; - PluginManager.PluginUnloading += OnPluginUnloading; + OnPluginsChanged(); + PluginManager.PluginsChanged += OnPluginsChanged; } - private void OnPluginLoaded(Plugin plugin) + private void OnPluginsChanged() + { + List toRemove = null; + foreach (var e in _entries) + { + if (!PluginManager.EditorPlugins.Contains(e.Key) && !PluginManager.GamePlugins.Contains(e.Key)) + { + if (toRemove == null) + toRemove = new List(); + toRemove.Add(e.Value); + } + } + if (toRemove != null) + { + foreach (var plugin in toRemove) + OnPluginRemove(plugin); + } + foreach (var plugin in PluginManager.GamePlugins) + OnPluginAdd(plugin); + foreach (var plugin in PluginManager.EditorPlugins) + OnPluginAdd(plugin); + } + + private void OnPluginAdd(Plugin plugin) { var entry = GetPluginEntry(plugin); if (entry != null) @@ -228,14 +245,10 @@ namespace FlaxEditor.Windows _tabs.SelectedTab = category; } - private void OnPluginUnloading(Plugin plugin) + private void OnPluginRemove(PluginEntry entry) { - var entry = GetPluginEntry(plugin); - if (entry == null) - return; - var category = entry.Category; - _entries.Remove(plugin); + _entries.Remove(entry.Plugin); entry.Dispose(); // If category is not used anymore @@ -285,8 +298,7 @@ namespace FlaxEditor.Windows /// public override void OnDestroy() { - PluginManager.PluginLoaded -= OnPluginLoaded; - PluginManager.PluginUnloading -= OnPluginUnloading; + PluginManager.PluginsChanged -= OnPluginsChanged; base.OnDestroy(); } diff --git a/Source/Engine/Scripting/Plugins/PluginManager.cs b/Source/Engine/Scripting/Plugins/PluginManager.cs index 32c5a97bf..e6da10e2f 100644 --- a/Source/Engine/Scripting/Plugins/PluginManager.cs +++ b/Source/Engine/Scripting/Plugins/PluginManager.cs @@ -19,12 +19,12 @@ namespace FlaxEngine private static readonly List _editorPlugins = new List(); /// - /// Gets the loaded and enabled game plugins. + /// Gets the game plugins. /// public static IReadOnlyList GamePlugins => _gamePlugins; /// - /// Gets the loaded and enabled editor plugins. + /// Gets the editor plugins. /// public static IReadOnlyList EditorPlugins => _editorPlugins; @@ -48,6 +48,11 @@ namespace FlaxEngine /// public static event PluginDelegate PluginUnloaded; + /// + /// Occurs when plugins collection gets edited (added or removed plugin). + /// + public static event Action PluginsChanged; + /// /// Determines whether can load the specified plugin. /// @@ -217,27 +222,37 @@ namespace FlaxEngine InvokeInitialize(plugin); } #endif + PluginsChanged?.Invoke(); } internal static void Internal_Dispose(Assembly assembly) { + bool changed = false; + for (int i = _editorPlugins.Count - 1; i >= 0 && _editorPlugins.Count > 0; i--) { - if (_editorPlugins[i].GetType().Assembly == assembly) + var plugin = _editorPlugins[i]; + if (plugin.GetType().Assembly == assembly) { - InvokeDeinitialize(_editorPlugins[i]); + InvokeDeinitialize(plugin); _editorPlugins.RemoveAt(i); + changed = true; } } for (int i = _gamePlugins.Count - 1; i >= 0 && _gamePlugins.Count > 0; i--) { - if (_gamePlugins[i].GetType().Assembly == assembly) + var plugin = _gamePlugins[i]; + if (plugin.GetType().Assembly == assembly) { - InvokeDeinitialize(_gamePlugins[i]); + InvokeDeinitialize(plugin); _gamePlugins.RemoveAt(i); + changed = true; } } + + if (changed) + PluginsChanged?.Invoke(); } internal static void Internal_Dispose() @@ -249,15 +264,19 @@ namespace FlaxEngine for (int i = _editorPlugins.Count - 1; i >= 0 && _editorPlugins.Count > 0; i--) { - InvokeDeinitialize(_editorPlugins[i]); + var plugin = _editorPlugins[i]; + InvokeDeinitialize(plugin); _editorPlugins.RemoveAt(i); } for (int i = _gamePlugins.Count - 1; i >= 0 && _gamePlugins.Count > 0; i--) { - InvokeDeinitialize(_gamePlugins[i]); + var plugin = _gamePlugins[i]; + InvokeDeinitialize(plugin); _gamePlugins.RemoveAt(i); } + + PluginsChanged?.Invoke(); } } }