diff options
author | GeckoEidechse <40122905+GeckoEidechse@users.noreply.github.com> | 2023-05-13 13:25:05 +0200 |
---|---|---|
committer | GitHub <noreply@github.com> | 2023-05-13 13:25:05 +0200 |
commit | 726b7466b34a936205a32098c56b7c723db34862 (patch) | |
tree | 0d0e5a725a605dd59ea450300ef28c34be98583c /src-tauri/src | |
parent | ec0f0a92f8387f4c6d8b314536bc3acdcae56c66 (diff) | |
download | FlightCore-726b7466b34a936205a32098c56b7c723db34862.tar.gz FlightCore-726b7466b34a936205a32098c56b7c723db34862.zip |
refactor: Move `extract` function to util submod (#354)
Diffstat (limited to 'src-tauri/src')
-rw-r--r-- | src-tauri/src/main.rs | 44 | ||||
-rw-r--r-- | src-tauri/src/northstar/install.rs | 2 | ||||
-rw-r--r-- | src-tauri/src/util.rs | 43 |
3 files changed, 45 insertions, 44 deletions
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<Vec<NorthstarThunderstoreR // As this was causing issues it was moved into `main.rs` until being later moved into dedicated modules use std::{fs, path::Path}; -use anyhow::{Context, Result}; +use anyhow::Result; pub mod constants; mod platform_specific; @@ -507,7 +507,6 @@ mod platform_specific; use platform_specific::linux; use sysinfo::SystemExt; -use zip::ZipArchive; use crate::constants::TITANFALL2_STEAM_ID; @@ -570,47 +569,6 @@ pub fn check_is_valid_game_path(game_install_path: &str) -> 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(()) +} |