aboutsummaryrefslogtreecommitdiff
path: root/src-tauri/src/lib.rs
diff options
context:
space:
mode:
authorGeckoEidechse <gecko.eidechse+git@pm.me>2022-09-09 23:03:39 +0200
committerGeckoEidechse <gecko.eidechse+git@pm.me>2022-09-09 23:03:39 +0200
commit8d5a7eba98fc01999d54ee89d1455ed024801359 (patch)
treecb870c29da8cf5a181c52d013863f7525a6452bd /src-tauri/src/lib.rs
parent4db6b3d79b7289717a16603c527bc0d5e3ea0594 (diff)
downloadFlightCore-8d5a7eba98fc01999d54ee89d1455ed024801359.tar.gz
FlightCore-8d5a7eba98fc01999d54ee89d1455ed024801359.zip
Add initial Northstar install support
still Steam version only
Diffstat (limited to 'src-tauri/src/lib.rs')
-rw-r--r--src-tauri/src/lib.rs87
1 files changed, 86 insertions, 1 deletions
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<String> {
+ 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())
+}