diff --git a/src/activator.rs b/src/activator.rs index 441d940..c73b6e0 100644 --- a/src/activator.rs +++ b/src/activator.rs @@ -1,4 +1,4 @@ -use anyhow::{Context, anyhow}; +use anyhow::Context; use log::{debug, trace}; use crate::types::{Game, Link, ModdedInstance, RootConfig}; @@ -12,11 +12,7 @@ pub fn activate_instance( instance: &ModdedInstance, target: impl AsRef, ) -> anyhow::Result<()> { - // TODO: Resolve game for instance config - let game = root_config - .games() - .first() - .ok_or(anyhow!("TODO: resolve game from config"))?; + let game = root_config.game_by_id(instance.game_id()).unwrap(); let resolved_links = resolve_links(root_config, instance, game)?; diff --git a/src/main.rs b/src/main.rs index f16373a..5a46a24 100644 --- a/src/main.rs +++ b/src/main.rs @@ -49,10 +49,7 @@ fn command_add(root_config: &RootConfig, instance_id: &str, mod_id: &str) -> any fn command_order(root_config: &RootConfig, instance_id: &str) -> anyhow::Result<()> { let mut instance = root_config.load_instance_by_id(instance_id)?; - let game = root_config - .games() - .first() - .ok_or(anyhow!("TODO: get game from instance"))?; + let game = root_config.game_by_id(instance.game_id()).unwrap(); let new_load_order = load_order::create_loadorder(root_config, game, &instance)?; diff --git a/src/types/game.rs b/src/types/game.rs index af66b3d..3a759e9 100644 --- a/src/types/game.rs +++ b/src/types/game.rs @@ -10,24 +10,19 @@ use crate::{types::link::Link, utils::walk_all_files}; /// Available game #[derive(Debug, Clone, Deserialize)] pub struct Game { - install_location: PathBuf, + path: PathBuf, } impl Game { pub fn export_links(&self) -> Result, io::Error> { - let links: Vec = walk_all_files(&self.install_location)? - .map(|entry| { - Link::new( - entry.path(), - entry.path().strip_prefix(&self.install_location).unwrap(), - ) - }) + let links: Vec = walk_all_files(&self.path)? + .map(|entry| Link::new(entry.path(), entry.path().strip_prefix(&self.path).unwrap())) .collect(); Ok(links) } pub fn install_location(&self) -> &Path { - &self.install_location + &self.path } } diff --git a/src/types/modded_instance.rs b/src/types/modded_instance.rs index a872627..07fe86a 100644 --- a/src/types/modded_instance.rs +++ b/src/types/modded_instance.rs @@ -12,7 +12,7 @@ use crate::types::{ConfigReadWriteError, installed_mod::InstalledMod, link::Link /// An modded game with all plugins and files #[derive(Debug, Clone, Deserialize, Serialize)] pub struct ModdedInstance { - name: String, + game: String, #[serde(default)] mods: Vec, @@ -25,15 +25,6 @@ pub struct ModdedInstance { } impl ModdedInstance { - pub fn new(name: &str) -> Self { - Self { - name: name.to_owned(), - mods: Vec::new(), - load_order: Vec::new(), - game_file_overrides: Vec::new(), - } - } - pub fn load_from_file(path: impl AsRef) -> Result { trace!( "Loading ModdedInstance from file: {}", @@ -85,6 +76,10 @@ impl ModdedInstance { } } + pub fn game_id(&self) -> &str { + &self.game + } + pub fn mods(&self) -> &[InstalledMod] { &self.mods } diff --git a/src/types/root_config.rs b/src/types/root_config.rs index e2b8c84..c65a1f3 100644 --- a/src/types/root_config.rs +++ b/src/types/root_config.rs @@ -1,4 +1,5 @@ use std::{ + collections::HashMap, fs::read_to_string, path::{Path, PathBuf}, }; @@ -11,7 +12,7 @@ use crate::types::{ConfigReadWriteError, ModConfig, game::Game, modded_instance: #[derive(Debug, Clone, Deserialize)] pub struct RootConfig { /// Available games - games: Vec, + games: HashMap, /// Where all mods are stored mod_location: PathBuf, @@ -37,8 +38,8 @@ impl RootConfig { Ok(config) } - pub fn games(&self) -> &[Game] { - &self.games + pub fn game_by_id(&self, id: &str) -> Option<&Game> { + self.games.get(id) } pub fn get_mod_by_id(&self, id: &str) -> Option<&ModConfig> {