From 726b7466b34a936205a32098c56b7c723db34862 Mon Sep 17 00:00:00 2001 From: GeckoEidechse <40122905+GeckoEidechse@users.noreply.github.com> Date: Sat, 13 May 2023 13:25:05 +0200 Subject: refactor: Move `extract` function to util submod (#354) --- src-tauri/src/main.rs | 44 +------------------------------------- src-tauri/src/northstar/install.rs | 2 +- src-tauri/src/util.rs | 43 +++++++++++++++++++++++++++++++++++++ 3 files changed, 45 insertions(+), 44 deletions(-) (limited to 'src-tauri/src') diff --git a/src-tauri/src/main.rs b/src-tauri/src/main.rs index 7cff8f33..ef041024 100644 --- a/src-tauri/src/main.rs +++ b/src-tauri/src/main.rs @@ -498,7 +498,7 @@ async fn get_available_northstar_versions() -> Result Result<(), String> { Ok(()) } -/// Copied from `papa` source code and modified -///Extract N* zip file to target game path -// fn extract(ctx: &Ctx, zip_file: File, target: &Path) -> Result<()> { -fn extract(zip_file: std::fs::File, target: &std::path::Path) -> Result<()> { - let mut archive = ZipArchive::new(&zip_file).context("Unable to open zip archive")?; - for i in 0..archive.len() { - let mut f = archive.by_index(i).unwrap(); - - //This should work fine for N* because the dir structure *should* always be the same - if f.enclosed_name().unwrap().starts_with("Northstar") { - let out = target.join( - f.enclosed_name() - .unwrap() - .strip_prefix("Northstar") - .unwrap(), - ); - - if (*f.name()).ends_with('/') { - log::info!("Create directory {}", f.name()); - std::fs::create_dir_all(target.join(f.name())) - .context("Unable to create directory")?; - continue; - } else if let Some(p) = out.parent() { - std::fs::create_dir_all(p).context("Unable to create directory")?; - } - - let mut outfile = std::fs::OpenOptions::new() - .create(true) - .write(true) - .truncate(true) - .open(&out)?; - - log::info!("Write file {}", out.display()); - - std::io::copy(&mut f, &mut outfile).context("Unable to write to file")?; - } - } - - Ok(()) -} - /// Returns identifier of host OS FlightCore is running on pub fn get_host_os() -> String { env::consts::OS.to_string() diff --git a/src-tauri/src/northstar/install.rs b/src-tauri/src/northstar/install.rs index 94029350..c01148a1 100644 --- a/src-tauri/src/northstar/install.rs +++ b/src-tauri/src/northstar/install.rs @@ -5,7 +5,7 @@ use std::{cell::RefCell, time::Instant}; use ts_rs::TS; use crate::constants::TITANFALL2_STEAM_ID; -use crate::{extract, GameInstall, InstallType}; +use crate::{util::extract, GameInstall, InstallType}; #[cfg(target_os = "windows")] use crate::platform_specific::windows; diff --git a/src-tauri/src/util.rs b/src-tauri/src/util.rs index 0802d68c..93844af5 100644 --- a/src-tauri/src/util.rs +++ b/src-tauri/src/util.rs @@ -1,6 +1,8 @@ //! This module contains various utility/helper functions that do not fit into any other module +use anyhow::{Context, Result}; use serde::{Deserialize, Serialize}; +use zip::ZipArchive; use crate::constants::{APP_USER_AGENT, MASTER_SERVER_URL, SERVER_BROWSER_ENDPOINT}; @@ -60,3 +62,44 @@ pub async fn get_server_player_count() -> Result<(i32, usize), String> { Ok((total_player_count, server_count)) } + +/// Copied from `papa` source code and modified +///Extract N* zip file to target game path +// fn extract(ctx: &Ctx, zip_file: File, target: &Path) -> Result<()> { +pub fn extract(zip_file: std::fs::File, target: &std::path::Path) -> Result<()> { + let mut archive = ZipArchive::new(&zip_file).context("Unable to open zip archive")?; + for i in 0..archive.len() { + let mut f = archive.by_index(i).unwrap(); + + //This should work fine for N* because the dir structure *should* always be the same + if f.enclosed_name().unwrap().starts_with("Northstar") { + let out = target.join( + f.enclosed_name() + .unwrap() + .strip_prefix("Northstar") + .unwrap(), + ); + + if (*f.name()).ends_with('/') { + log::info!("Create directory {}", f.name()); + std::fs::create_dir_all(target.join(f.name())) + .context("Unable to create directory")?; + continue; + } else if let Some(p) = out.parent() { + std::fs::create_dir_all(p).context("Unable to create directory")?; + } + + let mut outfile = std::fs::OpenOptions::new() + .create(true) + .write(true) + .truncate(true) + .open(&out)?; + + log::info!("Write file {}", out.display()); + + std::io::copy(&mut f, &mut outfile).context("Unable to write to file")?; + } + } + + Ok(()) +} -- cgit v1.2.3