From c349359e71170c2fa78ac0fa22df07932ab47210 Mon Sep 17 00:00:00 2001 From: Jonathan Bradley Date: Tue, 5 Dec 2023 13:07:57 -0500 Subject: minor refactor for plugins --- src/plugins.cpp | 51 +++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 51 insertions(+) create mode 100644 src/plugins.cpp (limited to 'src/plugins.cpp') 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 +#include + +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(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); + } +} -- cgit v1.2.3