From eac2a42d1e1a8ccef98ad53212480dc407d3feb4 Mon Sep 17 00:00:00 2001 From: zznty <94796179+zznty@users.noreply.github.com> Date: Sat, 9 Nov 2024 20:18:29 +0700 Subject: [PATCH] add plugin name patch to prevent game from logging plugin wrapper type --- CringeLauncher/Patches/PluginNamePatch.cs | 23 +++++++++++++++++++ CringePlugins/Loader/PluginInstance.cs | 27 ++++++++--------------- CringePlugins/Loader/PluginWrapper.cs | 21 ++++++++++-------- 3 files changed, 44 insertions(+), 27 deletions(-) create mode 100644 CringeLauncher/Patches/PluginNamePatch.cs diff --git a/CringeLauncher/Patches/PluginNamePatch.cs b/CringeLauncher/Patches/PluginNamePatch.cs new file mode 100644 index 0000000..99c5b73 --- /dev/null +++ b/CringeLauncher/Patches/PluginNamePatch.cs @@ -0,0 +1,23 @@ +using System.Reflection.Emit; +using HarmonyLib; +using Sandbox; + +namespace CringeLauncher.Patches; + +[HarmonyPatch(typeof(MySandboxGame), nameof(MySandboxGame.Run))] +public static class PluginNamePatch +{ + private static IEnumerable Transpiler(IEnumerable instructions) + { + // replaces $"Plugin Init: {plugin.GetType()}" + // to be just $"Plugin Init: {plugin}" + // so you could override .ToString + // doesn't change default behavior since base .ToString is .GetType().ToString() + + return new CodeMatcher(instructions) + .SearchForward(b => b.Is(OpCodes.Ldstr, "Plugin Init: ")) + .Advance(2) + .Set(OpCodes.Callvirt, AccessTools.DeclaredMethod(typeof(object), nameof(ToString))) + .InstructionEnumeration(); + } +} \ No newline at end of file diff --git a/CringePlugins/Loader/PluginInstance.cs b/CringePlugins/Loader/PluginInstance.cs index bd63878..114024b 100644 --- a/CringePlugins/Loader/PluginInstance.cs +++ b/CringePlugins/Loader/PluginInstance.cs @@ -8,27 +8,19 @@ using VRage.Plugins; namespace CringePlugins.Loader; -internal sealed class PluginInstance +internal sealed class PluginInstance(PluginMetadata metadata, string entrypointPath) { public bool HasConfig => _openConfigAction != null; - private readonly string _entrypointPath; private PluginAssemblyLoadContext? _context; private IPlugin? _instance; private Action? _openConfigAction; - private IHandleInputPlugin? _handleInputInstance; - private PluginWrapper? _wrappedInstance; + public PluginWrapper? WrappedInstance { get; private set; } private static readonly ILogger Log = LogManager.GetCurrentClassLogger(); - public PluginMetadata Metadata { get; } + public PluginMetadata Metadata { get; } = metadata; - public PluginInstance(PluginMetadata metadata, string entrypointPath) - { - _entrypointPath = entrypointPath; - Metadata = metadata; - } - public PluginInstance(string entrypointPath) : this(PluginMetadata.ReadFromEntrypoint(entrypointPath), entrypointPath) { } @@ -38,7 +30,7 @@ internal sealed class PluginInstance if (AssemblyLoadContext.GetLoadContext(typeof(PluginInstance).Assembly) is not ICoreLoadContext parentContext) throw new NotSupportedException("Plugin instantiation is not supported in this context"); - _context = new PluginAssemblyLoadContext(parentContext, _entrypointPath); + _context = new PluginAssemblyLoadContext(parentContext, entrypointPath); contextBuilder.Add(_context); var entrypoint = _context.LoadEntrypoint(); @@ -67,8 +59,7 @@ internal sealed class PluginInstance } } - _handleInputInstance = _instance as IHandleInputPlugin; - _wrappedInstance = new PluginWrapper(Metadata.Name, _instance); + WrappedInstance = new PluginWrapper(Metadata, _instance); } public void RegisterLifetime() @@ -76,9 +67,9 @@ internal sealed class PluginInstance if (_instance is null) throw new InvalidOperationException("Must call Instantiate first"); - MyPlugins.m_plugins.Add(_wrappedInstance); - if (_handleInputInstance is not null) - MyPlugins.m_handleInputPlugins.Add(_wrappedInstance); + MyPlugins.m_plugins.Add(WrappedInstance); + if (_instance is IHandleInputPlugin) + MyPlugins.m_handleInputPlugins.Add(WrappedInstance); } public void OpenConfig() @@ -92,7 +83,7 @@ internal sealed class PluginInstance } catch (Exception ex) { - Log.Error(ex, "Error opening config: {Exception}"); + Log.Error(ex, "Error opening config"); } } } \ No newline at end of file diff --git a/CringePlugins/Loader/PluginWrapper.cs b/CringePlugins/Loader/PluginWrapper.cs index 002231b..fccca66 100644 --- a/CringePlugins/Loader/PluginWrapper.cs +++ b/CringePlugins/Loader/PluginWrapper.cs @@ -3,14 +3,15 @@ using VRage.Plugins; namespace CringePlugins.Loader; -//todo: we should patch Mysanboxgame.Run to log init of actual plugin names instead of pluginwrapper -//also, maybe we could unload the plugin if there's an error? -internal sealed class PluginWrapper(string name, IPlugin plugin) : IHandleInputPlugin +//todo maybe we could unload the plugin if there's an error? +internal sealed class PluginWrapper(PluginMetadata metadata, IPlugin plugin) : IHandleInputPlugin { public bool HasError => LastException != null; public Exception? LastException { get; private set; } //todo: show exception when hovered in plugin menu? private static readonly ILogger Log = LogManager.GetCurrentClassLogger(); + + private readonly IHandleInputPlugin? _handleInputPlugin = plugin as IHandleInputPlugin; public void Dispose() { @@ -20,23 +21,23 @@ internal sealed class PluginWrapper(string name, IPlugin plugin) : IHandleInputP } catch (Exception e) { - Log.Error(e, "Error Disposing {Name}", name); + Log.Error(e, "Exception while Disposing {Metadata}", metadata); LastException = e; } } public void HandleInput() { - if (HasError || plugin is not IHandleInputPlugin input) + if (_handleInputPlugin is null || HasError) return; try { - input.HandleInput(); + _handleInputPlugin.HandleInput(); } catch (Exception e) { - Log.Error(e, "Error Updating {Name}", name); + Log.Error(e, "Exception while Updating {Metadata}", metadata); LastException = e; } } @@ -49,7 +50,7 @@ internal sealed class PluginWrapper(string name, IPlugin plugin) : IHandleInputP } catch (Exception e) { - Log.Error(e, "Error Initializing {Name}", name); + Log.Error(e, "Exception while Initializing {Metadata}", metadata); LastException = e; } } @@ -65,8 +66,10 @@ internal sealed class PluginWrapper(string name, IPlugin plugin) : IHandleInputP } catch (Exception e) { - Log.Error(e, "Error Updating {Name}", name); + Log.Error(e, "Exception while Updating {Metadata}", metadata); LastException = e; } } + + public override string ToString() => metadata.ToString(); }