diff --git a/CMakeLists.txt b/CMakeLists.txt index ff2e3b4..37c775c 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -67,6 +67,6 @@ endif() add_subdirectory(./src) if (SOLANACEAE_TOX_P2PRNG_BUILD_PLUGINS) - #add_subdirectory(./plugins) + add_subdirectory(./plugins) endif() diff --git a/plugins/CMakeLists.txt b/plugins/CMakeLists.txt new file mode 100644 index 0000000..eb81c1f --- /dev/null +++ b/plugins/CMakeLists.txt @@ -0,0 +1,16 @@ +cmake_minimum_required(VERSION 3.9...3.24 FATAL_ERROR) + +######################################## + +add_library(plugin_tox_p2prng MODULE + ./plugin_tox_p2prng.cpp +) +target_compile_features(plugin_tox_p2prng PUBLIC cxx_std_17) +set_target_properties(plugin_tox_p2prng PROPERTIES + C_VISIBILITY_PRESET hidden +) +target_link_libraries(plugin_tox_p2prng PUBLIC + solanaceae_tox_p2prng + solanaceae_plugin +) + diff --git a/plugins/plugin_tox_p2prng.cpp b/plugins/plugin_tox_p2prng.cpp new file mode 100644 index 0000000..cf20f53 --- /dev/null +++ b/plugins/plugin_tox_p2prng.cpp @@ -0,0 +1,62 @@ +#include + +#include +#include + +#include +#include +#include + +static std::unique_ptr g_tox_p2prng = nullptr; + +constexpr const char* plugin_name = "ToxP2PRNG"; + +extern "C" { + +SOLANA_PLUGIN_EXPORT const char* solana_plugin_get_name(void) { + return plugin_name; +} + +SOLANA_PLUGIN_EXPORT uint32_t solana_plugin_get_version(void) { + return SOLANA_PLUGIN_VERSION; +} + +SOLANA_PLUGIN_EXPORT uint32_t solana_plugin_start(struct SolanaAPI* solana_api) { + std::cout << "PLUGIN " << plugin_name << " START()\n"; + + if (solana_api == nullptr) { + return 1; + } + + try { + auto* tox_i = PLUG_RESOLVE_INSTANCE(ToxI); + auto* tep_i = PLUG_RESOLVE_INSTANCE(ToxEventProviderI); + auto* tcm = PLUG_RESOLVE_INSTANCE(ToxContactModel2); + + // static store, could be anywhere tho + // construct with fetched dependencies + g_tox_p2prng = std::make_unique(*tox_i, *tep_i, *tcm); + + // register types + PLUG_PROVIDE_INSTANCE(ToxP2PRNG, plugin_name, g_tox_p2prng.get()); + PLUG_PROVIDE_INSTANCE(P2PRNGI, plugin_name, g_tox_p2prng.get()); + } catch (const ResolveException& e) { + std::cerr << "PLUGIN " << plugin_name << " " << e.what << "\n"; + return 2; + } + + return 0; +} + +SOLANA_PLUGIN_EXPORT void solana_plugin_stop(void) { + std::cout << "PLUGIN " << plugin_name << " STOP()\n"; + + g_tox_p2prng.reset(); +} + +SOLANA_PLUGIN_EXPORT float solana_plugin_tick(float) { + return std::numeric_limits::max(); +} + +} // extern C + diff --git a/src/solanaceae/tox_p2prng/tox_p2prng.cpp b/src/solanaceae/tox_p2prng/tox_p2prng.cpp index 6ece3fa..ab15672 100644 --- a/src/solanaceae/tox_p2prng/tox_p2prng.cpp +++ b/src/solanaceae/tox_p2prng/tox_p2prng.cpp @@ -50,6 +50,14 @@ std::vector ToxP2PRNG::newGernationPeers(const std::vector