diff options
-rw-r--r-- | src-tauri/src/main.rs | 3 | ||||
-rw-r--r-- | src-tauri/src/mod_management/mod.rs | 45 | ||||
-rw-r--r-- | src-vue/src/views/ModsView.vue | 36 |
3 files changed, 82 insertions, 2 deletions
diff --git a/src-tauri/src/main.rs b/src-tauri/src/main.rs index 0d7e69af..a9483e95 100644 --- a/src-tauri/src/main.rs +++ b/src-tauri/src/main.rs @@ -23,7 +23,7 @@ use repair_and_verify::{ mod mod_management; use mod_management::{ - fc_download_mod_and_install, get_installed_mods_and_properties, set_mod_enabled_status, + fc_download_mod_and_install, get_installed_mods_and_properties, set_mod_enabled_status, delete_northstar_mod, }; mod northstar; @@ -103,6 +103,7 @@ fn main() { install_mod_caller, clean_up_download_folder_caller, get_newest_flightcore_version, + delete_northstar_mod, ]) .run(tauri::generate_context!()) .expect("error while running tauri application"); diff --git a/src-tauri/src/mod_management/mod.rs b/src-tauri/src/mod_management/mod.rs index 4fe1b517..f6897b90 100644 --- a/src-tauri/src/mod_management/mod.rs +++ b/src-tauri/src/mod_management/mod.rs @@ -10,6 +10,8 @@ use app::GameInstall; use json5; +use crate::northstar::CORE_MODS; + pub const BLACKLISTED_MODS: [&str; 3] = [ "northstar-Northstar", "northstar-NorthstarReleaseCandidate", @@ -385,3 +387,46 @@ pub async fn fc_download_mod_and_install( Ok(()) } + +/// Deletes a given Northstar mod folder +fn delete_mod_folder(ns_mod_directory: String) -> Result<(), String> { + let ns_mod_dir_path = std::path::Path::new(&ns_mod_directory); + + // Safety check: Check whether `mod.json` exists and exit early if not + // If it does not exist, we might not be dealing with a Northstar mod + let mod_json_path = ns_mod_dir_path.join("mod.json"); + if !mod_json_path.exists() { + // If it doesn't exist, return an error + return Err(format!("mod.json does not exist in {}", ns_mod_directory)); + } + + match std::fs::remove_dir_all(&ns_mod_directory) { + Ok(()) => Ok(()), + Err(err) => Err(format!("Failed deleting mod: {err}")), + } +} + +/// Deletes a Northstar mod based on its name +#[tauri::command] +pub fn delete_northstar_mod(game_install: GameInstall, nsmod_name: String) -> Result<(), String> { + // Prevent deleting core mod + for core_mod in CORE_MODS { + if nsmod_name == core_mod { + return Err(format!("Cannot remove core mod {nsmod_name}")); + } + } + + // Get installed mods + let installed_ns_mods = get_installed_mods_and_properties(game_install)?; + + // Get folder name based on northstarmods + for installed_ns_mod in installed_ns_mods { + // Installed mod matches specified mod + if installed_ns_mod.name == nsmod_name { + // Delete folder + return delete_mod_folder(installed_ns_mod.directory); + } + } + + Err(format!("Mod {nsmod_name} not found to be installed")) +} diff --git a/src-vue/src/views/ModsView.vue b/src-vue/src/views/ModsView.vue index af3b58a3..2f352ded 100644 --- a/src-vue/src/views/ModsView.vue +++ b/src-vue/src/views/ModsView.vue @@ -7,7 +7,15 @@ <el-card v-else shadow="hover" v-for="mod in installedMods" v-bind:key="mod.name"> <el-switch style="--el-switch-on-color: #13ce66; --el-switch-off-color: #8957e5" v-model="mod.enabled" :before-change="() => updateWhichModsEnabled(mod)" :loading="global_load_indicator" /> - {{mod.name}} + <el-popconfirm + title="Are you sure to delete this mod?" + @confirm="deleteMod(mod)" + > + <template #reference> + <el-button type="danger">Delete</el-button> + </template> + </el-popconfirm> + {{ mod.name }} </el-card> </div> </el-scrollbar> @@ -69,6 +77,32 @@ export default defineComponent({ this.global_load_indicator = false; return true; + }, + async deleteMod(mod: NorthstarMod) { + let game_install = { + game_path: this.$store.state.game_path, + install_type: this.$store.state.install_type + } as GameInstall; + await invoke("delete_northstar_mod", { gameInstall: game_install, nsmodName: mod.name }) + .then((message) => { + // Just a visual indicator that it worked + ElNotification({ + title: `Success deleting ${mod.name}`, + type: 'success', + position: 'bottom-right' + }); + }) + .catch((error) => { + ElNotification({ + title: 'Error', + message: error, + type: 'error', + position: 'bottom-right' + }); + }) + .finally(() => { + this.$store.commit('loadInstalledMods'); + }); } } }); |