|
| 1 | +use std::{ |
| 2 | + mem, |
| 3 | + sync::atomic::{AtomicPtr, Ordering}, |
| 4 | +}; |
| 5 | + |
| 6 | +use hooklet::windows::x86::{hook_call_rel32, CallRel32Hook}; |
| 7 | +use log::debug; |
| 8 | +use p3_api::{class35::Class35Ptr, data::ddraw_set_constant_color, mods, ui::draw_geometry_abs}; |
| 9 | + |
| 10 | +static DRAW_NAVIGATION_LINE_HOOK_PTR: AtomicPtr<CallRel32Hook> = AtomicPtr::new(std::ptr::null_mut()); |
| 11 | +const STROKE_LENGTH: i32 = 4; |
| 12 | + |
| 13 | +#[no_mangle] |
| 14 | +unsafe extern "C" fn start() -> u32 { |
| 15 | + mods::init_mod(); |
| 16 | + |
| 17 | + debug!("Deploying draw_navigation_line hook"); |
| 18 | + match hook_call_rel32(0x4AE38, draw_line_hook as usize as u32) { |
| 19 | + Ok(hook) => { |
| 20 | + DRAW_NAVIGATION_LINE_HOOK_PTR.store(Box::into_raw(Box::new(hook)), Ordering::SeqCst); |
| 21 | + } |
| 22 | + Err(_) => return 1, |
| 23 | + } |
| 24 | + |
| 25 | + 0 |
| 26 | +} |
| 27 | + |
| 28 | +#[no_mangle] |
| 29 | +unsafe extern "thiscall" fn draw_line_hook(this: u32, screen_rectangle: u32) { |
| 30 | + let class35 = Class35Ptr::new(); |
| 31 | + ddraw_set_constant_color(0xffcc0000); |
| 32 | + for i in 0..class35.get_nav_vec_count() { |
| 33 | + let point = class35.get_nav_vec_entry(i as _); |
| 34 | + let x = nav_vec_coord_to_game_coord(point.0 as _); |
| 35 | + let y = nav_vec_coord_to_game_coord(point.1 as _); |
| 36 | + draw_marker(x, y); |
| 37 | + } |
| 38 | + |
| 39 | + let orig_address = (*DRAW_NAVIGATION_LINE_HOOK_PTR.load(Ordering::SeqCst)).old_absolute; |
| 40 | + let orig: extern "thiscall" fn(this: u32, screen_rectangle: u32) = unsafe { mem::transmute(orig_address) }; |
| 41 | + orig(this, screen_rectangle) |
| 42 | +} |
| 43 | + |
| 44 | +unsafe fn draw_marker(x: i32, y: i32) { |
| 45 | + draw_geometry_abs(x - STROKE_LENGTH, y - STROKE_LENGTH, x + STROKE_LENGTH, y + STROKE_LENGTH); |
| 46 | + draw_geometry_abs(x - STROKE_LENGTH, y + STROKE_LENGTH, x + STROKE_LENGTH, y - STROKE_LENGTH); |
| 47 | +} |
| 48 | + |
| 49 | +fn nav_vec_coord_to_game_coord(i: i32) -> i32 { |
| 50 | + (26 * i + 487) * 17 / 125 |
| 51 | +} |
0 commit comments