improved root_config

This commit is contained in:
2026-03-09 23:50:12 +01:00
parent 22c5c7ee91
commit 16ed5f9a46
5 changed files with 29 additions and 10 deletions

View File

@@ -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());

View File

@@ -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

View File

@@ -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)?;

View File

@@ -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
}

View File

@@ -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)
}