crumblingstatue / hexerator

Versatile GUI hex editor focused on binary file exploration and aiding pattern recognition
https://crumblingstatue.github.io/hexerator-book/
Apache License 2.0
328 stars 6 forks source link

Feature Request: Add ability for plugins to be a source provider #51

Open y-haidar opened 3 weeks ago

y-haidar commented 3 weeks ago

Use case

Create a plugin for outputting mgba's memory.

Thoughts on possible implementation

pub trait Plugin {
    fn name(&self) -> &str;
...
    // if `Some` then a sub menu `Open with...` with a sub item `PLUGIN_HUMAN_NAME` is added to `File` menu.
    fn has_source_provider(&self) -> Option<PluginSourceProviderParams>;
}
pub struct PluginSourceProviderParams {
    can_write: bool,
    auto_reload_type: AutoReloadType,
}

pub enum AutoReloadType {
    Both,
    // It is not always possible to read everything, as for example, the plugin could be using a slow
    // network protocol like TCP. Another example, a remote gdb session.
    RegionOnly,
    // This can be the type of a plugin that acts as a network proxy and dumps its buffer
    // that was filled from an internal continuous stream 
    OneShot, // or All
}

The trait:

pub trait PluginSourceProvider {
    fn read(&mut self, buf: &mut [u8]);
    // to be honest, I didn't look at the source code for writing.
    // if the plugin doesn't support write, it should panic, as `can_write` should prevent
    // the call of this function
    fn write(&mut self, buf: &mut [u8]);
}

A possible solution for the lack of parameters, would be to add PluginSettings, where it would create a menu for configuring plugins. Provided plugins need to provide a schema containing fields with their name, description, type(string or Int only to keep it simple) and a default value.

y-haidar commented 3 weeks ago

I have started playing around with this idea, it turned ugly, and that made me think maybe stdin is more than enough. Closing

crumblingstatue commented 3 weeks ago

I eventually do want to make source handling more flexible, since I want to add memory mapped file support, so this isn't entirely out of the question. I'll have to think about it, but I'll reopen this until it's certain that this is an impossible/unwanted feature.

y-haidar commented 3 weeks ago

If it is helpful, this is what I did: https://github.com/y-haidar/hexerator/tree/feat_plugin_source_provider

When I run it via cargo run -- --load-plugin target/debug/libhello_world.so I get this:

image

I don't fully understand the code base yet, so I doubt my work will be useful, but hopefully you can use it to gauge the difficulty of this feature.

y-haidar commented 3 weeks ago

hexerator

Now that I am beginning to understand the project code, this feature isn't that difficult. Managed to get streaming to work.

Want me to make a PR? If yes, then feel free to tell me what you need.

I am thinking of splitting normal plugins and source provider plugins, in order to avoid having other plugins in a RwLock. This means, that a plugin can only be a normal plugin(Plugin) or a source provider plugin (PluginSP). I had to use RwLock due to the thread in try_read_stream.

crumblingstatue commented 3 weeks ago

Want me to make a PR? If yes, then feel free to tell me what you need.

Feel free to make a PR, and I'll review it. I'm fine with experimental features in Hexerator, since I myself have a lot of weird half-baked experimental features implemented in it.