载入中...
搜索中...
未找到
ReloadSession.cpp
浏览该文件的文档.
2
4
5namespace eve::dev {
6
8 static ReloadSession inst;
9 return inst;
10}
11
12bool ReloadSession::begin(HSQUIRRELVM vm, std::string* err) {
13 if (active_) {
14 if (err) *err = "reload session already active";
15 return false;
16 }
17 persistentRoots_ = Snapshot::instance().roots();
18 transientRoots_ = Snapshot::instance().transientRoots();
19 if (!Snapshot::instance().captureState(vm, buffer_, err)) {
20 buffer_ = StateValue::null();
21 persistentRoots_.clear();
22 transientRoots_.clear();
23 return false;
24 }
25 // Best-effort manual rollback point (same file as the F6/F7 snapshot).
26 std::string saveErr;
27 Snapshot::instance().saveFile(vm, "eve_snapshot.json", &saveErr);
28 active_ = true;
29 return true;
30}
31
32bool ReloadSession::commit(HSQUIRRELVM vm, std::string* err) {
33 if (!active_) {
34 if (err) *err = "no active reload session";
35 return false;
36 }
37 StateValue merged = buffer_;
38
39 // Merge freshly-defined roots under the captured values: captured values
40 // win, fields the reloaded script added are kept.
42 std::string captureErr;
43 if (Snapshot::instance().captureState(vm, current, &captureErr)) {
44 StateValue* mergedRoots = merged.find("roots");
45 const StateValue* currentRoots = current.find("roots");
46 if (mergedRoots && mergedRoots->isObject() && currentRoots && currentRoots->isObject()) {
47 for (const auto& name : mergedRoots->keys()) {
48 StateValue* oldRoot = mergedRoots->find(name);
49 const StateValue* newRoot = currentRoots->find(name);
50 if (oldRoot && oldRoot->isObject() && newRoot && newRoot->isObject()) {
51 oldRoot->mergeDefaults(*newRoot);
52 }
53 }
54 }
55 }
56
57 const bool ok = restore(vm, merged, err);
58 // Keep the original buffer and the session open when restoration fails.
59 // The script orchestrator can then restore old root bindings and call
60 // abort(), instead of being stranded in a partially restored state.
61 if (ok) {
62 buffer_ = StateValue::null();
63 persistentRoots_.clear();
64 transientRoots_.clear();
65 active_ = false;
66 }
67 return ok;
68}
69
70bool ReloadSession::abort(HSQUIRRELVM vm, std::string* err) {
71 if (!active_) {
72 if (err) *err = "no active reload session";
73 return false;
74 }
75 Snapshot::instance().setRootPolicies(persistentRoots_, transientRoots_);
76 const bool ok = restore(vm, buffer_, err);
77 buffer_ = StateValue::null();
78 persistentRoots_.clear();
79 transientRoots_.clear();
80 active_ = false;
81 return ok;
82}
83
84bool ReloadSession::restore(HSQUIRRELVM vm, const StateValue& state, std::string* err) {
85 return Snapshot::instance().restoreState(vm, state, err);
86}
87
88} // namespace eve::dev
struct SQVM * HSQUIRRELVM
HSQUIRRELVM vm
Definition ECS.cpp:20
std::string name
double current
JSON-compatible state value tree used by state hot reload.
Definition StateValue.h:20
bool mergeDefaults(const StateValue &defaults)
Fill missing members from defaults; object members merge recursively, existing values are never overw...
bool isObject() const
Definition StateValue.h:50
const StateValue * find(const std::string &key) const
Look up key; nullptr when absent.
static StateValue null()
Null value.
Definition StateValue.h:28
std::vector< std::string > keys() const
Member names in insertion order.
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.
bool restoreState(HSQUIRRELVM vm, const StateValue &state, std::string *error=nullptr) const
Restore a captured state; v1-shaped values restore roots only.
Definition Snapshot.cpp:455
std::vector< std::string > roots() const
Return the explicit persistent roots.
Definition Snapshot.cpp:328
std::vector< std::string > transientRoots() const
Return the explicit transient roots.
Definition Snapshot.cpp:330
void setRootPolicies(std::vector< std::string > persistent, std::vector< std::string > transient)
Atomically replace explicit persistent/transient root policy.
Definition Snapshot.cpp:332
bool saveFile(HSQUIRRELVM vm, const std::string &path, std::string *error=nullptr) const
Definition Snapshot.cpp:526
static Snapshot & instance()
Definition Snapshot.cpp:290