From 478536e663553391726db2460db40ee5f58f8325 Mon Sep 17 00:00:00 2001 From: GeckoEidechse <40122905+GeckoEidechse@users.noreply.github.com> Date: Mon, 6 Feb 2023 08:31:48 +0100 Subject: refactor: Parse using serde deserialisation (#158) * refactor: Parse using serde deserialisation * refactor: Parse using serde deserial. (TS mod str) Reduce code by making use of serde deserialisation. Still supports legacy method * chore: Remove commented out fields While useful, it just made the code messy... --- src-tauri/src/mod_management/mod.rs | 82 +++++++++++++------------------------ 1 file changed, 28 insertions(+), 54 deletions(-) (limited to 'src-tauri/src') 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, +} + /// 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 { - // 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 { - // 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 { - 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 Result, String> { +fn parse_installed_mods(game_install: GameInstall) -> Result, 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 = Vec::new(); @@ -224,23 +187,31 @@ fn parse_installed_mods(game_install: GameInstall) -> Result, } // 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, 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) { -- cgit v1.2.3 From acd8a26fcd68ffa7ecd0451279a6b64a44e28b53 Mon Sep 17 00:00:00 2001 From: GeckoEidechse <40122905+GeckoEidechse@users.noreply.github.com> Date: Mon, 6 Feb 2023 21:59:46 +0100 Subject: chore: Bump libthermite version to 0.5.2 (#163) And fix corresponding code that broke in the process Basically just removing `await`s as the networking functions are no longer async. --- src-tauri/Cargo.lock | 150 +++++++++++++++++++++--------------- src-tauri/Cargo.toml | 2 +- src-tauri/src/lib.rs | 6 +- src-tauri/src/main.rs | 2 +- src-tauri/src/mod_management/mod.rs | 6 +- 5 files changed, 96 insertions(+), 70 deletions(-) (limited to 'src-tauri/src') diff --git a/src-tauri/Cargo.lock b/src-tauri/Cargo.lock index 5a0a5256..af5fae1e 100644 --- a/src-tauri/Cargo.lock +++ b/src-tauri/Cargo.lock @@ -460,20 +460,6 @@ dependencies = [ "memchr", ] -[[package]] -name = "console" -version = "0.15.2" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "c050367d967ced717c04b65d8c619d863ef9292ce0c5760028655a2fb298718c" -dependencies = [ - "encode_unicode", - "lazy_static", - "libc", - "terminal_size", - "unicode-width", - "winapi", -] - [[package]] name = "constant_time_eq" version = "0.1.5" @@ -854,12 +840,6 @@ version = "1.2.2" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "4ef6b89e5b37196644d8796de5268852ff179b44e96276cf4290264843743bb7" -[[package]] -name = "encode_unicode" -version = "0.3.6" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "a357d28ed41a50f9c765dbfe56cbc04a64e53e5fc58ba79fbc34c10ef3df831f" - [[package]] name = "encoding_rs" version = "0.8.31" @@ -1628,18 +1608,6 @@ dependencies = [ "hashbrown 0.12.3", ] -[[package]] -name = "indicatif" -version = "0.17.2" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "4295cbb7573c16d310e99e713cf9e75101eb190ab31fccd35f2d2691b4352b19" -dependencies = [ - "console", - "number_prefix", - "portable-atomic", - "unicode-width", -] - [[package]] name = "infer" version = "0.7.0" @@ -1825,17 +1793,16 @@ dependencies = [ [[package]] name = "libthermite" -version = "0.4.0" +version = "0.5.2" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "31ec1288a3f54f21ee6bd7eec92c5d098d9acc82d3bfdcf3c5ae29498ddfdc0e" +checksum = "80ab0312cd1bab244b8b1abd8a0f48b771d4591d4521344468e91a26d6d9fad0" dependencies = [ - "futures-util", - "indicatif", - "log", - "reqwest", + "json5", "serde", "serde_json", "thiserror", + "tracing", + "ureq", "zip", ] @@ -2196,12 +2163,6 @@ dependencies = [ "syn", ] -[[package]] -name = "number_prefix" -version = "0.4.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "830b246a0e5f20af87141b25c173cd1b609bd7779a4617d6ec582abaf90870f3" - [[package]] name = "objc" version = "0.2.7" @@ -2629,12 +2590,6 @@ dependencies = [ "miniz_oxide 0.6.2", ] -[[package]] -name = "portable-atomic" -version = "0.3.15" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "15eb2c6e362923af47e13c23ca5afb859e83d54452c55b0b9ac763b8f7c1ac16" - [[package]] name = "ppv-lite86" version = "0.2.17" @@ -2965,7 +2920,6 @@ dependencies = [ "serde_urlencoded", "tokio", "tokio-native-tls", - "tokio-util", "tower-service", "url", "wasm-bindgen", @@ -2998,6 +2952,21 @@ dependencies = [ "windows 0.37.0", ] +[[package]] +name = "ring" +version = "0.16.20" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "3053cf52e236a3ed746dfc745aa9cacf1b791d846bdaf412f60a8d7d6e17c8fc" +dependencies = [ + "cc", + "libc", + "once_cell", + "spin", + "untrusted", + "web-sys", + "winapi", +] + [[package]] name = "rusqlite" version = "0.27.0" @@ -3037,6 +3006,18 @@ dependencies = [ "semver 1.0.14", ] +[[package]] +name = "rustls" +version = "0.20.8" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "fff78fc74d175294f4e83b28343315ffcfb114b156f0185e9741cb5570f50e2f" +dependencies = [ + "log", + "ring", + "sct", + "webpki", +] + [[package]] name = "rustversion" version = "1.0.9" @@ -3092,6 +3073,16 @@ version = "1.0.2" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "9c8132065adcfd6e02db789d9285a0deb2f3fcb04002865ab67d5fb103533898" +[[package]] +name = "sct" +version = "0.7.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d53dcdb7c9f8158937a7981b48accfd39a43af418591a5d008c7b22b5e1b7ca4" +dependencies = [ + "ring", + "untrusted", +] + [[package]] name = "security-framework" version = "2.7.0" @@ -3471,6 +3462,12 @@ dependencies = [ "system-deps 5.0.0", ] +[[package]] +name = "spin" +version = "0.5.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "6e63cff320ae2c57904679ba7cb63280a3dc4613885beafb148ee7bf9aa9042d" + [[package]] name = "stable_deref_trait" version = "1.2.0" @@ -3928,16 +3925,6 @@ dependencies = [ "winapi-util", ] -[[package]] -name = "terminal_size" -version = "0.1.17" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "633c1a546cee861a1a6d0dc69ebeca693bf4296661ba7852b9d21d159e0506df" -dependencies = [ - "libc", - "winapi", -] - [[package]] name = "thin-slice" version = "0.1.1" @@ -4250,6 +4237,28 @@ version = "0.1.10" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "c0edd1e5b14653f783770bce4a4dabb4a5108a5370a5f5d8cfe8710c361f6c8b" +[[package]] +name = "untrusted" +version = "0.7.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a156c684c91ea7d62626509bce3cb4e1d9ed5c4d978f7b4352658f96a4c26b4a" + +[[package]] +name = "ureq" +version = "2.6.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "338b31dd1314f68f3aabf3ed57ab922df95ffcd902476ca7ba3c4ce7b908c46d" +dependencies = [ + "base64", + "flate2", + "log", + "once_cell", + "rustls", + "url", + "webpki", + "webpki-roots", +] + [[package]] name = "url" version = "2.3.1" @@ -4476,6 +4485,25 @@ dependencies = [ "system-deps 6.0.3", ] +[[package]] +name = "webpki" +version = "0.22.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f095d78192e208183081cc07bc5515ef55216397af48b873e5edcd72637fa1bd" +dependencies = [ + "ring", + "untrusted", +] + +[[package]] +name = "webpki-roots" +version = "0.22.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b6c71e40d7d2c34a5106301fb632274ca37242cd0c9d3e64dbece371a40a2d87" +dependencies = [ + "webpki", +] + [[package]] name = "webview2-com" version = "0.19.1" diff --git a/src-tauri/Cargo.toml b/src-tauri/Cargo.toml index 9c0ad2b8..405349da 100644 --- a/src-tauri/Cargo.toml +++ b/src-tauri/Cargo.toml @@ -25,7 +25,7 @@ steamlocate = "1.0.2" # Error messages anyhow = "1.0" # libthermite for Northstar/mod install handling -libthermite = "0.4.0" +libthermite = "0.5.2" # zip stuff zip = "0.6.2" # Regex diff --git a/src-tauri/src/lib.rs b/src-tauri/src/lib.rs index e43e5935..1ae55a6f 100644 --- a/src-tauri/src/lib.rs +++ b/src-tauri/src/lib.rs @@ -192,9 +192,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 +221,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 4eab7a00..ba9264ee 100644 --- a/src-tauri/src/main.rs +++ b/src-tauri/src/main.rs @@ -185,7 +185,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 01e2cbb5..90dc85da 100644 --- a/src-tauri/src/mod_management/mod.rs +++ b/src-tauri/src/mod_management/mod.rs @@ -261,7 +261,7 @@ pub fn get_installed_mods_and_properties( async fn get_ns_mod_download_url(thunderstore_mod_string: String) -> Result { // 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() { @@ -297,7 +297,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("-", "/"); @@ -379,7 +379,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()), }; -- cgit v1.2.3 From 83b6b454bf88cb80aea478f8b46f95bec4d9ea00 Mon Sep 17 00:00:00 2001 From: GeckoEidechse <40122905+GeckoEidechse@users.noreply.github.com> Date: Tue, 7 Feb 2023 23:39:59 +0100 Subject: feat: Show mod version in LocalMods View (#159) * refactor: Parse using serde deserialisation * refactor: Parse using serde deserial. (TS mod str) Reduce code by making use of serde deserialisation. Still supports legacy method * chore: Remove commented out fields While useful, it just made the code messy... * feat: Show mod version in LocalMods view * refactor: Move mod version to new line in source code Makes diffing in git easier * fix: Hide version number if not available --- src-tauri/bindings/NorthstarMod.ts | 2 +- src-tauri/src/lib.rs | 1 + src-tauri/src/mod_management/mod.rs | 3 +++ src-vue/src/views/mods/LocalModsView.vue | 1 + 4 files changed, 6 insertions(+), 1 deletion(-) (limited to 'src-tauri/src') diff --git a/src-tauri/bindings/NorthstarMod.ts b/src-tauri/bindings/NorthstarMod.ts index ed9d9297..3686485b 100644 --- a/src-tauri/bindings/NorthstarMod.ts +++ b/src-tauri/bindings/NorthstarMod.ts @@ -1,3 +1,3 @@ // This file was generated by [ts-rs](https://github.com/Aleph-Alpha/ts-rs). Do not edit this file manually. -export interface NorthstarMod { name: string, thunderstore_mod_string: string | null, enabled: boolean, directory: string, } \ No newline at end of file +export interface NorthstarMod { name: string, version: string | null, thunderstore_mod_string: string | null, enabled: boolean, directory: string, } \ No newline at end of file diff --git a/src-tauri/src/lib.rs b/src-tauri/src/lib.rs index 1ae55a6f..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, pub thunderstore_mod_string: Option, pub enabled: bool, pub directory: String, diff --git a/src-tauri/src/mod_management/mod.rs b/src-tauri/src/mod_management/mod.rs index 90dc85da..75d63972 100644 --- a/src-tauri/src/mod_management/mod.rs +++ b/src-tauri/src/mod_management/mod.rs @@ -57,6 +57,8 @@ pub struct ModJson { name: String, #[serde(rename = "ThunderstoreModString")] thunderstore_mod_string: Option, + #[serde(rename = "Version")] + version: Option, } /// Gets all currently installed and enabled/disabled mods to rebuild `enabledmods.json` @@ -212,6 +214,7 @@ fn parse_installed_mods(game_install: GameInstall) -> Result, let ns_mod = NorthstarMod { name: parsed_mod_json.name, + version: parsed_mod_json.version, thunderstore_mod_string: thunderstore_mod_string, enabled: false, // Placeholder directory: mod_directory, diff --git a/src-vue/src/views/mods/LocalModsView.vue b/src-vue/src/views/mods/LocalModsView.vue index 59b51936..ed801b7a 100644 --- a/src-vue/src/views/mods/LocalModsView.vue +++ b/src-vue/src/views/mods/LocalModsView.vue @@ -14,6 +14,7 @@ {{ mod.name }} + (v{{ mod.version }})