1
0
mirror of synced 2024-12-02 03:07:18 +01:00
ImHex/plugins/builtin/source/content/views/view_constants.cpp

150 lines
6.6 KiB
C++
Raw Normal View History

2021-12-07 22:47:41 +01:00
#include "content/views/view_constants.hpp"
2021-06-26 01:18:33 +02:00
#include <hex/helpers/fs.hpp>
2021-08-29 22:15:18 +02:00
#include <hex/helpers/logger.hpp>
#include <hex/helpers/utils.hpp>
2021-08-29 22:15:18 +02:00
2023-03-12 18:43:05 +01:00
#include <wolv/utils/string.hpp>
#include <wolv/io/file.hpp>
2023-03-12 18:43:05 +01:00
2021-06-26 01:18:33 +02:00
#include <filesystem>
#include <nlohmann/json.hpp>
2021-12-07 22:47:41 +01:00
namespace hex::plugin::builtin {
2021-06-26 01:18:33 +02:00
2024-01-08 21:51:48 +01:00
ViewConstants::ViewConstants() : View::Window("hex.builtin.view.constants.name", ICON_VS_SYMBOL_CONSTANT) {
2021-06-26 01:18:33 +02:00
this->reloadConstants();
}
void ViewConstants::reloadConstants() {
2023-12-19 13:10:25 +01:00
m_constants.clear();
m_filterIndices.clear();
2021-06-26 01:18:33 +02:00
for (const auto &path : fs::getDefaultPaths(fs::ImHexPath::Constants)) {
if (!wolv::io::fs::exists(path)) continue;
2021-06-26 01:18:33 +02:00
std::error_code error;
for (auto &file : std::fs::directory_iterator(path, error)) {
2021-06-26 01:18:33 +02:00
if (!file.is_regular_file()) continue;
if (file.path().extension() != ".json") continue;
if (file.path().filename().u8string().starts_with('_')) continue;
2021-06-26 01:18:33 +02:00
try {
auto fileData = wolv::io::File(file.path(), wolv::io::File::Mode::Read).readString();
auto content = nlohmann::json::parse(fileData);
2021-06-26 01:18:33 +02:00
for (auto value : content.at("values")) {
2021-06-26 01:18:33 +02:00
Constant constant;
constant.category = content.at("name").get<std::string>();
constant.name = value.at("name").get<std::string>();
2021-06-26 01:18:33 +02:00
if (value.contains("desc"))
constant.description = value.at("desc").get<std::string>();
constant.value = value.at("value").get<std::string>();
2021-06-26 01:18:33 +02:00
auto type = value.at("type");
2021-06-26 01:18:33 +02:00
if (type == "int10")
constant.type = ConstantType::Int10;
else if (type == "int16be")
constant.type = ConstantType::Int16BigEndian;
else if (type == "int16le")
constant.type = ConstantType::Int16LittleEndian;
else
throw std::runtime_error("Invalid type");
2023-12-19 13:10:25 +01:00
m_filterIndices.push_back(m_constants.size());
m_constants.push_back(constant);
2021-06-26 01:18:33 +02:00
}
} catch (...) {
2023-03-12 18:43:05 +01:00
log::error("Failed to parse constants file {}", wolv::util::toUTF8String(file.path()));
2021-06-26 01:18:33 +02:00
}
}
}
}
void ViewConstants::drawContent() {
ImGui::PushItemWidth(-1);
2023-12-19 13:10:25 +01:00
if (ImGuiExt::InputTextIcon("##search", ICON_VS_FILTER, m_filter)) {
m_filterIndices.clear();
// Filter the constants according to the entered value
2023-12-19 13:10:25 +01:00
for (u64 i = 0; i < m_constants.size(); i++) {
auto &constant = m_constants[i];
if (hex::containsIgnoreCase(constant.name, m_filter) ||
hex::containsIgnoreCase(constant.category, m_filter) ||
hex::containsIgnoreCase(constant.description, m_filter) ||
hex::containsIgnoreCase(constant.value, m_filter))
m_filterIndices.push_back(i);
}
}
ImGui::PopItemWidth();
if (ImGui::BeginTable("##strings", 4, ImGuiTableFlags_Borders | ImGuiTableFlags_Resizable | ImGuiTableFlags_Sortable | ImGuiTableFlags_Reorderable | ImGuiTableFlags_RowBg | ImGuiTableFlags_ScrollY)) {
ImGui::TableSetupScrollFreeze(0, 1);
ImGui::TableSetupColumn("hex.builtin.view.constants.row.category"_lang, 0, -1, ImGui::GetID("category"));
ImGui::TableSetupColumn("hex.builtin.view.constants.row.name"_lang, 0, -1, ImGui::GetID("name"));
ImGui::TableSetupColumn("hex.builtin.view.constants.row.desc"_lang, 0, -1, ImGui::GetID("desc"));
ImGui::TableSetupColumn("hex.builtin.view.constants.row.value"_lang, 0, -1, ImGui::GetID("value"));
auto sortSpecs = ImGui::TableGetSortSpecs();
2021-06-26 01:18:33 +02:00
// Handle table sorting
if (sortSpecs->SpecsDirty) {
2023-12-19 13:10:25 +01:00
std::sort(m_constants.begin(), m_constants.end(), [&sortSpecs](const Constant &left, const Constant &right) -> bool {
if (sortSpecs->Specs->ColumnUserID == ImGui::GetID("category")) {
if (sortSpecs->Specs->SortDirection == ImGuiSortDirection_Ascending)
return left.category > right.category;
else
return left.category < right.category;
} else if (sortSpecs->Specs->ColumnUserID == ImGui::GetID("name")) {
if (sortSpecs->Specs->SortDirection == ImGuiSortDirection_Ascending)
return left.name > right.name;
else
return left.name < right.name;
} else if (sortSpecs->Specs->ColumnUserID == ImGui::GetID("desc")) {
if (sortSpecs->Specs->SortDirection == ImGuiSortDirection_Ascending)
return left.description > right.description;
else
return left.description < right.description;
} else if (sortSpecs->Specs->ColumnUserID == ImGui::GetID("value")) {
if (sortSpecs->Specs->SortDirection == ImGuiSortDirection_Ascending)
return left.value > right.value;
else
return left.value < right.value;
2021-06-26 01:18:33 +02:00
}
return false;
});
sortSpecs->SpecsDirty = false;
2021-06-26 01:18:33 +02:00
}
ImGui::TableHeadersRow();
ImGuiListClipper clipper;
2023-12-19 13:10:25 +01:00
clipper.Begin(m_filterIndices.size());
// Draw the constants table
while (clipper.Step()) {
for (int i = clipper.DisplayStart; i < clipper.DisplayEnd; i++) {
2023-12-19 13:10:25 +01:00
auto &constant = m_constants[m_filterIndices[i]];
ImGui::TableNextRow();
ImGui::TableNextColumn();
ImGui::TextUnformatted(constant.category.c_str());
ImGui::TableNextColumn();
ImGui::TextUnformatted(constant.name.c_str());
ImGui::TableNextColumn();
ImGui::TextUnformatted(constant.description.c_str());
ImGui::TableNextColumn();
ImGui::TextUnformatted(constant.value.c_str());
}
}
clipper.End();
ImGui::EndTable();
2021-06-26 01:18:33 +02:00
}
}
}