RePatch_GameScript_Update.cs 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. using GadgetCore.API;
  2. using GadgetCore.Util;
  3. using HarmonyLib;
  4. using System;
  5. using System.Collections.Generic;
  6. using System.Reflection;
  7. using System.Reflection.Emit;
  8. using UnityEngine;
  9. namespace StorageExpansion.Patches
  10. {
  11. [HarmonyPatch()]
  12. [HarmonyGadget("StorageExpansion")]
  13. public static class RePatch_GameScript_Update
  14. {
  15. [HarmonyPrepare]
  16. static bool Prepare()
  17. {
  18. foreach (var a in AppDomain.CurrentDomain.GetAssemblies())
  19. {
  20. if (a.FullName.StartsWith("QuickStack,"))
  21. {
  22. return true;
  23. }
  24. }
  25. return false;
  26. }
  27. [HarmonyTargetMethods]
  28. static IEnumerable<MethodBase> TargetMethods()
  29. {
  30. foreach (var a in AppDomain.CurrentDomain.GetAssemblies())
  31. {
  32. if (a.FullName.StartsWith("QuickStack,"))
  33. {
  34. yield return AccessTools.Method(a.GetType("QuickStack.Patches.Patch_GameScript_Update"), "Prefix");
  35. }
  36. }
  37. yield break;
  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. }