Skip to content

Commit

Permalink
Add bxt_viewmodel_remove (#52)
Browse files Browse the repository at this point in the history
* Add bxt_viewmodel_remove

* check R_PreDrawViewModel

* add pattern

* Add 4554 pattern for R_DrawViewModel

* changes requested

* what?

* Update src/hooks/engine.rs

* Update src/hooks/engine.rs

Co-authored-by: Ivan Molodetskikh <[email protected]>
  • Loading branch information
khanghugo and YaLTeR authored Oct 28, 2022
1 parent 4f92e25 commit 825ab54
Show file tree
Hide file tree
Showing 3 changed files with 97 additions and 0 deletions.
55 changes: 55 additions & 0 deletions src/hooks/engine.rs
Original file line number Diff line number Diff line change
Expand Up @@ -536,6 +536,33 @@ pub static R_DrawSkyBox: Pointer<unsafe extern "C" fn()> = Pointer::empty_patter
]),
my_R_DrawSkyBox as _,
);
pub static R_DrawViewModel: Pointer<unsafe extern "C" fn()> = Pointer::empty_patterns(
// To find, search for "R_RenderView". This is R_RenderView.
// There will be an assignment of `mirror = false` and a function call follows.
// The next line should be branching of `r_refdef.onlyClientDraws == false`, which will repeat again in R_RenderView().
// R_DrawViewModel is called in the block where branch appears the second time.
// In that branch block, it contains two functions called. The first one is R_DrawViewModel().
b"R_DrawViewModel\0",
Patterns(&[
// 8684
pattern!(55 8B EC 83 EC 50 D9 05 ?? ?? ?? ?? D8 1D ?? ?? ?? ?? 56 57 33 FF C7 45),
// 4554
pattern!(83 EC ?? D9 05 ?? ?? ?? ?? D8 1D ?? ?? ?? ?? 56 57 33 FF C7 44),
]),
my_R_DrawViewModel as _,
);
pub static R_PreDrawViewModel: Pointer<unsafe extern "C" fn()> = Pointer::empty_patterns(
// To find, search for "R_RenderView". This is R_RenderView.
// There will be an assignment of `mirror = false` and a function call follows.
// The next line should be branching of `r_refdef.onlyClientDraws == false`, which will repeat again in R_RenderView().
// In that branching block, there is one function called, that is R_PreDrawViewModel().
b"R_PreDrawViewModel\0",
Patterns(&[
// 8684
pattern!(D9 05 ?? ?? ?? ?? D8 1D ?? ?? ?? ?? 56 C7 05),
]),
my_R_PreDrawViewModel as _,
);
pub static R_SetFrustum: Pointer<unsafe extern "C" fn()> = Pointer::empty_patterns(
b"R_SetFrustum\0",
// To find, search for "R_RenderView". This is R_RenderView(). The call between two if (global
Expand Down Expand Up @@ -843,6 +870,8 @@ static POINTERS: &[&dyn PointerTrait] = &[
&R_Clear,
&R_DrawSequentialPoly,
&R_DrawSkyBox,
&R_DrawViewModel,
&R_PreDrawViewModel,
&S_PaintChannels,
&S_TransferStereo16,
&SCR_DrawLoading,
Expand Down Expand Up @@ -1713,6 +1742,32 @@ pub mod exported {
})
}

#[export_name = "R_DrawViewModel"]
pub unsafe extern "C" fn my_R_DrawViewModel() {
abort_on_panic(move || {
let marker = MainThreadMarker::new();

if viewmodel::is_removed(marker) {
return;
}

R_DrawViewModel.get(marker)();
})
}

#[export_name = "R_PreDrawViewModel"]
pub unsafe extern "C" fn my_R_PreDrawViewModel() {
abort_on_panic(move || {
let marker = MainThreadMarker::new();

if viewmodel::is_removed(marker) {
return;
}

R_PreDrawViewModel.get(marker)();
})
}

#[export_name = "V_ApplyShake"]
pub unsafe extern "C" fn my_V_ApplyShake(
origin: *mut [f32; 3],
Expand Down
2 changes: 2 additions & 0 deletions src/modules/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ pub mod tas_optimizer;
pub mod tas_recording;
pub mod tas_server_time_fix;
pub mod triangle_drawing;
pub mod viewmodel;
pub mod wallhack;

/// Trait for getting module information.
Expand Down Expand Up @@ -94,5 +95,6 @@ pub static MODULES: &[&dyn Module] = &[
&tas_recording::TasRecording,
&tas_server_time_fix::TasServerTimeFix,
&triangle_drawing::TriangleDrawing,
&viewmodel::Viewmodel,
&wallhack::Wallhack,
];
40 changes: 40 additions & 0 deletions src/modules/viewmodel.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
//! `bxt_viewmodel_remove`
use super::Module;
use crate::hooks::engine;
use crate::modules::cvars::CVar;
use crate::utils::*;

pub struct Viewmodel;
impl Module for Viewmodel {
fn name(&self) -> &'static str {
"bxt_viewmodel_remove"
}

fn description(&self) -> &'static str {
"Removing the viewmodel."
}

fn cvars(&self) -> &'static [&'static CVar] {
static CVARS: &[&CVar] = &[&BXT_VIEWMODEL_REMOVE];
CVARS
}

fn is_enabled(&self, marker: MainThreadMarker) -> bool {
engine::R_DrawViewModel.is_set(marker) && engine::R_PreDrawViewModel.is_set(marker)
}
}

static BXT_VIEWMODEL_REMOVE: CVar = CVar::new(
b"bxt_viewmodel_remove\0",
b"0\0",
"Set to `1` to disable rendering viewmodel.",
);

pub fn is_removed(marker: MainThreadMarker) -> bool {
if !Viewmodel.is_enabled(marker) {
return false;
}

BXT_VIEWMODEL_REMOVE.as_bool(marker)
}

0 comments on commit 825ab54

Please sign in to comment.