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.

279 lines
11 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. {
  44. try
  45. {
  46. icon = UIUtilities.LoadSpriteRaw(UIUtilities.GetResource(plugin.Metadata.Assembly, plugin.Metadata.Manifest.IconPath));
  47. }
  48. catch (Exception e)
  49. {
  50. Logger.log.Error($"Error loading icon for {plugin.Metadata.Name}");
  51. Logger.log.Error(e);
  52. }
  53. }
  54. if (icon == null)
  55. icon = DefaultIcon;
  56. Logger.log.Debug($"BSIPAModCell {plugin.Metadata.Name} {plugin.Metadata.Version}");
  57. }
  58. private ModInfoViewController infoView;
  59. public void OnSelect(ModListController cntrl)
  60. {
  61. Logger.log.Debug($"Selected BSIPAModCell {Plugin.Metadata.Name} {Plugin.Metadata.Version}");
  62. if (infoView == null)
  63. {
  64. var desc = Plugin.Metadata.Manifest.Description;
  65. if (string.IsNullOrWhiteSpace(desc))
  66. desc = "<color=#BFBFBF><i>No description</i>";
  67. infoView = BeatSaberUI.CreateViewController<ModInfoViewController>();
  68. infoView.Init(icon, Plugin.Metadata.Name, "v" + Plugin.Metadata.Version.ToString(), subtext,
  69. desc, Plugin.Metadata.Features.FirstOrDefault(f => f is NoUpdateFeature) != null ? Plugin.Metadata : null);
  70. }
  71. list.flow.SetSelected(infoView, immediate: list.flow.HasSelected);
  72. }
  73. }
  74. private class BSIPAIgnoredModCell : CustomCellInfo, IClickableCell
  75. {
  76. internal PluginLoader.PluginMetadata Plugin;
  77. private ModListController list;
  78. private const string authorFormat = "{0} <color=#BFBFBF>- <i>Not loaded</i>";
  79. private string authorText;
  80. public BSIPAIgnoredModCell(ModListController list, PluginLoader.PluginMetadata plugin)
  81. : base($"<color=#878787>{plugin.Name} <size=60%>v{plugin.Version}", "", BSIPAModCell.DefaultIcon)
  82. {
  83. Plugin = plugin;
  84. this.list = list;
  85. if (string.IsNullOrWhiteSpace(plugin.Manifest.Author))
  86. authorText = "<color=#BFBFBF><i>Unspecified Author</i>";
  87. else
  88. authorText = plugin.Manifest.Author;
  89. subtext = string.Format(authorFormat, authorText);
  90. Logger.log.Debug($"BSIPAIgnoredModCell {plugin.Name} {plugin.Version}");
  91. }
  92. private ModInfoViewController infoView;
  93. public void OnSelect(ModListController cntrl)
  94. {
  95. Logger.log.Debug($"Selected BSIPAIgnoredModCell {Plugin.Name} {Plugin.Version}");
  96. if (infoView == null)
  97. {
  98. var desc = Plugin.Manifest.Description;
  99. if (string.IsNullOrWhiteSpace(desc))
  100. desc = "<color=#BFBFBF><i>No description</i>";
  101. infoView = BeatSaberUI.CreateViewController<ModInfoViewController>();
  102. infoView.Init(icon, Plugin.Name, "v" + Plugin.Version.ToString(), authorText,
  103. desc, Plugin.Features.FirstOrDefault(f => f is NoUpdateFeature) != null ? Plugin : null);
  104. }
  105. list.flow.SetSelected(infoView, immediate: list.flow.HasSelected);
  106. }
  107. }
  108. private class LibraryModCell : CustomCellInfo, IClickableCell
  109. {
  110. private static Sprite _defaultIcon;
  111. public static Sprite DefaultIcon
  112. {
  113. get
  114. {
  115. if (_defaultIcon == null)
  116. _defaultIcon = UIUtilities.LoadSpriteFromResources("BSIPA_ModList.Icons.library.png");
  117. return _defaultIcon;
  118. }
  119. }
  120. internal PluginLoader.PluginInfo Plugin;
  121. private ModListController list;
  122. public LibraryModCell(ModListController list, PluginLoader.PluginInfo plugin)
  123. : base($"{plugin.Metadata.Name} <size=60%>v{plugin.Metadata.Version}", plugin.Metadata.Manifest.Author, null)
  124. {
  125. Plugin = plugin;
  126. this.list = list;
  127. if (string.IsNullOrWhiteSpace(subtext))
  128. subtext = "<color=#BFBFBF><i>Unspecified Author</i>";
  129. icon = DefaultIcon;
  130. Logger.log.Debug($"LibraryModCell {plugin.Metadata.Name} {plugin.Metadata.Version}");
  131. }
  132. private ModInfoViewController infoView;
  133. public void OnSelect(ModListController cntrl)
  134. {
  135. Logger.log.Debug($"Selected LibraryModCell {Plugin.Metadata.Name} {Plugin.Metadata.Version}");
  136. if (infoView == null)
  137. {
  138. var desc = Plugin.Metadata.Manifest.Description;
  139. if (string.IsNullOrWhiteSpace(desc))
  140. desc = "<color=#BFBFBF><i>No description</i>";
  141. infoView = BeatSaberUI.CreateViewController<ModInfoViewController>();
  142. infoView.Init(icon, Plugin.Metadata.Name, "v" + Plugin.Metadata.Version.ToString(), subtext,
  143. desc, Plugin.Metadata.Features.FirstOrDefault(f => f is NoUpdateFeature) != null ? Plugin.Metadata : null);
  144. }
  145. list.flow.SetSelected(infoView, immediate: list.flow.HasSelected);
  146. }
  147. }
  148. #pragma warning disable CS0618
  149. private class IPAModCell : CustomCellInfo, IClickableCell
  150. {
  151. private static Sprite _defaultIcon;
  152. public static Sprite DefaultIcon
  153. {
  154. get
  155. {
  156. if (_defaultIcon == null)
  157. _defaultIcon = UIUtilities.LoadSpriteFromResources("BSIPA_ModList.Icons.mod_ipa.png");
  158. return _defaultIcon;
  159. }
  160. }
  161. internal IPlugin Plugin;
  162. private ModListController list;
  163. public IPAModCell(ModListController list, IPlugin plugin)
  164. : base($"{plugin.Name} <size=60%>{plugin.Version}", "<color=#BFBFBF><i>Legacy</i>", DefaultIcon)
  165. {
  166. Plugin = plugin;
  167. this.list = list;
  168. Logger.log.Debug($"IPAModCell {plugin.Name} {plugin.Version}");
  169. }
  170. private ModInfoViewController infoView;
  171. public void OnSelect(ModListController cntrl)
  172. {
  173. Logger.log.Debug($"Selected IPAModCell {Plugin.Name} {Plugin.Version}");
  174. if (infoView == null)
  175. {
  176. infoView = BeatSaberUI.CreateViewController<ModInfoViewController>();
  177. infoView.Init(icon, Plugin.Name, "v" + Plugin.Version.ToString(), "<color=#BFBFBF><i>Unknown Author</i>",
  178. "<color=#A0A0A0>This mod was written for IPA Reloaded. No metadata is avaliable for this mod. " +
  179. "Please contact the mod author and ask them to port it to BSIPA to provide more information.", null);
  180. }
  181. list.flow.SetSelected(infoView, immediate: list.flow.HasSelected);
  182. }
  183. }
  184. #pragma warning restore
  185. public override TableCell CellForIdx(int idx)
  186. {
  187. var cell = base.CellForIdx(idx) as LevelListTableCell;
  188. var nameText = cell.GetPrivateField<TextMeshProUGUI>("_songNameText");
  189. nameText.overflowMode = TextOverflowModes.Overflow;
  190. return cell;
  191. }
  192. private ModListFlowCoordinator flow;
  193. #pragma warning disable CS0618
  194. public void Init(ModListFlowCoordinator flow, IEnumerable<PluginLoader.PluginInfo> bsipaPlugins, IEnumerable<PluginLoader.PluginMetadata> ignoredPlugins, IEnumerable<IPlugin> ipaPlugins)
  195. {
  196. Logger.log.Debug("List Controller Init");
  197. DidActivateEvent = DidActivate;
  198. DidSelectRowEvent = DidSelectRow;
  199. rectTransform.anchorMin = new Vector2(0f, 0f);
  200. rectTransform.anchorMax = new Vector2(.4f, 1f);
  201. includePageButtons = true;
  202. this.flow = flow;
  203. reuseIdentifier = "BSIPAModListTableCell";
  204. foreach (var plugin in bsipaPlugins.Where(p => !p.Metadata.IsBare))
  205. Data.Add(new BSIPAModCell(this, plugin));
  206. foreach (var plugin in ignoredPlugins)
  207. Data.Add(new BSIPAIgnoredModCell(this, plugin));
  208. foreach (var plugin in bsipaPlugins.Where(p => p.Metadata.IsBare))
  209. Data.Add(new LibraryModCell(this, plugin));
  210. foreach (var plugin in ipaPlugins)
  211. Data.Add(new IPAModCell(this, plugin));
  212. }
  213. #pragma warning restore
  214. private void DidSelectRow(TableView view, int index)
  215. {
  216. Debug.Assert(ReferenceEquals(view.dataSource, this));
  217. (Data[index] as IClickableCell)?.OnSelect(this);
  218. }
  219. private new void DidActivate(bool first, ActivationType type)
  220. {
  221. var rt = _customListTableView.transform as RectTransform;
  222. rt.anchorMin = new Vector2(.1f, 0f);
  223. rt.anchorMax = new Vector2(.9f, 1f);
  224. }
  225. }
  226. }