From b51414f0491db75010800bfb68d3d31d5dff982a Mon Sep 17 00:00:00 2001 From: Green Sky Date: Wed, 7 Aug 2024 11:08:19 +0200 Subject: [PATCH] fix imgui, add dice tool and p2prng (non functional yet) --- .gitmodules | 3 + external/CMakeLists.txt | 3 + external/solanaceae_crdtnotes | 2 +- external/solanaceae_ngc_ft1 | 2 +- external/solanaceae_tox_p2prng | 1 + external/solanaceae_toxic_games | 2 +- plugins/CMakeLists.txt | 18 ++++ plugins/dice_tool.cpp | 162 ++++++++++++++++++++++++++++++++ plugins/dice_tool.hpp | 36 +++++++ plugins/plugin_dice_tool.cpp | 79 ++++++++++++++++ 10 files changed, 305 insertions(+), 3 deletions(-) create mode 160000 external/solanaceae_tox_p2prng create mode 100644 plugins/dice_tool.cpp create mode 100644 plugins/dice_tool.hpp create mode 100644 plugins/plugin_dice_tool.cpp diff --git a/.gitmodules b/.gitmodules index c4339ca..67b695d 100644 --- a/.gitmodules +++ b/.gitmodules @@ -59,3 +59,6 @@ [submodule "external/solanaceae_message_fragment_store"] path = external/solanaceae_message_fragment_store url = https://github.com/Green-Sky/solanaceae_message_fragment_store.git +[submodule "external/solanaceae_tox_p2prng"] + path = external/solanaceae_tox_p2prng + url = https://github.com/Green-Sky/solanaceae_tox_p2prng.git diff --git a/external/CMakeLists.txt b/external/CMakeLists.txt index 7a4f49a..418c047 100644 --- a/external/CMakeLists.txt +++ b/external/CMakeLists.txt @@ -26,6 +26,9 @@ add_subdirectory(./solanaceae_tox) set(SOLANACEAE_TOX_UPNP_BUILD_PLUGINS ON CACHE BOOL "") add_subdirectory(./solanaceae_tox_upnp) +set(SOLANACEAE_TOX_P2PRNG_BUILD_PLUGINS ON CACHE BOOL "") +add_subdirectory(./solanaceae_tox_p2prng) + set(SOLANACEAE_NGCFT1_SHA1_BUILD_TESTING ${SOLANACEAE_ECOSYSTEM_BUILD_TESTING} CACHE BOOL "") add_subdirectory(./solanaceae_ngc_ft1) diff --git a/external/solanaceae_crdtnotes b/external/solanaceae_crdtnotes index a0d1225..d7400e3 160000 --- a/external/solanaceae_crdtnotes +++ b/external/solanaceae_crdtnotes @@ -1 +1 @@ -Subproject commit a0d122540b1121b7b4a51c46c7d3861cf581f12d +Subproject commit d7400e375c6c7485659a0c5fc4bd7741cda57e37 diff --git a/external/solanaceae_ngc_ft1 b/external/solanaceae_ngc_ft1 index 60d6f27..a761378 160000 --- a/external/solanaceae_ngc_ft1 +++ b/external/solanaceae_ngc_ft1 @@ -1 +1 @@ -Subproject commit 60d6f27a125ad74e20e43999f8ddff4e2e6eb152 +Subproject commit a761378dd94e557ee1bd039fd99cb7c552ae44b3 diff --git a/external/solanaceae_tox_p2prng b/external/solanaceae_tox_p2prng new file mode 160000 index 0000000..516df11 --- /dev/null +++ b/external/solanaceae_tox_p2prng @@ -0,0 +1 @@ +Subproject commit 516df11eda8932248a7df1ae73d9543a96546def diff --git a/external/solanaceae_toxic_games b/external/solanaceae_toxic_games index 599094c..c4608bb 160000 --- a/external/solanaceae_toxic_games +++ b/external/solanaceae_toxic_games @@ -1 +1 @@ -Subproject commit 599094c8e4b432008f09892043abe7034ff18a3a +Subproject commit c4608bb3c9c37908bfb264a4b3d60b5630998b84 diff --git a/plugins/CMakeLists.txt b/plugins/CMakeLists.txt index 7493460..5db1b81 100644 --- a/plugins/CMakeLists.txt +++ b/plugins/CMakeLists.txt @@ -35,3 +35,21 @@ target_link_libraries(plugin_transfer_auto_accept PUBLIC solanaceae_message3 solanaceae_tox_messages # sad, for filekind ) + +######################################## + +add_library(plugin_dice_tool MODULE + ./plugin_dice_tool.cpp + ./dice_tool.hpp + ./dice_tool.cpp +) +set_target_properties(plugin_dice_tool PROPERTIES + C_VISIBILITY_PRESET hidden +) +target_compile_definitions(plugin_dice_tool PUBLIC ENTT_API_IMPORT) + +target_link_libraries(plugin_dice_tool PUBLIC + solanaceae_plugin + solanaceae_tox_p2prng + imgui +) diff --git a/plugins/dice_tool.cpp b/plugins/dice_tool.cpp new file mode 100644 index 0000000..a563292 --- /dev/null +++ b/plugins/dice_tool.cpp @@ -0,0 +1,162 @@ +#include "./dice_tool.hpp" + +#include + +#include + +#include + +#include +#include + +DiceTool::DiceTool(P2PRNGI& p2prng, Contact3Registry& cr) : _p2prng(p2prng), _cr(cr) { + p2prng.subscribe(this, P2PRNG_Event::init); + p2prng.subscribe(this, P2PRNG_Event::hmac); + p2prng.subscribe(this, P2PRNG_Event::secret); + p2prng.subscribe(this, P2PRNG_Event::done); + p2prng.subscribe(this, P2PRNG_Event::val_error); +} + +DiceTool::~DiceTool(void) { +} + +float DiceTool::render(float) { + if (ImGui::Begin("DiceTool")) { + // header with values for new roll + + ImGui::TextUnformatted("sides:"); + ImGui::SameLine(); + static uint16_t g_sides {6}; + ImGui::InputScalar("##sides", ImGuiDataType_U16, &g_sides); + + static entt::dense_set peers; + + if (ImGui::CollapsingHeader("peers")) { + ImGui::Indent(); + // list with peers participating + + for (auto it = peers.begin(); it != peers.end();) { + ImGui::PushID(entt::to_integral(*it)); + if (ImGui::SmallButton("-")) { + it = peers.erase(it); + ImGui::PopID(); + continue; + } + + Contact3Handle c {_cr, *it}; + + const char* str_ptr = ""; + if (const auto* name_ptr = c.try_get(); name_ptr != nullptr && !name_ptr->name.empty()) { + str_ptr = name_ptr->name.c_str(); + } + + ImGui::SameLine(); + ImGui::TextUnformatted(str_ptr); + + ImGui::PopID(); + it++; + } + + if (ImGui::Button("add")) { + ImGui::OpenPopup("peer selector"); + } + if (ImGui::BeginPopup("peer selector")) { + for (const auto& [cv] : _cr.view().each()) { + Contact3Handle c {_cr, cv}; + + if (peers.contains(c)) { + continue; + } + + const char* str_ptr = ""; + if (const auto* name_ptr = c.try_get(); name_ptr != nullptr && !name_ptr->name.empty()) { + str_ptr = name_ptr->name.c_str(); + } + + if (c.all_of()) { + if (ImGui::BeginMenu(str_ptr)) { + for (const Contact3 child_cv : c.get().subs) { + Contact3Handle child_c {_cr, child_cv}; + + if (peers.contains(child_c)) { + continue; + } + + const char* child_str_ptr = ""; + if (const auto* name_ptr = child_c.try_get(); name_ptr != nullptr && !name_ptr->name.empty()) { + child_str_ptr = name_ptr->name.c_str(); + } + + if (ImGui::MenuItem(child_str_ptr)) { + peers.emplace(child_c); + } + } + ImGui::EndMenu(); + } + } else { + if (ImGui::MenuItem(str_ptr)) { + peers.emplace(c); + } + } + } + + + ImGui::EndPopup(); + } + + ImGui::SameLine(); + if (ImGui::Button("clear")) { + peers.clear(); + } + + ImGui::Unindent(); + } + + if (ImGui::Button("roll")) { + //std::vector c_vec{peers.cbegin(), peers.cend()}; + std::vector c_vec; + for (const auto cv : peers) { + c_vec.emplace_back(_cr, cv); + } + + auto new_id = _p2prng.newGernationPeers(c_vec, ByteSpan{reinterpret_cast(&g_sides), sizeof(g_sides)}); + if (!new_id.empty()) { + auto& new_roll = _rolls.emplace_back(); + new_roll.id = new_id; + } + } + + ImGui::SeparatorText("Rolls"); + + // list of past rolls and their state + //ImGui::CollapsingHeader("d"); + ImGui::Text("d6 [?] hmac 4/6"); + ImGui::Text("d6 [?] secret 1/3"); + ImGui::Text("d6 [1]"); + } + ImGui::End(); + + return 10.f; +} + +bool DiceTool::onEvent(const P2PRNG::Events::Init&) { + return false; +} + +bool DiceTool::onEvent(const P2PRNG::Events::HMAC&) { + return false; +} + +bool DiceTool::onEvent(const P2PRNG::Events::Secret&) { + return false; +} + +bool DiceTool::onEvent(const P2PRNG::Events::Done&) { + std::cout << "got a done!!!!!!!!!!!!!!!!!!\n"; + return false; +} + +bool DiceTool::onEvent(const P2PRNG::Events::ValError&) { + return false; +} + diff --git a/plugins/dice_tool.hpp b/plugins/dice_tool.hpp new file mode 100644 index 0000000..efb7535 --- /dev/null +++ b/plugins/dice_tool.hpp @@ -0,0 +1,36 @@ +#pragma once + +#include +#include + +#include + +class DiceTool : public P2PRNGEventI { + P2PRNGI& _p2prng; + Contact3Registry& _cr; + + struct Rolls { + std::vector id; + + P2PRNG::State state {P2PRNG::State::UNKNOWN}; + uint16_t state_number_1{}; + uint16_t state_number_2{}; + + uint16_t final_result{}; + }; + std::vector _rolls; + + public: + DiceTool(P2PRNGI& p2prng, Contact3Registry& cr); + ~DiceTool(void); + + float render(float time_delta); + + protected: // p2prng + bool onEvent(const P2PRNG::Events::Init&) override; + bool onEvent(const P2PRNG::Events::HMAC&) override; + bool onEvent(const P2PRNG::Events::Secret&) override; + bool onEvent(const P2PRNG::Events::Done&) override; + bool onEvent(const P2PRNG::Events::ValError&) override; +}; + diff --git a/plugins/plugin_dice_tool.cpp b/plugins/plugin_dice_tool.cpp new file mode 100644 index 0000000..87b77fc --- /dev/null +++ b/plugins/plugin_dice_tool.cpp @@ -0,0 +1,79 @@ +#include + +#include "./dice_tool.hpp" +#include + +#include +#include + +#include +#include +#include + +static std::unique_ptr g_dt = nullptr; + +constexpr const char* plugin_name = "DiceTool"; + +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* p2prng_i = PLUG_RESOLVE_INSTANCE(P2PRNGI); + auto* cr = PLUG_RESOLVE_INSTANCE_VERSIONED(Contact3Registry, "1"); + + // static store, could be anywhere tho + // construct with fetched dependencies + g_dt = std::make_unique(*p2prng_i, *cr); + + auto* imguic = PLUG_RESOLVE_INSTANCE_VERSIONED(ImGuiContext, ImGui::GetVersion()); + auto* imguimemaf = PLUG_RESOLVE_INSTANCE_VERSIONED(ImGuiMemAllocFunc, ImGui::GetVersion()); + auto* imguimemff = PLUG_RESOLVE_INSTANCE_VERSIONED(ImGuiMemFreeFunc, ImGui::GetVersion()); + // meh + auto* imguimemud = plug_resolveInstanceOptional(solana_api, "ImGuiMemUserData", ImGui::GetVersion()); + + ImGui::SetCurrentContext(imguic); + ImGui::SetAllocatorFunctions(imguimemaf, imguimemff, imguimemud); + + // register types + PLUG_PROVIDE_INSTANCE(DiceTool, plugin_name, g_dt.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_dt.reset(); +} + +SOLANA_PLUGIN_EXPORT float solana_plugin_tick(float delta) { + (void)delta; + //g_dt->iterate(); + + return std::numeric_limits::max(); +} + +SOLANA_PLUGIN_EXPORT float solana_plugin_render(float delta) { + return g_dt->render(delta); +} + +} // extern C +