SliderUnlocker.cs 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. using BepInEx;
  2. using ChaCustom;
  3. using System;
  4. using System.Collections.Generic;
  5. using System.Linq;
  6. using System.Reflection;
  7. using UnityEngine;
  8. using UnityEngine.SceneManagement;
  9. using UnityEngine.UI;
  10. namespace SliderUnlocker
  11. {
  12. public class SliderUnlocker : BaseUnityPlugin
  13. {
  14. public override string ID => "com.bepis.bepinex.sliderunlocker";
  15. public override string Name => "Slider Unlocker";
  16. public override Version Version => new Version("1.5");
  17. public static float Minimum = -1.0f;
  18. public static float Maximum = 2.0f;
  19. void Awake()
  20. {
  21. Hooks.InstallHooks();
  22. }
  23. void LevelFinishedLoading(Scene scene, LoadSceneMode mode)
  24. {
  25. SetAllSliders(scene, Minimum, Maximum);
  26. }
  27. public void SetAllSliders(Scene scene, float minimum, float maximum)
  28. {
  29. List<object> cvsInstances = new List<object>();
  30. Assembly illusion = typeof(CvsAccessory).Assembly;
  31. var sceneObjects = scene.GetRootGameObjects();
  32. foreach (Type type in illusion.GetTypes())
  33. {
  34. if (type.Name.ToUpper().StartsWith("CVS") &&
  35. type != typeof(CvsDrawCtrl) &&
  36. type != typeof(CvsColor))
  37. {
  38. foreach (var obj in sceneObjects)
  39. {
  40. cvsInstances.AddRange(obj.GetComponentsInChildren(type));
  41. }
  42. }
  43. }
  44. foreach (object cvs in cvsInstances)
  45. {
  46. if (cvs == null)
  47. continue;
  48. var fields = cvs.GetType().GetFields(BindingFlags.Instance | BindingFlags.NonPublic | BindingFlags.Public)
  49. .Where(x => typeof(Slider).IsAssignableFrom(x.FieldType));
  50. foreach (Slider slider in fields.Select(x => x.GetValue(cvs)))
  51. {
  52. if (slider == null)
  53. continue;
  54. slider.minValue = minimum;
  55. slider.maxValue = maximum;
  56. }
  57. }
  58. }
  59. #region MonoBehaviour
  60. void OnEnable()
  61. {
  62. SceneManager.sceneLoaded += LevelFinishedLoading;
  63. }
  64. void OnDisable()
  65. {
  66. SceneManager.sceneLoaded -= LevelFinishedLoading;
  67. }
  68. #endregion
  69. }
  70. }