Fix showing Game Plugins in editor plugins window

This commit is contained in:
Wojtek Figat
2021-05-18 11:41:49 +02:00
parent 2e93a7fd08
commit 7f6a6f9fff
2 changed files with 56 additions and 25 deletions

View File

@@ -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<PluginEntry> toRemove = null;
foreach (var e in _entries)
{
if (!PluginManager.EditorPlugins.Contains(e.Key) && !PluginManager.GamePlugins.Contains(e.Key))
{
if (toRemove == null)
toRemove = new List<PluginEntry>();
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
/// <inheritdoc />
public override void OnDestroy()
{
PluginManager.PluginLoaded -= OnPluginLoaded;
PluginManager.PluginUnloading -= OnPluginUnloading;
PluginManager.PluginsChanged -= OnPluginsChanged;
base.OnDestroy();
}

View File

@@ -19,12 +19,12 @@ namespace FlaxEngine
private static readonly List<Plugin> _editorPlugins = new List<Plugin>();
/// <summary>
/// Gets the loaded and enabled game plugins.
/// Gets the game plugins.
/// </summary>
public static IReadOnlyList<GamePlugin> GamePlugins => _gamePlugins;
/// <summary>
/// Gets the loaded and enabled editor plugins.
/// Gets the editor plugins.
/// </summary>
public static IReadOnlyList<Plugin> EditorPlugins => _editorPlugins;
@@ -48,6 +48,11 @@ namespace FlaxEngine
/// </summary>
public static event PluginDelegate PluginUnloaded;
/// <summary>
/// Occurs when plugins collection gets edited (added or removed plugin).
/// </summary>
public static event Action PluginsChanged;
/// <summary>
/// Determines whether can load the specified plugin.
/// </summary>
@@ -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();
}
}
}