Skip to content

Commit

Permalink
Allow user to modify width and height of reticle (#354)
Browse files Browse the repository at this point in the history
* Allow user to modify width, height and scale of reticle

* Fix Linux build

* Respond to review

* Respond to review 2

* Respond to review 3

* Respond to review 4
  • Loading branch information
LukeSaward1 authored Nov 5, 2024
1 parent d46ecca commit ec5b750
Show file tree
Hide file tree
Showing 14 changed files with 139 additions and 2 deletions.
1 change: 1 addition & 0 deletions gen-consts/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ const NAMES: &[(&str, &str)] = &[
("?DrawText@AHUD@@QAEXABVFString@@UFLinearColor@@MMPAVUFont@@M_N@Z", "AHUD_DRAWTEXT"),
("?DrawTextureSimple@AHUD@@QAEXPAVUTexture@@MMM_N@Z", "AHUD_DRAWTEXTURESIMPLE"),
("?DrawTexture@AHUD@@QAEXPAVUTexture@@MMMMMMMMUFLinearColor@@W4EBlendMode@@M_NMUFVector2D@@@Z", "AHUD_DRAWTEXTURE"),
("?DrawMaterialSimple@AHUD@@QAEXPAVUMaterialInterface@@MMMMM_N@Z", "AHUD_DRAWMATERIALSIMPLE"),
("?Project@AHUD@@QBE?AUFVector@@U2@@Z", "AHUD_PROJECT"),
("?GetTextSize@AHUD@@QBEXABVFString@@AAM1PAVUFont@@M@Z", "AHUD_GETTEXTSIZE"),
("=GWorld", "GWORLD"),
Expand Down
2 changes: 2 additions & 0 deletions make.bat
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ copy tool\movement.re build\practice-windows
copy tool\minimap.re build\practice-windows
copy tool\mapeditor.re build\practice-windows
copy tool\world_options.re build\practice-windows
copy tool\player.re build\practice-windows
echo Converting lf to crlf
call :convert main.re
call :convert prelude.re
Expand All @@ -51,6 +52,7 @@ call :convert movement.re
call :convert minimap.re
call :convert mapeditor.re
call :convert world_options.re
call :convert player.re

echo Don't forget to create a zip
exit /b 0
Expand Down
9 changes: 8 additions & 1 deletion rtil/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 rtil/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ bit_field = "0.10.2"
image = "0.24.6"
ureq = "2.6.2"
corosensei = "0.1.3"
atomic_float = "1.1.0"

[target.'cfg(unix)'.dependencies]
libc = "0.2.119"
Expand Down
31 changes: 30 additions & 1 deletion rtil/src/native/hud.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,13 @@ use std::mem;
use std::ptr;
use std::sync::atomic::{AtomicPtr, Ordering};
use bit_field::BitField;
use atomic_float::AtomicF32;

#[cfg(unix)] use libc::c_void;
#[cfg(windows)] use winapi::ctypes::c_void;

use crate::native::ue::{FLinearColor, FString, FVector, FVector2D};
use crate::native::{AHUD_DRAWLINE, AHUD_DRAWTEXT, AHUD_DRAWTEXTURESIMPLE, AHUD_DRAWTEXTURE, AHUD_PROJECT, AHUD_GETTEXTSIZE, Args, REBO_DOESNT_START_SEMAPHORE, UTexture2D};
use crate::native::{AHUD_DRAWLINE, AHUD_DRAWTEXT, AHUD_DRAWTEXTURESIMPLE, AHUD_DRAWTEXTURE, AHUD_PROJECT, AHUD_GETTEXTSIZE, Args, REBO_DOESNT_START_SEMAPHORE, UTexture2D, UObject, ObjectWrapper, AMyCharacter};
use crate::native::texture::UTexture2DUE;
use crate::threads::ue;

Expand Down Expand Up @@ -102,6 +103,14 @@ impl AMyHud {
pub fn show_hud() {
unsafe { (*get_amyhud!("show_hud")).bitfield.set_bit(1, true); }
}

pub fn set_reticle_width(width: f32) {
RETICLE_W.store(width, Ordering::Relaxed);
}

pub fn set_reticle_height(height: f32) {
RETICLE_H.store(height, Ordering::Relaxed);
}
}

#[rtil_derive::hook_before(AMyHUD::DrawHUD)]
Expand All @@ -116,6 +125,26 @@ fn draw_hud(args: &mut Args) {
ue::draw_hud();
}

static RETICLE_W: AtomicF32 = AtomicF32::new(6.);
static RETICLE_H: AtomicF32 = AtomicF32::new(6.);

#[rtil_derive::hook_before(AHUD::DrawMaterialSimple)]
fn draw_material_simple(args: &mut Args) {
let (
_this, material, screen_x, screen_y, screen_w, screen_h, scale, scale_position
) = unsafe { args.with_this_pointer::<(*mut UObject, *mut UObject, f32, f32, f32, f32, f32, usize)>() };
unsafe {
let obj = ObjectWrapper::new(material);
if obj.name() == "M_Player_Crosshair" {
*screen_w = RETICLE_W.load(Ordering::Relaxed);
*screen_h = RETICLE_H.load(Ordering::Relaxed);
let sw = *screen_w * *scale;
let sh = *screen_h * *scale;
*screen_x = (AMyCharacter::get_player().get_viewport_size().0 as f32 / 2.) - (sw / 2.);
*screen_y = (AMyCharacter::get_player().get_viewport_size().1 as f32 / 2.) - (sh / 2.);
}
}
}
#[allow(unused)]
#[repr(i32)]
pub enum EBlendMode {
Expand Down
1 change: 1 addition & 0 deletions rtil/src/native/linux/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,7 @@ find! {
AHUD_DRAWTEXT, "^AHUD::DrawText(FString const&, FLinearColor, float, float, UFont*, float, bool)",
AHUD_DRAWTEXTURESIMPLE, "^AHUD::DrawTextureSimple(UTexture*, float, float, float, bool)",
AHUD_DRAWTEXTURE, "^AHUD::DrawTexture(UTexture*, float, float, float, float, float, float, float, float, FLinearColor, EBlendMode, float, bool, float, FVector2D)",
AHUD_DRAWMATERIALSIMPLE, "^AHUD::DrawMaterialSimple(UMaterialInterface*, float, float, float, float, float, bool)",
AHUD_PROJECT, "^AHUD::Project(FVector)",
AHUD_GETTEXTSIZE, "^AHUD::GetTextSize(FString const&, float&, float&, UFont*, float)",
GWORLD, "^GWorld",
Expand Down
1 change: 1 addition & 0 deletions rtil/src/native/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,7 @@ pub fn init() {
tick::hook_aliftbase_addbasedcharacter();
tick::hook_aliftbase_removebasedcharacter();
hud::hook_amyhud_drawhud();
hud::hook_ahud_drawmaterialsimple();
character::hook_amycharacter_tick();
gameusersettings::hook_ugameusersettings_applyresolutionsettings();
uworld::hook_uuserwidget_addtoscreen();
Expand Down
1 change: 1 addition & 0 deletions rtil/src/native/windows/consts.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ pub const AHUD_DRAWTEXT: usize = 0xc4b600;
pub const AHUD_DRAWLINE: usize = 0xc4b1c0;
pub const AHUD_DRAWTEXTURE: usize = 0xc4b790;
pub const AHUD_DRAWTEXTURESIMPLE: usize = 0xc4b920;
pub const AHUD_DRAWMATERIALSIMPLE: usize = 0xc4b3d0;
pub const AHUD_PROJECT: usize = 0xc5a680;
pub const UGAMEUSERSETTINGS_APPLYRESOLUTIONSETTINGS: usize = 0xc1e780;
pub const UWORLD_DESTROYACTOR: usize = 0xc93d60;
Expand Down
1 change: 1 addition & 0 deletions rtil/src/native/windows/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,7 @@ find! {
AHUD_DRAWTEXT,
AHUD_DRAWTEXTURESIMPLE,
AHUD_DRAWTEXTURE,
AHUD_DRAWMATERIALSIMPLE,
AHUD_PROJECT,
AHUD_GETTEXTSIZE,
GWORLD,
Expand Down
11 changes: 11 additions & 0 deletions rtil/src/threads/ue/rebo/rebo_init.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ use std::fs::File;
use std::io::{ErrorKind, Write};
use std::ops::Deref;
use std::path::PathBuf;
use std::sync::atomic::Ordering;
use std::time::Duration;
use crossbeam_channel::{Sender, TryRecvError};
use clipboard::{ClipboardProvider, ClipboardContext};
Expand Down Expand Up @@ -133,6 +134,8 @@ pub fn create_config(rebo_stream_tx: Sender<ReboToStream>) -> ReboConfig {
.add_function(set_kill_z)
.add_function(set_gamma)
.add_function(set_screen_percentage)
.add_function(set_reticle_width)
.add_function(set_reticle_height)
.add_external_type(Location)
.add_external_type(Rotation)
.add_external_type(Velocity)
Expand Down Expand Up @@ -1382,4 +1385,12 @@ fn set_gamma(value: f32) {
#[rebo::function("Tas::set_screen_percentage")]
fn set_screen_percentage(percentage: f32) {
UWorld::set_screen_percentage(percentage);
}
#[rebo::function("Tas::set_reticle_width")]
fn set_reticle_width(width: f32) {
AMyHud::set_reticle_width(width);
}
#[rebo::function("Tas::set_reticle_height")]
fn set_reticle_height(height: f32) {
AMyHud::set_reticle_height(height);
}
1 change: 1 addition & 0 deletions tool/main.re
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ include "movement.re";
include "misc.re";
include "mapeditor.re";
include "world_options.re";
include "player.re";

static mut NEW_VERSION: Option<string> = Tas::new_version_string();

Expand Down
6 changes: 6 additions & 0 deletions tool/misc.re
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,12 @@ fn create_misc_menu() -> Ui {
selected: Tas::get_movement_mode(),
onchange: fn(index: int) { Tas::set_movement_mode(index); },
}),
UiElement::Button(UiButton {
label: Text { text: "Player" },
onclick: fn(label: Text) {
enter_ui(create_player_menu());
}
}),
UiElement::Button(UiButton {
label: Text { text: "Minimap" },
onclick: fn(label: Text) {
Expand Down
63 changes: 63 additions & 0 deletions tool/player.re
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
Tas::set_reticle_width(SETTINGS.reticle_w);
Tas::set_reticle_height(SETTINGS.reticle_h);

fn create_reticle_menu() -> Ui {
static mut PLAYER_RETICLE_WIDTH_INPUT_LABEL = Text { text: "Reticle Width" };
static mut PLAYER_RETICLE_HEIGHT_INPUT_LABEL = Text { text: "Reticle Height" };
static mut PLAYER_RETICLE_SCALE_INPUT_LABEL = Text { text: "Reticle Scale" };
Ui::new("Reticle:", List::of(
UiElement::FloatInput(FloatInput {
label: PLAYER_RETICLE_WIDTH_INPUT_LABEL,
input: f"{SETTINGS.reticle_w}",
onclick: fn(input: string) {},
onchange: fn(input: string) {
PLAYER_RETICLE_WIDTH_INPUT_LABEL.text = "Reticle Width";
match input.parse_float() {
Result::Ok(num) => {
Tas::set_reticle_width(num);
SETTINGS.reticle_w = num;
SETTINGS.store();
},
Result::Err(e) => {
PLAYER_RETICLE_WIDTH_INPUT_LABEL.text = f"Reticle Width (invalid value)";
},
}
},
}),
UiElement::FloatInput(FloatInput {
label: PLAYER_RETICLE_HEIGHT_INPUT_LABEL,
input: f"{SETTINGS.reticle_h}",
onclick: fn(input: string) {},
onchange: fn(input: string) {
PLAYER_RETICLE_HEIGHT_INPUT_LABEL.text = "Reticle Height";
match input.parse_float() {
Result::Ok(num) => {
Tas::set_reticle_height(num);
SETTINGS.reticle_h = num;
SETTINGS.store();
},
Result::Err(e) => {
PLAYER_RETICLE_HEIGHT_INPUT_LABEL.text = f"Reticle Height (invalid value)";
},
}
},
}),
UiElement::Button(UiButton {
label: Text { text: "Back" },
onclick: fn(label: Text) { leave_ui() },
}),
))
}

fn create_player_menu() -> Ui {
Ui::new("Player:", List::of(
UiElement::Button(UiButton {
label: Text { text: "Reticle" },
onclick: fn(label: Text) { enter_ui(create_reticle_menu()); }
}),
UiElement::Button(UiButton {
label: Text { text: "Back" },
onclick: fn(label: Text) { leave_ui() },
}),
))
}
12 changes: 12 additions & 0 deletions tool/settings.re
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,10 @@ struct Settings {
day_stars_brightness: float,
night_stars_brightness: float,
screen_percentage: float,
reticle_w: float,
reticle_h: float,
reticle_scale: float,
reticle_scale_position: bool,
}
static mut SETTINGS = Settings::load();

Expand Down Expand Up @@ -200,6 +204,10 @@ impl Settings {
day_stars_brightness: get_float("day_stars_brightness", 0.),
night_stars_brightness: get_float("night_stars_brightness", 5.),
screen_percentage: get_float("screen_percentage", 100.),
reticle_w: get_float("reticle_w", 6.),
reticle_h: get_float("reticle_h", 6.),
reticle_scale: get_float("reticle_scale", 1.),
reticle_scale_position: get_bool("reticle_scale_position", false),
}
}

Expand Down Expand Up @@ -237,6 +245,10 @@ impl Settings {
map.insert("day_stars_brightness", f"{SETTINGS.day_stars_brightness}");
map.insert("night_stars_brightness", f"{SETTINGS.night_stars_brightness}");
map.insert("screen_percentage", f"{SETTINGS.screen_percentage}");
map.insert("reticle_w", f"{SETTINGS.reticle_w}");
map.insert("reticle_h", f"{SETTINGS.reticle_h}");
map.insert("reticle_scale", f"{SETTINGS.reticle_scale}");
map.insert("reticle_scale_position", f"{SETTINGS.reticle_scale_position}");
Tas::store_settings(map);
}

Expand Down

0 comments on commit ec5b750

Please sign in to comment.