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; using UnityExplorer.Helpers; 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 StreamWriter s_streamWriter; public static readonly List AllMessages = new List(); public static readonly List MessageHolders = new List(); // logs that occured before the actual UI was ready. // these ones include the hex color codes. internal static readonly List s_preInitMessages = new List(); private InputField m_textInput; public DebugConsole(GameObject parent) { Instance = this; ConstructUI(parent); // append messages that logged before we were set up 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; // set up IO if (!SaveToDisk) return; var path = ExplorerCore.EXPLORER_FOLDER + @"\Logs"; if (!Directory.Exists(path)) Directory.CreateDirectory(path); // clean old log(s) var files = Directory.GetFiles(path); if (files.Length >= 10) { var sorted = files.ToList(); // sort by 'datetime.ToString("u")' will put the oldest ones first sorted.Sort(); for (int i = 0; i < files.Length - 9; i++) File.Delete(files[i]); } var fileName = "UnityExplorer " + DateTime.Now.ToString("u") + ".txt"; fileName = ExplorerCore.RemoveInvalidFilenameChars(fileName); var stream = File.Create(path + @"\" + fileName); s_streamWriter = new StreamWriter(stream) { AutoFlush = true }; foreach (var msg in AllMessages) s_streamWriter.WriteLine(msg); } 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); s_streamWriter?.WriteLine(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