From d038d138677fe375bbbfacdd2a88bc941e5a80a9 Mon Sep 17 00:00:00 2001 From: sinaioutlander <49360850+sinaioutlander@users.noreply.github.com> Date: Sun, 8 Nov 2020 21:04:41 +1100 Subject: [PATCH] lots... * Created a TMP AssetBundle for games which don't have the default TextMeshPro Resources package. This also allows us to use a custom monospace font for the Console and Debug window. * Unstripped the AssetBundle class (just the stuff we need) * Finished Search Page * Finished Options Page (very simple) * Various refactoring and restructuring of the project * cleanups --- src/Config/ModConfig.cs | 37 +- src/Console/AutoCompleter.cs | 4 +- src/Console/CodeEditor.cs | 477 +++++++++++++-- src/ExplorerCore.cs | 14 +- src/Helpers/UnityHelpers.cs | 2 +- src/Inspectors/GOInspector/ChildList.cs | 16 +- src/Inspectors/GOInspector/ComponentList.cs | 10 +- .../GOInspector/GameObjectControls.cs | 20 +- src/Inspectors/GameObjectInspector.cs | 6 +- src/Inspectors/MouseInspector.cs | 8 +- src/Inspectors/SceneExplorer.cs | 7 +- src/Tests/Tests.cs | 2 +- src/UI/ForceUnlockCursor.cs | 70 +-- src/UI/MainMenu.cs | 13 +- src/UI/PageModel/ConsolePage.cs | 413 +------------ src/UI/PageModel/DebugConsole.cs | 80 ++- src/UI/PageModel/OptionsPage.cs | 219 ++++++- src/UI/PageModel/SearchPage.cs | 562 ++++++++++++++++-- src/UI/PanelDragger.cs | 2 +- src/UI/Shared/PageHandler.cs | 61 +- src/UI/Shared/SliderScrollbar.cs | 74 ++- src/UI/UIFactory.cs | 295 ++------- src/UI/UIManager.cs | 97 ++- src/UnityExplorer.csproj | 5 + src/Unstrip/AssetBundle/AssetBundleUnstrip.cs | 57 ++ 25 files changed, 1604 insertions(+), 947 deletions(-) create mode 100644 src/Unstrip/AssetBundle/AssetBundleUnstrip.cs diff --git a/src/Config/ModConfig.cs b/src/Config/ModConfig.cs index 8f73d76..9867f1d 100644 --- a/src/Config/ModConfig.cs +++ b/src/Config/ModConfig.cs @@ -1,4 +1,5 @@ -using System.IO; +using System; +using System.IO; using System.Xml.Serialization; using UnityEngine; @@ -8,24 +9,27 @@ namespace UnityExplorer.Config { [XmlIgnore] public static readonly XmlSerializer Serializer = new XmlSerializer(typeof(ModConfig)); - [XmlIgnore] private const string EXPLORER_FOLDER = @"Mods\Explorer"; - [XmlIgnore] private const string SETTINGS_PATH = EXPLORER_FOLDER + @"\config.xml"; + //[XmlIgnore] private const string EXPLORER_FOLDER = @"Mods\UnityExplorer"; + [XmlIgnore] private const string SETTINGS_PATH = ExplorerCore.EXPLORER_FOLDER + @"\config.xml"; [XmlIgnore] public static ModConfig Instance; // Actual configs - public KeyCode Main_Menu_Toggle = KeyCode.F7; - public Vector2 Default_Window_Size = new Vector2(550, 700); - public int Default_Page_Limit = 20; - public bool Bitwise_Support = false; - public bool Tab_View = true; - public string Default_Output_Path = @"Mods\Explorer"; + public KeyCode Main_Menu_Toggle = KeyCode.F7; + public bool Force_Unlock_Mouse = true; + public int Default_Page_Limit = 20; + public string Default_Output_Path = ExplorerCore.EXPLORER_FOLDER; + public bool Log_Unity_Debug = false; + + public static event Action OnConfigChanged; + + internal static void InvokeConfigChanged() + { + OnConfigChanged?.Invoke(); + } public static void OnLoad() { - if (!Directory.Exists(EXPLORER_FOLDER)) - Directory.CreateDirectory(EXPLORER_FOLDER); - if (LoadSettings()) return; @@ -33,20 +37,15 @@ namespace UnityExplorer.Config SaveSettings(); } - // returns true if settings successfully loaded public static bool LoadSettings() { if (!File.Exists(SETTINGS_PATH)) - { return false; - } try { using (var file = File.OpenRead(SETTINGS_PATH)) - { Instance = (ModConfig)Serializer.Deserialize(file); - } } catch { @@ -59,14 +58,10 @@ namespace UnityExplorer.Config public static void SaveSettings() { if (File.Exists(SETTINGS_PATH)) - { File.Delete(SETTINGS_PATH); - } using (var file = File.Create(SETTINGS_PATH)) - { Serializer.Serialize(file, Instance); - } } } } diff --git a/src/Console/AutoCompleter.cs b/src/Console/AutoCompleter.cs index 51e93b4..cdd5bf3 100644 --- a/src/Console/AutoCompleter.cs +++ b/src/Console/AutoCompleter.cs @@ -44,7 +44,7 @@ namespace UnityExplorer.Console return; } - if (!ConsolePage.EnableSuggestions) + if (!ConsolePage.EnableAutocompletes) { if (m_mainObj.activeSelf) { @@ -267,7 +267,7 @@ namespace UnityExplorer.Console { var parent = UIManager.CanvasRoot; - var obj = UIFactory.CreateScrollView(parent, out GameObject content, new Color(0.1f, 0.1f, 0.1f, 0.95f)); + var obj = UIFactory.CreateScrollView(parent, out GameObject content, out _, new Color(0.1f, 0.1f, 0.1f, 0.95f)); m_mainObj = obj; diff --git a/src/Console/CodeEditor.cs b/src/Console/CodeEditor.cs index dcac903..6f06c10 100644 --- a/src/Console/CodeEditor.cs +++ b/src/Console/CodeEditor.cs @@ -9,6 +9,13 @@ using UnityEngine.EventSystems; using UnityEngine.UI; using UnityExplorer.UI; using UnityExplorer.UI.PageModel; +using System.Collections.Generic; +using System.Reflection; +#if CPP +using UnityExplorer.Unstrip.Resources; +using UnityExplorer.Helpers; +using UnhollowerRuntimeLib; +#endif namespace UnityExplorer.Console { @@ -16,19 +23,19 @@ namespace UnityExplorer.Console { private readonly InputLexer inputLexer = new InputLexer(); - public TMP_InputField InputField { get; } + public TMP_InputField InputField { get; internal set; } - public readonly TextMeshProUGUI inputText; - private readonly TextMeshProUGUI inputHighlightText; - private readonly TextMeshProUGUI lineText; - private readonly Image background; - private readonly Image lineHighlight; - private readonly Image lineNumberBackground; - private readonly Image scrollbar; + public TextMeshProUGUI inputText; + private TextMeshProUGUI inputHighlightText; + private TextMeshProUGUI lineText; + private Image background; + private Image lineNumberBackground; + private Image scrollbar; - private bool lineHighlightLocked; - private readonly RectTransform inputTextTransform; - private readonly RectTransform lineHighlightTransform; + //private readonly RectTransform inputTextTransform; + //private readonly RectTransform lineHighlightTransform; + //private bool lineHighlightLocked; + //private Image lineHighlight; public int LineCount { get; private set; } public int CurrentLine { get; private set; } @@ -66,27 +73,38 @@ namespace UnityExplorer.Console } } - public CodeEditor(TMP_InputField inputField, TextMeshProUGUI inputText, TextMeshProUGUI inputHighlightText, TextMeshProUGUI lineText, - Image background, Image lineHighlight, Image lineNumberBackground, Image scrollbar) + internal const string STARTUP_TEXT = @"Welcome to the UnityExplorer C# Console. + +The following helper methods are available: + +* Log(""message"") logs a message to the debug console + +* CurrentTarget() returns the currently inspected target on the Home page + +* AllTargets() returns an object[] array containing all inspected instances + +* Inspect(someObject) to inspect an instance, eg. Inspect(Camera.main); + +* Inspect(typeof(SomeClass)) to inspect a Class with static reflection + +* AddUsing(""SomeNamespace"") adds a using directive to the C# console + +* GetUsing() logs the current using directives to the debug console + +* Reset() resets all using directives and variables +"; + + public CodeEditor() { - InputField = inputField; - this.inputText = inputText; - this.inputHighlightText = inputHighlightText; - this.lineText = lineText; - this.background = background; - this.lineHighlight = lineHighlight; - this.lineNumberBackground = lineNumberBackground; - this.scrollbar = scrollbar; + ConstructUI(); if (!AllReferencesAssigned()) { throw new Exception("References are missing!"); } - InputField.restoreOriginalTextOnEscape = false; - - inputTextTransform = inputText.GetComponent(); - lineHighlightTransform = lineHighlight.GetComponent(); + //inputTextTransform = inputText.GetComponent(); + //lineHighlightTransform = lineHighlight.GetComponent(); ApplyTheme(); inputLexer.UseMatchers(CSharpLexer.DelimiterSymbols, CSharpLexer.Matchers); @@ -124,7 +142,7 @@ namespace UnityExplorer.Console // Update line highlight if (focusKeyPressed || InputManager.GetMouseButton(0)) { - UpdateHighlight(); + //UpdateHighlight(); ConsolePage.Instance.OnInputChanged(); } } @@ -146,25 +164,26 @@ namespace UnityExplorer.Console } UpdateLineNumbers(); - UpdateHighlight(); + //UpdateHighlight(); ConsolePage.Instance.OnInputChanged(); } - public void SetLineHighlight(int lineNumber, bool lockLineHighlight) - { - if (lineNumber < 1 || lineNumber > LineCount) - { - return; - } + //public void SetLineHighlight(int lineNumber, bool lockLineHighlight) + //{ + // if (lineNumber < 1 || lineNumber > LineCount) + // { + // return; + // } - lineHighlightTransform.anchoredPosition = new Vector2(5, - (inputText.textInfo.lineInfo[inputText.textInfo.characterInfo[0].lineNumber].lineHeight * - -(lineNumber - 1)) - 4f + - inputTextTransform.anchoredPosition.y); + // lineHighlightTransform.anchoredPosition = new Vector2(5, + // (inputText.textInfo.lineInfo[inputText.textInfo.characterInfo[0].lineNumber].lineHeight * + // //-(lineNumber - 1)) - 4f + + // -(lineNumber - 1)) + + // inputTextTransform.anchoredPosition.y); - lineHighlightLocked = lockLineHighlight; - } + // lineHighlightLocked = lockLineHighlight; + //} private void UpdateLineNumbers() { @@ -268,28 +287,28 @@ namespace UnityExplorer.Console } } - private void UpdateHighlight() - { - if (lineHighlightLocked) - { - return; - } + //private void UpdateHighlight() + //{ + // if (lineHighlightLocked) + // { + // return; + // } - try - { - int caret = InputField.caretPosition - 1; + // try + // { + // int caret = InputField.caretPosition - 1; - float lineHeight = inputText.textInfo.lineInfo[inputText.textInfo.characterInfo[0].lineNumber].lineHeight; - int lineNumber = inputText.textInfo.characterInfo[caret].lineNumber; - float offset = lineNumber + inputTextTransform.anchoredPosition.y; + // float lineHeight = inputText.textInfo.lineInfo[inputText.textInfo.characterInfo[0].lineNumber].lineHeight; + // int lineNumber = inputText.textInfo.characterInfo[caret].lineNumber; + // float offset = lineNumber + inputTextTransform.anchoredPosition.y; - lineHighlightTransform.anchoredPosition = new Vector2(5, -(offset * lineHeight)); - } - catch //(Exception e) - { - //ExplorerCore.LogWarning("Exception on Update Line Highlight: " + e); - } - } + // lineHighlightTransform.anchoredPosition = new Vector2(5, -(offset * lineHeight)); + // } + // catch //(Exception e) + // { + // //ExplorerCore.LogWarning("Exception on Update Line Highlight: " + e); + // } + //} private const string CLOSE_COLOR_TAG = ""; @@ -397,6 +416,8 @@ namespace UnityExplorer.Console return tab; } + // ============== Theme ============== // + private static Color caretColor = new Color32(255, 255, 255, 255); private static Color textColor = new Color32(255, 255, 255, 255); private static Color backgroundColor = new Color32(37, 37, 37, 255); @@ -417,7 +438,7 @@ namespace UnityExplorer.Console inputText.color = textColor; inputHighlightText.color = textColor; background.color = backgroundColor; - lineHighlight.color = lineHighlightColor; + //lineHighlight.color = lineHighlightColor; lineNumberBackground.color = lineNumberBackgroundColor; lineText.color = lineNumberTextColor; scrollbar.color = scrollbarColor; @@ -430,7 +451,7 @@ namespace UnityExplorer.Console !inputHighlightText || !lineText || !background || - !lineHighlight || + //!lineHighlight || !lineNumberBackground || !scrollbar) { @@ -439,5 +460,341 @@ namespace UnityExplorer.Console } return true; } + + + // ========== UI CONSTRUCTION =========== // + + public void ConstructUI() + { + ConsolePage.Instance.Content = UIFactory.CreateUIObject("C# Console", MainMenu.Instance.PageViewport); + + var mainLayout = ConsolePage.Instance.Content.AddComponent(); + mainLayout.preferredHeight = 9900; + mainLayout.flexibleHeight = 9000; + + var mainGroup = ConsolePage.Instance.Content.AddComponent(); + mainGroup.childControlHeight = true; + mainGroup.childControlWidth = true; + mainGroup.childForceExpandHeight = true; + mainGroup.childForceExpandWidth = true; + + #region TOP BAR + + // Main group object + + var topBarObj = UIFactory.CreateHorizontalGroup(ConsolePage.Instance.Content); + LayoutElement topBarLayout = topBarObj.AddComponent(); + topBarLayout.minHeight = 50; + topBarLayout.flexibleHeight = 0; + + var topBarGroup = topBarObj.GetComponent(); + topBarGroup.padding.left = 30; + topBarGroup.padding.right = 30; + topBarGroup.padding.top = 8; + topBarGroup.padding.bottom = 8; + topBarGroup.spacing = 10; + topBarGroup.childForceExpandHeight = true; + topBarGroup.childForceExpandWidth = true; + topBarGroup.childControlWidth = true; + topBarGroup.childControlHeight = true; + topBarGroup.childAlignment = TextAnchor.LowerCenter; + + var topBarLabel = UIFactory.CreateLabel(topBarObj, TextAnchor.MiddleLeft); + var topBarLabelLayout = topBarLabel.AddComponent(); + topBarLabelLayout.preferredWidth = 800; + topBarLabelLayout.flexibleWidth = 10; + var topBarText = topBarLabel.GetComponent(); + topBarText.text = "C# Console"; + topBarText.fontSize = 20; + + // Enable Suggestions toggle + + var suggestToggleObj = UIFactory.CreateToggle(topBarObj, out Toggle suggestToggle, out Text suggestToggleText); +#if CPP + suggestToggle.onValueChanged.AddListener(new Action(SuggestToggleCallback)); +#else + suggestToggle.onValueChanged.AddListener(SuggestToggleCallback); +#endif + void SuggestToggleCallback(bool val) + { + ConsolePage.EnableAutocompletes = val; + AutoCompleter.Update(); + } + + suggestToggleText.text = "Suggestions"; + suggestToggleText.alignment = TextAnchor.UpperLeft; + var suggestTextPos = suggestToggleText.transform.localPosition; + suggestTextPos.y = -14; + suggestToggleText.transform.localPosition = suggestTextPos; + + var suggestLayout = suggestToggleObj.AddComponent(); + suggestLayout.minWidth = 120; + suggestLayout.flexibleWidth = 0; + + var suggestRect = suggestToggleObj.transform.Find("Background"); + var suggestPos = suggestRect.localPosition; + suggestPos.y = -14; + suggestRect.localPosition = suggestPos; + + // Enable Auto-indent toggle + + var autoIndentToggleObj = UIFactory.CreateToggle(topBarObj, out Toggle autoIndentToggle, out Text autoIndentToggleText); +#if CPP + autoIndentToggle.onValueChanged.AddListener(new Action(OnIndentChanged)); +#else + autoIndentToggle.onValueChanged.AddListener(OnIndentChanged); +#endif + void OnIndentChanged(bool val) => ConsolePage.EnableAutoIndent = val; + + autoIndentToggleText.text = "Auto-indent"; + autoIndentToggleText.alignment = TextAnchor.UpperLeft; + var autoIndentTextPos = autoIndentToggleText.transform.localPosition; + autoIndentTextPos.y = -14; + autoIndentToggleText.transform.localPosition = autoIndentTextPos; + + var autoIndentLayout = autoIndentToggleObj.AddComponent(); + autoIndentLayout.minWidth = 120; + autoIndentLayout.flexibleWidth = 0; + + var autoIndentRect = autoIndentToggleObj.transform.Find("Background"); + suggestPos = autoIndentRect.localPosition; + suggestPos.y = -14; + autoIndentRect.localPosition = suggestPos; + + #endregion + + #region CONSOLE INPUT + + var consoleBase = UIFactory.CreateUIObject("CodeEditor", ConsolePage.Instance.Content); + + var consoleLayout = consoleBase.AddComponent(); + consoleLayout.preferredHeight = 500; + consoleLayout.flexibleHeight = 50; + + consoleBase.AddComponent(); + + var mainRect = consoleBase.GetComponent(); + mainRect.pivot = Vector2.one * 0.5f; + mainRect.anchorMin = Vector2.zero; + mainRect.anchorMax = Vector2.one; + mainRect.offsetMin = Vector2.zero; + mainRect.offsetMax = Vector2.zero; + + var mainBg = UIFactory.CreateUIObject("MainBackground", consoleBase); + + var mainBgRect = mainBg.GetComponent(); + mainBgRect.pivot = new Vector2(0, 1); + mainBgRect.anchorMin = Vector2.zero; + mainBgRect.anchorMax = Vector2.one; + mainBgRect.offsetMin = Vector2.zero; + mainBgRect.offsetMax = Vector2.zero; + + var mainBgImage = mainBg.AddComponent(); + + //var lineHighlight = UIFactory.CreateUIObject("LineHighlight", consoleBase); + + //var lineHighlightRect = lineHighlight.GetComponent(); + //lineHighlightRect.pivot = new Vector2(0.5f, 1); + //lineHighlightRect.anchorMin = new Vector2(0, 1); + //lineHighlightRect.anchorMax = new Vector2(1, 1); + //lineHighlightRect.SetSizeWithCurrentAnchors(RectTransform.Axis.Vertical, 21); + + //var lineHighlightImage = lineHighlight.GetComponent(); + //if (!lineHighlightImage) + //{ + // lineHighlightImage = lineHighlight.AddComponent(); + //} + + var linesBg = UIFactory.CreateUIObject("LinesBackground", consoleBase); + var linesBgRect = linesBg.GetComponent(); + linesBgRect.anchorMin = Vector2.zero; + linesBgRect.anchorMax = new Vector2(0, 1); + linesBgRect.offsetMin = new Vector2(-17.5f, 0); + linesBgRect.offsetMax = new Vector2(17.5f, 0); + linesBgRect.sizeDelta = new Vector2(65, 0); + + var linesBgImage = linesBg.AddComponent(); + + var inputObj = UIFactory.CreateTMPInput(consoleBase); + + var inputField = inputObj.GetComponent(); + inputField.richText = false; + inputField.restoreOriginalTextOnEscape = false; + + var inputRect = inputObj.GetComponent(); + inputRect.pivot = new Vector2(0.5f, 0.5f); + inputRect.anchorMin = Vector2.zero; + inputRect.anchorMax = new Vector2(0.92f, 1); + inputRect.offsetMin = new Vector2(20, 0); + inputRect.offsetMax = new Vector2(14, 0); + inputRect.anchoredPosition = new Vector2(40, 0); + + var textAreaObj = inputObj.transform.Find("TextArea"); + var textAreaRect = textAreaObj.GetComponent(); + textAreaRect.pivot = new Vector2(0.5f, 0.5f); + textAreaRect.anchorMin = Vector2.zero; + textAreaRect.anchorMax = Vector2.one; + + var mainTextObj = textAreaObj.transform.Find("Text"); + var mainTextRect = mainTextObj.GetComponent(); + mainTextRect.pivot = new Vector2(0.5f, 0.5f); + mainTextRect.anchorMin = Vector2.zero; + mainTextRect.anchorMax = Vector2.one; + mainTextRect.offsetMin = Vector2.zero; + mainTextRect.offsetMax = Vector2.zero; + + var mainTextInput = mainTextObj.GetComponent(); + //mainTextInput.fontSize = 18; + + var placeHolderText = textAreaObj.transform.Find("Placeholder").GetComponent(); + placeHolderText.text = CodeEditor.STARTUP_TEXT; + + var linesTextObj = UIFactory.CreateUIObject("LinesText", mainTextObj.gameObject); + var linesTextRect = linesTextObj.GetComponent(); + + var linesTextInput = linesTextObj.AddComponent(); + linesTextInput.fontSize = 18; + + var highlightTextObj = UIFactory.CreateUIObject("HighlightText", mainTextObj.gameObject); + var highlightTextRect = highlightTextObj.GetComponent(); + highlightTextRect.anchorMin = Vector2.zero; + highlightTextRect.anchorMax = Vector2.one; + highlightTextRect.offsetMin = Vector2.zero; + highlightTextRect.offsetMax = Vector2.zero; + + var highlightTextInput = highlightTextObj.AddComponent(); + //highlightTextInput.fontSize = 18; + + var scroll = UIFactory.CreateScrollbar(consoleBase); + + var scrollRect = scroll.GetComponent(); + scrollRect.anchorMin = new Vector2(1, 0); + scrollRect.anchorMax = new Vector2(1, 1); + scrollRect.pivot = new Vector2(0.5f, 1); + scrollRect.offsetMin = new Vector2(-25f, 0); + + var scroller = scroll.GetComponent(); + scroller.direction = Scrollbar.Direction.TopToBottom; + var scrollColors = scroller.colors; + scrollColors.normalColor = new Color(0.6f, 0.6f, 0.6f, 1.0f); + scroller.colors = scrollColors; + + var scrollImage = scroll.GetComponent(); + + var tmpInput = inputObj.GetComponent(); + tmpInput.scrollSensitivity = 15; + tmpInput.verticalScrollbar = scroller; + + // set lines text anchors here after UI is fleshed out + linesTextRect.pivot = Vector2.zero; + linesTextRect.anchorMin = new Vector2(0, 0); + linesTextRect.anchorMax = new Vector2(1, 1); + linesTextRect.offsetMin = Vector2.zero; + linesTextRect.offsetMax = Vector2.zero; + linesTextRect.anchoredPosition = new Vector2(-40, 0); + + tmpInput.GetComponentInChildren().enabled = false; + inputObj.GetComponent().enabled = false; + + #endregion + + #region COMPILE BUTTON + + var compileBtnObj = UIFactory.CreateButton(ConsolePage.Instance.Content); + var compileBtnLayout = compileBtnObj.AddComponent(); + compileBtnLayout.preferredWidth = 80; + compileBtnLayout.flexibleWidth = 0; + compileBtnLayout.minHeight = 45; + compileBtnLayout.flexibleHeight = 0; + var compileButton = compileBtnObj.GetComponent