Plugins: Fail gracefully on multiple instances of same plugin

This commit is contained in:
Katy Coe
2020-12-27 23:59:41 +01:00
parent 4901d9b4bc
commit a32a68b02f
3 changed files with 24 additions and 6 deletions

View File

@@ -121,13 +121,21 @@ namespace Il2CppInspector.CLI
// Get plugin option classes // Get plugin option classes
public static Type[] GetPluginOptionTypes() { public static Type[] GetPluginOptionTypes() {
// Don't do anything if there are no loaded plugins // Don't do anything if there are no loaded plugins
var plugins = PluginManager.AvailablePlugins; try {
var plugins = PluginManager.AvailablePlugins;
if (!plugins.Any()) if (!plugins.Any())
return Array.Empty<Type>(); return Array.Empty<Type>();
// Create CommandLine-friendly option classes for each plugin // Create CommandLine-friendly option classes for each plugin
return plugins.Select(p => CreateOptionsFromPlugin(p)).ToArray(); return plugins.Select(p => CreateOptionsFromPlugin(p)).ToArray();
}
catch (InvalidOperationException ex) {
Console.Error.WriteLine(ex.Message);
Environment.Exit(1);
return null;
}
} }
// Parse all options for all plugins // Parse all options for all plugins

View File

@@ -174,6 +174,11 @@ namespace Il2CppInspector
// Current version // Current version
if (typeof(IPlugin).IsAssignableFrom(type) && !type.IsAbstract) { if (typeof(IPlugin).IsAssignableFrom(type) && !type.IsAbstract) {
var plugin = (IPlugin) Activator.CreateInstance(type); var plugin = (IPlugin) Activator.CreateInstance(type);
// Don't allow multiple identical plugins to load
if (AsInstance.ManagedPlugins.Any(p => p.Plugin.Id == plugin.Id))
throw new Exception($"Multiple copies of {plugin.Name} were found. Please ensure the plugins folder only contains one copy of each plugin.");
AsInstance.ManagedPlugins.Add(new ManagedPlugin { Plugin = plugin, Available = true, Enabled = false }); AsInstance.ManagedPlugins.Add(new ManagedPlugin { Plugin = plugin, Available = true, Enabled = false });
} }

View File

@@ -126,7 +126,12 @@ namespace Il2CppInspectorGUI
catch (NotSupportedException) { } catch (NotSupportedException) { }
// Load plugins if they aren't already // Load plugins if they aren't already
PluginManager.EnsureInit(); try {
PluginManager.EnsureInit();
} catch (InvalidOperationException ex) {
MessageBox.Show(ex.Message, "Fatal error loading plugins");
Environment.Exit(1);
}
// Arrange plugins // Arrange plugins
var loadedPlugins = PluginManager.AsInstance.ManagedPlugins; var loadedPlugins = PluginManager.AsInstance.ManagedPlugins;