ClampPatches.cs 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  1. using HarmonyLib;
  2. using System;
  3. using System.Linq;
  4. using System.Collections.Generic;
  5. using System.Reflection;
  6. using System.Reflection.Emit;
  7. namespace SongCore.HarmonyPatches
  8. {
  9. [HarmonyPatch(typeof(BeatmapData))]
  10. [HarmonyPatch("AddBeatmapObjectData", MethodType.Normal)]
  11. class BeatmapDataLoaderGetBeatmapDataFromBeatmapSaveData
  12. {
  13. static readonly MethodInfo clampMethod = SymbolExtensions.GetMethodInfo(() => Clamp(0, 0, 0));
  14. static readonly CodeInstruction[] clampInstructions = new CodeInstruction[] { new CodeInstruction(OpCodes.Ldc_I4_0),
  15. new CodeInstruction(OpCodes.Ldc_I4_3), new CodeInstruction(OpCodes.Call, clampMethod) };
  16. static IEnumerable<CodeInstruction> Transpiler(IEnumerable<CodeInstruction> instructions)
  17. {
  18. List<CodeInstruction> instructionList = instructions.ToList();
  19. for (int i = 0; i < instructionList.Count; i++)
  20. {
  21. if (instructionList[i].opcode == OpCodes.Ldelem_Ref)
  22. {
  23. if (instructionList[i + 2].opcode != OpCodes.Callvirt || instructionList[i + 1].opcode != OpCodes.Ldarg_1)
  24. {
  25. continue;
  26. }
  27. // Type varType = ((LocalVariableInfo)(instructionList[i - 2].operand)).LocalType;
  28. // if (varType == typeof(BeatmapObjectData))
  29. // {
  30. Utilities.Logging.logger.Debug($"{i}Inserting Clamp Instruction for SaveData Reading");
  31. instructionList.InsertRange(i, clampInstructions);
  32. i += clampInstructions.Count();
  33. // }
  34. }
  35. }
  36. return instructionList.AsEnumerable();
  37. }
  38. static int Clamp(int input, int min, int max)
  39. {
  40. return Math.Min(Math.Max(input, min), max);
  41. }
  42. }
  43. [HarmonyPatch(typeof(NotesInTimeRowProcessor))]
  44. [HarmonyPatch("ProcessAllNotesInTimeRow", MethodType.Normal)]
  45. class NoteProcessorClampPatch
  46. {
  47. static readonly MethodInfo clampMethod = SymbolExtensions.GetMethodInfo(() => Clamp(0, 0, 0));
  48. static readonly CodeInstruction[] clampInstructions = new CodeInstruction[] { new CodeInstruction(OpCodes.Ldc_I4_0),
  49. new CodeInstruction(OpCodes.Ldc_I4_3), new CodeInstruction(OpCodes.Call, clampMethod) };
  50. static IEnumerable<CodeInstruction> Transpiler(IEnumerable<CodeInstruction> instructions)
  51. {
  52. List<CodeInstruction> instructionList = instructions.ToList();
  53. for (int i = 0; i < instructionList.Count; i++)
  54. {
  55. if (instructionList[i].opcode == OpCodes.Ldelem_Ref)
  56. {
  57. if (instructionList[i - 1].opcode != OpCodes.Callvirt || instructionList[i - 2].opcode != OpCodes.Ldloc_2)
  58. {
  59. continue;
  60. }
  61. // Type varType = ((LocalVariableInfo)(instructionList[i - 2].operand)).LocalType;
  62. // if (varType == typeof(BeatmapObjectData))
  63. // {
  64. Utilities.Logging.logger.Debug($"{i}Inserting Clamp Instruction for Note Processor");
  65. instructionList.InsertRange(i, clampInstructions);
  66. i += clampInstructions.Count();
  67. // }
  68. }
  69. }
  70. return instructionList.AsEnumerable();
  71. }
  72. static int Clamp(int input, int min, int max)
  73. {
  74. return Math.Min(Math.Max(input, min), max);
  75. }
  76. }
  77. [HarmonyPatch(typeof(BeatmapData))]
  78. [HarmonyPatch("beatmapObjectsData", MethodType.Getter)]
  79. class BeatmapObjectsDataClampPatch
  80. {
  81. public static bool Prefix(BeatmapLineData[] ____beatmapLinesData, BeatmapData __instance, ref IEnumerable<BeatmapObjectData> __result)
  82. {
  83. IEnumerable<BeatmapObjectData> getObjects(BeatmapLineData[] _beatmapLinesData)
  84. {
  85. BeatmapLineData[] beatmapLinesData = _beatmapLinesData;
  86. int[] idxs = new int[beatmapLinesData.Length];
  87. for (; ; )
  88. {
  89. BeatmapObjectData minBeatmapObjectData = null;
  90. float num = float.MaxValue;
  91. for (int i = 0; i < beatmapLinesData.Length; i++)
  92. {
  93. if (idxs[i] < beatmapLinesData[i].beatmapObjectsData.Count)
  94. {
  95. BeatmapObjectData beatmapObjectData = beatmapLinesData[i].beatmapObjectsData[idxs[i]];
  96. float time = beatmapObjectData.time;
  97. if (time < num)
  98. {
  99. num = time;
  100. minBeatmapObjectData = beatmapObjectData;
  101. }
  102. }
  103. }
  104. if (minBeatmapObjectData == null)
  105. {
  106. break;
  107. }
  108. yield return minBeatmapObjectData;
  109. idxs[minBeatmapObjectData.lineIndex > 3 ? 3 : minBeatmapObjectData.lineIndex < 0 ? 0 : minBeatmapObjectData.lineIndex]++;
  110. minBeatmapObjectData = null;
  111. }
  112. yield break;
  113. yield break;
  114. }
  115. __result = getObjects(____beatmapLinesData);
  116. return false;
  117. }
  118. /*
  119. static readonly MethodInfo clampMethod = SymbolExtensions.GetMethodInfo(() => Clamp(0, 0, 0));
  120. static readonly CodeInstruction[] clampInstructions = new CodeInstruction[] { new CodeInstruction(OpCodes.Ldc_I4_0),
  121. new CodeInstruction(OpCodes.Ldc_I4_3), new CodeInstruction(OpCodes.Call, clampMethod) };
  122. static IEnumerable<CodeInstruction> Transpiler(IEnumerable<CodeInstruction> instructions)
  123. {
  124. List<CodeInstruction> instructionList = instructions.ToList();
  125. for (int i = 0; i < instructionList.Count; i++)
  126. {
  127. Utilities.Logging.logger.Debug($"{instructionList[i].opcode}");
  128. if (instructionList[i].opcode == OpCodes.Ldelema)
  129. {
  130. if (instructionList[i - 1].opcode != OpCodes.Callvirt || instructionList[i - 2].opcode != OpCodes.Ldfld)
  131. {
  132. continue;
  133. }
  134. // Type varType = ((LocalVariableInfo)(instructionList[i - 2].operand)).LocalType;
  135. // if (varType == typeof(BeatmapObjectData))
  136. // {
  137. Utilities.Logging.logger.Debug($"{i}Inserting Clamp Instruction for BeatmapObjectsData Getter");
  138. instructionList.InsertRange(i, clampInstructions);
  139. i += clampInstructions.Count();
  140. // }
  141. }
  142. }
  143. return instructionList.AsEnumerable();
  144. }
  145. static int Clamp(int input, int min, int max)
  146. {
  147. return Math.Min(Math.Max(input, min), max);
  148. }
  149. */
  150. }
  151. }