Skip to content
This repository was archived by the owner on May 9, 2023. It is now read-only.

Commit 69912d7

Browse files
committed
Prevent GC Mark Overflow on C# Console copy+paste
1 parent 9c5596a commit 69912d7

File tree

2 files changed

+38
-24
lines changed

2 files changed

+38
-24
lines changed

src/CSConsole/CodeEditor.cs

Lines changed: 36 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ public class CodeEditor
3232
private Text inputHighlightText;
3333

3434
private readonly CSharpLexer highlightLexer;
35-
private readonly StringBuilder sbHighlight;
35+
//private readonly StringBuilder sbHighlight;
3636

3737
internal int m_lastCaretPos;
3838
internal int m_fixCaretPos;
@@ -68,16 +68,31 @@ public class CodeEditor
6868

6969
public CodeEditor()
7070
{
71-
sbHighlight = new StringBuilder();
7271
highlightLexer = new CSharpLexer();
7372

7473
ConstructUI();
7574

7675
InputField.onValueChanged.AddListener((string s) => { OnInputChanged(s); });
7776
}
7877

78+
internal static bool IsUserCopyPasting()
79+
{
80+
return (InputManager.GetKey(KeyCode.LeftControl) || InputManager.GetKey(KeyCode.RightControl))
81+
&& InputManager.GetKeyDown(KeyCode.V);
82+
}
83+
7984
public void Update()
8085
{
86+
if (s_copyPasteBuffer != null)
87+
{
88+
if (!IsUserCopyPasting())
89+
{
90+
OnInputChanged(s_copyPasteBuffer);
91+
92+
s_copyPasteBuffer = null;
93+
}
94+
}
95+
8196
if (EnableCtrlRShortcut)
8297
{
8398
if ((InputManager.GetKey(KeyCode.LeftControl) || InputManager.GetKey(KeyCode.RightControl))
@@ -149,11 +164,18 @@ public void UseAutocomplete(string suggestion)
149164
AutoCompleter.ClearAutocompletes();
150165
}
151166

152-
public void OnInputChanged(string newInput, bool forceUpdate = false)
167+
internal static string s_copyPasteBuffer;
168+
169+
public void OnInputChanged(string newText, bool forceUpdate = false)
153170
{
154-
string newText = newInput;
171+
if (IsUserCopyPasting())
172+
{
173+
//Console.WriteLine("Copy+Paste detected!");
174+
s_copyPasteBuffer = newText;
175+
return;
176+
}
155177

156-
UpdateIndent(newInput);
178+
UpdateIndent(newText);
157179

158180
if (!forceUpdate && string.IsNullOrEmpty(newText))
159181
inputHighlightText.text = string.Empty;
@@ -203,35 +225,29 @@ private string SyntaxHighlightContent(string inputText)
203225
{
204226
int offset = 0;
205227

206-
sbHighlight.Length = 0;
228+
//Console.WriteLine("Highlighting input text:\r\n" + inputText);
229+
230+
string ret = "";
207231

208232
foreach (LexerMatchInfo match in highlightLexer.GetMatches(inputText))
209233
{
210234
for (int i = offset; i < match.startIndex; i++)
211-
{
212-
sbHighlight.Append(inputText[i]);
213-
}
235+
ret += inputText[i];
214236

215-
sbHighlight.Append($"{match.htmlColor}");
237+
ret += $"{match.htmlColor}";
216238

217239
for (int i = match.startIndex; i < match.endIndex; i++)
218-
{
219-
sbHighlight.Append(inputText[i]);
220-
}
240+
ret += inputText[i];
221241

222-
sbHighlight.Append(CLOSE_COLOR_TAG);
242+
ret += CLOSE_COLOR_TAG;
223243

224244
offset = match.endIndex;
225245
}
226246

227247
for (int i = offset; i < inputText.Length; i++)
228-
{
229-
sbHighlight.Append(inputText[i]);
230-
}
231-
232-
inputText = sbHighlight.ToString();
248+
ret += inputText[i];
233249

234-
return inputText;
250+
return ret;
235251
}
236252

237253
private void AutoIndentCaret()

src/ExplorerCore.cs

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,20 +4,18 @@
44
using UnityEngine;
55
using UnityEngine.SceneManagement;
66
using UnityExplorer.Config;
7+
using UnityExplorer.Helpers;
78
using UnityExplorer.Input;
89
using UnityExplorer.Inspectors;
910
using UnityExplorer.UI;
1011
using UnityExplorer.UI.Modules;
11-
#if CPP
12-
using UnityExplorer.Helpers;
13-
#endif
1412

1513
namespace UnityExplorer
1614
{
1715
public class ExplorerCore
1816
{
1917
public const string NAME = "UnityExplorer";
20-
public const string VERSION = "3.1.8";
18+
public const string VERSION = "3.1.9";
2119
public const string AUTHOR = "Sinai";
2220
public const string GUID = "com.sinai.unityexplorer";
2321

0 commit comments

Comments
 (0)