SliderUnlocker.cs 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  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. private static FieldInfo akf_dictInfo = (typeof(AnimationKeyInfo).GetField("dictInfo", BindingFlags.NonPublic | BindingFlags.Instance));
  18. public SliderUnlocker()
  19. {
  20. var harmony = HarmonyInstance.Create("com.bepis.bepinex.sliderunlocker");
  21. MethodInfo original = AccessTools.Method(typeof(CustomBase), "ConvertTextFromRate");
  22. HarmonyMethod postfix = new HarmonyMethod(typeof(SliderUnlocker).GetMethod("ConvertTextFromRateHook"));
  23. harmony.Patch(original, null, postfix);
  24. original = AccessTools.Method(typeof(CustomBase), "ConvertRateFromText");
  25. postfix = new HarmonyMethod(typeof(SliderUnlocker).GetMethod("ConvertRateFromTextHook"));
  26. harmony.Patch(original, null, postfix);
  27. original = AccessTools.Method(typeof(Mathf), "Clamp", new Type[] { typeof(float), typeof(float), typeof(float) });
  28. postfix = new HarmonyMethod(typeof(SliderUnlocker).GetMethod("MathfClampHook"));
  29. harmony.Patch(original, null, postfix);
  30. original = typeof(AnimationKeyInfo).GetMethods().Where(x => x.Name.Contains("GetInfo")).ToArray()[1];
  31. var prefix = new HarmonyMethod(typeof(SliderUnlocker).GetMethod("GetInfoPreHook"));
  32. postfix = new HarmonyMethod(typeof(SliderUnlocker).GetMethod("GetInfoPostHook"));
  33. harmony.Patch(original, prefix, postfix);
  34. }
  35. protected override void LevelFinishedLoading(Scene scene, LoadSceneMode mode)
  36. {
  37. foreach (Slider gameObject in GameObject.FindObjectsOfType<Slider>())
  38. {
  39. gameObject.maxValue = 2f;
  40. }
  41. }
  42. [HarmonyPostfix]
  43. public static void ConvertTextFromRateHook(ref string __result, int min, int max, float value)
  44. {
  45. if (min == 0 && max == 100)
  46. __result = Math.Round(100 * value).ToString();
  47. }
  48. [HarmonyPostfix]
  49. public static void ConvertRateFromTextHook(ref float __result, int min, int max, string buf)
  50. {
  51. if (min == 0 && max == 100)
  52. {
  53. if (buf.IsNullOrEmpty())
  54. {
  55. __result = 0f;
  56. }
  57. else
  58. {
  59. if (!float.TryParse(buf, out float val))
  60. {
  61. __result = 0f;
  62. }
  63. else
  64. {
  65. __result = val / 100;
  66. }
  67. }
  68. }
  69. }
  70. [HarmonyPostfix]
  71. public static void MathfClampHook(ref float __result, float value, float min, float max)
  72. {
  73. if (min == 0f && max == 100f)
  74. __result = value;
  75. }
  76. [HarmonyPrefix]
  77. public static void GetInfoPreHook(ref float __state, string name, ref float rate, ref Vector3[] value, bool[] flag)
  78. {
  79. __state = rate;
  80. if (rate > 1)
  81. rate = 1f;
  82. }
  83. [HarmonyPostfix]
  84. public static void GetInfoPostHook(AnimationKeyInfo __instance, bool __result, float __state, string name, float rate, ref Vector3[] value, bool[] flag)
  85. {
  86. if (!__result)
  87. return;
  88. rate = __state;
  89. if (rate < 0f || rate > 1f)
  90. {
  91. var dictInfo = (Dictionary<string, List<AnimationKeyInfo.AnmKeyInfo>>)akf_dictInfo.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. }