Files
UnityExplorer_Fix/src/UI/Modules/CSConsolePage.cs

121 lines
3.1 KiB
C#
Raw Normal View History

using System;
using System.Collections.Generic;
using System.IO;
using System.Reflection;
using System.Text;
using UnityExplorer.CSConsole;
namespace UnityExplorer.UI.Modules
{
2020-11-14 00:46:26 +11:00
public class CSConsolePage : MainMenu.Page
{
public override string Name => "C# Console";
2020-11-14 00:46:26 +11:00
public static CSConsolePage Instance { get; private set; }
public CodeEditor m_codeEditor;
public ScriptEvaluator m_evaluator;
public static List<string> UsingDirectives;
public static readonly string[] DefaultUsing = new string[]
{
"System",
"System.Linq",
"System.Collections",
"System.Collections.Generic",
"System.Reflection",
"UnityEngine",
#if CPP
"UnhollowerBaseLib",
"UnhollowerRuntimeLib",
#endif
};
public override void Init()
{
Instance = this;
try
{
m_codeEditor = new CodeEditor();
AutoCompleter.Init();
ResetConsole();
// Make sure compiler is supported on this platform
m_evaluator.Compile("");
foreach (string use in DefaultUsing)
{
AddUsing(use);
}
}
catch (Exception e)
{
// TODO remove page button from menu?
ExplorerCore.LogWarning($"Error setting up console!\r\nMessage: {e.Message}");
}
}
public override void Update()
{
2020-10-26 01:07:59 +11:00
m_codeEditor?.Update();
AutoCompleter.Update();
}
public void AddUsing(string asm)
{
if (!UsingDirectives.Contains(asm))
{
Evaluate($"using {asm};", true);
UsingDirectives.Add(asm);
}
}
public void Evaluate(string code, bool suppressWarning = false)
{
m_evaluator.Compile(code, out Mono.CSharp.CompiledMethod compiled);
if (compiled == null)
{
if (!suppressWarning)
ExplorerCore.LogWarning("Unable to compile the code!");
}
else
{
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}");
}
}
}
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() { }
}
}
}