Skip to content

Commit

Permalink
Appease clippy globals
Browse files Browse the repository at this point in the history
  • Loading branch information
trumank committed Nov 4, 2024
1 parent f0bf3ec commit 926f0a7
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 18 deletions.
29 changes: 14 additions & 15 deletions hook/src/hooks/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use std::{
ffi::c_void,
path::{Path, PathBuf},
ptr::NonNull,
sync::Arc,
sync::{Arc, OnceLock},
};

use anyhow::{Context, Result};
Expand Down Expand Up @@ -106,17 +106,16 @@ pub unsafe fn initialize() -> Result<()> {
}
}
DRGInstallationType::Xbox => {
SAVES_DIR = Some(
std::env::current_exe()
.ok()
.as_deref()
.and_then(Path::parent)
.and_then(Path::parent)
.and_then(Path::parent)
.context("could not determine save location")?
.join("Saved")
.join("SaveGames"),
);
let saves_dir = std::env::current_exe()
.ok()
.as_deref()
.and_then(Path::parent)
.and_then(Path::parent)
.and_then(Path::parent)
.context("could not determine save location")?
.join("Saved")
.join("SaveGames");
SAVES_DIR.get_or_init(|| saves_dir);

if let Ok(save_game) = &globals().resolution.save_game {
SaveGameToSlot
Expand Down Expand Up @@ -207,14 +206,14 @@ unsafe fn patch_mem(address: *mut u8, patch: impl AsRef<[u8]>) -> Result<()> {
Ok(())
}

static mut SAVES_DIR: Option<PathBuf> = None;
static SAVES_DIR: OnceLock<PathBuf> = OnceLock::new();

fn get_path_for_slot(slot_name: &ue::FString) -> Option<PathBuf> {
let mut str_path = slot_name.to_string();
str_path.push_str(".sav");

let path = std::path::Path::new(&str_path);
let mut normalized_path = unsafe { SAVES_DIR.as_ref() }?.clone();
let mut normalized_path = SAVES_DIR.get().unwrap().clone();

for component in path.components() {
if let std::path::Component::Normal(c) = component {
Expand Down Expand Up @@ -300,7 +299,7 @@ fn detour_main(
};

// about to exit, drop log guard
drop(unsafe { LOG_GUARD.take() });
drop(LOG_GUARD.with_borrow_mut(|g| g.take()).unwrap());

ret
}
Expand Down
11 changes: 8 additions & 3 deletions hook/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,9 @@ unsafe extern "system" fn init(_: usize) {
}

static mut GLOBALS: Option<Globals> = None;
static mut LOG_GUARD: Option<tracing_appender::non_blocking::WorkerGuard> = None;
thread_local! {
static LOG_GUARD: std::cell::RefCell<Option<tracing_appender::non_blocking::WorkerGuard>> = None.into();
}

pub struct Globals {
resolution: hook_resolvers::HookResolution,
Expand Down Expand Up @@ -126,7 +128,10 @@ impl Globals {
}

pub fn globals() -> &'static Globals {
unsafe { GLOBALS.as_ref().unwrap() }
#[allow(static_mut_refs)]
unsafe {
GLOBALS.as_ref().unwrap()
}
}

unsafe fn patch() -> Result<()> {
Expand Down Expand Up @@ -156,7 +161,7 @@ unsafe fn patch() -> Result<()> {
info!("PS scan: {:#x?}", resolution);

GLOBALS = Some(Globals { resolution, meta });
LOG_GUARD = guard;
LOG_GUARD.with_borrow_mut(|g| *g = guard);

hooks::initialize()?;

Expand Down

0 comments on commit 926f0a7

Please sign in to comment.