diff options
author | Jan <sentrycraft123@gmail.com> | 2023-08-02 12:24:57 +0200 |
---|---|---|
committer | GitHub <noreply@github.com> | 2023-08-02 12:24:57 +0200 |
commit | 08d066c7c6c7b99076efb5217474cc5b72df015f (patch) | |
tree | 5bb5e2e16c81e6ac01782a6c10d06622dcec8c13 /src-tauri | |
parent | fae4de2fba580e4a1a92b853c479b61e856a3a42 (diff) | |
download | FlightCore-08d066c7c6c7b99076efb5217474cc5b72df015f.tar.gz FlightCore-08d066c7c6c7b99076efb5217474cc5b72df015f.zip |
Extract Northstar into temporary location before installing (#456)
This is done in order to enable future changes
Diffstat (limited to 'src-tauri')
-rw-r--r-- | src-tauri/src/northstar/install.rs | 44 | ||||
-rw-r--r-- | src-tauri/src/util.rs | 39 |
2 files changed, 70 insertions, 13 deletions
diff --git a/src-tauri/src/northstar/install.rs b/src-tauri/src/northstar/install.rs index eef6c148..80425c56 100644 --- a/src-tauri/src/northstar/install.rs +++ b/src-tauri/src/northstar/install.rs @@ -5,7 +5,10 @@ use std::{cell::RefCell, time::Instant}; use ts_rs::TS; use crate::constants::TITANFALL2_STEAM_ID; -use crate::{util::extract, GameInstall, InstallType}; +use crate::{ + util::{extract, move_dir_all}, + GameInstall, InstallType, +}; #[cfg(target_os = "windows")] use crate::platform_specific::windows; @@ -36,16 +39,13 @@ async fn do_install( game_install: GameInstall, ) -> Result<()> { let filename = format!("northstar-{}.zip", nmod.version); - let download_directory = format!( - "{}/___flightcore-temp/download-dir/", - game_install.game_path - ); - - log::info!( - "Attempting to create temporary directory {}", - download_directory - ); + let temp_dir = format!("{}/___flightcore-temp", game_install.game_path); + let download_directory = format!("{}/download-dir", temp_dir); + let extract_directory = format!("{}/extract-dir", temp_dir); + + log::info!("Attempting to create temporary directory {}", temp_dir); std::fs::create_dir_all(download_directory.clone())?; + std::fs::create_dir_all(extract_directory.clone())?; let download_path = format!("{}/{}", download_directory, filename); log::info!("Download path: {download_path}"); @@ -94,11 +94,29 @@ async fn do_install( .unwrap(); log::info!("Extracting Northstar..."); - extract(nfile, std::path::Path::new(&game_install.game_path))?; + extract(nfile, std::path::Path::new(&extract_directory))?; + + log::info!("Installing Northstar..."); + + for entry in std::fs::read_dir(extract_directory).unwrap() { + let entry = entry.unwrap(); + let destination = format!( + "{}/{}", + game_install.game_path, + entry.path().file_name().unwrap().to_str().unwrap() + ); + + log::info!("Installing {}", entry.path().display()); + if !entry.file_type().unwrap().is_dir() { + std::fs::rename(entry.path(), destination)?; + } else { + move_dir_all(entry.path(), destination)?; + } + } // Delete old copy - log::info!("Delete temp folder again"); - std::fs::remove_dir_all(download_directory).unwrap(); + log::info!("Delete temporary directory"); + std::fs::remove_dir_all(temp_dir).unwrap(); log::info!("Done installing Northstar!"); window diff --git a/src-tauri/src/util.rs b/src-tauri/src/util.rs index f1ba6b8a..b21b2208 100644 --- a/src-tauri/src/util.rs +++ b/src-tauri/src/util.rs @@ -143,3 +143,42 @@ pub fn check_northstar_running() -> bool { || s.processes_by_name("Titanfall2.exe").next().is_some(); x } + +/// Copies a folder and all its contents to a new location +#[allow(dead_code)] +pub fn copy_dir_all( + src: impl AsRef<std::path::Path>, + dst: impl AsRef<std::path::Path>, +) -> std::io::Result<()> { + std::fs::create_dir_all(&dst)?; + for entry in std::fs::read_dir(src)? { + let entry = entry?; + let ty = entry.file_type()?; + if ty.is_dir() { + copy_dir_all(entry.path(), dst.as_ref().join(entry.file_name()))?; + } else { + std::fs::copy(entry.path(), dst.as_ref().join(entry.file_name()))?; + } + } + Ok(()) +} + +/// Moves a folders file structure to a new location +/// Old folders are not removed +pub fn move_dir_all( + src: impl AsRef<std::path::Path>, + dst: impl AsRef<std::path::Path>, +) -> std::io::Result<()> { + std::fs::create_dir_all(&dst)?; + for entry in std::fs::read_dir(src)? { + let entry = entry?; + let ty = entry.file_type()?; + if ty.is_dir() { + move_dir_all(entry.path(), dst.as_ref().join(entry.file_name()))?; + std::fs::remove_dir(entry.path())?; + } else { + std::fs::rename(entry.path(), dst.as_ref().join(entry.file_name()))?; + } + } + Ok(()) +} |