From 69912d7ea4a9bcd075fab756f395730f88d211f1 Mon Sep 17 00:00:00 2001 From: Sinai Date: Fri, 26 Feb 2021 17:54:00 +1100 Subject: [PATCH] Prevent GC Mark Overflow on C# Console copy+paste --- src/CSConsole/CodeEditor.cs | 58 +++++++++++++++++++++++-------------- src/ExplorerCore.cs | 6 ++-- 2 files changed, 39 insertions(+), 25 deletions(-) diff --git a/src/CSConsole/CodeEditor.cs b/src/CSConsole/CodeEditor.cs index 5f3c654..af4433e 100644 --- a/src/CSConsole/CodeEditor.cs +++ b/src/CSConsole/CodeEditor.cs @@ -32,7 +32,7 @@ namespace UnityExplorer.CSConsole private Text inputHighlightText; private readonly CSharpLexer highlightLexer; - private readonly StringBuilder sbHighlight; + //private readonly StringBuilder sbHighlight; internal int m_lastCaretPos; internal int m_fixCaretPos; @@ -68,7 +68,6 @@ The following helper methods are available: public CodeEditor() { - sbHighlight = new StringBuilder(); highlightLexer = new CSharpLexer(); ConstructUI(); @@ -76,8 +75,24 @@ The following helper methods are available: InputField.onValueChanged.AddListener((string s) => { OnInputChanged(s); }); } + internal static bool IsUserCopyPasting() + { + return (InputManager.GetKey(KeyCode.LeftControl) || InputManager.GetKey(KeyCode.RightControl)) + && InputManager.GetKeyDown(KeyCode.V); + } + public void Update() { + if (s_copyPasteBuffer != null) + { + if (!IsUserCopyPasting()) + { + OnInputChanged(s_copyPasteBuffer); + + s_copyPasteBuffer = null; + } + } + if (EnableCtrlRShortcut) { if ((InputManager.GetKey(KeyCode.LeftControl) || InputManager.GetKey(KeyCode.RightControl)) @@ -149,11 +164,18 @@ The following helper methods are available: AutoCompleter.ClearAutocompletes(); } - public void OnInputChanged(string newInput, bool forceUpdate = false) - { - string newText = newInput; + internal static string s_copyPasteBuffer; - UpdateIndent(newInput); + public void OnInputChanged(string newText, bool forceUpdate = false) + { + if (IsUserCopyPasting()) + { + //Console.WriteLine("Copy+Paste detected!"); + s_copyPasteBuffer = newText; + return; + } + + UpdateIndent(newText); if (!forceUpdate && string.IsNullOrEmpty(newText)) inputHighlightText.text = string.Empty; @@ -203,35 +225,29 @@ The following helper methods are available: { int offset = 0; - sbHighlight.Length = 0; + //Console.WriteLine("Highlighting input text:\r\n" + inputText); + + string ret = ""; foreach (LexerMatchInfo match in highlightLexer.GetMatches(inputText)) { for (int i = offset; i < match.startIndex; i++) - { - sbHighlight.Append(inputText[i]); - } + ret += inputText[i]; - sbHighlight.Append($"{match.htmlColor}"); + ret += $"{match.htmlColor}"; for (int i = match.startIndex; i < match.endIndex; i++) - { - sbHighlight.Append(inputText[i]); - } + ret += inputText[i]; - sbHighlight.Append(CLOSE_COLOR_TAG); + ret += CLOSE_COLOR_TAG; offset = match.endIndex; } for (int i = offset; i < inputText.Length; i++) - { - sbHighlight.Append(inputText[i]); - } + ret += inputText[i]; - inputText = sbHighlight.ToString(); - - return inputText; + return ret; } private void AutoIndentCaret() diff --git a/src/ExplorerCore.cs b/src/ExplorerCore.cs index 3507bae..7d15e1b 100644 --- a/src/ExplorerCore.cs +++ b/src/ExplorerCore.cs @@ -4,20 +4,18 @@ using System.Reflection; using UnityEngine; using UnityEngine.SceneManagement; using UnityExplorer.Config; +using UnityExplorer.Helpers; using UnityExplorer.Input; using UnityExplorer.Inspectors; using UnityExplorer.UI; using UnityExplorer.UI.Modules; -#if CPP -using UnityExplorer.Helpers; -#endif namespace UnityExplorer { public class ExplorerCore { public const string NAME = "UnityExplorer"; - public const string VERSION = "3.1.8"; + public const string VERSION = "3.1.9"; public const string AUTHOR = "Sinai"; public const string GUID = "com.sinai.unityexplorer";