SliderUnlocker.cs 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  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. Console.WriteLine("test");
  27. //foreach (var method in typeof(AnimationKeyInfo).GetMethods().Where(x => x.Name.Contains("GetInfo")))
  28. //{typeof(AnimationKeyInfo).GetMethods().Where(x => x.Name == "GetInfo");
  29. // Console.WriteLine(method.GetParameters().Select(x => x.ParameterType.FullName).Aggregate((a, b) => $"{a};{b}"));
  30. //}
  31. original = typeof(AnimationKeyInfo).GetMethods().Where(x => x.Name.Contains("GetInfo")).ToArray()[1];
  32. //original = AccessTools.Method(typeof(AnimationKeyInfo), "GetInfo"); //new Type[] { typeof(string), typeof(float), typeof(Vector3[]), typeof(bool[]) }
  33. postfix = new HarmonyMethod(typeof(SliderUnlocker).GetMethod("GetInfoHook"));
  34. harmony.Patch(original, null, postfix);
  35. Console.WriteLine("hooked");
  36. }
  37. protected override void LevelFinishedLoading(Scene scene, LoadSceneMode mode)
  38. {
  39. foreach (Slider gameObject in GameObject.FindObjectsOfType<Slider>())
  40. {
  41. gameObject.maxValue = 2;
  42. }
  43. }
  44. //TypeDefinition customBase = assembly.MainModule.Types.First(x => x.Name == "CustomBase");
  45. //var methods = customBase.Methods;
  46. //var convertTextFromRate = methods.First(x => x.Name == "ConvertTextFromRate");
  47. //var IL = convertTextFromRate.Body.GetILProcessor();
  48. //IL.Replace(convertTextFromRate.Body.Instructions[0], IL.Create(OpCodes.Ldc_I4, -0));
  49. // IL.Replace(convertTextFromRate.Body.Instructions[2], IL.Create(OpCodes.Ldc_I4, 200));
  50. // var convertRateFromText = methods.First(x => x.Name == "ConvertRateFromText");
  51. //IL = convertRateFromText.Body.GetILProcessor();
  52. // IL.Replace(convertRateFromText.Body.Instructions[11], IL.Create(OpCodes.Ldc_I4, -0));
  53. // IL.Replace(convertRateFromText.Body.Instructions[13], IL.Create(OpCodes.Ldc_I4, 200));
  54. [HarmonyPostfix]
  55. public static void ConvertTextFromRateHook(ref string __result, int min, int max, float value)
  56. {
  57. if (min == 0 && max == 100)
  58. __result = Math.Round(100 * value).ToString();
  59. }
  60. [HarmonyPostfix]
  61. public static void ConvertRateFromTextHook(ref float __result, int min, int max, string buf)
  62. {
  63. if (min == 0 && max == 100)
  64. {
  65. if (buf.IsNullOrEmpty())
  66. {
  67. __result = 0f;
  68. }
  69. else
  70. {
  71. if (!float.TryParse(buf, out float val))
  72. {
  73. __result = 0f;
  74. }
  75. else
  76. {
  77. __result = val / 100;
  78. }
  79. }
  80. }
  81. }
  82. [HarmonyPostfix]
  83. public static void GetInfoHook(AnimationKeyInfo __instance, bool __result, string name, float rate, ref Vector3[] value, bool[] flag)
  84. {
  85. if (rate > 1)
  86. Console.WriteLine(rate);
  87. rate *= 2f;
  88. if (!__result)
  89. return;
  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[0])
  94. //{
  95. // Vector3 min = list[0].pos;
  96. // Vector3 max = list[list.Count - 1].pos;
  97. // Vector3 diff = new Vector3(max.x - min.x,
  98. // max.y - min.y,
  99. // max.z - min.z);
  100. // value[0] = new Vector3(min.x + (diff.x * rate),
  101. // min.y + (diff.y * rate),
  102. // min.z + (diff.z * rate));
  103. //}
  104. //if (flag[1])
  105. //{
  106. // //if (rate == 0f)
  107. // //{
  108. // // value[1] = list[0].rot;
  109. // //}
  110. // //else if (rate == 1f)
  111. // //{
  112. // // value[1] = list[list.Count - 1].rot;
  113. // //}
  114. // //else
  115. // //{
  116. // // float num3 = (float)(list.Count - 1) * rate;
  117. // // int num4 = Mathf.FloorToInt(num3);
  118. // // float t2 = num3 - (float)num4;
  119. // // value[1].x = Mathf.LerpAngle(list[num4].rot.x, list[num4 + 1].rot.x, t2);
  120. // // value[1].y = Mathf.LerpAngle(list[num4].rot.y, list[num4 + 1].rot.y, t2);
  121. // // value[1].z = Mathf.LerpAngle(list[num4].rot.z, list[num4 + 1].rot.z, t2);
  122. // //}
  123. // Vector3 min = list[0].rot;
  124. // Vector3 max = list[list.Count - 1].rot;
  125. // Vector3 diff = new Vector3(max.x - min.x,
  126. // max.y - min.y,
  127. // max.z - min.z);
  128. // value[1] = new Vector3(min.x + (diff.x * rate),
  129. // min.y + (diff.y * rate),
  130. // min.z + (diff.z * rate));
  131. //}
  132. if (flag[2])
  133. {
  134. Vector3 min = list[0].scl;
  135. Vector3 max = list[list.Count - 1].scl;
  136. Vector3 diff = new Vector3(max.x - min.x,
  137. max.y - min.y,
  138. max.z - min.z);
  139. value[2] = new Vector3(min.x + (diff.x * rate),
  140. min.y + (diff.y * rate),
  141. min.z + (diff.z * rate));
  142. }
  143. }
  144. }
  145. }