1
0
mirror of synced 2024-12-11 23:46:00 +01:00
ImHex/plugins/builtin/include/content/providers/view_provider.hpp

162 lines
5.0 KiB
C++
Raw Normal View History

2022-11-14 10:02:46 +01:00
#pragma once
#include <hex/providers/provider.hpp>
#include <hex/helpers/fmt.hpp>
#include <hex/api/event_manager.hpp>
2022-11-14 10:02:46 +01:00
namespace hex::plugin::builtin {
class ViewProvider : public hex::prv::Provider {
public:
ViewProvider() = default;
~ViewProvider() override = default;
2022-11-14 10:02:46 +01:00
[[nodiscard]] bool isAvailable() const override {
2023-12-19 13:10:25 +01:00
if (m_provider == nullptr)
return false;
else
2023-12-19 13:10:25 +01:00
return m_provider->isAvailable();
}
[[nodiscard]] bool isReadable() const override {
2023-12-19 13:10:25 +01:00
if (m_provider == nullptr)
return false;
else
2023-12-19 13:10:25 +01:00
return m_provider->isReadable();
}
[[nodiscard]] bool isWritable() const override {
2023-12-19 13:10:25 +01:00
if (m_provider == nullptr)
return false;
else
2023-12-19 13:10:25 +01:00
return m_provider->isWritable();
}
2022-11-14 10:02:46 +01:00
[[nodiscard]] bool isResizable() const override { return true; }
[[nodiscard]] bool isSavable() const override {
2023-12-19 13:10:25 +01:00
if (m_provider == nullptr)
return false;
else
2023-12-19 13:10:25 +01:00
return m_provider->isSavable();
}
2022-11-14 10:02:46 +01:00
[[nodiscard]] bool isSavableAsRecent() const override { return false; }
2022-11-14 10:02:46 +01:00
void save() override {
2023-12-19 13:10:25 +01:00
m_provider->save();
2022-11-14 10:02:46 +01:00
}
[[nodiscard]] bool open() override {
if (m_provider == this)
return false;
EventProviderClosing::subscribe(this, [this](const prv::Provider *provider, bool*) {
if (m_provider == provider)
ImHexApi::Provider::remove(this, false);
});
return true;
}
void close() override {
EventProviderClosing::unsubscribe(this);
}
2022-11-14 10:02:46 +01:00
2023-12-08 16:22:36 +01:00
void resizeRaw(u64 newSize) override {
2023-12-19 13:10:25 +01:00
m_size = newSize;
2022-11-14 10:02:46 +01:00
}
2023-12-08 16:22:36 +01:00
void insertRaw(u64 offset, u64 size) override {
2023-12-19 13:10:25 +01:00
if (m_provider == nullptr)
return;
2023-12-19 13:10:25 +01:00
m_size += size;
m_provider->insert(offset + m_startAddress, size);
2022-11-14 10:02:46 +01:00
}
2023-12-08 16:22:36 +01:00
void removeRaw(u64 offset, u64 size) override {
2023-12-19 13:10:25 +01:00
if (m_provider == nullptr)
return;
2023-12-19 13:10:25 +01:00
m_size -= size;
m_provider->remove(offset + m_startAddress, size);
2022-11-14 10:02:46 +01:00
}
void readRaw(u64 offset, void *buffer, size_t size) override {
2023-12-19 13:10:25 +01:00
if (m_provider == nullptr)
return;
2023-12-19 13:10:25 +01:00
m_provider->read(offset + m_startAddress, buffer, size);
}
void writeRaw(u64 offset, const void *buffer, size_t size) override {
2023-12-19 13:10:25 +01:00
if (m_provider == nullptr)
return;
2023-12-19 13:10:25 +01:00
m_provider->write(offset + m_startAddress, buffer, size);
}
2023-12-19 13:10:25 +01:00
[[nodiscard]] u64 getActualSize() const override { return m_size; }
2022-11-14 10:02:46 +01:00
[[nodiscard]] std::string getName() const override {
2023-12-19 13:10:25 +01:00
if (m_provider == nullptr)
return "View";
else
2023-12-19 13:10:25 +01:00
return hex::format("{} View", m_provider->getName());
}
[[nodiscard]] std::vector<Description> getDataDescription() const override {
2023-12-19 13:10:25 +01:00
if (m_provider == nullptr)
return { };
2023-12-19 13:10:25 +01:00
return m_provider->getDataDescription();
}
2022-11-14 10:02:46 +01:00
void loadSettings(const nlohmann::json &settings) override {
Provider::loadSettings(settings);
auto id = settings.at("id").get<u64>();
m_startAddress = settings.at("start_address").get<u64>();
m_size = settings.at("size").get<size_t>();
const auto &providers = ImHexApi::Provider::getProviders();
auto provider = std::ranges::find_if(providers, [id](const prv::Provider *provider) {
return provider->getID() == id;
});
if (provider == providers.end())
return;
m_provider = *provider;
}
[[nodiscard]] nlohmann::json storeSettings(nlohmann::json settings) const override {
settings["id"] = m_provider->getID();
settings["start_address"] = m_startAddress;
settings["size"] = m_size;
return Provider::storeSettings(settings);
}
2022-11-14 10:02:46 +01:00
[[nodiscard]] std::string getTypeName() const override {
return "hex.builtin.provider.view";
}
void setProvider(u64 startAddress, size_t size, hex::prv::Provider *provider) {
2023-12-19 13:10:25 +01:00
m_startAddress = startAddress;
m_size = size;
m_provider = provider;
2022-11-14 10:02:46 +01:00
}
[[nodiscard]] std::pair<Region, bool> getRegionValidity(u64 address) const override {
address -= this->getBaseAddress();
if (address < this->getActualSize())
return { Region { this->getBaseAddress() + address, this->getActualSize() - address }, true };
else
return { Region::Invalid(), false };
}
private:
u64 m_startAddress = 0x00;
size_t m_size = 0x00;
prv::Provider *m_provider = nullptr;
};
}