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.

216 lines
8.1 KiB

  1. using System;
  2. using System.Linq;
  3. using System.Collections.Generic;
  4. using CustomUI.BeatSaber;
  5. using CustomUI.Utilities;
  6. using HMUI;
  7. using IPA.Loader;
  8. using IPA.Old;
  9. using UnityEngine;
  10. using VRUI;
  11. using IPA.Loader.Features;
  12. using TMPro;
  13. namespace BSIPA_ModList.UI
  14. {
  15. internal class ModListController : CustomListViewController
  16. {
  17. private interface IClickableCell
  18. {
  19. void OnSelect(ModListController cntrl);
  20. }
  21. private class BSIPAModCell : CustomCellInfo, IClickableCell
  22. {
  23. private static Sprite _defaultIcon;
  24. public static Sprite DefaultIcon
  25. {
  26. get
  27. {
  28. if (_defaultIcon == null)
  29. _defaultIcon = UIUtilities.LoadSpriteFromResources("BSIPA_ModList.Icons.mod_bsipa.png");
  30. return _defaultIcon;
  31. }
  32. }
  33. internal PluginLoader.PluginInfo Plugin;
  34. private ModListController list;
  35. public BSIPAModCell(ModListController list, PluginLoader.PluginInfo plugin)
  36. : base($"{plugin.Metadata.Name} <size=60%>v{plugin.Metadata.Version}", plugin.Metadata.Manifest.Author, null)
  37. {
  38. Plugin = plugin;
  39. this.list = list;
  40. if (string.IsNullOrWhiteSpace(subtext))
  41. subtext = "<color=#BFBFBF><i>Unspecified Author</i>";
  42. if (plugin.Metadata.Manifest.IconPath != null)
  43. icon = UIUtilities.LoadSpriteRaw(UIUtilities.GetResource(plugin.Metadata.Assembly, plugin.Metadata.Manifest.IconPath));
  44. else
  45. icon = DefaultIcon;
  46. Logger.log.Debug($"BSIPAModCell {plugin.Metadata.Name} {plugin.Metadata.Version}");
  47. }
  48. private ModInfoViewController infoView;
  49. public void OnSelect(ModListController cntrl)
  50. {
  51. Logger.log.Debug($"Selected BSIPAModCell {Plugin.Metadata.Name} {Plugin.Metadata.Version}");
  52. if (infoView == null)
  53. {
  54. var desc = Plugin.Metadata.Manifest.Description;
  55. if (string.IsNullOrWhiteSpace(desc))
  56. desc = "<color=#BFBFBF><i>No description</i>";
  57. infoView = BeatSaberUI.CreateViewController<ModInfoViewController>();
  58. infoView.Init(icon, Plugin.Metadata.Name, "v" + Plugin.Metadata.Version.ToString(), subtext,
  59. desc, Plugin.Metadata.Features.FirstOrDefault(f => f is NoUpdateFeature) == null);
  60. }
  61. list.flow.SetSelected(infoView, immediate: list.flow.HasSelected);
  62. }
  63. }
  64. private class BSIPAIgnoredModCell : CustomCellInfo, IClickableCell
  65. {
  66. internal PluginLoader.PluginMetadata Plugin;
  67. private ModListController list;
  68. private const string authorFormat = "{0} <color=#BFBFBF>- <i>Not loaded</i>";
  69. private string authorText;
  70. public BSIPAIgnoredModCell(ModListController list, PluginLoader.PluginMetadata plugin)
  71. : base($"<color=#878787>{plugin.Name} <size=60%>v{plugin.Version}", "", BSIPAModCell.DefaultIcon)
  72. {
  73. Plugin = plugin;
  74. this.list = list;
  75. if (string.IsNullOrWhiteSpace(plugin.Manifest.Author))
  76. authorText = "<color=#BFBFBF><i>Unspecified Author</i>";
  77. else
  78. authorText = plugin.Manifest.Author;
  79. subtext = string.Format(authorFormat, authorText);
  80. Logger.log.Debug($"BSIPAIgnoredModCell {plugin.Name} {plugin.Version}");
  81. }
  82. private ModInfoViewController infoView;
  83. public void OnSelect(ModListController cntrl)
  84. {
  85. Logger.log.Debug($"Selected BSIPAIgnoredModCell {Plugin.Name} {Plugin.Version}");
  86. if (infoView == null)
  87. {
  88. var desc = Plugin.Manifest.Description;
  89. if (string.IsNullOrWhiteSpace(desc))
  90. desc = "<color=#BFBFBF><i>No description</i>";
  91. infoView = BeatSaberUI.CreateViewController<ModInfoViewController>();
  92. infoView.Init(icon, Plugin.Name, "v" + Plugin.Version.ToString(), authorText,
  93. desc, Plugin.Features.FirstOrDefault(f => f is NoUpdateFeature) == null);
  94. }
  95. list.flow.SetSelected(infoView, immediate: list.flow.HasSelected);
  96. }
  97. }
  98. #pragma warning disable CS0618
  99. private class IPAModCell : CustomCellInfo, IClickableCell
  100. {
  101. private static Sprite _defaultIcon;
  102. public static Sprite DefaultIcon
  103. {
  104. get
  105. {
  106. if (_defaultIcon == null)
  107. _defaultIcon = UIUtilities.LoadSpriteFromResources("BSIPA_ModList.Icons.mod_ipa.png");
  108. return _defaultIcon;
  109. }
  110. }
  111. internal IPlugin Plugin;
  112. private ModListController list;
  113. public IPAModCell(ModListController list, IPlugin plugin)
  114. : base($"{plugin.Name} <size=60%>{plugin.Version}", "<color=#BFBFBF><i>Legacy</i>", DefaultIcon)
  115. {
  116. Plugin = plugin;
  117. this.list = list;
  118. Logger.log.Debug($"IPAModCell {plugin.Name} {plugin.Version}");
  119. }
  120. private ModInfoViewController infoView;
  121. public void OnSelect(ModListController cntrl)
  122. {
  123. Logger.log.Debug($"Selected IPAModCell {Plugin.Name} {Plugin.Version}");
  124. if (infoView == null)
  125. {
  126. infoView = BeatSaberUI.CreateViewController<ModInfoViewController>();
  127. infoView.Init(icon, Plugin.Name, "v" + Plugin.Version.ToString(), "<color=#BFBFBF><i>Unknown Author</i>",
  128. "<color=#A0A0A0>This mod was written for IPA Reloaded. No metadata is avaliable for this mod. " +
  129. "Please contact the mod author and ask them to port it to BSIPA to provide more information.", false);
  130. }
  131. list.flow.SetSelected(infoView, immediate: list.flow.HasSelected);
  132. }
  133. }
  134. #pragma warning restore
  135. public override TableCell CellForIdx(int idx)
  136. {
  137. var cell = base.CellForIdx(idx) as LevelListTableCell;
  138. var nameText = cell.GetPrivateField<TextMeshProUGUI>("_songNameText");
  139. nameText.overflowMode = TextOverflowModes.Overflow;
  140. return cell;
  141. }
  142. private ModListFlowCoordinator flow;
  143. #pragma warning disable CS0618
  144. public void Init(ModListFlowCoordinator flow, IEnumerable<PluginLoader.PluginInfo> bsipaPlugins, IEnumerable<PluginLoader.PluginMetadata> ignoredPlugins, IEnumerable<IPlugin> ipaPlugins)
  145. {
  146. Logger.log.Debug("List Controller Init");
  147. DidActivateEvent = DidActivate;
  148. DidSelectRowEvent = DidSelectRow;
  149. rectTransform.anchorMin = new Vector2(0f, 0f);
  150. rectTransform.anchorMax = new Vector2(.4f, 1f);
  151. includePageButtons = true;
  152. this.flow = flow;
  153. reuseIdentifier = "BSIPAModListTableCell";
  154. foreach (var plugin in bsipaPlugins)
  155. Data.Add(new BSIPAModCell(this, plugin));
  156. foreach (var plugin in ignoredPlugins)
  157. Data.Add(new BSIPAIgnoredModCell(this, plugin));
  158. foreach (var plugin in ipaPlugins)
  159. Data.Add(new IPAModCell(this, plugin));
  160. }
  161. #pragma warning restore
  162. private void DidSelectRow(TableView view, int index)
  163. {
  164. Debug.Assert(ReferenceEquals(view.dataSource, this));
  165. (Data[index] as IClickableCell)?.OnSelect(this);
  166. }
  167. private new void DidActivate(bool first, ActivationType type)
  168. {
  169. var rt = _customListTableView.transform as RectTransform;
  170. rt.anchorMin = new Vector2(.1f, 0f);
  171. rt.anchorMax = new Vector2(.9f, 1f);
  172. }
  173. }
  174. }