From bf235d13afbdabe61f6e0e278c1cad6a64739282 Mon Sep 17 00:00:00 2001 From: Jan200101 Date: Sat, 13 Jul 2024 21:20:18 +0200 Subject: [PATCH] Use temporary sourcemod directory when installing Implements #25 --- src/ctx.rs | 23 +++++++++++++++++++++-- src/lib.rs | 5 +++++ src/workflows/install.rs | 2 +- 3 files changed, 27 insertions(+), 3 deletions(-) diff --git a/src/ctx.rs b/src/ctx.rs index 0056547..8cac592 100644 --- a/src/ctx.rs +++ b/src/ctx.rs @@ -77,6 +77,14 @@ impl RunnerContext helper::join_path(self.sourcemod_path.clone(), crate::STAGING_DIR.to_string()) } + /// Get temporary location on the same drive as sourcemods. + /// {sourcemod_dir}{crate::TEMP_SOURCEMOD_DIR} + /// e.g; /home/kate/.var/app/com.valvesoftware.Steam/.local/share/Steam/steamapps/sourcemods/.tmp-butler + /// C:\Games\Steam\steamapps\sourcemods\.tmp-butler + pub fn get_temp_sourcemod_location(&mut self) -> String { + helper::join_path(self.sourcemod_path.clone(), crate::TEMP_SOURCEMOD_DIR.to_string()) + } + /// Get the latest item in `remote_version_list` pub fn latest_remote_version(&mut self) -> (usize, RemoteVersion) { @@ -185,10 +193,21 @@ impl RunnerContext /// Download package with Progress Bar. /// Ok is the location to where it was downloaded to. - pub async fn download_package(version: RemoteVersion) -> Result + pub async fn download_package(&mut self, version: RemoteVersion) -> Result { let av = crate::appvar::parse(); - let mut out_loc = helper::get_tmp_dir(); + let mut out_loc = self.get_temp_sourcemod_location(); + + // Remove leftovers from partial installations + // We don't really care if this succeeds so we discard the result + let _ = std::fs::remove_dir_all(&out_loc); + + if !helper::dir_exists(out_loc.clone()) { + if let Err(e) = std::fs::create_dir(&out_loc) { + trace!("[RunnerContext::download_package] {:#?}", e); + panic!("failed to make tmp directory at {} ({:})", out_loc, e); + } + } if let Some(size) = version.pre_sz { if helper::has_free_space(out_loc.clone(), size)? == false { diff --git a/src/lib.rs b/src/lib.rs index 77e85ae..14fab3c 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -52,6 +52,11 @@ pub const STAGING_DIR: &str = "/butler-staging"; #[cfg(target_os = "windows")] pub const STAGING_DIR: &str = "\\butler-staging"; +#[cfg(not(target_os = "windows"))] +pub const TEMP_SOURCEMOD_DIR: &str = "/.tmp-butler"; +#[cfg(target_os = "windows")] +pub const TEMP_SOURCEMOD_DIR: &str = "\\.tmp-butler"; + #[cfg(target_os = "windows")] flate!(pub static BUTLER_BINARY: [u8] from "Binaries/butler.exe"); #[cfg(not(target_os = "windows"))] diff --git a/src/workflows/install.rs b/src/workflows/install.rs index adf030b..f7a6906 100644 --- a/src/workflows/install.rs +++ b/src/workflows/install.rs @@ -86,7 +86,7 @@ impl InstallWorkflow { } println!("{:=>60}\nInstalling version {} to {}\n{0:=>60}", "=", version_id, &ctx.sourcemod_path); - let presz_loc = RunnerContext::download_package(version).await?; + let presz_loc = ctx.download_package(version).await?; Self::install_from(presz_loc.clone(), ctx.sourcemod_path.clone(), Some(version_id)).await?; if helper::file_exists(presz_loc.clone()) { std::fs::remove_file(presz_loc)?;