diff --git a/src/command.rs b/src/command.rs index ccfd833..e95781b 100644 --- a/src/command.rs +++ b/src/command.rs @@ -2,18 +2,20 @@ use std::{io, process::{Command, Output}}; use tempfile::NamedTempFile; -pub struct UMUCommand { +pub struct UmuCommand { inner: Command, - file: NamedTempFile } -impl UMUCommand { - pub fn new(inner: Command, file: NamedTempFile) -> Self { +#[allow(dead_code)] +impl UmuCommand { + pub fn new(inner: Command) -> Self { Self { inner, - file } } - pub fn run(&mut self) -> io::Result { + pub fn output(&mut self) -> io::Result { self.inner.output() } + pub fn spawn(&mut self) -> Result { + self.inner.spawn() + } } \ No newline at end of file diff --git a/src/command_builder.rs b/src/command_builder.rs index aa52254..bb38ece 100644 --- a/src/command_builder.rs +++ b/src/command_builder.rs @@ -1,76 +1,64 @@ -use std::{io::Write, path::PathBuf, process::Command}; +use std::{ffi::OsStr, io::Write, path::PathBuf, process::Command}; use serde::Serialize; -use crate::command::UMUCommand; +use crate::command::UmuCommand; #[derive(Debug, Default, Clone, Serialize)] -pub struct UMUCommandBuilder { - #[serde(skip)] - executable: PathBuf, - umu: UMU -} -#[serde(rename_all = "snake_case")] -#[derive(Debug, Default, Clone, Serialize)] -struct UMU { +pub struct UmuCommandBuilder { + source: PathBuf, + + program: String, game_id: Option, wineprefix: Option, - proton_executable: Option, - winetricks_verbs: Option>, + proton_path: Option, + launch_args: Option>, store: Option, - } - -impl UMUCommandBuilder { - pub fn new>(executable: T) -> Self { +#[allow(dead_code)] +impl UmuCommandBuilder { + pub fn new, D: Into>(source: T, program: D) -> Self { Self { - executable: executable.into(), + source: source.into(), + program: program.into(), ..Default::default() } } - pub fn build(self) -> UMUCommand { - let mut file = tempfile::NamedTempFile::new().unwrap(); - let serialized = toml::to_string(&self).unwrap(); - println!("Serialized: \n{}", serialized); - file.write_all(serialized.as_bytes()).unwrap(); + pub fn build(self) -> UmuCommand { + let mut command = Command::new(self.source); - let mut command = Command::new(self.executable); - command - .arg("--config") - .arg(file.path()); + command.arg(self.program); + set_env(&mut command, "GAMEID", self.game_id); + set_env(&mut command, "PROTONPATH", self.proton_path); + set_env(&mut command, "WINEPREFIX", self.wineprefix); + //set_env(&mut command, "PROTON_VERB", self.proton_path); + set_env(&mut command, "STORE", self.store); - UMUCommand::new(command, file) + UmuCommand::new(command) } pub fn game_id(mut self, game_id: String) -> Self { - self.umu.game_id = Some(game_id); + self.game_id = Some(game_id); self } pub fn proton>(mut self, proton_executable: T) -> Self { - self.umu.proton_executable = Some(proton_executable.into()); - self - } - pub fn verbs(mut self, winetricks_verbs: Vec) -> Self { - self.umu.winetricks_verbs = Some(winetricks_verbs); + self.proton_path = Some(proton_executable.into()); self } pub fn store(mut self, store: String) -> Self { - self.umu.store = Some(store); + self.store = Some(store); self } pub fn wineprefix(mut self, wineprefix: PathBuf) -> Self { - self.umu.wineprefix = Some(wineprefix); + self.wineprefix = Some(wineprefix); + self + } + pub fn launch_args(mut self, launch_args: Vec) -> Self { + self.launch_args = Some(launch_args); self } } -fn add_arg(a: &str, b: &str) -> String { - let mut s = String::new(); - s.push_str(a); - s.push_str(b); - s -} -fn add_arg_pathbuf(a: &str, b: PathBuf) -> String { - let mut s = String::new(); - s.push_str(a); - s.push_str(&b.into_os_string().into_string().unwrap()); - s + +fn set_env, V: AsRef>(command: &mut Command, key: K, variable: Option) { + if variable.is_none() { return; } + command.env(key, variable.unwrap()); } \ No newline at end of file diff --git a/src/lib.rs b/src/lib.rs index 2bf08b7..0e2770a 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -1,3 +1,4 @@ +#[cfg(test)] mod tests; mod command; mod command_builder; \ No newline at end of file diff --git a/src/tests.rs b/src/tests.rs index 09166b4..f80ece9 100644 --- a/src/tests.rs +++ b/src/tests.rs @@ -1,10 +1,26 @@ -use crate::command_builder::UMUCommandBuilder; +use crate::command_builder::UmuCommandBuilder; #[test] pub fn print_sample() { - UMUCommandBuilder::new("/usr/bin/umu-run") + UmuCommandBuilder::new("/usr/bin/umu-run", "/home/quexeky/Downloads/Mini.Metro/setup_mini_metro_202101201241_(release-46)_(44304).exe") .game_id("0".to_owned()) .wineprefix("~/.wine".into()) .build(); } + +#[test] +pub fn local_test() { + let mut umu_command = UmuCommandBuilder::new("/usr/bin/umu-run", "/home/quexeky/Downloads/Mini.Metro/setup_mini_metro_202101201241_(release-46)_(44304).exe") + .game_id("0".into()) + .wineprefix("~/.wine".into()) + .build(); + let child = umu_command + .spawn() + .unwrap(); + + + println!("{:?}", child.wait_with_output().unwrap()); + + +} \ No newline at end of file