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.

180 lines
8.7 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 const float TableXOffset = -20f;
  29. private const float ButtonXOffset = 36f;
  30. private static readonly Vector2 ButtonSize = new Vector2(40f, 10f);
  31. protected override void DidActivate(bool firstActivation, ActivationType type)
  32. {
  33. if (firstActivation && type == ActivationType.AddedToHierarchy)
  34. {
  35. DownloadController.Instance.OnDownloaderListChanged -= Refresh;
  36. DownloadController.Instance.OnDownloadStateChanged -= DownloaderStateChanged;
  37. _songListTableCellInstance = Resources.FindObjectsOfTypeAll<LevelListTableCell>().First(x => (x.name == "LevelListTableCell"));
  38. _titleText = BeatSaberUI.CreateText(rectTransform, "DOWNLOAD QUEUE", new Vector2(0f, 35f));
  39. _titleText.alignment = TextAlignmentOptions.Top;
  40. _titleText.fontSize = 6f;
  41. _manualDownloadText = BeatSaberUI.CreateText(rectTransform, "Manual Restart Required", new Vector2(37f, -3f));
  42. _manualDownloadText.alignment = TextAlignmentOptions.Top;
  43. _manualDownloadText.fontSize = 4f;
  44. _manualDownloadText.gameObject.SetActive(false);
  45. _pageUpButton = Instantiate(Resources.FindObjectsOfTypeAll<Button>().Last(x => (x.name == "PageUpButton")), rectTransform, false);
  46. (_pageUpButton.transform as RectTransform).anchorMin = new Vector2(0.5f, 1f);
  47. (_pageUpButton.transform as RectTransform).anchorMax = new Vector2(0.5f, 1f);
  48. (_pageUpButton.transform as RectTransform).anchoredPosition = new Vector2(TableXOffset, -14f);
  49. (_pageUpButton.transform as RectTransform).sizeDelta = new Vector2(40f, 10f);
  50. _pageUpButton.interactable = true;
  51. _pageUpButton.onClick.AddListener(delegate ()
  52. {
  53. _currentlyUpdatingTableView.PageScrollUp();
  54. });
  55. _pageDownButton = Instantiate(Resources.FindObjectsOfTypeAll<Button>().First(x => (x.name == "PageDownButton")), rectTransform, false);
  56. (_pageDownButton.transform as RectTransform).anchorMin = new Vector2(0.5f, 0f);
  57. (_pageDownButton.transform as RectTransform).anchorMax = new Vector2(0.5f, 0f);
  58. (_pageDownButton.transform as RectTransform).anchoredPosition = new Vector2(TableXOffset, 8f);
  59. (_pageDownButton.transform as RectTransform).sizeDelta = new Vector2(40f, 10f);
  60. _pageDownButton.interactable = true;
  61. _pageDownButton.onClick.AddListener(delegate ()
  62. {
  63. _currentlyUpdatingTableView.PageScrollDown();
  64. });
  65. var gobj = new GameObject("DownloadTable");
  66. gobj.SetActive(false);
  67. _currentlyUpdatingTableView = gobj.AddComponent<TableView>();
  68. _currentlyUpdatingTableView.transform.SetParent(rectTransform, false);
  69. _currentlyUpdatingTableView.SetPrivateField("_isInitialized", false);
  70. _currentlyUpdatingTableView.SetPrivateField("_preallocatedCells", new TableView.CellsGroup[0]);
  71. _currentlyUpdatingTableView.Init();
  72. RectMask2D viewportMask = Instantiate(Resources.FindObjectsOfTypeAll<RectMask2D>().First(), _currentlyUpdatingTableView.transform, false);
  73. viewportMask.transform.DetachChildren();
  74. _currentlyUpdatingTableView.GetComponentsInChildren<RectTransform>().First(x => x.name == "Content").transform.SetParent(viewportMask.rectTransform, false);
  75. (_currentlyUpdatingTableView.transform as RectTransform).anchorMin = new Vector2(0.3f, 0.5f);
  76. (_currentlyUpdatingTableView.transform as RectTransform).anchorMax = new Vector2(0.7f, 0.5f);
  77. (_currentlyUpdatingTableView.transform as RectTransform).sizeDelta = new Vector2(0f, 60f);
  78. (_currentlyUpdatingTableView.transform as RectTransform).anchoredPosition = new Vector3(TableXOffset, -3f);
  79. ReflectionUtil.SetPrivateField(_currentlyUpdatingTableView, "_pageUpButton", _pageUpButton);
  80. ReflectionUtil.SetPrivateField(_currentlyUpdatingTableView, "_pageDownButton", _pageDownButton);
  81. _currentlyUpdatingTableView.selectionType = TableViewSelectionType.None;
  82. _currentlyUpdatingTableView.dataSource = this;
  83. gobj.SetActive(true);
  84. _checkForUpdates = BeatSaberUI.CreateUIButton(rectTransform, "CreditsButton", new Vector2(ButtonXOffset, -30f), ButtonSize, CheckUpdates, "Check for updates");
  85. _checkForUpdates.interactable = DownloadController.Instance.CanCheck || DownloadController.Instance.CanReset;
  86. _checkForUpdates.ToggleWordWrapping(false);
  87. _downloadUpdates = BeatSaberUI.CreateUIButton(rectTransform, "CreditsButton", new Vector2(ButtonXOffset, -19f), ButtonSize, DownloadUpdates, "Download Updates");
  88. _downloadUpdates.interactable = DownloadController.Instance.CanDownload;
  89. _downloadUpdates.ToggleWordWrapping(false);
  90. _restartGame = BeatSaberUI.CreateUIButton(rectTransform, "CreditsButton", new Vector2(ButtonXOffset, -8f), ButtonSize, Restart, "Restart Game");
  91. _restartGame.interactable = DownloadController.Instance.HadUpdates;
  92. _restartGame.ToggleWordWrapping(false);
  93. }
  94. DownloadController.Instance.OnDownloaderListChanged += Refresh;
  95. DownloadController.Instance.OnDownloadStateChanged += DownloaderStateChanged;
  96. DownloaderStateChanged();
  97. }
  98. private void Restart()
  99. {
  100. Process.Start(Path.Combine(Environment.CurrentDirectory, Process.GetCurrentProcess().MainModule.FileName), Environment.CommandLine);
  101. Application.Quit();
  102. }
  103. private void DownloadUpdates()
  104. {
  105. if (DownloadController.Instance.CanDownload)
  106. DownloadController.Instance.StartDownloads();
  107. }
  108. private void CheckUpdates()
  109. {
  110. Updater.ResetRequestCache();
  111. if (DownloadController.Instance.CanReset)
  112. DownloadController.Instance.ResetCheck(false);
  113. if (DownloadController.Instance.CanCheck)
  114. DownloadController.Instance.CheckForUpdates();
  115. }
  116. private void DownloaderStateChanged()
  117. {
  118. _checkForUpdates.interactable = (DownloadController.Instance.CanCheck || DownloadController.Instance.CanReset) && !Updater.NeedsManualRestart;
  119. _downloadUpdates.interactable = DownloadController.Instance.CanDownload && !Updater.NeedsManualRestart;
  120. _restartGame.interactable = DownloadController.Instance.HadUpdates && !Updater.NeedsManualRestart;
  121. _manualDownloadText.gameObject.SetActive(Updater.NeedsManualRestart);
  122. }
  123. protected override void DidDeactivate(DeactivationType type)
  124. {
  125. DownloadController.Instance.OnDownloaderListChanged -= Refresh;
  126. DownloadController.Instance.OnDownloadStateChanged -= DownloaderStateChanged;
  127. if (DownloadController.Instance.IsDone)
  128. FloatingNotification.instance.Close();
  129. }
  130. private void Refresh()
  131. {
  132. _currentlyUpdatingTableView.ReloadData();
  133. }
  134. public float CellSize()
  135. {
  136. return 8.5f;
  137. }
  138. public int NumberOfCells()
  139. {
  140. return DownloadController.Instance.Downloads.Count;
  141. }
  142. public TableCell CellForIdx(int row)
  143. {
  144. LevelListTableCell _tableCell = Instantiate(_songListTableCellInstance);
  145. DownloadProgressCell _queueCell = _tableCell.gameObject.AddComponent<DownloadProgressCell>();
  146. _queueCell.Init(DownloadController.Instance.Downloads[row]);
  147. return _queueCell;
  148. }
  149. }
  150. }