Skip to content
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
69 changes: 51 additions & 18 deletions MCPForUnity/Editor/Tools/ExecuteCode.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
using System.Linq;
using System.Reflection;
using System.Text;
using System.Text.RegularExpressions;
using MCPForUnity.Editor.Helpers;
using MCPForUnity.Runtime.Helpers;
using Microsoft.CSharp;
Expand All @@ -19,7 +20,6 @@ public static class ExecuteCode
private const int MaxCodeLength = 50000;
private const int MaxHistoryEntries = 50;
private const int MaxHistoryCodePreview = 500;
internal const int WrapperLineOffset = 10;
private const string WrapperClassName = "MCPDynamicCode";
private const string WrapperMethodName = "Execute";

Expand Down Expand Up @@ -201,7 +201,7 @@ private static object HandleReplay(JObject @params)

private static object CompileAndExecute(string code, string compiler)
{
string wrappedSource = WrapUserCode(code);
string wrappedSource = WrapUserCode(code, out int lineOffset);

string cacheKey = compiler + "\n" + wrappedSource;
if (_compiledCache.TryGetValue(cacheKey, out CompiledSnippet cached))
Expand All @@ -217,14 +217,14 @@ private static object CompileAndExecute(string code, string compiler)
case "roslyn":
if (!RoslynCompiler.IsAvailable)
return new ErrorResponse("Roslyn (Microsoft.CodeAnalysis) is not available. Install it via NuGet or use compiler='codedom'.");
compiled = RoslynCompiler.Compile(wrappedSource, assemblyPaths, out var roslynErrors);
compiled = RoslynCompiler.Compile(wrappedSource, assemblyPaths, lineOffset, out var roslynErrors);
if (compiled == null)
return new ErrorResponse("Compilation failed", new { errors = OffsetErrors(roslynErrors), compiler = "roslyn" });
usedCompiler = "roslyn";
break;

case "codedom":
compiled = CodeDomCompile(wrappedSource, assemblyPaths, out var codedomErrors);
compiled = CodeDomCompile(wrappedSource, assemblyPaths, lineOffset, out var codedomErrors);
if (compiled == null)
return new ErrorResponse("Compilation failed", new { errors = OffsetErrors(codedomErrors), compiler = "codedom" });
usedCompiler = "codedom";
Expand All @@ -233,14 +233,14 @@ private static object CompileAndExecute(string code, string compiler)
default: // "auto"
if (RoslynCompiler.IsAvailable)
{
compiled = RoslynCompiler.Compile(wrappedSource, assemblyPaths, out var autoErrors);
compiled = RoslynCompiler.Compile(wrappedSource, assemblyPaths, lineOffset, out var autoErrors);
if (compiled == null)
return new ErrorResponse("Compilation failed", new { errors = OffsetErrors(autoErrors), compiler = "roslyn" });
usedCompiler = "roslyn";
}
else
{
compiled = CodeDomCompile(wrappedSource, assemblyPaths, out var autoFallbackErrors);
compiled = CodeDomCompile(wrappedSource, assemblyPaths, lineOffset, out var autoFallbackErrors);
if (compiled == null)
return new ErrorResponse("Compilation failed", new { errors = OffsetErrors(autoFallbackErrors), compiler = "codedom" });
usedCompiler = "codedom";
Expand Down Expand Up @@ -302,7 +302,7 @@ private static List<string> OffsetErrors(List<string> errors)

// ──────────────────── CodeDom compiler ────────────────────

private static Assembly CodeDomCompile(string source, string[] assemblyPaths, out List<string> errors)
private static Assembly CodeDomCompile(string source, string[] assemblyPaths, int lineOffset, out List<string> errors)
{
errors = new List<string>();

Expand Down Expand Up @@ -360,7 +360,7 @@ private static Assembly CodeDomCompile(string source, string[] assemblyPaths, ou
continue;

hasRealErrors = true;
int userLine = Math.Max(1, error.Line - WrapperLineOffset);
int userLine = Math.Max(1, error.Line - lineOffset);
errors.Add($"Line {userLine}: {error.ErrorText}");
}

Expand Down Expand Up @@ -508,22 +508,54 @@ public CodeDomAssemblyCandidate(string path, AssemblyName assemblyName)

// ──────────────────── Shared helpers ────────────────────

private static string WrapUserCode(string code)
private static readonly string[] BuiltinUsings =
{
"using System;",
"using System.Collections.Generic;",
"using System.Linq;",
"using System.Reflection;",
"using System.Text;",
"using UnityEngine;",
"using UnityEditor;",
};

private static readonly Regex UsingDirectiveLine = new Regex(
@"^\s*using\s+(?:static\s+)?[A-Za-z_]\w*(?:\s*\.\s*[A-Za-z_]\w*)*\s*;\s*$", RegexOptions.Compiled);

// Must not match the C# 8 `using var x = ...` declaration, which has two tokens before the =
private static readonly Regex UsingAliasLine = new Regex(
@"^\s*using\s+[A-Za-z_]\w*\s*=\s*[A-Za-z_][\w.]*\s*(?:<[^;]*>)?\s*(?:\[\s*\])*\s*;\s*$", RegexOptions.Compiled);
Comment thread
coderabbitai[bot] marked this conversation as resolved.

private static bool IsUsingDirective(string line)
=> UsingDirectiveLine.IsMatch(line) || UsingAliasLine.IsMatch(line);

// A using directive is illegal in a method body, so the parser reads it as a using-statement and wants a '('
// Hoisted lines are blanked, not removed, so compiler line numbers still map to the caller's.
private static string WrapUserCode(string code, out int lineOffset)
{
var body = code.Replace("\r\n", "\n").Split('\n');
var hoisted = new List<string>();
for (int i = 0; i < body.Length; i++)
{
string trimmed = body[i].Trim();
if (trimmed.Length == 0 || trimmed.StartsWith("//")) continue;
if (!IsUsingDirective(body[i])) break;
if (!BuiltinUsings.Contains(trimmed) && !hoisted.Contains(trimmed)) hoisted.Add(trimmed);
body[i] = string.Empty;
Comment thread
coderabbitai[bot] marked this conversation as resolved.
Outdated
}

var sb = new StringBuilder();
sb.AppendLine("using System;");
sb.AppendLine("using System.Collections.Generic;");
sb.AppendLine("using System.Linq;");
sb.AppendLine("using System.Reflection;");
sb.AppendLine("using UnityEngine;");
sb.AppendLine("using UnityEditor;");
foreach (string u in BuiltinUsings) sb.AppendLine(u);
foreach (string u in hoisted) sb.AppendLine(u);
sb.AppendLine($"public static class {WrapperClassName}");
sb.AppendLine("{");
sb.AppendLine($" public static object {WrapperMethodName}()");
sb.AppendLine(" {");
sb.AppendLine(code);
sb.AppendLine(string.Join("\n", body));
sb.AppendLine(" }");
sb.AppendLine("}");
// 4 = the class line, its brace, the method line, its brace
lineOffset = BuiltinUsings.Length + hoisted.Count + 4;
return sb.ToString();
}

Expand Down Expand Up @@ -663,6 +695,7 @@ public static void ResetCache()
_isAvailable = null;
}


private static bool Initialize()
{
try
Expand Down Expand Up @@ -752,7 +785,7 @@ private static bool Initialize()
}
}

public static Assembly Compile(string source, string[] assemblyPaths, out List<string> errors)
public static Assembly Compile(string source, string[] assemblyPaths, int lineOffset, out List<string> errors)
{
errors = new List<string>();

Expand Down Expand Up @@ -837,7 +870,7 @@ public static Assembly Compile(string source, string[] assemblyPaths, out List<s
var msgProp = diag.GetType().GetMethod("GetMessage", new[] { typeof(System.Globalization.CultureInfo) });
string msg = (string)msgProp.Invoke(diag, new object[] { null });

int userLine = Math.Max(1, line + 1 - ExecuteCode.WrapperLineOffset);
int userLine = Math.Max(1, line + 1 - lineOffset);
errors.Add($"Line {userLine}: {msg}");
}
return null;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -537,6 +537,65 @@ private static void AssertCompilerSuccess(CompilerResults results)
Assert.IsFalse(results.Errors.HasErrors, string.Join("\n", errors));
}


// ──────────────────── Execute: using directives ────────────────────

[Test]
public void Execute_LeadingUsingDirective_IsHoistedAndCompiles()
{
var result = Execute("using System.IO;\nreturn Path.GetFileName(\"/a/b.txt\");");

Assert.IsTrue(result.Value<bool>("success"), result.ToString());
Assert.AreEqual("b.txt", result["data"]["result"].Value<string>());
}

[Test]
public void Execute_UsingStaticDirective_IsHoisted()
{
var result = Execute("using static System.Math;\nreturn Max(3, 4);");

Assert.IsTrue(result.Value<bool>("success"), result.ToString());
Assert.AreEqual(4, result["data"]["result"].Value<int>());
}

[Test]
public void Execute_UsingAliasWithGenericTarget_IsHoisted()
{
var result = Execute("using L = System.Collections.Generic.List<int>;\nreturn new L { 1, 2 }.Count;");

Assert.IsTrue(result.Value<bool>("success"), result.ToString());
Assert.AreEqual(2, result["data"]["result"].Value<int>());
}

[Test]
public void Execute_UsingAfterComment_IsStillHoisted()
{
var result = Execute("// leading comment\nusing System.IO;\nreturn Path.GetFileName(\"/x/y.txt\");");

Assert.IsTrue(result.Value<bool>("success"), result.ToString());
Assert.AreEqual("y.txt", result["data"]["result"].Value<string>());
}


[Test]
public void Execute_RedundantBuiltinUsing_DoesNotDuplicate()
{
var result = Execute("using System;\nreturn String.Concat(\"a\", \"b\");");

Assert.IsTrue(result.Value<bool>("success"), result.ToString());
Assert.AreEqual("ab", result["data"]["result"].Value<string>());
}

[Test]
public void Execute_ErrorLineNumber_CountsFromCallerCodeAfterHoisting()
{
var result = Execute("using System.IO;\nint ok = 1;\nnope();\nreturn ok;");

Assert.IsFalse(result.Value<bool>("success"), result.ToString());
var errors = string.Join("\n", (result["data"]["errors"] as JArray).Select(e => e.Value<string>()));
StringAssert.Contains("Line 3", errors);
}

private static JObject Execute(string code)
{
return ToJObject(ExecuteCode.HandleCommand(new JObject
Expand Down