From 3b9ca4c0cb534bb04531d36440b93479198c1021 Mon Sep 17 00:00:00 2001 From: BrettMayson Date: Mon, 30 Oct 2023 16:01:29 -0600 Subject: [PATCH] binarize: copy files from addons folder --- bin/src/commands/launch.rs | 5 +---- bin/src/executor.rs | 26 ++++++++++++++++++++++++-- bin/src/modules/file_patching.rs | 9 ++------- bin/src/utils/link.rs | 11 +++++++---- bin/tests/alpha/addons/model.cfg | 1 + libs/common/src/project/addon/mod.rs | 1 + 6 files changed, 36 insertions(+), 17 deletions(-) create mode 100644 bin/tests/alpha/addons/model.cfg diff --git a/bin/src/commands/launch.rs b/bin/src/commands/launch.rs index c5deebfb..7e36cb76 100644 --- a/bin/src/commands/launch.rs +++ b/bin/src/commands/launch.rs @@ -113,10 +113,7 @@ pub fn execute(matches: &ArgMatches) -> Result<(), Error> { let link = prefix_folder.join(ctx.config().prefix()); if !link.exists() { - create_link( - link.display().to_string().as_str(), - ctx.build_folder().display().to_string().as_str(), - )?; + create_link(&link, ctx.build_folder())?; } let mut args: Vec = [ diff --git a/bin/src/executor.rs b/bin/src/executor.rs index ebf369f1..dc40a4ac 100644 --- a/bin/src/executor.rs +++ b/bin/src/executor.rs @@ -133,7 +133,29 @@ fn setup_tmp(ctx: &Context) -> Result<(), Error> { .trim_start_matches('/') .replace('/', "\\"), ); - create_link(tmp_addon.to_str().unwrap(), target.to_str().unwrap())?; + create_link(&tmp_addon, &target)?; + } + // maybe replace with config or rhai in the future? + let addons = ctx.project_folder().join("addons"); + for file in std::fs::read_dir(addons)? { + let file = file?.path(); + if file.is_dir() { + continue; + } + let tmp_file = tmp + .join(ctx.addons().first().unwrap().prefix().as_pathbuf()) + .parent() + .unwrap() + .join(file.file_name().unwrap()); + // check size of file + if file.metadata()?.len() > 1024 * 1024 * 10 { + warn!( + "File `{}` is larger than 10MB, this will slow builds.", + file.display() + ); + } + trace!("copying `{}` to tmp for binarization", file.display()); + std::fs::copy(&file, &tmp_file)?; } let include = ctx.project_folder().join("include"); if !include.exists() { @@ -148,7 +170,7 @@ fn setup_tmp(ctx: &Context) -> Result<(), Error> { if prefix.is_dir() { let tmp_mod = tmp_outer_prefix.join(prefix.file_name().unwrap()); create_dir_all(tmp_mod.parent().unwrap())?; - create_link(tmp_mod.to_str().unwrap(), prefix.to_str().unwrap())?; + create_link(&tmp_mod, &prefix)?; } } } diff --git a/bin/src/modules/file_patching.rs b/bin/src/modules/file_patching.rs index ba9c1ba9..172c0e73 100644 --- a/bin/src/modules/file_patching.rs +++ b/bin/src/modules/file_patching.rs @@ -22,13 +22,8 @@ impl Module for FilePatching { create_link( &ctx.build_folder() .join("addons") - .join(addon.name().replace('/', "\\")) - .display() - .to_string(), - &ctx.project_folder() - .join(addon.folder().replace('/', "\\")) - .display() - .to_string(), + .join(addon.name().replace('/', "\\")), + &ctx.project_folder().join(addon.folder().replace('/', "\\")), ) }) .collect::>() diff --git a/bin/src/utils/link.rs b/bin/src/utils/link.rs index 2dfc8edd..b795bc77 100644 --- a/bin/src/utils/link.rs +++ b/bin/src/utils/link.rs @@ -1,3 +1,5 @@ +use std::path::PathBuf; + use crate::error::Error; #[allow(clippy::module_name_repetitions)] @@ -7,11 +9,11 @@ use crate::error::Error; /// # Errors /// - [`Error::Link`] if the link could not be created /// - [`std::io::Error`] if the link command could not be executed -pub fn create_link(link: &str, target: &str) -> Result<(), Error> { +pub fn create_link(link: &PathBuf, target: &PathBuf) -> Result<(), Error> { use std::process::Command; - trace!("link {:?} => {:?}", link, target); // attempt junction + trace!("junction link {:?} => {:?}", link, target); let mut out = Command::new("cmd") .arg("/C") .arg("mklink") @@ -22,6 +24,7 @@ pub fn create_link(link: &str, target: &str) -> Result<(), Error> { if !out.status.success() { // fall-back to directory symbolic link + trace!("directory symbolic link {:?} => {:?}", link, target); out = Command::new("cmd") .arg("/C") .arg("mklink") @@ -45,9 +48,9 @@ pub fn create_link(link: &str, target: &str) -> Result<(), Error> { /// /// # Errors /// - [`std::io::Error`] if the link could not be created -pub fn create_link(link: &str, target: &str) -> Result<(), Error> { +pub fn create_link(link: &PathBuf, target: &str) -> Result<(), Error> { let target = target.replace('\\', "/"); - trace!("link {:?} => {:?}", link, target); + trace!("symlink {:?} => {:?}", link, target); std::os::unix::fs::symlink(target, link)?; Ok(()) } diff --git a/bin/tests/alpha/addons/model.cfg b/bin/tests/alpha/addons/model.cfg new file mode 100644 index 00000000..967fa6d5 --- /dev/null +++ b/bin/tests/alpha/addons/model.cfg @@ -0,0 +1 @@ +/shrug diff --git a/libs/common/src/project/addon/mod.rs b/libs/common/src/project/addon/mod.rs index ecf753af..d1bb582a 100644 --- a/libs/common/src/project/addon/mod.rs +++ b/libs/common/src/project/addon/mod.rs @@ -30,6 +30,7 @@ pub struct AddonConfig { /// Files to exclude from the pbo /// Supports glob patterns exclude: Vec, + #[serde(default)] /// Files to exclude from the pbo files: FilesConfig,