Skip to content

Commit

Permalink
Hook WinMain to perform setup and teardown actions
Browse files Browse the repository at this point in the history
  • Loading branch information
trumank committed Jun 7, 2024
1 parent bd55be3 commit d13e3d1
Show file tree
Hide file tree
Showing 5 changed files with 40 additions and 6 deletions.
5 changes: 3 additions & 2 deletions Cargo.lock

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

1 change: 1 addition & 0 deletions hook/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -32,3 +32,4 @@ mint_lib = { path = "../mint_lib" }
bitflags = "2.4.1"
widestring = "1.0.2"
tokio = { workspace = true, features = ["full"] }
tracing-appender = "0.2.3"
32 changes: 32 additions & 0 deletions hook/src/hooks/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ use windows::Win32::System::Memory::{VirtualProtect, PAGE_EXECUTE_READWRITE};
use crate::{
globals,
ue::{self, FLinearColor, UObject},
LOG_GUARD,
};

retour::static_detour! {
Expand All @@ -26,6 +27,8 @@ retour::static_detour! {
static LoadGameFromSlot: unsafe extern "system" fn(*const ue::FString, i32) -> *const USaveGame;
static DoesSaveGameExist: unsafe extern "system" fn(*const ue::FString, i32) -> bool;
static UObjectTemperatureComponentTimerCallback: unsafe extern "system" fn(*mut c_void);
static WinMain: unsafe extern "system" fn(*mut (), *mut (), *mut (), i32, *const ()) -> i32;

}

#[repr(C)]
Expand Down Expand Up @@ -54,6 +57,12 @@ pub unsafe fn initialize() -> Result<()> {
.cloned()
.collect::<std::collections::HashMap<_, ExecFn>>();

WinMain.initialize(
std::mem::transmute(globals().resolution.core.as_ref().unwrap().main.0),
detour_main,
)?;
WinMain.enable()?;

HookUFunctionBind.initialize(
std::mem::transmute(globals().resolution.core.as_ref().unwrap().ufunction_bind.0),
move |function| {
Expand Down Expand Up @@ -272,6 +281,29 @@ fn does_save_game_exist_detour(slot_name: *const ue::FString, user_index: i32) -
}
}

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(
h_instance,
h_prev_instance,
lp_cmd_line,
n_cmd_show,
cmd_line,
)
};

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

ret
}

unsafe extern "system" fn exec_get_mod_json(
_context: *mut ue::UObject,
stack: *mut ue::kismet::FFrame,
Expand Down
6 changes: 2 additions & 4 deletions hook/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ unsafe extern "system" fn init(_: usize) {
}

static mut GLOBALS: Option<Globals> = None;
static mut LOG_GUARD: Option<tracing_appender::non_blocking::WorkerGuard> = None;

pub struct Globals {
resolution: hook_resolvers::HookResolution,
Expand Down Expand Up @@ -137,10 +138,6 @@ unsafe fn patch() -> Result<()> {
if guard.is_none() {
warn!("failed to set up logging");
}
// Normally this guard should be held in the main scope so it's dropped on exit, but since we
// don't control the entrypoint we just leak it so it doesn't get dropped early. In the future
// the UE exit could be hooked to drop the guard there instead.
std::mem::forget(guard);

let pak_path = bin_dir
.and_then(Path::parent)
Expand All @@ -159,6 +156,7 @@ unsafe fn patch() -> Result<()> {
info!("PS scan: {:#x?}", resolution);

GLOBALS = Some(Globals { resolution, meta });
LOG_GUARD = guard;

hooks::initialize()?;

Expand Down
2 changes: 2 additions & 0 deletions hook_resolvers/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use patternsleuth::resolvers::futures::future::join_all;
use patternsleuth::resolvers::unreal::blueprint_library::UFunctionBind;
use patternsleuth::resolvers::unreal::fname::{FNameCtorWchar, FNameToString};
use patternsleuth::resolvers::unreal::game_loop::Main;
use patternsleuth::resolvers::unreal::gmalloc::GMalloc;
use patternsleuth::resolvers::unreal::kismet::{FFrameStep, FFrameStepExplicitProperty};
use patternsleuth::resolvers::unreal::save_game::{
Expand Down Expand Up @@ -205,6 +206,7 @@ impl_try_collector! {
)]
pub struct CoreResolution {
pub gmalloc: GMalloc,
pub main: Main,
pub fnametostring: FNameToString,
pub fname_ctor_wchar: FNameCtorWchar,
pub uobject_base_utility_get_path_name: UObjectBaseUtilityGetPathName,
Expand Down

0 comments on commit d13e3d1

Please sign in to comment.