Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature/er fps patch arc mutex bool #64

Open
wants to merge 3 commits into
base: feature/ER-fps-patch
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
67 changes: 50 additions & 17 deletions src/soulmods/src/games/x64/eldenring.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.

use std::sync::{Arc, Mutex};
use ilhook::x64::{Hooker, HookType, Registers, CallbackOption, HookFlags, HookPoint};
use mem_rs::prelude::*;
use log::info;
Expand All @@ -36,8 +37,8 @@ static mut FPS_HOOK: Option<HookPoint> = None;
static mut FPS_HISTORY_HOOK: Option<HookPoint> = None;
static mut FPS_CUSTOM_LIMIT_HOOK: Option<HookPoint> = None;

static mut FPS_HOOK_ENABLED: bool = false;
static mut FPS_CUSTOM_LIMIT: f32 = 0.0f32;
static mut FPS_HOOK_ENABLED: Option<Mutex<bool>> = None;
static mut FPS_CUSTOM_LIMIT: Option<Arc<Mutex<f32>>> = None;

static mut FPS_OFFSETS: FpsOffsets = FpsOffsets {
target_frame_delta: 0x0,
Expand All @@ -54,7 +55,10 @@ pub fn init_eldenring()
unsafe
{
info!("version: {}", GLOBAL_VERSION);


FPS_HOOK_ENABLED = Some(Mutex::new(false));
FPS_CUSTOM_LIMIT = Some(Arc::new(Mutex::new(0.0f32)));

// Get ER process
let mut process = Process::new("eldenring.exe");
process.refresh().unwrap();
Expand Down Expand Up @@ -129,30 +133,54 @@ pub fn init_eldenring()
}
}

#[no_mangle]
pub extern "C" fn fps_patch_get(b: &mut bool) // Get FPS patch status
fn get_is_fps_hook_enabled() -> bool
{
unsafe
if let Some(mutex) = unsafe { FPS_HOOK_ENABLED.as_ref() }
{
*b = FPS_HOOK_ENABLED;
if let Ok(guard) = mutex.lock()
{
return *guard;
}
panic!("Failed to get FPS_HOOK_ENABLED mutex lock");
}
panic!("Failed to get FPS_HOOK_ENABLED option as ref");
}

#[no_mangle]
pub extern "C" fn fps_patch_set(b: &bool) // Set FPS patch status
fn set_is_fps_hook_enabled(value: bool)
{
unsafe
if let Some(mutex) = unsafe { FPS_HOOK_ENABLED.as_ref() }
{
FPS_HOOK_ENABLED = *b;
if let Ok(mut guard) = mutex.lock()
{
*guard = value;
}
panic!("Failed to get FPS_HOOK_ENABLED mutex lock");
}
panic!("Failed to get FPS_HOOK_ENABLED option as ref");
}



#[no_mangle]
pub extern "C" fn fps_patch_get(b: &mut bool) // Get FPS patch status
{
*b = get_is_fps_hook_enabled();
}

#[no_mangle]
pub extern "C" fn fps_patch_set(b: &bool) // Set FPS patch status
{
set_is_fps_hook_enabled(*b);
}

#[no_mangle]
pub extern "C" fn fps_limit_get(f: &mut f32) // Get FPS custom limit
{
unsafe
{
*f = FPS_CUSTOM_LIMIT;
let arc = Arc::clone(FPS_CUSTOM_LIMIT.as_ref().unwrap());
let fps_custom_limit = arc.lock().unwrap();
*f = *fps_custom_limit;
}
}

Expand All @@ -161,7 +189,9 @@ pub extern "C" fn fps_limit_set(f: &f32) // Set FPS custom limit
{
unsafe
{
FPS_CUSTOM_LIMIT = *f;
let arc = Arc::clone(FPS_CUSTOM_LIMIT.as_ref().unwrap());
let mut fps_custom_limit = arc.lock().unwrap();
*fps_custom_limit = *f;
}
}

Expand Down Expand Up @@ -194,7 +224,7 @@ unsafe extern "win64" fn increment_igt(registers: *mut Registers, _:usize)
// A second patch, "FPS history" below, is required in addition to this one to ensure accuracy.
unsafe extern "win64" fn fps(registers: *mut Registers, _:usize)
{
if FPS_HOOK_ENABLED
if get_is_fps_hook_enabled()
{
let ptr_flipper = (*registers).rbx as *const u8; // Flipper struct - Contains all the stuff we need

Expand Down Expand Up @@ -223,7 +253,7 @@ unsafe extern "win64" fn fps(registers: *mut Registers, _:usize)
// This gets stored in an array with 32 elements, possibly for calculating FPS averages.
unsafe extern "win64" fn fps_history(registers: *mut Registers, _:usize)
{
if FPS_HOOK_ENABLED
if get_is_fps_hook_enabled()
{
let ptr_flipper = (*registers).rbx as *const u8; // Flipper struct - Contains all the stuff we need

Expand All @@ -245,15 +275,18 @@ unsafe extern "win64" fn fps_history(registers: *mut Registers, _:usize)
// This does not allow you to go above the stock FPS limit. It is purely a QoL patch to improve glitch consistency, not an FPS unlocker.
unsafe extern "win64" fn fps_custom_limit(registers: *mut Registers, _:usize)
{
if FPS_HOOK_ENABLED && FPS_CUSTOM_LIMIT > 0.0f32
let arc_custom_limit = Arc::clone(FPS_CUSTOM_LIMIT.as_ref().unwrap());
let fps_custom_limit = arc_custom_limit.lock().unwrap();

if get_is_fps_hook_enabled() && *fps_custom_limit > 0.0f32
{
let ptr_flipper = (*registers).rbx as *const u8; // Flipper struct - Contains all the stuff we need

let ptr_target_frame_delta = ptr_flipper.offset(FPS_OFFSETS.target_frame_delta) as *mut f32; // Target frame delta - Set in a switch/case at the start

// Read the stock target frame delta and calculate the custom target frame delta
let target_frame_delta = std::ptr::read_volatile(ptr_target_frame_delta);
let custom_target_frame_delta = 1.0f32 / FPS_CUSTOM_LIMIT;
let custom_target_frame_delta = 1.0f32 / *fps_custom_limit;

// Make sure the custom target frame delta is higher than the stock one, in order to avoid going above the stock FPS limit
if custom_target_frame_delta > target_frame_delta
Expand Down
10 changes: 8 additions & 2 deletions src/soulmods/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,14 +22,14 @@ mod games;
mod util;

use std::ffi::c_void;
use std::{env, thread};
use std::{env, panic, thread};
use mem_rs::prelude::Process;
use windows::Win32::Foundation::{BOOL, HINSTANCE};
use windows::Win32::System::SystemServices::{ DLL_PROCESS_ATTACH, DLL_PROCESS_DETACH};

use crate::console::init_console;
use crate::logger::init_log;
use log::info;
use log::{error, info};
use crate::util::{GLOBAL_HMODULE, GLOBAL_VERSION, Version};


Expand Down Expand Up @@ -67,6 +67,12 @@ fn dispatched_dll_main()
let process_name = Process::get_current_process_name().unwrap();
info!("process: {}", process_name);

//Redirect panics
panic::set_hook(Box::new(|i| {
error!("panic");
error!("{}", i);
}));

#[cfg(target_arch = "x86_64")]
match process_name.to_lowercase().as_str()
{
Expand Down
Loading