2026-02-28 20:56:11 +08:00
|
|
|
|
#include <module/module_registry.h>
|
|
|
|
|
|
#include <event/event_bus.h>
|
|
|
|
|
|
#include <algorithm>
|
|
|
|
|
|
|
|
|
|
|
|
namespace extra2d {
|
|
|
|
|
|
|
|
|
|
|
|
ModuleRegistry::ModuleRegistry() = default;
|
|
|
|
|
|
|
|
|
|
|
|
ModuleRegistry::~ModuleRegistry() {
|
|
|
|
|
|
if (inited_) {
|
|
|
|
|
|
shutdownAll();
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
ModuleRegistry::ModuleRegistry(ModuleRegistry&&) noexcept = default;
|
|
|
|
|
|
ModuleRegistry& ModuleRegistry::operator=(ModuleRegistry&&) noexcept = default;
|
|
|
|
|
|
|
|
|
|
|
|
void ModuleRegistry::registerModule(IModule* module) {
|
|
|
|
|
|
if (!module) {
|
|
|
|
|
|
return;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
const char* name = module->name();
|
2026-02-28 21:48:35 +08:00
|
|
|
|
if (!name) {
|
|
|
|
|
|
return; // 名称为空
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// 如果已存在同名模块,先注销旧的
|
|
|
|
|
|
if (moduleMap_.find(name) != moduleMap_.end()) {
|
|
|
|
|
|
unregisterModule(name);
|
2026-02-28 20:56:11 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
modules_.push_back(module);
|
|
|
|
|
|
moduleMap_[name] = module;
|
|
|
|
|
|
sorted_ = false;
|
|
|
|
|
|
|
|
|
|
|
|
// 模块注册事件(暂不发送,避免依赖 events.h)
|
|
|
|
|
|
// event::broadcast<events::OnModuleRegistered>(name, module->type());
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void ModuleRegistry::unregisterModule(const char* name) {
|
|
|
|
|
|
if (!name) {
|
|
|
|
|
|
return;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
auto it = moduleMap_.find(name);
|
|
|
|
|
|
if (it == moduleMap_.end()) {
|
|
|
|
|
|
return;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
IModule* module = it->second;
|
|
|
|
|
|
|
|
|
|
|
|
// 如果已初始化,先关闭
|
|
|
|
|
|
if (inited_ && module) {
|
|
|
|
|
|
module->shutdown();
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// 从列表中移除
|
|
|
|
|
|
modules_.erase(
|
|
|
|
|
|
std::remove(modules_.begin(), modules_.end(), module),
|
|
|
|
|
|
modules_.end()
|
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
|
|
moduleMap_.erase(it);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
IModule* ModuleRegistry::getModule(const char* name) const {
|
|
|
|
|
|
if (!name) {
|
|
|
|
|
|
return nullptr;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
auto it = moduleMap_.find(name);
|
|
|
|
|
|
return it != moduleMap_.end() ? it->second : nullptr;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
bool ModuleRegistry::hasModule(const char* name) const {
|
|
|
|
|
|
if (!name) {
|
|
|
|
|
|
return false;
|
|
|
|
|
|
}
|
|
|
|
|
|
return moduleMap_.find(name) != moduleMap_.end();
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
std::vector<IModule*> ModuleRegistry::getAllModules() const {
|
|
|
|
|
|
return modules_;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
std::vector<IModule*> ModuleRegistry::getModulesByType(ModuleType type) const {
|
|
|
|
|
|
std::vector<IModule*> result;
|
|
|
|
|
|
for (auto* module : modules_) {
|
|
|
|
|
|
if (module && module->type() == type) {
|
|
|
|
|
|
result.push_back(module);
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
return result;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
bool ModuleRegistry::initAll() {
|
|
|
|
|
|
if (inited_) {
|
|
|
|
|
|
return true;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// 按优先级排序
|
|
|
|
|
|
sortModules();
|
|
|
|
|
|
|
|
|
|
|
|
// 初始化所有模块
|
|
|
|
|
|
for (auto* module : modules_) {
|
|
|
|
|
|
if (module && !module->init()) {
|
|
|
|
|
|
// 初始化失败,关闭已初始化的模块
|
|
|
|
|
|
shutdownAll();
|
|
|
|
|
|
return false;
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
inited_ = true;
|
|
|
|
|
|
return true;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void ModuleRegistry::shutdownAll() {
|
|
|
|
|
|
if (!inited_) {
|
|
|
|
|
|
return;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// 按优先级逆序关闭
|
|
|
|
|
|
for (auto it = modules_.rbegin(); it != modules_.rend(); ++it) {
|
|
|
|
|
|
if (*it) {
|
|
|
|
|
|
(*it)->shutdown();
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
inited_ = false;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
size_t ModuleRegistry::getModuleCount() const {
|
|
|
|
|
|
return modules_.size();
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void ModuleRegistry::sortModules() {
|
|
|
|
|
|
if (sorted_) {
|
|
|
|
|
|
return;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// 按优先级排序(数值小的优先)
|
|
|
|
|
|
std::sort(modules_.begin(), modules_.end(),
|
|
|
|
|
|
[](IModule* a, IModule* b) {
|
|
|
|
|
|
if (!a || !b) return a < b;
|
|
|
|
|
|
return a->priority() < b->priority();
|
|
|
|
|
|
}
|
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
|
|
sorted_ = true;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
} // namespace extra2d
|