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());
});
}

View File

@@ -6,8 +6,12 @@
#include <vector>
#include <string>
#include <hex/helpers/concepts.hpp>
namespace hex {
namespace prv { class Provider; }
struct ImHexApi {
ImHexApi() = delete;
@@ -36,6 +40,24 @@ namespace hex {
static std::list<Entry>& getEntries();
};
struct Provider {
static prv::Provider* get();
static const std::vector<prv::Provider*>& getProviders();
static bool isValid();
template<hex::derived_from<prv::Provider> T>
static void add(auto&& ... args) {
add(new T(std::forward<decltype(args)>(args)...));
}
static void remove(prv::Provider *provider);
private:
static void add(prv::Provider *provider);
};
};
}

View File

@@ -53,7 +53,10 @@ namespace hex {
public:
static std::vector<std::function<void()>> deferredCalls;
static prv::Provider *currentProvider;
static std::vector<prv::Provider*> providers;
static u32 currentProvider;
static std::map<std::string, std::vector<ContentRegistry::Settings::Entry>> settingsEntries;
static nlohmann::json settingsJson;
static std::vector<ContentRegistry::CommandPaletteCommands::Entry> commandPaletteCommands;

View File

@@ -19,11 +19,11 @@ namespace hex::prv {
Provider();
virtual ~Provider();
virtual bool isAvailable() = 0;
virtual bool isReadable() = 0;
virtual bool isWritable() = 0;
virtual bool isResizable() = 0;
virtual bool isSavable() = 0;
virtual bool isAvailable() const = 0;
virtual bool isReadable() const = 0;
virtual bool isWritable() const = 0;
virtual bool isResizable() const = 0;
virtual bool isSavable() const = 0;
virtual void read(u64 offset, void *buffer, size_t size, bool overlays = true);
virtual void readRelative(u64 offset, void *buffer, size_t size, bool overlays = true);
@@ -37,35 +37,37 @@ namespace hex::prv {
virtual void readRaw(u64 offset, void *buffer, size_t size) = 0;
virtual void writeRaw(u64 offset, const void *buffer, size_t size) = 0;
virtual size_t getActualSize() = 0;
virtual size_t getActualSize() const = 0;
void applyOverlays(u64 offset, void *buffer, size_t size);
std::map<u64, u8>& getPatches();
const std::map<u64, u8>& getPatches() const;
void applyPatches();
[[nodiscard]] Overlay* newOverlay();
void deleteOverlay(Overlay *overlay);
[[nodiscard]] const std::list<Overlay*>& getOverlays();
u32 getPageCount();
u32 getPageCount() const;
u32 getCurrentPage() const;
void setCurrentPage(u32 page);
virtual void setBaseAddress(u64 address);
virtual u64 getBaseAddress();
virtual size_t getSize();
virtual std::optional<u32> getPageOfAddress(u64 address);
virtual u64 getBaseAddress() const;
virtual size_t getSize() const;
virtual std::optional<u32> getPageOfAddress(u64 address) const;
virtual std::vector<std::pair<std::string, std::string>> getDataInformation() = 0;
[[nodiscard]] virtual std::string getName() const = 0;
[[nodiscard]] virtual std::vector<std::pair<std::string, std::string>> getDataInformation() const = 0;
void addPatch(u64 offset, const void *buffer, size_t size);
void undo();
void redo();
bool canUndo();
bool canRedo();
bool canUndo() const;
bool canRedo() const;
protected:
u32 m_currPage = 0;

View File

@@ -43,4 +43,38 @@ namespace hex {
return SharedData::bookmarkEntries;
}
prv::Provider* ImHexApi::Provider::get() {
if (!ImHexApi::Provider::isValid())
return nullptr;
return SharedData::providers[SharedData::currentProvider];
}
const std::vector<prv::Provider*>& ImHexApi::Provider::getProviders() {
return SharedData::providers;
}
bool ImHexApi::Provider::isValid() {
return !SharedData::providers.empty();
}
void ImHexApi::Provider::add(prv::Provider *provider) {
SharedData::providers.push_back(provider);
SharedData::currentProvider = SharedData::providers.size() - 1;
}
void ImHexApi::Provider::remove(prv::Provider *provider) {
auto &providers = SharedData::providers;
auto it = std::find(providers.begin(), providers.end(), provider);
providers.erase(it);
if (it - providers.begin() == SharedData::currentProvider)
SharedData::currentProvider = 0;
delete provider;
}
}

View File

@@ -5,7 +5,10 @@
namespace hex {
std::vector<std::function<void()>> SharedData::deferredCalls;
prv::Provider *SharedData::currentProvider;
std::vector<prv::Provider*> SharedData::providers;
u32 SharedData::currentProvider;
std::map<std::string, std::vector<ContentRegistry::Settings::Entry>> SharedData::settingsEntries;
nlohmann::json SharedData::settingsJson;
std::vector<ContentRegistry::CommandPaletteCommands::Entry> SharedData::commandPaletteCommands;

View File

@@ -49,7 +49,7 @@ namespace hex::pl {
this->m_preprocessor->addPragmaHandler("base_address", [](std::string value) {
auto baseAddress = strtoull(value.c_str(), nullptr, 0);
SharedData::currentProvider->setBaseAddress(baseAddress);
ImHexApi::Provider::get()->setBaseAddress(baseAddress);
return true;
});

View File

@@ -57,6 +57,10 @@ namespace hex::prv {
return *(this->m_patches.end() - 1 - this->m_patchTreeOffset);
}
const std::map<u64, u8>& Provider::getPatches() const {
return *(this->m_patches.end() - 1 - this->m_patchTreeOffset);
}
void Provider::applyPatches() {
for (auto &[patchAddress, patch] : getPatches())
this->writeRaw(patchAddress, &patch, 1);
@@ -77,7 +81,7 @@ namespace hex::prv {
}
u32 Provider::getPageCount() {
u32 Provider::getPageCount() const {
return std::ceil(this->getActualSize() / double(PageSize));
}
@@ -95,15 +99,15 @@ namespace hex::prv {
this->m_baseAddress = address;
}
u64 Provider::getBaseAddress() {
u64 Provider::getBaseAddress() const {
return this->m_baseAddress + PageSize * this->m_currPage;
}
size_t Provider::getSize() {
size_t Provider::getSize() const {
return std::min(this->getActualSize() - PageSize * this->m_currPage, PageSize);
}
std::optional<u32> Provider::getPageOfAddress(u64 address) {
std::optional<u32> Provider::getPageOfAddress(u64 address) const {
u32 page = std::floor((address - this->getBaseAddress()) / double(PageSize));
if (page >= this->getPageCount())
@@ -134,11 +138,11 @@ namespace hex::prv {
this->m_patchTreeOffset--;
}
bool Provider::canUndo() {
bool Provider::canUndo() const {
return this->m_patchTreeOffset < this->m_patches.size() - 1;
}
bool Provider::canRedo() {
bool Provider::canRedo() const {
return this->m_patchTreeOffset > 0;
}

View File

@@ -16,7 +16,7 @@ namespace hex {
bool View::handleShortcut(bool keys[512], bool ctrl, bool shift, bool alt) { return false; }
bool View::isAvailable() {
return SharedData::currentProvider != nullptr && SharedData::currentProvider->isAvailable();
return ImHexApi::Provider::isValid() && ImHexApi::Provider::get()->isAvailable();
}
std::vector<std::function<void()>>& View::getDeferedCalls() {