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.

189 lines
9.1 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 = -20f;
  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, -3f);
  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. _currentlyUpdatingTableView.RefreshScrollButtons();
  90. _checkForUpdates = BeatSaberUI.CreateUIButton(rectTransform, "CreditsButton", new Vector2(ButtonXOffset, -30f), ButtonSize, CheckUpdates, "Check for updates");
  91. _checkForUpdates.interactable = DownloadController.Instance.CanCheck || DownloadController.Instance.CanReset;
  92. _checkForUpdates.ToggleWordWrapping(false);
  93. _downloadUpdates = BeatSaberUI.CreateUIButton(rectTransform, "CreditsButton", new Vector2(ButtonXOffset, -19f), ButtonSize, DownloadUpdates, "Download Updates");
  94. _downloadUpdates.interactable = DownloadController.Instance.CanDownload;
  95. _downloadUpdates.ToggleWordWrapping(false);
  96. _restartGame = BeatSaberUI.CreateUIButton(rectTransform, "CreditsButton", new Vector2(ButtonXOffset, -8f), ButtonSize, Restart, "Restart Game");
  97. _restartGame.interactable = DownloadController.Instance.HadUpdates;
  98. _restartGame.ToggleWordWrapping(false);
  99. }
  100. DownloadController.Instance.OnDownloaderListChanged += Refresh;
  101. DownloadController.Instance.OnDownloadStateChanged += DownloaderStateChanged;
  102. DownloaderStateChanged();
  103. }
  104. private void Restart()
  105. {
  106. Process.Start(Path.Combine(Environment.CurrentDirectory, Process.GetCurrentProcess().MainModule.FileName), Environment.CommandLine);
  107. Application.Quit();
  108. }
  109. private void DownloadUpdates()
  110. {
  111. if (DownloadController.Instance.CanDownload)
  112. DownloadController.Instance.StartDownloads();
  113. }
  114. private void CheckUpdates()
  115. {
  116. Updater.ResetRequestCache();
  117. if (DownloadController.Instance.CanReset)
  118. DownloadController.Instance.ResetCheck(false);
  119. if (DownloadController.Instance.CanCheck)
  120. DownloadController.Instance.CheckForUpdates();
  121. }
  122. private void DownloaderStateChanged()
  123. {
  124. _checkForUpdates.interactable = (DownloadController.Instance.CanCheck || DownloadController.Instance.CanReset) && !Updater.NeedsManualRestart;
  125. _downloadUpdates.interactable = DownloadController.Instance.CanDownload && !Updater.NeedsManualRestart;
  126. _restartGame.interactable = DownloadController.Instance.HadUpdates && !Updater.NeedsManualRestart;
  127. _manualDownloadText.gameObject.SetActive(Updater.NeedsManualRestart);
  128. }
  129. protected override void DidDeactivate(DeactivationType type)
  130. {
  131. DownloadController.Instance.OnDownloaderListChanged -= Refresh;
  132. DownloadController.Instance.OnDownloadStateChanged -= DownloaderStateChanged;
  133. if (DownloadController.Instance.IsDone)
  134. FloatingNotification.instance.Close();
  135. }
  136. private void Refresh()
  137. {
  138. _currentlyUpdatingTableView.ReloadData();
  139. }
  140. public float CellSize()
  141. {
  142. return 8.5f;
  143. }
  144. public int NumberOfCells()
  145. {
  146. return DownloadController.Instance.Downloads.Count;
  147. }
  148. public TableCell CellForIdx(TableView view, int row)
  149. {
  150. LevelListTableCell _tableCell = Instantiate(_songListTableCellInstance);
  151. DownloadProgressCell _queueCell = _tableCell.gameObject.AddComponent<DownloadProgressCell>();
  152. _queueCell.Init(DownloadController.Instance.Downloads[row]);
  153. return _queueCell;
  154. }
  155. }
  156. }