aboutsummaryrefslogtreecommitdiff
path: root/src-tauri/src/mod_management
diff options
context:
space:
mode:
authorGeckoEidechse <gecko.eidechse+git@pm.me>2023-07-18 17:50:41 +0200
committerGeckoEidechse <gecko.eidechse+git@pm.me>2023-07-18 17:50:41 +0200
commit9f9c5559cb9d29656e16a01082ad5617bb0a41e0 (patch)
tree6d900b343dfe85db726e76215227a137ea016f78 /src-tauri/src/mod_management
parent5fc6669845c4b39c7c1da0af45b6c94c6f858393 (diff)
downloadFlightCore-9f9c5559cb9d29656e16a01082ad5617bb0a41e0.tar.gz
FlightCore-9f9c5559cb9d29656e16a01082ad5617bb0a41e0.zip
feat: Support deleting older versions of package
on update
Diffstat (limited to 'src-tauri/src/mod_management')
-rw-r--r--src-tauri/src/mod_management/mod.rs63
1 files changed, 62 insertions, 1 deletions
diff --git a/src-tauri/src/mod_management/mod.rs b/src-tauri/src/mod_management/mod.rs
index c9b7ae0c..14411acc 100644
--- a/src-tauri/src/mod_management/mod.rs
+++ b/src-tauri/src/mod_management/mod.rs
@@ -407,8 +407,60 @@ async fn get_mod_dependencies(thunderstore_mod_string: &str) -> Result<Vec<Strin
Ok(Vec::<String>::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<PathBuf> = 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
+// Should be replaced with a library call to >libthermite in the future
/// Download and install mod to the specified target.
#[async_recursion]
pub async fn fc_download_mod_and_install(
@@ -503,6 +555,15 @@ pub async fn fc_download_mod_and_install(
}
};
+ // Succesful package install
+ match delete_older_versions(thunderstore_mod_string, game_install) {
+ Ok(()) => (),
+ Err(err) => {
+ log::warn!("Failed deleting older versions due to: {}", err);
+ todo!(); // should we do something on error or ignore?
+ }
+ }
+
Ok(())
}