using System; using System.Collections.Generic; using UnityExplorer.Unstrip; //using TMPro; using UnityEngine; using UnityEngine.UI; using UnityExplorer.Config; using UnityExplorer.UI.Shared; using System.IO; using System.Linq; namespace UnityExplorer.UI.Modules { public class DebugConsole { public static DebugConsole Instance { get; private set; } public static bool LogUnity { get; set; } = ModConfig.Instance.Log_Unity_Debug; public static bool SaveToDisk { get; set; } = ModConfig.Instance.Save_Logs_To_Disk; internal static bool m_savedToDiskChecked; public static readonly List AllMessages = new List(); public static readonly List MessageHolders = new List(); // logs that occured before the actual UI was ready. internal static readonly List s_preInitMessages = new List(); private InputField m_textInput; public DebugConsole(GameObject parent) { Instance = this; ConstructUI(parent); string preAppend = ""; for (int i = s_preInitMessages.Count - 1; i >= 0; i--) { var msg = s_preInitMessages[i]; if (preAppend != "") preAppend += "\r\n"; preAppend += msg; } m_textInput.text = preAppend; } public static void OnQuit() { if (m_savedToDiskChecked) return; m_savedToDiskChecked = true; if (!SaveToDisk) return; var path = ExplorerCore.EXPLORER_FOLDER + @"\Logs"; if (!Directory.Exists(path)) Directory.CreateDirectory(path); // delete oldest log var files = Directory.GetFiles(path); if (files.Length >= 10) { var sorted = files.ToList(); // sort by datetime.ToString will put the oldest one first sorted.Sort(); File.Delete(sorted[0]); } var fileName = "UnityExplorer " + DateTime.Now.ToString("u") + ".txt"; fileName = ExplorerCore.RemoveInvalidFilenameChars(fileName); File.WriteAllText(path + @"\" + fileName, Instance.m_textInput.text); } public static void Log(string message) { Log(message, null); } public static void Log(string message, Color color) { Log(message, color.ToHex()); } public static void Log(string message, string hexColor) { message = $"{AllMessages.Count}: {message}"; AllMessages.Add(message); if (hexColor != null) message = $"{message}"; if (Instance?.m_textInput) Instance.m_textInput.text = $"{message}\n{Instance.m_textInput.text}"; else s_preInitMessages.Add(message); } public void ConstructUI(GameObject parent) { var mainObj = UIFactory.CreateVerticalGroup(parent, new Color(0.1f, 0.1f, 0.1f, 1.0f)); var mainGroup = mainObj.GetComponent(); mainGroup.childControlHeight = true; mainGroup.childControlWidth = true; mainGroup.childForceExpandHeight = true; mainGroup.childForceExpandWidth = true; var mainImage = mainObj.GetComponent(); mainImage.maskable = true; var mask = mainObj.AddComponent(); mask.showMaskGraphic = true; var mainLayout = mainObj.AddComponent(); mainLayout.minHeight = 190; mainLayout.flexibleHeight = 0; #region LOG AREA var logAreaObj = UIFactory.CreateHorizontalGroup(mainObj); var logAreaGroup = logAreaObj.GetComponent(); logAreaGroup.childControlHeight = true; logAreaGroup.childControlWidth = true; logAreaGroup.childForceExpandHeight = true; logAreaGroup.childForceExpandWidth = true; var logAreaLayout = logAreaObj.AddComponent(); logAreaLayout.preferredHeight = 190; logAreaLayout.flexibleHeight = 0; var inputScrollerObj = UIFactory.CreateSrollInputField(logAreaObj, out InputFieldScroller inputScroll, 14, new Color(0.05f, 0.05f, 0.05f)); inputScroll.inputField.textComponent.font = UIManager.ConsoleFont; inputScroll.inputField.readOnly = true; m_textInput = inputScroll.inputField; #endregion #region BOTTOM BAR var bottomBarObj = UIFactory.CreateHorizontalGroup(mainObj); LayoutElement topBarLayout = bottomBarObj.AddComponent(); topBarLayout.minHeight = 40; topBarLayout.flexibleHeight = 0; var bottomGroup = bottomBarObj.GetComponent(); bottomGroup.padding.left = 10; bottomGroup.padding.right = 10; bottomGroup.padding.top = 2; bottomGroup.padding.bottom = 2; bottomGroup.spacing = 10; bottomGroup.childForceExpandHeight = true; bottomGroup.childForceExpandWidth = false; bottomGroup.childControlWidth = true; bottomGroup.childControlHeight = true; bottomGroup.childAlignment = TextAnchor.MiddleLeft; // Debug Console label var bottomLabel = UIFactory.CreateLabel(bottomBarObj, TextAnchor.MiddleLeft); var topBarLabelLayout = bottomLabel.AddComponent(); topBarLabelLayout.minWidth = 100; topBarLabelLayout.flexibleWidth = 0; var topBarText = bottomLabel.GetComponent(); topBarText.fontStyle = FontStyle.Bold; topBarText.text = "Debug Console"; topBarText.fontSize = 14; // Hide button var hideButtonObj = UIFactory.CreateButton(bottomBarObj); var hideBtnText = hideButtonObj.GetComponentInChildren(); hideBtnText.text = "Hide"; var hideButton = hideButtonObj.GetComponent