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();
}
}