Skip to content

Commit

Permalink
refactor: Move relevant functions to northstar module (#626)
Browse files Browse the repository at this point in the history
* refactor: Move `get_available_northstar_versions`
to northstar module

* refactor: Move `check_is_northstar_outdated`
to northstar module
  • Loading branch information
GeckoEidechse authored Oct 13, 2023
1 parent b3b558d commit b58d8d1
Show file tree
Hide file tree
Showing 2 changed files with 94 additions and 94 deletions.
97 changes: 3 additions & 94 deletions src-tauri/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ mod repair_and_verify;
mod thunderstore;
mod util;

use semver::Version;
use serde::{Deserialize, Serialize};
#[cfg(target_os = "windows")]
use tauri::api::dialog::blocking::MessageDialogBuilder;
Expand All @@ -38,7 +37,7 @@ struct NorthstarThunderstoreRelease {

#[derive(Serialize, Deserialize, Debug, Clone, TS)]
#[ts(export)]
struct NorthstarThunderstoreReleaseWrapper {
pub struct NorthstarThunderstoreReleaseWrapper {
label: String,
value: NorthstarThunderstoreRelease,
}
Expand Down Expand Up @@ -121,7 +120,7 @@ fn main() {
northstar::install::find_game_install_location,
util::get_flightcore_version_number,
northstar::get_northstar_version_number,
check_is_northstar_outdated,
northstar::check_is_northstar_outdated,
repair_and_verify::verify_install_location,
platform_specific::get_host_os,
northstar::install::install_northstar_wrapper,
Expand Down Expand Up @@ -157,7 +156,7 @@ fn main() {
github::pull_requests::get_launcher_download_link,
util::close_application,
development::install_git_main,
get_available_northstar_versions,
northstar::get_available_northstar_versions,
northstar::profile::fetch_profiles,
northstar::profile::validate_profile,
northstar::profile::delete_profile,
Expand Down Expand Up @@ -203,56 +202,6 @@ pub fn convert_release_candidate_number(version_number: String) -> String {
version_number.replace("-rc", "0").replace("00", "")
}

/// Checks if installed Northstar version is up-to-date
/// false -> Northstar install is up-to-date
/// true -> Northstar install is outdated
#[tauri::command]
async fn check_is_northstar_outdated(
game_install: GameInstall,
northstar_package_name: Option<String>,
) -> Result<bool, String> {
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 northstar::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 = 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)
}
}

/// Installs the specified mod
#[tauri::command]
async fn install_mod_wrapper(
Expand Down Expand Up @@ -290,46 +239,6 @@ async fn clean_up_download_folder_wrapper(
}
}

/// Gets list of available Northstar versions from Thunderstore
#[tauri::command]
async fn get_available_northstar_versions() -> Result<Vec<NorthstarThunderstoreReleaseWrapper>, ()>
{
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<NorthstarThunderstoreReleaseWrapper> = 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 = Version::parse(&a.value.version).unwrap();
let b_ver = Version::parse(&b.value.version).unwrap();
b_ver.partial_cmp(&a_ver).unwrap() // Sort newest first
});

Ok(releases)
}

use anyhow::Result;

/// Defines how Titanfall2 was installed (Steam, Origin, ...)
Expand Down
91 changes: 91 additions & 0 deletions src-tauri/src/northstar/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<Vec<NorthstarThunderstoreReleaseWrapper>, ()> {
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<NorthstarThunderstoreReleaseWrapper> = 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<String>,
) -> Result<bool, String> {
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<String, anyhow::Error> {
let data = std::fs::read_to_string(format!("{path_to_mod_folder}/mod.json"))?;
Expand Down

0 comments on commit b58d8d1

Please sign in to comment.