Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: Move code for legacy TS mod deletion to separate file #424

Merged
merged 1 commit into from
Jul 17, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
61 changes: 61 additions & 0 deletions src-tauri/src/mod_management/legacy.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
use crate::constants::BLACKLISTED_MODS;
use crate::mod_management::{
delete_mod_folder, get_installed_mods_and_properties, ParsedThunderstoreModString,
};
use crate::GameInstall;
use crate::NorthstarMod;
use anyhow::{anyhow, Result};
Expand Down Expand Up @@ -110,3 +114,60 @@ pub fn parse_installed_mods(
// Return found mod names
Ok(mods)
}

/// Deletes all NorthstarMods related to a Thunderstore mod
pub fn delete_thunderstore_mod(
game_install: GameInstall,
thunderstore_mod_string: String,
) -> Result<(), String> {
// Prevent deleting core mod
for core_ts_mod in BLACKLISTED_MODS {
if thunderstore_mod_string == core_ts_mod {
return Err(format!("Cannot remove core mod {thunderstore_mod_string}"));
}
}

let parsed_ts_mod_string: ParsedThunderstoreModString =
thunderstore_mod_string.parse().unwrap();

// Get installed mods
let installed_ns_mods = get_installed_mods_and_properties(game_install)?;

// List of mod folders to remove
let mut mod_folders_to_remove: Vec<String> = Vec::new();

// Get folder name based on Thundestore mod string
for installed_ns_mod in installed_ns_mods {
if installed_ns_mod.thunderstore_mod_string.is_none() {
// Not a Thunderstore mod
continue;
}

let installed_ns_mod_ts_string: ParsedThunderstoreModString = installed_ns_mod
.thunderstore_mod_string
.unwrap()
.parse()
.unwrap();

// Installed mod matches specified Thunderstore mod string
if parsed_ts_mod_string.author_name == installed_ns_mod_ts_string.author_name
&& parsed_ts_mod_string.mod_name == installed_ns_mod_ts_string.mod_name
{
// Add folder to list of folder to remove
mod_folders_to_remove.push(installed_ns_mod.directory);
}
}

if mod_folders_to_remove.is_empty() {
return Err(format!(
"No mods removed as no Northstar mods matching {thunderstore_mod_string} were found to be installed."
));
}

// Delete given folders
for mod_folder in mod_folders_to_remove {
delete_mod_folder(&mod_folder)?;
}

Ok(())
}
51 changes: 1 addition & 50 deletions src-tauri/src/mod_management/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -418,54 +418,5 @@ pub fn delete_thunderstore_mod(
game_install: GameInstall,
thunderstore_mod_string: String,
) -> Result<(), String> {
// Prevent deleting core mod
for core_ts_mod in BLACKLISTED_MODS {
if thunderstore_mod_string == core_ts_mod {
return Err(format!("Cannot remove core mod {thunderstore_mod_string}"));
}
}

let parsed_ts_mod_string: ParsedThunderstoreModString =
thunderstore_mod_string.parse().unwrap();

// Get installed mods
let installed_ns_mods = get_installed_mods_and_properties(game_install)?;

// List of mod folders to remove
let mut mod_folders_to_remove: Vec<String> = Vec::new();

// Get folder name based on Thundestore mod string
for installed_ns_mod in installed_ns_mods {
if installed_ns_mod.thunderstore_mod_string.is_none() {
// Not a Thunderstore mod
continue;
}

let installed_ns_mod_ts_string: ParsedThunderstoreModString = installed_ns_mod
.thunderstore_mod_string
.unwrap()
.parse()
.unwrap();

// Installed mod matches specified Thunderstore mod string
if parsed_ts_mod_string.author_name == installed_ns_mod_ts_string.author_name
&& parsed_ts_mod_string.mod_name == installed_ns_mod_ts_string.mod_name
{
// Add folder to list of folder to remove
mod_folders_to_remove.push(installed_ns_mod.directory);
}
}

if mod_folders_to_remove.is_empty() {
return Err(format!(
"No mods removed as no Northstar mods matching {thunderstore_mod_string} were found to be installed."
));
}

// Delete given folders
for mod_folder in mod_folders_to_remove {
delete_mod_folder(&mod_folder)?;
}

Ok(())
legacy::delete_thunderstore_mod(game_install, thunderstore_mod_string)
}