fix missing GameType in root_config game

This commit is contained in:
2026-03-20 13:10:07 +01:00
parent fcc65f68bb
commit afc3f68f36
3 changed files with 49 additions and 7 deletions

View File

@@ -19,10 +19,10 @@ pub struct Game {
} }
impl Game { impl Game {
pub fn new(path: impl AsRef<Path>) -> Self { pub fn new(path: impl AsRef<Path>, game_type: GameType) -> Self {
Self { Self {
path: path.as_ref().to_owned(), path: path.as_ref().to_owned(),
kind: GameType::default(), kind: game_type,
} }
} }
@@ -45,4 +45,8 @@ impl Game {
pub fn install_location(&self) -> &Path { pub fn install_location(&self) -> &Path {
&self.path &self.path
} }
pub fn game_type(&self) -> GameType {
self.kind.clone()
}
} }

View File

@@ -32,6 +32,8 @@ pub struct ModConfig {
nexus_id: Option<NexusID>, nexus_id: Option<NexusID>,
#[serde(default)]
#[serde(skip_serializing_if = "is_default")]
game: GameType, game: GameType,
} }
@@ -96,3 +98,7 @@ impl ModConfig {
fn is_false(b: &bool) -> bool { fn is_false(b: &bool) -> bool {
!b !b
} }
fn is_default<T: Default + PartialEq>(t: &T) -> bool {
t == &T::default()
}

View File

@@ -57,6 +57,10 @@ impl RootConfig {
} }
pub fn save_to_file(&self) -> Result<(), ConfigReadWriteError> { 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 content = toml::to_string_pretty(self)?;
let mut file = fs::File::create(&self.self_path)?; let mut file = fs::File::create(&self.self_path)?;
write!(file, "{}", content)?; write!(file, "{}", content)?;
@@ -66,7 +70,13 @@ impl RootConfig {
pub fn game_by_id(&self, id: &str) -> Option<Game> { pub fn game_by_id(&self, id: &str) -> Option<Game> {
self.games.get(id).map(|parsed_game| { self.games.get(id).map(|parsed_game| {
if parsed_game.install_location().is_relative() { 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 { } else {
parsed_game.clone() parsed_game.clone()
} }
@@ -82,13 +92,19 @@ impl RootConfig {
} }
pub fn load_instance_by_id(&self, id: &str) -> Result<ModdedInstance, ConfigReadWriteError> { pub fn load_instance_by_id(&self, id: &str) -> Result<ModdedInstance, ConfigReadWriteError> {
debug!("Loading instance {}", id);
let conf = self let conf = self
.instances .instances
.get(id) .get(id)
.ok_or(ConfigReadWriteError::IDNotFound)?; .ok_or(ConfigReadWriteError::IDNotFound)?;
if conf.path.is_relative() { 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 { } else {
ModdedInstance::load_from_file(&conf.path) ModdedInstance::load_from_file(&conf.path)
} }
@@ -96,7 +112,12 @@ impl RootConfig {
pub fn mod_location(&self) -> PathBuf { pub fn mod_location(&self) -> PathBuf {
if self.mod_location.is_relative() { 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 { } else {
self.mod_location.clone() self.mod_location.clone()
} }
@@ -109,7 +130,12 @@ impl RootConfig {
pub fn download_location(&self) -> Option<PathBuf> { pub fn download_location(&self) -> Option<PathBuf> {
self.download_location.as_ref().map(|e| { self.download_location.as_ref().map(|e| {
if e.is_relative() { 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 { } else {
e.clone() e.clone()
} }
@@ -124,11 +150,16 @@ struct InstancePointer {
#[cfg(test)] #[cfg(test)]
mod tests { mod tests {
use crate::types::GameType;
use super::*; use super::*;
fn create_config() -> RootConfig { fn create_config() -> RootConfig {
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"), mod_location: PathBuf::from("mods"),
download_location: Some(PathBuf::from("download")), download_location: Some(PathBuf::from("download")),
nexus_api_key: Some("1234".to_owned()), nexus_api_key: Some("1234".to_owned()),
@@ -157,6 +188,7 @@ mod tests {
let unwraped = game.expect("Asserted before"); let unwraped = game.expect("Asserted before");
assert_eq!(unwraped.install_location(), "/games/sse"); assert_eq!(unwraped.install_location(), "/games/sse");
assert_eq!(unwraped.game_type(), GameType::SkyrimSE);
} }
#[test] #[test]