From 24fb67f88ceca9bec04b49fae5b58759b7b25ec5 Mon Sep 17 00:00:00 2001 From: Jan Date: Tue, 8 Aug 2023 23:59:05 +0200 Subject: feat: Add dropdown menu for profiles (#494) Adds a dropdown menu to settings that allows selecting a different profile. Currently gated behind dev mode being active. --- src-tauri/src/northstar/mod.rs | 1 + src-tauri/src/northstar/profile.rs | 76 ++++++++++++++++++++++++++++++++++++++ 2 files changed, 77 insertions(+) create mode 100644 src-tauri/src/northstar/profile.rs (limited to 'src-tauri/src/northstar') diff --git a/src-tauri/src/northstar/mod.rs b/src-tauri/src/northstar/mod.rs index 173495c6..4fecfe51 100644 --- a/src-tauri/src/northstar/mod.rs +++ b/src-tauri/src/northstar/mod.rs @@ -1,6 +1,7 @@ //! This module deals with handling things around Northstar such as //! - getting version number pub mod install; +pub mod profile; use crate::util::check_ea_app_or_origin_running; use crate::{ diff --git a/src-tauri/src/northstar/profile.rs b/src-tauri/src/northstar/profile.rs new file mode 100644 index 00000000..78e734d0 --- /dev/null +++ b/src-tauri/src/northstar/profile.rs @@ -0,0 +1,76 @@ +use crate::GameInstall; + +// These folders are part of Titanfall 2 and +// should NEVER be used as a Profile +const SKIP_PATHS: [&str; 8] = [ + "___flightcore-temp", + "__overlay", + "bin", + "Core", + "r2", + "vpk", + "platform", + "Support", +]; + +// A profile may have one of these to be detected +const MAY_CONTAIN: [&str; 10] = [ + "mods/", + "plugins/", + "packages/", + "logs/", + "runtime/", + "save_data/", + "Northstar.dll", + "enabledmods.json", + "placeholder.playerdata.pdata", + "LEGAL.txt", +]; + +/// Returns a list of Profile names +/// All the returned Profiles can be found relative to the game path +#[tauri::command] +pub fn fetch_profiles(game_install: GameInstall) -> Result, String> { + let mut profiles: Vec = Vec::new(); + + for content in MAY_CONTAIN { + let pattern = format!("{}/*/{}", game_install.game_path, content); + for e in glob::glob(&pattern).expect("Failed to read glob pattern") { + let path = e.unwrap(); + let mut ancestors = path.ancestors(); + + ancestors.next(); + + let profile_path = std::path::Path::new(ancestors.next().unwrap()); + let profile_name = profile_path + .file_name() + .unwrap() + .to_os_string() + .into_string() + .unwrap(); + + if !profiles.contains(&profile_name) { + profiles.push(profile_name); + } + } + } + + Ok(profiles) +} + +/// Validates if a given profile is actually a valid profile +#[tauri::command] +pub fn validate_profile(game_install: GameInstall, profile: String) -> bool { + // Game files are never a valid profile + // Prevent users with messed up installs from making it even worse + if SKIP_PATHS.contains(&profile.as_str()) { + return false; + } + + log::info!("Validating Profile {}", profile); + + let profile_path = format!("{}/{}", game_install.game_path, profile); + let profile_dir = std::path::Path::new(profile_path.as_str()); + + profile_dir.is_dir() +} -- cgit v1.2.3