SongBrowserApplication.cs 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  1. using UnityEngine;
  2. using System.Linq;
  3. using System;
  4. using SongLoaderPlugin.OverrideClasses;
  5. using UnityEngine.SceneManagement;
  6. using SongLoaderPlugin;
  7. using UnityEngine.UI;
  8. using SongBrowserPlugin.UI;
  9. using System.Collections;
  10. using System.Collections.Generic;
  11. namespace SongBrowserPlugin
  12. {
  13. public class SongBrowserApplication : MonoBehaviour
  14. {
  15. // Which scene index to load into
  16. public const int MenuIndex = 1;
  17. public static SongBrowserApplication Instance;
  18. private Logger _log = new Logger("SongBrowserApplication");
  19. // Song Browser UI Elements
  20. private SongBrowserUI _songBrowserUI;
  21. public Dictionary<String, Sprite> CachedIcons;
  22. /// <summary>
  23. ///
  24. /// </summary>
  25. internal static void OnLoad()
  26. {
  27. if (Instance != null)
  28. {
  29. return;
  30. }
  31. new GameObject("BeatSaber SongBrowser Mod").AddComponent<SongBrowserApplication>();
  32. }
  33. /// <summary>
  34. ///
  35. /// </summary>
  36. private void Awake()
  37. {
  38. _log.Trace("Awake()");
  39. Instance = this;
  40. _songBrowserUI = gameObject.AddComponent<SongBrowserUI>();
  41. }
  42. /// <summary>
  43. /// Acquire any UI elements from Beat saber that we need. Wait for the song list to be loaded.
  44. /// </summary>
  45. public void Start()
  46. {
  47. _log.Trace("Start()");
  48. AcquireUIElements();
  49. StartCoroutine(WaitForSongListUI());
  50. }
  51. /// <summary>
  52. /// Wait for the song list to be visible to draw it.
  53. /// </summary>
  54. /// <returns></returns>
  55. private IEnumerator WaitForSongListUI()
  56. {
  57. _log.Trace("WaitForSongListUI()");
  58. yield return new WaitUntil(delegate () { return Resources.FindObjectsOfTypeAll<StandardLevelSelectionFlowCoordinator>().Any(); });
  59. _log.Debug("Found StandardLevelSelectionFlowCoordinator...");
  60. _songBrowserUI.CreateUI();
  61. if (SongLoaderPlugin.SongLoader.AreSongsLoaded)
  62. {
  63. OnSongLoaderLoadedSongs(null, SongLoader.CustomLevels);
  64. }
  65. else
  66. {
  67. SongLoader.SongsLoadedEvent += OnSongLoaderLoadedSongs;
  68. }
  69. _songBrowserUI.RefreshSongList();
  70. }
  71. /// <summary>
  72. /// Only gets called once during boot of BeatSaber.
  73. /// </summary>
  74. /// <param name="loader"></param>
  75. /// <param name="levels"></param>
  76. private void OnSongLoaderLoadedSongs(SongLoader loader, List<CustomLevel> levels)
  77. {
  78. _log.Trace("OnSongLoaderLoadedSongs");
  79. try
  80. {
  81. _songBrowserUI.UpdateSongList();
  82. }
  83. catch (Exception e)
  84. {
  85. _log.Exception("Exception during OnSongLoaderLoadedSongs: ", e);
  86. }
  87. }
  88. /// <summary>
  89. /// Get a handle to the view controllers we are going to add elements to.
  90. /// </summary>
  91. public void AcquireUIElements()
  92. {
  93. _log.Trace("AcquireUIElements()");
  94. try
  95. {
  96. CachedIcons = new Dictionary<String, Sprite>();
  97. foreach (Sprite sprite in Resources.FindObjectsOfTypeAll<Sprite>())
  98. {
  99. if (CachedIcons.ContainsKey(sprite.name))
  100. {
  101. continue;
  102. }
  103. CachedIcons.Add(sprite.name, sprite);
  104. }
  105. // Append our own event to appropriate events so we can refresh the song list before the user sees it.
  106. MainFlowCoordinator mainFlow = Resources.FindObjectsOfTypeAll<MainFlowCoordinator>().First();
  107. SoloModeSelectionViewController view = Resources.FindObjectsOfTypeAll<SoloModeSelectionViewController>().First();
  108. view.didFinishEvent += HandleSoloModeSelectionViewControllerDidSelectMode;
  109. }
  110. catch (Exception e)
  111. {
  112. _log.Exception("Exception AcquireUIElements(): ", e);
  113. }
  114. }
  115. /// <summary>
  116. /// Perfect time to refresh the level list on first entry.
  117. /// </summary>
  118. /// <param name="arg1"></param>
  119. /// <param name="arg2"></param>
  120. private void HandleSoloModeSelectionViewControllerDidSelectMode(SoloModeSelectionViewController arg1, SoloModeSelectionViewController.SubMenuType arg2)
  121. {
  122. _log.Trace("HandleSoloModeSelectionViewControllerDidSelectMode()");
  123. this._songBrowserUI.RefreshSongList();
  124. }
  125. /// <summary>
  126. /// Helper for invoking buttons.
  127. /// </summary>
  128. /// <param name="buttonName"></param>
  129. public static void InvokeBeatSaberButton(String buttonName)
  130. {
  131. Button buttonInstance = Resources.FindObjectsOfTypeAll<Button>().First(x => (x.name == buttonName));
  132. buttonInstance.onClick.Invoke();
  133. }
  134. /// <summary>
  135. /// Map some key presses directly to UI interactions to make testing easier.
  136. /// </summary>
  137. private void LateUpdate()
  138. {
  139. // z,x,c,v can be used to get into a song, b will hit continue button after song ends
  140. if (Input.GetKeyDown(KeyCode.Z))
  141. {
  142. InvokeBeatSaberButton("SoloButton");
  143. }
  144. if (Input.GetKeyDown(KeyCode.X))
  145. {
  146. InvokeBeatSaberButton("StandardButton");
  147. }
  148. if (Input.GetKeyDown(KeyCode.B))
  149. {
  150. InvokeBeatSaberButton("ContinueButton");
  151. }
  152. }
  153. }
  154. }