Hooks.cs 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  1. using ChaCustom;
  2. using Harmony;
  3. using System;
  4. using System.Collections.Generic;
  5. using System.Linq;
  6. using System.Reflection;
  7. using UnityEngine;
  8. namespace SliderUnlocker
  9. {
  10. public static class Hooks
  11. {
  12. public static void InstallHooks()
  13. {
  14. var harmony = HarmonyInstance.Create("com.bepis.bepinex.sliderunlocker");
  15. MethodInfo original = AccessTools.Method(typeof(CustomBase), "ConvertTextFromRate");
  16. HarmonyMethod postfix = new HarmonyMethod(typeof(Hooks).GetMethod("ConvertTextFromRateHook"));
  17. harmony.Patch(original, null, postfix);
  18. original = AccessTools.Method(typeof(CustomBase), "ConvertRateFromText");
  19. postfix = new HarmonyMethod(typeof(Hooks).GetMethod("ConvertRateFromTextHook"));
  20. harmony.Patch(original, null, postfix);
  21. original = AccessTools.Method(typeof(Mathf), "Clamp", new Type[] { typeof(float), typeof(float), typeof(float) });
  22. postfix = new HarmonyMethod(typeof(Hooks).GetMethod("MathfClampHook"));
  23. harmony.Patch(original, null, postfix);
  24. original = typeof(AnimationKeyInfo).GetMethods().Where(x => x.Name.Contains("GetInfo")).ToArray()[0];
  25. var prefix = new HarmonyMethod(typeof(Hooks).GetMethod("GetInfoSingularPreHook"));
  26. postfix = new HarmonyMethod(typeof(Hooks).GetMethod("GetInfoSingularPostHook"));
  27. harmony.Patch(original, prefix, postfix);
  28. original = typeof(AnimationKeyInfo).GetMethods().Where(x => x.Name.Contains("GetInfo")).ToArray()[1];
  29. prefix = new HarmonyMethod(typeof(Hooks).GetMethod("GetInfoPreHook"));
  30. postfix = new HarmonyMethod(typeof(Hooks).GetMethod("GetInfoPostHook"));
  31. harmony.Patch(original, prefix, postfix);
  32. }
  33. private static FieldInfo akf_dictInfo = (typeof(AnimationKeyInfo).GetField("dictInfo", BindingFlags.NonPublic | BindingFlags.Instance));
  34. public static void ConvertTextFromRateHook(ref string __result, int min, int max, float value)
  35. {
  36. if (min == 0 && max == 100)
  37. __result = Math.Round(100 * value).ToString();
  38. }
  39. public static void ConvertRateFromTextHook(ref float __result, int min, int max, string buf)
  40. {
  41. if (min == 0 && max == 100)
  42. {
  43. if (buf.IsNullOrEmpty())
  44. {
  45. __result = 0f;
  46. }
  47. else
  48. {
  49. if (!float.TryParse(buf, out float val))
  50. {
  51. __result = 0f;
  52. }
  53. else
  54. {
  55. __result = val / 100;
  56. }
  57. }
  58. }
  59. }
  60. public static void MathfClampHook(ref float __result, float value, float min, float max)
  61. {
  62. if (min == 0f && max == 100f)
  63. __result = value;
  64. }
  65. public static void GetInfoPreHook(ref float __state, string name, ref float rate, ref Vector3[] value, bool[] flag)
  66. {
  67. __state = rate;
  68. if (rate > 1)
  69. rate = 1f;
  70. if (rate < 0)
  71. rate = 0f;
  72. }
  73. public static void GetInfoSingularPreHook(ref float __state, string name, ref float rate, ref Vector3 value, byte type)
  74. {
  75. __state = rate;
  76. if (rate > 1)
  77. rate = 1f;
  78. if (rate < 0)
  79. rate = 0f;
  80. }
  81. public static void GetInfoSingularPostHook(AnimationKeyInfo __instance, bool __result, float __state, string name, float rate, ref Vector3 value, byte type)
  82. {
  83. if (!__result)
  84. return;
  85. rate = __state;
  86. if (rate < 0f || rate > 1f)
  87. {
  88. var dictInfo = (Dictionary<string, List<AnimationKeyInfo.AnmKeyInfo>>)akf_dictInfo.GetValue(__instance);
  89. List<AnimationKeyInfo.AnmKeyInfo> list = dictInfo[name];
  90. switch (type)
  91. {
  92. case 0:
  93. value = SliderMath.CalculatePosition(list, rate);
  94. break;
  95. case 1:
  96. value = SliderMath.SafeCalculateRotation(value, name, list, rate);
  97. break;
  98. default:
  99. value = SliderMath.CalculateScale(list, rate);
  100. break;
  101. }
  102. }
  103. }
  104. public static void GetInfoPostHook(AnimationKeyInfo __instance, bool __result, float __state, string name, float rate, ref Vector3[] value, bool[] flag)
  105. {
  106. if (!__result)
  107. return;
  108. rate = __state;
  109. if (rate < 0f || rate > 1f)
  110. {
  111. var dictInfo = (Dictionary<string, List<AnimationKeyInfo.AnmKeyInfo>>)akf_dictInfo.GetValue(__instance);
  112. List<AnimationKeyInfo.AnmKeyInfo> list = dictInfo[name];
  113. if (flag[0])
  114. {
  115. value[0] = SliderMath.CalculatePosition(list, rate);
  116. }
  117. if (flag[1])
  118. {
  119. value[1] = SliderMath.SafeCalculateRotation(value[1], name, list, rate);
  120. }
  121. if (flag[2])
  122. {
  123. value[2] = SliderMath.CalculateScale(list, rate);
  124. }
  125. }
  126. }
  127. }
  128. }