载入中...
搜索中...
未找到
SoftRestart.cpp
浏览该文件的文档.
2
3#include "common/Capability.h"
6#include "common/Resource.h"
11
12#include <Poco/Dynamic/Var.h>
13#include <Poco/JSON/Array.h>
14#include <Poco/JSON/Object.h>
15
16#include <squirrel.h>
17
18#include <string>
19
20namespace eve::dev {
21namespace {
22
23constexpr int kMaxArgumentDepth = 16;
24
25void pushJsonValue(HSQUIRRELVM vm, const Poco::Dynamic::Var& value, int depth) {
26 if (depth > kMaxArgumentDepth || value.isEmpty()) {
27 sq_pushnull(vm);
28 return;
29 }
30 if (value.isBoolean()) {
31 sq_pushbool(vm, value.convert<bool>() ? SQTrue : SQFalse);
32 return;
33 }
34 if (value.isInteger()) {
35 sq_pushinteger(vm, static_cast<SQInteger>(value.convert<Poco::Int64>()));
36 return;
37 }
38 if (value.isNumeric()) {
39 sq_pushfloat(vm, static_cast<SQFloat>(value.convert<double>()));
40 return;
41 }
42 if (value.isString()) {
43 const std::string text = value.convert<std::string>();
44 sq_pushstring(vm, text.c_str(), static_cast<SQInteger>(text.size()));
45 return;
46 }
47 if (value.type() == typeid(Poco::JSON::Array::Ptr)) {
48 auto array = value.extract<Poco::JSON::Array::Ptr>();
49 sq_newarray(vm, 0);
50 const SQInteger target = sq_gettop(vm);
51 for (unsigned index = 0; index < array->size(); ++index) {
52 pushJsonValue(vm, array->get(index), depth + 1);
53 sq_arrayappend(vm, target);
54 }
55 return;
56 }
57 if (value.type() == typeid(Poco::JSON::Object::Ptr)) {
58 auto object = value.extract<Poco::JSON::Object::Ptr>();
59 sq_newtable(vm);
60 const SQInteger target = sq_gettop(vm);
61 for (const auto& entry : *object) {
62 sq_pushstring(vm, entry.first.c_str(), static_cast<SQInteger>(entry.first.size()));
63 pushJsonValue(vm, entry.second, depth + 1);
64 sq_newslot(vm, target, SQFalse);
65 }
66 return;
67 }
68 sq_pushnull(vm);
69}
70
71std::string lastVmError(HSQUIRRELVM vm) {
73 if (!ctx.empty()) return eve::script::formatScriptError(ctx);
74 return "soft restart failed";
75}
76
78Result<void> installRestartArgs(HSQUIRRELVM vm, Poco::JSON::Object::Ptr args) {
79 const SQInteger top = sq_gettop(vm);
80 sq_pushroottable(vm);
81 sq_pushstring(vm, _SC("eve"), -1);
82 if (SQ_FAILED(sq_get(vm, -2)) || sq_gettype(vm, -1) != OT_TABLE) {
83 sq_settop(vm, top);
85 Diagnostic::error(DiagnosticCode::NotFound, "eve table missing; is load.nut running?", "eve"));
86 }
87 sq_pushstring(vm, _SC("restartArgs"), -1);
88 if (args)
89 pushJsonValue(vm, Poco::Dynamic::Var(args), 0);
90 else
91 sq_newtable(vm);
92 if (SQ_FAILED(sq_newslot(vm, -3, SQFalse))) {
93 sq_settop(vm, top);
95 Diagnostic::error(DiagnosticCode::Failed, "failed to set eve.restartArgs", "eve.restartArgs"));
96 }
97 sq_settop(vm, top);
98 return Result<void>::success();
99}
100
102Result<bool> callSoftRestartGame(HSQUIRRELVM vm, bool reloadScripts) {
103 const SQInteger top = sq_gettop(vm);
104 sq_pushroottable(vm);
105 sq_pushstring(vm, _SC("soft_restart_game"), -1);
106 if (SQ_FAILED(sq_get(vm, -2)) || sq_gettype(vm, -1) != OT_CLOSURE) {
107 sq_settop(vm, top);
110 "soft_restart_game is missing; start the game with load.nut (eve run --mcp-port ...)",
111 "soft_restart_game"));
112 }
113 sq_pushroottable(vm); // this
114 sq_pushbool(vm, reloadScripts ? SQTrue : SQFalse);
115 if (SQ_FAILED(sq_call(vm, 2, SQTrue, SQTrue))) {
116 const std::string err = lastVmError(vm);
117 sq_settop(vm, top);
119 Diagnostic::error(DiagnosticCode::Failed, err, "soft_restart_game"));
120 }
121 SQBool ok = SQFalse;
122 if (sq_gettype(vm, -1) == OT_BOOL) sq_getbool(vm, -1, &ok);
123 sq_settop(vm, top);
124 if (!ok) {
126 DiagnosticCode::Failed, "soft_restart_game returned false (see console / eve_console_read)",
127 "soft_restart_game"));
128 }
129 return Result<bool>::success(true);
130}
131
132} // namespace
133
135 std::uint32_t ok = 0;
136 eve::cap::forEach<eve::caps::IStateProvider>([&](eve::caps::IStateProvider* provider) {
137 if (provider) {
138 auto reset = provider->resetToDefaults();
139 if (reset.ok()) ++ok;
140 }
141 });
142 return ok;
143}
144
146
148 if (!vm) {
150 Diagnostic::error(DiagnosticCode::NotFound, "no VM attached", "vm"));
151 }
154 DiagnosticCode::Conflict, "a state hot-reload session is already active", "ReloadSession"));
155 }
156
157 SoftRestartReport report;
159 report.args = request.args ? request.args : Poco::JSON::Object::Ptr(new Poco::JSON::Object());
160 report.scriptsReloaded = request.reloadScripts;
161
162 // Runtime world briefs are re-declared in eve_init after a full script
163 // reload. Param-only restarts keep the overlay so Discover stays stable.
164 if (request.reloadScripts) clearWorldContext();
165
166 auto installed = installRestartArgs(vm, report.args);
167 if (!installed) return Result<SoftRestartReport>::failure(installed.status());
168
169 if (auto* capture = cap::query<IRenderFrameCapture>()) capture->invalidate();
170 auto restarted = callSoftRestartGame(vm, request.reloadScripts);
171 if (!restarted) return Result<SoftRestartReport>::failure(restarted.status());
172
173 report.initCalled = true;
175 // Soft restart must not drop the unified resource cache. A drop here means
176 // someone called clear()/unload during the restart path — treat as failure
177 // so agents do not silently pay for a cold asset load on the next frame.
178 if (report.resourceCountAfter < report.resourceCountBefore) {
181 "resource cache shrank during soft restart (expected resources to be kept)",
182 "ResourceManager"));
183 }
184 return Result<SoftRestartReport>::success(std::move(report));
185}
186
187} // namespace eve::dev
LogicalId target
bool & active
struct SQVM * HSQUIRRELVM
HSQUIRRELVM vm
Definition ECS.cpp:38
Runtime game-world Discover snapshot for MCP / Play Host.
const GltfImportRequest & request
Object::Ptr value
std::string text
Soft-restart a live game without unloading the resource cache.
Json object
std::uint32_t depth
uint32_t index
static Diagnostic error(DiagnosticCode code, std::string message, std::string path={})
Construct an error diagnostic with the standard error severity.
size_t count() const
Number of cached entries.
Definition Resource.cpp:58
static ResourceManager & getInstance()
Returns the instance.
Definition Resource.cpp:15
Move-only operation result carrying either a value or Status.
Definition Result.h:176
static Result success(T value)
Construct a successful result owning value.
Definition Result.h:185
static Result failure(Status status)
Construct a failed result from a structured status.
Definition Result.h:196
Runtime-state serialization for state hot reload.
static ReloadSession & instance()
Instance.
SQInteger top
Definition Snapshot.cpp:115
std::size_t resourceCacheCount()
Current ResourceManager cache entry count (for restart telemetry).
std::uint32_t resetNativeStateProviders()
Reset every registered IStateProvider to its defaults.
Result< SoftRestartReport > executeSoftRestart(HSQUIRRELVM vm, SoftRestartRequest request)
Soft-restart the game attached to vm.
void clearWorldContext()
Drop the runtime world brief (soft restart with script reload).
std::string formatScriptError(const ScriptErrorContext &ctx)
Formats a context into a human-readable multi-line report.
ScriptErrorContext takeLastScriptError(HSQUIRRELVM vm)
Consumes and clears the last recorded error for a VM.
Outcome of a successful soft restart. @ownership Owning value; safe to keep across frames.
bool scriptsReloaded
Whether tracked scripts were re-dofile'd.
Poco::JSON::Object::Ptr args
Echo of the args table that was installed (may be empty).
std::size_t resourceCountBefore
Cached resource entries before the restart (must equal after).
bool initCalled
Whether eve_init / eve_restart ran.
std::size_t resourceCountAfter
Cached resource entries after the restart.
Inputs for one soft restart. @thread Game / MCP poll thread only.
Structured snapshot of a script error: message, throw site and stack.
Definition ScriptError.h:46
bool empty() const noexcept
True when no error payload was captured.
Definition ScriptError.h:57