diff options
Diffstat (limited to 'src/plugins.cpp')
| -rw-r--r-- | src/plugins.cpp | 51 |
1 files changed, 51 insertions, 0 deletions
diff --git a/src/plugins.cpp b/src/plugins.cpp new file mode 100644 index 0000000..aa7efd5 --- /dev/null +++ b/src/plugins.cpp @@ -0,0 +1,51 @@ + +#include "macros.hpp" +#include "plugins.hpp" + +#include <cstdio> +#include <dlfcn.h> + +PKEPluginInterface pkePlugin{}; + +void *loadedPlugin = nullptr; + +void Pke_UpdatePlugin(const PKEPluginInterface &plugin) { + pkePlugin.OnInit = plugin.OnInit; + pkePlugin.OnTick = plugin.OnTick; + pkePlugin.OnTeardown = plugin.OnTeardown; + pkePlugin.OnImGuiRender = plugin.OnImGuiRender; +} + +void Pke_LoadPlugin(const char *path) { + if (loadedPlugin != nullptr || loadedPlugin != CAFE_BABE(void)) { + return; + } + void *extension = dlopen(path, RTLD_NOW); + char *err = dlerror(); + if (!extension) { + fprintf(stderr, "Given plugin library (%s) failed to load: %s\n", path, err); + return; + } + PKEPluginInterface *interface = reinterpret_cast<PKEPluginInterface *>(dlsym(extension, "pkePluginInterface")); + err = dlerror(); + if (err != NULL) { + fprintf(stderr, "Given plugin library (%s) did not contain 'pkePluginInterface': %s\n", path, err); + dlclose(extension); + return; + } + Pke_UpdatePlugin(*interface); + loadedPlugin = interface; +} + +void Pke_UnloadPlugin() { + pkePlugin = { + .OnInit = nullptr, + .OnTick= nullptr, + .OnTeardown= nullptr, + .OnImGuiRender= nullptr, + }; + if (loadedPlugin != nullptr && loadedPlugin != CAFE_BABE(void)) { + dlclose(loadedPlugin); + loadedPlugin = CAFE_BABE(void); + } +} |
