From 7d49cb1281f8debb78b11150c343f3df9d920071 Mon Sep 17 00:00:00 2001 From: Jerom Venneker Date: Thu, 29 May 2025 00:00:13 +0200 Subject: [PATCH] Refactor so apclientpp.h doesn't have to be included a second time --- soh/soh/Network/Archipelago/Archipelago.cpp | 52 ++++++++++++++- soh/soh/Network/Archipelago/Archipelago.h | 5 ++ .../Archipelago/ArchipelagoConsoleWindow.cpp | 66 ++++--------------- .../Archipelago/ArchipelagoConsoleWindow.h | 4 +- 4 files changed, 70 insertions(+), 57 deletions(-) diff --git a/soh/soh/Network/Archipelago/Archipelago.cpp b/soh/soh/Network/Archipelago/Archipelago.cpp index c760f3f1a..6e64489cc 100644 --- a/soh/soh/Network/Archipelago/Archipelago.cpp +++ b/soh/soh/Network/Archipelago/Archipelago.cpp @@ -134,7 +134,57 @@ bool ArchipelagoClient::StartClient() { return; } - ArchipelagoConsole_PrintJson(arg.data); + std::vector coloredNodes; + + for(const APClient::TextNode& node : arg.data) { + APClient* client = apClient.get(); + std::string color; + std::string text; + + if(node.type == "player_id") { + int id = std::stoi(node.text); + if (color.empty() && id == client->get_player_number()) color = "magenta"; + else if(color.empty()) color = "yellow"; + text = client->get_player_alias(id); + } else if (node.type == "item_id") { + int64_t id = std::stoll(node.text); + if(color.empty()) { + if (node.flags & APClient::ItemFlags::FLAG_ADVANCEMENT) color = "plum"; + else if (node.flags & APClient::ItemFlags::FLAG_NEVER_EXCLUDE) color = "slateblue"; + else if (node.flags & APClient::ItemFlags::FLAG_TRAP) color = "salmon"; + else color = "cyan"; + } + text = client->get_item_name(id, client->get_player_game(node.player)); + } else if (node.type == "location_id") { + int64_t id = std::stoll(node.text); + if (color.empty()) color = "blue"; + text = client->get_location_name(id, client->get_player_game(node.player)); + } else if (node.type == "hint_status") { + text = node.text; + if (node.hintStatus == APClient::HINT_FOUND) color = "green"; + else if (node.hintStatus == APClient::HINT_UNSPECIFIED) color = "grey"; + else if (node.hintStatus == APClient::HINT_NO_PRIORITY) color = "slateblue"; + else if (node.hintStatus == APClient::HINT_AVOID) color = "salmon"; + else if (node.hintStatus == APClient::HINT_PRIORITY) color = "plum"; + else color = "red"; // unknown status -> red + } else if (node.type == "ERROR") { + color = "ERROR"; + text = node.text; + } else if (node.type == "LOG") { + color = "LOG"; + text = node.text; + } else { + color = "white"; + text = node.text; + } + + ColoredTextNode Colornode; + Colornode.color = color; + Colornode.text = text; + coloredNodes.push_back(Colornode); + } + + ArchipelagoConsole_PrintJson(coloredNodes); }); return true; diff --git a/soh/soh/Network/Archipelago/Archipelago.h b/soh/soh/Network/Archipelago/Archipelago.h index c52a7bea9..bd8a14d6e 100644 --- a/soh/soh/Network/Archipelago/Archipelago.h +++ b/soh/soh/Network/Archipelago/Archipelago.h @@ -27,6 +27,11 @@ class ArchipelagoClient{ uint64_t index; }; + struct ColoredTextNode { + std::string text; + std::string color; + }; + static ArchipelagoClient& GetInstance(); bool StartClient(); diff --git a/soh/soh/Network/Archipelago/ArchipelagoConsoleWindow.cpp b/soh/soh/Network/Archipelago/ArchipelagoConsoleWindow.cpp index 169571c70..eea74ac02 100644 --- a/soh/soh/Network/Archipelago/ArchipelagoConsoleWindow.cpp +++ b/soh/soh/Network/Archipelago/ArchipelagoConsoleWindow.cpp @@ -4,7 +4,7 @@ #include "soh/SohGui/SohGui.hpp" #include "soh/OTRGlobals.h" -std::vector> Items; +std::vector> Items; bool autoScroll = true; using namespace UIWidgets; @@ -19,21 +19,20 @@ void ArchipelagoConsole_SendMessage(const char* fmt, bool debugMessage, ...) { vsnprintf(buf, IM_ARRAYSIZE(buf), fmt, args); buf[IM_ARRAYSIZE(buf) - 1] = 0; va_end(args); - APClient::TextNode node; + ArchipelagoClient::ColoredTextNode node; + node.text = std::string(buf); + node.color = "white"; if (strstr(buf, "[ERROR]")) { - node.type = "ERROR"; node.color = "ERROR"; } else if (strstr(buf, "[LOG]")) { - node.type = "LOG"; node.color = "LOG"; } - node.text = std::string(buf); - std::list line; + std::vector line; line.push_back(node); Items.push_back(line); } -void ArchipelagoConsole_PrintJson(const std::list nodes) { +void ArchipelagoConsole_PrintJson(const std::vector nodes) { Items.push_back(nodes); } @@ -43,56 +42,15 @@ void ArchipelagoConsoleWindow::DrawElement() { ImGui::PushStyleColor(ImGuiCol_ChildBg, ImVec4(0.15f, 0.15f, 0.15f, 1.0f)); ImGui::PushStyleVar(ImGuiStyleVar_ChildRounding, 8.0f); ImGui::PushStyleVar(ImGuiStyleVar_WindowPadding, ImVec2(15.0f, 12.0f)); - ImGui::PushStyleVar(ImGuiStyleVar_ItemSpacing, ImVec2(0.0f, 2.0f)); + ImGui::PushStyleVar(ImGuiStyleVar_ItemSpacing, ImVec2(0.0f, 1.0f)); if (ImGui::BeginChild("ScrollingRegion", ImVec2(0, 400), ImGuiChildFlags_AlwaysUseWindowPadding, ImGuiWindowFlags_HorizontalScrollbar)) { - for (const std::list& line : Items) { - for(const APClient::TextNode& node : line) { - APClient* client = ArchipelagoClient::GetInstance().apClient.get(); - std::string color; - std::string text; - - if(node.type == "player_id") { - int id = std::stoi(node.text); - if (color.empty() && id == client->get_player_number()) color = "magenta"; - else if(color.empty()) color = "yellow"; - text = client->get_player_alias(id); - } else if (node.type == "item_id") { - int64_t id = std::stoll(node.text); - if(color.empty()) { - if (node.flags & APClient::ItemFlags::FLAG_ADVANCEMENT) color = "plum"; - else if (node.flags & APClient::ItemFlags::FLAG_NEVER_EXCLUDE) color = "slateblue"; - else if (node.flags & APClient::ItemFlags::FLAG_TRAP) color = "salmon"; - else color = "cyan"; - } - text = client->get_item_name(id, client->get_player_game(node.player)); - } else if (node.type == "location_id") { - int64_t id = std::stoll(node.text); - if (color.empty()) color = "blue"; - text = client->get_location_name(id, client->get_player_game(node.player)); - } else if (node.type == "hint_status") { - text = node.text; - if (node.hintStatus == APClient::HINT_FOUND) color = "green"; - else if (node.hintStatus == APClient::HINT_UNSPECIFIED) color = "grey"; - else if (node.hintStatus == APClient::HINT_NO_PRIORITY) color = "slateblue"; - else if (node.hintStatus == APClient::HINT_AVOID) color = "salmon"; - else if (node.hintStatus == APClient::HINT_PRIORITY) color = "plum"; - else color = "red"; // unknown status -> red - } else if (node.type == "ERROR") { - color = "ERROR"; - text = node.text; - } else if (node.type == "LOG") { - color = "LOG"; - text = node.text; - } else { - color = "white"; - text = node.text; - } - - ImGui::PushStyleColor(ImGuiCol_Text, getColorVal(color)); - ImGui::TextUnformatted(text.c_str()); + for(const std::vector& line : Items) { + for(const ArchipelagoClient::ColoredTextNode& node : line) { + ImGui::PushStyleColor(ImGuiCol_Text, getColorVal(node.color)); + ImGui::TextUnformatted(node.text.c_str()); ImGui::SameLine(); ImGui::PopStyleColor(); } @@ -110,7 +68,7 @@ void ArchipelagoConsoleWindow::DrawElement() { ImGui::PopStyleVar(3); }; -ImVec4 getColorVal(const std::string& color) { +ImVec4 getColorVal(const std::string& color) { // TODO change color strings to an enum if (color == "ERROR") { return ImVec4(1.0f, 0.4f, 0.4f, 1.0f); } else if(color =="LOG") { diff --git a/soh/soh/Network/Archipelago/ArchipelagoConsoleWindow.h b/soh/soh/Network/Archipelago/ArchipelagoConsoleWindow.h index 60332e1b6..07597c515 100644 --- a/soh/soh/Network/Archipelago/ArchipelagoConsoleWindow.h +++ b/soh/soh/Network/Archipelago/ArchipelagoConsoleWindow.h @@ -3,7 +3,7 @@ #define ARCHIPELAGO_CONSOLE_WINDOW_H #include -#include +#include "Archipelago.h" #include #include @@ -19,7 +19,7 @@ class ArchipelagoConsoleWindow final : public Ship::GuiWindow { }; void ArchipelagoConsole_SendMessage(const char* fmt, bool debugMessage = false, ...); -void ArchipelagoConsole_PrintJson(const std::list nodes); +void ArchipelagoConsole_PrintJson(const std::vector nodes); ImVec4 getColorVal(const std::string& color); #endif // ARCHIPELAGO_CONSOLE_WINDOW_H \ No newline at end of file