载入中...
搜索中...
未找到
Module.cpp
浏览该文件的文档.
1#include "common/Module.h"
2#include "common/Assert.h"
3#include "common/ECS.h"
4#include "common/Runtime.h"
5
6#include <simplesquirrel/simplesquirrel.hpp>
7
8namespace eve
9{
10
11
13 static ModuleManager instance;
14 return instance;
15}
16
18 EV_PARAM_CHECK(name != nullptr, "module name must not be null");
19 auto p = inst().registered_modules.find(name);
20 if (p == inst().registered_modules.end() || p->second.instance == nullptr) return nullptr;
21 return p->second.instance;
22}
23
24void ModuleManager::insert(const char* name, Module* instance) {
25 EV_PARAM_CHECK(name != nullptr, "module name must not be null");
26 EV_PARAM_CHECK(instance != nullptr, "module instance must not be null");
27 inst().registered_modules[name].instance = instance;
28}
29
30void ModuleManager::register_module(const char* name, creator_t c, exposer_t e) {
31 EV_PARAM_CHECK(name != nullptr, "module name must not be null");
32 const bool hasCreator = c != nullptr; // function pointers can't be printed by zeroerr
33 EV_PARAM_CHECK(hasCreator, "module creator must not be null");
34 auto p = inst().registered_modules.find(name);
35 if (p == inst().registered_modules.end()) {
36 inst().registered_modules.insert(
37 std::make_pair(std::string(name),
38 ModuleInfo{c, e, nullptr}));
39 } else {
40 p->second = {c, e, nullptr};
41 }
42}
43
45 ssq::Table table;
46 // Runtime::initialize() guarantees expose() runs once, so addTable never
47 // sees an existing "eve" slot. Avoid find()+catch here: ASYNCIFY-wrapped
48 // frames can drop the catch and leak NotFoundException to the JS boundary.
49 table = vm.addTable("eve");
50 for (auto& D : inst().registered_modules) {
51 if (D.second.exposer) D.second.exposer(table);
52 D.second.exposed = true;
53 }
54 // After modules: script ECS owns eve.Component / Entity / System / view.
55 exposeECS(table);
56}
57
62
63void ModuleManager::expose(ssq::VM& vm) {
64 inst().active_runtime_ = nullptr;
65 exposeVM(vm);
66}
67
69
72 return active ? &active->vm() : nullptr;
73}
74
76 if (inst().active_runtime_ == runtime) inst().active_runtime_ = nullptr;
77}
78
81 if (!active)
82 return -1;
83 int count = 0;
84 try {
85 auto scope = active->enter();
86 auto stack = active->guard();
87 ssq::Table table = active->table("eve");
88 for (auto& D : inst().registered_modules) {
89 if (D.second.exposed || !D.second.exposer)
90 continue;
91 D.second.exposer(table);
92 D.second.exposed = true;
93 ++count;
94 }
95 } catch (...) {
96 return -1;
97 }
98 return count;
99}
100
105
106
107} // namespace eve
EVEngine assertion entry point, backed by zeroerr.
#define EV_PARAM_CHECK(cond,...)
Validate a function parameter / public API precondition.
Definition Assert.h:30
bool active
Definition CardTypes.cpp:34
HSQUIRRELVM vm
Definition ECS.cpp:20
JobScope scope
uint32_t c
glm::vec4 p[6]
const char * name
Definition RockMesh.cpp:21
std::unordered_map< std::string, ModuleInfo > registered_modules
Definition Module.h:101
Module *(* creator_t)()
Definition Module.h:51
static void detach(Runtime *runtime)
Clears the active runtime if it matches; called during shutdown.
Definition Module.cpp:75
static void exposeVM(ssq::VM &vm)
Definition Module.cpp:44
void(* exposer_t)(ssq::Table &)
Definition Module.h:52
static void register_module(const char *name, creator_t c, exposer_t e)
Registers a module factory (creator + script exposer) by name.
Definition Module.cpp:30
static Module * find(const char *name)
Finds a live module instance by name, or nullptr.
Definition Module.cpp:17
static Runtime * runtime()
Active runtime associated with the last expose() call, or nullptr.
Definition Module.cpp:68
static void expose(Runtime &runtime)
Exposes every registered module into the given runtime's root table.
Definition Module.cpp:58
Runtime * active_runtime_
Definition Module.h:102
static int expose_pending()
Definition Module.cpp:79
static ModuleManager & inst()
Singleton registry holding every registered engine module.
Definition Module.cpp:12
static ssq::VM * vm()
Script VM of the active runtime, or nullptr.
Definition Module.cpp:70
static void insert(const char *name, Module *inst)
Stores a module instance under a name (used by Module_IMPL).
Definition Module.cpp:24
Scope enter()
Pushes this runtime on the thread-local runtime stack (RAII pop).
Definition Runtime.h:178
ssq::VM & vm() noexcept
Underlying SimpleSquirrel VM (requires a live, initialized runtime).
Definition Runtime.cpp:195
Definition Build.cpp:11
void exposeECS(ssq::Table &table)
脚本侧 ECS:Entity / Component / System / ShaderSystem / view(见 ECS.cpp)。 C++ 游戏实体仍直接用 ECS....
Definition ECS.cpp:562
ModuleRegister(const char *name, ModuleManager::creator_t c, ModuleManager::exposer_t e)
Definition Module.cpp:101