|
| 1 | +using System; |
| 2 | +using System.Collections.Generic; |
| 3 | +using System.Reflection; |
| 4 | +using System.Reflection.Emit; |
| 5 | +using HarmonyLib; |
| 6 | + |
| 7 | +namespace KSPCommunityFixes.QoL |
| 8 | +{ |
| 9 | + class TargetParentBody : BasePatch |
| 10 | + { |
| 11 | + protected override Version VersionMin => new Version(1, 8, 0); |
| 12 | + |
| 13 | + protected override void ApplyPatches() |
| 14 | + { |
| 15 | + AddPatch(PatchType.Prefix, typeof(OrbitTargeter), nameof(OrbitTargeter.DropInvalidTargets)); |
| 16 | + |
| 17 | + AddPatch(PatchType.Transpiler, typeof(FlightGlobals), nameof(FlightGlobals.UpdateInformation)); |
| 18 | + } |
| 19 | + |
| 20 | + // OrbitTargeter.DropInvalidTargets is only ever used to clear the target if the target is in the Parent body heriarchy |
| 21 | + // Always return false and do not run the base method |
| 22 | + private static bool OrbitTargeter_DropInvalidTargets_Prefix(bool __result) |
| 23 | + { |
| 24 | + __result = false; |
| 25 | + return false; |
| 26 | + } |
| 27 | + |
| 28 | + // FlightGlobals.UpdateInformation contains two sequential if statements that each make a call to FlightGlobals.SetVesselTarget |
| 29 | + // to clear the target if the target is the direct parent of the vessel or in the parent body heriarchy |
| 30 | + // Starting from the end of the if statement making the second call to FlightGlobals.SetVesselTarget, iterate backwards and |
| 31 | + // replace opcodes with Nop until reaching the beginning of the first if statement |
| 32 | + static IEnumerable<CodeInstruction> FlightGlobals_UpdateInformation_Transpiler(IEnumerable<CodeInstruction> instructions) |
| 33 | + { |
| 34 | + FieldInfo FlightGlobals_currentMainBody = AccessTools.Field(typeof(FlightGlobals), nameof(FlightGlobals.currentMainBody)); |
| 35 | + MethodInfo FlightGlobals_SetVesselTarget = AccessTools.Method(typeof(FlightGlobals), nameof(FlightGlobals.SetVesselTarget)); |
| 36 | + |
| 37 | + List<CodeInstruction> code = new List<CodeInstruction>(instructions); |
| 38 | + int numFlightGlobals_currentMainBodysSeen = 0; |
| 39 | + int numFlightGlobals_SetVesselTargetsSeen = 0; |
| 40 | + for (int i = 1; i < code.Count; i++) |
| 41 | + { |
| 42 | + if (code[i].opcode == OpCodes.Br |
| 43 | + && code[i - 1].opcode == OpCodes.Call |
| 44 | + && ReferenceEquals(code[i - 1].operand, FlightGlobals_SetVesselTarget)) |
| 45 | + { |
| 46 | + numFlightGlobals_SetVesselTargetsSeen++; |
| 47 | + if (numFlightGlobals_SetVesselTargetsSeen == 2) |
| 48 | + { |
| 49 | + for (int j = i; j >= 0; j--) |
| 50 | + { |
| 51 | + if (code[j].opcode == OpCodes.Ldsfld && ReferenceEquals(code[j].operand, FlightGlobals_currentMainBody)) |
| 52 | + { |
| 53 | + numFlightGlobals_currentMainBodysSeen++; |
| 54 | + } |
| 55 | + OpCode opcode = code[j].opcode; |
| 56 | + code[j].opcode = OpCodes.Nop; |
| 57 | + code[j].operand = null; |
| 58 | + if (numFlightGlobals_currentMainBodysSeen == 2 && opcode == OpCodes.Ldarg_0) |
| 59 | + { |
| 60 | + break; |
| 61 | + } |
| 62 | + } |
| 63 | + break; |
| 64 | + } |
| 65 | + } |
| 66 | + } |
| 67 | + return code; |
| 68 | + } |
| 69 | + } |
| 70 | +} |
0 commit comments