SDRPlusPlus/core/src/module.cpp

104 lines
3.4 KiB
C++
Raw Normal View History

2020-08-07 14:29:06 +02:00
#include <module.h>
2020-09-20 00:19:39 +02:00
#include <signal_path/vfo_manager.h>
#include <gui/main_window.h>
#include <signal_path/audio.h>
2020-08-07 14:29:06 +02:00
namespace mod {
std::map<std::string, Module_t> modules;
std::vector<std::string> moduleNames;
2020-08-12 16:43:44 +02:00
ImGui::WaterFall* _wtf;
2020-08-07 14:29:06 +02:00
void loadModule(std::string path, std::string name) {
if (!std::filesystem::exists(path)) {
spdlog::error("{0} does not exist", path);
return;
}
if (!std::filesystem::is_regular_file(path)) {
spdlog::error("{0} isn't a loadable module", path);
return;
}
Module_t mod;
#ifdef _WIN32
mod.inst = LoadLibraryA(path.c_str());
if (mod.inst == NULL) {
spdlog::error("Couldn't load {0}.", name);
return;
}
2020-10-01 13:46:12 +02:00
mod._INIT_ = (void(*)())GetProcAddress(mod.inst, "_INIT_");
mod._CREATE_INSTANCE_ = (void*(*)(std::string))GetProcAddress(mod.inst, "_CREATE_INSTANCE_");
mod._DELETE_INSTANCE_ = (void(*)(void*))GetProcAddress(mod.inst, "_DELETE_INSTANCE_");
2020-10-02 01:44:18 +02:00
mod._STOP_ = (void(*)())GetProcAddress(mod.inst, "_STOP_");
2020-08-07 14:29:06 +02:00
#else
2020-08-11 18:33:42 +02:00
mod.inst = dlopen(path.c_str(), RTLD_LAZY);
if (mod.inst == NULL) {
spdlog::error("Couldn't load {0}.", name);
return;
}
2020-10-01 13:46:12 +02:00
mod._INIT_ = (void(*)())dlsym(mod.inst, "_INIT_");
mod._CREATE_INSTANCE_ = (void*(*)(std::string))dlsym(mod.inst, "_CREATE_INSTANCE_");
mod._DELETE_INSTANCE_ = (void(*)(void*))dlsym(mod.inst, "_DELETE_INSTANCE_");
mod._STOP_ = (void(*)())dlsym(mod.inst, "_STOP_");
2020-08-07 14:29:06 +02:00
#endif
if (mod._INIT_ == NULL) {
spdlog::error("Couldn't load {0} because it's missing _INIT_.", name);
return;
}
2020-10-01 13:46:12 +02:00
if (mod._CREATE_INSTANCE_ == NULL) {
spdlog::error("Couldn't load {0} because it's missing _CREATE_INSTANCE_.", name);
2020-08-12 16:43:44 +02:00
return;
}
2020-10-01 13:46:12 +02:00
if (mod._DELETE_INSTANCE_ == NULL) {
spdlog::error("Couldn't load {0} because it's missing _DELETE_INSTANCE_.", name);
2020-08-11 18:33:42 +02:00
return;
}
2020-08-07 14:29:06 +02:00
if (mod._STOP_ == NULL) {
spdlog::error("Couldn't load {0} because it's missing _STOP_.", name);
return;
}
2020-10-01 13:46:12 +02:00
if (!isLoaded(mod.inst)) {
mod._INIT_();
}
mod.ctx = mod._CREATE_INSTANCE_(name);
2020-08-07 14:29:06 +02:00
if (mod.ctx == NULL) {
spdlog::error("{0} Failed to initialize.", name);
}
2020-10-01 13:46:12 +02:00
2020-08-07 14:29:06 +02:00
modules[name] = mod;
moduleNames.push_back(name);
}
2020-08-11 18:33:42 +02:00
2020-08-12 16:43:44 +02:00
void loadFromList(std::string path) {
if (!std::filesystem::exists(path)) {
spdlog::error("Module list file does not exist");
return;
}
if (!std::filesystem::is_regular_file(path)) {
spdlog::error("Module list file isn't a file...");
return;
}
std::ifstream file(path.c_str());
json data;
2020-09-20 01:36:25 +02:00
file >> data;
2020-08-12 16:43:44 +02:00
file.close();
std::map<std::string, std::string> list = data.get<std::map<std::string, std::string>>();
for (auto const& [name, file] : list) {
spdlog::info("Loading {0} ({1})", name, file);
loadModule(file, name);
}
}
2020-10-01 13:46:12 +02:00
bool isLoaded(void* handle) {
for (auto const& [name, module] : modules) {
if (module.inst == handle) {
return true;
}
}
return false;
}
2020-08-07 14:29:06 +02:00
};