diff options
author | GeckoEidechse <40122905+GeckoEidechse@users.noreply.github.com> | 2022-11-19 15:09:48 +0100 |
---|---|---|
committer | GitHub <noreply@github.com> | 2022-11-19 15:09:48 +0100 |
commit | 9570f66d59107c87eaa93611c7d4a26e0d4cca0e (patch) | |
tree | 34f0891f26c28e586d54ec841b99f4ddc556d15f /src-tauri/src/mod_management | |
parent | 38e12489a517662157d85ec6efec00f225cccc9c (diff) | |
download | FlightCore-9570f66d59107c87eaa93611c7d4a26e0d4cca0e.tar.gz FlightCore-9570f66d59107c87eaa93611c7d4a26e0d4cca0e.zip |
feat: Return Thunderstore mod string of installed mods. (#56)
* refactor: Rename function
to better differentiate what it does
* refactor: Add TS mod string field to NS mod struct
This allows us to expose to frontend which Thunderstore mod a Northstar
mod may belong to. Field is optional as not all mods may contain a
Thunderstore mod string for example when they were installed manually.
* feat: Add TS mod string field to NS mod interace
* feat: Parse TS mod string and expose to frontend
* fix: Mark unused variable as such
Diffstat (limited to 'src-tauri/src/mod_management')
-rw-r--r-- | src-tauri/src/mod_management/mod.rs | 40 |
1 files changed, 34 insertions, 6 deletions
diff --git a/src-tauri/src/mod_management/mod.rs b/src-tauri/src/mod_management/mod.rs index 96bb1a0f..a057532f 100644 --- a/src-tauri/src/mod_management/mod.rs +++ b/src-tauri/src/mod_management/mod.rs @@ -56,14 +56,35 @@ fn parse_mod_json_for_mod_name(mod_json_path: String) -> Result<String, anyhow:: Ok(mod_name.to_string()) } +/// Parses `mod.json` for Thunderstore mod string +// 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()) +} + /// Parse `mods` folder for installed mods. -fn get_installed_mods(game_install: GameInstall) -> Result<Vec<String>, String> { +fn parse_installed_mods(game_install: GameInstall) -> Result<Vec<(String, Option<String>)>, String> { let ns_mods_folder = format!("{}/R2Northstar/mods/", game_install.game_path); let paths = std::fs::read_dir(ns_mods_folder).unwrap(); let mut directories: Vec<PathBuf> = Vec::new(); - let mut mod_names: Vec<String> = Vec::new(); + let mut mods: Vec<(String, Option<String>)> = Vec::new(); // Get list of folders in `mods` directory for path in paths { @@ -91,11 +112,17 @@ fn get_installed_mods(game_install: GameInstall) -> Result<Vec<String>, String> continue; } }; - mod_names.push(mod_name); + let thunderstore_mod_string = + match parse_mod_json_for_thunderstore_mod_string(mod_json_path.clone()) { + Ok(thunderstore_mod_string) => Some(thunderstore_mod_string), + Err(_err) => None, + }; + + mods.push((mod_name, thunderstore_mod_string)); } // Return found mod names - Ok(mod_names) + Ok(mods) } /// Gets list of installed mods and their properties @@ -105,7 +132,7 @@ pub fn get_installed_mods_and_properties( game_install: GameInstall, ) -> Result<Vec<NorthstarMod>, String> { // Get actually installed mods - let found_installed_mods = get_installed_mods(game_install.clone())?; + let found_installed_mods = parse_installed_mods(game_install.clone())?; // Get enabled mods as JSON let enabled_mods: serde_json::Value = match get_enabled_mods(game_install) { @@ -117,13 +144,14 @@ pub fn get_installed_mods_and_properties( let mapping = enabled_mods.as_object().unwrap(); // Use list of installed mods and set enabled based on `enabledmods.json` - for name in found_installed_mods { + for (name, thunderstore_mod_string) in found_installed_mods { let current_mod_enabled = match mapping.get(&name) { Some(enabled) => enabled.as_bool().unwrap(), None => true, // Northstar considers mods not in mapping as enabled. }; let current_mod: NorthstarMod = NorthstarMod { name: name, + thunderstore_mod_string: thunderstore_mod_string, enabled: current_mod_enabled, }; installed_mods.push(current_mod); |