From afc3f68f36d1cab1f1e18396cb5fc4fae631f0eb Mon Sep 17 00:00:00 2001 From: Niklas Kapelle Date: Fri, 20 Mar 2026 13:10:07 +0100 Subject: [PATCH] fix missing GameType in root_config game --- src/types/game.rs | 8 ++++++-- src/types/mod_config.rs | 6 ++++++ src/types/root_config.rs | 42 +++++++++++++++++++++++++++++++++++----- 3 files changed, 49 insertions(+), 7 deletions(-) diff --git a/src/types/game.rs b/src/types/game.rs index 68ab620..684df33 100644 --- a/src/types/game.rs +++ b/src/types/game.rs @@ -19,10 +19,10 @@ pub struct Game { } impl Game { - pub fn new(path: impl AsRef) -> Self { + pub fn new(path: impl AsRef, game_type: GameType) -> Self { Self { path: path.as_ref().to_owned(), - kind: GameType::default(), + kind: game_type, } } @@ -45,4 +45,8 @@ impl Game { pub fn install_location(&self) -> &Path { &self.path } + + pub fn game_type(&self) -> GameType { + self.kind.clone() + } } diff --git a/src/types/mod_config.rs b/src/types/mod_config.rs index fcd50ef..296b20b 100644 --- a/src/types/mod_config.rs +++ b/src/types/mod_config.rs @@ -32,6 +32,8 @@ pub struct ModConfig { nexus_id: Option, + #[serde(default)] + #[serde(skip_serializing_if = "is_default")] game: GameType, } @@ -96,3 +98,7 @@ impl ModConfig { fn is_false(b: &bool) -> bool { !b } + +fn is_default(t: &T) -> bool { + t == &T::default() +} diff --git a/src/types/root_config.rs b/src/types/root_config.rs index a42387b..1656d11 100644 --- a/src/types/root_config.rs +++ b/src/types/root_config.rs @@ -57,6 +57,10 @@ impl RootConfig { } pub fn save_to_file(&self) -> Result<(), ConfigReadWriteError> { + debug!( + "Saving root_config to: {}", + self.self_path.to_string_lossy() + ); let content = toml::to_string_pretty(self)?; let mut file = fs::File::create(&self.self_path)?; write!(file, "{}", content)?; @@ -66,7 +70,13 @@ impl RootConfig { pub fn game_by_id(&self, id: &str) -> Option { self.games.get(id).map(|parsed_game| { if parsed_game.install_location().is_relative() { - Game::new(self.self_parent.join(parsed_game.install_location())) + let abs_path = self.self_parent.join(parsed_game.install_location()); + debug!( + "game path for {} is relative. Resolving to {}", + id, + abs_path.to_string_lossy() + ); + Game::new(abs_path, parsed_game.game_type()) } else { parsed_game.clone() } @@ -82,13 +92,19 @@ impl RootConfig { } pub fn load_instance_by_id(&self, id: &str) -> Result { + debug!("Loading instance {}", id); let conf = self .instances .get(id) .ok_or(ConfigReadWriteError::IDNotFound)?; if conf.path.is_relative() { - ModdedInstance::load_from_file(self.self_parent.join(&conf.path)) + let abs_path = self.self_parent.join(&conf.path); + debug!( + "instance path is relative. Resolving to {}", + abs_path.to_string_lossy() + ); + ModdedInstance::load_from_file(abs_path) } else { ModdedInstance::load_from_file(&conf.path) } @@ -96,7 +112,12 @@ impl RootConfig { pub fn mod_location(&self) -> PathBuf { if self.mod_location.is_relative() { - self.self_parent.join(&self.mod_location) + let abs_path = self.self_parent.join(&self.mod_location); + debug!( + "mod_location path is relative. Resolving to {}", + abs_path.to_string_lossy() + ); + abs_path } else { self.mod_location.clone() } @@ -109,7 +130,12 @@ impl RootConfig { pub fn download_location(&self) -> Option { self.download_location.as_ref().map(|e| { if e.is_relative() { - self.self_parent.join(e) + let abs_path = self.self_parent.join(e); + debug!( + "download_location path is relative. Resolving to {}", + abs_path.to_string_lossy() + ); + abs_path } else { e.clone() } @@ -124,11 +150,16 @@ struct InstancePointer { #[cfg(test)] mod tests { + use crate::types::GameType; + use super::*; fn create_config() -> RootConfig { RootConfig { - games: HashMap::from([("sse".to_owned(), Game::new("/games/sse"))]), + games: HashMap::from([( + "sse".to_owned(), + Game::new("/games/sse", GameType::SkyrimSE), + )]), mod_location: PathBuf::from("mods"), download_location: Some(PathBuf::from("download")), nexus_api_key: Some("1234".to_owned()), @@ -157,6 +188,7 @@ mod tests { let unwraped = game.expect("Asserted before"); assert_eq!(unwraped.install_location(), "/games/sse"); + assert_eq!(unwraped.game_type(), GameType::SkyrimSE); } #[test]