aboutsummaryrefslogtreecommitdiff
path: root/src-tauri/src/mod_management
diff options
context:
space:
mode:
Diffstat (limited to 'src-tauri/src/mod_management')
-rw-r--r--src-tauri/src/mod_management/mod.rs82
1 files changed, 28 insertions, 54 deletions
diff --git a/src-tauri/src/mod_management/mod.rs b/src-tauri/src/mod_management/mod.rs
index d269a627..01e2cbb5 100644
--- a/src-tauri/src/mod_management/mod.rs
+++ b/src-tauri/src/mod_management/mod.rs
@@ -51,6 +51,14 @@ pub struct ThunderstoreManifest {
version_number: String,
}
+#[derive(Serialize, Deserialize, Debug, Clone)]
+pub struct ModJson {
+ #[serde(rename = "Name")]
+ name: String,
+ #[serde(rename = "ThunderstoreModString")]
+ thunderstore_mod_string: Option<String>,
+}
+
/// Gets all currently installed and enabled/disabled mods to rebuild `enabledmods.json`
pub fn rebuild_enabled_mods_json(game_install: GameInstall) -> Result<(), String> {
let enabledmods_json_path = format!("{}/R2Northstar/enabledmods.json", game_install.game_path);
@@ -124,56 +132,11 @@ pub fn set_mod_enabled_status(
Ok(())
}
-/// Parses `mod.json` for mod name
-// TODO: Maybe pass PathBuf or serde json object
-fn parse_mod_json_for_mod_name(mod_json_path: String) -> Result<String, anyhow::Error> {
- // Read file into string and parse it
- let data = std::fs::read_to_string(mod_json_path)?;
- let parsed_json: serde_json::Value = json5::from_str(&data)?;
-
- // Extract mod name
- let mod_name = match parsed_json.get("Name").and_then(|value| value.as_str()) {
- Some(name) => name,
- None => return Err(anyhow!("No name found")),
- };
-
- Ok(mod_name.to_string())
-}
-
-/// Parses `mod.json` for Thunderstore mod string
-/// This is a legacy function as nowadays `manifest.json` should be in Northtar mods folder and read from there
-// TODO: Maybe pass PathBuf or serde json object
-fn parse_mod_json_for_thunderstore_mod_string(
- mod_json_path: String,
-) -> Result<String, anyhow::Error> {
- // Read file into string and parse it
- let data = std::fs::read_to_string(mod_json_path)?;
- let parsed_json: serde_json::Value = json5::from_str(&data)?;
-
- // Extract TS mod string
- let thunderstore_mod_string = match parsed_json
- .get("ThunderstoreModString")
- .and_then(|value| value.as_str())
- {
- Some(thunderstore_mod_string) => thunderstore_mod_string,
- None => return Err(anyhow!("No ThunderstoreModString found")),
- };
-
- Ok(thunderstore_mod_string.to_string())
-}
-
/// Parses `manifest.json` for Thunderstore mod string
fn parse_for_thunderstore_mod_string(nsmod_path: String) -> Result<String, anyhow::Error> {
- let mod_json_path = format!("{}/mod.json", nsmod_path);
let manifest_json_path = format!("{}/manifest.json", nsmod_path);
let ts_author_txt_path = format!("{}/thunderstore_author.txt", nsmod_path);
- // Attempt legacy method for getting Thunderstore string first
- match parse_mod_json_for_thunderstore_mod_string(mod_json_path) {
- Ok(thunderstore_mod_string) => return Ok(thunderstore_mod_string),
- Err(_err) => (),
- }
-
// Check if `manifest.json` exists and parse
let data = std::fs::read_to_string(manifest_json_path)?;
let thunderstore_manifest: ThunderstoreManifest = json5::from_str(&data)?;
@@ -193,12 +156,12 @@ 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>, String> {
+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) {
Ok(paths) => paths,
- Err(_err) => return Err("No mods folder found".to_string()),
+ Err(_err) => return Err(anyhow!("No mods folder found")),
};
let mut directories: Vec<PathBuf> = Vec::new();
@@ -224,23 +187,31 @@ fn parse_installed_mods(game_install: GameInstall) -> Result<Vec<NorthstarMod>,
}
// Parse mod.json and get mod name
- let mod_name = match parse_mod_json_for_mod_name(mod_json_path.clone()) {
- Ok(mod_name) => mod_name,
+
+ // Read file into string and parse it
+ let data = std::fs::read_to_string(mod_json_path.clone())?;
+ 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());
continue;
}
};
// Get Thunderstore mod string if it exists
- let thunderstore_mod_string = match parse_for_thunderstore_mod_string(directory_str) {
- Ok(thunderstore_mod_string) => Some(thunderstore_mod_string),
- Err(_err) => None,
+ let thunderstore_mod_string = match parsed_mod_json.thunderstore_mod_string {
+ // 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) {
+ Ok(thunderstore_mod_string) => Some(thunderstore_mod_string),
+ Err(_err) => None,
+ },
};
// Get directory path
let mod_directory = directory.to_str().unwrap().to_string();
let ns_mod = NorthstarMod {
- name: mod_name,
+ name: parsed_mod_json.name,
thunderstore_mod_string: thunderstore_mod_string,
enabled: false, // Placeholder
directory: mod_directory,
@@ -261,7 +232,10 @@ pub fn get_installed_mods_and_properties(
game_install: GameInstall,
) -> Result<Vec<NorthstarMod>, String> {
// Get actually installed mods
- let found_installed_mods = parse_installed_mods(game_install.clone())?;
+ let found_installed_mods = match parse_installed_mods(game_install.clone()) {
+ 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) {