From 16ed5f9a46f752be7561236f94e2cedd00b283f0 Mon Sep 17 00:00:00 2001 From: Niklas Kapelle Date: Mon, 9 Mar 2026 23:50:12 +0100 Subject: [PATCH] improved root_config --- src/activator.rs | 2 +- src/load_order.rs | 2 +- src/main.rs | 2 +- src/types.rs | 4 +++- src/types/root_config.rs | 29 +++++++++++++++++++++++------ 5 files changed, 29 insertions(+), 10 deletions(-) diff --git a/src/activator.rs b/src/activator.rs index cf8022e..a99e831 100644 --- a/src/activator.rs +++ b/src/activator.rs @@ -65,7 +65,7 @@ fn resolve_link_for_instance( for installed_mod in instance.mods() { let mod_config = root_config - .get_mod_by_id(installed_mod.mod_id()) + .mod_by_id(installed_mod.mod_id()) .ok_or(ActivationError::ModNotFound)?; let mod_source_root = root_config.mod_location().join(mod_config.path()); diff --git a/src/load_order.rs b/src/load_order.rs index f9d3cf5..6bba889 100644 --- a/src/load_order.rs +++ b/src/load_order.rs @@ -50,7 +50,7 @@ pub fn create_loadorder( .mods() .iter() .flat_map(|installed_mod| { - let mod_config = root_config.get_mod_by_id(installed_mod.mod_id()).unwrap(); + let mod_config = root_config.mod_by_id(installed_mod.mod_id()).unwrap(); let mod_source_root = root_config.mod_location().join(mod_config.path()); installed_mod diff --git a/src/main.rs b/src/main.rs index 7371bb1..0274482 100644 --- a/src/main.rs +++ b/src/main.rs @@ -36,7 +36,7 @@ fn command_activate( fn command_add(root_config: &RootConfig, instance_id: &str, mod_id: &str) -> anyhow::Result<()> { let mut instance = root_config.load_instance_by_id(instance_id)?; let mod_to_install = root_config - .get_mod_by_id(mod_id) + .mod_by_id(mod_id) .ok_or(anyhow!("Can't find mod in config"))?; let files = files_to_install_mod(root_config, &instance, mod_to_install)?; diff --git a/src/types.rs b/src/types.rs index 3727a0a..ffed997 100644 --- a/src/types.rs +++ b/src/types.rs @@ -8,7 +8,6 @@ mod mod_file; mod modded_instance; mod root_config; - pub use game::*; pub use installed_mod::*; pub use link::*; @@ -30,4 +29,7 @@ pub enum ConfigReadWriteError { #[error("The provided ID could not be found")] IDNotFound, + + #[error("Could not determine the parent path of the file")] + NoParent, //fatty fatty no parents } diff --git a/src/types/root_config.rs b/src/types/root_config.rs index 0ff2217..430c35c 100644 --- a/src/types/root_config.rs +++ b/src/types/root_config.rs @@ -1,6 +1,6 @@ use std::{ collections::HashMap, - fs::read_to_string, + fs::{self, read_to_string}, path::{Path, PathBuf}, }; @@ -31,6 +31,9 @@ pub struct RootConfig { #[serde(skip)] self_path: PathBuf, + + #[serde(skip)] + self_parent: PathBuf, } impl RootConfig { @@ -43,16 +46,30 @@ impl RootConfig { let data = read_to_string(&path)?; let mut config: Self = toml::from_str(&data)?; - config.self_path = path.as_ref().to_owned(); + let absolute = fs::canonicalize(path.as_ref())?; + config.self_parent = absolute + .parent() + .ok_or(ConfigReadWriteError::NoParent)? + .to_owned(); + config.self_path = absolute; if config.mod_location.is_relative() { - config.mod_location = path.as_ref().join(config.mod_location).to_owned(); + config.mod_location = config.self_parent.join(config.mod_location).to_owned(); + debug!( + "Resolved mod_location to absolue path: {}", + config.mod_location.to_string_lossy() + ); } if let Some(dl_location) = &config.download_location && dl_location.is_relative() { - config.download_location = Some(path.as_ref().join(dl_location).to_owned()); + let dl_abs_path = config.self_parent.join(dl_location).to_owned(); + debug!( + "Resolve download_location to absolute path {}", + dl_abs_path.to_string_lossy() + ); + config.download_location = Some(dl_abs_path); } Ok(config) @@ -62,7 +79,7 @@ impl RootConfig { self.games.get(id) } - pub fn get_mod_by_id(&self, id: &str) -> Option<&ModConfig> { + pub fn mod_by_id(&self, id: &str) -> Option<&ModConfig> { self.mods.iter().find(|e| e.id() == id) } @@ -73,7 +90,7 @@ impl RootConfig { .ok_or(ConfigReadWriteError::IDNotFound)?; if conf.path.is_relative() { - ModdedInstance::load_from_file(self.self_path.join(&conf.path)) + ModdedInstance::load_from_file(self.self_parent.join(&conf.path)) } else { ModdedInstance::load_from_file(&conf.path) }