Skip to content

Commit

Permalink
Hook CalcRefdef through function table
Browse files Browse the repository at this point in the history
  • Loading branch information
YaLTeR committed Jul 26, 2023
1 parent f512916 commit 94211d6
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 25 deletions.
24 changes: 22 additions & 2 deletions src/hooks/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@
use std::os::raw::*;
use std::ptr::NonNull;

use super::engine;
use super::engine::{self, ref_params_s};
use crate::ffi::usercmd::usercmd_s;
use crate::modules::{hud, hud_scale, tas_studio, triangle_drawing};
use crate::modules::{hud, hud_scale, tas_studio, triangle_drawing, viewmodel_sway};
use crate::utils::{abort_on_panic, MainThreadMarker, Pointer, PointerTrait};

pub static HudInitFunc: Pointer<unsafe extern "C" fn()> = Pointer::empty(b"HudInitFunc\0");
Expand All @@ -16,6 +16,8 @@ pub static HudRedrawFunc: Pointer<unsafe extern "C" fn(c_float, c_int)> =
Pointer::empty(b"HudRedrawFunc\0");
pub static HudUpdateClientDataFunc: Pointer<unsafe extern "C" fn(*mut c_void, c_float) -> c_int> =
Pointer::empty(b"HudUpdateClientDataFunc\0");
pub static CalcRefdef: Pointer<unsafe extern "C" fn(*mut ref_params_s)> =
Pointer::empty(b"CalcRefdef\0");
pub static IN_ActivateMouse: Pointer<unsafe extern "C" fn()> =
Pointer::empty(b"IN_ActivateMouse\0");
pub static IN_DeactivateMouse: Pointer<unsafe extern "C" fn()> =
Expand Down Expand Up @@ -88,6 +90,12 @@ pub unsafe fn hook_client_interface(marker: MainThreadMarker) {
functions.HudUpdateClientDataFunc = Some(my_HudUpdateClientDataFunc);
HudUpdateClientDataFunc.log(marker);

if let Some(ptr) = &mut functions.CalcRefdef {
CalcRefdef.set(marker, Some(NonNull::new_unchecked(*ptr as _)));
}
functions.CalcRefdef = Some(my_CalcRefdef);
CalcRefdef.log(marker);

if let Some(ptr) = &mut functions.IN_ActivateMouse {
IN_ActivateMouse.set(marker, Some(NonNull::new_unchecked(*ptr as _)));
}
Expand Down Expand Up @@ -138,6 +146,8 @@ unsafe fn reset_client_interface(marker: MainThreadMarker) {
HudRedrawFunc.reset(marker);
functions.HudUpdateClientDataFunc = HudUpdateClientDataFunc.get_opt(marker);
HudUpdateClientDataFunc.reset(marker);
functions.CalcRefdef = CalcRefdef.get_opt(marker);
CalcRefdef.reset(marker);
functions.IN_ActivateMouse = IN_ActivateMouse.get_opt(marker);
IN_ActivateMouse.reset(marker);
functions.IN_DeactivateMouse = IN_DeactivateMouse.get_opt(marker);
Expand Down Expand Up @@ -236,6 +246,16 @@ pub unsafe extern "C" fn my_HudRedrawFunc(time: c_float, intermission: c_int) {
})
}

pub unsafe extern "C" fn my_CalcRefdef(rp: *mut ref_params_s) {
abort_on_panic(move || {
let marker = MainThreadMarker::new();

CalcRefdef.get(marker)(rp);

viewmodel_sway::add_viewmodel_sway(marker, &*rp);
})
}

pub unsafe extern "C" fn my_DrawTransparentTriangles() {
abort_on_panic(move || {
let marker = MainThreadMarker::new();
Expand Down
22 changes: 0 additions & 22 deletions src/hooks/engine.rs
Original file line number Diff line number Diff line change
Expand Up @@ -121,15 +121,6 @@ pub static CL_PlayDemo_f: Pointer<unsafe extern "C" fn()> = Pointer::empty_patte
]),
my_CL_PlayDemo_f as _,
);
// TODO: figure out how to use V_CalcRefdef from cl_funcs instead of this.
pub static ClientDLL_CalcRefdef: Pointer<unsafe extern "C" fn(*mut ref_params_s)> =
Pointer::empty_patterns(
b"ClientDLL_CalcRefdef\0",
// You are in V_RenderView. The first call taking in one argument inside the first `do{}`
// block will be ClientDLL_CalcRefdef().
Patterns(&[]),
my_ClientDLL_CalcRefdef as _,
);
pub static ClientDLL_Init: Pointer<unsafe extern "C" fn()> = Pointer::empty_patterns(
b"ClientDLL_Init\0",
// To find, search for "cl_dlls\\client.dll" (with a backslash).
Expand Down Expand Up @@ -874,7 +865,6 @@ static POINTERS: &[&dyn PointerTrait] = &[
&CL_Move,
&CL_PlayDemo_f,
&ClientDLL_Init,
&ClientDLL_CalcRefdef,
&ClientDLL_DrawTransparentTriangles,
&cl,
&cl_viewent,
Expand Down Expand Up @@ -1709,7 +1699,6 @@ pub unsafe fn find_pointers(marker: MainThreadMarker, base: *mut c_void, size: u
Some(0) => {
cl_viewent.set(marker, ptr.by_offset(marker, 146));
cl_viewent_viewmodel.set(marker, cl_viewent.offset(marker, 2888));
ClientDLL_CalcRefdef.set(marker, ptr.by_relative_call(marker, 166));
}
_ => (),
}
Expand Down Expand Up @@ -2205,17 +2194,6 @@ pub mod exported {
})
}

#[export_name = "ClientDLL_CalcRefdef"]
pub unsafe extern "C" fn my_ClientDLL_CalcRefdef(rp: *mut ref_params_s) {
abort_on_panic(move || {
let marker = MainThreadMarker::new();

ClientDLL_CalcRefdef.get(marker)(rp);

viewmodel_sway::add_viewmodel_sway(marker, &*rp);
})
}

#[export_name = "ClientDLL_Init"]
pub unsafe extern "C" fn my_ClientDLL_Init() {
abort_on_panic(move || {
Expand Down
1 change: 0 additions & 1 deletion src/modules/viewmodel_sway.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@ impl Module for ViewmodelSway {

fn is_enabled(&self, marker: MainThreadMarker) -> bool {
CVars.is_enabled(marker)
&& engine::ClientDLL_CalcRefdef.is_set(marker)
&& engine::cl_viewent.is_set(marker)
&& engine::cl_viewent_viewmodel.is_set(marker)
}
Expand Down

0 comments on commit 94211d6

Please sign in to comment.