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

@@ -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() {