aboutsummaryrefslogtreecommitdiff
path: root/src-tauri/src/mod_management
diff options
context:
space:
mode:
authorGeckoEidechse <gecko.eidechse+git@pm.me>2023-04-26 21:58:55 +0200
committerGeckoEidechse <gecko.eidechse+git@pm.me>2023-04-26 21:58:55 +0200
commit10616b295eb23c8250a0d874fe05211f73a8ba81 (patch)
treeaa9496bd16d973f69f6c4d9df7695cae469fad83 /src-tauri/src/mod_management
parent85bb5253657c16d9674a9be2f6c8090b413ca7fb (diff)
parente38ab60e1e4f565f0dafdb7b539e386a390594d7 (diff)
downloadFlightCore-10616b295eb23c8250a0d874fe05211f73a8ba81.tar.gz
FlightCore-10616b295eb23c8250a0d874fe05211f73a8ba81.zip
Merge branch 'main' into fix/handle-failed-download
Diffstat (limited to 'src-tauri/src/mod_management')
-rw-r--r--src-tauri/src/mod_management/mod.rs88
1 files changed, 41 insertions, 47 deletions
diff --git a/src-tauri/src/mod_management/mod.rs b/src-tauri/src/mod_management/mod.rs
index d2b5040b..728e72c0 100644
--- a/src-tauri/src/mod_management/mod.rs
+++ b/src-tauri/src/mod_management/mod.rs
@@ -12,8 +12,6 @@ use std::path::PathBuf;
use app::get_enabled_mods;
use app::GameInstall;
-use json5;
-
#[derive(Debug, Clone)]
struct ParsedThunderstoreModString {
author_name: String,
@@ -25,7 +23,7 @@ impl std::str::FromStr for ParsedThunderstoreModString {
type Err = ();
fn from_str(s: &str) -> Result<Self, Self::Err> {
- let mut parts = s.split("-");
+ let mut parts = s.split('-');
let author_name = parts.next().unwrap().to_string();
let mod_name = parts.next().unwrap().to_string();
@@ -56,9 +54,9 @@ pub struct ModJson {
}
/// Gets all currently installed and enabled/disabled mods to rebuild `enabledmods.json`
-pub fn rebuild_enabled_mods_json(game_install: GameInstall) -> Result<(), String> {
+pub fn rebuild_enabled_mods_json(game_install: &GameInstall) -> Result<(), String> {
let enabledmods_json_path = format!("{}/R2Northstar/enabledmods.json", game_install.game_path);
- let mods_and_properties = get_installed_mods_and_properties(game_install)?;
+ let mods_and_properties = get_installed_mods_and_properties(game_install.clone())?;
// Create new mapping
let mut my_map = serde_json::Map::new();
@@ -91,28 +89,27 @@ pub fn set_mod_enabled_status(
let enabledmods_json_path = format!("{}/R2Northstar/enabledmods.json", game_install.game_path);
// Parse JSON
- let mut res: serde_json::Value = match get_enabled_mods(game_install.clone()) {
+ let mut res: serde_json::Value = match get_enabled_mods(&game_install) {
Ok(res) => res,
Err(err) => {
- println!("Couldn't parse `enabledmod.json`: {}", err);
- println!("Rebuilding file.");
+ log::warn!("Couldn't parse `enabledmod.json`: {}", err);
+ log::warn!("Rebuilding file.");
- rebuild_enabled_mods_json(game_install.clone())?;
+ rebuild_enabled_mods_json(&game_install)?;
// Then try again
- let res = get_enabled_mods(game_install.clone())?;
- res
+ get_enabled_mods(&game_install)?
}
};
// Check if key exists
if res.get(mod_name.clone()).is_none() {
// If it doesn't exist, rebuild `enabledmod.json`
- println!("Value not found in `enabledmod.json`. Rebuilding file");
- rebuild_enabled_mods_json(game_install.clone())?;
+ log::info!("Value not found in `enabledmod.json`. Rebuilding file");
+ rebuild_enabled_mods_json(&game_install)?;
// Then try again
- res = get_enabled_mods(game_install)?;
+ res = get_enabled_mods(&game_install)?;
}
// Update value
@@ -129,7 +126,7 @@ pub fn set_mod_enabled_status(
}
/// Parses `manifest.json` for Thunderstore mod string
-fn parse_for_thunderstore_mod_string(nsmod_path: String) -> Result<String, anyhow::Error> {
+fn parse_for_thunderstore_mod_string(nsmod_path: &str) -> Result<String, anyhow::Error> {
let manifest_json_path = format!("{}/manifest.json", nsmod_path);
let ts_author_txt_path = format!("{}/thunderstore_author.txt", nsmod_path);
@@ -152,7 +149,7 @@ fn parse_for_thunderstore_mod_string(nsmod_path: String) -> Result<String, anyho
}
/// Parse `mods` folder for installed mods.
-fn parse_installed_mods(game_install: GameInstall) -> Result<Vec<NorthstarMod>, anyhow::Error> {
+fn parse_installed_mods(game_install: &GameInstall) -> Result<Vec<NorthstarMod>, anyhow::Error> {
let ns_mods_folder = format!("{}/R2Northstar/mods/", game_install.game_path);
let paths = match std::fs::read_dir(ns_mods_folder) {
@@ -189,7 +186,7 @@ fn parse_installed_mods(game_install: GameInstall) -> Result<Vec<NorthstarMod>,
let parsed_mod_json: ModJson = match json5::from_str(&data) {
Ok(parsed_json) => parsed_json,
Err(err) => {
- println!("Failed parsing {} with {}", mod_json_path, err.to_string());
+ log::warn!("Failed parsing {} with {}", mod_json_path, err.to_string());
continue;
}
};
@@ -198,7 +195,7 @@ fn parse_installed_mods(game_install: GameInstall) -> Result<Vec<NorthstarMod>,
// Attempt legacy method for getting Thunderstore string first
Some(ts_mod_string) => Some(ts_mod_string),
// Legacy method failed
- None => match parse_for_thunderstore_mod_string(directory_str) {
+ None => match parse_for_thunderstore_mod_string(&directory_str) {
Ok(thunderstore_mod_string) => Some(thunderstore_mod_string),
Err(_err) => None,
},
@@ -209,7 +206,7 @@ fn parse_installed_mods(game_install: GameInstall) -> Result<Vec<NorthstarMod>,
let ns_mod = NorthstarMod {
name: parsed_mod_json.name,
version: parsed_mod_json.version,
- thunderstore_mod_string: thunderstore_mod_string,
+ thunderstore_mod_string,
enabled: false, // Placeholder
directory: mod_directory,
};
@@ -229,13 +226,13 @@ pub fn get_installed_mods_and_properties(
game_install: GameInstall,
) -> Result<Vec<NorthstarMod>, String> {
// Get actually installed mods
- let found_installed_mods = match parse_installed_mods(game_install.clone()) {
+ let found_installed_mods = match parse_installed_mods(&game_install) {
Ok(res) => res,
Err(err) => return Err(err.to_string()),
};
// Get enabled mods as JSON
- let enabled_mods: serde_json::Value = match get_enabled_mods(game_install) {
+ let enabled_mods: serde_json::Value = match get_enabled_mods(&game_install) {
Ok(enabled_mods) => enabled_mods,
Err(_) => serde_json::from_str("{}").unwrap(), // `enabledmods.json` not found, create empty object
};
@@ -256,7 +253,7 @@ pub fn get_installed_mods_and_properties(
Ok(installed_mods)
}
-async fn get_ns_mod_download_url(thunderstore_mod_string: String) -> Result<String, String> {
+async fn get_ns_mod_download_url(thunderstore_mod_string: &str) -> Result<String, String> {
// TODO: This will crash the thread if not internet connection exist. `match` should be used instead
let index = thermite::api::get_package_index().unwrap().to_vec();
@@ -276,7 +273,7 @@ async fn get_ns_mod_download_url(thunderstore_mod_string: String) -> Result<Stri
for ns_mod in index {
// Iterate over all versions of a given mod
- for (_key, ns_mod) in &ns_mod.versions {
+ for ns_mod in ns_mod.versions.values() {
if ns_mod.url.contains(&ts_mod_string_url) {
dbg!(ns_mod.clone());
return Ok(ns_mod.url.clone());
@@ -288,21 +285,19 @@ async fn get_ns_mod_download_url(thunderstore_mod_string: String) -> Result<Stri
}
/// Returns a vector of modstrings containing the dependencies of a given mod
-async fn get_mod_dependencies(
- thunderstore_mod_string: String,
-) -> Result<Vec<String>, anyhow::Error> {
- dbg!(thunderstore_mod_string.clone());
+async fn get_mod_dependencies(thunderstore_mod_string: &str) -> Result<Vec<String>, anyhow::Error> {
+ dbg!(thunderstore_mod_string);
// TODO: This will crash the thread if not internet connection exist. `match` should be used instead
let index = thermite::api::get_package_index().unwrap().to_vec();
// String replace works but more care should be taken in the future
- let ts_mod_string_url = thunderstore_mod_string.replace("-", "/");
+ let ts_mod_string_url = thunderstore_mod_string.replace('-', "/");
// Iterate over index
for ns_mod in index {
// Iterate over all versions of a given mod
- for (_key, ns_mod) in &ns_mod.versions {
+ for ns_mod in ns_mod.versions.values() {
if ns_mod.url.contains(&ts_mod_string_url) {
dbg!(ns_mod.clone());
return Ok(ns_mod.deps.clone());
@@ -317,8 +312,8 @@ async fn get_mod_dependencies(
/// Download and install mod to the specified target.
#[async_recursion]
pub async fn fc_download_mod_and_install(
- game_install: GameInstall,
- thunderstore_mod_string: String,
+ game_install: &GameInstall,
+ thunderstore_mod_string: &str,
) -> Result<(), String> {
// Get mods and download directories
let download_directory = format!(
@@ -328,11 +323,11 @@ pub async fn fc_download_mod_and_install(
let mods_directory = format!("{}/R2Northstar/mods/", game_install.game_path);
// Early return on empty string
- if thunderstore_mod_string.len() == 0 {
+ if thunderstore_mod_string.is_empty() {
return Err("Passed empty string".to_string());
}
- let deps = match get_mod_dependencies(thunderstore_mod_string.clone()).await {
+ let deps = match get_mod_dependencies(thunderstore_mod_string).await {
Ok(deps) => deps,
Err(err) => return Err(err.to_string()),
};
@@ -340,13 +335,13 @@ pub async fn fc_download_mod_and_install(
// Recursively install dependencies
for dep in deps {
- match fc_download_mod_and_install(game_install.clone(), dep).await {
+ match fc_download_mod_and_install(game_install, &dep).await {
Ok(()) => (),
Err(err) => {
- if err.to_string() == "Cannot install Northstar as a mod!" {
+ if err == "Cannot install Northstar as a mod!" {
continue; // For Northstar as a dependency, we just skip it
} else {
- return Err(err.to_string());
+ return Err(err);
}
}
};
@@ -361,7 +356,7 @@ pub async fn fc_download_mod_and_install(
}
// Get download URL for the specified mod
- let download_url = get_ns_mod_download_url(thunderstore_mod_string.clone()).await?;
+ let download_url = get_ns_mod_download_url(thunderstore_mod_string).await?;
// Create download directory
match std::fs::create_dir_all(download_directory.clone()) {
@@ -369,20 +364,19 @@ pub async fn fc_download_mod_and_install(
Err(err) => return Err(err.to_string()),
};
- let name = thunderstore_mod_string.clone();
let path = format!(
- "{}/___flightcore-temp-download-dir/{}.zip",
- game_install.game_path, name
+ "{}/___flightcore-temp-download-dir/{thunderstore_mod_string}.zip",
+ game_install.game_path
);
// Download the mod
- let f = match thermite::core::manage::download_file(&download_url, path.clone()) {
+ let f = match thermite::core::manage::download_file(download_url, path.clone()) {
Ok(f) => f,
Err(e) => return Err(e.to_string()),
};
// Get Thunderstore mod author
- let author = thunderstore_mod_string.split("-").next().unwrap();
+ let author = thunderstore_mod_string.split('-').next().unwrap();
// Extract the mod to the mods directory
match thermite::core::manage::install_mod(author, &f, std::path::Path::new(&mods_directory)) {
@@ -397,7 +391,7 @@ pub async fn fc_download_mod_and_install(
}
/// Deletes a given Northstar mod folder
-fn delete_mod_folder(ns_mod_directory: String) -> Result<(), String> {
+fn delete_mod_folder(ns_mod_directory: &str) -> 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
@@ -408,7 +402,7 @@ fn delete_mod_folder(ns_mod_directory: String) -> Result<(), String> {
return Err(format!("mod.json does not exist in {}", ns_mod_directory));
}
- match std::fs::remove_dir_all(&ns_mod_directory) {
+ match std::fs::remove_dir_all(ns_mod_directory) {
Ok(()) => Ok(()),
Err(err) => Err(format!("Failed deleting mod: {err}")),
}
@@ -432,7 +426,7 @@ pub fn delete_northstar_mod(game_install: GameInstall, nsmod_name: String) -> Re
// Installed mod matches specified mod
if installed_ns_mod.name == nsmod_name {
// Delete folder
- return delete_mod_folder(installed_ns_mod.directory);
+ return delete_mod_folder(&installed_ns_mod.directory);
}
}
@@ -483,7 +477,7 @@ pub fn delete_thunderstore_mod(
}
}
- if !(mod_folders_to_remove.len() > 0) {
+ 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."
));
@@ -491,7 +485,7 @@ pub fn delete_thunderstore_mod(
// Delete given folders
for mod_folder in mod_folders_to_remove {
- delete_mod_folder(mod_folder)?;
+ delete_mod_folder(&mod_folder)?;
}
Ok(())