载入中...
搜索中...
未找到
ScriptComponentStore.cpp
浏览该文件的文档.
2
3#include "common/Diagnostic.h"
4
5#include <map>
6#include <utility>
7
8namespace eve {
9namespace {
10
11using Store = std::map<ScriptComponentStoreKey, Value>;
12
13Store& scriptComponentStore() {
14 static Store store;
15 return store;
16}
17
19std::uint64_t& scriptComponentRevisionCounter() {
20 static std::uint64_t revision = 1;
21 return revision;
22}
23
24void bumpScriptComponentRevision() { ++scriptComponentRevisionCounter(); }
25
26Diagnostic storeDiagnostic(DiagnosticCode code, std::string message, const std::string& path) {
27 return Diagnostic::error(code, std::move(message), path, {}, "ecs.component.store");
28}
29
30} // namespace
31
33 if (!values.isObject())
35 storeDiagnostic(DiagnosticCode::InvalidArgument, "script component values must be an object", key.name));
36 scriptComponentStore()[key] = std::move(values);
37 bumpScriptComponentRevision();
39}
40
42 const Store& store = scriptComponentStore();
43 const auto found = store.find(key);
44 return found == store.end() ? nullptr : &found->second;
45}
46
49 if (values == nullptr)
51 storeDiagnostic(DiagnosticCode::NotFound, "no stored values for this component generation", key.name));
52 const Value* value = values->find(field);
53 if (value == nullptr)
55 storeDiagnostic(DiagnosticCode::NotFound, "field is not part of the component schema", field));
57}
58
60 Store& store = scriptComponentStore();
61 const auto found = store.find(key);
62 if (found == store.end())
64 storeDiagnostic(DiagnosticCode::NotFound, "no stored values for this component generation", key.name));
65 if (!found->second.isObject() || found->second.find(field) == nullptr)
67 storeDiagnostic(DiagnosticCode::NotFound, "field is not part of the component schema", field));
68 *found->second.find(field) = std::move(value);
69 bumpScriptComponentRevision();
71}
72
73std::vector<ScriptComponentStoreEntry> scriptComponentStoreEntries() {
74 const Store& store = scriptComponentStore();
75 std::vector<ScriptComponentStoreEntry> out;
76 out.reserve(store.size());
77 for (const auto& [key, values] : store) out.push_back(ScriptComponentStoreEntry{key, values});
78 return out;
79}
80
81void replaceScriptComponentValues(std::vector<ScriptComponentStoreEntry> entries) {
82 Store replacement;
83 for (auto& entry : entries) replacement[entry.key] = std::move(entry.values);
84 scriptComponentStore() = std::move(replacement);
85 bumpScriptComponentRevision();
86}
87
89 const std::vector<std::string>& fields, const std::vector<Value>& defaults) {
90 Store& store = scriptComponentStore();
91 bool touched = false;
92 for (auto& [key, values] : store) {
93 if (key.name != name || key.version != version) continue;
94 // 逐字段重建:旧的留着、新的取默认值、不再声明的丢掉。原地改 value 而不是插删键,
95 // 迭代器因此不会失效。
96 Value::Object rebuilt;
97 for (std::size_t index = 0; index < fields.size(); ++index) {
98 const Value* existing = values.isObject() ? values.find(fields[index]) : nullptr;
99 if (existing != nullptr) {
100 rebuilt.emplace(fields[index], *existing);
101 } else {
102 rebuilt.emplace(fields[index], index < defaults.size() ? defaults[index] : Value::null());
103 }
104 }
105 values = Value::object(std::move(rebuilt));
106 touched = true;
107 }
108 if (touched) bumpScriptComponentRevision();
109}
110
111std::size_t removeScriptComponentValues(std::uint64_t entityId) {
112 Store& store = scriptComponentStore();
113 std::size_t removed = 0;
114 for (auto it = store.begin(); it != store.end();) {
115 if (it->first.entityId == entityId) {
116 it = store.erase(it);
117 ++removed;
118 } else {
119 ++it;
120 }
121 }
122 if (removed > 0) bumpScriptComponentRevision();
123 return removed;
124}
125
127 if (!scriptComponentStore().empty()) bumpScriptComponentRevision();
128 scriptComponentStore().clear();
129}
130
131std::size_t scriptComponentStoreSize() { return scriptComponentStore().size(); }
132
133std::uint64_t scriptComponentStoreRevision() { return scriptComponentRevisionCounter(); }
134
135} // namespace eve
std::uint64_t revision
std::unordered_map< std::string, QuestRuntime > entries
Stable, structured diagnostics shared by engine modules.
std::vector< Value > defaults
Definition ECS.cpp:49
SQObjectPtr key
Definition ECS.cpp:155
std::string name
Definition ECS.cpp:154
std::vector< std::string > fields
Definition ECS.cpp:42
std::string message
DiagnosticCode code
bool touched
std::string path
Definition PlayHost.cpp:111
std::set< std::string > values
Object::Ptr value
bool found
int removed
脚本组件字段的引擎侧权威存储。
ScriptStructure out
uint32_t index
static Diagnostic error(DiagnosticCode code, std::string message, std::string path={})
Construct an error diagnostic with the standard error severity.
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
static Status success(StatusCode code=StatusCode::Ok)
Construct a successful status with an explicit non-error outcome.
Definition Status.cpp:19
The canonical owning dynamic value used by data-facing protocols.
Definition Value.h:31
std::map< std::string, Value > Object
Definition Value.h:34
bool isObject() const noexcept
Return true when this value is an object.
Definition Value.h:110
Value * find(const std::string &key) noexcept
Return an object member, or nullptr when absent.
Definition Value.cpp:244
eve::Diagnostic Diagnostic
Build metadata (engine git commit, build time, third-party version).
Definition Build.cpp:16
std::size_t removeScriptComponentValues(std::uint64_t entityId)
实体销毁时清掉它的全部组件数据。
DiagnosticCode
Stable machine-readable diagnostic codes.
Definition Diagnostic.h:47
std::size_t scriptComponentStoreSize()
当前数据条数(诊断与测试用)。
const Value * findScriptComponentValues(const ScriptComponentStoreKey &key)
读一条组件数据。
void replaceScriptComponentValues(std::vector< ScriptComponentStoreEntry > entries)
用给定条目**整体替换**存储(恢复快照用)。
Result< void > setScriptComponentValues(const ScriptComponentStoreKey &key, Value values)
写入一条组件的引擎侧数据(创建实体、或迁移到新代时调用)。
std::uint64_t scriptComponentStoreRevision()
存储的单调版本号:任何一次写(含恢复、换代对齐、实体销毁)都会 +1。
Result< void > setScriptComponentField(const ScriptComponentStoreKey &key, const std::string &field, Value value)
写一个字段(脚本侧的写入与原生侧的写入都走这里)。
void clearScriptComponentStore()
换 VM 时清空:脚本世界连同它的组件数据一起消失。
std::vector< ScriptComponentStoreEntry > scriptComponentStoreEntries()
列出存储里的全部条目,按 (实体, 组件名, 版本) 升序。
void reconcileScriptComponentValues(const std::string &name, SchemaVersion version, const std::vector< std::string > &fields, const std::vector< Value > &defaults)
把某个组件(名字 + 版本)在**所有实体**上的数据重建到新 schema 上(热重载换代)。
Result< Value > scriptComponentField(const ScriptComponentStoreKey &key, const std::string &field)
读一个字段。
存储里的一条数据:键 + 一份字段对象。
一条脚本组件数据的键:实体 + 组件名 + 版本。