From 15757f800fffe9823ff64616f65d0b2432fb7249 Mon Sep 17 00:00:00 2001 From: BrettMayson Date: Sat, 7 Jan 2023 04:11:55 -0600 Subject: [PATCH] per addon config --- Cargo.lock | 86 ++++++++----------- Cargo.toml | 8 +- bin/app/Cargo.toml | 4 +- bin/app/src/addons.rs | 24 +++++- bin/app/src/commands/build.rs | 2 +- bin/app/src/commands/launch.rs | 2 +- bin/app/src/context.rs | 2 +- bin/app/src/modules/binarize.rs | 17 ++++ bin/app/src/modules/preprocessor.rs | 11 ++- bin/libs/{project => config}/Cargo.toml | 5 +- bin/libs/config/src/addon/mod.rs | 64 ++++++++++++++ bin/libs/{project => config}/src/lib.rs | 4 +- .../src => config/src/project}/hemtt.rs | 0 .../src/config => config/src/project}/mod.rs | 7 +- .../config => config/src/project}/signing.rs | 0 .../config => config/src/project}/version.rs | 0 bin/libs/error/Cargo.toml | 2 +- bin/libs/error/src/lib.rs | 2 + book/SUMMARY.md | 1 + book/configuration-addon.md | 24 ++++++ book/configuration-version.md | 2 +- 21 files changed, 195 insertions(+), 72 deletions(-) rename bin/libs/{project => config}/Cargo.toml (86%) create mode 100644 bin/libs/config/src/addon/mod.rs rename bin/libs/{project => config}/src/lib.rs (74%) rename bin/libs/{project/src => config/src/project}/hemtt.rs (100%) rename bin/libs/{project/src/config => config/src/project}/mod.rs (96%) rename bin/libs/{project/src/config => config/src/project}/signing.rs (100%) rename bin/libs/{project/src/config => config/src/project}/version.rs (100%) create mode 100644 book/configuration-addon.md diff --git a/Cargo.lock b/Cargo.lock index 3ff6dc40..61cbdaad 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -93,9 +93,9 @@ checksum = "14c189c53d098945499cdfa7ecc63567cf3886b3332b312a5b4585d8d3a6a610" [[package]] name = "bzip2" -version = "0.4.3" +version = "0.4.4" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "6afcd980b5f3a45017c57e57a2fcccbb351cc43a356ce117ef760ef8052b89b0" +checksum = "bdb116a6ef3f6c3698828873ad02c3014b3c85cadb88496095628e3ef1e347f8" dependencies = [ "bzip2-sys", "libc", @@ -225,16 +225,15 @@ dependencies = [ [[package]] name = "console" -version = "0.15.2" +version = "0.15.4" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "c050367d967ced717c04b65d8c619d863ef9292ce0c5760028655a2fb298718c" +checksum = "c9b6515d269224923b26b5febea2ed42b2d5f2ce37284a4dd670fedd6cb8347a" dependencies = [ "encode_unicode", "lazy_static", "libc", - "terminal_size", "unicode-width", - "winapi", + "windows-sys", ] [[package]] @@ -515,9 +514,9 @@ dependencies = [ [[package]] name = "glob" -version = "0.3.0" +version = "0.3.1" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "9b919933a397b79c37e33b77bb2aa3dc8eb6e165ad809e58ff75bc7db2e34574" +checksum = "d2fabcfbdc87f4758337ca535fb41a6d701b65693ce38287d856d1674551ec9b" [[package]] name = "half" @@ -542,9 +541,9 @@ dependencies = [ "fs_extra", "git2", "glob", + "hemtt-bin-config", "hemtt-bin-error", "hemtt-bin-internal", - "hemtt-bin-project", "hemtt-config", "hemtt-error", "hemtt-pbo", @@ -563,11 +562,12 @@ dependencies = [ ] [[package]] -name = "hemtt-bin-error" +name = "hemtt-bin-config" version = "0.10.0" dependencies = [ "git2", "glob", + "hemtt-bin-error", "hemtt-config", "hemtt-error", "hemtt-pbo", @@ -576,34 +576,34 @@ dependencies = [ "hemtt-version", "serde", "toml", - "vfs", ] [[package]] -name = "hemtt-bin-internal" +name = "hemtt-bin-error" version = "0.10.0" dependencies = [ - "clap 4.0.32", + "git2", + "glob", "hemtt-config", "hemtt-error", + "hemtt-pbo", "hemtt-preprocessor", - "peekmore", + "hemtt-signing", + "hemtt-version", + "serde", + "toml", + "vfs", ] [[package]] -name = "hemtt-bin-project" +name = "hemtt-bin-internal" version = "0.10.0" dependencies = [ - "git2", - "hemtt-bin-error", + "clap 4.0.32", "hemtt-config", "hemtt-error", - "hemtt-pbo", "hemtt-preprocessor", - "hemtt-signing", - "hemtt-version", - "serde", - "toml", + "peekmore", ] [[package]] @@ -983,9 +983,9 @@ dependencies = [ [[package]] name = "once_cell" -version = "1.16.0" +version = "1.17.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "86f0b0d4bf799edbc74508c1e8bf170ff5f41238e5f8225603ca7caaae2b7860" +checksum = "6f61fba1741ea2b3d6a1e3178721804bb716a68a6aeba1149b5d52e3d464ea66" [[package]] name = "oorandom" @@ -1076,9 +1076,9 @@ checksum = "478c572c3d73181ff3c2539045f6eb99e5491218eae919370993b890cdbdd98e" [[package]] name = "pest" -version = "2.5.1" +version = "2.5.2" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "cc8bed3549e0f9b0a2a78bf7c0018237a2cdf085eecbbc048e52612438e4e9d0" +checksum = "0f6e86fb9e7026527a0d46bc308b841d73170ef8f443e1807f6ef88526a816d4" dependencies = [ "thiserror", "ucd-trie", @@ -1086,9 +1086,9 @@ dependencies = [ [[package]] name = "pest_derive" -version = "2.5.1" +version = "2.5.2" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "cdc078600d06ff90d4ed238f0119d84ab5d43dbaad278b0e33a8820293b32344" +checksum = "96504449aa860c8dcde14f9fba5c58dc6658688ca1fe363589d6327b8662c603" dependencies = [ "pest", "pest_generator", @@ -1096,9 +1096,9 @@ dependencies = [ [[package]] name = "pest_generator" -version = "2.5.1" +version = "2.5.2" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "28a1af60b1c4148bb269006a750cff8e2ea36aff34d2d96cf7be0b14d1bed23c" +checksum = "798e0220d1111ae63d66cb66a5dcb3fc2d986d520b98e49e1852bfdb11d7c5e7" dependencies = [ "pest", "pest_meta", @@ -1109,9 +1109,9 @@ dependencies = [ [[package]] name = "pest_meta" -version = "2.5.1" +version = "2.5.2" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "fec8605d59fc2ae0c6c1aefc0c7c7a9769732017c0ce07f7a9cfffa7b4404f20" +checksum = "984298b75898e30a843e278a9f2452c31e349a073a0ce6fd950a12a74464e065" dependencies = [ "once_cell", "pest", @@ -1353,9 +1353,9 @@ dependencies = [ [[package]] name = "rustix" -version = "0.36.5" +version = "0.36.6" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "a3807b5d10909833d3e9acd1eb5fb988f79376ff10fce42937de71a449c4c588" +checksum = "4feacf7db682c6c329c4ede12649cd36ecab0f3be5b7d74e6a20304725db4549" dependencies = [ "bitflags", "errno", @@ -1388,18 +1388,18 @@ checksum = "d29ab0c6d3fc0ee92fe66e2d99f700eab17a8d57d1c1d3b748380fb20baa78cd" [[package]] name = "serde" -version = "1.0.151" +version = "1.0.152" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "97fed41fc1a24994d044e6db6935e69511a1153b52c15eb42493b26fa87feba0" +checksum = "bb7d1f0d3021d347a83e556fc4683dea2ea09d87bccdf88ff5c12545d89d5efb" dependencies = [ "serde_derive", ] [[package]] name = "serde_derive" -version = "1.0.151" +version = "1.0.152" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "255abe9a125a985c05190d687b320c12f9b1f0b99445e608c21ba0782c719ad8" +checksum = "af487d118eecd09402d70a5d72551860e788df87b464af30e5ea6a38c75c541e" dependencies = [ "proc-macro2", "quote", @@ -1540,16 +1540,6 @@ dependencies = [ "winapi-util", ] -[[package]] -name = "terminal_size" -version = "0.1.17" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "633c1a546cee861a1a6d0dc69ebeca693bf4296661ba7852b9d21d159e0506df" -dependencies = [ - "libc", - "winapi", -] - [[package]] name = "textwrap" version = "0.16.0" diff --git a/Cargo.toml b/Cargo.toml index debda392..c3994280 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,9 +1,9 @@ [workspace] members = [ "bin/app", + "bin/libs/config", "bin/libs/error", "bin/libs/internal", - "bin/libs/project", "libs/config", "libs/error", @@ -16,8 +16,8 @@ members = [ ] [workspace.dependencies] -pest = "2.5.1" -pest_derive = "2.5.1" +pest = "2.5.2" +pest_derive = "2.5.2" clap = "4.0.32" -serde = { version = "1.0.151", features = ["derive"] } +serde = { version = "1.0.152", features = ["derive"] } sha-1 = "0.10.1" diff --git a/bin/app/Cargo.toml b/bin/app/Cargo.toml index 00d00d35..2e18ee96 100644 --- a/bin/app/Cargo.toml +++ b/bin/app/Cargo.toml @@ -9,7 +9,7 @@ hemtt-error = { path = "../../libs/error" } hemtt-bin-error = { path = "../libs/error"} hemtt-bin-internal = { path = "../libs/internal" } -hemtt-bin-project = { path = "../libs/project" } +hemtt-bin-config = { path = "../libs/config" } hemtt-config = { path = "../../libs/config" } hemtt-pbo = { path = "../../libs/pbo" } @@ -23,7 +23,7 @@ clap = { workspace = true } dialoguer = "0.10.2" fs_extra = "1.2.0" git2 = "0.15.0" -glob = "0.3.0" +glob = "0.3.1" lazy_static = "1.4.0" peekmore = "1.0.0" rayon = "1.6.1" diff --git a/bin/app/src/addons.rs b/bin/app/src/addons.rs index 01379d05..155ce952 100644 --- a/bin/app/src/addons.rs +++ b/bin/app/src/addons.rs @@ -1,16 +1,30 @@ use std::{fs::DirEntry, path::PathBuf, str::FromStr}; +use hemtt_bin_config::addon::Configuration; use hemtt_bin_error::Error; -#[derive(Debug, Clone, PartialEq, Eq)] +#[derive(Debug, Clone)] pub struct Addon { name: String, location: Location, + config: Option, } impl Addon { - pub const fn new(name: String, location: Location) -> Self { - Self { name, location } + pub fn new(name: String, location: Location) -> Self { + Self { + config: { + let path = + PathBuf::from(format!("{}/{}", location.to_string(), name)).join("hemtt.toml"); + if path.exists() { + Some(Configuration::from_file(&path).unwrap()) + } else { + None + } + }, + location, + name, + } } pub fn name(&self) -> &str { @@ -29,6 +43,10 @@ impl Addon { format!("{}/{}", self.location.to_string(), self.name) } + pub const fn config(&self) -> Option<&Configuration> { + self.config.as_ref() + } + pub fn scan() -> Result, Error> { let mut addons = Vec::new(); for location in [Location::Addons, Location::Optionals] { diff --git a/bin/app/src/commands/build.rs b/bin/app/src/commands/build.rs index 5842d18e..37856b47 100644 --- a/bin/app/src/commands/build.rs +++ b/bin/app/src/commands/build.rs @@ -1,4 +1,4 @@ -use clap::{ArgMatches, Command, ArgAction}; +use clap::{ArgAction, ArgMatches, Command}; use hemtt_bin_error::Error; use crate::{ diff --git a/bin/app/src/commands/launch.rs b/bin/app/src/commands/launch.rs index abef978e..0beb6cb1 100644 --- a/bin/app/src/commands/launch.rs +++ b/bin/app/src/commands/launch.rs @@ -1,8 +1,8 @@ use std::path::Path; use clap::{ArgMatches, Command}; +use hemtt_bin_config::project::Configuration; use hemtt_bin_error::Error; -use hemtt_bin_project::config::Configuration; use steamlocate::SteamDir; use crate::utils::create_link; diff --git a/bin/app/src/context.rs b/bin/app/src/context.rs index 5f08320d..9f3c6c4a 100644 --- a/bin/app/src/context.rs +++ b/bin/app/src/context.rs @@ -4,8 +4,8 @@ use std::{ path::{Path, PathBuf}, }; +use hemtt_bin_config::project::Configuration; use hemtt_bin_error::Error; -use hemtt_bin_project::config::Configuration; use vfs::{AltrootFS, MemoryFS, OverlayFS, PhysicalFS, VfsPath}; use crate::addons::Addon; diff --git a/bin/app/src/modules/binarize.rs b/bin/app/src/modules/binarize.rs index 4b981952..1f72bb10 100644 --- a/bin/app/src/modules/binarize.rs +++ b/bin/app/src/modules/binarize.rs @@ -99,6 +99,23 @@ impl Module for Binarize { if entry.metadata().unwrap().file_type == VfsFileType::File && ["rtm", "p3d"].contains(&entry.extension().unwrap_or_default().as_str()) { + if let Some(config) = addon.config() { + if config + .no_bin(&addon.folder())? + .contains(&PathBuf::from(entry.as_str().trim_start_matches('/'))) + { + println!("skipping binarization of {}", entry.as_str()); + continue; + } + } + + // skip OLOD & BMTR files as they are already binarized + let mut buf = [0; 4]; + entry.open_file().unwrap().read_exact(&mut buf).unwrap(); + if buf == [0x4F, 0x4C, 0x4F, 0x44] || buf == [0x42, 0x4D, 0x54, 0x52] { + continue; + } + let addon_root = PathBuf::from(entry.as_str()) .components() .take(3) diff --git a/bin/app/src/modules/preprocessor.rs b/bin/app/src/modules/preprocessor.rs index ccbec86b..cf3800b0 100644 --- a/bin/app/src/modules/preprocessor.rs +++ b/bin/app/src/modules/preprocessor.rs @@ -28,18 +28,25 @@ impl Module for Preprocessor { fn pre_build(&self, ctx: &Context) -> Result<(), Error> { let resolver = VfsResolver::new(ctx)?; // TODO map to extra error + // TODO ^ remember what that means ctx.addons() .par_iter() .map(|addon| { - // TODO fix error in vfs for entry in ctx.vfs().join(addon.folder())?.walk_dir()? { let entry = entry?; if entry.metadata()?.file_type == VfsFileType::File && can_preprocess(entry.as_str()) { + if entry.filename() == "config.cpp" { + if let Some(config) = addon.config() { + if !config.preprocess() { + println!("skiping {}", entry.as_str()); + continue; + } + } + } println!("preprocessing {}", entry.as_str()); preprocess(entry.clone(), ctx, &resolver)?; - println!("done {}", entry.as_str()); } } Ok(()) diff --git a/bin/libs/project/Cargo.toml b/bin/libs/config/Cargo.toml similarity index 86% rename from bin/libs/project/Cargo.toml rename to bin/libs/config/Cargo.toml index 0669d2e7..b95416e5 100644 --- a/bin/libs/project/Cargo.toml +++ b/bin/libs/config/Cargo.toml @@ -1,6 +1,6 @@ [package] -name = "hemtt-bin-project" -description = "HEMTT - Project Configuration" +name = "hemtt-bin-config" +description = "HEMTT - Configuration" version = "0.10.0" edition = "2021" @@ -15,5 +15,6 @@ hemtt-signing = { path = "../../../libs/signing" } hemtt-version = { path = "../../../libs/version" } git2 = "0.15.0" +glob = "0.3.1" serde = { workspace = true, features = ["derive"] } toml = { version = "0.5.10" } diff --git a/bin/libs/config/src/addon/mod.rs b/bin/libs/config/src/addon/mod.rs new file mode 100644 index 00000000..e9f9fc51 --- /dev/null +++ b/bin/libs/config/src/addon/mod.rs @@ -0,0 +1,64 @@ +use std::{ + path::{Path, PathBuf}, + str::FromStr, +}; + +use hemtt_bin_error::Error; +use serde::{Deserialize, Serialize}; + +#[derive(Debug, Clone, Serialize, Deserialize)] +pub struct Configuration { + #[serde(default)] + /// Whether to preprocess config.cpp + preprocess: Option, + + #[serde(default)] + /// A list of files to skip binarizing + no_bin: Vec, +} + +impl Configuration { + #[must_use] + pub const fn preprocess(&self) -> bool { + if let Some(preprocess) = self.preprocess { + preprocess + } else { + true + } + } + + /// A list of files to skip binarizing + /// + /// # Errors + /// + /// [`Error::GlobError`] if a glob pattern is invalid + pub fn no_bin(&self, root: &str) -> Result, Error> { + self.no_bin + .iter() + .map(|f| glob::glob(format!("{root}/{f}").as_str())) + .collect::, glob::PatternError>>()? + .into_iter() + .flatten() + .collect::, _>>() + .map_err(Error::from) + } + + /// Load a configuration from a file. + /// + /// # Errors + /// + /// If the file cannot be read, or if the file is not valid TOML, or if the + /// file does not contain a valid configuration, an error is returned. + pub fn from_file(path: &Path) -> Result { + let file = std::fs::read_to_string(path)?; + Self::from_str(&file) + } +} + +impl FromStr for Configuration { + type Err = Error; + + fn from_str(s: &str) -> Result { + toml::from_str(s).map_err(Error::from) + } +} diff --git a/bin/libs/project/src/lib.rs b/bin/libs/config/src/lib.rs similarity index 74% rename from bin/libs/project/src/lib.rs rename to bin/libs/config/src/lib.rs index ea1c96d8..1d62f9e6 100644 --- a/bin/libs/project/src/lib.rs +++ b/bin/libs/config/src/lib.rs @@ -2,5 +2,5 @@ #![warn(clippy::pedantic)] #![allow(clippy::use_self)] -pub mod config; -pub mod hemtt; +pub mod addon; +pub mod project; diff --git a/bin/libs/project/src/hemtt.rs b/bin/libs/config/src/project/hemtt.rs similarity index 100% rename from bin/libs/project/src/hemtt.rs rename to bin/libs/config/src/project/hemtt.rs diff --git a/bin/libs/project/src/config/mod.rs b/bin/libs/config/src/project/mod.rs similarity index 96% rename from bin/libs/project/src/config/mod.rs rename to bin/libs/config/src/project/mod.rs index a255577f..9d0db99d 100644 --- a/bin/libs/project/src/config/mod.rs +++ b/bin/libs/config/src/project/mod.rs @@ -3,8 +3,7 @@ use std::{collections::HashMap, path::Path, str::FromStr}; use hemtt_bin_error::Error; use serde::{Deserialize, Serialize}; -use crate::hemtt::Features; - +mod hemtt; mod signing; mod version; @@ -30,7 +29,7 @@ pub struct Configuration { files: Vec, #[serde(default)] - hemtt: Features, + hemtt: hemtt::Features, #[serde(default)] signing: signing::Options, @@ -88,7 +87,7 @@ impl Configuration { } #[must_use] - pub const fn hemtt(&self) -> &Features { + pub const fn hemtt(&self) -> &hemtt::Features { &self.hemtt } diff --git a/bin/libs/project/src/config/signing.rs b/bin/libs/config/src/project/signing.rs similarity index 100% rename from bin/libs/project/src/config/signing.rs rename to bin/libs/config/src/project/signing.rs diff --git a/bin/libs/project/src/config/version.rs b/bin/libs/config/src/project/version.rs similarity index 100% rename from bin/libs/project/src/config/version.rs rename to bin/libs/config/src/project/version.rs diff --git a/bin/libs/error/Cargo.toml b/bin/libs/error/Cargo.toml index a42eff68..9f00ca05 100644 --- a/bin/libs/error/Cargo.toml +++ b/bin/libs/error/Cargo.toml @@ -14,7 +14,7 @@ hemtt-signing = { path = "../../../libs/signing" } hemtt-version = { path = "../../../libs/version" } git2 = "0.15.0" -glob = "0.3.0" +glob = "0.3.1" serde = { workspace = true, features = ["derive"] } toml = { version = "0.5.10" } vfs = "0.9.0" diff --git a/bin/libs/error/src/lib.rs b/bin/libs/error/src/lib.rs index 50ffe3b2..1711605e 100644 --- a/bin/libs/error/src/lib.rs +++ b/bin/libs/error/src/lib.rs @@ -40,6 +40,8 @@ pub enum Error { #[error("Git Error: {0}")] Git(#[from] git2::Error), #[error("Glob Error: {0}")] + GlobError(#[from] glob::GlobError), + #[error("Glob Pattern Error: {0}")] GlobPattern(#[from] glob::PatternError), #[error("IO Error: {0}")] Io(#[from] std::io::Error), diff --git a/book/SUMMARY.md b/book/SUMMARY.md index 2af435c8..e02d60a5 100644 --- a/book/SUMMARY.md +++ b/book/SUMMARY.md @@ -9,6 +9,7 @@ - [Configuration](configuration.md) - [Minimum](configuration-minimum.md) - [Version](configuration-version.md) + - [Addon](configuration-addon.md) - [Commands](commands.md) - [new](commands-new.md) - [dev](commands-dev.md) diff --git a/book/configuration-addon.md b/book/configuration-addon.md new file mode 100644 index 00000000..e56a17aa --- /dev/null +++ b/book/configuration-addon.md @@ -0,0 +1,24 @@ +# Addon Configuration + +In addition to the `hemtt.toml` in the root of the project, HEMTT also supports an optional `hemtt.toml` in each addon folder. + +***hemtt.toml*** + +```toml +no_bin = [ + "data/*.p3d", + "data/anim/chop.rtm", +] + +preprocess = false # Default: true +``` + +## No Bin + +The `no_bin` key is an array of glob patterns that will be excluded from binarization and packed as is. + +## Preprocess + +The `preprocess` key is a boolean that determines if the addon `config.cpp` should be preprocessed. This is not recommended, and should only be used when required, such as when using `__has_include`. + +When it is required, it is recommended to create a separate addon to house any optional config, with the minimum amount of code required to make it work. Disabling preprocessing will allow you to ship invalid config, which could cause issues for your players. It will also cause slower load times when the config is valid. diff --git a/book/configuration-version.md b/book/configuration-version.md index c811f55d..5d54ba85 100644 --- a/book/configuration-version.md +++ b/book/configuration-version.md @@ -1,4 +1,4 @@ -# Version +# Version Configuration HEMTT uses the project version as part of the signing authority. It is also included as an extension in built PBOs.