From da1b53420f0d30c90f478c018f9a7bd757de800a Mon Sep 17 00:00:00 2001 From: WerWolv Date: Fri, 13 Oct 2023 23:46:48 +0200 Subject: [PATCH] feat: Added debug variables to aid with development --- lib/external/libwolv | 2 +- lib/libimhex/CMakeLists.txt | 1 + .../include/hex/helpers/debugging.hpp | 48 +++++++++++++++++++ .../include/hex/ui/imgui_imhex_extensions.h | 30 ++++++++++++ lib/libimhex/source/helpers/debugging.cpp | 15 ++++++ .../source/content/data_visualizers.cpp | 46 ++++-------------- .../source/content/main_menu_items.cpp | 3 ++ 7 files changed, 107 insertions(+), 38 deletions(-) create mode 100644 lib/libimhex/include/hex/helpers/debugging.hpp create mode 100644 lib/libimhex/source/helpers/debugging.cpp diff --git a/lib/external/libwolv b/lib/external/libwolv index 7a35d84fa..c5698ed2c 160000 --- a/lib/external/libwolv +++ b/lib/external/libwolv @@ -1 +1 @@ -Subproject commit 7a35d84fa35eb7ac71745275c9b0289e1be8535b +Subproject commit c5698ed2cf3ea14382a597cc041fb6fc2674a982 diff --git a/lib/libimhex/CMakeLists.txt b/lib/libimhex/CMakeLists.txt index 49592b2c7..53daf137b 100644 --- a/lib/libimhex/CMakeLists.txt +++ b/lib/libimhex/CMakeLists.txt @@ -35,6 +35,7 @@ set(LIBIMHEX_SOURCES source/helpers/logger.cpp source/helpers/stacktrace.cpp source/helpers/tar.cpp + source/helpers/debugging.cpp source/providers/provider.cpp diff --git a/lib/libimhex/include/hex/helpers/debugging.hpp b/lib/libimhex/include/hex/helpers/debugging.hpp new file mode 100644 index 000000000..131aa61a8 --- /dev/null +++ b/lib/libimhex/include/hex/helpers/debugging.hpp @@ -0,0 +1,48 @@ +#pragma once + +#include +#include + +#include + + +#if defined(DEBUG) + #define DBG_DEFINE_DEBUG_VARIABLE(type, name) \ + static type name; \ + hex::dbg::impl::drawDebugVariable(name, WOLV_STRINGIFY(name)); +#else + #define DBG_DEFINE_DEBUG_VARIABLE(type, name) \ + static_assert(false, "Debug variables are only intended for use during development."); +#endif + +namespace hex::dbg { + + namespace impl { + bool &getDebugWindowState(); + + template + static void drawDebugVariable(T &variable, std::string_view name) { + if (!getDebugWindowState()) + return; + + if (ImGui::Begin("Debug Variables", &getDebugWindowState(), ImGuiWindowFlags_AlwaysAutoResize)) { + using Type = std::remove_cvref_t; + if constexpr (std::same_as) { + ImGui::Checkbox(name.data(), &variable); + } else if constexpr (std::integral || std::floating_point) { + ImGui::InputScalar(name.data(), ImGui::getImGuiDataType(), &variable); + } else if constexpr (std::same_as) { + ImGui::InputFloat2(name.data(), &variable.x); + } else if constexpr (std::same_as) { + ImGui::InputText(name.data(), variable); + } else if constexpr (std::same_as) { + ImGui::ColorEdit4(name.data(), &variable.Value.x, ImGuiColorEditFlags_AlphaBar); + } else { + static_assert(hex::always_false::value, "Unsupported type"); + } + } + ImGui::End(); + } + } + +} \ No newline at end of file diff --git a/lib/libimhex/include/hex/ui/imgui_imhex_extensions.h b/lib/libimhex/include/hex/ui/imgui_imhex_extensions.h index 756420772..4af1e6ad8 100644 --- a/lib/libimhex/include/hex/ui/imgui_imhex_extensions.h +++ b/lib/libimhex/include/hex/ui/imgui_imhex_extensions.h @@ -11,6 +11,7 @@ #include #include +#include #include @@ -246,4 +247,33 @@ namespace ImGui { bool DimmedIconToggle(const char *icon, bool *v); void TextOverlay(const char *text, ImVec2 pos); + + template + constexpr ImGuiDataType getImGuiDataType() { + if constexpr (std::same_as) return ImGuiDataType_U8; + else if constexpr (std::same_as) return ImGuiDataType_U16; + else if constexpr (std::same_as) return ImGuiDataType_U32; + else if constexpr (std::same_as) return ImGuiDataType_U64; + else if constexpr (std::same_as) return ImGuiDataType_S8; + else if constexpr (std::same_as) return ImGuiDataType_S16; + else if constexpr (std::same_as) return ImGuiDataType_S32; + else if constexpr (std::same_as) return ImGuiDataType_S64; + else if constexpr (std::same_as) return ImGuiDataType_Float; + else if constexpr (std::same_as) return ImGuiDataType_Double; + else static_assert(hex::always_false::value, "Invalid data type!"); + } + + template + constexpr const char *getFormatLengthSpecifier() { + if constexpr (std::same_as) return "hh"; + else if constexpr (std::same_as) return "h"; + else if constexpr (std::same_as) return "l"; + else if constexpr (std::same_as) return "ll"; + else if constexpr (std::same_as) return "hh"; + else if constexpr (std::same_as) return "h"; + else if constexpr (std::same_as) return "l"; + else if constexpr (std::same_as) return "ll"; + else static_assert(hex::always_false::value, "Invalid data type!"); + } + } \ No newline at end of file diff --git a/lib/libimhex/source/helpers/debugging.cpp b/lib/libimhex/source/helpers/debugging.cpp new file mode 100644 index 000000000..d996866f2 --- /dev/null +++ b/lib/libimhex/source/helpers/debugging.cpp @@ -0,0 +1,15 @@ +#include + +namespace hex::dbg { + + namespace impl { + + bool &getDebugWindowState() { + static bool state = false; + + return state; + } + + } + +} \ No newline at end of file diff --git a/plugins/builtin/source/content/data_visualizers.cpp b/plugins/builtin/source/content/data_visualizers.cpp index a55cd7ff7..dfcef3ea9 100644 --- a/plugins/builtin/source/content/data_visualizers.cpp +++ b/plugins/builtin/source/content/data_visualizers.cpp @@ -11,34 +11,6 @@ namespace hex::plugin::builtin { - template - constexpr ImGuiDataType getImGuiDataType() { - if constexpr (std::same_as) return ImGuiDataType_U8; - else if constexpr (std::same_as) return ImGuiDataType_U16; - else if constexpr (std::same_as) return ImGuiDataType_U32; - else if constexpr (std::same_as) return ImGuiDataType_U64; - else if constexpr (std::same_as) return ImGuiDataType_S8; - else if constexpr (std::same_as) return ImGuiDataType_S16; - else if constexpr (std::same_as) return ImGuiDataType_S32; - else if constexpr (std::same_as) return ImGuiDataType_S64; - else if constexpr (std::same_as) return ImGuiDataType_Float; - else if constexpr (std::same_as) return ImGuiDataType_Double; - else static_assert(hex::always_false::value, "Invalid data type!"); - } - - template - constexpr const char *getFormatLengthSpecifier() { - if constexpr (std::same_as) return "hh"; - else if constexpr (std::same_as) return "h"; - else if constexpr (std::same_as) return "l"; - else if constexpr (std::same_as) return "ll"; - else if constexpr (std::same_as) return "hh"; - else if constexpr (std::same_as) return "h"; - else if constexpr (std::same_as) return "l"; - else if constexpr (std::same_as) return "ll"; - else static_assert(hex::always_false::value, "Invalid data type!"); - } - template class DataVisualizerHexadecimal : public hex::ContentRegistry::HexEditor::DataVisualizer { public: @@ -57,7 +29,7 @@ namespace hex::plugin::builtin { hex::unused(address, startedEditing); if (size == ByteCount) { - return drawDefaultScalarEditingTextBox(address, getFormatString(upperCase), getImGuiDataType(), data, ImGuiInputTextFlags_CharsHexadecimal); + return drawDefaultScalarEditingTextBox(address, getFormatString(upperCase), ImGui::getImGuiDataType(), data, ImGuiInputTextFlags_CharsHexadecimal); } else return false; @@ -67,8 +39,8 @@ namespace hex::plugin::builtin { constexpr static inline auto ByteCount = sizeof(T); constexpr static inline auto CharCount = ByteCount * 2; - const static inline auto FormattingUpperCase = hex::format("%0{}{}X", CharCount, getFormatLengthSpecifier()); - const static inline auto FormattingLowerCase = hex::format("%0{}{}x", CharCount, getFormatLengthSpecifier()); + const static inline auto FormattingUpperCase = hex::format("%0{}{}X", CharCount, ImGui::getFormatLengthSpecifier()); + const static inline auto FormattingLowerCase = hex::format("%0{}{}x", CharCount, ImGui::getFormatLengthSpecifier()); const char *getFormatString(bool upperCase) { if (upperCase) @@ -110,7 +82,7 @@ namespace hex::plugin::builtin { hex::unused(address, startedEditing); if (size == ByteCount) { - return drawDefaultScalarEditingTextBox(address, getFormatString(upperCase), getImGuiDataType(), data, ImGuiInputTextFlags_None); + return drawDefaultScalarEditingTextBox(address, getFormatString(upperCase), ImGui::getImGuiDataType(), data, ImGuiInputTextFlags_None); } else return false; @@ -120,8 +92,8 @@ namespace hex::plugin::builtin { constexpr static inline auto ByteCount = 1; constexpr static inline auto CharCount = ByteCount * 2; - const static inline auto FormattingUpperCase = hex::format("%0{}{}X", CharCount, getFormatLengthSpecifier()); - const static inline auto FormattingLowerCase = hex::format("%0{}{}x", CharCount, getFormatLengthSpecifier()); + const static inline auto FormattingUpperCase = hex::format("%0{}{}X", CharCount, ImGui::getFormatLengthSpecifier()); + const static inline auto FormattingLowerCase = hex::format("%0{}{}x", CharCount, ImGui::getFormatLengthSpecifier()); static const char *getFormatString(bool upperCase) { if (upperCase) @@ -153,7 +125,7 @@ namespace hex::plugin::builtin { hex::unused(address, upperCase, startedEditing); if (size == ByteCount) { - return drawDefaultScalarEditingTextBox(address, FormatString.c_str(), getImGuiDataType(), data, ImGuiInputTextFlags_None); + return drawDefaultScalarEditingTextBox(address, FormatString.c_str(), ImGui::getImGuiDataType(), data, ImGuiInputTextFlags_None); } else return false; @@ -163,7 +135,7 @@ namespace hex::plugin::builtin { constexpr static inline auto ByteCount = sizeof(T); constexpr static inline auto CharCount = std::numeric_limits::digits10 + 2; - const static inline auto FormatString = hex::format("%{}{}{}", CharCount, getFormatLengthSpecifier(), std::is_signed::value ? "d" : "u"); + const static inline auto FormatString = hex::format("%{}{}{}", CharCount, ImGui::getFormatLengthSpecifier(), std::is_signed::value ? "d" : "u"); const char *getFormatString() { return FormatString.c_str(); @@ -190,7 +162,7 @@ namespace hex::plugin::builtin { hex::unused(address, upperCase, startedEditing); if (size == ByteCount) { - return drawDefaultScalarEditingTextBox(address, getFormatString(upperCase), getImGuiDataType(), data, ImGuiInputTextFlags_CharsScientific); + return drawDefaultScalarEditingTextBox(address, getFormatString(upperCase), ImGui::getImGuiDataType(), data, ImGuiInputTextFlags_CharsScientific); } else return false; diff --git a/plugins/builtin/source/content/main_menu_items.cpp b/plugins/builtin/source/content/main_menu_items.cpp index 1fee6e06d..a8d4cd6c4 100644 --- a/plugins/builtin/source/content/main_menu_items.cpp +++ b/plugins/builtin/source/content/main_menu_items.cpp @@ -10,6 +10,7 @@ #include #include +#include #include #include @@ -441,6 +442,8 @@ namespace hex::plugin::builtin { #if defined(DEBUG) ImGui::MenuItem("hex.builtin.menu.view.demo"_lang, "", &g_demoWindowOpen); + + ImGui::MenuItem("hex.builtin.menu.view.debug"_lang, "", &hex::dbg::impl::getDebugWindowState()); #endif });