diff options
author | GeckoEidechse <gecko.eidechse+git@pm.me> | 2023-07-16 19:11:12 +0200 |
---|---|---|
committer | GeckoEidechse <gecko.eidechse+git@pm.me> | 2023-07-16 19:11:12 +0200 |
commit | 2263d2c0136346d96ef1978fa872a2ffc613dcc0 (patch) | |
tree | 86ade7754f06b97527c3a54ae09be64ebef9e739 | |
parent | c8be2cc35d9440260e57c1169a8bffc81f16c2e9 (diff) | |
download | FlightCore-2263d2c0136346d96ef1978fa872a2ffc613dcc0.tar.gz FlightCore-2263d2c0136346d96ef1978fa872a2ffc613dcc0.zip |
commit current dirty working treetemp/dev
-rw-r--r-- | src-tauri/src/main.rs | 15 | ||||
-rw-r--r-- | src-tauri/src/mod_management/mod.rs | 200 |
2 files changed, 211 insertions, 4 deletions
diff --git a/src-tauri/src/main.rs b/src-tauri/src/main.rs index 5d71713a..71d5d6f0 100644 --- a/src-tauri/src/main.rs +++ b/src-tauri/src/main.rs @@ -477,6 +477,20 @@ pub struct GameInstall { pub install_type: InstallType, } +/// Describes whether a mod +/// - is a core mod +/// - follows the old legacy format +/// - was manually installe +/// - or is part of the newer package system +#[derive(Serialize, Deserialize, Debug, Clone, TS)] +#[ts(export)] +pub enum LegacyOrPackage { + CORE, + LEGACY, + MANUAL, + PACKAGE, +} + #[derive(Serialize, Deserialize, Debug, Clone, TS)] #[ts(export)] pub struct NorthstarMod { @@ -485,6 +499,7 @@ pub struct NorthstarMod { pub thunderstore_mod_string: Option<String>, pub enabled: bool, pub directory: String, + // pub install_type: LegacyOrPackage, } // I intend to add more linux related stuff to check here, so making a func diff --git a/src-tauri/src/mod_management/mod.rs b/src-tauri/src/mod_management/mod.rs index a2aca85a..2ca613ca 100644 --- a/src-tauri/src/mod_management/mod.rs +++ b/src-tauri/src/mod_management/mod.rs @@ -4,15 +4,17 @@ use crate::constants::{BLACKLISTED_MODS, CORE_MODS}; use async_recursion::async_recursion; use crate::NorthstarMod; -use anyhow::Result; +use anyhow::{anyhow, Result}; use serde::{Deserialize, Serialize}; +use std::str::FromStr; +use std::string::ToString; use std::{fs, path::PathBuf}; mod legacy; use crate::GameInstall; #[derive(Debug, Clone)] -struct ParsedThunderstoreModString { +pub struct ParsedThunderstoreModString { author_name: String, mod_name: String, version: String, @@ -22,6 +24,12 @@ impl std::str::FromStr for ParsedThunderstoreModString { type Err = &'static str; // todo use an better error management fn from_str(s: &str) -> Result<Self, Self::Err> { + // Check whether Thunderstore string passse reges + let re = regex::Regex::new(r"^[a-zA-Z0-9_]+-[a-zA-Z0-9_]+-\d+\.\d+\.\d++$").unwrap(); + if !re.is_match(s) { + return Err("Incorrect format"); + } + let mut parts = s.split('-'); let author_name = parts.next().ok_or("None value on author_name")?.to_string(); @@ -36,6 +44,12 @@ impl std::str::FromStr for ParsedThunderstoreModString { } } +impl ToString for ParsedThunderstoreModString { + fn to_string(&self) -> String { + format!("{}-{}-{}", self.author_name, self.mod_name, self.version) + } +} + #[derive(Serialize, Deserialize, Debug, Clone)] pub struct ThunderstoreManifest { name: String, @@ -170,6 +184,175 @@ pub fn set_mod_enabled_status( Ok(()) } +#[derive(Serialize, Deserialize, Debug, Clone)] +pub struct ModJson { + #[serde(rename = "Name")] + name: String, + #[serde(rename = "Version")] + version: Option<String>, +} + +/// Parse `mods` folder for installed mods. +pub fn parse_mods_in_package( + package_mods_path: PathBuf, + thunderstore_mod_string: ParsedThunderstoreModString, +) -> Result<Vec<NorthstarMod>, anyhow::Error> { + dbg!(package_mods_path.clone()); + let paths = match std::fs::read_dir(package_mods_path) { + Ok(paths) => paths, + Err(_err) => return Err(anyhow!("No mods folder found")), + }; + + let mut directories: Vec<PathBuf> = Vec::new(); + let mut mods: Vec<NorthstarMod> = Vec::new(); + + log::info!("----------- {paths:?}"); + + // Get list of folders in `mods` directory + for path in paths { + // dbg!(path); + log::info!("{path:?}"); + let my_path = path.unwrap().path(); + log::info!("{my_path:?}"); + + let md = std::fs::metadata(my_path.clone()).unwrap(); + if md.is_dir() { + directories.push(my_path); + } + } + + dbg!(directories.clone()); + // todo!(); + // Iterate over folders and check if they are Northstar mods + for directory in directories { + let directory_str = directory.to_str().unwrap().to_string(); + // Check if mod.json exists + let mod_json_path = format!("{}/mod.json", directory_str); + if !std::path::Path::new(&mod_json_path).exists() { + continue; + } + + // Parse mod.json and get 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) => { + log::warn!("Failed parsing {} with {}", mod_json_path, err.to_string()); + continue; + } + }; + + // Get directory path + let mod_directory = directory.to_str().unwrap().to_string(); + + let ns_mod = NorthstarMod { + name: parsed_mod_json.name, + version: parsed_mod_json.version, + thunderstore_mod_string: Some(thunderstore_mod_string.to_string()), + enabled: false, // Placeholder + directory: mod_directory, + }; + + mods.push(ns_mod); + } + + // Return found mod names + Ok(mods) +} + +/// Parse `packages` folder for installed mods. +pub fn parse_installed_package_mods( + game_install: &GameInstall, +) -> Result<Vec<NorthstarMod>, anyhow::Error> { + let mut collected_mods: Vec<NorthstarMod> = Vec::new(); + + let packages_folder = format!("{}/R2Northstar/packages/", game_install.game_path); + + let packages_dir = match fs::read_dir(packages_folder) { + Ok(res) => res, + Err(err) => { + // We couldn't read directory, probably cause it doesn't exist yet. + // In that case we just say no package mods installed. + log::warn!("{err}"); + return Ok(vec![]); + } + }; + + // Iteratore over folders in `packages` dir + for entry in packages_dir { + let entry = entry?; + let entry_path = entry.path(); + let entry_str = entry_path.file_name().unwrap().to_str().unwrap(); + log::warn!("Trying: {entry_str}"); + + // Use the struct's from_str function to verify format + if entry_path.is_dir() { + let package_thunderstore_string = match ParsedThunderstoreModString::from_str(entry_str) + { + Ok(res) => res, + Err(err) => { + log::warn!( + "Not a Thunderstore mod string \"{}\" cause: {}", + entry_path.display(), + err + ); + continue; + } + }; + let manifest_path = entry_path.join("manifest.json"); + let mods_path = entry_path.join("mods"); + + // Ensure `manifest.json` and `mods/` dir exist + if manifest_path.exists() && mods_path.is_dir() { + dbg!(mods_path.clone()); + + // Do something with mod path here + log::warn!("{}", mods_path.to_string_lossy()); + + // let paths = match std::fs::read_dir(mods_path) { + // Ok(paths) => paths, + // Err(_err) => todo!(), + // }; + + // // Get list of folders in `mods` directory + // for path in paths { + // log::info!("{path:?}"); + // let my_path = path.unwrap().path(); + // log::info!("{my_path:?}"); + + // let md = std::fs::metadata(my_path.clone()).unwrap(); + // if !md.is_dir() { + // continue; + // } + log::warn!("Found: {}", mods_path.display()); + let mods = + match parse_mods_in_package(mods_path, package_thunderstore_string.clone()) { + Ok(res) => res, + Err(err) => { + log::warn!("Failed parsing cause: {err}"); + continue; + } + }; + dbg!(mods.clone()); + collected_mods.extend(mods); + // } + + // let ns_mod = NorthstarMod { + // name: parsed_mod_json.name, + // version: parsed_mod_json.version, + // thunderstore_mod_string, + // enabled: false, // Placeholder + // directory: mod_directory, + // }; + } + } + } + + Ok(collected_mods) +} + /// Gets list of installed mods and their properties /// - name /// - is enabled? @@ -177,12 +360,21 @@ pub fn set_mod_enabled_status( pub fn get_installed_mods_and_properties( game_install: GameInstall, ) -> Result<Vec<NorthstarMod>, String> { - // Get actually installed mods - let found_installed_mods = match legacy::parse_installed_mods(&game_install) { + log::info!("{game_install:?}"); + // Get installed mods from packages + let mut found_installed_mods = match parse_installed_package_mods(&game_install) { + Ok(res) => res, + Err(err) => return Err(err.to_string()), + }; + // Get installed legacy mods + let found_installed_legacy_mods = match legacy::parse_installed_mods(&game_install) { Ok(res) => res, Err(err) => return Err(err.to_string()), }; + // Combine list of package and legacy mods + found_installed_mods.extend(found_installed_legacy_mods); + // Get enabled mods as JSON let enabled_mods: serde_json::Value = match get_enabled_mods(&game_install) { Ok(enabled_mods) => enabled_mods, |