File: plugin.cpp

package info (click to toggle)
satdump 1.2.2%2Bgb79af48-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 81,648 kB
  • sloc: cpp: 276,768; ansic: 164,598; lisp: 1,219; sh: 283; xml: 106; makefile: 7
file content (110 lines) | stat: -rw-r--r-- 3,739 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
#define SATDUMP_DLL_EXPORT 1
#include "plugin.h"
#ifdef _WIN32
#include "libs/dlfcn/dlfcn.h"
#else
#include <dlfcn.h>
#endif
#include "logger.h"
#include <filesystem>
#include "satdump_vars.h"
#include "core/exception.h"

std::shared_ptr<satdump::Plugin> loadPlugin(std::string plugin)
{
    logger->trace("Loading plugin " + plugin + "...");

    void *dynlib = dlopen(plugin.c_str(), RTLD_LAZY);
    if (!dynlib)
        throw satdump_exception("Error loading " + plugin + "! Error : " + std::string(dlerror()));

    void *create = dlsym(dynlib, "loader");
    const char *dlsym_error = dlerror();
    if (dlsym_error != NULL)
        logger->warn("Possible error loading symbols from plugin!");

    satdump::Plugin *pluginObject = reinterpret_cast<satdump::Plugin *(*)()>(create)();
    pluginObject->init();
    logger->trace("Plugin " + pluginObject->getID() + " loaded!");

    return std::shared_ptr<satdump::Plugin>(pluginObject);
}

namespace satdump
{
    SATDUMP_DLL std::map<std::string, std::shared_ptr<satdump::Plugin>> loaded_plugins;
    SATDUMP_DLL std::shared_ptr<EventBus> eventBus = std::make_shared<EventBus>();
    SATDUMP_DLL std::shared_ptr<TaskScheduler> taskScheduler = std::make_shared<TaskScheduler>();
};

#ifdef __ANDROID__
std::string android_plugins_dir = "";
#endif

void loadPlugins(std::map<std::string, std::shared_ptr<satdump::Plugin>> &loaded_plugins)
{
    // Get possible paths
    std::vector<std::string> plugins_paths;
#ifdef __ANDROID__
    plugins_paths.push_back(android_plugins_dir + "/");
#else
    if (std::filesystem::exists("plugins"))
        plugins_paths.push_back("./plugins");              // Try local plugins directory first
    plugins_paths.push_back(satdump::LIBPATH + "plugins"); // Followed by system
#endif

    // Get platform file extensions
#if defined(_WIN32)
    std::filesystem::path extension = ".dll";
#elif defined(__APPLE__)
    std::filesystem::path extension = ".dylib";
#else
    std::filesystem::path extension = ".so";
#endif

    // Load all plugins
    for (std::string &plugins_path : plugins_paths)
    {
        logger->info("Loading plugins from " + plugins_path);

        std::filesystem::recursive_directory_iterator pluginIterator(plugins_path);
        std::error_code iteratorError;
        while (pluginIterator != std::filesystem::recursive_directory_iterator())
        {
            std::string path = pluginIterator->path().string();
            if (!std::filesystem::is_regular_file(pluginIterator->path()) || pluginIterator->path().extension() != extension)
                goto skip_this;

#ifdef __ANDROID__
            if (path.find("libandroid_imgui.so") != std::string::npos ||
                path.find("libsatdump_core.so") != std::string::npos ||
                path.find("libsatdump_interface.so") != std::string::npos)
                goto skip_this;
#endif
            try
            {
                std::shared_ptr<satdump::Plugin> pl = loadPlugin(path);
                loaded_plugins.insert({pl->getID(), pl});
            }
            catch (std::runtime_error &e)
            {
                logger->error(e.what());
            }

        skip_this:
            pluginIterator.increment(iteratorError);
            if (iteratorError)
                logger->critical(iteratorError.message());
        }

        if (loaded_plugins.size() > 0)
        {
            logger->debug("Loaded plugins (" + std::to_string(loaded_plugins.size()) + ") : ");
            for (std::pair<const std::string, std::shared_ptr<satdump::Plugin>> &it : loaded_plugins)
                logger->debug(" - " + it.first);
            break;
        }
        else
            logger->warn("No Plugins in " + plugins_path + "!");
    }
}