|
| 1 | +using System.Collections.Generic; |
| 2 | +using System.Reflection; |
| 3 | +using System.Reflection.Emit; |
| 4 | +using HarmonyLib; |
| 5 | +using HMUI; |
| 6 | + |
| 7 | +namespace BeatSaberMarkupLanguage.Harmony_Patches |
| 8 | +{ |
| 9 | + [HarmonyPatch] |
| 10 | + internal class TableView_GetMinMaxVisibleIdx |
| 11 | + { |
| 12 | + private static readonly FieldInfo TableTypeField = AccessTools.DeclaredField(typeof(TableView), nameof(TableView._tableType)); |
| 13 | + private static readonly FieldInfo ScrollViewField = AccessTools.DeclaredField(typeof(TableView), nameof(TableView._scrollView)); |
| 14 | + private static readonly MethodInfo PositionPropertyGetter = AccessTools.DeclaredPropertyGetter(typeof(ScrollView), nameof(ScrollView.position)); |
| 15 | + |
| 16 | + private static IEnumerable<MethodBase> TargetMethods() |
| 17 | + { |
| 18 | + yield return AccessTools.DeclaredMethod(typeof(TableView), "GetMinVisibleIdx"); |
| 19 | + yield return AccessTools.DeclaredMethod(typeof(TableView), "GetMaxVisibleIdx"); |
| 20 | + } |
| 21 | + |
| 22 | + private static IEnumerable<CodeInstruction> Transpiler(IEnumerable<CodeInstruction> instructions) |
| 23 | + { |
| 24 | + return new CodeMatcher(instructions) |
| 25 | + .MatchForward( |
| 26 | + false, |
| 27 | + new CodeMatch(OpCodes.Ldarg_0), |
| 28 | + new CodeMatch(i => i.LoadsField(TableTypeField)), |
| 29 | + new CodeMatch(i => i.Branches(out Label? _)), |
| 30 | + new CodeMatch(OpCodes.Ldarg_0), |
| 31 | + new CodeMatch(i => i.LoadsField(ScrollViewField)), |
| 32 | + new CodeMatch(i => i.Calls(PositionPropertyGetter)), |
| 33 | + new CodeMatch(OpCodes.Neg), |
| 34 | + new CodeMatch(i => i.Branches(out Label? _))) |
| 35 | + .ThrowIfInvalid("Ternary operator not found") |
| 36 | + .RemoveInstructions(8) |
| 37 | + .InstructionEnumeration(); |
| 38 | + } |
| 39 | + } |
| 40 | +} |
0 commit comments