Improvements to CS Console

* Errors are now logged properly.
* Can now define classes, methods, etc - no longer has to be an expression body.
* Added `StartCoroutine(IEnumerator routine)` helper method to easily run a Coroutine
* Disabling suggestions now properly stops Explorer trying to update suggestion cache instead of just not showing them. In the rare cases that suggestions cause a crash, disabling them will now prevent those crashes.
* Various other misc improvements behind the scenes
This commit is contained in:
Sinai
2021-03-25 18:39:35 +11:00
parent a9fbea7c96
commit 2107df70ad
11 changed files with 289 additions and 56 deletions

View File

@ -205,7 +205,7 @@ namespace UnityExplorer.UI.Main.CSConsole
{
// Credit ManylMarco
CSharpConsole.AutoCompletes.Clear();
string[] completions = CSharpConsole.Instance.m_evaluator.GetCompletions(input, out string prefix);
string[] completions = CSharpConsole.Instance.Evaluator.GetCompletions(input, out string prefix);
if (completions != null)
{
if (prefix == null)

View File

@ -53,15 +53,10 @@ namespace UnityExplorer.UI.Main.CSConsole
"else", "equals", "false", "finally", "float", "for", "foreach", "from", "global", "goto", "group",
"if", "in", "int", "into", "is", "join", "let", "lock", "long", "new", "null", "object", "on", "orderby", "out",
"ref", "remove", "return", "sbyte", "select", "short", "sizeof", "stackalloc", "string",
"switch", "throw", "true", "try", "typeof", "uint", "ulong", "ushort", "var", "where", "while", "yield" }
};
public static KeywordMatch invalidKeywordMatcher = new KeywordMatch()
{
highlightColor = new Color(0.95f, 0.10f, 0.10f, 1.0f),
Keywords = new[] { "abstract", "async", "base", "class", "delegate", "enum", "explicit", "extern", "fixed", "get",
"implicit", "interface", "internal", "namespace", "operator", "override", "params", "private", "protected", "public",
"using", "partial", "readonly", "sealed", "set", "static", "struct", "this", "unchecked", "unsafe", "value", "virtual", "volatile", "void" }
"switch", "throw", "true", "try", "typeof", "uint", "ulong", "ushort", "var", "where", "while", "yield",
"abstract", "async", "base", "class", "delegate", "enum", "explicit", "extern", "fixed", "get",
"implicit", "interface", "internal", "namespace", "operator", "override", "params", "private", "protected", "public",
"using", "partial", "readonly", "sealed", "set", "static", "struct", "this", "unchecked", "unsafe", "value", "virtual", "volatile", "void"}
};
// ~~~~~~~ ctor ~~~~~~~
@ -78,7 +73,6 @@ namespace UnityExplorer.UI.Main.CSConsole
numberMatcher,
stringMatcher,
validKeywordMatcher,
invalidKeywordMatcher,
};
foreach (Matcher lexer in matchers)

View File

@ -12,6 +12,9 @@ using UnityEngine.UI;
using UnityExplorer.UI.Reusable;
using UnityExplorer.UI.Main.CSConsole;
using UnityExplorer.Core;
#if CPP
using UnityExplorer.Core.Runtime.Il2Cpp;
#endif
namespace UnityExplorer.UI.Main.CSConsole
{
@ -21,8 +24,8 @@ namespace UnityExplorer.UI.Main.CSConsole
public static CSharpConsole Instance { get; private set; }
//public UI.CSConsole.CSharpConsole m_codeEditor;
public ScriptEvaluator m_evaluator;
public ScriptEvaluator Evaluator;
internal StringBuilder m_evalLogBuilder;
public static List<string> UsingDirectives;
@ -49,11 +52,13 @@ namespace UnityExplorer.UI.Main.CSConsole
InitConsole();
AutoCompleter.Init();
#if MONO
DummyBehaviour.Setup();
#endif
ResetConsole();
// Make sure compiler is supported on this platform
m_evaluator.Compile("");
Evaluator.Compile("");
foreach (string use in DefaultUsing)
AddUsing(use);
@ -74,10 +79,27 @@ namespace UnityExplorer.UI.Main.CSConsole
}
}
public void ResetConsole()
{
if (Evaluator != null)
Evaluator.Dispose();
m_evalLogBuilder = new StringBuilder();
Evaluator = new ScriptEvaluator(new StringWriter(m_evalLogBuilder)) { InteractiveBaseClass = typeof(ScriptInteraction) };
UsingDirectives = new List<string>();
}
public override void Update()
{
UpdateConsole();
AutoCompleter.Update();
#if CPP
Il2CppCoroutine.Process();
#endif
}
public void AddUsing(string asm)
@ -89,40 +111,34 @@ namespace UnityExplorer.UI.Main.CSConsole
}
}
public void Evaluate(string code, bool suppressWarning = false)
public void Evaluate(string code, bool supressLog = false)
{
m_evaluator.Compile(code, out Mono.CSharp.CompiledMethod compiled);
if (compiled == null)
try
{
if (!suppressWarning)
ExplorerCore.LogWarning("Unable to compile the code!");
Evaluator.Run(code);
string output = ScriptEvaluator._textWriter.ToString();
var outputSplit = output.Split('\n');
if (outputSplit.Length >= 2)
output = outputSplit[outputSplit.Length - 2];
m_evalLogBuilder.Clear();
if (ScriptEvaluator._reportPrinter.ErrorsCount > 0)
throw new FormatException($"Unable to compile the code. Evaluator's last output was:\r\n{output}");
if (!supressLog)
ExplorerCore.Log("Code executed successfully.");
}
else
catch (FormatException fex)
{
try
{
object ret = VoidType.Value;
compiled.Invoke(ref ret);
}
catch (Exception e)
{
if (!suppressWarning)
ExplorerCore.LogWarning($"Exception executing code: {e.GetType()}, {e.Message}\r\n{e.StackTrace}");
}
if (!supressLog)
ExplorerCore.LogWarning(fex.Message);
}
}
public void ResetConsole()
{
if (m_evaluator != null)
catch (Exception ex)
{
m_evaluator.Dispose();
if (!supressLog)
ExplorerCore.LogWarning(ex);
}
m_evaluator = new ScriptEvaluator(new StringWriter(new StringBuilder())) { InteractiveBaseClass = typeof(ScriptInteraction) };
UsingDirectives = new List<string>();
}
// =================================================================================================
@ -160,6 +176,8 @@ The following helper methods are available:
* <color=#add490>Log(""message"")</color> logs a message to the debug console
* <color=#add490>StartCoroutine(IEnumerator routine)</color> start the IEnumerator as a UnityEngine.Coroutine
* <color=#add490>CurrentTarget()</color> returns the currently inspected target on the Home page
* <color=#add490>AllTargets()</color> returns an object[] array containing all inspected instances
@ -447,7 +465,7 @@ The following helper methods are available:
mainGroup.childForceExpandHeight = true;
mainGroup.childForceExpandWidth = true;
#region TOP BAR
#region TOP BAR
// Main group object
@ -523,9 +541,9 @@ The following helper methods are available:
autoIndentLayout.flexibleWidth = 0;
autoIndentLayout.minHeight = 25;
#endregion
#endregion
#region CONSOLE INPUT
#region CONSOLE INPUT
int fontSize = 16;
@ -554,9 +572,9 @@ The following helper methods are available:
highlightTextInput.supportRichText = true;
highlightTextInput.fontSize = fontSize;
#endregion
#endregion
#region COMPILE BUTTON
#region COMPILE BUTTON
var compileBtnObj = UIFactory.CreateButton(Content);
var compileBtnLayout = compileBtnObj.AddComponent<LayoutElement>();
@ -583,7 +601,7 @@ The following helper methods are available:
}
}
#endregion
#endregion
//mainTextInput.supportRichText = false;