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 crate::types::{Game, Link, ModdedInstance, RootConfig};
@@ -12,11 +12,7 @@ pub fn activate_instance(
instance: &ModdedInstance,
target: impl AsRef<Path>,
) -> 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)?;

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<()> {
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)?;

View File

@@ -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<Vec<Link>, io::Error> {
let links: Vec<Link> = walk_all_files(&self.install_location)?
.map(|entry| {
Link::new(
entry.path(),
entry.path().strip_prefix(&self.install_location).unwrap(),
)
})
let links: Vec<Link> = 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
}
}

View File

@@ -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<InstalledMod>,
@@ -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<Path>) -> Result<Self, ConfigReadWriteError> {
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
}

View File

@@ -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<Game>,
games: HashMap<String, Game>,
/// 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> {