From e3c68e1f322b8bbf8b39ecc3b39b46baf59f6ca7 Mon Sep 17 00:00:00 2001 From: GeckoEidechse Date: Thu, 20 Jul 2023 00:49:14 +0200 Subject: fix: Typo in Cargo.toml --- src-tauri/Cargo.toml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src-tauri/Cargo.toml b/src-tauri/Cargo.toml index 1c758eb3..261e0b9d 100644 --- a/src-tauri/Cargo.toml +++ b/src-tauri/Cargo.toml @@ -33,7 +33,7 @@ steamlocate = "1.2" # Error messages anyhow = "1.0" # libthermite for Northstar/mod install handling -libthermite = { version = ""0.7.0-alpha", features = ["proton"] } +libthermite = { version = "0.7.0-alpha", features = ["proton"] } # zip stuff zip = "0.6.2" # Regex -- cgit v1.2.3 From 8ae71ef5f04cab43b19b2b17a26012eeffa62566 Mon Sep 17 00:00:00 2001 From: GeckoEidechse Date: Thu, 20 Jul 2023 01:04:26 +0200 Subject: feat: Delete old version of package on update also deletes old version of legacy package if installed --- src-tauri/src/mod_management/legacy.rs | 36 ++++++++++++++++++ src-tauri/src/mod_management/mod.rs | 69 ++++++++++++++++++++++++++++++++++ 2 files changed, 105 insertions(+) diff --git a/src-tauri/src/mod_management/legacy.rs b/src-tauri/src/mod_management/legacy.rs index f24f44b6..3136a618 100644 --- a/src-tauri/src/mod_management/legacy.rs +++ b/src-tauri/src/mod_management/legacy.rs @@ -115,6 +115,42 @@ pub fn parse_installed_mods( Ok(mods) } +/// Deletes all legacy packages that match in author and mod name +/// regardless of version +pub fn delete_legacy_package_install( + thunderstore_mod_string: &str, + game_install: &GameInstall, +) -> Result<(), String> { + let thunderstore_mod_string: ParsedThunderstoreModString = + thunderstore_mod_string.parse().unwrap(); + let found_installed_legacy_mods = match parse_installed_mods(game_install) { + Ok(res) => res, + Err(err) => return Err(err.to_string()), + }; + + for legacy_mod in found_installed_legacy_mods { + if legacy_mod.thunderstore_mod_string.is_none() { + continue; // Not a thunderstore mod + } + + let current_mod_ts_string: ParsedThunderstoreModString = legacy_mod + .clone() + .thunderstore_mod_string + .unwrap() + .parse() + .unwrap(); + + if thunderstore_mod_string.author_name == current_mod_ts_string.author_name + && thunderstore_mod_string.mod_name == current_mod_ts_string.mod_name + { + // They match, delete + delete_mod_folder(&legacy_mod.directory)?; + } + } + + Ok(()) +} + /// Deletes all NorthstarMods related to a Thunderstore mod pub fn delete_thunderstore_mod( game_install: GameInstall, diff --git a/src-tauri/src/mod_management/mod.rs b/src-tauri/src/mod_management/mod.rs index b11181c2..afed9692 100644 --- a/src-tauri/src/mod_management/mod.rs +++ b/src-tauri/src/mod_management/mod.rs @@ -407,6 +407,58 @@ async fn get_mod_dependencies(thunderstore_mod_string: &str) -> Result::new()) } +/// Deletes all versions of Thunderstore package except the specified one +fn delete_older_versions( + thunderstore_mod_string: &str, + game_install: &GameInstall, +) -> Result<(), String> { + let thunderstore_mod_string: ParsedThunderstoreModString = + thunderstore_mod_string.parse().unwrap(); + log::info!( + "Deleting other versions of {}", + thunderstore_mod_string.to_string() + ); + let packages_folder = format!("{}/R2Northstar/packages", game_install.game_path); + + // Get folders in packages dir + let paths = match std::fs::read_dir(&packages_folder) { + Ok(paths) => paths, + Err(_err) => return Err(format!("Failed to read directory {}", &packages_folder)), + }; + + let mut directories: Vec = Vec::new(); + + // Get list of folders in `mods` directory + for path in paths { + let my_path = path.unwrap().path(); + + let md = std::fs::metadata(my_path.clone()).unwrap(); + if md.is_dir() { + directories.push(my_path); + } + } + + for directory in directories { + let folder_name = directory.file_name().unwrap().to_str().unwrap(); + let ts_mod_string_from_folder: ParsedThunderstoreModString = match folder_name.parse() { + Ok(res) => res, + Err(err) => { + log::warn!("{err}"); + continue; + } + }; + // Check which match `AUTHOR-MOD` and do NOT match `AUTHOR-MOD-VERSION` + if ts_mod_string_from_folder.author_name == thunderstore_mod_string.author_name + && ts_mod_string_from_folder.mod_name == thunderstore_mod_string.mod_name + && ts_mod_string_from_folder.version != thunderstore_mod_string.version + { + delete_package_folder(&directory.display().to_string())?; + } + } + + Ok(()) +} + // Copied from `libtermite` source code and modified // Should be replaced with a library call to libthermite in the future /// Download and install mod to the specified target. @@ -503,6 +555,23 @@ pub async fn fc_download_mod_and_install( } }; + // Successful package install + match legacy::delete_legacy_package_install(thunderstore_mod_string, game_install) { + Ok(()) => (), + Err(err) => { + // Catch error but ignore + log::warn!("Failed deleting legacy versions due to: {}", err); + } + }; + + match delete_older_versions(thunderstore_mod_string, game_install) { + Ok(()) => (), + Err(err) => { + // Catch error but ignore + log::warn!("Failed deleting older versions due to: {}", err); + } + }; + Ok(()) } -- cgit v1.2.3 From d335e7483dfd1959945ee06038c02877a349ef66 Mon Sep 17 00:00:00 2001 From: GeckoEidechse Date: Thu, 20 Jul 2023 02:21:25 +0200 Subject: feat: Add sanity check for package install --- src-tauri/src/mod_management/mod.rs | 47 +++++++++++++++++++++++++++++++++++-- 1 file changed, 45 insertions(+), 2 deletions(-) diff --git a/src-tauri/src/mod_management/mod.rs b/src-tauri/src/mod_management/mod.rs index afed9692..cef7ff71 100644 --- a/src-tauri/src/mod_management/mod.rs +++ b/src-tauri/src/mod_management/mod.rs @@ -2,6 +2,7 @@ use crate::constants::{BLACKLISTED_MODS, CORE_MODS}; use async_recursion::async_recursion; +use thermite::prelude::ThermiteError; use crate::NorthstarMod; use anyhow::{anyhow, Result}; @@ -459,6 +460,41 @@ fn delete_older_versions( Ok(()) } +/// Checks whether some mod is correctly formatted +/// Currently checks whether +/// - Some `mod.json` exists under `mods/*/mod.json` +fn fc_sanity_check(input: &&fs::File) -> bool { + let mut archive = match zip::read::ZipArchive::new(*input) { + Ok(archive) => archive, + Err(_) => return false, + }; + + let mut has_mods = false; + let mut mod_json_exists = false; + + // Checks for `mods/*/mod.json` + for i in 0..archive.len() { + let file = match archive.by_index(i) { + Ok(file) => file, + Err(_) => continue, + }; + let file_path = file.mangled_name(); + if file_path.starts_with("mods/") { + has_mods = true; + if let Some(name) = file_path.file_name() { + if name == "mod.json" { + let parent_path = file_path.parent().unwrap(); + if parent_path.parent().unwrap().to_str().unwrap() == "mods" { + mod_json_exists = true; + } + } + } + } + } + + has_mods && mod_json_exists +} + // Copied from `libtermite` source code and modified // Should be replaced with a library call to libthermite in the future /// Download and install mod to the specified target. @@ -544,14 +580,21 @@ pub async fn fc_download_mod_and_install( ); // Extract the mod to the mods directory - match thermite::core::manage::install_mod( + match thermite::core::manage::install_with_sanity( temp_file.file(), std::path::Path::new(&install_directory), + fc_sanity_check, ) { Ok(_) => (), Err(err) => { log::warn!("libthermite couldn't install mod {thunderstore_mod_string} due to {err:?}",); - return Err(err.to_string()); + return match err { + ThermiteError::SanityError => Err( + "Mod failed sanity check during install. It's probably not correctly formatted" + .to_string(), + ), + _ => Err(err.to_string()), + }; } }; -- cgit v1.2.3