ScoreSaberDatabaseDownloader.cs 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. using Mobcast.Coffee.AssetSystem;
  2. using SongBrowserPlugin.DataAccess;
  3. using SongBrowserPlugin.DataAccess.Network;
  4. using System;
  5. using System.Collections;
  6. using UnityEngine;
  7. using UnityEngine.Networking;
  8. namespace SongBrowserPlugin.UI
  9. {
  10. public class ScoreSaberDatabaseDownloader : MonoBehaviour
  11. {
  12. public const String PP_DATA_URL = "https://raw.githubusercontent.com/DuoVR/PPFarming/master/js/songlist.tsv";
  13. private Logger _log = new Logger("ScoreSaberDatabaseDownloader");
  14. public static ScoreSaberDatabaseDownloader Instance;
  15. public ScoreSaberDataFile ScoreSaberDataFile;
  16. public Action onScoreSaberDataDownloaded;
  17. /// <summary>
  18. /// Awake.
  19. /// </summary>
  20. private void Awake()
  21. {
  22. _log.Trace("Awake()");
  23. if (Instance == null)
  24. {
  25. Instance = this;
  26. }
  27. }
  28. /// <summary>
  29. /// Acquire any UI elements from Beat saber that we need. Wait for the song list to be loaded.
  30. /// </summary>
  31. public void Start()
  32. {
  33. _log.Trace("Start()");
  34. StartCoroutine(WaitForDownload());
  35. }
  36. /// <summary>
  37. /// Wait for the tsv file from DuoVR to download.
  38. /// </summary>
  39. /// <returns></returns>
  40. private IEnumerator WaitForDownload()
  41. {
  42. SongBrowserApplication.MainProgressBar.ShowMessage("Downloading DuoVR ScoreSaber data...");
  43. _log.Info("Attempting to download: {0}", ScoreSaberDatabaseDownloader.PP_DATA_URL);
  44. using (UnityWebRequest www = UnityWebRequest.Get(ScoreSaberDatabaseDownloader.PP_DATA_URL))
  45. {
  46. // Use 4MB cache, large enough for this file to grow for awhile.
  47. www.SetCacheable(new CacheableDownloadHandlerScoreSaberData(www, new byte[4 * 1048576]));
  48. yield return www.SendWebRequest();
  49. _log.Debug("Returned from web request!...");
  50. try
  51. {
  52. this.ScoreSaberDataFile = (www.downloadHandler as CacheableDownloadHandlerScoreSaberData).ScoreSaberDataFile;
  53. _log.Info("Success downloading DuoVR ScoreSaber data!");
  54. SongBrowserApplication.MainProgressBar.ShowMessage("Success downloading DuoVR ScoreSaber data...");
  55. onScoreSaberDataDownloaded?.Invoke();
  56. }
  57. catch (System.InvalidOperationException)
  58. {
  59. _log.Error("Failed to download DuoVR ScoreSaber data file...");
  60. }
  61. catch (Exception e)
  62. {
  63. _log.Exception("Exception trying to download DuoVR ScoreSaber data file...", e);
  64. }
  65. }
  66. }
  67. }
  68. }