Patch_GameScript_MultiPachSlots.cs 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. using GadgetCore.API;
  2. using GadgetCore.Util;
  3. using HarmonyLib;
  4. using System.Collections.Generic;
  5. using System.Reflection;
  6. using System.Reflection.Emit;
  7. using UnityEngine;
  8. namespace StorageExpansion.Patches
  9. {
  10. internal class SlotsPerPageGetter
  11. {
  12. public static int SlotsPerPage { get => Core.settingAmountSlots; }
  13. }
  14. [HarmonyPatch()]
  15. [HarmonyGadget("StorageExpansion")]
  16. public static class Patch_GameScript_MultiPachSlots
  17. {
  18. [HarmonyTargetMethods]
  19. static IEnumerable<MethodBase> TargetMethods()
  20. {
  21. yield return AccessTools.Method(typeof(GameScript), "Update");
  22. yield return AccessTools.Method(typeof(GameScript), "PlaceItemStorage");
  23. yield return AccessTools.Method(typeof(GameScript), "PlaceOneItemStorage");
  24. yield return AccessTools.Method(typeof(GameScript), "SelectItemStorage");
  25. yield return AccessTools.Method(typeof(GameScript), "SplitItemStorage");
  26. yield return AccessTools.Method(typeof(GameScript), "CombineItemStorage");
  27. yield return AccessTools.Method(typeof(GameScript), "SwapItemStorage");
  28. yield return AccessTools.Method(typeof(GameScript), "HoverItemStorage");
  29. yield return AccessTools.Method(typeof(GadgetCoreAPI).Assembly.GetType("GadgetCore.Patches.Patch_GameScript_RefreshSlotStorage"), "Prefix");
  30. yield return AccessTools.Method(typeof(GadgetCoreAPI).Assembly.GetType("GadgetCore.Patches.Patch_GameScript_RefreshSlotStorage"), "Postfix");
  31. yield return AccessTools.Method(typeof(GadgetCoreAPI).Assembly.GetType("GadgetCore.Patches.Patch_GameScript_ShiftClick"), "Prefix");
  32. yield return AccessTools.Method(typeof(GadgetCoreAPI).Assembly.GetType("GadgetCore.Patches.Patch_GameScript_HoverItemStorage"), "Prefix");
  33. yield return AccessTools.Method(typeof(GadgetCoreAPI).Assembly.GetType("GadgetCore.Patches.Patch_GameScript_CombineItemStorage"), "Prefix");
  34. yield return AccessTools.Method(typeof(GadgetCoreAPI).Assembly.GetType("GadgetCore.Patches.Patch_GameScript_PlaceOneItemStorage"), "Prefix");
  35. yield return AccessTools.Method(typeof(GadgetCoreAPI).Assembly.GetType("GadgetCore.Patches.Patch_GameScript_SplitItemStorage"), "Prefix");
  36. yield return AccessTools.Method(typeof(GadgetCoreAPI).Assembly.GetType("GadgetCore.Patches.Patch_GameScript_ShiftClickStorage"), "Prefix");
  37. yield return AccessTools.Method(typeof(GadgetCoreAPI).Assembly.GetType("GadgetCore.Patches.Patch_GameScript_RefreshStoragePage"), "Prefix");
  38. }
  39. [HarmonyTranspiler]
  40. public static IEnumerable<CodeInstruction> Transpiler(IEnumerable<CodeInstruction> instructions, ILGenerator gen)
  41. {
  42. var p = TranspilerHelper.CreateProcessor(instructions, gen);
  43. var ilRefs = p.FindAllRefsByInsn(new CodeInstruction(OpCodes.Ldc_I4_S, 30));
  44. MethodInfo getValue = typeof(SlotsPerPageGetter).GetProperty("SlotsPerPage").GetGetMethod();
  45. foreach (var ilRef in ilRefs)
  46. {
  47. p.InjectInsn(ilRef, new CodeInstruction(OpCodes.Call, getValue), false);
  48. }
  49. return p.Insns;
  50. }
  51. }
  52. }