You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

190 lines
9.2 KiB

  1. using System;
  2. using System.Collections.Generic;
  3. using System.Diagnostics;
  4. using System.IO;
  5. using System.Linq;
  6. using CustomUI.BeatSaber;
  7. using CustomUI.Utilities;
  8. using HMUI;
  9. using IPA.Updating.BeatMods;
  10. using TMPro;
  11. using UnityEngine;
  12. using UnityEngine.UI;
  13. using VRUI;
  14. namespace BSIPA_ModList.UI.ViewControllers
  15. {
  16. // originally ripped verbatim from Andruzz's BeatSaverDownloader
  17. internal class DownloadProgressViewController : VRUIViewController, TableView.IDataSource
  18. {
  19. private TextMeshProUGUI _titleText;
  20. private TextMeshProUGUI _manualDownloadText;
  21. private Button _checkForUpdates;
  22. private Button _downloadUpdates;
  23. private Button _restartGame;
  24. private TableView _currentlyUpdatingTableView;
  25. private LevelListTableCell _songListTableCellInstance;
  26. private Button _pageUpButton;
  27. private Button _pageDownButton;
  28. private TableViewScroller _scroller;
  29. private const float TableXOffset = 0f;
  30. private const float ButtonXOffset = 36f;
  31. private static readonly Vector2 ButtonSize = new Vector2(40f, 10f);
  32. protected override void DidActivate(bool firstActivation, ActivationType type)
  33. {
  34. if (firstActivation && type == ActivationType.AddedToHierarchy)
  35. {
  36. DownloadController.Instance.OnDownloaderListChanged -= Refresh;
  37. DownloadController.Instance.OnDownloadStateChanged -= DownloaderStateChanged;
  38. _songListTableCellInstance = Resources.FindObjectsOfTypeAll<LevelListTableCell>().First(x => (x.name == "LevelListTableCell"));
  39. _titleText = BeatSaberUI.CreateText(rectTransform, "DOWNLOAD QUEUE", new Vector2(0f, 35f));
  40. _titleText.alignment = TextAlignmentOptions.Top;
  41. _titleText.fontSize = 6f;
  42. _manualDownloadText = BeatSaberUI.CreateText(rectTransform, "Manual Restart Required", new Vector2(37f, -3f));
  43. _manualDownloadText.alignment = TextAlignmentOptions.Top;
  44. _manualDownloadText.fontSize = 4f;
  45. _manualDownloadText.gameObject.SetActive(false);
  46. _pageUpButton = Instantiate(Resources.FindObjectsOfTypeAll<Button>().Last(x => (x.name == "PageUpButton")), rectTransform, false);
  47. (_pageUpButton.transform as RectTransform).anchorMin = new Vector2(0.5f, 1f);
  48. (_pageUpButton.transform as RectTransform).anchorMax = new Vector2(0.5f, 1f);
  49. (_pageUpButton.transform as RectTransform).anchoredPosition = new Vector2(TableXOffset, -14f);
  50. (_pageUpButton.transform as RectTransform).sizeDelta = new Vector2(40f, 10f);
  51. _pageUpButton.interactable = true;
  52. _pageUpButton.onClick.AddListener(delegate ()
  53. {
  54. _scroller.PageScrollUp();
  55. });
  56. _pageDownButton = Instantiate(Resources.FindObjectsOfTypeAll<Button>().First(x => (x.name == "PageDownButton")), rectTransform, false);
  57. (_pageDownButton.transform as RectTransform).anchorMin = new Vector2(0.5f, 0f);
  58. (_pageDownButton.transform as RectTransform).anchorMax = new Vector2(0.5f, 0f);
  59. (_pageDownButton.transform as RectTransform).anchoredPosition = new Vector2(TableXOffset, 8f);
  60. (_pageDownButton.transform as RectTransform).sizeDelta = new Vector2(40f, 10f);
  61. _pageDownButton.interactable = true;
  62. _pageDownButton.onClick.AddListener(delegate ()
  63. {
  64. _scroller.PageScrollDown();
  65. });
  66. var gobj = new GameObject("DownloadTable");
  67. gobj.SetActive(false);
  68. _currentlyUpdatingTableView = gobj.AddComponent<TableView>();
  69. _currentlyUpdatingTableView.transform.SetParent(rectTransform, false);
  70. _currentlyUpdatingTableView.SetPrivateField("_isInitialized", false);
  71. _currentlyUpdatingTableView.SetPrivateField("_preallocatedCells", new TableView.CellsGroup[0]);
  72. var viewport = new GameObject("Viewport").AddComponent<RectTransform>();
  73. viewport.SetParent(gobj.GetComponent<RectTransform>(), false);
  74. gobj.GetComponent<ScrollRect>().viewport = viewport;
  75. /*RectMask2D viewportMask = Instantiate(Resources.FindObjectsOfTypeAll<RectMask2D>().First(), _currentlyUpdatingTableView.transform, false);
  76. viewportMask.transform.DetachChildren();
  77. _currentlyUpdatingTableView.GetComponentsInChildren<RectTransform>().First(x => x.name == "Content").transform.SetParent(viewportMask.rectTransform, false);*/
  78. (_currentlyUpdatingTableView.transform as RectTransform).anchorMin = new Vector2(0.3f, 0.5f);
  79. (_currentlyUpdatingTableView.transform as RectTransform).anchorMax = new Vector2(0.7f, 0.5f);
  80. (_currentlyUpdatingTableView.transform as RectTransform).sizeDelta = new Vector2(0f, 60f);
  81. (_currentlyUpdatingTableView.transform as RectTransform).anchoredPosition = new Vector3(TableXOffset, -20f);
  82. ReflectionUtil.SetPrivateField(_currentlyUpdatingTableView, "_pageUpButton", _pageUpButton);
  83. ReflectionUtil.SetPrivateField(_currentlyUpdatingTableView, "_pageDownButton", _pageDownButton);
  84. _currentlyUpdatingTableView.selectionType = TableViewSelectionType.None;
  85. _currentlyUpdatingTableView.Init();
  86. _currentlyUpdatingTableView.dataSource = this; // calls Init
  87. _scroller = gobj.GetComponent<TableViewScroller>();
  88. gobj.SetActive(true);
  89. gobj.GetComponent<ScrollRect>().content.anchorMax = new Vector2(.6f, 1f);
  90. _currentlyUpdatingTableView.RefreshScrollButtons();
  91. _checkForUpdates = BeatSaberUI.CreateUIButton(rectTransform, "CreditsButton", new Vector2(ButtonXOffset, -30f), ButtonSize, CheckUpdates, "Check for updates");
  92. _checkForUpdates.interactable = DownloadController.Instance.CanCheck || DownloadController.Instance.CanReset;
  93. _checkForUpdates.ToggleWordWrapping(false);
  94. _downloadUpdates = BeatSaberUI.CreateUIButton(rectTransform, "CreditsButton", new Vector2(ButtonXOffset, -19f), ButtonSize, DownloadUpdates, "Download Updates");
  95. _downloadUpdates.interactable = DownloadController.Instance.CanDownload;
  96. _downloadUpdates.ToggleWordWrapping(false);
  97. _restartGame = BeatSaberUI.CreateUIButton(rectTransform, "CreditsButton", new Vector2(ButtonXOffset, -8f), ButtonSize, Restart, "Restart Game");
  98. _restartGame.interactable = DownloadController.Instance.HadUpdates;
  99. _restartGame.ToggleWordWrapping(false);
  100. }
  101. DownloadController.Instance.OnDownloaderListChanged += Refresh;
  102. DownloadController.Instance.OnDownloadStateChanged += DownloaderStateChanged;
  103. DownloaderStateChanged();
  104. }
  105. private void Restart()
  106. {
  107. Process.Start(Path.Combine(Environment.CurrentDirectory, Process.GetCurrentProcess().MainModule.FileName), Environment.CommandLine);
  108. Application.Quit();
  109. }
  110. private void DownloadUpdates()
  111. {
  112. if (DownloadController.Instance.CanDownload)
  113. DownloadController.Instance.StartDownloads();
  114. }
  115. private void CheckUpdates()
  116. {
  117. Updater.ResetRequestCache();
  118. if (DownloadController.Instance.CanReset)
  119. DownloadController.Instance.ResetCheck(false);
  120. if (DownloadController.Instance.CanCheck)
  121. DownloadController.Instance.CheckForUpdates();
  122. }
  123. private void DownloaderStateChanged()
  124. {
  125. _checkForUpdates.interactable = (DownloadController.Instance.CanCheck || DownloadController.Instance.CanReset) && !Updater.NeedsManualRestart;
  126. _downloadUpdates.interactable = DownloadController.Instance.CanDownload && !Updater.NeedsManualRestart;
  127. _restartGame.interactable = DownloadController.Instance.HadUpdates && !Updater.NeedsManualRestart;
  128. _manualDownloadText.gameObject.SetActive(Updater.NeedsManualRestart);
  129. }
  130. protected override void DidDeactivate(DeactivationType type)
  131. {
  132. DownloadController.Instance.OnDownloaderListChanged -= Refresh;
  133. DownloadController.Instance.OnDownloadStateChanged -= DownloaderStateChanged;
  134. if (DownloadController.Instance.IsDone)
  135. FloatingNotification.instance.Close();
  136. }
  137. private void Refresh()
  138. {
  139. _currentlyUpdatingTableView.ReloadData();
  140. }
  141. public float CellSize()
  142. {
  143. return 8.5f;
  144. }
  145. public int NumberOfCells()
  146. {
  147. return DownloadController.Instance.Downloads.Count;
  148. }
  149. public TableCell CellForIdx(TableView view, int row)
  150. {
  151. LevelListTableCell _tableCell = Instantiate(_songListTableCellInstance);
  152. DownloadProgressCell _queueCell = _tableCell.gameObject.AddComponent<DownloadProgressCell>();
  153. _queueCell.Init(DownloadController.Instance.Downloads[row]);
  154. return _queueCell;
  155. }
  156. }
  157. }