From aedd3e1692e9be3ed9cc81c9a3398177f805f37a Mon Sep 17 00:00:00 2001 From: faumaray Date: Mon, 22 Jun 2026 21:40:42 +0300 Subject: [PATCH] Expose Core as a Void::Plugin (C ABI + v2 state), additive Add a CorePlugin adapter (IS-A Void::Plugin::IPlugin and IS-A Core, hence Api::ICoreInterface) and export the void_plugin_* C ABI from Export.cpp: create/destroy/info plus the v2 state protocol (serialize/deserialize routed through Core's already-Void::Json prepareForHotReload/restoreAfterHotReload). The host recovers the bot interface by dynamic_cast-ing the IPlugin* back to ICoreInterface*. Additive: the legacy CreateCore/DestroyCore exports stay, so the current dylib CoreLoader keeps working while the host migrates to Void::Plugin::PluginManager. Verified via a dlopen smoke test (symbols resolve; info/version correct; create/serialize/deserialize/destroy safe on an uninitialized Core). The live hot-reload + state-survival test under TeamSpeak is the remaining gate for the host-side loader swap. Link Void::Plugin. Co-Authored-By: Claude Opus 4.8 --- CMakeLists.txt | 1 + src/Export.cpp | 90 ++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 91 insertions(+) diff --git a/CMakeLists.txt b/CMakeLists.txt index a4e033e..dfa6973 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -108,6 +108,7 @@ target_link_libraries(EchoionCore Void::Net Void::Sqlite Void::Service + Void::Plugin PRIVATE Threads::Threads qjs diff --git a/src/Export.cpp b/src/Export.cpp index 176edc8..c61668b 100644 --- a/src/Export.cpp +++ b/src/Export.cpp @@ -1,6 +1,10 @@ #include #include "echoion/core/Core.hpp" +#include +#include +#include #include +#include namespace Echoion::Api { @@ -17,3 +21,89 @@ namespace Echoion::Api } } // namespace Echoion::Api + +namespace Echoion::Core +{ + // Adapter that makes Core loadable through Void::Plugin. It IS-A + // Void::Plugin::IPlugin (the type the loader manages) and IS-A Core (hence + // Api::ICoreInterface), so the host recovers the bot interface by + // dynamic_cast-ing the IPlugin* back to Api::ICoreInterface*. + class CorePlugin final : public Void::Plugin::IPlugin, public Core + { + public: + + [[nodiscard]] std::string_view name() const noexcept override + { + return "Echoion"; + } + }; +} // namespace Echoion::Core + +// ── Void::Plugin C ABI ────────────────────────────────────────────────────── +// Exposes Core as a Void plugin with the v2 state protocol (serialize/ +// deserialize via the already-Void::Json hot-reload path). The legacy +// CreateCore/DestroyCore exports above remain for the current dylib CoreLoader; +// these are additive and let the host migrate to Void::Plugin::PluginManager. +// +// NOTE: the live hot-reload + state-survival behaviour is only exercised +// against a running TeamSpeak host — compile-verified here, runtime-gated there. +extern "C" +{ + void* void_plugin_create() + { + return static_cast( + new Echoion::Core::CorePlugin()); + } + + void void_plugin_destroy(void* instance) + { + delete static_cast(instance); + } + + Void::Plugin::PluginInfo void_plugin_info() + { + return Void::Plugin::PluginInfo{ + .abi_version = Void::Plugin::ABI_VERSION, + .name = "Echoion", + .plugin_version = Echoion::Api::VERSION_STRING, + .description = "Echoion TeamSpeak music bot core"}; + } + + // v2 state protocol — survives layout changes across plugin versions. + std::uint32_t void_plugin_state_version() + { + return 1; // matches StateSerializer::CURRENT_VERSION + } + + const char* void_plugin_serialize_state(void* instance) + { + auto* core = dynamic_cast( + static_cast(instance)); + if (!core) { + return nullptr; + } + auto state = core->prepareForHotReload(); + if (!state) { + return nullptr; + } + // Heap copy owned by this .so; released via void_plugin_free_string. + return strdup(state->c_str()); + } + + void void_plugin_free_string(const char* s) + { + std::free(const_cast(s)); + } + + int void_plugin_deserialize_state(void* instance, const char* json, + std::uint32_t /*from_version*/) + { + auto* core = dynamic_cast( + static_cast(instance)); + if (!core || !json) { + return -1; + } + auto result = core->restoreAfterHotReload(std::string(json)); + return result ? 0 : -1; + } +}