From 25942a7e6b27c8ba394a827c9fcf172b87de14e3 Mon Sep 17 00:00:00 2001 From: Katy Coe Date: Thu, 24 Dec 2020 01:10:18 +0100 Subject: [PATCH] Plugins: Catch TargetInvocationException -> MissingFieldException on instantiation --- .../Plugins/PluginManager.cs | 37 ++++++++++++------- 1 file changed, 23 insertions(+), 14 deletions(-) diff --git a/Il2CppInspector.Common/Plugins/PluginManager.cs b/Il2CppInspector.Common/Plugins/PluginManager.cs index a91f1f6..dd883d0 100644 --- a/Il2CppInspector.Common/Plugins/PluginManager.cs +++ b/Il2CppInspector.Common/Plugins/PluginManager.cs @@ -146,6 +146,13 @@ namespace Il2CppInspector //typeof(PluginAPI.V101.IPlugin) }); + // Construct disabled plugin as a placeholder if loading fails (mainly for the GUI) + var disabledPlugin = new ManagedPlugin { + Plugin = null, + Available = false, + Enabled = false + }; + // Load plugin try { var asm = loader.LoadDefaultAssembly(); @@ -173,13 +180,7 @@ namespace Il2CppInspector catch (ReflectionTypeLoadException ex) { var name = Path.GetFileName(dll); - // Construct disabled plugin - var plugin = new ManagedPlugin { - Plugin = null, - Available = false, - Enabled = false - }; - AsInstance.ManagedPlugins.Add(plugin); + AsInstance.ManagedPlugins.Add(disabledPlugin); // Determine error switch (ex.LoaderExceptions[0]) { @@ -189,20 +190,20 @@ namespace Il2CppInspector if (failedType.TypeName.StartsWith("Il2CppInspector.PluginAPI.")) { // Requires newer plugin API version - plugin.Plugin = new InvalidPlugin { Name = name, Description = "This plugin requires a newer version of Il2CppInspector" }; - Console.Error.WriteLine($"Error loading plugin {plugin.Plugin.Name}: {plugin.Plugin.Description}"); + disabledPlugin.Plugin = new InvalidPlugin { Name = name, Description = "This plugin requires a newer version of Il2CppInspector" }; + Console.Error.WriteLine($"Error loading plugin {disabledPlugin.Plugin.Name}: {disabledPlugin.Plugin.Description}"); } else { - // Missing dependencies - plugin.Plugin = new InvalidPlugin { Name = name, Description = "This plugin has dependencies that could not be found. Check that all required DLLs are present in the plugins folder." }; - Console.Error.WriteLine($"Error loading plugin {plugin.Plugin.Name}: {plugin.Plugin.Description}"); + // Missing dependencies or some inherited interfaces not implemented + disabledPlugin.Plugin = new InvalidPlugin { Name = name, Description = "This plugin has dependencies that could not be found or may require a newer version of Il2CppInspector. Check that all required DLLs are present in the plugins folder." }; + Console.Error.WriteLine($"Error loading plugin {disabledPlugin.Plugin.Name}: {disabledPlugin.Plugin.Description}"); } break; // Assembly could not be found case FileNotFoundException failedFile: - plugin.Plugin = new InvalidPlugin { Name = name, Description = $"This plugin needs {failedFile.FileName} but the file could not be found" }; - Console.Error.WriteLine($"Error loading plugin {plugin.Plugin.Name}: {plugin.Plugin.Description}"); + disabledPlugin.Plugin = new InvalidPlugin { Name = name, Description = $"This plugin needs {failedFile.FileName} but the file could not be found" }; + Console.Error.WriteLine($"Error loading plugin {disabledPlugin.Plugin.Name}: {disabledPlugin.Plugin.Description}"); break; // Some other type loading error @@ -211,6 +212,14 @@ namespace Il2CppInspector } } + // Some field not implemented in class + catch (TargetInvocationException ex) when (ex.InnerException is MissingFieldException fEx) { + var name = Path.GetFileName(dll); + + disabledPlugin.Plugin = new InvalidPlugin { Name = name, Description = fEx.Message }; + Console.Error.WriteLine($"Error loading plugin {disabledPlugin.Plugin.Name}: {disabledPlugin.Plugin.Description} - this plugin may require a newer version of Il2CppInspector"); + } + // Ignore unmanaged DLLs catch (BadImageFormatException) { }