sys: Allow multiple files to be loaded simultaneously

This commit is contained in:
WerWolv
2021-09-21 02:29:54 +02:00
parent a302448b76
commit 8631cb0c2a
30 changed files with 256 additions and 161 deletions

View File

@@ -138,7 +138,7 @@ namespace hex::plugin::builtin {
constexpr static auto MaxStringLength = 32;
std::string stringBuffer(std::min<ssize_t>(MaxStringLength, currSelection.size), 0x00);
SharedData::currentProvider->read(currSelection.address, stringBuffer.data(), stringBuffer.size());
ImHexApi::Provider::get()->read(currSelection.address, stringBuffer.data(), stringBuffer.size());
if (currSelection.size > MaxStringLength)
stringBuffer += "...";

View File

@@ -380,7 +380,7 @@ namespace hex::plugin::builtin {
std::vector<u8> data;
data.resize(size);
SharedData::currentProvider->readRaw(address, data.data(), size);
ImHexApi::Provider::get()->readRaw(address, data.data(), size);
this->setBufferOnOutput(2, data);
}
@@ -407,7 +407,7 @@ namespace hex::plugin::builtin {
}) { }
void process() override {
auto size = SharedData::currentProvider->getActualSize();
auto size = ImHexApi::Provider::get()->getActualSize();
this->setIntegerOnOutput(0, size);
}

View File

@@ -89,12 +89,12 @@ namespace hex::plugin::builtin {
/* base_address() */
ContentRegistry::PatternLanguageFunctions::add(nsStdMem, "base_address", ContentRegistry::PatternLanguageFunctions::NoParameters, [](auto &ctx, auto params) -> ASTNode* {
return new ASTNodeIntegerLiteral(u64(SharedData::currentProvider->getBaseAddress()));
return new ASTNodeIntegerLiteral(u64(ImHexApi::Provider::get()->getBaseAddress()));
});
/* size() */
ContentRegistry::PatternLanguageFunctions::add(nsStdMem, "size", ContentRegistry::PatternLanguageFunctions::NoParameters, [](auto &ctx, auto params) -> ASTNode* {
return new ASTNodeIntegerLiteral(u64(SharedData::currentProvider->getActualSize()));
return new ASTNodeIntegerLiteral(u64(ImHexApi::Provider::get()->getActualSize()));
});
/* find_sequence(occurrence_index, bytes...) */
@@ -112,8 +112,8 @@ namespace hex::plugin::builtin {
std::vector<u8> bytes(sequence.size(), 0x00);
u32 occurrences = 0;
for (u64 offset = 0; offset < SharedData::currentProvider->getSize() - sequence.size(); offset++) {
SharedData::currentProvider->read(offset, bytes.data(), bytes.size());
for (u64 offset = 0; offset < ImHexApi::Provider::get()->getSize() - sequence.size(); offset++) {
ImHexApi::Provider::get()->read(offset, bytes.data(), bytes.size());
if (bytes == sequence) {
if (LITERAL_COMPARE(occurrenceIndex, occurrences < occurrenceIndex)) {
@@ -133,7 +133,7 @@ namespace hex::plugin::builtin {
auto address = AS_TYPE(ASTNodeIntegerLiteral, params[0])->getValue();
auto size = AS_TYPE(ASTNodeIntegerLiteral, params[1])->getValue();
if (LITERAL_COMPARE(address, address >= SharedData::currentProvider->getActualSize()))
if (LITERAL_COMPARE(address, address >= ImHexApi::Provider::get()->getActualSize()))
ctx.getConsole().abortEvaluation("address out of range");
return std::visit([&](auto &&address, auto &&size) {
@@ -141,7 +141,7 @@ namespace hex::plugin::builtin {
ctx.getConsole().abortEvaluation("invalid read size");
u8 value[(u8)size];
SharedData::currentProvider->read(address, value, size);
ImHexApi::Provider::get()->read(address, value, size);
switch ((u8)size) {
case 1: return new ASTNodeIntegerLiteral(*reinterpret_cast<u8*>(value));
@@ -159,7 +159,7 @@ namespace hex::plugin::builtin {
auto address = AS_TYPE(ASTNodeIntegerLiteral, params[0])->getValue();
auto size = AS_TYPE(ASTNodeIntegerLiteral, params[1])->getValue();
if (LITERAL_COMPARE(address, address >= SharedData::currentProvider->getActualSize()))
if (LITERAL_COMPARE(address, address >= ImHexApi::Provider::get()->getActualSize()))
ctx.getConsole().abortEvaluation("address out of range");
return std::visit([&](auto &&address, auto &&size) {
@@ -167,7 +167,7 @@ namespace hex::plugin::builtin {
ctx.getConsole().abortEvaluation("invalid read size");
u8 value[(u8)size];
SharedData::currentProvider->read(address, value, size);
ImHexApi::Provider::get()->read(address, value, size);
switch ((u8)size) {
case 1: return new ASTNodeIntegerLiteral(*reinterpret_cast<s8*>(value));

View File

@@ -1,4 +1,5 @@
#include <hex/api/content_registry.hpp>
#include <hex/api/imhex_api.hpp>
#include <hex/helpers/net.hpp>
#include <hex/helpers/shared_data.hpp>
@@ -156,8 +157,8 @@ namespace hex::plugin::builtin {
evaluator.setFunction("read", [](auto args) -> std::optional<long double> {
u8 value = 0;
auto provider = SharedData::currentProvider;
if (provider == nullptr || !provider->isReadable() || args[0] >= provider->getActualSize())
auto provider = ImHexApi::Provider::get();
if (!ImHexApi::Provider::isValid() || !provider->isReadable() || args[0] >= provider->getActualSize())
return { };
provider->read(args[0], &value, sizeof(u8));
@@ -166,8 +167,8 @@ namespace hex::plugin::builtin {
}, 1, 1);
evaluator.setFunction("write", [](auto args) -> std::optional<long double> {
auto provider = SharedData::currentProvider;
if (provider == nullptr || !provider->isWritable() || args[0] >= provider->getActualSize())
auto provider = ImHexApi::Provider::get();
if (!ImHexApi::Provider::isValid() || !provider->isWritable() || args[0] >= provider->getActualSize())
return { };
if (args[1] > 0xFF)

View File

@@ -28,19 +28,19 @@ namespace hex::plugin::builtin {
ContentRegistry::Interface::addToolbarItem([] {
const static auto buttonSize = ImVec2(ImGui::GetCurrentWindow()->MenuBarHeight(), ImGui::GetCurrentWindow()->MenuBarHeight());
auto provider = SharedData::currentProvider;
auto provider = ImHexApi::Provider::get();
// Undo
ImGui::Disabled([&provider] {
if (ImGui::ToolBarButton(ICON_VS_DISCARD, ImGui::GetCustomColorVec4(ImGuiCustomCol_ToolbarBlue), buttonSize))
provider->undo();
}, provider == nullptr || !provider->canUndo());
}, !ImHexApi::Provider::isValid() || !provider->canUndo());
// Redo
ImGui::Disabled([&provider] {
if (ImGui::ToolBarButton(ICON_VS_REDO, ImGui::GetCustomColorVec4(ImGuiCustomCol_ToolbarBlue), buttonSize))
provider->redo();
}, provider == nullptr || !provider->canRedo());
}, !ImHexApi::Provider::isValid() || !provider->canRedo());
ImGui::SeparatorEx(ImGuiSeparatorFlags_Vertical);
@@ -60,7 +60,7 @@ namespace hex::plugin::builtin {
ImGui::Disabled([&provider] {
if (ImGui::ToolBarButton(ICON_VS_SAVE, ImGui::GetCustomColorVec4(ImGuiCustomCol_ToolbarBlue), buttonSize))
provider->save();
}, provider == nullptr || !provider->isWritable() || !provider->isSavable());
}, !ImHexApi::Provider::isValid() || !provider->isWritable() || !provider->isSavable());
// Save file as
ImGui::Disabled([&provider] {
@@ -68,7 +68,7 @@ namespace hex::plugin::builtin {
hex::openFileBrowser("hex.view.hexeditor.save_as"_lang, DialogMode::Save, { }, [&provider](auto path) {
provider->saveAs(path);
});
}, provider == nullptr || !provider->isSavable());
}, !ImHexApi::Provider::isValid() || !provider->isSavable());
ImGui::SeparatorEx(ImGuiSeparatorFlags_Vertical);
@@ -82,8 +82,33 @@ namespace hex::plugin::builtin {
ImHexApi::Bookmarks::add(region.address, region.size, { }, { });
}
}, provider == nullptr || !provider->isReadable());
}, !ImHexApi::Provider::isValid() || !provider->isReadable());
ImGui::SeparatorEx(ImGuiSeparatorFlags_Vertical);
ImGui::Spacing();
// Provider switcher
ImGui::Disabled([] {
auto &providers = ImHexApi::Provider::getProviders();
std::string preview;
if (ImHexApi::Provider::isValid())
preview = providers[SharedData::currentProvider]->getName();
ImGui::SetNextItemWidth(200 * SharedData::globalScale);
if (ImGui::BeginCombo("", preview.c_str())) {
for (int i = 0; i < providers.size(); i++) {
if (ImGui::Selectable(providers[i]->getName().c_str())) {
SharedData::currentProvider = i;
}
}
ImGui::EndCombo();
}
}, !ImHexApi::Provider::isValid());
});
}