Files
Genarrative/plugins/agc-unity-editor/dotnet/vendor/Host/SnippetSourceBuilder.cs
T
kdletters 33f5ad68bf
Project CI / AI game creator shell Rust shard 1/4 (push) Successful in 7m19s
Project CI / AI game creator shell Rust shard 3/4 (push) Successful in 7m26s
Project CI / AI game creator shell Rust shard 4/4 (push) Successful in 7m32s
Project CI / AI game creator shell Rust shard 2/4 (push) Successful in 7m34s
Project CI / AI game creator shell Rust smoke (push) Successful in 1m57s
Project CI / AI game creator shell Rust crates (push) Successful in 3m18s
Project CI / Native shell tests (push) Successful in 10m54s
Project CI / Backend tests (push) Successful in 12m42s
Project CI / Frontend tests (push) Successful in 13m4s
Project CI / AI game creator shell web tests (push) Successful in 5m1s
Project CI / Repository checks (push) Successful in 12m48s
接入 DotCraft Unity 编辑器插件与受控执行链路 (#423)
AGC 原有插件系统无法直接操作已打开的 Unity Editor。本变更增加内置 `agc-unity-editor`,在 Windows x64 / Unity Mono 上支持当前项目探测、连接与 C# 执行,不向 Unity 工程安装 UPM 桥接包。

## 主要变更

- 固定复用 DotCraft.Unity 0.4.3 的 Attach 核心,提供自包含 .NET helper,保留上游许可证、来源及修改记录。
- GUI、Runtime、DirectProject 共用 Runner 执行服务;补齐项目身份、并发、总期限、回执确认与持久不确定状态阻断。
- 现有打开项目入口支持 Unity,按项目类型及开关暴露插件和 Agent 工具。
- Windows 构建准备 helper 并随包分发;插件 JS/Rust 测试接入现有 CI 组,Jenkins 增加 .NET 10 工具链预检。

## 验证

- .NET helper 27 项测试、自包含发布及最小环境协议 smoke 通过。
- Unity 6000.3.7f1 实机验证通过:连接、C# 执行、编译错误修复、断连重连、Domain Reload 后重新握手;真实 Runner 的 ACK、并发拒绝和跨重启阻断通过。
- 宿主 Unity、PluginHost、Cocos、MCP、工具目录与引擎识别定向回归通过;前端类型检查、插件 JS/Rust、CI 配置、格式、编码和文档门禁通过。

Linux CI 不代替 Windows helper/实机验证;发行安装包 UI smoke、其它 Unity 版本和 Unity CoreCLR 未验证。Unity 演示工程中的场景和组件已撤销,不在此 PR 范围内。

---------

Co-authored-by: kdletters <61648117+kdletters@users.noreply.github.com>
Reviewed-on: http://192.168.35.82/git/GenarrativeAI/Genarrative/pulls/423
2026-09-19 12:29:27 +08:00

189 lines
6.9 KiB
C#

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.CodeAnalysis.CSharp;
namespace DotCraft.Unity
{
/// <summary>Builds target-compatible snippets while preserving source diagnostics.</summary>
public static class SnippetSourceBuilder
{
public static string Build(
string className,
string code,
string sourceLabel,
bool asynchronous = true,
string runtimeNamespace = "DotCraft.Unity",
string? generatedNamespace = null)
{
generatedNamespace = generatedNamespace ?? runtimeNamespace + ".Execution.Generated";
sourceLabel = sourceLabel.Replace("\\", "/").Replace("\"", "\\\"");
var snippet = ParseSnippet(code);
var source = new StringBuilder();
AppendMappedUsings(source, snippet.GlobalUsings, sourceLabel);
source.AppendLine("using System;");
source.AppendLine("using System.Linq;");
source.AppendLine("using System.Collections.Generic;");
source.AppendLine("using UnityEngine;");
source.AppendLine("using UnityEditor;");
source.AppendLine("using System.Threading;");
source.AppendLine("using System.Threading.Tasks;");
source.AppendLine("using Newtonsoft.Json.Linq;");
source.Append("using ").Append(runtimeNamespace).AppendLine(";");
AppendMappedUsings(source, snippet.Usings, sourceLabel);
source.AppendLine();
source.Append("namespace ").AppendLine(generatedNamespace);
source.AppendLine("{");
source.Append(" public static class ").AppendLine(className);
source.AppendLine(" {");
source.Append(" public static ").Append(asynchronous ? "async Task<object>" : "object")
.AppendLine(" Run(JObject Args, UnityExecutionContext ctx, CancellationToken cancellationToken)");
source.AppendLine(" {");
source.Append("#line 1 \"").Append(sourceLabel).AppendLine("\"");
source.AppendLine(snippet.Body);
source.AppendLine("#line default");
source.AppendLine(" return null;");
source.AppendLine(" }");
source.AppendLine(" }");
source.AppendLine("}");
return source.ToString();
}
private static SnippetSource ParseSnippet(string code)
{
var globalPrefixes = FindGlobalUsingPrefixes(code);
var normalizedCode = code.ToCharArray();
foreach (var prefix in globalPrefixes)
BlankSpan(normalizedCode, prefix.Start, prefix.End);
var syntaxTree = CSharpSyntaxTree.ParseText(
new string(normalizedCode),
new CSharpParseOptions(LanguageVersion.Latest));
var root = syntaxTree.GetCompilationUnitRoot();
var body = code.ToCharArray();
var globalUsings = new List<MappedUsingDirective>();
var usings = new List<MappedUsingDirective>();
foreach (var directive in root.Usings)
{
var lineSpan = syntaxTree.GetLineSpan(directive.Span);
var mapped = new MappedUsingDirective(
directive.ToString(),
lineSpan.StartLinePosition.Line + 1);
var globalPrefix = globalPrefixes.FirstOrDefault(prefix =>
prefix.UsingStart == directive.UsingKeyword.SpanStart);
if (globalPrefix != null)
{
globalUsings.Add(mapped);
BlankSpan(body, globalPrefix.Start, globalPrefix.End);
}
else
usings.Add(mapped);
BlankSpan(body, directive.Span.Start, directive.Span.End);
}
return new SnippetSource(new string(body), globalUsings, usings);
}
private static List<GlobalUsingPrefix> FindGlobalUsingPrefixes(string code)
{
// ParseTokens keeps lexically valid tokens that Roslyn 3.7 would otherwise
// place in skipped trivia because global using was introduced after C# 9.
var tokens = SyntaxFactory.ParseTokens(code).ToList();
var prefixes = new List<GlobalUsingPrefix>();
for (var index = 0; index + 1 < tokens.Count; index++)
{
var globalToken = tokens[index];
var usingToken = tokens[index + 1];
if (globalToken.ValueText == "global" && usingToken.ValueText == "using")
{
prefixes.Add(new GlobalUsingPrefix(
globalToken.SpanStart,
globalToken.Span.End,
usingToken.SpanStart));
}
}
return prefixes;
}
private static void BlankSpan(char[] text, int start, int end)
{
for (var index = start; index < end; index++)
{
if (text[index] != '\r' && text[index] != '\n')
text[index] = ' ';
}
}
private static void AppendMappedUsings(
StringBuilder source,
IEnumerable<MappedUsingDirective> directives,
string sourceLabel)
{
foreach (var directive in directives)
{
source.Append("#line ")
.Append(directive.Line)
.Append(" \"")
.Append(sourceLabel)
.AppendLine("\"");
source.AppendLine(directive.Text);
source.AppendLine("#line default");
}
}
private sealed class SnippetSource
{
public SnippetSource(
string body,
List<MappedUsingDirective> globalUsings,
List<MappedUsingDirective> usings)
{
Body = body;
GlobalUsings = globalUsings;
Usings = usings;
}
public string Body { get; }
public List<MappedUsingDirective> GlobalUsings { get; }
public List<MappedUsingDirective> Usings { get; }
}
private sealed class MappedUsingDirective
{
public MappedUsingDirective(string text, int line)
{
Text = text;
Line = line;
}
public string Text { get; }
public int Line { get; }
}
private sealed class GlobalUsingPrefix
{
public GlobalUsingPrefix(int start, int end, int usingStart)
{
Start = start;
End = end;
UsingStart = usingStart;
}
public int Start { get; }
public int End { get; }
public int UsingStart { get; }
}
}
}