2022-05-27 20:42:07 +02:00
|
|
|
#include <hex/api/content_registry.hpp>
|
|
|
|
|
|
|
|
#include <imgui.h>
|
|
|
|
#include <hex/ui/imgui_imhex_extensions.h>
|
2023-07-09 12:53:31 +02:00
|
|
|
|
2022-09-28 18:30:41 +02:00
|
|
|
#include <hex/helpers/utils.hpp>
|
2022-05-27 20:42:07 +02:00
|
|
|
|
2023-07-09 12:53:31 +02:00
|
|
|
#include <wolv/utils/string.hpp>
|
|
|
|
|
|
|
|
|
2022-05-27 20:42:07 +02:00
|
|
|
namespace hex::plugin::builtin {
|
|
|
|
|
2022-06-25 12:19:59 +02:00
|
|
|
template<std::integral T>
|
2022-05-27 20:42:07 +02:00
|
|
|
class DataVisualizerHexadecimal : public hex::ContentRegistry::HexEditor::DataVisualizer {
|
|
|
|
public:
|
2023-11-10 20:47:08 +01:00
|
|
|
explicit DataVisualizerHexadecimal(const std::string &name) : DataVisualizer(name, ByteCount, CharCount) { }
|
2022-05-27 20:42:07 +02:00
|
|
|
|
|
|
|
void draw(u64 address, const u8 *data, size_t size, bool upperCase) override {
|
|
|
|
hex::unused(address);
|
|
|
|
|
|
|
|
if (size == ByteCount)
|
2024-06-26 19:15:49 +02:00
|
|
|
ImGuiExt::TextFormatted(upperCase ? "{:0{}X}" : "{:0{}x}", *reinterpret_cast<const T*>(data), sizeof(T) * 2);
|
2022-05-27 20:42:07 +02:00
|
|
|
else
|
2023-11-16 22:24:06 +01:00
|
|
|
ImGuiExt::TextFormatted("{: {}s}", CharCount);
|
2022-05-27 20:42:07 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
bool drawEditing(u64 address, u8 *data, size_t size, bool upperCase, bool startedEditing) override {
|
|
|
|
hex::unused(address, startedEditing);
|
|
|
|
|
|
|
|
if (size == ByteCount) {
|
2023-11-16 22:24:06 +01:00
|
|
|
return drawDefaultScalarEditingTextBox(address, getFormatString(upperCase), ImGuiExt::getImGuiDataType<T>(), data, ImGuiInputTextFlags_CharsHexadecimal);
|
2023-12-27 16:33:49 +01:00
|
|
|
} else {
|
2022-05-27 20:42:07 +02:00
|
|
|
return false;
|
2023-12-27 16:33:49 +01:00
|
|
|
}
|
2022-05-27 20:42:07 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
constexpr static inline auto ByteCount = sizeof(T);
|
|
|
|
constexpr static inline auto CharCount = ByteCount * 2;
|
|
|
|
|
2023-11-16 22:24:06 +01:00
|
|
|
const static inline auto FormattingUpperCase = hex::format("%0{}{}X", CharCount, ImGuiExt::getFormatLengthSpecifier<T>());
|
|
|
|
const static inline auto FormattingLowerCase = hex::format("%0{}{}x", CharCount, ImGuiExt::getFormatLengthSpecifier<T>());
|
2022-05-27 20:42:07 +02:00
|
|
|
|
|
|
|
const char *getFormatString(bool upperCase) {
|
|
|
|
if (upperCase)
|
|
|
|
return FormattingUpperCase.c_str();
|
|
|
|
else
|
|
|
|
return FormattingLowerCase.c_str();
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
class DataVisualizerHexii : public hex::ContentRegistry::HexEditor::DataVisualizer {
|
|
|
|
public:
|
2023-06-05 09:07:58 +02:00
|
|
|
DataVisualizerHexii() : DataVisualizer("hex.builtin.visualizer.hexii", ByteCount, CharCount) { }
|
2022-05-27 20:42:07 +02:00
|
|
|
|
|
|
|
void draw(u64 address, const u8 *data, size_t size, bool upperCase) override {
|
|
|
|
hex::unused(address);
|
|
|
|
|
|
|
|
if (size == ByteCount) {
|
|
|
|
const u8 c = data[0];
|
|
|
|
switch (c) {
|
|
|
|
case 0x00:
|
2024-06-26 19:41:06 +02:00
|
|
|
ImGui::TextUnformatted(" ");
|
2022-05-27 20:42:07 +02:00
|
|
|
break;
|
|
|
|
case 0xFF:
|
|
|
|
ImGui::TextDisabled("##");
|
|
|
|
break;
|
|
|
|
default:
|
2023-01-04 14:03:09 +01:00
|
|
|
if (c >= ' ' && c <= '~')
|
|
|
|
ImGui::Text(".%c", c);
|
|
|
|
else
|
|
|
|
ImGui::Text(getFormatString(upperCase), c);
|
2022-05-27 20:42:07 +02:00
|
|
|
break;
|
|
|
|
}
|
2023-12-27 16:33:49 +01:00
|
|
|
} else {
|
2023-11-16 22:24:06 +01:00
|
|
|
ImGuiExt::TextFormatted("{: {}s}", CharCount);
|
2023-12-27 16:33:49 +01:00
|
|
|
}
|
2022-05-27 20:42:07 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
bool drawEditing(u64 address, u8 *data, size_t size, bool upperCase, bool startedEditing) override {
|
|
|
|
hex::unused(address, startedEditing);
|
|
|
|
|
|
|
|
if (size == ByteCount) {
|
2023-11-16 22:24:06 +01:00
|
|
|
return drawDefaultScalarEditingTextBox(address, getFormatString(upperCase), ImGuiExt::getImGuiDataType<u8>(), data, ImGuiInputTextFlags_None);
|
2023-12-27 16:33:49 +01:00
|
|
|
} else {
|
2022-05-27 20:42:07 +02:00
|
|
|
return false;
|
2023-12-27 16:33:49 +01:00
|
|
|
}
|
2022-05-27 20:42:07 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
constexpr static inline auto ByteCount = 1;
|
|
|
|
constexpr static inline auto CharCount = ByteCount * 2;
|
|
|
|
|
2023-11-16 22:24:06 +01:00
|
|
|
const static inline auto FormattingUpperCase = hex::format("%0{}{}X", CharCount, ImGuiExt::getFormatLengthSpecifier<u8>());
|
|
|
|
const static inline auto FormattingLowerCase = hex::format("%0{}{}x", CharCount, ImGuiExt::getFormatLengthSpecifier<u8>());
|
2022-05-27 20:42:07 +02:00
|
|
|
|
2022-10-02 17:30:26 +02:00
|
|
|
static const char *getFormatString(bool upperCase) {
|
2022-05-27 20:42:07 +02:00
|
|
|
if (upperCase)
|
|
|
|
return FormattingUpperCase.c_str();
|
|
|
|
else
|
|
|
|
return FormattingLowerCase.c_str();
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2022-06-25 12:19:59 +02:00
|
|
|
template<std::integral T>
|
2022-05-27 20:42:07 +02:00
|
|
|
class DataVisualizerDecimal : public hex::ContentRegistry::HexEditor::DataVisualizer {
|
|
|
|
public:
|
2023-11-10 20:47:08 +01:00
|
|
|
explicit DataVisualizerDecimal(const std::string &name) : DataVisualizer(name, ByteCount, CharCount) { }
|
2022-05-27 20:42:07 +02:00
|
|
|
|
|
|
|
void draw(u64 address, const u8 *data, size_t size, bool upperCase) override {
|
|
|
|
hex::unused(address, upperCase);
|
|
|
|
|
|
|
|
if (size == ByteCount) {
|
2023-11-10 20:47:08 +01:00
|
|
|
if (std::is_signed_v<T>)
|
2022-05-27 20:42:07 +02:00
|
|
|
ImGui::Text(getFormatString(), static_cast<i64>(*reinterpret_cast<const T*>(data)));
|
|
|
|
else
|
|
|
|
ImGui::Text(getFormatString(), static_cast<u64>(*reinterpret_cast<const T*>(data)));
|
2023-12-27 16:33:49 +01:00
|
|
|
} else {
|
2023-11-16 22:24:06 +01:00
|
|
|
ImGuiExt::TextFormatted("{: {}s}", CharCount);
|
2023-12-27 16:33:49 +01:00
|
|
|
}
|
2022-05-27 20:42:07 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
bool drawEditing(u64 address, u8 *data, size_t size, bool upperCase, bool startedEditing) override {
|
|
|
|
hex::unused(address, upperCase, startedEditing);
|
|
|
|
|
|
|
|
if (size == ByteCount) {
|
2023-11-16 22:24:06 +01:00
|
|
|
return drawDefaultScalarEditingTextBox(address, FormatString.c_str(), ImGuiExt::getImGuiDataType<T>(), data, ImGuiInputTextFlags_None);
|
2023-12-27 16:33:49 +01:00
|
|
|
} else {
|
2022-05-27 20:42:07 +02:00
|
|
|
return false;
|
2023-12-27 16:33:49 +01:00
|
|
|
}
|
2022-05-27 20:42:07 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
constexpr static inline auto ByteCount = sizeof(T);
|
|
|
|
constexpr static inline auto CharCount = std::numeric_limits<T>::digits10 + 2;
|
|
|
|
|
2023-11-16 22:24:06 +01:00
|
|
|
const static inline auto FormatString = hex::format("%{}{}{}", CharCount, ImGuiExt::getFormatLengthSpecifier<T>(), std::is_signed_v<T> ? "d" : "u");
|
2022-05-27 20:42:07 +02:00
|
|
|
|
|
|
|
const char *getFormatString() {
|
|
|
|
return FormatString.c_str();
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2022-09-28 18:30:41 +02:00
|
|
|
enum class Float16 : u16 {};
|
|
|
|
|
|
|
|
template<typename T>
|
2022-05-27 20:42:07 +02:00
|
|
|
class DataVisualizerFloatingPoint : public hex::ContentRegistry::HexEditor::DataVisualizer {
|
|
|
|
public:
|
2023-11-10 20:47:08 +01:00
|
|
|
explicit DataVisualizerFloatingPoint(const std::string &name) : DataVisualizer(name, ByteCount, CharCount) { }
|
2022-05-27 20:42:07 +02:00
|
|
|
|
|
|
|
void draw(u64 address, const u8 *data, size_t size, bool upperCase) override {
|
|
|
|
hex::unused(address);
|
|
|
|
|
|
|
|
if (size == ByteCount)
|
|
|
|
ImGui::Text(getFormatString(upperCase), *reinterpret_cast<const T*>(data));
|
|
|
|
else
|
2023-11-16 22:24:06 +01:00
|
|
|
ImGuiExt::TextFormatted("{: {}s}", CharCount);
|
2022-05-27 20:42:07 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
bool drawEditing(u64 address, u8 *data, size_t size, bool upperCase, bool startedEditing) override {
|
|
|
|
hex::unused(address, upperCase, startedEditing);
|
|
|
|
|
|
|
|
if (size == ByteCount) {
|
2023-11-16 22:24:06 +01:00
|
|
|
return drawDefaultScalarEditingTextBox(address, getFormatString(upperCase), ImGuiExt::getImGuiDataType<T>(), data, ImGuiInputTextFlags_CharsScientific);
|
2023-12-27 16:33:49 +01:00
|
|
|
} else {
|
2022-05-27 20:42:07 +02:00
|
|
|
return false;
|
2023-12-27 16:33:49 +01:00
|
|
|
}
|
2022-05-27 20:42:07 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
constexpr static inline auto ByteCount = sizeof(T);
|
|
|
|
constexpr static inline auto CharCount = 14;
|
|
|
|
|
2022-08-04 09:05:46 +02:00
|
|
|
const static inline auto FormatStringUpperCase = hex::format("%{}G", CharCount);
|
|
|
|
const static inline auto FormatStringLowerCase = hex::format("%{}g", CharCount);
|
2022-05-27 20:42:07 +02:00
|
|
|
|
2023-11-10 20:47:08 +01:00
|
|
|
const char *getFormatString(bool upperCase) const {
|
2022-05-27 20:42:07 +02:00
|
|
|
if (upperCase)
|
|
|
|
return FormatStringUpperCase.c_str();
|
|
|
|
else
|
|
|
|
return FormatStringLowerCase.c_str();
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2022-09-28 18:30:41 +02:00
|
|
|
template<>
|
|
|
|
class DataVisualizerFloatingPoint<Float16> : public hex::ContentRegistry::HexEditor::DataVisualizer {
|
|
|
|
public:
|
2023-11-10 20:47:08 +01:00
|
|
|
explicit DataVisualizerFloatingPoint(const std::string &name) : DataVisualizer(name, ByteCount, CharCount) { }
|
2022-09-28 18:30:41 +02:00
|
|
|
|
|
|
|
void draw(u64 address, const u8 *data, size_t size, bool upperCase) override {
|
|
|
|
hex::unused(address);
|
|
|
|
|
|
|
|
if (size == ByteCount)
|
|
|
|
ImGui::Text(getFormatString(upperCase), hex::float16ToFloat32(*reinterpret_cast<const u16*>(data)));
|
|
|
|
else
|
2023-11-16 22:24:06 +01:00
|
|
|
ImGuiExt::TextFormatted("{: {}s}", CharCount);
|
2022-09-28 18:30:41 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
bool drawEditing(u64 address, u8 *data, size_t size, bool upperCase, bool startedEditing) override {
|
|
|
|
hex::unused(startedEditing);
|
|
|
|
|
|
|
|
this->draw(address, data, size, upperCase);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
constexpr static inline auto ByteCount = sizeof(Float16);
|
|
|
|
constexpr static inline auto CharCount = 14;
|
|
|
|
|
|
|
|
const static inline auto FormatStringUpperCase = hex::format("%{}G", CharCount);
|
|
|
|
const static inline auto FormatStringLowerCase = hex::format("%{}g", CharCount);
|
|
|
|
|
2022-10-02 17:30:26 +02:00
|
|
|
static const char *getFormatString(bool upperCase) {
|
2022-09-28 18:30:41 +02:00
|
|
|
if (upperCase)
|
|
|
|
return FormatStringUpperCase.c_str();
|
|
|
|
else
|
|
|
|
return FormatStringLowerCase.c_str();
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2022-05-27 20:42:07 +02:00
|
|
|
class DataVisualizerRGBA8 : public hex::ContentRegistry::HexEditor::DataVisualizer {
|
|
|
|
public:
|
2023-06-05 09:07:58 +02:00
|
|
|
DataVisualizerRGBA8() : DataVisualizer("hex.builtin.visualizer.rgba8", 4, 2) { }
|
2022-05-27 20:42:07 +02:00
|
|
|
|
|
|
|
void draw(u64 address, const u8 *data, size_t size, bool upperCase) override {
|
|
|
|
hex::unused(address, upperCase);
|
|
|
|
|
|
|
|
if (size == 4)
|
|
|
|
ImGui::ColorButton("##color", ImColor(data[0], data[1], data[2], data[3]), ImGuiColorEditFlags_AlphaPreview | ImGuiColorEditFlags_NoLabel | ImGuiColorEditFlags_NoDragDrop, ImVec2(ImGui::GetColumnWidth(), ImGui::GetTextLineHeight()));
|
|
|
|
else
|
|
|
|
ImGui::ColorButton("##color", ImColor(0, 0, 0, 0xFF), ImGuiColorEditFlags_AlphaPreview | ImGuiColorEditFlags_NoLabel | ImGuiColorEditFlags_NoDragDrop, ImVec2(ImGui::GetColumnWidth(), ImGui::GetTextLineHeight()));
|
|
|
|
}
|
|
|
|
|
|
|
|
bool drawEditing(u64 address, u8 *data, size_t size, bool upperCase, bool startedEditing) override {
|
|
|
|
hex::unused(address, data, size, upperCase);
|
|
|
|
|
2023-12-19 23:20:56 +01:00
|
|
|
m_currColor = { float(data[0]) / 0xFF, float(data[1]) / 0xFF, float(data[2]) / 0xFF, float(data[3]) / 0xFF };
|
2022-05-27 20:42:07 +02:00
|
|
|
if (startedEditing) {
|
|
|
|
ImGui::OpenPopup("##color_popup");
|
|
|
|
}
|
|
|
|
|
2023-12-19 13:10:25 +01:00
|
|
|
ImGui::ColorButton("##color", ImColor(m_currColor[0], m_currColor[1], m_currColor[2], m_currColor[3]), ImGuiColorEditFlags_AlphaPreview | ImGuiColorEditFlags_NoLabel | ImGuiColorEditFlags_NoDragDrop, ImVec2(ImGui::GetColumnWidth(), ImGui::GetTextLineHeight()));
|
2022-05-27 20:42:07 +02:00
|
|
|
|
|
|
|
if (ImGui::BeginPopup("##color_popup")) {
|
2023-12-19 23:20:56 +01:00
|
|
|
if (ImGui::ColorPicker4("##picker", m_currColor.data(), ImGuiColorEditFlags_AlphaBar | ImGuiColorEditFlags_InputRGB)) {
|
2022-05-27 20:42:07 +02:00
|
|
|
for (u8 i = 0; i < 4; i++)
|
2023-12-19 13:10:25 +01:00
|
|
|
data[i] = m_currColor[i] * 0xFF;
|
2022-05-27 20:42:07 +02:00
|
|
|
}
|
|
|
|
ImGui::EndPopup();
|
|
|
|
} else {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2022-08-04 09:46:17 +02:00
|
|
|
std::array<float, 4> m_currColor = { 0 };
|
2022-05-27 20:42:07 +02:00
|
|
|
|
|
|
|
};
|
|
|
|
|
2023-02-18 22:20:02 +01:00
|
|
|
class DataVisualizerBinary : public hex::ContentRegistry::HexEditor::DataVisualizer {
|
|
|
|
public:
|
2023-06-05 09:07:58 +02:00
|
|
|
DataVisualizerBinary() : DataVisualizer("hex.builtin.visualizer.binary", 1, 8) { }
|
2023-02-18 22:20:02 +01:00
|
|
|
|
|
|
|
void draw(u64 address, const u8 *data, size_t size, bool) override {
|
|
|
|
hex::unused(address);
|
|
|
|
|
|
|
|
if (size == 1)
|
2023-11-16 22:24:06 +01:00
|
|
|
ImGuiExt::TextFormatted("{:08b}", *data);
|
2023-02-18 22:20:02 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
bool drawEditing(u64 address, u8 *data, size_t, bool, bool startedEditing) override {
|
|
|
|
hex::unused(address, startedEditing);
|
|
|
|
|
|
|
|
if (startedEditing) {
|
2023-12-19 13:10:25 +01:00
|
|
|
m_inputBuffer = hex::format("{:08b}", *data);
|
2023-02-18 22:20:02 +01:00
|
|
|
}
|
|
|
|
|
2023-12-19 13:10:25 +01:00
|
|
|
if (drawDefaultTextEditingTextBox(address, m_inputBuffer, ImGuiInputTextFlags_None)) {
|
|
|
|
if (auto result = hex::parseBinaryString(wolv::util::trim(m_inputBuffer)); result.has_value()) {
|
2023-02-18 22:20:02 +01:00
|
|
|
*data = result.value();
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
std::string m_inputBuffer;
|
|
|
|
};
|
|
|
|
|
2022-05-27 20:42:07 +02:00
|
|
|
void registerDataVisualizers() {
|
|
|
|
ContentRegistry::HexEditor::addDataVisualizer<DataVisualizerHexadecimal<u8>>("hex.builtin.visualizer.hexadecimal.8bit");
|
|
|
|
ContentRegistry::HexEditor::addDataVisualizer<DataVisualizerHexadecimal<u16>>("hex.builtin.visualizer.hexadecimal.16bit");
|
|
|
|
ContentRegistry::HexEditor::addDataVisualizer<DataVisualizerHexadecimal<u32>>("hex.builtin.visualizer.hexadecimal.32bit");
|
|
|
|
ContentRegistry::HexEditor::addDataVisualizer<DataVisualizerHexadecimal<u64>>("hex.builtin.visualizer.hexadecimal.64bit");
|
|
|
|
|
|
|
|
ContentRegistry::HexEditor::addDataVisualizer<DataVisualizerDecimal<u8>>("hex.builtin.visualizer.decimal.unsigned.8bit");
|
|
|
|
ContentRegistry::HexEditor::addDataVisualizer<DataVisualizerDecimal<u16>>("hex.builtin.visualizer.decimal.unsigned.16bit");
|
|
|
|
ContentRegistry::HexEditor::addDataVisualizer<DataVisualizerDecimal<u32>>("hex.builtin.visualizer.decimal.unsigned.32bit");
|
|
|
|
ContentRegistry::HexEditor::addDataVisualizer<DataVisualizerDecimal<u64>>("hex.builtin.visualizer.decimal.unsigned.64bit");
|
|
|
|
|
|
|
|
ContentRegistry::HexEditor::addDataVisualizer<DataVisualizerDecimal<i8>>("hex.builtin.visualizer.decimal.signed.8bit");
|
|
|
|
ContentRegistry::HexEditor::addDataVisualizer<DataVisualizerDecimal<i16>>("hex.builtin.visualizer.decimal.signed.16bit");
|
|
|
|
ContentRegistry::HexEditor::addDataVisualizer<DataVisualizerDecimal<i32>>("hex.builtin.visualizer.decimal.signed.32bit");
|
|
|
|
ContentRegistry::HexEditor::addDataVisualizer<DataVisualizerDecimal<i64>>("hex.builtin.visualizer.decimal.signed.64bit");
|
|
|
|
|
2022-09-28 18:30:41 +02:00
|
|
|
ContentRegistry::HexEditor::addDataVisualizer<DataVisualizerFloatingPoint<Float16>>("hex.builtin.visualizer.floating_point.16bit");
|
2022-05-27 20:42:07 +02:00
|
|
|
ContentRegistry::HexEditor::addDataVisualizer<DataVisualizerFloatingPoint<float>>("hex.builtin.visualizer.floating_point.32bit");
|
|
|
|
ContentRegistry::HexEditor::addDataVisualizer<DataVisualizerFloatingPoint<double>>("hex.builtin.visualizer.floating_point.64bit");
|
|
|
|
|
2023-06-05 09:07:58 +02:00
|
|
|
ContentRegistry::HexEditor::addDataVisualizer<DataVisualizerRGBA8>();
|
|
|
|
ContentRegistry::HexEditor::addDataVisualizer<DataVisualizerHexii>();
|
2023-02-18 22:20:02 +01:00
|
|
|
|
2023-06-05 09:07:58 +02:00
|
|
|
ContentRegistry::HexEditor::addDataVisualizer<DataVisualizerBinary>();
|
2022-05-27 20:42:07 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|