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.

425 lines
16 KiB

  1. using BSIPA_ModList.UI.ViewControllers;
  2. using CustomUI.BeatSaber;
  3. using CustomUI.MenuButton;
  4. using IPA.Loader;
  5. using IPA.Updating.BeatMods;
  6. using IPA.Utilities;
  7. using System;
  8. using System.Collections;
  9. using System.Collections.Generic;
  10. using System.Diagnostics;
  11. using System.Linq;
  12. using System.Text;
  13. using System.Threading.Tasks;
  14. using TMPro;
  15. using UnityEngine;
  16. using UnityEngine.UI;
  17. using VRUI;
  18. namespace BSIPA_ModList.UI
  19. {
  20. internal class ModInfoViewController : VRUIViewController
  21. {
  22. internal Sprite Icon;
  23. internal string Name;
  24. internal string Version;
  25. internal string Author;
  26. internal string Description;
  27. internal PluginLoader.PluginMetadata UpdateInfo;
  28. private static RectTransform rowTransformOriginal;
  29. private ModInfoView view;
  30. private RectTransform rowTransform;
  31. private Button linkHomeButton;
  32. private Button linkSourceButton;
  33. private Button linkDonateButton;
  34. private ModListFlowCoordinator flowController;
  35. private bool showEnableDisable = false;
  36. private TextMeshProUGUI restartMessage;
  37. private Button enableDisableButton;
  38. private new bool enabled = false;
  39. public void Init(Sprite icon, string name, string version, string author, string description, PluginLoader.PluginMetadata updateInfo, PluginManifest.LinksObject links = null, bool showEnDis = false, ModListFlowCoordinator mlfc = null)
  40. {
  41. showEnableDisable = showEnDis;
  42. Plugin.OnConfigChaned -= OptHideButton;
  43. Icon = icon;
  44. Name = name;
  45. Version = version;
  46. Author = author;
  47. Description = description;
  48. UpdateInfo = updateInfo;
  49. enabled = !PluginManager.IsDisabled(updateInfo);
  50. flowController = mlfc;
  51. if (rowTransformOriginal == null)
  52. rowTransformOriginal = MenuButtonUI.Instance.GetPrivateField<RectTransform>("menuButtonsOriginal");
  53. // i also have no clue why this is necessary
  54. rectTransform.anchorMin = new Vector2(0f, 0f);
  55. rectTransform.anchorMax = new Vector2(0.5f, 1f);
  56. var go = new GameObject("Info View", typeof(RectTransform));
  57. go.SetActive(false);
  58. go.AddComponent<RectMask2D>();
  59. view = go.AddComponent<ModInfoView>();
  60. var rt = view.transform as RectTransform;
  61. rt.SetParent(transform);
  62. rt.anchorMin = new Vector2(0f, 0f);
  63. rt.anchorMax = new Vector2(1f, 1f);
  64. rt.anchoredPosition = new Vector2(0f, 0);
  65. view.Init(this);
  66. go.SetActive(true);
  67. if (showEnDis)
  68. {
  69. restartMessage = BeatSaberUI.CreateText(rectTransform, "A restart is required to apply", new Vector2(11f, 33.5f));
  70. restartMessage.fontSize = 4f;
  71. restartMessage.gameObject.SetActive(false);
  72. enableDisableButton = BeatSaberUI.CreateUIButton(rectTransform, "CreditsButton", new Vector2(33, 32), new Vector2(25, 10), ToggleEnable);
  73. enableDisableButton.GetComponentInChildren<StartMiddleEndButtonBackgroundController>().SetMiddleSprite();
  74. UpdateButtonText();
  75. Plugin.OnConfigChaned += OptHideButton;
  76. OptHideButton(Plugin.config.Value);
  77. }
  78. SetupLinks(links);
  79. }
  80. ~ModInfoViewController()
  81. {
  82. Plugin.OnConfigChaned -= OptHideButton;
  83. }
  84. private void OptHideButton(SelfConfig cfg)
  85. {
  86. enableDisableButton?.gameObject.SetActive(cfg.ShowEnableDisable);
  87. }
  88. private Action setAction = () => { };
  89. private void ToggleEnable()
  90. {
  91. var info = enabled ? PluginManager.InfoFromMetadata(UpdateInfo) : null;
  92. bool needsRestart;
  93. if (enabled)
  94. needsRestart = PluginManager.DisablePlugin(info);
  95. else
  96. needsRestart = PluginManager.EnablePlugin(UpdateInfo);
  97. UpdateButtonText(!enabled, needsRestart);
  98. if (!needsRestart)
  99. flowController.exitActions.Add(setAction);
  100. }
  101. private void UpdateButtonText(bool? _enabled = null, bool _needsRestart = false)
  102. {
  103. enabled = _enabled ?? enabled;
  104. if (enabled)
  105. enableDisableButton.SetButtonText("Disable");
  106. else
  107. enableDisableButton.SetButtonText("Enable");
  108. restartMessage.gameObject.SetActive(_needsRestart);
  109. }
  110. public void Reload(string name, string version, string author, string description)
  111. {
  112. Name = name;
  113. Version = version;
  114. Author = author;
  115. Description = description;
  116. view.Refresh();
  117. }
  118. private void SetupLinks(PluginManifest.LinksObject links = null, Uri moreInfoLink = null)
  119. {
  120. bool addedLink = false;
  121. if (links != null || moreInfoLink != null)
  122. {
  123. Logger.log.Debug($"Adding links");
  124. rowTransform = Instantiate(rowTransformOriginal, rectTransform);
  125. rowTransform.anchorMin = new Vector2(0f, 0f);
  126. rowTransform.anchorMax = new Vector2(1f, .15f);
  127. rowTransform.anchoredPosition = new Vector2(-3.5f, 4f);
  128. rowTransform.sizeDelta = Vector2.zero;
  129. Destroy(rowTransform.GetComponent<StartMiddleEndButtonsGroup>());
  130. foreach (Transform child in rowTransform)
  131. {
  132. child.name = string.Empty;
  133. Destroy(child.gameObject);
  134. }
  135. if (links?.ProjectHome != null)
  136. {
  137. linkHomeButton = BeatSaberUI.CreateUIButton(rowTransform, "CreditsButton", buttonText: "Home", anchoredPosition: Vector2.zero, sizeDelta: new Vector2(20, 10),
  138. onClick: () => Process.Start(links.ProjectHome.ToString()));
  139. addedLink = true;
  140. }
  141. if (links?.ProjectSource != null)
  142. {
  143. linkSourceButton = BeatSaberUI.CreateUIButton(rowTransform, "CreditsButton", buttonText: "Source", anchoredPosition: Vector2.zero, sizeDelta: new Vector2(20, 10),
  144. onClick: () => Process.Start(links.ProjectSource.ToString()));
  145. addedLink = true;
  146. }
  147. if (links?.Donate != null)
  148. {
  149. linkDonateButton = BeatSaberUI.CreateUIButton(rowTransform, "CreditsButton", buttonText: "Donate", anchoredPosition: Vector2.zero, sizeDelta: new Vector2(20, 10),
  150. onClick: () => Process.Start(links.Donate.ToString()));
  151. addedLink = true;
  152. }
  153. if (moreInfoLink != null)
  154. {
  155. linkDonateButton = BeatSaberUI.CreateUIButton(rowTransform, "CreditsButton", buttonText: "More Info", anchoredPosition: Vector2.zero, sizeDelta: new Vector2(20, 10),
  156. onClick: () => Process.Start(moreInfoLink.ToString()));
  157. addedLink = true;
  158. }
  159. foreach (var cmp in rowTransform.GetComponentsInChildren<StartMiddleEndButtonBackgroundController>())
  160. cmp.SetMiddleSprite();
  161. }
  162. if (UpdateInfo != null && !addedLink)
  163. StartCoroutine(GetMoreInfoLink());
  164. }
  165. private IEnumerator GetMoreInfoLink()
  166. {
  167. Logger.log.Debug($"Getting more info link");
  168. Ref<ApiEndpoint.Mod> mod = new Ref<ApiEndpoint.Mod>(null);
  169. if (UpdateInfo.Id == null) yield break;
  170. yield return Updater.GetModInfo(UpdateInfo.Id, UpdateInfo.Version.ToString(), mod);
  171. try { mod.Verify(); }
  172. catch (Exception e)
  173. {
  174. Logger.log.Warn($"Error getting more info link for mod {UpdateInfo.Id}");
  175. Logger.log.Warn(e);
  176. yield break;
  177. }
  178. SetupLinks(null, mod.Value.Link);
  179. }
  180. #if DEBUG
  181. public void Update()
  182. {
  183. #if ADJUST_INFO_BUTTON_UI_LINKS
  184. RectTransform rt = rowTransform;
  185. if (rt == null) return;
  186. var cpos = rt.anchoredPosition;
  187. if (Input.GetKey(KeyCode.LeftArrow))
  188. {
  189. rt.anchoredPosition = new Vector2(cpos.x - .1f, cpos.y);
  190. }
  191. else if (Input.GetKey(KeyCode.RightArrow))
  192. {
  193. rt.anchoredPosition = new Vector2(cpos.x + .1f, cpos.y);
  194. }
  195. else if (Input.GetKey(KeyCode.UpArrow))
  196. {
  197. rt.anchoredPosition = new Vector2(cpos.x, cpos.y + .1f);
  198. }
  199. else if (Input.GetKey(KeyCode.DownArrow))
  200. {
  201. rt.anchoredPosition = new Vector2(cpos.x, cpos.y - .1f);
  202. }
  203. else
  204. return;
  205. Logger.log.Debug($"Position now at {rt.anchoredPosition}");
  206. #endif
  207. }
  208. #endif
  209. }
  210. internal class ModInfoView : MonoBehaviour
  211. {
  212. private MarkdownView mdv;
  213. private TextMeshProUGUI titleText;
  214. private TextMeshProUGUI authorText;
  215. private Image icon;
  216. private ModInfoViewController controller;
  217. private const string TitleFormat = "{0} <size=60%>{1}";
  218. public void Init(ModInfoViewController controller)
  219. {
  220. this.controller = controller;
  221. var rectTransform = transform as RectTransform;
  222. rectTransform.sizeDelta = new Vector2(60f, 10f);
  223. titleText = BeatSaberUI.CreateText(rectTransform, string.Format(TitleFormat, controller.Name, controller.Version), new Vector2(11f, 27.5f));
  224. titleText.fontSize = 6f;
  225. authorText = BeatSaberUI.CreateText(rectTransform, controller.Author, new Vector2(11f, 22f));
  226. authorText.fontSize = 4.5f;
  227. var mdvgo = new GameObject("MarkDown Desc");
  228. mdvgo.SetActive(false);
  229. mdv = mdvgo.AddComponent<MarkdownView>();
  230. mdv.rectTransform.SetParent(rectTransform);
  231. mdv.rectTransform.anchorMin = new Vector2(.5f, .5f);
  232. mdv.rectTransform.anchorMax = new Vector2(.5f, .5f);
  233. mdv.rectTransform.anchoredPosition = new Vector2(-4f, -3.6f);
  234. mdv.rectTransform.sizeDelta = new Vector2(65f, 40f);
  235. mdvgo.SetActive(true);
  236. mdv.Markdown = controller.Description;
  237. icon = new GameObject("Mod Info View Icon", typeof(RectTransform)).AddComponent<Image>();
  238. icon.gameObject.SetActive(false);
  239. icon.rectTransform.SetParent(rectTransform, false);
  240. icon.rectTransform.anchorMin = new Vector2(0.5f, 0.44f);
  241. icon.rectTransform.anchorMax = new Vector2(0.5f, 0.5f);
  242. icon.rectTransform.sizeDelta = new Vector2(60f, 10f);
  243. icon.rectTransform.anchoredPosition = new Vector2(-27.8f, 27.3f);
  244. icon.sprite = controller.Icon;
  245. icon.preserveAspect = true;
  246. icon.useSpriteMesh = true;
  247. icon.material = CustomUI.Utilities.UIUtilities.NoGlowMaterial;
  248. icon.gameObject.SetActive(true);
  249. }
  250. public void Refresh()
  251. {
  252. titleText.text = string.Format(TitleFormat, controller.Name, controller.Version);
  253. authorText.text = controller.Author;
  254. mdv.Markdown = controller.Description;
  255. icon.sprite = controller.Icon;
  256. }
  257. #if DEBUG
  258. #if ADJUST_INFO_TEXT_UI_KEYS
  259. private int currentItem = 0;
  260. #endif
  261. public void Update()
  262. {
  263. #if ADJUST_INFO_TEXT_UI_KEYS
  264. RectTransform rt;
  265. switch (currentItem)
  266. {
  267. default:
  268. currentItem = 0;
  269. goto case 0; // idk why this is needed tbh
  270. case 0:
  271. rt = titleText.rectTransform;
  272. break;
  273. case 1:
  274. rt = authorText.rectTransform;
  275. break;
  276. case 2:
  277. rt = descText.rectTransform;
  278. break;
  279. }
  280. if (Input.GetKeyDown(KeyCode.N))
  281. currentItem++;
  282. var cpos = rt.anchoredPosition;
  283. if (Input.GetKey(KeyCode.LeftArrow))
  284. {
  285. rt.anchoredPosition = new Vector2(cpos.x - .1f, cpos.y);
  286. }
  287. else if (Input.GetKey(KeyCode.RightArrow))
  288. {
  289. rt.anchoredPosition = new Vector2(cpos.x + .1f, cpos.y);
  290. }
  291. else if (Input.GetKey(KeyCode.UpArrow))
  292. {
  293. rt.anchoredPosition = new Vector2(cpos.x, cpos.y + .1f);
  294. }
  295. else if (Input.GetKey(KeyCode.DownArrow))
  296. {
  297. rt.anchoredPosition = new Vector2(cpos.x, cpos.y - .1f);
  298. }
  299. else
  300. return;
  301. Logger.log.Debug($"Position now at {rt.anchoredPosition}");
  302. #endif
  303. #if ADJUST_INFO_ICON_UI_KEYS
  304. var rt = icon.rectTransform;
  305. if (Input.GetKey(KeyCode.Z))
  306. { // adjust anchormin
  307. var cpos = rt.anchorMin;
  308. if (Input.GetKey(KeyCode.LeftArrow))
  309. {
  310. rt.anchorMin = new Vector2(cpos.x - .001f, cpos.y);
  311. }
  312. else if (Input.GetKey(KeyCode.RightArrow))
  313. {
  314. rt.anchorMin = new Vector2(cpos.x + .001f, cpos.y);
  315. }
  316. else if (Input.GetKey(KeyCode.UpArrow))
  317. {
  318. rt.anchorMin = new Vector2(cpos.x, cpos.y + .001f);
  319. }
  320. else if (Input.GetKey(KeyCode.DownArrow))
  321. {
  322. rt.anchorMin = new Vector2(cpos.x, cpos.y - .001f);
  323. }
  324. else
  325. return;
  326. Logger.log.Debug($"Anchor min now at {rt.anchorMin}");
  327. }
  328. else if(Input.GetKey(KeyCode.X))
  329. { // adjust anchorMax
  330. var cpos = rt.anchorMax;
  331. if (Input.GetKey(KeyCode.LeftArrow))
  332. {
  333. rt.anchorMax = new Vector2(cpos.x - .001f, cpos.y);
  334. }
  335. else if (Input.GetKey(KeyCode.RightArrow))
  336. {
  337. rt.anchorMax = new Vector2(cpos.x + .001f, cpos.y);
  338. }
  339. else if (Input.GetKey(KeyCode.UpArrow))
  340. {
  341. rt.anchorMax = new Vector2(cpos.x, cpos.y + .001f);
  342. }
  343. else if (Input.GetKey(KeyCode.DownArrow))
  344. {
  345. rt.anchorMax = new Vector2(cpos.x, cpos.y - .001f);
  346. }
  347. else
  348. return;
  349. Logger.log.Debug($"Anchor max now at {rt.anchorMax}");
  350. }
  351. else
  352. {
  353. var cpos = rt.anchoredPosition;
  354. if (Input.GetKey(KeyCode.LeftArrow))
  355. {
  356. rt.anchoredPosition = new Vector2(cpos.x - .1f, cpos.y);
  357. }
  358. else if (Input.GetKey(KeyCode.RightArrow))
  359. {
  360. rt.anchoredPosition = new Vector2(cpos.x + .1f, cpos.y);
  361. }
  362. else if (Input.GetKey(KeyCode.UpArrow))
  363. {
  364. rt.anchoredPosition = new Vector2(cpos.x, cpos.y + .1f);
  365. }
  366. else if (Input.GetKey(KeyCode.DownArrow))
  367. {
  368. rt.anchoredPosition = new Vector2(cpos.x, cpos.y - .1f);
  369. }
  370. else
  371. return;
  372. Logger.log.Debug($"Position now at {rt.anchoredPosition}");
  373. }
  374. #endif
  375. }
  376. #endif
  377. }
  378. }