hax-rs / hax

🧪 The versatile and intuitive memory hacking framework.
https://crates.io/crates/hax
MIT License
29 stars 0 forks source link

Feature list #5

Closed not-matthias closed 1 year ago

not-matthias commented 1 year ago

Check if we can use this: https://crates.io/crates/inventory

not-matthias commented 1 year ago

Experimented a little bit and i think we could use the inventory crate for feature registration.

image

it has a few downsides though:

  1. these are registered by the loader, so if you are manual mapping, you need to make sure that these are handled correctly. We could use linkme (https://docs.rs/linkme/0.3.6/linkme/) then, since it only requires linker features.
  2. you need to create the struct at compile-time. linkme could be used to expose an init function, which returns the struct.

@7ap what do you think?

7ap commented 1 year ago

I think this is a great solution! I'll need to take a look at inventory myself but from your screenshots it shows like it does what it needs to do.

not-matthias commented 1 year ago

Implemented both versions and I think it's easier to go for linkme.

inventory

We can't just create the object, we need to pass the function so that we can create it at compile time.

use crate::{Feature, FeatureTrait};

pub struct Esp {
    pub enabled: bool,
}

impl FeatureTrait for Esp {
    fn new() -> Box<dyn FeatureTrait> {
        Box::new(Self { enabled: false })
    }
}

inventory::submit! {
    Feature::new("ESP", Esp::new)
}

linkme

Here we also have the option to link one function, but it's much less verbose and doesn't require life-before-main. We can hide the functionality of linkme so that they never even have to specify the initialization function.

use linkme::distributed_slice;

use crate::Feature;

pub struct Esp {
    pub enabled: bool,
}

impl Feature for Esp {
    fn new() -> Box<Self> {
        Box::new(Self { enabled: false })
    }
}

#[distributed_slice(crate::FEATURES)]
fn init_esp() -> Box<dyn Feature> {
    println!("Init esp");

    Esp::new()
}
not-matthias commented 1 year ago

Implemented the first draft. See:

@7ap Feedback is welcome.