1
0
mirror of synced 2025-01-19 01:24:15 +01:00

impr: Save custom encoding file to project

Fixes #1005
This commit is contained in:
WerWolv 2023-03-31 11:06:51 +02:00
parent 06a7b6e446
commit 1460044e91
4 changed files with 47 additions and 8 deletions

@ -1 +1 @@
Subproject commit d7faee43b43a90e8a3c1d3e4de79d5b4de4e6e42
Subproject commit af893b9c1348eda19ef51680a6910c4cff53fb8a

View File

@ -22,6 +22,7 @@ namespace hex {
EncodingFile() = default;
EncodingFile(Type type, const std::fs::path &path);
EncodingFile(Type type, const std::string &path);
[[nodiscard]] std::pair<std::string_view, size_t> getEncodingFor(std::span<u8> buffer) const;
[[nodiscard]] size_t getEncodingLengthFor(std::span<u8> buffer) const;
@ -29,11 +30,14 @@ namespace hex {
[[nodiscard]] bool valid() const { return this->m_valid; }
[[nodiscard]] const std::string& getTableContent() const { return this->m_tableContent; }
private:
void parseThingyFile(wolv::io::File &file);
void parse(const std::string &content);
bool m_valid = false;
std::string m_tableContent;
std::map<size_t, std::map<std::vector<u8>, std::string>> m_mapping;
size_t m_longestSequence = 0;
};

View File

@ -11,7 +11,19 @@ namespace hex {
auto file = wolv::io::File(path, wolv::io::File::Mode::Read);
switch (type) {
case Type::Thingy:
parseThingyFile(file);
parse(file.readString());
break;
default:
return;
}
this->m_valid = true;
}
EncodingFile::EncodingFile(Type type, const std::string &content) {
switch (type) {
case Type::Thingy:
parse(content);
break;
default:
return;
@ -48,8 +60,9 @@ namespace hex {
return 1;
}
void EncodingFile::parseThingyFile(wolv::io::File &file) {
for (const auto &line : splitString(file.readString(), "\n")) {
void EncodingFile::parse(const std::string &content) {
this->m_tableContent = content;
for (const auto &line : splitString(this->m_tableContent, "\n")) {
std::string from, to;
{

View File

@ -5,6 +5,7 @@
#include <hex/helpers/utils.hpp>
#include <hex/providers/buffered_reader.hpp>
#include <hex/helpers/crypto.hpp>
#include <hex/api/project_file_manager.hpp>
#include <content/providers/view_provider.hpp>
#include <content/helpers/math_evaluator.hpp>
@ -952,6 +953,24 @@ namespace hex::plugin::builtin {
EventManager::post<EventRegionSelected>(ImHexApi::HexEditor::ProviderRegion{ this->getSelection(), newProvider });
}
});
ProjectFile::registerPerProviderHandler({
.basePath = "custom_encoding.json",
.required = false,
.load = [this](prv::Provider *, const std::fs::path &basePath, Tar &tar) {
this->m_hexEditor.setCustomEncoding(EncodingFile(hex::EncodingFile::Type::Thingy, tar.readString(basePath)));
return true;
},
.store = [this](prv::Provider *, const std::fs::path &basePath, Tar &tar) {
if (const auto &encoding = this->m_hexEditor.getCustomEncoding(); encoding.has_value()) {
tar.writeString(basePath, encoding->getTableContent());
return true;
}
return false;
}
});
}
void ViewHexEditor::registerMenuItems() {
@ -995,9 +1014,12 @@ namespace hex::plugin::builtin {
}
View::showFileChooserPopup(paths, { {"Thingy Table File", "tbl"} }, false,
[this](const auto &path) {
this->m_hexEditor.setCustomEncoding(EncodingFile(EncodingFile::Type::Thingy, path));
});
[this](const auto &path) {
TaskManager::createTask("Loading encoding file", 0, [this, path](auto&) {
this->m_hexEditor.setCustomEncoding(EncodingFile(EncodingFile::Type::Thingy, path));
ImHexApi::Provider::markDirty();
});
});
},
ImHexApi::Provider::isValid);