feat: Added option to enable debug features in release builds

This commit is contained in:
WerWolv 2025-01-25 15:38:57 +01:00
parent f4bad0053a
commit a44959ce1b
5 changed files with 114 additions and 82 deletions

View File

@ -44,4 +44,7 @@ namespace hex::dbg {
}
}
bool debugModeEnabled();
void setDebugModeEnabled(bool enabled);
}

View File

@ -12,4 +12,13 @@ namespace hex::dbg {
}
static bool s_debugMode = false;
bool debugModeEnabled() {
return s_debugMode;
}
void setDebugModeEnabled(bool enabled) {
s_debugMode = enabled;
}
}

View File

@ -7,6 +7,7 @@
#include <hex/helpers/utils.hpp>
#include <hex/helpers/fmt.hpp>
#include <hex/providers/provider.hpp>
#include <toasts/toast_notification.hpp>
#include <wolv/math_eval/math_evaluator.hpp>
#include <wolv/utils/guards.hpp>
@ -297,7 +298,25 @@ namespace hex::plugin::builtin {
return hex::format("hex.builtin.command.cmd.result"_lang, input.data());
},
[](auto input) {
hex::executeCommand(input);
if (input.starts_with("imhex ")) {
// Handle ImHex internal commands
auto command = input.substr(6);
auto parts = splitConversionCommandInput(command);
if (!parts.empty()) {
if (parts.size() == 2 && parts[0] == "debug") {
if (parts[1] == "on") {
ContentRegistry::Settings::write<bool>("hex.builtin.setting.general", "hex.builtin.setting.general.debug_mode_enabled", true);
ui::ToastInfo::open("Debug mode enabled!");
} else if (parts[1] == "off") {
ContentRegistry::Settings::write<bool>("hex.builtin.setting.general", "hex.builtin.setting.general.debug_mode_enabled", false);
ui::ToastInfo::open("Debug mode disabled!");
}
}
}
} else {
hex::executeCommand(input);
}
return std::nullopt;
});

View File

@ -23,7 +23,7 @@
namespace hex::plugin::builtin {
void addTitleBarButtons() {
#if defined(DEBUG)
if (dbg::debugModeEnabled()) {
ContentRegistry::Interface::addTitleBarButton(ICON_VS_DEBUG, "hex.builtin.title_bar_button.debug_build", []{
if (ImGui::GetIO().KeyShift) {
RequestOpenPopup::post("DebugMenu");
@ -31,7 +31,7 @@ namespace hex::plugin::builtin {
hex::openWebpage("https://imhex.werwolv.net/debug");
}
});
#endif
}
ContentRegistry::Interface::addTitleBarButton(ICON_VS_SMILEY, "hex.builtin.title_bar_button.feedback", []{
hex::openWebpage("https://github.com/WerWolv/ImHex/discussions/categories/feedback");
@ -50,89 +50,85 @@ namespace hex::plugin::builtin {
}
}
#if defined(DEBUG)
static void drawDebugPopup() {
static bool showImGuiDemo = false;
static bool showImPlotDemo = false;
static bool showImPlot3DDemo = false;
static void drawDebugPopup() {
static bool showImGuiDemo = false;
static bool showImPlotDemo = false;
static bool showImPlot3DDemo = false;
ImGui::SetNextWindowSize(scaled({ 300, 150 }), ImGuiCond_Always);
if (ImGui::BeginPopup("DebugMenu")) {
if (ImGui::BeginTabBar("DebugTabBar")) {
if (ImGui::BeginTabItem("ImHex")) {
if (ImGui::BeginChild("Scrolling", ImGui::GetContentRegionAvail())) {
ImGui::Checkbox("Show Debug Variables", &dbg::impl::getDebugWindowState());
ImGui::SetNextWindowSize(scaled({ 300, 150 }), ImGuiCond_Always);
if (ImGui::BeginPopup("DebugMenu")) {
if (ImGui::BeginTabBar("DebugTabBar")) {
if (ImGui::BeginTabItem("ImHex")) {
if (ImGui::BeginChild("Scrolling", ImGui::GetContentRegionAvail())) {
ImGui::Checkbox("Show Debug Variables", &dbg::impl::getDebugWindowState());
ImGuiExt::Header("Information");
ImGuiExt::TextFormatted("Running Tasks: {0}", TaskManager::getRunningTaskCount());
ImGuiExt::TextFormatted("Running Background Tasks: {0}", TaskManager::getRunningBackgroundTaskCount());
ImGuiExt::TextFormatted("Last Frame Time: {0:.3f}ms", ImHexApi::System::getLastFrameTime() * 1000.0F);
}
ImGui::EndChild();
ImGui::EndTabItem();
ImGuiExt::Header("Information");
ImGuiExt::TextFormatted("Running Tasks: {0}", TaskManager::getRunningTaskCount());
ImGuiExt::TextFormatted("Running Background Tasks: {0}", TaskManager::getRunningBackgroundTaskCount());
ImGuiExt::TextFormatted("Last Frame Time: {0:.3f}ms", ImHexApi::System::getLastFrameTime() * 1000.0F);
}
if (ImGui::BeginTabItem("ImGui")) {
if (ImGui::BeginChild("Scrolling", ImGui::GetContentRegionAvail())) {
auto ctx = ImGui::GetCurrentContext();
ImGui::Checkbox("Show ImGui Demo", &showImGuiDemo);
ImGui::Checkbox("Show ImPlot Demo", &showImPlotDemo);
ImGui::Checkbox("Show ImPlot3D Demo", &showImPlot3DDemo);
ImGui::EndChild();
if (ImGui::Button("Trigger Breakpoint in Item") || ctx->DebugItemPickerActive)
ImGui::DebugStartItemPicker();
}
ImGui::EndChild();
ImGui::EndTabItem();
}
if (ImGui::BeginTabItem("Crashes")) {
if (ImGui::BeginChild("Scrolling", ImGui::GetContentRegionAvail())) {
if (ImGui::Button("Throw Exception")) {
TaskManager::doLater([] {
throw std::runtime_error("Test exception");
});
}
if (ImGui::Button("Access Invalid Memory")) {
TaskManager::doLater([] {
*reinterpret_cast<u32*>(0x10) = 0x10;
std::unreachable();
});
}
if (ImGui::Button("Raise SIGSEGV")) {
TaskManager::doLater([] {
raise(SIGSEGV);
});
}
if (ImGui::Button("Corrupt Memory")) {
TaskManager::doLater([] {
auto bytes = new u8[0xFFFFF];
delete[] bytes;
delete[] bytes;
});
}
}
ImGui::EndChild();
ImGui::EndTabItem();
}
ImGui::EndTabBar();
ImGui::EndTabItem();
}
ImGui::EndPopup();
}
if (ImGui::BeginTabItem("ImGui")) {
if (ImGui::BeginChild("Scrolling", ImGui::GetContentRegionAvail())) {
auto ctx = ImGui::GetCurrentContext();
ImGui::Checkbox("Show ImGui Demo", &showImGuiDemo);
ImGui::Checkbox("Show ImPlot Demo", &showImPlotDemo);
ImGui::Checkbox("Show ImPlot3D Demo", &showImPlot3DDemo);
if (showImGuiDemo)
ImGui::ShowDemoWindow(&showImGuiDemo);
if (showImPlotDemo)
ImPlot::ShowDemoWindow(&showImPlotDemo);
if (showImPlot3DDemo)
ImPlot3D::ShowDemoWindow(&showImPlot3DDemo);
if (ImGui::Button("Trigger Breakpoint in Item") || ctx->DebugItemPickerActive)
ImGui::DebugStartItemPicker();
}
ImGui::EndChild();
ImGui::EndTabItem();
}
if (ImGui::BeginTabItem("Crashes")) {
if (ImGui::BeginChild("Scrolling", ImGui::GetContentRegionAvail())) {
if (ImGui::Button("Throw Exception")) {
TaskManager::doLater([] {
throw std::runtime_error("Test exception");
});
}
if (ImGui::Button("Access Invalid Memory")) {
TaskManager::doLater([] {
*reinterpret_cast<u32*>(0x10) = 0x10;
std::unreachable();
});
}
if (ImGui::Button("Raise SIGSEGV")) {
TaskManager::doLater([] {
raise(SIGSEGV);
});
}
if (ImGui::Button("Corrupt Memory")) {
TaskManager::doLater([] {
auto bytes = new u8[0xFFFFF];
delete[] bytes;
delete[] bytes;
});
}
}
ImGui::EndChild();
ImGui::EndTabItem();
}
ImGui::EndTabBar();
}
ImGui::EndPopup();
}
#endif
if (showImGuiDemo)
ImGui::ShowDemoWindow(&showImGuiDemo);
if (showImPlotDemo)
ImPlot::ShowDemoWindow(&showImPlotDemo);
if (showImPlot3DDemo)
ImPlot3D::ShowDemoWindow(&showImPlot3DDemo);
}
static bool s_drawDragDropOverlay = false;
static void drawDragNDropOverlay() {
@ -178,9 +174,9 @@ namespace hex::plugin::builtin {
EventFrameEnd::subscribe(drawGlobalPopups);
EventFrameEnd::subscribe(drawDragNDropOverlay);
#if defined(DEBUG)
if (dbg::debugModeEnabled()) {
EventFrameEnd::subscribe(drawDebugPopup);
#endif
}
EventFileDragged::subscribe([](bool entered) {
s_drawDragDropOverlay = entered;
@ -196,7 +192,7 @@ namespace hex::plugin::builtin {
});
}
#if defined(DEBUG)
if (dbg::debugModeEnabled()) {
ContentRegistry::Interface::addFooterItem([] {
static float framerate = 0;
if (ImGuiExt::HasSecondPassed()) {
@ -237,7 +233,7 @@ namespace hex::plugin::builtin {
ImGui::PopStyleVar();
}
});
#endif
}
ContentRegistry::Interface::addFooterItem([] {
static bool shouldResetProgress = false;

View File

@ -3,6 +3,7 @@
#include <hex/api/content_registry.hpp>
#include <hex/api/task_manager.hpp>
#include <hex/helpers/logger.hpp>
#include <hex/helpers/debugging.hpp>
#include <romfs/romfs.hpp>
#include <nlohmann/json.hpp>
@ -135,5 +136,9 @@ IMHEX_PLUGIN_SETUP("Built-in", "WerWolv", "Default ImHex functionality") {
// Show a warning banner on debug builds
#if defined(DEBUG)
ui::BannerIcon::open(ICON_VS_ERROR, "You're running a Debug build of ImHex. Performance will be degraded!", ImColor(153, 58, 58));
dbg::setDebugModeEnabled(true);
#else
const auto enabled = ContentRegistry::Settings::read<bool>("hex.builtin.setting.general", "hex.builtin.setting.general.debug_mode_enabled", false);
dbg::setDebugModeEnabled(enabled);
#endif
}