123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179 |
- using UnityEngine;
- using System.Linq;
- using System;
- using SongLoaderPlugin.OverrideClasses;
- using UnityEngine.SceneManagement;
- using SongLoaderPlugin;
- using UnityEngine.UI;
- using SongBrowserPlugin.UI;
- using System.Collections;
- using System.Collections.Generic;
- namespace SongBrowserPlugin
- {
- public class SongBrowserApplication : MonoBehaviour
- {
- // Which scene index to load into
- public const int MenuIndex = 1;
- public static SongBrowserApplication Instance;
- private Logger _log = new Logger("SongBrowserApplication");
- // Song Browser UI Elements
- private SongBrowserUI _songBrowserUI;
- public Dictionary<String, Sprite> CachedIcons;
- /// <summary>
- ///
- /// </summary>
- internal static void OnLoad()
- {
- if (Instance != null)
- {
- return;
- }
- new GameObject("BeatSaber SongBrowser Mod").AddComponent<SongBrowserApplication>();
- }
- /// <summary>
- ///
- /// </summary>
- private void Awake()
- {
- _log.Trace("Awake()");
- Instance = this;
- _songBrowserUI = gameObject.AddComponent<SongBrowserUI>();
- }
- /// <summary>
- /// Acquire any UI elements from Beat saber that we need. Wait for the song list to be loaded.
- /// </summary>
- public void Start()
- {
- _log.Trace("Start()");
- AcquireUIElements();
- StartCoroutine(WaitForSongListUI());
- }
- /// <summary>
- /// Wait for the song list to be visible to draw it.
- /// </summary>
- /// <returns></returns>
- private IEnumerator WaitForSongListUI()
- {
- _log.Trace("WaitForSongListUI()");
- yield return new WaitUntil(delegate () { return Resources.FindObjectsOfTypeAll<StandardLevelSelectionFlowCoordinator>().Any(); });
- _log.Debug("Found StandardLevelSelectionFlowCoordinator...");
- _songBrowserUI.CreateUI();
- if (SongLoaderPlugin.SongLoader.AreSongsLoaded)
- {
- OnSongLoaderLoadedSongs(null, SongLoader.CustomLevels);
- }
- else
- {
- SongLoader.SongsLoadedEvent += OnSongLoaderLoadedSongs;
- }
- _songBrowserUI.RefreshSongList();
- }
- /// <summary>
- /// Only gets called once during boot of BeatSaber.
- /// </summary>
- /// <param name="loader"></param>
- /// <param name="levels"></param>
- private void OnSongLoaderLoadedSongs(SongLoader loader, List<CustomLevel> levels)
- {
- _log.Trace("OnSongLoaderLoadedSongs");
- try
- {
- _songBrowserUI.UpdateSongList();
- }
- catch (Exception e)
- {
- _log.Exception("Exception during OnSongLoaderLoadedSongs: ", e);
- }
- }
- /// <summary>
- /// Get a handle to the view controllers we are going to add elements to.
- /// </summary>
- public void AcquireUIElements()
- {
- _log.Trace("AcquireUIElements()");
- try
- {
- CachedIcons = new Dictionary<String, Sprite>();
- foreach (Sprite sprite in Resources.FindObjectsOfTypeAll<Sprite>())
- {
- if (CachedIcons.ContainsKey(sprite.name))
- {
- continue;
- }
- CachedIcons.Add(sprite.name, sprite);
- }
- // Append our own event to appropriate events so we can refresh the song list before the user sees it.
- MainFlowCoordinator mainFlow = Resources.FindObjectsOfTypeAll<MainFlowCoordinator>().First();
- SoloModeSelectionViewController view = Resources.FindObjectsOfTypeAll<SoloModeSelectionViewController>().First();
- view.didFinishEvent += HandleSoloModeSelectionViewControllerDidSelectMode;
- }
- catch (Exception e)
- {
- _log.Exception("Exception AcquireUIElements(): ", e);
- }
- }
- /// <summary>
- /// Perfect time to refresh the level list on first entry.
- /// </summary>
- /// <param name="arg1"></param>
- /// <param name="arg2"></param>
- private void HandleSoloModeSelectionViewControllerDidSelectMode(SoloModeSelectionViewController arg1, SoloModeSelectionViewController.SubMenuType arg2)
- {
- _log.Trace("HandleSoloModeSelectionViewControllerDidSelectMode()");
- this._songBrowserUI.RefreshSongList();
- }
- /// <summary>
- /// Helper for invoking buttons.
- /// </summary>
- /// <param name="buttonName"></param>
- public static void InvokeBeatSaberButton(String buttonName)
- {
- Button buttonInstance = Resources.FindObjectsOfTypeAll<Button>().First(x => (x.name == buttonName));
- buttonInstance.onClick.Invoke();
- }
- /// <summary>
- /// Map some key presses directly to UI interactions to make testing easier.
- /// </summary>
- private void LateUpdate()
- {
- // z,x,c,v can be used to get into a song, b will hit continue button after song ends
- if (Input.GetKeyDown(KeyCode.Z))
- {
- InvokeBeatSaberButton("SoloButton");
- }
- if (Input.GetKeyDown(KeyCode.X))
- {
- InvokeBeatSaberButton("StandardButton");
- }
- if (Input.GetKeyDown(KeyCode.B))
- {
- InvokeBeatSaberButton("ContinueButton");
- }
- }
- }
- }
|