diff --git a/crates/yabg3nml/src/autostart.rs b/crates/yabg3nml/src/autostart.rs index d7e8189..02f80de 100644 --- a/crates/yabg3nml/src/autostart.rs +++ b/crates/yabg3nml/src/autostart.rs @@ -12,7 +12,7 @@ use windows::Win32::System::{ }; use crate::{ - event::Event, loader::run_loader, paths::get_game_binary_paths, setup::init, + event::Event, loader::run_loader, paths::get_game_binary_for, setup::init, single_instance::SingleInstance, }; @@ -41,35 +41,25 @@ pub fn autostart() -> Result<()> { bg3_exe }; - let Some(bg3_exe) = Path::new(&bg3_exe).file_name() else { - fatal_popup( - "No direct launch", - "This autostart program is not a launcher. Please check instructions for how to use it. (file_name() missing)", - ); - }; - - let exes = get_game_binary_paths(init.config); - - let bg3_path = match &*bg3_exe.to_string_lossy() { - "bg3.exe" => exes.bg3, - "bg3_dx11.exe" => exes.bg3_dx11, + let Some(bg3_path) = get_game_binary_for(Path::new(&bg3_exe), init.config) else { // it's not a bg3 executable; or at least, it's not named correctly - exe => fatal_popup( + fatal_popup( "No direct launch", - format!("This autostart program is not a launcher. Please check instructions for how to use it. (The target - {exe} - has an incorrect filename)"), + format!("This autostart program is not a launcher. Please check instructions for how to use it. (The target - {bg3_exe} - has an incorrect filename)"), ) }; trace!(game = ?bg3_exe, ?args, "launching"); trace!(env = ?env::vars()); - let child = match Command::new(bg3_path) + let cmd = Command::new(bg3_path) .args(args) // bypass IFEO on this launch .creation_flags(DEBUG_PROCESS.0 | DEBUG_ONLY_THIS_PROCESS.0) .envs(env::vars()) - .spawn() - { + .spawn(); + + let child = match cmd { Ok(v) => v, Err(e) => { fatal_popup( diff --git a/crates/yabg3nml/src/paths.rs b/crates/yabg3nml/src/paths.rs index d5e0e38..8ba3b55 100644 --- a/crates/yabg3nml/src/paths.rs +++ b/crates/yabg3nml/src/paths.rs @@ -1,7 +1,11 @@ -use std::{fs, path::Path}; +use std::{ + fs, + path::{Path, PathBuf}, +}; use shared::{config::Config, popup::fatal_popup}; use tracing::{error, trace}; +use unicase::UniCase; #[allow(dead_code)] pub struct Bg3Exes { @@ -63,3 +67,42 @@ pub fn get_game_binary_paths(config: &Config) -> Bg3Exes { "Failed to resolve `install_root` path. Does the path (or its target) exist and point to a directory? And does this program have permissions to read that path?", ); } + +#[allow(dead_code)] +pub enum Bg3Exe { + Vulkan, + Dx11, + None, +} + +impl From<&Path> for Bg3Exe { + fn from(value: &Path) -> Self { + let Some(value) = value.file_name() else { + return Self::None; + }; + + let value = UniCase::new(value.to_string_lossy()); + let vulkan = UniCase::new("bg3.exe"); + let dx11 = UniCase::new("bg3_dx11.exe"); + + match value { + v if v == vulkan => Self::Vulkan, + v if v == dx11 => Self::Dx11, + _ => Self::None, + } + } +} + +#[allow(dead_code)] +pub fn get_game_binary_for(exe: impl Into, config: &Config) -> Option { + let exe: Bg3Exe = exe.into(); + + let binaries = get_game_binary_paths(config); + let exe = match exe { + Bg3Exe::Vulkan => binaries.bg3, + Bg3Exe::Dx11 => binaries.bg3_dx11, + Bg3Exe::None => return None, + }; + + Some(exe.into()) +}