From a5999f28eb61af8d2cf2074c98c447390b2ca057 Mon Sep 17 00:00:00 2001 From: Niklas Kapelle Date: Tue, 3 Mar 2026 22:12:45 +0100 Subject: [PATCH] refactored linking game --- src/basic_types.rs | 16 +++++++++------- src/linker.rs | 20 +++++++------------- src/main.rs | 2 +- 3 files changed, 17 insertions(+), 21 deletions(-) diff --git a/src/basic_types.rs b/src/basic_types.rs index cac73ff..75a83e7 100644 --- a/src/basic_types.rs +++ b/src/basic_types.rs @@ -13,6 +13,7 @@ use crate::{ utils::walk_files_recursive, }; +/// A link between a file from a mod and a destination in a ModdedInstance #[derive(Debug, Clone, Deserialize, Serialize, PartialEq)] #[serde(from = "(PathBuf, PathBuf)", into = "(PathBuf,PathBuf)")] pub struct Link { @@ -106,13 +107,14 @@ pub struct Game { } impl Game { - // pub fn export_links(&self) -> Result<, io::Error> { - // walk_files_recursive(&self.install_location) - // .unwrap() - // .map(|file| file.path()) - // .map(|path| ()); - // todo!() - // } + pub fn export_links(&self) -> Result, io::Error> { + let links: Vec = walk_files_recursive(&self.install_location) + .unwrap() + .map(|file| file.path()) + .map(|path| Link::new(&path, path.strip_prefix(&self.install_location).unwrap())) + .collect(); + Ok(links) + } } #[derive(Debug, Clone, Deserialize, PartialEq)] diff --git a/src/linker.rs b/src/linker.rs index 24e60e7..2253636 100644 --- a/src/linker.rs +++ b/src/linker.rs @@ -1,7 +1,7 @@ use log::debug; use crate::{ - basic_types::{ModdedInstance, RootConfig}, + basic_types::{Game, Link, ModdedInstance, RootConfig}, utils::walk_files_recursive, }; use std::io::Write; @@ -27,18 +27,12 @@ pub fn link_instance_to_target( Ok(()) } -pub fn link_game_to_target( - game_dir: impl AsRef, - target: impl AsRef, -) -> Result<(), io::Error> { - debug!("Linking install to {}", target.as_ref().to_string_lossy()); - walk_files_recursive(&game_dir)?.try_for_each(|file| { - let link_target = file.path(); - let link_name = target - .as_ref() - .join(file.path().strip_prefix(&game_dir).unwrap()); - link_file(&link_target, &link_name) - }) +pub fn link_game_to_target(game: &Game, target: impl AsRef) -> Result<(), io::Error> { + for link in game.export_links()? { + link_file(&link.src, &target.as_ref().join(&link.dst))?; + } + + Ok(()) } fn link_file(target: &Path, link_name: &Path) -> Result<(), io::Error> { diff --git a/src/main.rs b/src/main.rs index d84b959..f2073da 100644 --- a/src/main.rs +++ b/src/main.rs @@ -150,7 +150,7 @@ pub fn activate_instance( ) -> Result<(), Box> { let instance_config = root_config.load_instance_by_id(instance_id)?; - link_game_to_target(&root_config.games.first().unwrap().install_location, target)?; + link_game_to_target(root_config.games.first().unwrap(), target)?; link_instance_to_target(root_config, &instance_config, target)?; create_plugins_txt(&instance_config, target)?;