1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142
use crate::util::error::STError; use std::io::Write; use std::{ env, fs, io, path::{Path, PathBuf}, }; fn touch(path: &Path) -> io::Result<()> { match fs::OpenOptions::new().create(true).write(true).open(path) { Ok(_) => Ok(()), Err(e) => Err(e), } } pub fn config_directory() -> Result<PathBuf, STError> { let dir = match env::var("STEAM_TUI_DIR") { Ok(dir) => dir, _ => "~/.config/steam-tui".to_string(), }; let dir = shellexpand::full(&dir)?.to_string(); let dir = Path::new(&dir); fs::create_dir_all(dir)?; Ok(dir.to_path_buf()) } pub fn steam_directory() -> Result<PathBuf, STError> { let dir = match env::var("STEAM_APP_DIR") { Ok(dir) => dir, _ => "~/.steam/steam/steamapps/common/".to_string(), }; let dir = shellexpand::full(&dir)?.to_string(); let dir = Path::new(&dir); fs::create_dir_all(dir)?; Ok(dir.to_path_buf()) } pub fn steam_run_wrapper() -> Result<PathBuf, STError> { let run = match env::var("STEAM_RUN_WRAPPER") { Ok(run) => run, _ => "~/.steam/bin32/steam-runtime/run.sh".to_string(), }; let run = shellexpand::full(&run)?.to_string(); let run = Path::new(&run); if run.exists() { Ok(run.to_path_buf()) } else { Err(STError::Problem(format!( "Run wrapper doesn't exist: {:?}", run ))) } } pub fn executable_join(executable: &str, installdir: &str) -> Result<PathBuf, STError> { let installdir = Path::new(installdir); let executable = Path::new(executable); let script_path = installdir.join(executable); Ok(script_path) } pub fn image_exists(id: i32) -> Result<PathBuf, STError> { let dir = config_directory()?; let image = &format!("{}.ico", id); let image = Path::new(image); let image = dir.join(image); if image.exists() { Ok(image) } else { Err(STError::Problem(format!( "Image doesn't exist: {:?}", image ))) } } pub fn executable_exists(executable: &str) -> Result<PathBuf, STError> { let dir = steam_directory()?; let executable = Path::new(executable); let script_path = dir.join(executable); if script_path.exists() { Ok(script_path) } else { Err(STError::Problem("Executable doesn't exist".to_string())) } } fn script_location(file: &Path, contents: &str) -> Result<PathBuf, STError> { let dir = config_directory()?; let script_path = dir.join(file); let mut f = fs::File::create(&script_path)?; f.write_all(contents.as_bytes())?; Ok(script_path) } pub fn install_script_location(login: String, id: i32) -> Result<PathBuf, STError> { let file = &format!("{}.install", id); let file = Path::new(file); let contents = format!( r#" login {} app_update "{}" -validate quit "#, login, id ); script_location(file, &contents) } pub fn launch_script_location(login: String, id: i32) -> Result<PathBuf, STError> { let file = &format!("{}.launch", id); let file = Path::new(file); let contents = format!( r#" login {} app_update "{}" -validate app_run {} quit "#, login, id, id ); script_location(file, &contents) } pub fn config_location() -> Result<PathBuf, STError> { let dir = config_directory()?; let config_path = Path::new("config.json"); let config_path = dir.join(config_path); touch(&config_path)?; Ok(config_path) } pub fn cache_location() -> Result<PathBuf, STError> { let dir = config_directory()?; let config_path = Path::new("games.json"); let config_path = dir.join(config_path); touch(&config_path)?; Ok(config_path) }