static public CompileResult Evaluate(string code, bool autoCompleteComma = true) { if (autoCompleteComma && !code.EndsWith(";")) { code += ";"; } var result = new CompileResult(); result.code = code; // find commands and expand it if found. if (RuntimeCommands.ConvertIntoCodeIfCommand(ref code) || Commands.ConvertIntoCodeIfCommand(ref code)) { // the give code is a command and converted into an actual code. } // eval the code using Mono. object ret = null; bool hasReturnValue = false; var originalOutput = Mono.MessageOutput; var errorWriter = new System.IO.StringWriter(); bool isPartial = false; Mono.MessageOutput = errorWriter; try { isPartial = Mono.Evaluate(code, out ret, out hasReturnValue) != null; } catch (System.Exception e) { errorWriter.Write(e.Message); } Mono.MessageOutput = originalOutput; var error = errorWriter.ToString(); if (!string.IsNullOrEmpty(error)) { error = error.Replace("{interactive}", ""); var lastLineBreakPos = error.LastIndexOf('\n'); if (lastLineBreakPos != -1) { error = error.Remove(lastLineBreakPos); } result.type = CompileResult.Type.Error; result.error = error; return(result); } errorWriter.Dispose(); if (isPartial) { result.type = CompileResult.Type.Partial; return(result); } result.type = CompileResult.Type.Success; result.value = (hasReturnValue && ret != null) ? ret : "null"; return(result); }
static private void SetUsings() { Mono.Run("using uREPL;"); Mono.Run("using System;"); Mono.Run("using UnityEngine;"); // #if UNITY_EDITOR // Mono.Run("using UnityEditor;"); // #endif }
static public void Initialize() { if (isInitialized) { return; } isInitialized = true; Mono.Initialize(); ReferenceAllAssemblies(); SetUsings(); Log.Initialize(); Inspector.Initialize(); }
static public void Initialize() { if (isInitialized) { return; } isInitialized = true; Mono.Initialize(); SetUsings(); Log.Initialize(); Inspector.Initialize(); Evaluate("null;"); }
static private void ReferenceAllAssemblies() { // See the detailed information about this hack at: // http://forum.unity3d.com/threads/mono-csharp-evaluator.102162/ for (int n = 0; n < 2;) { foreach (var assembly in System.AppDomain.CurrentDomain.GetAssemblies()) { if (assembly == null) { continue; } Mono.ReferenceAssembly(assembly); } Mono.Evaluate("null;"); n++; } }
static public string GetUsing() => Mono.GetUsing();
static public string GetVars() => Mono.GetVars();
static public string[] GetCompletions(string input, out string prefix) => Mono.GetCompletions(input, out prefix);
static public string GetUsing() { return(Mono.GetUsing()); }
static public string GetVars() { return(Mono.GetVars()); }
static public string[] GetCompletions(string input, out string prefix) { return(Mono.GetCompletions(input, out prefix)); }