SliderUnlocker.cs 5.8 KB

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