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,26 +8,18 @@ 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 PluginInstance(PluginMetadata metadata, string entrypointPath)
{
_entrypointPath = entrypointPath;
Metadata = metadata;
}
public PluginMetadata Metadata { get; } = 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");
}
}
}

View File

@@ -3,15 +3,16 @@ 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()
{
try
@@ -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();
}