From b58d8d138919ec911369b644d7a49c94215c77a9 Mon Sep 17 00:00:00 2001 From: GeckoEidechse <40122905+GeckoEidechse@users.noreply.github.com> Date: Fri, 13 Oct 2023 13:43:51 +0200 Subject: refactor: Move relevant functions to northstar module (#626) * refactor: Move `get_available_northstar_versions` to northstar module * refactor: Move `check_is_northstar_outdated` to northstar module --- src-tauri/src/northstar/mod.rs | 91 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 91 insertions(+) (limited to 'src-tauri/src/northstar') diff --git a/src-tauri/src/northstar/mod.rs b/src-tauri/src/northstar/mod.rs index a6b895db..1c2023b6 100644 --- a/src-tauri/src/northstar/mod.rs +++ b/src-tauri/src/northstar/mod.rs @@ -9,8 +9,99 @@ use crate::{ platform_specific::get_host_os, GameInstall, InstallType, }; +use crate::{NorthstarThunderstoreRelease, NorthstarThunderstoreReleaseWrapper}; use anyhow::anyhow; +/// Gets list of available Northstar versions from Thunderstore +#[tauri::command] +pub async fn get_available_northstar_versions( +) -> Result, ()> { + let northstar_package_name = "Northstar"; + let index = thermite::api::get_package_index().unwrap().to_vec(); + let nsmod = index + .iter() + .find(|f| f.name.to_lowercase() == northstar_package_name.to_lowercase()) + .ok_or_else(|| panic!("Couldn't find Northstar on thunderstore???")) + .unwrap(); + + let mut releases: Vec = vec![]; + for (_version_string, nsmod_version_obj) in nsmod.versions.iter() { + let current_elem = NorthstarThunderstoreRelease { + package: nsmod_version_obj.name.clone(), + version: nsmod_version_obj.version.clone(), + }; + let current_elem_wrapped = NorthstarThunderstoreReleaseWrapper { + label: format!( + "{} v{}", + nsmod_version_obj.name.clone(), + nsmod_version_obj.version.clone() + ), + value: current_elem, + }; + + releases.push(current_elem_wrapped); + } + + releases.sort_by(|a, b| { + // Parse version number + let a_ver = semver::Version::parse(&a.value.version).unwrap(); + let b_ver = semver::Version::parse(&b.value.version).unwrap(); + b_ver.partial_cmp(&a_ver).unwrap() // Sort newest first + }); + + Ok(releases) +} + +/// Checks if installed Northstar version is up-to-date +/// false -> Northstar install is up-to-date +/// true -> Northstar install is outdated +#[tauri::command] +pub async fn check_is_northstar_outdated( + game_install: GameInstall, + northstar_package_name: Option, +) -> Result { + let northstar_package_name = match northstar_package_name { + Some(northstar_package_name) => { + if northstar_package_name.len() <= 1 { + "Northstar".to_string() + } else { + northstar_package_name + } + } + None => "Northstar".to_string(), + }; + + let index = match thermite::api::get_package_index() { + Ok(res) => res.to_vec(), + Err(err) => return Err(format!("Couldn't check if Northstar up-to-date: {err}")), + }; + let nmod = index + .iter() + .find(|f| f.name.to_lowercase() == northstar_package_name.to_lowercase()) + .expect("Couldn't find Northstar on thunderstore???"); + // .ok_or_else(|| anyhow!("Couldn't find Northstar on thunderstore???"))?; + + let version_number = match get_northstar_version_number(game_install) { + Ok(version_number) => version_number, + Err(err) => { + log::warn!("{}", err); + // If we fail to get new version just assume we are up-to-date + return Err(err); + } + }; + + // Release candidate version numbers are different between `mods.json` and Thunderstore + let version_number = crate::convert_release_candidate_number(version_number); + + if version_number != nmod.latest { + log::info!("Installed Northstar version outdated"); + Ok(true) + } else { + log::info!("Installed Northstar version up-to-date"); + Ok(false) + } +} + /// Check version number of a mod pub fn check_mod_version_number(path_to_mod_folder: &str) -> Result { let data = std::fs::read_to_string(format!("{path_to_mod_folder}/mod.json"))?; -- cgit v1.2.3