17 files changed
+451
-421
No files matched your search
@@ -0,0 +1,55 @@
|
||||
#include <Mods/Loader.hpp>
|
||||
#include <Agent.hpp>
|
||||
#include <dlfcn.h>
|
||||
#include <iostream>
|
||||
#include <stdexcept>
|
||||
|
||||
namespace vl::mod {
|
||||
|
||||
std::size_t ModLoader::loadModule(const std::string& path, const Context& ctx) {
|
||||
void* handle = dlopen(path.c_str(), RTLD_NOW | RTLD_GLOBAL);
|
||||
if (!handle) throw std::runtime_error(std::string("dlopen failed: ") + dlerror());
|
||||
handles_.push_back(handle);
|
||||
|
||||
const std::size_t before = Registry::all().size();
|
||||
Registry::addFromModule(handle);
|
||||
const std::size_t after = Registry::all().size();
|
||||
const std::size_t added = after - before;
|
||||
if (added > 0) {
|
||||
auto& all = Registry::mutableAll();
|
||||
for (std::size_t i = all.size() - added; i < all.size(); ++i) {
|
||||
auto& e = all[i];
|
||||
auto inst = e.factory(ctx);
|
||||
auto name = std::string(inst->name());
|
||||
auto version = std::string(inst->version());
|
||||
e.name = std::move(name);
|
||||
e.version = std::move(version);
|
||||
instances_.emplace_back(e.name, e.version, std::move(inst), handle);
|
||||
}
|
||||
}
|
||||
return added;
|
||||
}
|
||||
|
||||
void ModLoader::startAll() {
|
||||
auto& instance = vl::Agent::Agent::getInstance();
|
||||
for (auto& lm : instances_)
|
||||
{
|
||||
auto name = std::string(lm.mod().name());
|
||||
auto version = std::string(lm.mod().version());
|
||||
instance.m_logger->info("Starting {}, Version: {}", name, version);
|
||||
lm.mod().onStart();
|
||||
}
|
||||
}
|
||||
|
||||
void ModLoader::stopAll() {
|
||||
for (auto it = instances_.rbegin(); it != instances_.rend(); ++it) it->mod().onStop();
|
||||
}
|
||||
|
||||
void ModLoader::unloadAll() {
|
||||
stopAll();
|
||||
instances_.clear();
|
||||
for (auto* h : handles_) if (h) dlclose(h);
|
||||
handles_.clear();
|
||||
}
|
||||
|
||||
} // namespace vl::log
|
||||
Reference in new issue
Block a user