1
0
Fork 0

simplified steam launch option setting

This commit is contained in:
Kim, Jimin 2022-02-01 23:45:21 +09:00
parent 71729727ec
commit 93c5daeb1e
4 changed files with 39 additions and 94 deletions

View file

@ -1,5 +1,4 @@
use super::InstallErr;
use crate::util;
use std::fs;
use std::os::unix::prelude::PermissionsExt;
@ -27,15 +26,6 @@ pub async fn unix_launch_option_setup(
}
};
//
// Skip if launch option is already set
//
if is_already_set() {
println!("Steam launch option is already set. Skipping.");
return Ok(());
}
//
// make run_bepinex.sh executable
//
@ -88,70 +78,3 @@ pub async fn unix_launch_option_setup(
println!("Prompting to set launch option.");
return Err(InstallErr::LaunchOptionNotSet);
}
fn is_already_set() -> bool {
//
// get localconfig.vdf path
//
let localconfig_path = match get_localconfig_path() {
Some(localconfig_path) => localconfig_path,
None => {
println!("Failed to locate localconfig.vdf");
return false;
}
};
//
// check if launch option is set
//
match fs::read_to_string(localconfig_path) {
Ok(_content) => {
// todo: improve logic
// 1. find line only containing "750470"
// 2. find next closest line only containing "}"
// 3. check if section has line containing both "run_bepinex.sh" and "%command%"
return true;
}
Err(err) => {
println!("Failed to read localconfig.vdf: {:#?}", err);
return false;
}
};
}
fn get_localconfig_path() -> Option<String> {
match util::get_default_steam_path() {
// using forward slash because this part of the code is only reachable in linux and macOS
Some(steam_path) => {
// get steam user ID
let paths = std::fs::read_dir(format!("{}/userdata", steam_path)).unwrap();
let mut user_path: &str = "";
let mut _user_path_buf: std::path::PathBuf;
let mut count = 0;
for path in paths {
count += 1;
_user_path_buf = path.unwrap().path();
user_path = _user_path_buf.to_str().unwrap();
}
if count > 1 {
// todo: choose steam user ID when there's multiple
println!("Failed to get steam user");
return None;
}
return Some(format!("{}/config/localconfig.vdf", user_path));
}
None => return None,
};
}

View file

@ -9,8 +9,6 @@ mod launch_options;
use types::InstallErr;
// todo: show current step in the frontend
/// automated version of the [manual installation](https://github.com/War-Brokers-Mods/WBM#installation).
///
/// This function exits if it requires a user input and is called again with the user input as its arguments.
@ -20,9 +18,14 @@ use types::InstallErr;
///
/// ## Arguments
///
/// * `game_path` - absolute path to the game folder/directory.
/// * `game_path` - Absolute path to the game folder/directory.
/// * `is_launch_option_set` - Whether if steam launch option is already set or not.
#[tauri::command]
pub async fn install(window: tauri::Window, game_path: String) -> Result<(), InstallErr> {
pub async fn install(
window: tauri::Window,
game_path: String,
is_launch_option_set: bool,
) -> Result<(), InstallErr> {
println!("install command called");
//
@ -88,9 +91,11 @@ pub async fn install(window: tauri::Window, game_path: String) -> Result<(), Ins
// Set steam launch option if OS is linux or macOS
//
match launch_options::unix_launch_option_setup(&window, game_path).await {
Ok(_) => {}
Err(err) => return Err(err),
if !is_launch_option_set {
match launch_options::unix_launch_option_setup(&window, game_path).await {
Ok(_) => {}
Err(err) => return Err(err),
}
}
//

View file

@ -44,7 +44,7 @@
<button
on:click|once={() => {
selectGamePathAndRun(install)
selectGamePathAndRun("install")
lastInstallErr = undefined
}}
>
@ -88,7 +88,7 @@
<button
on:click|once={() => {
install()
install({ isLaunchOptionSet: true })
lastInstallErr = undefined
}}
>

View file

@ -11,22 +11,31 @@ function buttonClicked() {
store.spinCog.set(true)
}
interface InstallArgs {
gamePath?: string
isLaunchOptionSet?: boolean
}
/**
* Calls the install command in the backend.
*
* @param {string} gamePath - Absolute path to the game directory in the steam library. Leave it empty to use default location.
* @param {InstallArgs} gamePath - Absolute path to the game directory in the steam library. Leave it empty to use default location.
*/
export function install(gamePath: string = "") {
export function install(args: InstallArgs = {}) {
buttonClicked()
if (!gamePath) {
// get stored gamePath if it's empty
if (!args.gamePath) {
store.gamePath.update((value) => {
gamePath = value
args.gamePath = value
return value
})
}
invoke(COMMANDS.INSTALL, { gamePath })
invoke(COMMANDS.INSTALL, {
gamePath: args.gamePath || "",
isLaunchOptionSet: args.isLaunchOptionSet || false,
})
.then(() => {
store.wasInstallSuccessful.set(true)
})
@ -67,12 +76,20 @@ export function remove(gamePath: string = "") {
* Opens a file selection dialog for the user to manually select the game directory.
* Called when the default game location was not found.
*
* @param {(gamePath?: string) => void} f - Function that will run after selecting a directory. Expected to be either the {@link install} function or the {@link remove} function.
* @param {"install" | "remove"} type - Function that will run after selecting a directory. Expected to be either the {@link install} function or the {@link remove} function.
*/
export function selectGamePathAndRun(f: (gamePath?: string) => void) {
export function selectGamePathAndRun(type: "install" | "remove") {
dialogOpen({ directory: true, multiple: false }).then((value: string) => {
store.gamePath.set(value)
f(value)
switch (type) {
case "install": {
install({ gamePath: value })
}
case "remove": {
remove(value)
}
}
})
}