diff options
| author | Jonathan Bradley <jcb@pikum.xyz> | 2023-12-05 13:07:57 -0500 |
|---|---|---|
| committer | Jonathan Bradley <jcb@pikum.xyz> | 2023-12-05 13:07:57 -0500 |
| commit | c349359e71170c2fa78ac0fa22df07932ab47210 (patch) | |
| tree | 74795fee5b64fe94683c6078d3b50ec5112a4707 /src/plugins.cpp | |
| parent | 6e498a3781f5ccbbaa6cf5e407cf67722624760f (diff) | |
minor refactor for plugins
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); + } +} |
