mirror of
https://github.com/WerWolv/ImHex.git
synced 2026-03-30 13:05:25 -05:00
feat: Added support for adding custom providers through C#
This commit is contained in:
@@ -1,4 +1,5 @@
|
||||
#include <script_api.hpp>
|
||||
#include <hex/api/content_registry.hpp>
|
||||
|
||||
#include <hex/api/imhex_api.hpp>
|
||||
#include <hex/providers/provider.hpp>
|
||||
@@ -41,4 +42,64 @@ SCRIPT_API(bool getSelection, u64 *start, u64 *end) {
|
||||
*end = selection->getEndAddress();
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
class ScriptDataProvider : public hex::prv::Provider {
|
||||
public:
|
||||
using ReadFunction = void(*)(u64, void*, u64);
|
||||
using WriteFunction = void(*)(u64, const void*, u64);
|
||||
using GetSizeFunction = u64(*)();
|
||||
using GetNameFunction = std::string(*)();
|
||||
|
||||
bool open() override { return true; }
|
||||
void close() override { }
|
||||
|
||||
[[nodiscard]] bool isAvailable() const override { return true; }
|
||||
[[nodiscard]] bool isReadable() const override { return true; }
|
||||
[[nodiscard]] bool isWritable() const override { return true; }
|
||||
[[nodiscard]] bool isResizable() const override { return true; }
|
||||
[[nodiscard]] bool isSavable() const override { return true; }
|
||||
[[nodiscard]] bool isDumpable() const override { return true; }
|
||||
|
||||
void readRaw(u64 offset, void *buffer, size_t size) override {
|
||||
m_readFunction(offset, buffer, size);
|
||||
}
|
||||
|
||||
void writeRaw(u64 offset, const void *buffer, size_t size) override {
|
||||
m_writeFunction(offset, const_cast<void*>(buffer), size);
|
||||
}
|
||||
|
||||
void setFunctions(ReadFunction readFunc, WriteFunction writeFunc, GetSizeFunction getSizeFunc) {
|
||||
m_readFunction = readFunc;
|
||||
m_writeFunction = writeFunc;
|
||||
m_getSizeFunction = getSizeFunc;
|
||||
}
|
||||
|
||||
[[nodiscard]] u64 getActualSize() const override { return m_getSizeFunction(); }
|
||||
|
||||
void setTypeName(std::string typeName) { m_typeName = std::move(typeName);}
|
||||
void setName(std::string name) { m_name = std::move(name);}
|
||||
[[nodiscard]] std::string getTypeName() const override { return m_typeName; }
|
||||
[[nodiscard]] std::string getName() const override { return m_name; }
|
||||
|
||||
private:
|
||||
ReadFunction m_readFunction = nullptr;
|
||||
WriteFunction m_writeFunction = nullptr;
|
||||
GetSizeFunction m_getSizeFunction = nullptr;
|
||||
GetNameFunction m_getNameFunction = nullptr;
|
||||
|
||||
std::string m_typeName, m_name;
|
||||
};
|
||||
|
||||
SCRIPT_API(void registerProvider, const char *typeName, const char *name, ScriptDataProvider::ReadFunction readFunc, ScriptDataProvider::WriteFunction writeFunc, ScriptDataProvider::GetSizeFunction getSizeFunc) {
|
||||
auto typeNameString = std::string(typeName);
|
||||
auto nameString = std::string(name);
|
||||
hex::ContentRegistry::Provider::impl::add(typeNameString, [typeNameString, nameString, readFunc, writeFunc, getSizeFunc] -> std::unique_ptr<hex::prv::Provider> {
|
||||
auto provider = std::make_unique<ScriptDataProvider>();
|
||||
provider->setTypeName(typeNameString);
|
||||
provider->setName(nameString);
|
||||
provider->setFunctions(readFunc, writeFunc, getSizeFunc);
|
||||
return provider;
|
||||
});
|
||||
hex::ContentRegistry::Provider::impl::addProviderName(typeNameString);
|
||||
}
|
||||
Reference in New Issue
Block a user