Downloader.cs 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  1. using SongBrowserPlugin.DataAccess.BeatSaverApi;
  2. using SongBrowserPlugin.UI;
  3. using SongLoaderPlugin;
  4. using System;
  5. using System.Collections;
  6. using System.Collections.Generic;
  7. using System.IO;
  8. using System.IO.Compression;
  9. using System.Linq;
  10. using System.Text;
  11. using System.Threading.Tasks;
  12. using TMPro;
  13. using UnityEngine;
  14. using UnityEngine.Networking;
  15. namespace SongBrowserPlugin
  16. {
  17. class Downloader : MonoBehaviour
  18. {
  19. private Logger _log = new Logger("Downloader");
  20. public static Downloader Instance;
  21. private StandardLevelDetailViewController _levelDetailViewController;
  22. public Action<Song> downloadStarted;
  23. public Action<Song> downloadFinished;
  24. /// <summary>
  25. /// Load this.
  26. /// </summary>
  27. internal static void OnLoad()
  28. {
  29. if (Instance != null)
  30. {
  31. return;
  32. }
  33. new GameObject("SongBrowserDownloader").AddComponent<Downloader>();
  34. }
  35. /// <summary>
  36. /// Downloader has awoken.
  37. /// </summary>
  38. private void Awake()
  39. {
  40. _log.Trace("Awake()");
  41. Instance = this;
  42. }
  43. /// <summary>
  44. /// Acquire any UI elements from Beat saber that we need. Wait for the song list to be loaded.
  45. /// </summary>
  46. public void Start()
  47. {
  48. _log.Trace("Start()");
  49. StandardLevelSelectionFlowCoordinator levelSelectionFlowCoordinator = Resources.FindObjectsOfTypeAll<StandardLevelSelectionFlowCoordinator>().First();
  50. _levelDetailViewController = levelSelectionFlowCoordinator.GetPrivateField<StandardLevelDetailViewController>("_levelDetailViewController");
  51. }
  52. /// <summary>
  53. /// Handle downloading a song.
  54. /// Ported from: https://github.com/andruzzzhka/BeatSaverDownloader/blob/master/BeatSaverDownloader/PluginUI/PluginUI.cs
  55. /// </summary>
  56. /// <param name="songInfo"></param>
  57. /// <returns></returns>
  58. public IEnumerator DownloadSongCoroutine(Song songInfo)
  59. {
  60. songInfo.songQueueState = SongQueueState.Downloading;
  61. downloadStarted?.Invoke(songInfo);
  62. UnityWebRequest www;
  63. bool timeout = false;
  64. float time = 0f;
  65. UnityWebRequestAsyncOperation asyncRequest;
  66. try
  67. {
  68. www = UnityWebRequest.Get(songInfo.downloadUrl);
  69. asyncRequest = www.SendWebRequest();
  70. }
  71. catch
  72. {
  73. songInfo.songQueueState = SongQueueState.Error;
  74. songInfo.downloadingProgress = 1f;
  75. yield break;
  76. }
  77. while ((!asyncRequest.isDone || songInfo.downloadingProgress != 1f) && songInfo.songQueueState != SongQueueState.Error)
  78. {
  79. yield return null;
  80. time += Time.deltaTime;
  81. if ((time >= 15f && asyncRequest.progress == 0f) || songInfo.songQueueState == SongQueueState.Error)
  82. {
  83. www.Abort();
  84. timeout = true;
  85. }
  86. songInfo.downloadingProgress = asyncRequest.progress;
  87. }
  88. if (www.isNetworkError || www.isHttpError || timeout || songInfo.songQueueState == SongQueueState.Error)
  89. {
  90. if (timeout)
  91. {
  92. songInfo.songQueueState = SongQueueState.Error;
  93. TextMeshProUGUI _errorText = UIBuilder.CreateText(_levelDetailViewController.rectTransform, "Request timeout", new Vector2(18f, -64f), new Vector2(60f, 10f));
  94. Destroy(_errorText.gameObject, 2f);
  95. }
  96. else
  97. {
  98. songInfo.songQueueState = SongQueueState.Error;
  99. _log.Error($"Downloading error: {www.error}");
  100. TextMeshProUGUI _errorText = UIBuilder.CreateText(_levelDetailViewController.rectTransform, www.error, new Vector2(18f, -64f), new Vector2(60f, 10f));
  101. Destroy(_errorText.gameObject, 2f);
  102. }
  103. }
  104. else
  105. {
  106. _log.Debug("Received response from BeatSaver.com...");
  107. string zipPath = "";
  108. string docPath = "";
  109. string customSongsPath = "";
  110. byte[] data = www.downloadHandler.data;
  111. try
  112. {
  113. docPath = Application.dataPath;
  114. docPath = docPath.Substring(0, docPath.Length - 5);
  115. docPath = docPath.Substring(0, docPath.LastIndexOf("/"));
  116. customSongsPath = docPath + "/CustomSongs/" + songInfo.id + "/";
  117. zipPath = customSongsPath + songInfo.id + ".zip";
  118. if (!Directory.Exists(customSongsPath))
  119. {
  120. Directory.CreateDirectory(customSongsPath);
  121. }
  122. File.WriteAllBytes(zipPath, data);
  123. _log.Debug("Downloaded zip file!");
  124. }
  125. catch (Exception e)
  126. {
  127. _log.Exception("EXCEPTION: ", e);
  128. songInfo.songQueueState = SongQueueState.Error;
  129. yield break;
  130. }
  131. _log.Debug("Extracting...");
  132. try
  133. {
  134. ZipFile.ExtractToDirectory(zipPath, customSongsPath);
  135. }
  136. catch (Exception e)
  137. {
  138. _log.Exception("Can't extract ZIP! Exception: ", e);
  139. }
  140. songInfo.path = Directory.GetDirectories(customSongsPath).FirstOrDefault();
  141. if (string.IsNullOrEmpty(songInfo.path))
  142. {
  143. songInfo.path = customSongsPath;
  144. }
  145. try
  146. {
  147. File.Delete(zipPath);
  148. }
  149. catch (IOException e)
  150. {
  151. _log.Warning($"Can't delete zip! Exception: {e}");
  152. }
  153. songInfo.songQueueState = SongQueueState.Downloaded;
  154. _log.Debug("Downloaded!");
  155. downloadFinished?.Invoke(songInfo);
  156. }
  157. }
  158. }
  159. }