Patch_SceneInjector_InjectIngame.cs 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. using GadgetCore;
  2. using GadgetCore.API;
  3. using GadgetCore.Util;
  4. using HarmonyLib;
  5. using System;
  6. using System.Collections.Generic;
  7. using System.Linq;
  8. using System.Reflection;
  9. using System.Reflection.Emit;
  10. using UnityEngine;
  11. namespace Ships.Patches
  12. {
  13. [HarmonyPatch]
  14. [HarmonyGadget("Ships_")]
  15. public static class Patch_SceneInjector_InjectIngame
  16. {
  17. [HarmonyTargetMethod]
  18. static MethodBase TargetMethod()
  19. {
  20. return AccessTools.Method(typeof(GadgetCoreAPI).Assembly.GetType("GadgetCore.SceneInjector"), "InjectIngame");
  21. }
  22. [HarmonyTranspiler]
  23. public static IEnumerable<CodeInstruction> Transpiler(IEnumerable<CodeInstruction> instructions, ILGenerator gen)
  24. {
  25. var p = TranspilerHelper.CreateProcessor(instructions, gen);
  26. {
  27. var ilRefs = p.FindAllRefsByInsns(new List<CodeInstruction>(new CodeInstruction[] {
  28. new CodeInstruction(OpCodes.Ldloc_0)
  29. , new CodeInstruction(OpCodes.Ldc_I4_1)
  30. , new CodeInstruction(OpCodes.Sub)
  31. }));
  32. MethodInfo changeListMethod = typeof(Patch_SceneInjector_InjectIngame).GetMethod("ChangeList");
  33. foreach (var ilRef in ilRefs)
  34. {
  35. p.InjectHook(ilRef, changeListMethod);
  36. }
  37. }
  38. return p.Insns;
  39. }
  40. private static FieldInfo selectorPlanetsField = typeof(PlanetRegistry).GetField("selectorPlanets");
  41. private static FieldInfo planetButtonIconsField = typeof(PlanetRegistry).GetField("planetButtonIcons");
  42. public static void ChangeList()
  43. {
  44. List<PlanetInfo> list = Registry<PlanetRegistry, PlanetInfo, PlanetType>.Singleton.ToList<PlanetInfo>();
  45. foreach (var info in list)
  46. if (info.GetID() == Core.shipPlanet.GetID())
  47. list.Remove(info);
  48. selectorPlanetsField.SetValue(null, list.ToArray());
  49. }
  50. }
  51. }