MagicStorageVoidBag/MagicStorageVoidBag.cs

57 lines
1.7 KiB
C#
Raw Permalink Normal View History

2022-07-02 11:38:45 +01:00
using Mono.Cecil.Cil;
using Mono.Cecil;
using MonoMod.Cil;
2022-07-02 04:35:56 +01:00
using Terraria.ModLoader;
2022-07-02 11:38:45 +01:00
using MagicStorageVoidBag.Items;
2022-07-02 04:35:56 +01:00
2022-07-02 11:38:45 +01:00
namespace MagicStorageVoidBag {
public class MagicStorageVoidBag : Mod {
public override void Load() {
IL.MagicStorage.Components.StorageHeart.RightClick += HeartRightClickPatch;
}
public override void Unload() {
base.Unload();
}
// Patch MagicStorage IL to change the Storage Heart right click handler to also work with
// MSVoidBag.
private void HeartRightClickPatch(ILContext il) {
if (il == null) {
Logger.Error("ILContext null!");
return;
}
Logger.Debug("Patching MagicStorage IL...");
var c = new ILCursor(il);
if (!c.TryGotoNext(i => i.MatchCallOrCallvirt("Terraria.ModLoader.ModContent", "ItemType"))) {
Logger.Warn("IL patching failed! :(");
}
c = c.GotoPrev().GotoPrev();
var c2 = c.Clone();
// copy IL up to call
while (c2.Next.OpCode != OpCodes.Call) {
if (c2.Next.Operand != null) {
c.Emit(c2.Next.OpCode, c2.Next.Operand);
} else {
c.Emit(c2.Next.OpCode);
}
c2 = c2.GotoNext();
}
var method = typeof(ModContent).GetMethod("ItemType").MakeGenericMethod(typeof(MSVoidBag));
c.Emit(c2.Next.OpCode, method);
c2.GotoNext();
c.Emit(c2.Next.OpCode, c2.Next.Operand);
Logger.Debug("...MagicStorage IL patching complete!");
}
}
2022-07-02 04:35:56 +01:00
}