SliderUnlocker.cs 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  1. using BepInEx;
  2. using ChaCustom;
  3. using Harmony;
  4. using System;
  5. using System.Collections.Generic;
  6. using System.Linq;
  7. using System.Reflection;
  8. using System.Text;
  9. using UnityEngine;
  10. using UnityEngine.SceneManagement;
  11. using UnityEngine.UI;
  12. namespace SliderUnlocker
  13. {
  14. public class SliderUnlocker : BaseUnityPlugin
  15. {
  16. public override string Name => "Slider Unlocker";
  17. public SliderUnlocker()
  18. {
  19. var harmony = HarmonyInstance.Create("com.bepis.bepinex.sliderunlocker");
  20. MethodInfo original = AccessTools.Method(typeof(CustomBase), "ConvertTextFromRate");
  21. HarmonyMethod postfix = new HarmonyMethod(typeof(SliderUnlocker).GetMethod("ConvertTextFromRateHook"));
  22. harmony.Patch(original, null, postfix);
  23. original = AccessTools.Method(typeof(CustomBase), "ConvertRateFromText");
  24. postfix = new HarmonyMethod(typeof(SliderUnlocker).GetMethod("ConvertRateFromTextHook"));
  25. harmony.Patch(original, null, postfix);
  26. original = AccessTools.Method(typeof(Mathf), "Clamp", new Type[] { typeof(float), typeof(float), typeof(float) });
  27. postfix = new HarmonyMethod(typeof(SliderUnlocker).GetMethod("MathfClampHook"));
  28. harmony.Patch(original, null, postfix);
  29. original = typeof(AnimationKeyInfo).GetMethods().Where(x => x.Name.Contains("GetInfo")).ToArray()[1];
  30. var prefix = new HarmonyMethod(typeof(SliderUnlocker).GetMethod("GetInfoPreHook"));
  31. postfix = new HarmonyMethod(typeof(SliderUnlocker).GetMethod("GetInfoPostHook"));
  32. harmony.Patch(original, prefix, postfix);
  33. }
  34. protected override void LevelFinishedLoading(Scene scene, LoadSceneMode mode)
  35. {
  36. foreach (Slider gameObject in GameObject.FindObjectsOfType<Slider>())
  37. {
  38. gameObject.maxValue = 2f;
  39. }
  40. }
  41. [HarmonyPostfix]
  42. public static void ConvertTextFromRateHook(ref string __result, int min, int max, float value)
  43. {
  44. if (min == 0 && max == 100)
  45. __result = Math.Round(100 * value).ToString();
  46. }
  47. [HarmonyPostfix]
  48. public static void ConvertRateFromTextHook(ref float __result, int min, int max, string buf)
  49. {
  50. if (min == 0 && max == 100)
  51. {
  52. if (buf.IsNullOrEmpty())
  53. {
  54. __result = 0f;
  55. }
  56. else
  57. {
  58. if (!float.TryParse(buf, out float val))
  59. {
  60. __result = 0f;
  61. }
  62. else
  63. {
  64. __result = val / 100;
  65. }
  66. }
  67. }
  68. }
  69. [HarmonyPostfix]
  70. public static void MathfClampHook(ref float __result, float value, float min, float max)
  71. {
  72. if (min == 0f && max == 100f)
  73. __result = value;
  74. }
  75. [HarmonyPrefix]
  76. public static void GetInfoPreHook(ref float __state, string name, ref float rate, ref Vector3[] value, bool[] flag)
  77. {
  78. __state = rate;
  79. if (rate > 1)
  80. rate = 1f;
  81. }
  82. [HarmonyPostfix]
  83. public static void GetInfoPostHook(AnimationKeyInfo __instance, bool __result, float __state, string name, float rate, ref Vector3[] value, bool[] flag)
  84. {
  85. if (!__result)
  86. return;
  87. rate = __state;
  88. var dictInfo = (Dictionary < string, List<AnimationKeyInfo.AnmKeyInfo> > )
  89. (typeof(AnimationKeyInfo).GetField("dictInfo", BindingFlags.NonPublic | BindingFlags.Instance).GetValue(__instance));
  90. List<AnimationKeyInfo.AnmKeyInfo> list = dictInfo[name];
  91. //if (flag[0])
  92. //{
  93. // Vector3 min = list[0].pos;
  94. // Vector3 max = list[list.Count - 1].pos;
  95. // Vector3 diff = new Vector3(max.x - min.x,
  96. // max.y - min.y,
  97. // max.z - min.z);
  98. // value[0] = new Vector3(min.x + (diff.x * rate),
  99. // min.y + (diff.y * rate),
  100. // min.z + (diff.z * rate));
  101. //}
  102. //if (flag[1])
  103. //{
  104. // //if (rate == 0f)
  105. // //{
  106. // // value[1] = list[0].rot;
  107. // //}
  108. // //else if (rate == 1f)
  109. // //{
  110. // // value[1] = list[list.Count - 1].rot;
  111. // //}
  112. // //else
  113. // //{
  114. // // float num3 = (float)(list.Count - 1) * rate;
  115. // // int num4 = Mathf.FloorToInt(num3);
  116. // // float t2 = num3 - (float)num4;
  117. // // value[1].x = Mathf.LerpAngle(list[num4].rot.x, list[num4 + 1].rot.x, t2);
  118. // // value[1].y = Mathf.LerpAngle(list[num4].rot.y, list[num4 + 1].rot.y, t2);
  119. // // value[1].z = Mathf.LerpAngle(list[num4].rot.z, list[num4 + 1].rot.z, t2);
  120. // //}
  121. // Vector3 min = list[0].rot;
  122. // Vector3 max = list[list.Count - 1].rot;
  123. // Vector3 diff = new Vector3(max.x - min.x,
  124. // max.y - min.y,
  125. // max.z - min.z);
  126. // value[1] = new Vector3(min.x + (diff.x * rate),
  127. // min.y + (diff.y * rate),
  128. // min.z + (diff.z * rate));
  129. //}
  130. if (flag[2])
  131. {
  132. Vector3 min = list[0].scl;
  133. Vector3 max = list[list.Count - 1].scl;
  134. Vector3 diff = new Vector3(max.x - min.x,
  135. max.y - min.y,
  136. max.z - min.z);
  137. value[2] = new Vector3(min.x + (diff.x * rate),
  138. min.y + (diff.y * rate),
  139. min.z + (diff.z * rate));
  140. }
  141. }
  142. }
  143. }