SliderUnlocker.cs 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  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. if (rate < 0f || rate > 1f)
  89. {
  90. var dictInfo = (Dictionary<string, List<AnimationKeyInfo.AnmKeyInfo>>)
  91. (typeof(AnimationKeyInfo).GetField("dictInfo", BindingFlags.NonPublic | BindingFlags.Instance).GetValue(__instance));
  92. List<AnimationKeyInfo.AnmKeyInfo> list = dictInfo[name];
  93. if (flag[2])
  94. {
  95. Vector3 min = list[0].scl;
  96. Vector3 max = list[list.Count - 1].scl;
  97. value[2] = new Vector3(min.x + ((max.x - min.x) * rate),
  98. min.y + ((max.y - min.y) * rate),
  99. min.z + ((max.z - min.z) * rate));
  100. }
  101. }
  102. }
  103. }
  104. }