Skip to content

Commit

Permalink
WIP tracy
Browse files Browse the repository at this point in the history
  • Loading branch information
trumank committed Jun 8, 2024
1 parent 4f455a9 commit d090b5a
Show file tree
Hide file tree
Showing 5 changed files with 217 additions and 41 deletions.
149 changes: 123 additions & 26 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions hook/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -33,3 +33,6 @@ bitflags = "2.4.1"
widestring = "1.0.2"
tokio = { workspace = true, features = ["full"] }
tracing-appender = "0.2.3"
seq-macro = "0.3.5"
tracing-subscriber = "0.3.18"
tracing-tracy = "0.11.0"
76 changes: 68 additions & 8 deletions hook/src/hooks/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
mod server_list;

use std::{
collections::HashMap,
ffi::c_void,
path::{Path, PathBuf},
ptr::NonNull,
Expand All @@ -17,7 +18,7 @@ use windows::Win32::System::Memory::{VirtualProtect, PAGE_EXECUTE_READWRITE};

use crate::{
globals,
ue::{self, FLinearColor, UObject},
ue::{self, kismet::FFrame, FLinearColor, UFunction, UObject},
LOG_GUARD,
};

Expand Down Expand Up @@ -281,27 +282,86 @@ fn does_save_game_exist_detour(slot_name: *const ue::FString, user_index: i32) -
}
}

struct NativesArray([Option<ExecFn>; 0x100]);

static mut GNATIVES_OLD: NativesArray = NativesArray([None; 0x100]);
static mut NAME_CACHE: Option<HashMap<usize, String>> = None;

unsafe fn debug(expr: usize, ctx: *mut UObject, frame: *mut FFrame, ret: *mut c_void) {
if NAME_CACHE.is_none() {
NAME_CACHE = Some(Default::default());
}

let (index, path) = {
let stack = &*frame;
let func = &*(stack.node as *const UFunction);

let index = 0;
//TODO causes issues for some reason
//let index = (stack.code as isize)
// .wrapping_sub(func.ustruct.script.as_ptr() as isize);

let path = NAME_CACHE
.as_mut()
.unwrap_unchecked()
.entry(stack.node as usize)
.or_insert_with(|| {
func.ustruct
.ufield
.uobject
.uobject_base_utility
.uobject_base
.get_path_name(None)
});
(index, path)
};

tracing::info_span!("kismet", path, index).in_scope(|| {
((GNATIVES_OLD.0)[expr].unwrap())(ctx, frame, ret);
});
}

unsafe extern "system" fn hook_exec<const N: usize>(
ctx: *mut UObject,
frame: *mut FFrame,
ret: *mut c_void,
) {
debug(N, ctx, frame, ret);
}

unsafe fn hook_gnatives(gnatives: &mut NativesArray) {
seq_macro::seq!(N in 0..256 {
(GNATIVES_OLD.0)[N] = gnatives.0[N];
gnatives.0[N] = Some(hook_exec::<N>);
});
}

fn detour_main(
h_instance: *mut (),
h_prev_instance: *mut (),
lp_cmd_line: *mut (),
n_cmd_show: i32,
cmd_line: *const (),
) -> i32 {
let ret = unsafe {
WinMain.call(
unsafe {
if let Ok(debug) = &globals().resolution.debug {
tracing::info!("hooking GNatives");
hook_gnatives((debug.gnatives.0 as *mut NativesArray).as_mut().unwrap());
}

let ret = WinMain.call(
h_instance,
h_prev_instance,
lp_cmd_line,
n_cmd_show,
cmd_line,
)
};
);

// about to exit, drop log guard
drop(unsafe { LOG_GUARD.take() });
// about to exit, drop log guard
drop(LOG_GUARD.take());

ret
ret
}
}

unsafe extern "system" fn exec_get_mod_json(
Expand Down
Loading

0 comments on commit d090b5a

Please sign in to comment.