载入中...
搜索中...
未找到
McpSceneDirector.cpp
浏览该文件的文档.
2
6#include "scripts.h"
7
8#include <squirrel.h>
9
10#include <atomic>
11#include <mutex>
12
13namespace eve::dev {
14namespace {
15
16constexpr const SQChar* kMcpSnippetSource = _SC("memory://mcp/snippet");
17constexpr const SQChar* kMcpSceneDirectorSource = _SC("memory://mcp/scene-director");
18
19std::mutex g_installMu;
20std::atomic_bool g_installPending{false};
21
22std::string snippetErrorText(HSQUIRRELVM vm, bool compile, const SQChar* source) {
25 if (ctx.source.empty()) ctx.source = source;
26 if (ctx.message.empty()) ctx.message = compile ? "compile failed" : "runtime failed";
27 if (compile) ctx.stack.clear();
28 std::string report = std::string("type: ") + (compile ? "compile_error" : "runtime_error") +
29 "\norigin: " + (source == kMcpSnippetSource ? "eve_run_script" : "scene_director") +
30 "\nsource: " + ctx.source + "\nline: " + std::to_string(ctx.line) +
31 "\ncolumn: " + std::to_string(ctx.column) +
32 "\nmessage: " + (compile ? ctx.message : eve::script::formatScriptError(ctx));
34 return report;
35}
36
37bool sceneDirectorPresent(HSQUIRRELVM vm) {
38 const std::string check = "return (\"scene_director\" in getroottable()) ? (scene_director != null) : false;";
39 const std::string out = callSceneDirectorReturn(vm, check, nullptr);
40 return !out.empty() && out.find("true") != std::string::npos;
41}
42
43bool installSceneDirectorNow(HSQUIRRELVM vm, std::string* err) {
44 const char* kit = eve::scene_director_content;
45 if (!kit || !*kit) {
46 if (err) *err = "scene_director.nut not embedded (rebuild EVScripts)";
47 return false;
48 }
49 return runMcpVmSnippet(vm, kit, err);
50}
51
52} // namespace
53
54bool runMcpVmSnippet(HSQUIRRELVM vm, const std::string& source, std::string* err) {
56 const SQInteger top = sq_gettop(vm);
57 // MCP snippets are transient input, not project scripts. Compile them
58 // directly from memory so the unified project compiler cannot register a
59 // file-shaped source that hot reload may later treat as user content.
60 if (SQ_FAILED(
61 sq_compilebuffer(vm, source.c_str(), static_cast<SQInteger>(source.size()), kMcpSnippetSource, SQTrue))) {
62 sq_settop(vm, top);
63 if (err) *err = snippetErrorText(vm, true, kMcpSnippetSource);
64 return false;
65 }
66 sq_pushroottable(vm);
67 if (SQ_FAILED(sq_call(vm, 1, SQFalse, SQTrue))) {
68 sq_settop(vm, top);
69 if (err) *err = snippetErrorText(vm, false, kMcpSnippetSource);
70 return false;
71 }
72 sq_settop(vm, top);
73 return true;
74}
75
76std::string callSceneDirectorReturn(HSQUIRRELVM vm, const std::string& snippet, std::string* err) {
78 const SQInteger top = sq_gettop(vm);
79 if (SQ_FAILED(sq_compilebuffer(vm, snippet.c_str(), static_cast<SQInteger>(snippet.size()), kMcpSceneDirectorSource,
80 SQTrue))) {
81 sq_settop(vm, top);
82 if (err) *err = snippetErrorText(vm, true, kMcpSceneDirectorSource);
83 return {};
84 }
85 sq_pushroottable(vm);
86 if (SQ_FAILED(sq_call(vm, 1, SQTrue, SQTrue))) {
87 sq_settop(vm, top);
88 std::string text = snippetErrorText(vm, false, kMcpSceneDirectorSource);
89 if (err)
90 *err = text == "runtime failed" ? "runtime failed (is the scene_director kit installed?)" : std::move(text);
91 return {};
92 }
93 std::string json = sqValueToJson(vm, -1);
94 sq_settop(vm, top);
95 return json;
96}
97
99 return ensureSceneDirectorInstalled(vm, err, /*allowHeavyWork=*/true);
100}
101
102bool ensureSceneDirectorInstalled(HSQUIRRELVM vm, std::string* err, bool allowHeavyWork) {
103 if (!vm) {
104 if (err) *err = "no VM";
105 return false;
106 }
107 if (sceneDirectorPresent(vm)) {
108 g_installPending.store(false);
109 return true;
110 }
111 if (!allowHeavyWork) {
112 g_installPending.store(true);
113 if (err) *err = "deferred-to-update";
114 return false;
115 }
116 std::lock_guard lock(g_installMu);
117 if (sceneDirectorPresent(vm)) {
118 g_installPending.store(false);
119 return true;
120 }
121 const bool ok = installSceneDirectorNow(vm, err);
122 if (ok) g_installPending.store(false);
123 return ok;
124}
125
127 if (!g_installPending.load() || !vm) return;
128 std::string err;
129 (void)ensureSceneDirectorInstalled(vm, &err, /*allowHeavyWork=*/true);
130}
131
132bool sceneDirectorInstallPending() { return g_installPending.load(); }
133
134std::string sceneDirectorToolError(const std::string& name, const std::string& err) {
135 if (err == "deferred-to-update") {
136 return std::string("{\"ok\":false,\"retryable\":true,\"reason\":\"deferred-to-update\",\"tool\":\"") + name +
137 "\",\"hint\":\"scene_director install was queued for the next eve.dev.poll / eve_update; retry this "
138 "tool\"}";
139 }
140 return "error: " + name + ": " + (err.empty() ? "unknown" : err);
141}
142
143} // namespace eve::dev
struct SQVM * HSQUIRRELVM
HSQUIRRELVM vm
Definition ECS.cpp:38
std::string name
Definition ECS.cpp:154
std::string message
Shared helpers for MCP tools that drive the embedded scene_director kit.
std::string script
Definition PlayHost.cpp:117
unsigned line
std::string text
ScriptStructure out
std::string_view source
unsigned column
SQInteger top
Definition Snapshot.cpp:115
bool ensureSceneDirectorInstalled(HSQUIRRELVM vm, std::string *err)
Convenience overload: allow heavy install immediately (tests / explicit install tool).
bool sceneDirectorInstallPending()
Whether a deferred install is waiting for the next poll.
void flushPendingSceneDirectorInstall(HSQUIRRELVM vm)
Run a deferred scene_director install on the game/MCP poll thread.
std::string sceneDirectorToolError(const std::string &name, const std::string &err)
Format a scene-director tool failure.
void mcpRecordSnippetError(std::string report)
VM-thread-only owning diagnostic publication; clears stale slicing data.
Definition DevTool.cpp:905
bool runMcpVmSnippet(HSQUIRRELVM vm, const std::string &source, std::string *err)
Compile and run a transient MCP snippet (no return value). @thread Game-loop / MCP poll thread with a...
std::string sqValueToJson(HSQUIRRELVM vm, SQInteger idx, int depth=0)
Forward declaration: the key serializer falls back to the value serializer.
std::string callSceneDirectorReturn(HSQUIRRELVM vm, const std::string &snippet, std::string *err)
Compile+run a snippet that returns a value; serialize it as JSON.
std::string formatScriptError(const ScriptErrorContext &ctx)
Formats a context into a human-readable multi-line report.
void clearLastScriptError(HSQUIRRELVM vm)
Drops any recorded error for a VM.
ScriptErrorContext takeLastScriptError(HSQUIRRELVM vm)
Consumes and clears the last recorded error for a VM.
ScriptErrorContext captureCompileError(HSQUIRRELVM vm)
Captures the last compilation error recorded by the VM.
Build metadata (engine git commit, build time, third-party version).
Definition Build.cpp:16
const char * scene_director_content
Structured snapshot of a script error: message, throw site and stack.
Definition ScriptError.h:46
std::string message
Raw error value ("kaboom", "42", ...).
Definition ScriptError.h:47
std::vector< ScriptFrame > stack
Call stack, innermost frame first.
Definition ScriptError.h:53
std::string source
Source of the throwing frame.
Definition ScriptError.h:48