2020-10-23 20:40:44 +11:00
|
|
|
|
using System;
|
|
|
|
|
using System.Collections.Generic;
|
2020-10-25 20:57:34 +11:00
|
|
|
|
using System.IO;
|
2020-10-28 20:52:40 +11:00
|
|
|
|
using System.Reflection;
|
2020-10-23 20:40:44 +11:00
|
|
|
|
using System.Text;
|
2020-11-05 17:33:04 +11:00
|
|
|
|
using UnityExplorer.Console;
|
2020-10-23 20:40:44 +11:00
|
|
|
|
|
2020-11-05 17:33:04 +11:00
|
|
|
|
namespace UnityExplorer.UI.PageModel
|
2020-10-23 20:40:44 +11:00
|
|
|
|
{
|
2020-10-28 06:39:26 +11:00
|
|
|
|
public class ConsolePage : MainMenu.Page
|
2020-10-23 20:40:44 +11:00
|
|
|
|
{
|
|
|
|
|
public override string Name => "C# Console";
|
|
|
|
|
|
2020-10-25 20:57:34 +11:00
|
|
|
|
public static ConsolePage Instance { get; private set; }
|
|
|
|
|
|
2020-10-27 00:54:08 +11:00
|
|
|
|
public CodeEditor m_codeEditor;
|
|
|
|
|
public ScriptEvaluator m_evaluator;
|
2020-10-25 20:57:34 +11:00
|
|
|
|
|
2020-11-08 21:04:41 +11:00
|
|
|
|
public static bool EnableAutocompletes { get; set; } = true;
|
|
|
|
|
public static bool EnableAutoIndent { get; set; } = true;
|
|
|
|
|
|
2020-10-28 06:39:26 +11:00
|
|
|
|
public static List<Suggestion> AutoCompletes = new List<Suggestion>();
|
2020-10-25 20:57:34 +11:00
|
|
|
|
public static List<string> UsingDirectives;
|
|
|
|
|
|
|
|
|
|
public static readonly string[] DefaultUsing = new string[]
|
|
|
|
|
{
|
|
|
|
|
"System",
|
|
|
|
|
"UnityEngine",
|
|
|
|
|
"System.Linq",
|
|
|
|
|
"System.Collections",
|
|
|
|
|
"System.Collections.Generic",
|
|
|
|
|
"System.Reflection"
|
|
|
|
|
};
|
|
|
|
|
|
2020-10-23 20:40:44 +11:00
|
|
|
|
public override void Init()
|
|
|
|
|
{
|
2020-10-25 20:57:34 +11:00
|
|
|
|
Instance = this;
|
|
|
|
|
|
|
|
|
|
try
|
|
|
|
|
{
|
2020-11-08 21:04:41 +11:00
|
|
|
|
m_codeEditor = new CodeEditor();
|
2020-10-25 20:57:34 +11:00
|
|
|
|
|
2020-11-08 21:04:41 +11:00
|
|
|
|
AutoCompleter.Init();
|
|
|
|
|
|
|
|
|
|
try
|
2020-10-25 20:57:34 +11:00
|
|
|
|
{
|
2020-11-08 21:04:41 +11:00
|
|
|
|
ResetConsole();
|
2020-10-23 20:40:44 +11:00
|
|
|
|
|
2020-11-08 21:04:41 +11:00
|
|
|
|
// Make sure compiler is supported on this platform
|
|
|
|
|
m_evaluator.Compile("");
|
2020-10-27 00:54:08 +11:00
|
|
|
|
|
2020-11-08 21:04:41 +11:00
|
|
|
|
foreach (string use in DefaultUsing)
|
|
|
|
|
{
|
|
|
|
|
AddUsing(use);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
catch (Exception e)
|
|
|
|
|
{
|
|
|
|
|
ExplorerCore.LogWarning($"Exception setting up Console: {e.GetType()}, {e.Message}\r\n{e.StackTrace}");
|
|
|
|
|
}
|
2020-10-25 20:57:34 +11:00
|
|
|
|
}
|
|
|
|
|
catch (Exception e)
|
|
|
|
|
{
|
2020-11-08 21:04:41 +11:00
|
|
|
|
// TODO remove page button from menu?
|
2020-10-25 20:57:34 +11:00
|
|
|
|
ExplorerCore.LogWarning($"Error setting up console!\r\nMessage: {e.Message}");
|
|
|
|
|
}
|
2020-10-23 20:40:44 +11:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public override void Update()
|
|
|
|
|
{
|
2020-10-26 01:07:59 +11:00
|
|
|
|
m_codeEditor?.Update();
|
2020-10-25 20:57:34 +11:00
|
|
|
|
|
2020-10-27 00:54:08 +11:00
|
|
|
|
AutoCompleter.Update();
|
2020-10-25 20:57:34 +11:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void AddUsing(string asm)
|
|
|
|
|
{
|
|
|
|
|
if (!UsingDirectives.Contains(asm))
|
|
|
|
|
{
|
2020-10-28 06:39:26 +11:00
|
|
|
|
Evaluate($"using {asm};", true);
|
2020-11-08 21:04:41 +11:00
|
|
|
|
UsingDirectives.Add(asm);
|
2020-10-25 20:57:34 +11:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2020-11-08 21:04:41 +11:00
|
|
|
|
public void Evaluate(string code, bool suppressWarning = false)
|
2020-10-25 20:57:34 +11:00
|
|
|
|
{
|
2020-11-08 21:04:41 +11:00
|
|
|
|
m_evaluator.Compile(code, out Mono.CSharp.CompiledMethod compiled);
|
2020-10-25 20:57:34 +11:00
|
|
|
|
|
2020-10-27 01:42:29 +11:00
|
|
|
|
if (compiled == null)
|
2020-10-25 20:57:34 +11:00
|
|
|
|
{
|
2020-10-27 01:42:29 +11:00
|
|
|
|
if (!suppressWarning)
|
2020-10-28 06:39:26 +11:00
|
|
|
|
ExplorerCore.LogWarning("Unable to compile the code!");
|
2020-10-25 20:57:34 +11:00
|
|
|
|
}
|
2020-10-27 01:42:29 +11:00
|
|
|
|
else
|
2020-10-25 20:57:34 +11:00
|
|
|
|
{
|
2020-10-27 01:42:29 +11:00
|
|
|
|
try
|
|
|
|
|
{
|
2020-10-28 06:39:26 +11:00
|
|
|
|
object ret = VoidType.Value;
|
2020-10-27 01:42:29 +11:00
|
|
|
|
compiled.Invoke(ref ret);
|
|
|
|
|
}
|
|
|
|
|
catch (Exception e)
|
2020-10-25 20:57:34 +11:00
|
|
|
|
{
|
2020-11-08 21:04:41 +11:00
|
|
|
|
if (!suppressWarning)
|
|
|
|
|
ExplorerCore.LogWarning($"Exception executing code: {e.GetType()}, {e.Message}\r\n{e.StackTrace}");
|
2020-10-25 20:57:34 +11:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void ResetConsole()
|
|
|
|
|
{
|
|
|
|
|
if (m_evaluator != null)
|
|
|
|
|
{
|
|
|
|
|
m_evaluator.Dispose();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
m_evaluator = new ScriptEvaluator(new StringWriter(new StringBuilder())) { InteractiveBaseClass = typeof(ScriptInteraction) };
|
|
|
|
|
|
|
|
|
|
UsingDirectives = new List<string>();
|
|
|
|
|
}
|
|
|
|
|
|
2020-10-27 00:54:08 +11:00
|
|
|
|
internal void OnInputChanged()
|
2020-10-26 01:07:59 +11:00
|
|
|
|
{
|
2020-11-08 21:04:41 +11:00
|
|
|
|
if (!EnableAutocompletes)
|
|
|
|
|
return;
|
2020-10-26 01:07:59 +11:00
|
|
|
|
|
2020-11-08 21:04:41 +11:00
|
|
|
|
AutoCompleter.CheckAutocomplete();
|
2020-10-27 00:54:08 +11:00
|
|
|
|
AutoCompleter.SetSuggestions(AutoCompletes.ToArray());
|
2020-10-26 01:07:59 +11:00
|
|
|
|
}
|
|
|
|
|
|
2020-10-27 00:54:08 +11:00
|
|
|
|
public void UseAutocomplete(string suggestion)
|
2020-10-26 01:07:59 +11:00
|
|
|
|
{
|
|
|
|
|
int cursorIndex = m_codeEditor.InputField.caretPosition;
|
2020-10-28 06:39:26 +11:00
|
|
|
|
string input = m_codeEditor.InputField.text;
|
2020-10-26 01:07:59 +11:00
|
|
|
|
input = input.Insert(cursorIndex, suggestion);
|
|
|
|
|
m_codeEditor.InputField.text = input;
|
2020-10-27 00:54:08 +11:00
|
|
|
|
m_codeEditor.InputField.caretPosition += suggestion.Length;
|
2020-10-26 01:07:59 +11:00
|
|
|
|
|
2020-10-27 00:54:08 +11:00
|
|
|
|
AutoCompleter.ClearAutocompletes();
|
2020-10-26 01:07:59 +11:00
|
|
|
|
}
|
|
|
|
|
|
2020-10-25 20:57:34 +11:00
|
|
|
|
private class VoidType
|
|
|
|
|
{
|
|
|
|
|
public static readonly VoidType Value = new VoidType();
|
|
|
|
|
private VoidType() { }
|
2020-10-23 20:40:44 +11:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|