From 8d5a7eba98fc01999d54ee89d1455ed024801359 Mon Sep 17 00:00:00 2001 From: GeckoEidechse Date: Fri, 9 Sep 2022 23:03:39 +0200 Subject: Add initial Northstar install support still Steam version only --- src-tauri/src/lib.rs | 87 +++++++++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 86 insertions(+), 1 deletion(-) (limited to 'src-tauri/src/lib.rs') diff --git a/src-tauri/src/lib.rs b/src-tauri/src/lib.rs index 3542cab5..cf1213d1 100644 --- a/src-tauri/src/lib.rs +++ b/src-tauri/src/lib.rs @@ -1,4 +1,5 @@ -use anyhow::anyhow; +use anyhow::{anyhow, Context, Result}; +use zip::ZipArchive; #[derive(Debug)] pub enum InstallType { @@ -103,3 +104,87 @@ pub fn check_is_valid_game_path(game_install_path: &str) -> Result<(), anyhow::E } 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('/') { + println!("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)?; + + println!("Write file {}", out.display()); + + std::io::copy(&mut f, &mut outfile).context("Unable to write to file")?; + } + } + + Ok(()) +} + +/// Copied from `papa` source code and modified +///Install N* from the provided mod +/// +///Checks cache, else downloads the latest version +async fn do_install(nmod: &thermite::model::Mod, game_path: &std::path::Path) -> Result<()> { + let filename = format!("northstar-{}.zip", nmod.version); + let download_directory = format!("{}/flightcore-temp-download-dir/", game_path.display()); + + std::fs::create_dir_all(download_directory.clone())?; + + + let download_path = format!("{}/{}", download_directory, filename); + println!("{}", download_path); + + let nfile = thermite::core::actions::download_file( + &nmod.url, + download_path, + ) + .await + .unwrap(); + + println!("Extracting Northstar..."); + extract(nfile, game_path)?; + println!("Done!"); + + Ok(()) +} + +pub async fn install_northstar(game_path: &str) -> Result { + let index = thermite::api::get_package_index().await.unwrap().to_vec(); + let nmod = index + .iter() + .find(|f| f.name.to_lowercase() == "northstar") + .ok_or_else(|| panic!("Couldn't find Northstar on thunderstore???")) + .unwrap(); + + do_install(nmod, std::path::Path::new(game_path)) + .await + .unwrap(); + + Ok(nmod.version.clone()) +} -- cgit v1.2.3