#include #include #include #include #include #include namespace vl::mod { std::size_t ModLoader::loadModule(const std::string& path, const Context& ctx, bool isGlobal) { void* handle = LoadModLibrary(path, isGlobal); if (!handle) throw std::runtime_error(std::string("dlopen failed: ")); 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) CloseModLibrary(h); handles_.clear(); } } // namespace vl::log