Skip to content

Commit

Permalink
Extract Northstar into temporary location before installing (#456)
Browse files Browse the repository at this point in the history
This is done in order to enable future changes
  • Loading branch information
Jan200101 authored Aug 2, 2023
1 parent fae4de2 commit 08d066c
Show file tree
Hide file tree
Showing 2 changed files with 70 additions and 13 deletions.
44 changes: 31 additions & 13 deletions src-tauri/src/northstar/install.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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}");
Expand Down Expand Up @@ -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
Expand Down
39 changes: 39 additions & 0 deletions src-tauri/src/util.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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(())
}

0 comments on commit 08d066c

Please sign in to comment.