Files
Genarrative/plugins/agc-unity-editor/dotnet/vendor/Host/TargetCompiler.cs
T
kdletters 3fc3399032
Project CI / AI game creator shell Rust shard 1/4 (pull_request) Successful in 6m41s
Project CI / AI game creator shell Rust shard 3/4 (pull_request) Successful in 7m8s
Project CI / AI game creator shell Rust shard 2/4 (pull_request) Successful in 7m16s
Project CI / AI game creator shell Rust shard 4/4 (pull_request) Successful in 7m38s
Project CI / AI game creator shell Rust smoke (pull_request) Successful in 2m11s
Project CI / AI game creator shell Rust crates (pull_request) Successful in 3m11s
Project CI / Repository checks (pull_request) Successful in 4m22s
Project CI / Frontend tests (pull_request) Successful in 5m52s
Project CI / Native shell tests (pull_request) Successful in 9m52s
Project CI / Backend tests (pull_request) Successful in 11m3s
Project CI / AI game creator shell web tests (pull_request) Successful in 6m41s
接入 DotCraft Unity 编辑器插件
新增内置 Unity 插件与自包含 Attach helper,固定上游版本并保留许可证
统一 Runner 执行归属、项目身份校验、回执确认和不确定结果阻断
接入 Unity 工程导入、Agent 工具目录及 Windows 构建分发
补齐插件测试、CI 执行入口和 Windows .NET 10 工具链检查
记录真实 Unity 连接、执行、重载恢复及 Runner 边界验证
2026-09-18 19:44:36 +08:00

36 lines
2.3 KiB
C#

using Microsoft.CodeAnalysis;
using Microsoft.CodeAnalysis.CSharp;
using System.Security.Cryptography;
namespace DotCraft.Unity;
internal static class TargetCompiler
{
public static string Compile(string sourcePath, IEnumerable<string> references, string cache, string prefix = "Attach_", string generation = "")
{
var paths = references.Where(File.Exists).Distinct(StringComparer.OrdinalIgnoreCase).Order().ToArray();
var source = Directory.Exists(sourcePath)
? string.Join("\n", Directory.GetFiles(sourcePath, "*.cs").Order().Select(File.ReadAllText))
: File.ReadAllText(sourcePath);
var identity = "CSharp9|DOTCRAFT_ATTACH|Debug|" + generation + source + string.Join("\n", paths.Select(p => p + Convert.ToHexString(SHA256.HashData(File.ReadAllBytes(p)))));
var hash = Convert.ToHexString(SHA256.HashData(System.Text.Encoding.UTF8.GetBytes(identity)));
Directory.CreateDirectory(cache);
var output = Path.Combine(cache, prefix + hash + ".dll");
if (File.Exists(output)) return output;
var compilation = CSharpCompilation.Create(prefix + hash,
Directory.Exists(sourcePath)
? Directory.GetFiles(sourcePath, "*.cs").Order().Select(p => CSharpSyntaxTree.ParseText(File.ReadAllText(p), new CSharpParseOptions(LanguageVersion.CSharp9, preprocessorSymbols: new[] { "DOTCRAFT_ATTACH" }), p))
: new[] { CSharpSyntaxTree.ParseText(source, new CSharpParseOptions(LanguageVersion.CSharp9, preprocessorSymbols: new[] { "DOTCRAFT_ATTACH" }), sourcePath) },
paths.Select(p => MetadataReference.CreateFromFile(p)),
new CSharpCompilationOptions(OutputKind.DynamicallyLinkedLibrary, optimizationLevel: OptimizationLevel.Debug));
using var stream = new MemoryStream();
var result = compilation.Emit(stream);
if (!result.Success) throw new InvalidOperationException(string.Join("\n", result.Diagnostics.Where(d => d.Severity == DiagnosticSeverity.Error)));
var temporary = output + "." + Guid.NewGuid().ToString("N");
File.WriteAllBytes(temporary, stream.ToArray());
try { File.Move(temporary, output, false); }
catch (IOException) when (File.Exists(output)) { File.Delete(temporary); }
return output;
}
}