feat: Switched to using environment variables for config

Signed-off-by: quexeky <git@quexeky.dev>
This commit is contained in:
quexeky
2024-12-30 12:23:51 +11:00
parent 02cd7a6e7a
commit 0ebec8a136
4 changed files with 61 additions and 54 deletions

View File

@@ -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<Output> {
pub fn output(&mut self) -> io::Result<Output> {
self.inner.output()
}
pub fn spawn(&mut self) -> Result<std::process::Child, io::Error> {
self.inner.spawn()
}
}

View File

@@ -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<String>,
wineprefix: Option<PathBuf>,
proton_executable: Option<PathBuf>,
winetricks_verbs: Option<Vec<String>>,
proton_path: Option<PathBuf>,
launch_args: Option<Vec<String>>,
store: Option<String>,
}
impl UMUCommandBuilder {
pub fn new<T: Into<PathBuf>>(executable: T) -> Self {
#[allow(dead_code)]
impl UmuCommandBuilder {
pub fn new<T: Into<PathBuf>, D: Into<String>>(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<T: Into<PathBuf>>(mut self, proton_executable: T) -> Self {
self.umu.proton_executable = Some(proton_executable.into());
self
}
pub fn verbs(mut self, winetricks_verbs: Vec<String>) -> 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<String>) -> 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<K: AsRef<OsStr>, V: AsRef<OsStr>>(command: &mut Command, key: K, variable: Option<V>) {
if variable.is_none() { return; }
command.env(key, variable.unwrap());
}

View File

@@ -1,3 +1,4 @@
#[cfg(test)]
mod tests;
mod command;
mod command_builder;

View File

@@ -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());
}