|
| 1 | +using HarmonyLib; |
| 2 | +using KSP.UI.Screens.DebugToolbar; |
| 3 | +using System.Collections; |
| 4 | +using System.Collections.Generic; |
| 5 | +using System.Reflection; |
| 6 | +using System.Reflection.Emit; |
| 7 | +using UnityEngine.EventSystems; |
| 8 | + |
| 9 | +namespace KSPCommunityFixes.BugFixes |
| 10 | +{ |
| 11 | + internal class DebugConsoleDontStealInput : BasePatch |
| 12 | + { |
| 13 | + protected override void ApplyPatches() |
| 14 | + { |
| 15 | + AddPatch(PatchType.Transpiler, typeof(DebugScreenConsole), nameof(DebugScreenConsole.OnEnable), nameof(ScrollDownTranspiler)); |
| 16 | + AddPatch(PatchType.Transpiler, typeof(DebugScreenConsole), nameof(DebugScreenConsole.SubmitCommand), nameof(ScrollDownTranspiler)); |
| 17 | + AddPatch(PatchType.Transpiler, typeof(DebugScreenConsole), nameof(DebugScreenConsole.OnMemoryLogUpdated), nameof(ScrollDownTranspiler)); |
| 18 | + } |
| 19 | + |
| 20 | + private static IEnumerable<CodeInstruction> ScrollDownTranspiler(IEnumerable<CodeInstruction> instructions) |
| 21 | + { |
| 22 | + MethodInfo mScrollDownOriginal = AccessTools.Method(typeof(DebugScreenConsole), nameof(DebugScreenConsole.ScrollDown)); |
| 23 | + MethodInfo mScrollDownPatched = AccessTools.Method(typeof(DebugConsoleDontStealInput), nameof(ScrollDownPatched)); |
| 24 | + |
| 25 | + foreach (CodeInstruction il in instructions) |
| 26 | + { |
| 27 | + if (il.opcode == OpCodes.Call && ReferenceEquals(il.operand, mScrollDownOriginal)) |
| 28 | + il.operand = mScrollDownPatched; |
| 29 | + |
| 30 | + yield return il; |
| 31 | + } |
| 32 | + } |
| 33 | + |
| 34 | + private static IEnumerator ScrollDownPatched(DebugScreenConsole console) |
| 35 | + { |
| 36 | + yield return null; |
| 37 | + |
| 38 | + if (console.scrollRect != null) |
| 39 | + console.scrollRect.verticalNormalizedPosition = 0f; |
| 40 | + |
| 41 | + EventSystem.current.SetSelectedGameObject(console.inputField.gameObject, null); |
| 42 | + //console.inputField.OnPointerClick(new PointerEventData(EventSystem.current)); |
| 43 | + } |
| 44 | + } |
| 45 | +} |
0 commit comments