You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

146 lines
5.7 KiB

  1. using BSIPA_ModList.UI.ViewControllers;
  2. using CustomUI.BeatSaber;
  3. using CustomUI.Utilities;
  4. using IPA.Loader;
  5. using System;
  6. using System.Collections.Generic;
  7. using System.Linq;
  8. using System.Reflection;
  9. using UnityEngine;
  10. using UnityEngine.Events;
  11. using UnityEngine.SceneManagement;
  12. using VRUI;
  13. namespace BSIPA_ModList.UI
  14. {
  15. internal class ModListFlowCoordinator : FlowCoordinator
  16. {
  17. private BackButtonNavigationController navigationController;
  18. private ModListController modList;
  19. private DownloadProgressViewController downloads;
  20. private VRUIViewController settings;
  21. #pragma warning disable CS0618
  22. protected override void DidActivate(bool firstActivation, ActivationType activationType)
  23. { // thx Caeden
  24. if (firstActivation && activationType == ActivationType.AddedToHierarchy)
  25. {
  26. title = "Installed Mods";
  27. navigationController = BeatSaberUI.CreateViewController<BackButtonNavigationController>();
  28. navigationController.didFinishEvent += backButton_DidFinish;
  29. modList = BeatSaberUI.CreateViewController<ModListController>();
  30. modList.Init(this, PluginManager.AllPlugins.Select(p => p.Metadata).Concat(PluginManager.DisabledPlugins), PluginLoader.ignoredPlugins, PluginManager.Plugins);
  31. settings = SettingsViewController.Create();
  32. downloads = BeatSaberUI.CreateViewController<DownloadProgressViewController>();
  33. PushViewControllerToNavigationController(navigationController, modList);
  34. }
  35. ProvideInitialViewControllers(navigationController, settings, downloads);
  36. }
  37. #pragma warning restore
  38. private delegate void PresentFlowCoordDel(FlowCoordinator self, FlowCoordinator newF, Action finished, bool immediate, bool replaceTop);
  39. private static PresentFlowCoordDel presentFlow;
  40. public void Present(Action finished = null, bool immediate = false, bool replaceTop = false)
  41. {
  42. if (presentFlow == null)
  43. {
  44. var ty = typeof(FlowCoordinator);
  45. var m = ty.GetMethod("PresentFlowCoordinator", BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic);
  46. presentFlow = (PresentFlowCoordDel)Delegate.CreateDelegate(typeof(PresentFlowCoordDel), m);
  47. }
  48. MainFlowCoordinator mainFlow = Resources.FindObjectsOfTypeAll<MainFlowCoordinator>().First();
  49. presentFlow(mainFlow, this, finished, immediate, replaceTop);
  50. }
  51. public bool HasSelected { get; private set; } = false;
  52. public void SetSelected(VRUIViewController selected, Action callback = null, bool immediate = false)
  53. {
  54. if (immediate)
  55. {
  56. if (HasSelected)
  57. PopViewController(immediate: true);
  58. PushViewController(selected, callback, true);
  59. HasSelected = true;
  60. }
  61. else
  62. {
  63. if (HasSelected)
  64. PopViewController(() =>
  65. {
  66. PushViewController(selected, callback, immediate);
  67. HasSelected = true;
  68. }, immediate);
  69. else
  70. {
  71. PushViewController(selected, callback, immediate);
  72. HasSelected = true;
  73. }
  74. }
  75. }
  76. public void ClearSelected(Action callback = null, bool immediate = false)
  77. {
  78. if (HasSelected) PopViewController(callback, immediate);
  79. HasSelected = false;
  80. }
  81. public void PushViewController(VRUIViewController controller, Action callback = null, bool immediate = false)
  82. {
  83. PushViewControllerToNavigationController(navigationController, controller, callback, immediate);
  84. }
  85. public void PopViewController(Action callback = null, bool immediate = false)
  86. {
  87. PopViewControllerFromNavigationController(navigationController, callback, immediate);
  88. }
  89. internal HashSet<Action> exitActions = new HashSet<Action>();
  90. private delegate void DismissFlowDel(FlowCoordinator self, FlowCoordinator newF, Action finished, bool immediate);
  91. private static DismissFlowDel dismissFlow;
  92. private void backButton_DidFinish()
  93. {
  94. if (dismissFlow == null)
  95. {
  96. var ty = typeof(FlowCoordinator);
  97. var m = ty.GetMethod("DismissFlowCoordinator", BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic);
  98. dismissFlow = (DismissFlowDel)Delegate.CreateDelegate(typeof(DismissFlowDel), m);
  99. }
  100. if (exitActions.Count == 0)
  101. {
  102. MainFlowCoordinator mainFlow = Resources.FindObjectsOfTypeAll<MainFlowCoordinator>().First();
  103. dismissFlow(mainFlow, this, null, false);
  104. }
  105. else
  106. {
  107. var actions = exitActions;
  108. UnityAction<Scene, LoadSceneMode> releaseAction = null;
  109. releaseAction = (a, b) => SceneManager_sceneLoaded(actions, releaseAction, a, b);
  110. SceneManager.sceneLoaded += releaseAction;
  111. Resources.FindObjectsOfTypeAll<MenuTransitionsHelperSO>().First().RestartGame(true);
  112. }
  113. }
  114. private static void SceneManager_sceneLoaded(HashSet<Action> actions, UnityAction<Scene, LoadSceneMode> self, Scene arg0, LoadSceneMode arg1)
  115. {
  116. if (arg0.name == "Init")
  117. {
  118. SceneManager.sceneLoaded -= self;
  119. foreach (var act in actions)
  120. act();
  121. }
  122. }
  123. }
  124. }