add plugin name patch to prevent game from logging plugin wrapper type

This commit is contained in:
zznty
2024-11-09 20:18:29 +07:00
parent 36af9a722a
commit eac2a42d1e
3 changed files with 44 additions and 27 deletions

View File

@@ -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<CodeInstruction> Transpiler(IEnumerable<CodeInstruction> 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();
}
}

View File

@@ -8,27 +8,19 @@ using VRage.Plugins;
namespace CringePlugins.Loader; namespace CringePlugins.Loader;
internal sealed class PluginInstance internal sealed class PluginInstance(PluginMetadata metadata, string entrypointPath)
{ {
public bool HasConfig => _openConfigAction != null; public bool HasConfig => _openConfigAction != null;
private readonly string _entrypointPath;
private PluginAssemblyLoadContext? _context; private PluginAssemblyLoadContext? _context;
private IPlugin? _instance; private IPlugin? _instance;
private Action? _openConfigAction; private Action? _openConfigAction;
private IHandleInputPlugin? _handleInputInstance; public PluginWrapper? WrappedInstance { get; private set; }
private PluginWrapper? _wrappedInstance;
private static readonly ILogger Log = LogManager.GetCurrentClassLogger(); 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) 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) if (AssemblyLoadContext.GetLoadContext(typeof(PluginInstance).Assembly) is not ICoreLoadContext parentContext)
throw new NotSupportedException("Plugin instantiation is not supported in this context"); throw new NotSupportedException("Plugin instantiation is not supported in this context");
_context = new PluginAssemblyLoadContext(parentContext, _entrypointPath); _context = new PluginAssemblyLoadContext(parentContext, entrypointPath);
contextBuilder.Add(_context); contextBuilder.Add(_context);
var entrypoint = _context.LoadEntrypoint(); var entrypoint = _context.LoadEntrypoint();
@@ -67,8 +59,7 @@ internal sealed class PluginInstance
} }
} }
_handleInputInstance = _instance as IHandleInputPlugin; WrappedInstance = new PluginWrapper(Metadata, _instance);
_wrappedInstance = new PluginWrapper(Metadata.Name, _instance);
} }
public void RegisterLifetime() public void RegisterLifetime()
@@ -76,9 +67,9 @@ internal sealed class PluginInstance
if (_instance is null) if (_instance is null)
throw new InvalidOperationException("Must call Instantiate first"); throw new InvalidOperationException("Must call Instantiate first");
MyPlugins.m_plugins.Add(_wrappedInstance); MyPlugins.m_plugins.Add(WrappedInstance);
if (_handleInputInstance is not null) if (_instance is IHandleInputPlugin)
MyPlugins.m_handleInputPlugins.Add(_wrappedInstance); MyPlugins.m_handleInputPlugins.Add(WrappedInstance);
} }
public void OpenConfig() public void OpenConfig()
@@ -92,7 +83,7 @@ internal sealed class PluginInstance
} }
catch (Exception ex) catch (Exception ex)
{ {
Log.Error(ex, "Error opening config: {Exception}"); Log.Error(ex, "Error opening config");
} }
} }
} }

View File

@@ -3,14 +3,15 @@ using VRage.Plugins;
namespace CringePlugins.Loader; namespace CringePlugins.Loader;
//todo: we should patch Mysanboxgame.Run to log init of actual plugin names instead of pluginwrapper //todo maybe we could unload the plugin if there's an error?
//also, maybe we could unload the plugin if there's an error? internal sealed class PluginWrapper(PluginMetadata metadata, IPlugin plugin) : IHandleInputPlugin
internal sealed class PluginWrapper(string name, IPlugin plugin) : IHandleInputPlugin
{ {
public bool HasError => LastException != null; public bool HasError => LastException != null;
public Exception? LastException { get; private set; } //todo: show exception when hovered in plugin menu? public Exception? LastException { get; private set; } //todo: show exception when hovered in plugin menu?
private static readonly ILogger Log = LogManager.GetCurrentClassLogger(); private static readonly ILogger Log = LogManager.GetCurrentClassLogger();
private readonly IHandleInputPlugin? _handleInputPlugin = plugin as IHandleInputPlugin;
public void Dispose() public void Dispose()
{ {
@@ -20,23 +21,23 @@ internal sealed class PluginWrapper(string name, IPlugin plugin) : IHandleInputP
} }
catch (Exception e) catch (Exception e)
{ {
Log.Error(e, "Error Disposing {Name}", name); Log.Error(e, "Exception while Disposing {Metadata}", metadata);
LastException = e; LastException = e;
} }
} }
public void HandleInput() public void HandleInput()
{ {
if (HasError || plugin is not IHandleInputPlugin input) if (_handleInputPlugin is null || HasError)
return; return;
try try
{ {
input.HandleInput(); _handleInputPlugin.HandleInput();
} }
catch (Exception e) catch (Exception e)
{ {
Log.Error(e, "Error Updating {Name}", name); Log.Error(e, "Exception while Updating {Metadata}", metadata);
LastException = e; LastException = e;
} }
} }
@@ -49,7 +50,7 @@ internal sealed class PluginWrapper(string name, IPlugin plugin) : IHandleInputP
} }
catch (Exception e) catch (Exception e)
{ {
Log.Error(e, "Error Initializing {Name}", name); Log.Error(e, "Exception while Initializing {Metadata}", metadata);
LastException = e; LastException = e;
} }
} }
@@ -65,8 +66,10 @@ internal sealed class PluginWrapper(string name, IPlugin plugin) : IHandleInputP
} }
catch (Exception e) catch (Exception e)
{ {
Log.Error(e, "Error Updating {Name}", name); Log.Error(e, "Exception while Updating {Metadata}", metadata);
LastException = e; LastException = e;
} }
} }
public override string ToString() => metadata.ToString();
} }