impr: Replace horrible pattern extra data class with a more modular system

This commit is contained in:
WerWolv
2023-04-17 16:18:48 +02:00
parent 535aeb5e39
commit 99a736df27
26 changed files with 705 additions and 720 deletions

View File

@@ -1,5 +1,4 @@
#include "content/views/view_hashes.hpp"
#include "content/helpers/provider_extra_data.hpp"
#include <hex/api/project_file_manager.hpp>
#include <hex/helpers/crypto.hpp>
@@ -9,18 +8,18 @@
namespace hex::plugin::builtin {
ViewHashes::ViewHashes() : View("hex.builtin.view.hashes.name") {
EventManager::subscribe<EventRegionSelected>(this, [](const auto &providerRegion) {
for (auto &function : ProviderExtraData::get(providerRegion.getProvider()).hashes.hashFunctions)
EventManager::subscribe<EventRegionSelected>(this, [this](const auto &providerRegion) {
for (auto &function : this->m_hashFunctions.get(providerRegion.getProvider()))
function.reset();
});
ImHexApi::HexEditor::addTooltipProvider([](u64 address, const u8 *data, size_t size) {
ImHexApi::HexEditor::addTooltipProvider([this](u64 address, const u8 *data, size_t size) {
hex::unused(data);
auto selection = ImHexApi::HexEditor::getSelection();
if (ImGui::GetIO().KeyShift) {
auto &hashFunctions = ProviderExtraData::get(selection->getProvider()).hashes.hashFunctions;
auto &hashFunctions = this->m_hashFunctions.get(selection->getProvider());
if (!hashFunctions.empty() && selection.has_value() && selection->overlaps(Region { address, size })) {
ImGui::BeginTooltip();
@@ -62,20 +61,20 @@ namespace hex::plugin::builtin {
ProjectFile::registerPerProviderHandler({
.basePath = "hashes.json",
.required = false,
.load = [](prv::Provider *provider, const std::fs::path &basePath, Tar &tar) -> bool {
.load = [this](prv::Provider *provider, const std::fs::path &basePath, Tar &tar) -> bool {
auto fileContent = tar.readString(basePath);
if (fileContent.empty())
return true;
auto data = nlohmann::json::parse(fileContent.begin(), fileContent.end());
ProviderExtraData::get(provider).hashes.hashFunctions.clear();
this->m_hashFunctions->clear();
return ViewHashes::importHashes(provider, data);
return this->importHashes(provider, data);
},
.store = [](prv::Provider *provider, const std::fs::path &basePath, Tar &tar) -> bool {
.store = [this](prv::Provider *provider, const std::fs::path &basePath, Tar &tar) -> bool {
nlohmann::json data;
bool result = ViewHashes::exportHashes(provider, data);
bool result = this->exportHashes(provider, data);
tar.writeString(basePath, data.dump(4));
return result;
@@ -96,8 +95,6 @@ namespace hex::plugin::builtin {
}
if (ImGui::Begin(View::toWindowName("hex.builtin.view.hashes.name").c_str(), &this->getWindowOpenState(), ImGuiWindowFlags_NoCollapse)) {
auto &hashFunctions = ProviderExtraData::get(ImHexApi::Provider::get()).hashes.hashFunctions;
if (ImGui::BeginCombo("hex.builtin.view.hashes.function"_lang, this->m_selectedHash != nullptr ? LangEntry(this->m_selectedHash->getUnlocalizedName()) : "")) {
for (const auto hash : hashes) {
@@ -134,7 +131,7 @@ namespace hex::plugin::builtin {
ImGui::BeginDisabled(this->m_newHashName.empty() || this->m_selectedHash == nullptr);
if (ImGui::IconButton(ICON_VS_ADD, ImGui::GetStyleColorVec4(ImGuiCol_Text))) {
if (this->m_selectedHash != nullptr)
hashFunctions.push_back(this->m_selectedHash->create(this->m_newHashName));
this->m_hashFunctions->push_back(this->m_selectedHash->create(this->m_newHashName));
}
ImGui::EndDisabled();
@@ -149,8 +146,8 @@ namespace hex::plugin::builtin {
auto selection = ImHexApi::HexEditor::getSelection();
std::optional<u32> indexToRemove;
for (u32 i = 0; i < hashFunctions.size(); i++) {
auto &function = hashFunctions[i];
for (u32 i = 0; i < this->m_hashFunctions->size(); i++) {
auto &function = (*this->m_hashFunctions)[i];
ImGui::PushID(i);
@@ -195,7 +192,7 @@ namespace hex::plugin::builtin {
}
if (indexToRemove.has_value()) {
hashFunctions.erase(hashFunctions.begin() + indexToRemove.value());
this->m_hashFunctions->erase(this->m_hashFunctions->begin() + indexToRemove.value());
}
ImGui::EndTable();
@@ -213,7 +210,6 @@ namespace hex::plugin::builtin {
const auto &hashes = ContentRegistry::Hashes::impl::getHashes();
auto &hashFunctions = ProviderExtraData::get(provider).hashes.hashFunctions;
for (const auto &hash : json["hashes"]) {
if (!hash.contains("name") || !hash.contains("type"))
continue;
@@ -224,7 +220,7 @@ namespace hex::plugin::builtin {
auto newFunction = newHash->create(hash["name"]);
newFunction.getType()->load(hash["settings"]);
hashFunctions.push_back(std::move(newFunction));
this->m_hashFunctions.get(provider).push_back(std::move(newFunction));
break;
}
}
@@ -236,7 +232,7 @@ namespace hex::plugin::builtin {
bool ViewHashes::exportHashes(prv::Provider *provider, nlohmann::json &json) {
json["hashes"] = nlohmann::json::array();
size_t index = 0;
for (const auto &hashFunction : ProviderExtraData::get(provider).hashes.hashFunctions) {
for (const auto &hashFunction : this->m_hashFunctions.get(provider)) {
json["hashes"][index] = {
{ "name", hashFunction.getName() },
{ "type", hashFunction.getType()->getUnlocalizedName() },