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.

153 lines
7.3 KiB

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