Skip to content

Commit

Permalink
Merge pull request #1 from blu-dev/master
Browse files Browse the repository at this point in the history
  • Loading branch information
jam1garner authored Jan 21, 2021
2 parents 2e76f13 + a3e9630 commit 7b2462b
Showing 1 changed file with 29 additions and 7 deletions.
36 changes: 29 additions & 7 deletions src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#![feature(proc_macro_hygiene)]

use skyline::{hook, install_hook};
use skyline::{hook, install_hooks};
use skyline::nn::ro;
use skyline::libc::{c_void, c_int, size_t};
use skyline::from_c_str;
Expand All @@ -11,7 +11,8 @@ use skyline::nro::NroInfo;

type Callback = fn(&NroInfo);

static HOOKS: Mutex<Vec<Callback>> = Mutex::new(Vec::new());
static LOAD_HOOKS: Mutex<Vec<Callback>> = Mutex::new(Vec::new());
static UNLOAD_HOOKS: Mutex<Vec<Callback>> = Mutex::new(Vec::new());

#[hook(replace = ro::LoadModule)]
pub fn handle_load_module(
Expand All @@ -28,23 +29,44 @@ pub fn handle_load_module(
let name = unsafe { from_c_str(&(*out_module).Name as *const u8) };
println!("[NRO hook] Loaded {}.", name);
let nro_info = NroInfo::new(&name, unsafe { &mut *out_module });
for hook in HOOKS.lock().iter() {
for hook in LOAD_HOOKS.lock().iter() {
hook(&nro_info)
}

ret
}

#[hook(replace = ro::UnloadModule)]
pub fn handle_unload_module(in_module: *mut ro::Module) -> c_int {
let ret = original!()(in_module);

let name = unsafe { from_c_str(&(*in_module).Name as *const u8) };
println!("[NRO hook] Unloaded {}.", name);
let nro_info = NroInfo::new(&name, unsafe { &mut *in_module });
for hook in UNLOAD_HOOKS.lock().iter() {
hook(&nro_info);
}

ret
}

#[skyline::main(name = "nro_hook")]
pub fn main() {
println!("[NRO hook] Installing NRO hook...");
install_hook!(handle_load_module);
println!("[NRO hook] NRO hook installed.");
println!("[NRO hook] Installing NRO hooks...");
install_hooks!(handle_load_module, handle_unload_module);
println!("[NRO hook] NRO hooks installed.");
}

#[no_mangle]
pub extern "Rust" fn add_nro_load_hook(callback: Callback) {
let mut hooks = HOOKS.lock();
let mut hooks = LOAD_HOOKS.lock();

hooks.push(callback);
}

#[no_mangle]
pub extern "Rust" fn add_nro_unload_hook(callback: Callback) {
let mut hooks = UNLOAD_HOOKS.lock();

hooks.push(callback);
}

0 comments on commit 7b2462b

Please sign in to comment.