From 998794a6dd8faaa991a962d562f61ead7cd0b96b Mon Sep 17 00:00:00 2001 From: Green Sky Date: Thu, 18 Jan 2024 18:24:52 +0100 Subject: [PATCH] update to new versioned plugin api --- external/CMakeLists.txt | 2 +- plugins/plugin_crdtnotes.cpp | 47 +++++++---------- plugins/plugin_crdtnotes_imgui.cpp | 61 +++++++--------------- plugins/plugin_crdtnotes_toxsync.cpp | 75 +++++++--------------------- 4 files changed, 55 insertions(+), 130 deletions(-) diff --git a/external/CMakeLists.txt b/external/CMakeLists.txt index b6b9e37..029134d 100644 --- a/external/CMakeLists.txt +++ b/external/CMakeLists.txt @@ -13,7 +13,7 @@ endif() if (NOT TARGET imgui) FetchContent_Declare(imgui GIT_REPOSITORY https://github.com/ocornut/imgui.git - GIT_TAG d4ddc46e7 + GIT_TAG d6cb3c9 # v1.90.1 EXCLUDE_FROM_ALL ) diff --git a/plugins/plugin_crdtnotes.cpp b/plugins/plugin_crdtnotes.cpp index 536a186..247244d 100644 --- a/plugins/plugin_crdtnotes.cpp +++ b/plugins/plugin_crdtnotes.cpp @@ -2,21 +2,19 @@ #include #include -//#include #include #include -#define RESOLVE_INSTANCE(x) static_cast(solana_api->resolveInstance(#x)) -#define PROVIDE_INSTANCE(x, p, v) solana_api->provideInstance(#x, p, static_cast(v)) - static std::unique_ptr g_crdtn = nullptr; static std::unique_ptr g_crdtns = nullptr; +constexpr const char* plugin_name = "CRDTNotes"; + extern "C" { SOLANA_PLUGIN_EXPORT const char* solana_plugin_get_name(void) { - return "CRDTNotes"; + return plugin_name; } SOLANA_PLUGIN_EXPORT uint32_t solana_plugin_get_version(void) { @@ -24,44 +22,33 @@ SOLANA_PLUGIN_EXPORT uint32_t solana_plugin_get_version(void) { } SOLANA_PLUGIN_EXPORT uint32_t solana_plugin_start(struct SolanaAPI* solana_api) { - std::cout << "PLUGIN CRDTN START()\n"; + std::cout << "PLUGIN " << plugin_name << " START()\n"; if (solana_api == nullptr) { return 1; } - //ConfigModelI* conf = nullptr; - Contact3Registry* cr = nullptr; + try { + auto* cr = PLUG_RESOLVE_INSTANCE_VERSIONED(Contact3Registry, "1"); - { // make sure required types are loaded - //conf = RESOLVE_INSTANCE(ConfigModelI); - cr = RESOLVE_INSTANCE(Contact3Registry); + // static store, could be anywhere tho + // construct with fetched dependencies + g_crdtn = std::make_unique(); + g_crdtns = std::make_unique(*g_crdtn, *cr); - //if (conf == nullptr) { - //std::cerr << "PLUGIN CRDTN missing ConfigModelI\n"; - //return 2; - //} - - if (cr == nullptr) { - std::cerr << "PLUGIN CRDTNTS missing Contact3Registry\n"; - return 2; - } + // register types + PLUG_PROVIDE_INSTANCE(CRDTNotesSync, plugin_name, g_crdtns.get()); + PLUG_PROVIDE_INSTANCE(CRDTNotesEventI, plugin_name, g_crdtns.get()); + } catch (const ResolveException& e) { + std::cerr << "PLUGIN " << plugin_name << " " << e.what << "\n"; + return 2; } - // static store, could be anywhere tho - // construct with fetched dependencies - g_crdtn = std::make_unique(); - g_crdtns = std::make_unique(*g_crdtn, *cr); - - // register types - PROVIDE_INSTANCE(CRDTNotesSync, "CRDTNotes", g_crdtns.get()); - PROVIDE_INSTANCE(CRDTNotesEventI, "CRDTNotes", g_crdtns.get()); - return 0; } SOLANA_PLUGIN_EXPORT void solana_plugin_stop(void) { - std::cout << "PLUGIN CRDTN STOP()\n"; + std::cout << "PLUGIN " << plugin_name << " STOP()\n"; g_crdtn.reset(); g_crdtns.reset(); diff --git a/plugins/plugin_crdtnotes_imgui.cpp b/plugins/plugin_crdtnotes_imgui.cpp index 23f67af..3f2deb1 100644 --- a/plugins/plugin_crdtnotes_imgui.cpp +++ b/plugins/plugin_crdtnotes_imgui.cpp @@ -1,22 +1,20 @@ #include #include -//#include #include #include #include #include -#define RESOLVE_INSTANCE(x) static_cast(solana_api->resolveInstance(#x)) -#define PROVIDE_INSTANCE(x, p, v) solana_api->provideInstance(#x, p, static_cast(v)) - static std::unique_ptr g_crdtn_imgui = nullptr; +constexpr const char* plugin_name = "CRDTNotesImGui"; + extern "C" { SOLANA_PLUGIN_EXPORT const char* solana_plugin_get_name(void) { - return "CRDTNIMGUIotesImGui"; + return plugin_name; } SOLANA_PLUGIN_EXPORT uint32_t solana_plugin_get_version(void) { @@ -24,58 +22,35 @@ SOLANA_PLUGIN_EXPORT uint32_t solana_plugin_get_version(void) { } SOLANA_PLUGIN_EXPORT uint32_t solana_plugin_start(struct SolanaAPI* solana_api) { - std::cout << "PLUGIN CRDTNIMGUI START()\n"; + std::cout << "PLUGIN " << plugin_name << " START()\n"; if (solana_api == nullptr) { return 1; } - //ConfigModelI* conf = nullptr; - CRDTNotesSync* crdtns = nullptr; - Contact3Registry* cr = nullptr; - ImGuiContext* imguic = nullptr; + try { + auto* crdtns = PLUG_RESOLVE_INSTANCE(CRDTNotesSync); + auto* cr = PLUG_RESOLVE_INSTANCE_VERSIONED(Contact3Registry, "1"); + auto* imguic = PLUG_RESOLVE_INSTANCE_VERSIONED(ImGuiContext, ImGui::GetVersion()); - { // make sure required types are loaded - //conf = RESOLVE_INSTANCE(ConfigModelI); - crdtns = RESOLVE_INSTANCE(CRDTNotesSync); - cr = RESOLVE_INSTANCE(Contact3Registry); - imguic = RESOLVE_INSTANCE(ImGuiContext); + ImGui::SetCurrentContext(imguic); - //if (conf == nullptr) { - //std::cerr << "PLUGIN CRDTNIMGUI missing ConfigModelI\n"; - //return 2; - //} + // static store, could be anywhere tho + // construct with fetched dependencies + g_crdtn_imgui = std::make_unique(*crdtns, *cr); - if (crdtns == nullptr) { - std::cerr << "PLUGIN CRDTNIMGUI missing CRDTNotesSync\n"; - return 2; - } - - if (cr == nullptr) { - std::cerr << "PLUGIN CRDTNIMGUI missing Contact3Registry\n"; - return 2; - } - - if (imguic == nullptr) { - std::cerr << "PLUGIN CRDTNIMGUI missing ImGuiContext\n"; - return 2; - } + // register types + PLUG_PROVIDE_INSTANCE(CRDTNotesImGui, plugin_name, g_crdtn_imgui.get()); + } catch (const ResolveException& e) { + std::cerr << "PLUGIN " << plugin_name << " " << e.what << "\n"; + return 2; } - ImGui::SetCurrentContext(imguic); - - // static store, could be anywhere tho - // construct with fetched dependencies - g_crdtn_imgui = std::make_unique(*crdtns, *cr); - - // register types - PROVIDE_INSTANCE(CRDTNotesImGui, "CRDTNotesImGui", g_crdtn_imgui.get()); - return 0; } SOLANA_PLUGIN_EXPORT void solana_plugin_stop(void) { - std::cout << "PLUGIN CRDTNIMGUI STOP()\n"; + std::cout << "PLUGIN " << plugin_name << " STOP()\n"; g_crdtn_imgui.reset(); } diff --git a/plugins/plugin_crdtnotes_toxsync.cpp b/plugins/plugin_crdtnotes_toxsync.cpp index 14caff9..dd45560 100644 --- a/plugins/plugin_crdtnotes_toxsync.cpp +++ b/plugins/plugin_crdtnotes_toxsync.cpp @@ -3,7 +3,6 @@ #include #include #include -//#include #include #include #include @@ -12,15 +11,14 @@ #include #include -#define RESOLVE_INSTANCE(x) static_cast(solana_api->resolveInstance(#x)) -#define PROVIDE_INSTANCE(x, p, v) solana_api->provideInstance(#x, p, static_cast(v)) - static std::unique_ptr g_crdtn_ts = nullptr; +constexpr const char* plugin_name = "CRDTNotesToxSync"; + extern "C" { SOLANA_PLUGIN_EXPORT const char* solana_plugin_get_name(void) { - return "CRDTNotesToxSync"; + return plugin_name; } SOLANA_PLUGIN_EXPORT uint32_t solana_plugin_get_version(void) { @@ -28,70 +26,35 @@ SOLANA_PLUGIN_EXPORT uint32_t solana_plugin_get_version(void) { } SOLANA_PLUGIN_EXPORT uint32_t solana_plugin_start(struct SolanaAPI* solana_api) { - std::cout << "PLUGIN CRDTNTS START()\n"; + std::cout << "PLUGIN " << plugin_name << " START()\n"; if (solana_api == nullptr) { return 1; } - //ConfigModelI* conf = nullptr; - CRDTNotesEventI* notes_sync = nullptr; - Contact3Registry* cr = nullptr; - ToxI* t = nullptr; - ToxEventProviderI* tep = nullptr; - ToxContactModel2* tcm = nullptr; + try { + auto* notes_sync = PLUG_RESOLVE_INSTANCE(CRDTNotesEventI); + auto* cr = PLUG_RESOLVE_INSTANCE_VERSIONED(Contact3Registry, "1"); + auto* t = PLUG_RESOLVE_INSTANCE(ToxI); + auto* tep = PLUG_RESOLVE_INSTANCE(ToxEventProviderI); + auto* tcm = PLUG_RESOLVE_INSTANCE(ToxContactModel2); - { // make sure required types are loaded - //conf = RESOLVE_INSTANCE(ConfigModelI); - notes_sync = RESOLVE_INSTANCE(CRDTNotesEventI); - cr = RESOLVE_INSTANCE(Contact3Registry); - t = RESOLVE_INSTANCE(ToxI); - tep = RESOLVE_INSTANCE(ToxEventProviderI); - tcm = RESOLVE_INSTANCE(ToxContactModel2); + // static store, could be anywhere tho + // construct with fetched dependencies + g_crdtn_ts = std::make_unique(*notes_sync, *cr, *t, *tep, *tcm); - //if (conf == nullptr) { - //std::cerr << "PLUGIN CRDTN missing ConfigModelI\n"; - //return 2; - //} - - if (notes_sync == nullptr) { - std::cerr << "PLUGIN CRDTNTS missing CRDTNotesEventI\n"; - return 2; - } - - if (cr == nullptr) { - std::cerr << "PLUGIN CRDTNTS missing Contact3Registry\n"; - return 2; - } - - if (t == nullptr) { - std::cerr << "PLUGIN CRDTNTS missing ToxI\n"; - return 2; - } - - if (tep == nullptr) { - std::cerr << "PLUGIN CRDTNTS missing ToxEventProviderI\n"; - return 2; - } - - if (tcm == nullptr) { - std::cerr << "PLUGIN CRDTNTS missing ToxContactModel2\n"; - return 2; - } + // register types + PLUG_PROVIDE_INSTANCE(CRDTNotesToxSync, plugin_name, g_crdtn_ts.get()); + } catch (const ResolveException& e) { + std::cerr << "PLUGIN " << plugin_name << " " << e.what << "\n"; + return 2; } - // static store, could be anywhere tho - // construct with fetched dependencies - g_crdtn_ts = std::make_unique(*notes_sync, *cr, *t, *tep, *tcm); - - // register types - PROVIDE_INSTANCE(CRDTNotesToxSync, "CRDTNotesToxSync", g_crdtn_ts.get()); - return 0; } SOLANA_PLUGIN_EXPORT void solana_plugin_stop(void) { - std::cout << "PLUGIN CRDTNTS STOP()\n"; + std::cout << "PLUGIN " << plugin_name << " STOP()\n"; g_crdtn_ts.reset(); }