From 8a04d2f477c6ac2ef0a4c604da1982108ca490ad Mon Sep 17 00:00:00 2001 From: WerWolv Date: Thu, 28 Aug 2025 19:57:51 +0200 Subject: [PATCH] feat: Added fixed-point row to data inspector --- plugins/builtin/romfs/lang/en_US.json | 3 ++ .../builtin/source/content/data_inspector.cpp | 40 +++++++++++++++++-- 2 files changed, 40 insertions(+), 3 deletions(-) diff --git a/plugins/builtin/romfs/lang/en_US.json b/plugins/builtin/romfs/lang/en_US.json index 650fc758c..4ab276282 100644 --- a/plugins/builtin/romfs/lang/en_US.json +++ b/plugins/builtin/romfs/lang/en_US.json @@ -83,6 +83,9 @@ "hex.builtin.inspector.dos_date": "DOS Date", "hex.builtin.inspector.dos_time": "DOS Time", "hex.builtin.inspector.double": "double", + "hex.builtin.inspector.fixed_point": "Fixed Point", + "hex.builtin.inspector.fixed_point.total": "Total: {0} bits", + "hex.builtin.inspector.fixed_point.fraction": "Fraction: {0} bits", "hex.builtin.inspector.float": "float", "hex.builtin.inspector.float16": "half float", "hex.builtin.inspector.fp24": "fp24", diff --git a/plugins/builtin/source/content/data_inspector.cpp b/plugins/builtin/source/content/data_inspector.cpp index 926207391..e5a1adcc0 100644 --- a/plugins/builtin/source/content/data_inspector.cpp +++ b/plugins/builtin/source/content/data_inspector.cpp @@ -310,6 +310,42 @@ namespace hex::plugin::builtin { } ); + ContentRegistry::DataInspector::add("hex.builtin.inspector.fixed_point", 1, [totalBits = int(16), fractionBits = int(8)](const std::vector &, std::endian endian, Style style) mutable { + std::string value; + + const auto currSelection = ImHexApi::HexEditor::getSelection(); + const u32 sizeBytes = (totalBits + 7) / 8; + if (currSelection.has_value() && currSelection->size >= sizeBytes) { + u64 fixedPointValue = 0x00; + ImHexApi::Provider::get()->read(currSelection->address, &fixedPointValue, std::min(sizeof(fixedPointValue), currSelection->size)); + + fixedPointValue = changeEndianness(fixedPointValue, sizeBytes, endian); + fixedPointValue &= hex::bitmask(totalBits); + + const auto formatString = style == Style::Hexadecimal ? "{0:a}" : "{0:G}"; + value = fmt::format(fmt::runtime(formatString), double(fixedPointValue) / double(1ULL << fractionBits)); + } else { + value = "???"; + } + + return [&, value, totalBitsCopy = totalBits, fractionBitsCopy = fractionBits]() mutable -> std::string { + ContentRegistry::DataInspector::drawMenuItems([&] { + ImGui::SliderInt("##total_bits", &totalBits, 1, 64, fmt::format("hex.builtin.inspector.fixed_point.total"_lang, totalBits).c_str(), ImGuiSliderFlags_AlwaysClamp); + ImGui::SliderInt("##fractional_bits", &fractionBits, 0, totalBits - 1, fmt::format("hex.builtin.inspector.fixed_point.fraction"_lang, fractionBits).c_str(), ImGuiSliderFlags_AlwaysClamp); + + if (fractionBits >= totalBits) { + fractionBits = totalBits - 1; + } + }); + + ImGui::TextUnformatted(value.c_str()); + ImGui::SameLine(); + ImGui::TextDisabled("(fp%d.%d)", totalBitsCopy - fractionBitsCopy, fractionBitsCopy); + + return value; + }; + }); + ContentRegistry::DataInspector::add("hex.builtin.inspector.sleb128", 1, (16 * 8 / 7) + 1, [](auto buffer, auto endian, auto style) { std::ignore = endian; @@ -496,11 +532,9 @@ namespace hex::plugin::builtin { std::ignore = endian; std::ignore = style; - auto currSelection = ImHexApi::HexEditor::getSelection(); - - std::string value, copyValue; + auto currSelection = ImHexApi::HexEditor::getSelection(); if (currSelection.has_value()) { std::vector stringBuffer(std::min(currSelection->size, 0x1000), 0x00); ImHexApi::Provider::get()->read(currSelection->address, stringBuffer.data(), stringBuffer.size());