载入中...
搜索中...
未找到
ModuleRegistry.cpp
浏览该文件的文档.
2#include "common/Assert.h"
3#include "common/Module.h"
4
5#include <algorithm>
6
7namespace eve {
8ModuleManager::ModuleManager() : state_(std::make_unique<State>()) {}
9ModuleManager::~ModuleManager() = default;
10
11ModuleManager& ModuleManager::inst() {
12 static ModuleManager instance;
13 return instance;
14}
15
16Module* ModuleManager::find(const char* name) {
17 EV_PARAM_CHECK(name != nullptr, "module name must not be null");
18 auto p = inst().state_->registered_modules.find(name);
19 if (p == inst().state_->registered_modules.end() || p->second.instance == nullptr) return nullptr;
20 return p->second.instance;
21}
22
23Module* ModuleManager::find(const char* name, creator_t factory) {
24 EV_PARAM_CHECK(name != nullptr, "module name must not be null");
25 auto& state = *inst().state_;
26 auto current = state.registered_modules.find(name);
27 if (current != state.registered_modules.end() && current->second.creator == factory)
28 return current->second.instance;
29 auto previous = state.plugin_registration_previous_.find(name);
30 if (previous != state.plugin_registration_previous_.end() && previous->second.creator == factory)
31 return previous->second.instance;
32 for (auto& retired : state.retired_modules_)
33 if (retired.first == name && retired.second.creator == factory) return retired.second.instance;
34 return nullptr;
35}
36
37void ModuleManager::insert(const char* name, Module* instance, creator_t factory) {
38 EV_PARAM_CHECK(name != nullptr, "module name must not be null");
39 EV_PARAM_CHECK(instance != nullptr, "module instance must not be null");
40 auto& state = *inst().state_;
41 auto current = state.registered_modules.find(name);
42 if (current != state.registered_modules.end() && current->second.creator == factory) {
43 current->second.instance = instance;
44 return;
45 }
46 auto previous = state.plugin_registration_previous_.find(name);
47 if (previous != state.plugin_registration_previous_.end() && previous->second.creator == factory) {
48 previous->second.instance = instance;
49 return;
50 }
51 for (auto& retired : state.retired_modules_) {
52 if (retired.first == name && retired.second.creator == factory) {
53 retired.second.instance = instance;
54 return;
55 }
56 }
57 EV_ASSERT(false, "factory generation must be registered before instance creation");
58}
59
60Module* ModuleManager::getInstanceRaw(const char* name) {
61 EV_PARAM_CHECK(name != nullptr, "module name must not be null");
62 return inst().state_->registered_modules[name].instance;
63}
64
65Module* ModuleManager::requireInstanceRaw(const char* name) {
66 EV_PARAM_CHECK(name != nullptr, "module name must not be null");
67 auto& module = inst().state_->registered_modules[name];
68 if (module.instance) return module.instance;
69 return module.creator();
70}
71
72void ModuleManager::insert(const char* name, Module* instance) {
73 EV_PARAM_CHECK(name != nullptr, "module name must not be null");
74 EV_PARAM_CHECK(instance != nullptr, "module instance must not be null");
75 inst().state_->registered_modules[name].instance = instance;
76}
77
78void ModuleManager::requireAll() {
79 for (auto& entry : inst().state_->registered_modules) {
80 if (!entry.second.instance && entry.second.creator) entry.second.instance = entry.second.creator();
81 }
82}
83
84void ModuleManager::register_module(const char* name, creator_t c, exposer_t e) {
85 EV_PARAM_CHECK(name != nullptr, "module name must not be null");
86 const bool hasCreator = c != nullptr; // function pointers can't be printed by zeroerr
87 EV_PARAM_CHECK(hasCreator, "module creator must not be null");
88 auto p = inst().state_->registered_modules.find(name);
89 if (inst().state_->plugin_registration_active_ && p != inst().state_->registered_modules.end()) {
90 auto& state = *inst().state_;
91 if (state.plugin_registration_owner_.empty() || p->second.owner != state.plugin_registration_owner_ ||
92 std::find(state.plugin_registration_added_.begin(), state.plugin_registration_added_.end(), name) !=
93 state.plugin_registration_added_.end()) {
94 if (state.plugin_registration_error_.empty())
95 state.plugin_registration_error_ = std::string("module name already registered: ") + name;
96 return;
97 }
98 state.plugin_registration_previous_.emplace(name, p->second);
99 state.plugin_registration_added_.emplace_back(name);
100 p->second = {c, e, nullptr, false, state.plugin_registration_owner_};
101 return;
102 }
103 if (p == inst().state_->registered_modules.end()) {
104 inst().state_->registered_modules.insert(std::make_pair(
105 std::string(name), State::ModuleInfo{c, e, nullptr, false, inst().state_->plugin_registration_owner_}));
106 if (inst().state_->plugin_registration_active_) inst().state_->plugin_registration_added_.emplace_back(name);
107 } else {
108 p->second = {c, e, nullptr, false, {}};
109 }
110}
111
112eve::Result<void> ModuleManager::beginPluginRegistration() { return beginPluginRegistration({}); }
113
114eve::Result<void> ModuleManager::beginPluginRegistration(std::string owner) {
115 auto& manager = inst();
116 if (manager.state_->plugin_registration_active_)
118 eve::DiagnosticCode::Conflict, "a plugin registration transaction is already active", {}, {}, "module"));
119 manager.state_->plugin_registration_active_ = true;
120 manager.state_->plugin_registration_added_.clear();
121 manager.state_->plugin_registration_error_.clear();
122 manager.state_->plugin_registration_owner_ = std::move(owner);
123 manager.state_->plugin_registration_previous_.clear();
125}
126
127eve::Result<void> ModuleManager::finishPluginRegistration(bool commit) {
128 auto& manager = inst();
129 if (!manager.state_->plugin_registration_active_)
131 eve::DiagnosticCode::Conflict, "no plugin registration transaction is active", {}, {}, "module"));
132
133 std::string error = manager.state_->plugin_registration_error_;
134 if (commit && error.empty() && !manager.state_->plugin_registration_owner_.empty()) {
135 // Reserve retirement slots before publishing script bindings.
136 manager.state_->retired_modules_.reserve(manager.state_->retired_modules_.size() +
137 manager.state_->registered_modules.size());
138 auto published = publishPluginBindings();
139 if (!published.ok()) error = published.status().describe();
140 }
141 if (!commit || !error.empty()) {
142 for (const auto& name : manager.state_->plugin_registration_added_) {
143 auto it = manager.state_->registered_modules.find(name);
144 if (it == manager.state_->registered_modules.end()) continue;
145 delete it->second.instance;
146 manager.state_->registered_modules.erase(it);
147 }
148 for (auto& previous : manager.state_->plugin_registration_previous_)
149 manager.state_->registered_modules.emplace(previous.first, std::move(previous.second));
150 } else if (!manager.state_->plugin_registration_owner_.empty()) {
151 for (auto& previous : manager.state_->plugin_registration_previous_)
152 manager.state_->retired_modules_.emplace_back(previous.first, std::move(previous.second));
153 // A module removed by the new binary disappears from the current registry,
154 // while its captured constructors and live instances keep their generation.
155 auto& modules = manager.state_->registered_modules;
156 for (auto it = modules.begin(); it != modules.end();) {
157 if (it->second.owner == manager.state_->plugin_registration_owner_ &&
158 std::find(manager.state_->plugin_registration_added_.begin(),
159 manager.state_->plugin_registration_added_.end(),
160 it->first) == manager.state_->plugin_registration_added_.end()) {
161 manager.state_->retired_modules_.emplace_back(it->first, std::move(it->second));
162 it = modules.erase(it);
163 } else {
164 ++it;
165 }
166 }
167 }
168 manager.state_->plugin_registration_active_ = false;
169 manager.state_->plugin_registration_added_.clear();
170 manager.state_->plugin_registration_error_.clear();
171 manager.state_->plugin_registration_previous_.clear();
172 manager.state_->plugin_registration_owner_.clear();
173 if (!error.empty())
177}
178
179Runtime* ModuleManager::runtime() { return inst().state_->active_runtime_; }
180
181void ModuleManager::detach(Runtime* runtime) {
182 if (inst().state_->active_runtime_ == runtime) {
183 inst().state_->active_runtime_ = nullptr;
184 inst().state_->active_vm_ = nullptr;
185 }
186}
187
188ModuleRegister::ModuleRegister(const char* name, ModuleManager::creator_t c, ModuleManager::exposer_t e) {
189 ModuleManager::register_module(name, c, e);
190}
191
192
193} // namespace eve
EVEngine assertion entry point, backed by zeroerr.
#define EV_ASSERT(cond,...)
Assert an internal engine invariant (state that must always hold).
Definition Assert.h:37
#define EV_PARAM_CHECK(cond,...)
Validate a function parameter / public API precondition.
Definition Assert.h:30
std::vector< std::unique_ptr< ScriptClimbingEventSink > > retired
glm::vec4 p[6]
std::string name
Definition ECS.cpp:154
std::int32_t c
std::shared_ptr< OceanGpuSpectrum > owner
std::shared_ptr< const OceanSprayFrame > previous
std::string error
Definition Package.cpp:174
double current
static Diagnostic error(DiagnosticCode code, std::string message, std::string path={})
Construct an error diagnostic with the standard error severity.
EVENGINE_API_FOUNDATION public API.
Definition Module.h:57
EVENGINE_API_FOUNDATION public API.
Definition Module.h:48
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
EVENGINE_API_FOUNDATION public API.
Definition Runtime.h:189
static Status success(StatusCode code=StatusCode::Ok)
Construct a successful status with an explicit non-error outcome.
Definition Status.cpp:19
State
Lifecycle state of a generic transaction plan.
Build metadata (engine git commit, build time, third-party version).
Definition Build.cpp:16