fixed instances now point to games

This commit is contained in:
2026-03-06 18:13:39 +01:00
parent 3da2a4a252
commit 6c5b212d1c
5 changed files with 16 additions and 32 deletions

View File

@@ -1,4 +1,4 @@
use anyhow::{Context, anyhow}; use anyhow::Context;
use log::{debug, trace}; use log::{debug, trace};
use crate::types::{Game, Link, ModdedInstance, RootConfig}; use crate::types::{Game, Link, ModdedInstance, RootConfig};
@@ -12,11 +12,7 @@ pub fn activate_instance(
instance: &ModdedInstance, instance: &ModdedInstance,
target: impl AsRef<Path>, target: impl AsRef<Path>,
) -> anyhow::Result<()> { ) -> anyhow::Result<()> {
// TODO: Resolve game for instance config let game = root_config.game_by_id(instance.game_id()).unwrap();
let game = root_config
.games()
.first()
.ok_or(anyhow!("TODO: resolve game from config"))?;
let resolved_links = resolve_links(root_config, instance, game)?; let resolved_links = resolve_links(root_config, instance, game)?;

View File

@@ -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<()> { fn command_order(root_config: &RootConfig, instance_id: &str) -> anyhow::Result<()> {
let mut instance = root_config.load_instance_by_id(instance_id)?; let mut instance = root_config.load_instance_by_id(instance_id)?;
let game = root_config let game = root_config.game_by_id(instance.game_id()).unwrap();
.games()
.first()
.ok_or(anyhow!("TODO: get game from instance"))?;
let new_load_order = load_order::create_loadorder(root_config, game, &instance)?; let new_load_order = load_order::create_loadorder(root_config, game, &instance)?;

View File

@@ -10,24 +10,19 @@ use crate::{types::link::Link, utils::walk_all_files};
/// Available game /// Available game
#[derive(Debug, Clone, Deserialize)] #[derive(Debug, Clone, Deserialize)]
pub struct Game { pub struct Game {
install_location: PathBuf, path: PathBuf,
} }
impl Game { impl Game {
pub fn export_links(&self) -> Result<Vec<Link>, io::Error> { pub fn export_links(&self) -> Result<Vec<Link>, io::Error> {
let links: Vec<Link> = walk_all_files(&self.install_location)? let links: Vec<Link> = walk_all_files(&self.path)?
.map(|entry| { .map(|entry| Link::new(entry.path(), entry.path().strip_prefix(&self.path).unwrap()))
Link::new(
entry.path(),
entry.path().strip_prefix(&self.install_location).unwrap(),
)
})
.collect(); .collect();
Ok(links) Ok(links)
} }
pub fn install_location(&self) -> &Path { pub fn install_location(&self) -> &Path {
&self.install_location &self.path
} }
} }

View File

@@ -12,7 +12,7 @@ use crate::types::{ConfigReadWriteError, installed_mod::InstalledMod, link::Link
/// An modded game with all plugins and files /// An modded game with all plugins and files
#[derive(Debug, Clone, Deserialize, Serialize)] #[derive(Debug, Clone, Deserialize, Serialize)]
pub struct ModdedInstance { pub struct ModdedInstance {
name: String, game: String,
#[serde(default)] #[serde(default)]
mods: Vec<InstalledMod>, mods: Vec<InstalledMod>,
@@ -25,15 +25,6 @@ pub struct ModdedInstance {
} }
impl 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<Path>) -> Result<Self, ConfigReadWriteError> { pub fn load_from_file(path: impl AsRef<Path>) -> Result<Self, ConfigReadWriteError> {
trace!( trace!(
"Loading ModdedInstance from file: {}", "Loading ModdedInstance from file: {}",
@@ -85,6 +76,10 @@ impl ModdedInstance {
} }
} }
pub fn game_id(&self) -> &str {
&self.game
}
pub fn mods(&self) -> &[InstalledMod] { pub fn mods(&self) -> &[InstalledMod] {
&self.mods &self.mods
} }

View File

@@ -1,4 +1,5 @@
use std::{ use std::{
collections::HashMap,
fs::read_to_string, fs::read_to_string,
path::{Path, PathBuf}, path::{Path, PathBuf},
}; };
@@ -11,7 +12,7 @@ use crate::types::{ConfigReadWriteError, ModConfig, game::Game, modded_instance:
#[derive(Debug, Clone, Deserialize)] #[derive(Debug, Clone, Deserialize)]
pub struct RootConfig { pub struct RootConfig {
/// Available games /// Available games
games: Vec<Game>, games: HashMap<String, Game>,
/// Where all mods are stored /// Where all mods are stored
mod_location: PathBuf, mod_location: PathBuf,
@@ -37,8 +38,8 @@ impl RootConfig {
Ok(config) Ok(config)
} }
pub fn games(&self) -> &[Game] { pub fn game_by_id(&self, id: &str) -> Option<&Game> {
&self.games self.games.get(id)
} }
pub fn get_mod_by_id(&self, id: &str) -> Option<&ModConfig> { pub fn get_mod_by_id(&self, id: &str) -> Option<&ModConfig> {