From 8d69cfc5d951f5556cd4c609d4184530b266b487 Mon Sep 17 00:00:00 2001 From: pas2704 Date: Sun, 3 Nov 2024 01:49:10 -0400 Subject: [PATCH] Add support for pluginloader config method to PluginInstance --- CringePlugins/Loader/PluginInstance.cs | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) diff --git a/CringePlugins/Loader/PluginInstance.cs b/CringePlugins/Loader/PluginInstance.cs index eb12577..33ec15c 100644 --- a/CringePlugins/Loader/PluginInstance.cs +++ b/CringePlugins/Loader/PluginInstance.cs @@ -7,9 +7,12 @@ namespace CringePlugins.Loader; internal sealed class PluginInstance { + public bool HasConfig => _openConfigAction != null; + private readonly string _entrypointPath; private PluginAssemblyLoadContext? _context; private IPlugin? _instance; + private Action? _openConfigAction; private IHandleInputPlugin? _handleInputInstance; public PluginMetadata Metadata { get; } @@ -40,6 +43,18 @@ internal sealed class PluginInstance throw new InvalidOperationException("Entrypoint contains multiple plugins"); _instance = (IPlugin) Activator.CreateInstance(plugins[0])!; + + var openConfigMethod = plugins[0].GetMethod("OpenConfigDialog"); + + if (openConfigMethod is not null) + { + //todo: log this and continue without action instead of throwing exception + if (openConfigMethod.ReturnType != typeof(void) || openConfigMethod.IsStatic || openConfigMethod.GetParameters().Length > 0) + throw new InvalidOperationException("OpenConfigDialog method has an incorrect signature"); + + _openConfigAction = openConfigMethod.CreateDelegate(_instance); + } + _handleInputInstance = _instance as IHandleInputPlugin; } @@ -52,4 +67,12 @@ internal sealed class PluginInstance if (_handleInputInstance is not null) MyPlugins.m_handleInputPlugins.Add(_handleInputInstance); } + + public void OpenConfig() + { + if (_openConfigAction is null) + throw new InvalidOperationException("Plugin does not have OpenConfigDialog method"); + + _openConfigAction(); + } } \ No newline at end of file