载入中...
搜索中...
未找到
ReloadSession.cpp
浏览该文件的文档.
4
6
7#include <cstdio>
8
9namespace eve::dev {
10
12 static ReloadSession inst;
13 return inst;
14}
15
16bool ReloadSession::begin(HSQUIRRELVM vm, std::string* err) {
17 if (active_) {
18 if (err) *err = "reload session already active";
19 return false;
20 }
21 persistentRoots_ = Snapshot::instance().roots();
22 transientRoots_ = Snapshot::instance().transientRoots();
23 if (!Snapshot::instance().captureState(vm, buffer_, err)) {
24 buffer_ = StateValue::null();
25 persistentRoots_.clear();
26 transientRoots_.clear();
27 return false;
28 }
29 // Values the capture could not represent (entity instances, closures, ...) are left
30 // out, not fatal: refusing the session here used to make "keep a list of entities in a
31 // global" block hot reload entirely. Say so once; the same list stays in the captured
32 // state and in eve_snapshot.json for a tool to read.
33 if (const StateValue* skipped = buffer_.find("skipped");
34 skipped && skipped->isArray() && skipped->arraySize() > 0) {
35 std::string names;
36 for (std::size_t i = 0; i < skipped->arraySize() && i < 3; ++i) {
37 if (!names.empty()) names += ", ";
38 names += skipped->at(i).asString();
39 }
40 if (skipped->arraySize() > 3) names += ", ...";
41 std::fprintf(stderr, "EVEngine: reload state skips %zu unserializable value(s): %s\n", skipped->arraySize(),
42 names.c_str());
43 }
44 // Best-effort manual rollback point (same file as the F6/F7 snapshot).
45 std::string saveErr;
46 Snapshot::instance().saveFile(vm, "eve_snapshot.json", &saveErr);
49 active_ = true;
50 return true;
51}
52
53bool ReloadSession::commit(HSQUIRRELVM vm, std::string* err) {
54 if (!active_) {
55 if (err) *err = "no active reload session";
56 return false;
57 }
58 StateValue merged = buffer_;
59
60 // Merge freshly-defined roots under the captured values: captured values
61 // win, fields the reloaded script added are kept.
63 std::string captureErr;
64 if (Snapshot::instance().captureState(vm, current, &captureErr)) {
65 auto result = Snapshot::instance().mergeDefaultsState(buffer_, current);
66 if (!result.ok()) {
67 if (err) *err = result.diagnostics().front().message();
68 return false;
69 }
70 merged = std::move(result).takeValue();
71 }
72
73 const bool ok = restore(vm, merged, err);
74 // Keep the original buffer and the session open when restoration fails.
75 // The script orchestrator can then restore old root bindings and call
76 // abort(), instead of being stranded in a partially restored state.
77 if (ok) {
78 buffer_ = StateValue::null();
79 persistentRoots_.clear();
80 transientRoots_.clear();
81 active_ = false;
82 }
83 return ok;
84}
85
86bool ReloadSession::abort(HSQUIRRELVM vm, std::string* err) {
87 if (!active_) {
88 if (err) *err = "no active reload session";
89 return false;
90 }
91 Snapshot::instance().setRootPolicies(persistentRoots_, transientRoots_);
92 const bool ok = restore(vm, buffer_, err);
93 buffer_ = StateValue::null();
94 persistentRoots_.clear();
95 transientRoots_.clear();
96 active_ = false;
97 return ok;
98}
99
100bool ReloadSession::restore(HSQUIRRELVM vm, const StateValue& state, std::string* err) {
101 return Snapshot::instance().restoreState(vm, state, err);
102}
103
104} // namespace eve::dev
struct SQVM * HSQUIRRELVM
HSQUIRRELVM vm
Definition ECS.cpp:38
Poco::JSON::Object::Ptr result
double current
JSON-compatible state value tree used by state hot reload.
Definition StateValue.h:30
const StateValue * find(const std::string &key) const
Look up key; nullptr when absent.
static StateValue null()
Null value.
Definition StateValue.h:38
One state hot-reload transaction: capture → reload → restore.
bool commit(HSQUIRRELVM vm, std::string *err=nullptr)
Restore captured state over the new definitions.
bool abort(HSQUIRRELVM vm, std::string *err=nullptr)
End the session, rolling back to the captured state.
static ReloadSession & instance()
Instance.
bool begin(HSQUIRRELVM vm, std::string *err=nullptr)
Start a session: capture current script + native state.
static Snapshot & instance()
Definition Snapshot.cpp:468
void resetMcpFrameCapture()
Main-thread disconnect cleanup; workers observe cancellation before publishing files.
void resetMcpActionGuide()
Clear the process-local guide-pack session used by get/set.
std::vector< Object::Ptr > diagnostics