using System; using System.Collections.Generic; using System.Linq; using UnityEngine; using CommonMark; using CommonMark.Syntax; using UnityEngine.UI; using TMPro; using CustomUI.BeatSaber; using IPA.Utilities; using System.Reflection; using UnityEngine.EventSystems; using System.Diagnostics; using System.Collections; namespace BSIPA_ModList.UI.ViewControllers { [RequireComponent(typeof(RectTransform))] public class MarkdownView : MonoBehaviour { private class TagTypeComponent : MonoBehaviour { internal BlockTag Tag; internal HeadingData hData; internal ListData lData; internal int listCount; internal int listIndex; } private string markdown = ""; private bool mdDirty = false; public string Markdown { get => markdown; set { markdown = value; mdDirty = true; } } public RectTransform rectTransform => GetComponent(); private ScrollView scrView; private RectTransform content; private RectTransform viewport; private CommonMarkSettings settings; public MarkdownView() { settings = CommonMarkSettings.Default.Clone(); settings.AdditionalFeatures = CommonMarkAdditionalFeatures.All; settings.RenderSoftLineBreaksAsLineBreaks = false; settings.UriResolver = ResolveUri; } public Func HasEmbeddedImage; private string ResolveUri(string arg) { var name = arg.Substring(3); if (!arg.StartsWith("!::") && !arg.StartsWith("w::")) { // !:: means embedded, w:: means web // this block is for when neither is specified Logger.md.Debug($"Resolving nonspecific URI {arg}"); // check if its embedded if (HasEmbeddedImage != null && HasEmbeddedImage(arg)) return "!::" + arg; else return "w::" + arg; } Logger.md.Debug($"Resolved specific URI {arg}"); return arg; } private static string GetLinkUri(string uri) { if (uri[0] == '!') { Logger.md.Error($"Cannot link to embedded resource in mod description"); return null; } else return uri.Substring(3); } private static AssetBundle _bundle; private static AssetBundle Bundle { get { if (_bundle == null) _bundle = AssetBundle.LoadFromStream(Assembly.GetExecutingAssembly().GetManifestResourceStream("BSIPA_ModList.Bundles.consolas.font")); return _bundle; } } private static TMP_FontAsset _consolas; private static TMP_FontAsset Consolas { get { if (_consolas == null) { _consolas = Bundle?.LoadAsset("CONSOLAS"); if (_consolas != null) { var originalFont = Resources.FindObjectsOfTypeAll().Last(f => f.name == "Teko-Medium SDF No Glow"); var matCopy = Instantiate(originalFont.material); matCopy.mainTexture = _consolas.material.mainTexture; matCopy.mainTextureOffset = _consolas.material.mainTextureOffset; matCopy.mainTextureScale = _consolas.material.mainTextureScale; _consolas.material = matCopy; MaterialReferenceManager.AddFontAsset(_consolas); } } return _consolas; } } protected void Awake() { if (Consolas == null) Logger.md.Error($"Loading of Consolas font failed"); gameObject.SetActive(false); var vpgo = new GameObject("Viewport"); viewport = vpgo.AddComponent(); viewport.SetParent(transform); viewport.localPosition = Vector2.zero; viewport.anchorMin = Vector2.zero; viewport.anchorMax = Vector2.one; viewport.anchoredPosition = new Vector2(.5f, .5f); viewport.sizeDelta = Vector2.zero; var vpmask = vpgo.AddComponent(); var vpim = vpgo.AddComponent(); // supposedly Mask needs an Image? vpmask.showMaskGraphic = false; vpim.color = Color.white; vpim.sprite = WhitePixel; vpim.material = CustomUI.Utilities.UIUtilities.NoGlowMaterial; content = new GameObject("Content Wrapper").AddComponent(); content.SetParent(viewport); content.gameObject.AddComponent(); content.localPosition = Vector2.zero; content.anchorMin = new Vector2(0f, 1f); content.anchorMax = new Vector2(1f, 1f); content.anchoredPosition = Vector2.zero; var contentLayout = content.gameObject.AddComponent(); var contentFitter = content.gameObject.AddComponent(); contentFitter.horizontalFit = ContentSizeFitter.FitMode.PreferredSize; contentFitter.verticalFit = ContentSizeFitter.FitMode.PreferredSize; contentLayout.childControlHeight = true; contentLayout.childControlWidth = false; contentLayout.childForceExpandHeight = false; contentLayout.childForceExpandWidth = true; contentLayout.childAlignment = TextAnchor.UpperCenter; contentLayout.spacing = 0f; var pageUp = Instantiate(Resources.FindObjectsOfTypeAll