ScoreSaberDatabaseDownloader.cs 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  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 static ScoreSaberDataFile ScoreSaberDataFile = null;
  16. public Action onScoreSaberDataDownloaded;
  17. private readonly byte[] _buffer = new byte[4 * 1048576];
  18. /// <summary>
  19. /// Awake.
  20. /// </summary>
  21. private void Awake()
  22. {
  23. _log.Trace("Awake()");
  24. if (Instance == null)
  25. {
  26. Instance = this;
  27. }
  28. }
  29. /// <summary>
  30. /// Acquire any UI elements from Beat saber that we need. Wait for the song list to be loaded.
  31. /// </summary>
  32. public void Start()
  33. {
  34. _log.Trace("Start()");
  35. StartCoroutine(WaitForDownload());
  36. }
  37. private void DownloadScoreSaberData()
  38. {
  39. }
  40. /// <summary>
  41. /// Wait for the tsv file from DuoVR to download.
  42. /// </summary>
  43. /// <returns></returns>
  44. private IEnumerator WaitForDownload()
  45. {
  46. if (ScoreSaberDatabaseDownloader.ScoreSaberDataFile != null)
  47. {
  48. _log.Info("Using cached copy of DuoVR ScoreSaberData...");
  49. }
  50. else
  51. {
  52. SongBrowserApplication.MainProgressBar.ShowMessage("Downloading DuoVR ScoreSaber data...");
  53. _log.Info("Attempting to download: {0}", ScoreSaberDatabaseDownloader.PP_DATA_URL);
  54. using (UnityWebRequest www = UnityWebRequest.Get(ScoreSaberDatabaseDownloader.PP_DATA_URL))
  55. {
  56. // Use 4MB cache, large enough for this file to grow for awhile.
  57. www.SetCacheable(new CacheableDownloadHandlerScoreSaberData(www, _buffer));
  58. yield return www.SendWebRequest();
  59. _log.Debug("Returned from web request!...");
  60. try
  61. {
  62. ScoreSaberDatabaseDownloader.ScoreSaberDataFile = (www.downloadHandler as CacheableDownloadHandlerScoreSaberData).ScoreSaberDataFile;
  63. _log.Info("Success downloading DuoVR ScoreSaber data!");
  64. SongBrowserApplication.MainProgressBar.ShowMessage("Success downloading DuoVR ScoreSaber data...");
  65. onScoreSaberDataDownloaded?.Invoke();
  66. }
  67. catch (System.InvalidOperationException)
  68. {
  69. _log.Error("Failed to download DuoVR ScoreSaber data file...");
  70. }
  71. catch (Exception e)
  72. {
  73. _log.Exception("Exception trying to download DuoVR ScoreSaber data file...", e);
  74. }
  75. }
  76. }
  77. }
  78. }
  79. }