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-13 23:50:24 +11:00
|
|
|
|
using UnityExplorer.CSConsole;
|
2020-10-23 20:40:44 +11:00
|
|
|
|
|
2020-11-11 20:16:43 +11:00
|
|
|
|
namespace UnityExplorer.UI.Modules
|
2020-10-23 20:40:44 +11:00
|
|
|
|
{
|
2020-11-14 00:46:26 +11:00
|
|
|
|
public class CSConsolePage : MainMenu.Page
|
2020-10-23 20:40:44 +11:00
|
|
|
|
{
|
|
|
|
|
public override string Name => "C# Console";
|
|
|
|
|
|
2020-11-14 00:46:26 +11:00
|
|
|
|
public static CSConsolePage Instance { get; private set; }
|
2020-10-25 20:57:34 +11:00
|
|
|
|
|
2020-10-27 00:54:08 +11:00
|
|
|
|
public CodeEditor m_codeEditor;
|
|
|
|
|
public ScriptEvaluator m_evaluator;
|
2020-10-25 20:57:34 +11:00
|
|
|
|
|
|
|
|
|
public static List<string> UsingDirectives;
|
|
|
|
|
|
|
|
|
|
public static readonly string[] DefaultUsing = new string[]
|
|
|
|
|
{
|
|
|
|
|
"System",
|
|
|
|
|
"System.Linq",
|
|
|
|
|
"System.Collections",
|
|
|
|
|
"System.Collections.Generic",
|
2020-11-11 20:16:43 +11:00
|
|
|
|
"System.Reflection",
|
|
|
|
|
"UnityEngine",
|
|
|
|
|
#if CPP
|
|
|
|
|
"UnhollowerBaseLib",
|
|
|
|
|
"UnhollowerRuntimeLib",
|
|
|
|
|
#endif
|
2020-10-25 20:57:34 +11:00
|
|
|
|
};
|
|
|
|
|
|
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();
|
|
|
|
|
|
2020-11-11 20:16:43 +11:00
|
|
|
|
ResetConsole();
|
2020-10-23 20:40:44 +11:00
|
|
|
|
|
2020-11-11 20:16:43 +11:00
|
|
|
|
// Make sure compiler is supported on this platform
|
|
|
|
|
m_evaluator.Compile("");
|
2020-10-27 00:54:08 +11:00
|
|
|
|
|
2020-11-11 20:16:43 +11:00
|
|
|
|
foreach (string use in DefaultUsing)
|
2020-11-08 21:04:41 +11:00
|
|
|
|
{
|
2020-11-11 20:16:43 +11:00
|
|
|
|
AddUsing(use);
|
2020-11-08 21:04:41 +11:00
|
|
|
|
}
|
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>();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private class VoidType
|
|
|
|
|
{
|
|
|
|
|
public static readonly VoidType Value = new VoidType();
|
|
|
|
|
private VoidType() { }
|
2020-10-23 20:40:44 +11:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|