diff --git a/libultraship/libultraship/Console.cpp b/libultraship/libultraship/Console.cpp index efc13ca2ca..79064838ef 100644 --- a/libultraship/libultraship/Console.cpp +++ b/libultraship/libultraship/Console.cpp @@ -1,39 +1,35 @@ #include "Console.h" -#include -#include - #include "Cvar.h" #include "GlobalCtx2.h" #include "ImGuiImpl.h" #include "Lib/ImGui/imgui.h" #include "Utils/StringHelper.h" #include "Lib/ImGui/imgui_internal.h" +#include "Utils.h" namespace Ship { - std::map Bindings; - std::map BindingToggle; + std::string BuildUsage(const CommandEntry& entry) { + std::string usage; + for (const auto& arg : entry.arguments) + usage += StringHelper::Sprintf(arg.optional ? "[%s] " : "<%s> ", arg.info.c_str()); + return usage; + } - static bool HelpCommand(const std::vector&) { - SohImGui::console->SendInfoMessage("SoH Commands:"); - for (const auto& cmd : SohImGui::console->Commands) { - SohImGui::console->SendInfoMessage(" - " + cmd.first); + bool Console::HelpCommand(std::shared_ptr Console, const std::vector& args) { + Console->SendInfoMessage("SoH Commands:"); + for (const auto& cmd : Console->Commands) { + Console->SendInfoMessage(" - " + cmd.first); } return CMD_SUCCESS; } - static bool ClearCommand(const std::vector&) { - SohImGui::console->Log[SohImGui::console->selected_channel].clear(); + bool Console::ClearCommand(std::shared_ptr Console, const std::vector& args) { + Console->ClearLogs(Console->GetCurrentChannel()); return CMD_SUCCESS; } - std::string toLowerCase(std::string in) { - std::string cpy(in); - std::transform(cpy.begin(), cpy.end(), cpy.begin(), ::tolower); - return cpy; - } - - static bool BindCommand(const std::vector& args) { + bool Console::BindCommand(std::shared_ptr Console, const std::vector& args) { if (args.size() > 2) { const ImGuiIO* io = &ImGui::GetIO();; for (size_t k = 0; k < std::size(io->KeysData); k++) { @@ -44,8 +40,8 @@ namespace Ship { const char* const delim = " "; std::ostringstream imploded; std::copy(args.begin() + 2, args.end(), std::ostream_iterator(imploded, delim)); - Bindings[k] = imploded.str(); - SohImGui::console->SendInfoMessage("Binding '%s' to %s", args[1].c_str(), Bindings[k].c_str()); + Console->Bindings[k] = imploded.str(); + Console->SendInfoMessage("Binding '%s' to %s", args[1].c_str(), Console->Bindings[k].c_str()); break; } } @@ -53,15 +49,15 @@ namespace Ship { return CMD_SUCCESS; } - static bool BindToggleCommand(const std::vector& args) { + bool Console::BindToggleCommand(std::shared_ptr Console, const std::vector& args) { if (args.size() > 2) { const ImGuiIO* io = &ImGui::GetIO();; for (size_t k = 0; k < std::size(io->KeysData); k++) { std::string key(ImGui::GetKeyName(k)); if (toLowerCase(args[1]) == toLowerCase(key)) { - BindingToggle[k] = args[2]; - SohImGui::console->SendInfoMessage("Binding toggle '%s' to %s", args[1].c_str(), BindingToggle[k].c_str()); + Console->BindingToggle[k] = args[2]; + Console->SendInfoMessage("Binding toggle '%s' to %s", args[1].c_str(), Console->BindingToggle[k].c_str()); break; } } @@ -69,22 +65,15 @@ namespace Ship { return CMD_SUCCESS; } - std::string BuildUsage(const CommandEntry& entry) { - std::string usage; - for (const auto& arg : entry.arguments) - usage += StringHelper::Sprintf(arg.optional ? "[%s] " : "<%s> ", arg.info.c_str()); - return usage; - } - void Console::Init() { - this->InputBuffer = new char[MAX_BUFFER_SIZE]; - strcpy(this->InputBuffer, ""); - this->FilterBuffer = new char[MAX_BUFFER_SIZE]; - strcpy(this->FilterBuffer, ""); - this->Commands["help"] = { HelpCommand, "Shows all the commands" }; - this->Commands["clear"] = { ClearCommand, "Clear the console history" }; - this->Commands["bind"] = { BindCommand, "Binds key to commands" }; - this->Commands["bind-toggle"] = { BindToggleCommand, "Bind key as a bool toggle" }; + this->inputBuffer = new char[MAX_BUFFER_SIZE]; + strcpy(this->inputBuffer, ""); + this->filterBuffer = new char[MAX_BUFFER_SIZE]; + strcpy(this->filterBuffer, ""); + AddCommand("help", { HelpCommand, "Shows all the commands" }); + AddCommand("clear", { ClearCommand, "Clear the console history" }); + AddCommand("bind", { BindCommand, "Binds key to commands" }); + AddCommand("bind-toggle", { BindToggleCommand, "Bind key as a bool toggle" }); } void Console::Update() { @@ -114,7 +103,7 @@ namespace Ship { // SohImGui::ShowCursor(ImGui::IsWindowHovered(ImGuiHoveredFlags_RootAndChildWindows | ImGuiHoveredFlags_RectOnly), SohImGui::Dialogues::dConsole); // Renders autocomplete window - if (this->OpenAutocomplete) { + if (this->openAutocomplete) { ImGui::SetNextWindowSize(ImVec2(350, std::min(static_cast(this->Autocomplete.size()), 3) * 20.f), ImGuiCond_Once); ImGui::SetNextWindowPos(ImVec2(pos.x + 8, pos.y + size.y - 1)); ImGui::PushStyleVar(ImGuiStyleVar_WindowPadding, ImVec2(3, 3)); @@ -129,16 +118,16 @@ namespace Ship { ImGui::TableNextRow(); ImGui::TableSetColumnIndex(0); if (ImGui::Selectable(preview.c_str())) { - memset(this->InputBuffer, 0, MAX_BUFFER_SIZE); - memcpy(this->InputBuffer, autocomplete.c_str(), sizeof(char) * autocomplete.size()); - this->OpenAutocomplete = false; + memset(this->inputBuffer, 0, MAX_BUFFER_SIZE); + memcpy(this->inputBuffer, autocomplete.c_str(), sizeof(char) * autocomplete.size()); + this->openAutocomplete = false; input_focus = true; } } ImGui::EndTable(); } if (ImGui::IsKeyPressed(ImGuiKey_Escape)) { - this->OpenAutocomplete = false; + this->openAutocomplete = false; } ImGui::PopStyleColor(); ImGui::EndChild(); @@ -148,7 +137,7 @@ namespace Ship { if (ImGui::BeginPopupContextWindow("Context Menu")) { if (ImGui::MenuItem("Copy Text")) { - ImGui::SetClipboardText(this->Log[this->selected_channel][this->selectedId].text.c_str()); + ImGui::SetClipboardText(this->Log[this->currentChannel][this->selectedId].text.c_str()); this->selectedId = -1; } ImGui::EndPopup(); @@ -158,44 +147,44 @@ namespace Ship { } // Renders top bar filters - if (ImGui::Button("Clear")) this->Log[this->selected_channel].clear(); + if (ImGui::Button("Clear")) this->Log[this->currentChannel].clear(); if (CVar_GetS32("gSinkEnabled", 0)) { ImGui::SameLine(); ImGui::SetNextItemWidth(150); - if (ImGui::BeginCombo("##channel", this->selected_channel.c_str())) { - for (const auto& channel : log_channels) { - const bool is_selected = (channel == std::string(this->selected_channel)); + if (ImGui::BeginCombo("##channel", this->currentChannel.c_str())) { + for (const auto& channel : LogChannels) { + const bool is_selected = (channel == std::string(this->currentChannel)); if (ImGui::Selectable(channel.c_str(), is_selected)) - this->selected_channel = channel; + this->currentChannel = channel; if (is_selected) ImGui::SetItemDefaultFocus(); } ImGui::EndCombo(); } } else { - this->selected_channel = "Console"; + this->currentChannel = "Console"; } ImGui::SameLine(); ImGui::SetNextItemWidth(150); - if (this->selected_channel != "Console") { - if (ImGui::BeginCombo("##level", spdlog::level::to_string_view(this->level_filter).data())) { - for (const auto& priority_filter : priority_filters) { - const bool is_selected = priority_filter == this->level_filter; + if (this->currentChannel != "Console") { + if (ImGui::BeginCombo("##level", spdlog::level::to_string_view(this->levelFilter).data())) { + for (const auto& priority_filter : PriorityFilters) { + const bool is_selected = priority_filter == this->levelFilter; if (ImGui::Selectable(spdlog::level::to_string_view(priority_filter).data(), is_selected)) { - this->level_filter = priority_filter; + this->levelFilter = priority_filter; if (is_selected) ImGui::SetItemDefaultFocus(); } } ImGui::EndCombo(); } } else { - this->level_filter = spdlog::level::trace; + this->levelFilter = spdlog::level::trace; } ImGui::SameLine(); ImGui::PushItemWidth(-1); - if (ImGui::InputTextWithHint("##input", "Filter", this->FilterBuffer, MAX_BUFFER_SIZE))this->filter = std::string(this->FilterBuffer); + if (ImGui::InputTextWithHint("##input", "Filter", this->filterBuffer, MAX_BUFFER_SIZE))this->filter = std::string(this->filterBuffer); ImGui::PopItemWidth(); // Renders console history @@ -209,16 +198,16 @@ namespace Ship { if (ImGui::IsKeyPressed(ImGui::GetKeyIndex(ImGuiKey_UpArrow))) if (this->selectedId > 0)--this->selectedId; - const std::vector channel = this->Log[this->selected_channel]; + const std::vector channel = this->Log[this->currentChannel]; for (int i = 0; i < static_cast(channel.size()); i++) { ConsoleLine line = channel[i]; if (!this->filter.empty() && line.text.find(this->filter) == std::string::npos) continue; - if (this->level_filter > line.priority) continue; + if (this->levelFilter > line.priority) continue; std::string id = line.text + "##" + std::to_string(i); ImGui::TableNextRow(); ImGui::TableSetColumnIndex(0); const bool is_selected = (this->selectedId == i) || std::find(this->selectedEntries.begin(), this->selectedEntries.end(), i) != this->selectedEntries.end(); - ImGui::PushStyleColor(ImGuiCol_Text, this->priority_colors[line.priority]); + ImGui::PushStyleColor(ImGuiCol_Text, this->PriorityColours[line.priority]); if (ImGui::Selectable(id.c_str(), is_selected)) { if (ImGui::IsKeyDown(ImGui::GetKeyIndex(ImGuiKey_LeftCtrl)) && !is_selected) this->selectedEntries.push_back(i); @@ -236,25 +225,25 @@ namespace Ship { ImGui::SetScrollHereY(1.0f); ImGui::EndChild(); - if (this->selected_channel == "Console") { + if (this->currentChannel == "Console") { // Renders input textfield constexpr ImGuiInputTextFlags flags = ImGuiInputTextFlags_EnterReturnsTrue | ImGuiInputTextFlags_CallbackEdit | ImGuiInputTextFlags_CallbackCompletion | ImGuiInputTextFlags_CallbackHistory; ImGui::PushItemWidth(-53); - if (ImGui::InputTextWithHint("##CMDInput", ">", this->InputBuffer, MAX_BUFFER_SIZE, flags, &Console::CallbackStub, this)) { + if (ImGui::InputTextWithHint("##CMDInput", ">", this->inputBuffer, MAX_BUFFER_SIZE, flags, &Console::CallbackStub, this)) { input_focus = true; - if (this->InputBuffer[0] != '\0' && this->InputBuffer[0] != ' ') - this->Dispatch(std::string(this->InputBuffer)); - memset(this->InputBuffer, 0, MAX_BUFFER_SIZE); + if (this->inputBuffer[0] != '\0' && this->inputBuffer[0] != ' ') + this->Dispatch(std::string(this->inputBuffer)); + memset(this->inputBuffer, 0, MAX_BUFFER_SIZE); } - if (this->CMDHint != NULLSTR) { + if (this->cmdHint != NULLSTR) { if (ImGui::IsItemFocused()) { ImGui::SetNextWindowPos(ImVec2(pos.x, pos.y + size.y)); ImGui::SameLine(); ImGui::BeginTooltip(); ImGui::PushTextWrapPos(ImGui::GetFontSize() * 35.0f); - ImGui::TextUnformatted(this->CMDHint.c_str()); + ImGui::TextUnformatted(this->cmdHint.c_str()); ImGui::PopTextWrapPos(); ImGui::EndTooltip(); } @@ -262,9 +251,9 @@ namespace Ship { ImGui::SameLine(); ImGui::SetCursorPosX(ImGui::GetCursorPosX() + ImGui::GetContentRegionAvail().x - 50); - if (ImGui::Button("Submit") && !input_focus && this->InputBuffer[0] != '\0' && this->InputBuffer[0] != ' ') { - this->Dispatch(std::string(this->InputBuffer)); - memset(this->InputBuffer, 0, MAX_BUFFER_SIZE); + if (ImGui::Button("Submit") && !input_focus && this->inputBuffer[0] != '\0' && this->inputBuffer[0] != ' ') { + this->Dispatch(std::string(this->inputBuffer)); + memset(this->inputBuffer, 0, MAX_BUFFER_SIZE); } ImGui::SetItemDefaultFocus(); @@ -275,14 +264,16 @@ namespace Ship { } void Console::Dispatch(const std::string& line) { - this->CMDHint = NULLSTR; + this->cmdHint = NULLSTR; this->History.push_back(line); SendInfoMessage("> " + line); const std::vector cmd_args = StringHelper::Split(line, " "); if (this->Commands.contains(cmd_args[0])) { const CommandEntry entry = this->Commands[cmd_args[0]]; - if (!entry.handler(cmd_args) && !entry.arguments.empty()) + if (!entry.handler(shared_from_this(), cmd_args) && !entry.arguments.empty()) { SendErrorMessage("[SOH] Usage: " + cmd_args[0] + " " + BuildUsage(entry)); + } + return; } SendErrorMessage("[SOH] Command not found"); @@ -291,7 +282,7 @@ namespace Ship { int Console::CallbackStub(ImGuiInputTextCallbackData* data) { const auto instance = static_cast(data->UserData); const bool empty_history = instance->History.empty(); - const int history_index = instance->HistoryIndex; + const int history_index = instance->historyIndex; std::string history; switch (data->EventKey) { @@ -299,39 +290,39 @@ namespace Ship { instance->Autocomplete.clear(); for (auto& [cmd, entry] : instance->Commands) if (cmd.find(std::string(data->Buf)) != std::string::npos) instance->Autocomplete.push_back(cmd); - instance->OpenAutocomplete = !instance->Autocomplete.empty(); - instance->CMDHint = NULLSTR; + instance->openAutocomplete = !instance->Autocomplete.empty(); + instance->cmdHint = NULLSTR; break; case ImGuiKey_UpArrow: if (empty_history) break; - if (history_index < static_cast(instance->History.size()) - 1) instance->HistoryIndex += 1; + if (history_index < static_cast(instance->History.size()) - 1) instance->historyIndex += 1; data->DeleteChars(0, data->BufTextLen); - data->InsertChars(0, instance->History[instance->HistoryIndex].c_str()); - instance->CMDHint = NULLSTR; + data->InsertChars(0, instance->History[instance->historyIndex].c_str()); + instance->cmdHint = NULLSTR; break; case ImGuiKey_DownArrow: if (empty_history) break; - if (history_index > -1) instance->HistoryIndex -= 1; + if (history_index > -1) instance->historyIndex -= 1; data->DeleteChars(0, data->BufTextLen); if (history_index >= 0) data->InsertChars(0, instance->History[history_index].c_str()); - instance->CMDHint = NULLSTR; + instance->cmdHint = NULLSTR; break; case ImGuiKey_Escape: - instance->HistoryIndex = -1; + instance->historyIndex = -1; data->DeleteChars(0, data->BufTextLen); - instance->OpenAutocomplete = false; - instance->CMDHint = NULLSTR; + instance->openAutocomplete = false; + instance->cmdHint = NULLSTR; break; default: - instance->OpenAutocomplete = false; + instance->openAutocomplete = false; for (auto& [cmd, entry] : instance->Commands) { const std::vector cmd_args = StringHelper::Split(std::string(data->Buf), " "); if (data->BufTextLen > 2 && !cmd_args.empty() && cmd.find(cmd_args[0]) != std::string::npos) { - instance->CMDHint = cmd + " " + BuildUsage(entry); + instance->cmdHint = cmd + " " + BuildUsage(entry); break; } - instance->CMDHint = NULLSTR; + instance->cmdHint = NULLSTR; } } return 0; @@ -372,4 +363,30 @@ namespace Ship { void Console::SendErrorMessage(const std::string& str) { Append("Console", spdlog::level::err, str.c_str()); } + + void Console::ClearLogs(std::string channel) { + Log[channel].clear(); + } + + void Console::ClearLogs() { + for (auto [key, var] : Log) { + var.clear(); + } + } + + bool Console::HasCommand(const std::string& command) { + for (const auto& Command : Commands) { + if (Command.first == command) { + return true; + } + } + + return false; + } + + void Console::AddCommand(const std::string& command, CommandEntry entry) { + if (!HasCommand(command)) { + Commands[command] = entry; + } + } } \ No newline at end of file diff --git a/libultraship/libultraship/Console.h b/libultraship/libultraship/Console.h index 00ca518e26..f0b78aac90 100644 --- a/libultraship/libultraship/Console.h +++ b/libultraship/libultraship/Console.h @@ -16,7 +16,8 @@ namespace Ship { #define MAX_BUFFER_SIZE 255 #define NULLSTR "None" - typedef std::function args)> CommandHandler; + class Console; + typedef std::function Console, std::vector args)> CommandHandler; enum class ArgumentType { TEXT, NUMBER, PLAYER_POS, PLAYER_ROT @@ -40,14 +41,35 @@ namespace Ship { std::string channel = "Console"; }; - class Console { + class Console : public std::enable_shared_from_this { + private: + static int CallbackStub(ImGuiInputTextCallbackData* data); + static bool ClearCommand(std::shared_ptr Console, const std::vector& args); + static bool HelpCommand(std::shared_ptr Console, const std::vector& args); + static bool BindCommand(std::shared_ptr Console, const std::vector& args); + static bool BindToggleCommand(std::shared_ptr Console, const std::vector& args); + + bool opened = false; int selectedId = -1; + int historyIndex = -1; std::vector selectedEntries; std::string filter; - spdlog::level::level_enum level_filter = spdlog::level::trace; - std::vector log_channels = { "Console", "Logs" }; - std::vector priority_filters = { spdlog::level::off, spdlog::level::critical, spdlog::level::err, spdlog::level::warn, spdlog::level::info, spdlog::level::debug, spdlog::level::trace }; - std::vector priority_colors = { + std::string currentChannel = "Console"; + bool openAutocomplete = false; + char* inputBuffer = nullptr; + char* filterBuffer = nullptr; + std::string cmdHint = NULLSTR; + spdlog::level::level_enum levelFilter = spdlog::level::trace; + + std::vector History; + std::vector Autocomplete; + std::map Bindings; + std::map BindingToggle; + std::map Commands; + std::map> Log; + const std::vector LogChannels = { "Console", "Logs" }; + const std::vector PriorityFilters = { spdlog::level::off, spdlog::level::critical, spdlog::level::err, spdlog::level::warn, spdlog::level::info, spdlog::level::debug, spdlog::level::trace }; + const std::vector PriorityColours = { ImVec4(0.8f, 0.8f, 0.8f, 1.0f), // TRACE ImVec4(0.9f, 0.9f, 0.9f, 1.0f), // DEBUG ImVec4(1.0f, 1.0f, 1.0f, 1.0f), // INFO @@ -60,26 +82,23 @@ namespace Ship { void Append(const std::string& channel, spdlog::level::level_enum priority, const char* fmt, va_list args); public: - std::map> Log; - std::map Commands; - std::vector Autocomplete; - std::vector History; - std::string CMDHint = NULLSTR; - char* FilterBuffer = nullptr; - char* InputBuffer = nullptr; - bool OpenAutocomplete = false; - int HistoryIndex = -1; - std::string selected_channel = "Console"; - bool opened = false; + void ClearLogs(std::string channel); + void ClearLogs(); void Init(); void Update(); void Draw(); void Dispatch(const std::string& line); - static int CallbackStub(ImGuiInputTextCallbackData* data); void SendInfoMessage(const char* fmt, ...); void SendErrorMessage(const char* fmt, ...); void SendInfoMessage(const std::string& str); void SendErrorMessage(const std::string& str); void Append(const std::string& channel, spdlog::level::level_enum priority, const char* fmt, ...); + bool HasCommand(const std::string& command); + void AddCommand(const std::string& command, CommandEntry entry); + + std::string GetCurrentChannel() { return currentChannel; } + bool IsOpened() { return opened; } + void Close() { opened = false; } + void Open() { opened = true; } }; } \ No newline at end of file diff --git a/libultraship/libultraship/ControlDeck.cpp b/libultraship/libultraship/ControlDeck.cpp index 7437afeee8..59f25b44f9 100644 --- a/libultraship/libultraship/ControlDeck.cpp +++ b/libultraship/libultraship/ControlDeck.cpp @@ -9,7 +9,6 @@ #include "Cvar.h" namespace Ship { - uint8_t* controllerBits; void ControlDeck::Init(uint8_t* bits) { ScanPhysicalDevices(); @@ -187,4 +186,9 @@ namespace Ship { std::shared_ptr ControlDeck::GetPhysicalDeviceFromVirtualSlot(int slot) { return GetPhysicalDevice(GetVirtualDevice(slot)); } + + uint8_t* ControlDeck::GetControllerBits() { + return controllerBits; + } + } \ No newline at end of file diff --git a/libultraship/libultraship/ControlDeck.h b/libultraship/libultraship/ControlDeck.h index 15cba08e96..025bfcde40 100644 --- a/libultraship/libultraship/ControlDeck.h +++ b/libultraship/libultraship/ControlDeck.h @@ -18,8 +18,10 @@ namespace Ship { size_t GetNumPhysicalDevices(); int GetVirtualDevice(int slot); size_t GetNumVirtualDevices(); + uint8_t* GetControllerBits(); private: - std::vector virtualDevices; + std::vector virtualDevices = {}; std::vector> physicalDevices = {}; + uint8_t* controllerBits = nullptr; }; } diff --git a/libultraship/libultraship/GameOverlay.cpp b/libultraship/libultraship/GameOverlay.cpp index 8fe028667e..051669577b 100644 --- a/libultraship/libultraship/GameOverlay.cpp +++ b/libultraship/libultraship/GameOverlay.cpp @@ -9,6 +9,40 @@ #include "Utils/StringHelper.h" namespace Ship { + bool OverlayCommand(std::shared_ptr Console, const std::vector& args) { + if (args.size() < 3) { + return CMD_FAILED; + } + + if (CVar_Get(args[2].c_str()) != nullptr) { + const char* key = args[2].c_str(); + GameOverlay* overlay = SohImGui::overlay; + if (args[1] == "add") { + if (!overlay->RegisteredOverlays.contains(key)) { + overlay->RegisteredOverlays[key] = new Overlay({ OverlayType::TEXT, ImStrdup(key), -1.0f }); + SohImGui::console->SendInfoMessage("Added overlay: %s", key); + } + else { + SohImGui::console->SendErrorMessage("Overlay already exists: %s", key); + } + } + else if (args[1] == "remove") { + if (overlay->RegisteredOverlays.contains(key)) { + overlay->RegisteredOverlays.erase(key); + SohImGui::console->SendInfoMessage("Removed overlay: %s", key); + } + else { + SohImGui::console->SendErrorMessage("Overlay not found: %s", key); + } + } + } + else { + SohImGui::console->SendErrorMessage("CVar {} does not exist", args[2].c_str()); + } + + return CMD_SUCCESS; + } + void GameOverlay::LoadFont(const std::string& name, const std::string& path, float fontSize) { ImGuiIO& io = ImGui::GetIO(); std::shared_ptr base = GlobalCtx2::GetInstance()->GetResourceManager()->GetArchive(); @@ -122,7 +156,8 @@ namespace Ship { this->CurrentFont = DefaultFont; } } - SohImGui::console->Commands["overlay"] = { OverlayCommand, "Draw an overlay using a cvar value" }; + + SohImGui::console->AddCommand("overlay", { OverlayCommand, "Draw an overlay using a cvar value" }); } void GameOverlay::DrawSettings() { @@ -195,40 +230,4 @@ namespace Ship { ImGui::End(); } - - - bool OverlayCommand(const std::vector& args) { - if (args.size() < 3) { - return CMD_FAILED; - } - - if (CVar_Get(args[2].c_str()) != nullptr) { - const char* key = args[2].c_str(); - GameOverlay* overlay = SohImGui::overlay; - if (args[1] == "add") { - if (!overlay->RegisteredOverlays.contains(key)) { - overlay->RegisteredOverlays[key] = new Overlay({ OverlayType::TEXT, ImStrdup(key), -1.0f }); - SPDLOG_INFO("Added overlay: {} ", key); - SohImGui::console->SendInfoMessage("Added overlay: %s", key); - } - else { - SPDLOG_ERROR("Overlay already exists: {}", key); - } - } - else if (args[1] == "remove") { - if (overlay->RegisteredOverlays.contains(key)) { - overlay->RegisteredOverlays.erase(key); - SPDLOG_INFO("Removed overlay: {} ", key); - } - else { - SPDLOG_ERROR("Overlay not found: {}", key); - } - } - } - else { - SPDLOG_ERROR("CVar {} does not exist", args[2].c_str()); - } - - return CMD_SUCCESS; - } } diff --git a/libultraship/libultraship/GameOverlay.h b/libultraship/libultraship/GameOverlay.h index 061d259ba8..57bc418641 100644 --- a/libultraship/libultraship/GameOverlay.h +++ b/libultraship/libultraship/GameOverlay.h @@ -37,6 +37,4 @@ namespace Ship { void CleanupNotifications(); void LoadFont(const std::string& name, const std::string& path, float fontSize); }; - - bool OverlayCommand(const std::vector& args); } diff --git a/libultraship/libultraship/ImGuiImpl.cpp b/libultraship/libultraship/ImGuiImpl.cpp index b95073fd18..3b3b534bd3 100644 --- a/libultraship/libultraship/ImGuiImpl.cpp +++ b/libultraship/libultraship/ImGuiImpl.cpp @@ -91,7 +91,7 @@ namespace SohImGui { WindowImpl impl; ImGuiIO* io; - Console* console = new Console; + std::shared_ptr console = std::make_shared(); GameOverlay* overlay = new GameOverlay; InputEditor* controller = new InputEditor; static ImVector s_GroupPanelLabelStack; @@ -141,7 +141,11 @@ namespace SohImGui { Ship::RegisterHook(UpdateAudio); Ship::RegisterHook([] { gfx_get_current_rendering_api()->set_texture_filter((FilteringMode)CVar_GetS32("gTextureFilter", FILTER_THREE_POINT)); - SohImGui::console->opened = CVar_GetS32("gConsoleEnabled", 0); + if (CVar_GetS32("gConsoleEnabled", 0)) { + console->Open(); + } else { + console->Close(); + } SohImGui::controller->Opened = CVar_GetS32("gControllerConfigurationEnabled", 0); UpdateAudio(); }); @@ -857,13 +861,13 @@ namespace SohImGui { if ((ImGui::IsKeyDown(ImGuiKey_LeftSuper) || ImGui::IsKeyDown(ImGuiKey_RightSuper)) && ImGui::IsKeyPressed(ImGuiKey_R, false)) { - console->Commands["reset"].handler(emptyArgs); + console->Dispatch("reset"); } #else if ((ImGui::IsKeyDown(ImGuiKey_LeftCtrl) || ImGui::IsKeyDown(ImGuiKey_RightCtrl)) && ImGui::IsKeyPressed(ImGuiKey_R, false)) { - console->Commands["reset"].handler(emptyArgs); + console->Dispatch("reset"); } #endif @@ -891,7 +895,7 @@ namespace SohImGui { "Ctrl+R" #endif )) { - console->Commands["reset"].handler(emptyArgs); + console->Dispatch("reset"); } ImGui::EndMenu(); } @@ -1638,7 +1642,7 @@ namespace SohImGui { CVar_SetS32("gEnableBetaQuest", betaQuestEnabled); CVar_SetS32("gBetaQuestWorld", betaQuestWorld); - console->Commands["reset"].handler(emptyArgs); + console->Dispatch("reset"); needs_save = true; } @@ -1695,7 +1699,11 @@ namespace SohImGui { bool currentValue = CVar_GetS32("gConsoleEnabled", 0); CVar_SetS32("gConsoleEnabled", !currentValue); needs_save = true; - console->opened = CVar_GetS32("gConsoleEnabled", 0); + if(CVar_GetS32("gConsoleEnabled", 0)){ + console->Open(); + } else { + console->Close(); + } } Tooltip("Enables the console window, allowing you to input commands, type help for some examples"); InsertPadding(); @@ -2261,7 +2269,7 @@ namespace SohImGui { } void BindCmd(const std::string& cmd, CommandEntry entry) { - console->Commands[cmd] = std::move(entry); + console->AddCommand(cmd, entry); } void AddWindow(const std::string& category, const std::string& name, WindowDrawFunc drawFunc, bool isEnabled, bool isHidden) { diff --git a/libultraship/libultraship/ImGuiImpl.h b/libultraship/libultraship/ImGuiImpl.h index fd0aced6a2..8e20d415c5 100644 --- a/libultraship/libultraship/ImGuiImpl.h +++ b/libultraship/libultraship/ImGuiImpl.h @@ -69,7 +69,7 @@ namespace SohImGui { WindowDrawFunc drawFunc; } CustomWindow; - extern Ship::Console* console; + extern std::shared_ptr console; extern Ship::InputEditor* controller; extern Ship::GameOverlay* overlay; extern bool needs_save; diff --git a/libultraship/libultraship/Lib/spdlog/include/spdlog/sinks/sohconsole_sink.h b/libultraship/libultraship/Lib/spdlog/include/spdlog/sinks/sohconsole_sink.h index 65e98610aa..8a629aa1a5 100644 --- a/libultraship/libultraship/Lib/spdlog/include/spdlog/sinks/sohconsole_sink.h +++ b/libultraship/libultraship/Lib/spdlog/include/spdlog/sinks/sohconsole_sink.h @@ -41,7 +41,7 @@ protected: } formatted.push_back('\0'); const char* msg_output = formatted.data(); - if (CVar_GetS32("gSinkEnabled", 0) && SohImGui::console->opened) { + if (CVar_GetS32("gSinkEnabled", 0) && SohImGui::console->IsOpened()) { SohImGui::console->Append("Logs", msg.level, "%s", msg_output); } } diff --git a/libultraship/libultraship/Model.cpp b/libultraship/libultraship/Model.cpp index 9e37de177e..c59f38e8bf 100644 --- a/libultraship/libultraship/Model.cpp +++ b/libultraship/libultraship/Model.cpp @@ -2,7 +2,7 @@ namespace Ship { - Vertex::Vertex() + ModelVertex::ModelVertex() { pos = Vec3f(0, 0, 0); normal = Vec3f(0, 0, 0); @@ -10,7 +10,7 @@ namespace Ship uv = Vec2f(0, 0); } - Vertex::Vertex(BinaryReader* reader) + ModelVertex::ModelVertex(BinaryReader* reader) { pos = reader->ReadVec3f(); normal = reader->ReadVec3f(); @@ -36,7 +36,7 @@ namespace Ship uvCoords = reader->ReadUInt32(); boneWeights = reader->ReadUInt32(); - Vertex* vtxData = new Vertex[numVerts]; + ModelVertex* vtxData = new ModelVertex[numVerts]; uint32_t* indicesData = new uint32_t[numPolys]; if (vertices != 0) diff --git a/libultraship/libultraship/Model.h b/libultraship/libultraship/Model.h index f66ca7b846..241a2932ad 100644 --- a/libultraship/libultraship/Model.h +++ b/libultraship/libultraship/Model.h @@ -43,15 +43,15 @@ namespace Ship void ParseFileBinary(BinaryReader* reader, Resource* res) override; }; - struct Vertex + struct ModelVertex { Vec3f pos; Vec3f normal; Color3b color; Vec2f uv; - Vertex(); - Vertex(BinaryReader* reader); + ModelVertex(); + ModelVertex(BinaryReader* reader); }; class Model : public Resource @@ -62,7 +62,7 @@ namespace Ship uint32_t numVerts; uint32_t numPolys; - Vertex* vertices; + ModelVertex* vertices; Vec2f* boneWeights; uint32_t* indices; }; diff --git a/libultraship/libultraship/Utils.cpp b/libultraship/libultraship/Utils.cpp index 185c21c249..6d0e50f5c5 100644 --- a/libultraship/libultraship/Utils.cpp +++ b/libultraship/libultraship/Utils.cpp @@ -1,5 +1,6 @@ #include "Utils.h" #include +#include #ifdef _MSC_VER #define strdup _strdup @@ -58,4 +59,10 @@ namespace Ship { return args; } + + std::string toLowerCase(std::string in) { + std::string cpy(in); + std::transform(cpy.begin(), cpy.end(), cpy.begin(), ::tolower); + return cpy; + } } diff --git a/libultraship/libultraship/Utils.h b/libultraship/libultraship/Utils.h index 1dc3d34914..256db35e72 100644 --- a/libultraship/libultraship/Utils.h +++ b/libultraship/libultraship/Utils.h @@ -10,4 +10,5 @@ namespace Ship { } std::vector SplitText(const std::string& text, char separator, bool keep_quotes); + std::string toLowerCase(std::string in); } \ No newline at end of file diff --git a/soh/soh/Enhancements/debugconsole.cpp b/soh/soh/Enhancements/debugconsole.cpp index 203e5521ac..8e72392ca7 100644 --- a/soh/soh/Enhancements/debugconsole.cpp +++ b/soh/soh/Enhancements/debugconsole.cpp @@ -5,6 +5,7 @@ #include "debugconsole.h" #include "../libultraship/ImGuiImpl.h" #include "savestates.h" +#include "Console.h" #include #include @@ -32,7 +33,7 @@ extern GlobalContext* gGlobalCtx; #define CMD_REGISTER SohImGui::BindCmd -static bool ActorSpawnHandler(const std::vector& args) { +static bool ActorSpawnHandler(std::shared_ptr Console, const std::vector& args) { if ((args.size() != 9) && (args.size() != 3) && (args.size() != 6)) { SohImGui::console->SendErrorMessage("Not enough arguments passed to actorspawn"); return CMD_FAILED; @@ -82,13 +83,13 @@ static bool ActorSpawnHandler(const std::vector& args) { } -static bool KillPlayerHandler([[maybe_unused]] const std::vector&) { +static bool KillPlayerHandler(std::shared_ptr Console, const std::vector&) { gSaveContext.health = 0; SohImGui::console->SendInfoMessage("[SOH] You've met with a terrible fate, haven't you?"); return CMD_SUCCESS; } -static bool SetPlayerHealthHandler(const std::vector& args) { +static bool SetPlayerHealthHandler(std::shared_ptr Console, const std::vector& args) { if (args.size() != 2) { SohImGui::console->SendErrorMessage("[SOH] Unexpected arguments passed"); return CMD_FAILED; @@ -115,7 +116,7 @@ static bool SetPlayerHealthHandler(const std::vector& args) { } -static bool LoadSceneHandler(const std::vector&) { +static bool LoadSceneHandler(std::shared_ptr Console, const std::vector&) { gSaveContext.respawnFlag = 0; gSaveContext.seqId = 0xFF; gSaveContext.gameMode = 0; @@ -123,7 +124,7 @@ static bool LoadSceneHandler(const std::vector&) { return CMD_SUCCESS; } -static bool RuppeHandler(const std::vector& args) { +static bool RuppeHandler(std::shared_ptr Console, const std::vector& args) { if (args.size() < 2) return CMD_FAILED; @@ -147,7 +148,7 @@ static bool RuppeHandler(const std::vector& args) { return CMD_SUCCESS; } -static bool SetPosHandler(const std::vector args) { +static bool SetPosHandler(std::shared_ptr Console, const std::vector args) { if (gGlobalCtx == nullptr) { SohImGui::console->SendErrorMessage("GlobalCtx == nullptr"); return CMD_FAILED; @@ -174,7 +175,7 @@ static bool SetPosHandler(const std::vector args) { return CMD_SUCCESS; } -static bool ResetHandler(std::vector args) { +static bool ResetHandler(std::shared_ptr Console, std::vector args) { if (gGlobalCtx == nullptr) { SohImGui::console->SendErrorMessage("GlobalCtx == nullptr"); return CMD_FAILED; @@ -195,7 +196,7 @@ const static std::map ammoItems{ { "magic_beans", ITEM_BEAN }, }; -static bool AmmoHandler(const std::vector& args) { +static bool AmmoHandler(std::shared_ptr Console, const std::vector& args) { if (args.size() != 3) { SohImGui::console->SendErrorMessage("[SOH] Unexpected arguments passed"); return CMD_FAILED; @@ -238,7 +239,7 @@ const static std::map bottleItems{ { "big_poe", ITEM_BIG_POE }, { "blue_fire", ITEM_BLUE_FIRE }, { "rutos_letter", ITEM_LETTER_RUTO }, }; -static bool BottleHandler(const std::vector& args) { +static bool BottleHandler(std::shared_ptr Console, const std::vector& args) { if (args.size() != 3) { SohImGui::console->SendErrorMessage("[SOH] Unexpected arguments passed"); return CMD_FAILED; @@ -270,7 +271,7 @@ static bool BottleHandler(const std::vector& args) { return CMD_SUCCESS; } -static bool BHandler(const std::vector& args) { +static bool BHandler(std::shared_ptr Console, const std::vector& args) { if (args.size() != 2) { SohImGui::console->SendErrorMessage("[SOH] Unexpected arguments passed"); return CMD_FAILED; @@ -280,7 +281,7 @@ static bool BHandler(const std::vector& args) { return CMD_SUCCESS; } -static bool ItemHandler(const std::vector& args) { +static bool ItemHandler(std::shared_ptr Console, const std::vector& args) { if (args.size() != 3) { SohImGui::console->SendErrorMessage("[SOH] Unexpected arguments passed"); return CMD_FAILED; @@ -291,7 +292,7 @@ static bool ItemHandler(const std::vector& args) { return CMD_SUCCESS; } -static bool EntranceHandler(const std::vector& args) { +static bool EntranceHandler(std::shared_ptr Console, const std::vector& args) { if (args.size() != 2) { SohImGui::console->SendErrorMessage("[SOH] Unexpected arguments passed"); return CMD_FAILED; @@ -312,7 +313,7 @@ static bool EntranceHandler(const std::vector& args) { gSaveContext.nextTransition = 11; } -static bool SaveStateHandler(const std::vector& args) { +static bool SaveStateHandler(std::shared_ptr Console, const std::vector& args) { unsigned int slot = OTRGlobals::Instance->gSaveStateMgr->GetCurrentSlot(); const SaveStateReturn rtn = OTRGlobals::Instance->gSaveStateMgr->AddRequest({ slot, RequestType::SAVE }); @@ -323,11 +324,10 @@ static bool SaveStateHandler(const std::vector& args) { case SaveStateReturn::FAIL_WRONG_GAMESTATE: SohImGui::console->SendErrorMessage("[SOH] Can not save a state outside of \"GamePlay\""); return CMD_FAILED; - } } -static bool LoadStateHandler(const std::vector& args) { +static bool LoadStateHandler(std::shared_ptr Console, const std::vector& args) { unsigned int slot = OTRGlobals::Instance->gSaveStateMgr->GetCurrentSlot(); const SaveStateReturn rtn = OTRGlobals::Instance->gSaveStateMgr->AddRequest({ slot, RequestType::LOAD }); @@ -348,7 +348,7 @@ static bool LoadStateHandler(const std::vector& args) { } -static bool StateSlotSelectHandler(const std::vector& args) { +static bool StateSlotSelectHandler(std::shared_ptr Console, const std::vector& args) { if (args.size() != 2) { SohImGui::console->SendErrorMessage("[SOH] Unexpected arguments passed"); return CMD_FAILED; @@ -404,7 +404,7 @@ static int CheckVarType(const std::string& input) return result; } -static bool SetCVarHandler(const std::vector& args) { +static bool SetCVarHandler(std::shared_ptr Console, const std::vector& args) { if (args.size() < 3) return CMD_FAILED; @@ -434,7 +434,7 @@ static bool SetCVarHandler(const std::vector& args) { } -static bool GetCVarHandler(const std::vector& args) { +static bool GetCVarHandler(std::shared_ptr Console, const std::vector& args) { if (args.size() < 2) return CMD_FAILED;