diff options
author | GeckoEidechse <gecko.eidechse+git@pm.me> | 2023-02-08 03:02:28 +0100 |
---|---|---|
committer | GeckoEidechse <gecko.eidechse+git@pm.me> | 2023-02-08 03:02:28 +0100 |
commit | c60b7b606dd0b4bc7108f9a768a4545f0781730c (patch) | |
tree | 84566aca1cd4499288c00f236563541c00aef070 /src-tauri/src | |
parent | 9fd1b18373b495a777edd724c875cd376ee8fb2f (diff) | |
parent | 7df1f2dbc038d0d93463249f15ca2e1e7ec23094 (diff) | |
download | FlightCore-c60b7b606dd0b4bc7108f9a768a4545f0781730c.tar.gz FlightCore-c60b7b606dd0b4bc7108f9a768a4545f0781730c.zip |
Merge branch 'main' into feat/basic-log-parsing
Diffstat (limited to 'src-tauri/src')
-rw-r--r-- | src-tauri/src/lib.rs | 7 | ||||
-rw-r--r-- | src-tauri/src/main.rs | 2 | ||||
-rw-r--r-- | src-tauri/src/mod_management/mod.rs | 91 |
3 files changed, 38 insertions, 62 deletions
diff --git a/src-tauri/src/lib.rs b/src-tauri/src/lib.rs index e43e5935..5688512c 100644 --- a/src-tauri/src/lib.rs +++ b/src-tauri/src/lib.rs @@ -36,6 +36,7 @@ pub struct GameInstall { #[ts(export)] pub struct NorthstarMod { pub name: String, + pub version: Option<String>, pub thunderstore_mod_string: Option<String>, pub enabled: bool, pub directory: String, @@ -192,9 +193,7 @@ async fn do_install(nmod: &thermite::model::ModVersion, game_path: &std::path::P let download_path = format!("{}/{}", download_directory.clone(), filename); println!("{}", download_path); - let nfile = thermite::core::manage::download_file(&nmod.url, download_path) - .await - .unwrap(); + let nfile = thermite::core::manage::download_file(&nmod.url, download_path).unwrap(); println!("Extracting Northstar..."); extract(nfile, game_path)?; @@ -223,7 +222,7 @@ pub async fn install_northstar( None => "Northstar".to_string(), }; - let index = thermite::api::get_package_index().await.unwrap().to_vec(); + let index = thermite::api::get_package_index().unwrap().to_vec(); let nmod = index .iter() .find(|f| f.name.to_lowercase() == northstar_package_name.to_lowercase()) diff --git a/src-tauri/src/main.rs b/src-tauri/src/main.rs index 015ac925..ce7708b1 100644 --- a/src-tauri/src/main.rs +++ b/src-tauri/src/main.rs @@ -188,7 +188,7 @@ async fn check_is_northstar_outdated( None => "Northstar".to_string(), }; - let index = thermite::api::get_package_index().await.unwrap().to_vec(); + let index = thermite::api::get_package_index().unwrap().to_vec(); let nmod = index .iter() .find(|f| f.name.to_lowercase() == northstar_package_name.to_lowercase()) diff --git a/src-tauri/src/mod_management/mod.rs b/src-tauri/src/mod_management/mod.rs index d269a627..75d63972 100644 --- a/src-tauri/src/mod_management/mod.rs +++ b/src-tauri/src/mod_management/mod.rs @@ -51,6 +51,16 @@ 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>, + #[serde(rename = "Version")] + version: 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 +134,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 +158,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 +189,32 @@ 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, + version: parsed_mod_json.version, thunderstore_mod_string: thunderstore_mod_string, enabled: false, // Placeholder directory: mod_directory, @@ -261,7 +235,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) { @@ -287,7 +264,7 @@ pub fn get_installed_mods_and_properties( async fn get_ns_mod_download_url(thunderstore_mod_string: String) -> 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().await.unwrap().to_vec(); + let index = thermite::api::get_package_index().unwrap().to_vec(); // Parse mod string let parsed_ts_mod_string: ParsedThunderstoreModString = match thunderstore_mod_string.parse() { @@ -323,7 +300,7 @@ async fn get_mod_dependencies( dbg!(thunderstore_mod_string.clone()); // TODO: This will crash the thread if not internet connection exist. `match` should be used instead - let index = thermite::api::get_package_index().await.unwrap().to_vec(); + 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("-", "/"); @@ -405,7 +382,7 @@ pub async fn fc_download_mod_and_install( ); // Download the mod - let f = match thermite::core::manage::download_file(&download_url, path.clone()).await { + let f = match thermite::core::manage::download_file(&download_url, path.clone()) { Ok(f) => f, Err(e) => return Err(e.to_string()), }; |