Skip to content

Commit

Permalink
Offload some direct code
Browse files Browse the repository at this point in the history
  • Loading branch information
MolotovCherry committed Nov 17, 2024
1 parent 677fb3a commit 6d0e727
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 19 deletions.
26 changes: 8 additions & 18 deletions crates/yabg3nml/src/autostart.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
};

Expand Down Expand Up @@ -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(
Expand Down
45 changes: 44 additions & 1 deletion crates/yabg3nml/src/paths.rs
Original file line number Diff line number Diff line change
@@ -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 {
Expand Down Expand Up @@ -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<Bg3Exe>, config: &Config) -> Option<PathBuf> {
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())
}

0 comments on commit 6d0e727

Please sign in to comment.