From 5cc4d2bab280bd640700516f987dd46e67c782d2 Mon Sep 17 00:00:00 2001 From: Niklas Kapelle Date: Sat, 7 Mar 2026 00:26:33 +0100 Subject: [PATCH] save instances in map & internalized save path --- src/main.rs | 11 ++--------- src/types/modded_instance.rs | 17 +++++++++++------ src/types/root_config.rs | 14 +++++--------- 3 files changed, 18 insertions(+), 24 deletions(-) diff --git a/src/main.rs b/src/main.rs index 5a46a24..1d24936 100644 --- a/src/main.rs +++ b/src/main.rs @@ -41,8 +41,7 @@ fn command_add(root_config: &RootConfig, instance_id: &str, mod_id: &str) -> any insert_mod_to_instance(&mut instance, mod_to_install, &files, 0); - let path = &root_config.get_instance_config(instance_id).unwrap().path; - instance.save_to_file(path)?; + instance.save_to_file()?; Ok(()) } @@ -55,13 +54,7 @@ fn command_order(root_config: &RootConfig, instance_id: &str) -> anyhow::Result< instance.set_load_order(new_load_order); - instance.save_to_file( - root_config - .get_instance_config(instance_id) - .ok_or(anyhow!("Failed to find instance"))? - .path - .clone(), - )?; + instance.save_to_file()?; Ok(()) } diff --git a/src/types/modded_instance.rs b/src/types/modded_instance.rs index 07fe86a..622750b 100644 --- a/src/types/modded_instance.rs +++ b/src/types/modded_instance.rs @@ -1,7 +1,7 @@ use std::{ fs::{self, read_to_string}, io::Write, - path::Path, + path::{Path, PathBuf}, }; use log::trace; @@ -22,6 +22,9 @@ pub struct ModdedInstance { #[serde(default)] game_file_overrides: Vec, + + #[serde(skip)] + self_path: PathBuf, } impl ModdedInstance { @@ -31,20 +34,22 @@ impl ModdedInstance { path.as_ref().to_string_lossy() ); - let data = read_to_string(path)?; - let config = toml::from_str(&data)?; + let data = read_to_string(&path)?; + let mut config: Self = toml::from_str(&data)?; + + config.self_path = path.as_ref().to_owned(); Ok(config) } - pub fn save_to_file(&self, path: impl AsRef) -> Result<(), ConfigReadWriteError> { + pub fn save_to_file(&self) -> Result<(), ConfigReadWriteError> { trace!( "Saving ModdedInstance to: {}", - path.as_ref().to_string_lossy() + self.self_path.to_string_lossy() ); let content = toml::to_string_pretty(self)?; - let mut file = fs::File::create(path)?; + let mut file = fs::File::create(&self.self_path)?; write!(file, "{}", content)?; Ok(()) } diff --git a/src/types/root_config.rs b/src/types/root_config.rs index c65a1f3..efbe6f0 100644 --- a/src/types/root_config.rs +++ b/src/types/root_config.rs @@ -18,7 +18,7 @@ pub struct RootConfig { mod_location: PathBuf, #[serde(default)] - instances: Vec, + instances: HashMap, /// All available mods #[serde(default)] @@ -48,23 +48,19 @@ impl RootConfig { pub fn load_instance_by_id(&self, id: &str) -> Result { let conf = self - .get_instance_config(id) + .instances + .get(id) .ok_or(ConfigReadWriteError::IDNotFound)?; ModdedInstance::load_from_file(&conf.path) } - pub fn get_instance_config(&self, id: &str) -> Option<&InstancePointer> { - self.instances.iter().find(|e| e.id == id) - } - pub fn mod_location(&self) -> &Path { &self.mod_location } } #[derive(Debug, Clone, Deserialize)] -pub struct InstancePointer { - pub id: String, - pub path: PathBuf, +struct InstancePointer { + path: PathBuf, }