Struct red4ext_rs::SdkEnv

source ·
pub struct SdkEnv { /* private fields */ }
Expand description

A handle to the RED4ext SDK environment. This struct enables access to the SDK’s functions and logging facilities. It can be obtained statically using the PluginOps::env method from any plugin implementation.

Implementations§

source§

impl SdkEnv

source

pub fn info(&self, txt: impl Display)

Logs a message at the info level. You should generally use the info! macro instead of calling this method directly.

source

pub fn warn(&self, txt: impl Display)

Logs a message at the warn level. You should generally use the warn! macro instead of calling this method directly.

source

pub fn error(&self, txt: impl Display)

Logs a message at the error level. You should generally use the error! macro instead of calling this method directly.

source

pub fn debug(&self, txt: impl Display)

Logs a message at the debug level. You should generally use the debug! macro instead of calling this method directly.

source

pub fn trace(&self, txt: impl Display)

Logs a message at the trace level. You should generally use the trace! macro instead of calling this method directly.

source

pub fn add_listener(&self, typ: StateType, listener: StateListener) -> bool

Adds a listener to a specific state type. The listener will be called when the state is entered, updated, or exited. See StateType for the available state types.

§Example
use red4ext_rs::{GameApp, SdkEnv, StateListener, StateType};

fn add_state_listener(env: &SdkEnv) {
    let listener = StateListener::default()
        .with_on_enter(on_enter)
        .with_on_exit(on_exit);
    env.add_listener(StateType::Running, listener);
}

unsafe extern "C" fn on_enter(app: &GameApp) {
    // do something here...
}

unsafe extern "C" fn on_exit(app: &GameApp) {
    // do something here...
}
source

pub unsafe fn attach_hook<F1, A1, R1, F2, A2, R2>( &self, hook: *mut Hook<F1, F2>, target: F1, detour: F2, ) -> bool
where F1: FnPtr<A1, R1>, F2: FnPtr<A2, R2>,

Attaches a hook to a target function. The hook will be called instead of the target function. The hook must accept a callback function as its last argument, which should be called to execute the original function.

§Safety

The target and detour functions must both be valid and compatible function pointers.

§Example
use red4ext_rs::{hooks, SdkEnv};

hooks! {
   static ADD_HOOK: fn(a: u32, b: u32) -> u32;
}

fn attach_my_hook(env: &SdkEnv, addr: unsafe extern "C" fn(u32, u32) -> u32) {
    unsafe { env.attach_hook(ADD_HOOK, addr, detour) };
}

unsafe extern "C" fn detour(a: u32, b: u32, cb: unsafe extern "C" fn(u32, u32) -> u32) -> u32 {
    // do something here...
    cb(a, b)
}
source

pub unsafe fn detach_hook<F, A, R>(&self, target: F) -> bool
where F: FnPtr<A, R>,

Detaches a hook from a target function.

Trait Implementations§

source§

impl Debug for SdkEnv

source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
source§

impl Send for SdkEnv

source§

impl Sync for SdkEnv

Auto Trait Implementations§

Blanket Implementations§

source§

impl<T> Any for T
where T: 'static + ?Sized,

source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
source§

impl<T> Borrow<T> for T
where T: ?Sized,

source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
source§

impl<T> From<T> for T

source§

fn from(t: T) -> T

Returns the argument unchanged.

source§

impl<T, U> Into<U> for T
where U: From<T>,

source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

§

type Error = Infallible

The type returned in the event of a conversion error.
source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.