refactored linking game

This commit is contained in:
2026-03-03 22:12:45 +01:00
parent e9b7aedb6f
commit a5999f28eb
3 changed files with 17 additions and 21 deletions

View File

@@ -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<Vec<Link>, io::Error> {
let links: Vec<Link> = 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)]

View File

@@ -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<Path>,
target: impl AsRef<Path>,
) -> 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<Path>) -> 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> {

View File

@@ -150,7 +150,7 @@ pub fn activate_instance(
) -> Result<(), Box<dyn Error>> {
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)?;