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.

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