diff --git a/crates/dtmm/src/controller/deploy.rs b/crates/dtmm/src/controller/deploy.rs index e2d9c0e..02b6860 100644 --- a/crates/dtmm/src/controller/deploy.rs +++ b/crates/dtmm/src/controller/deploy.rs @@ -324,11 +324,11 @@ async fn build_bundles(state: Arc) -> Result> { let mut bundles = Vec::new(); - let mut add_lua_asset = |name, data: &str| { + let mut add_lua_asset = |name: &str, data: &str| { let span = tracing::info_span!("Compiling Lua", name, data_len = data.len()); let _enter = span.enter(); - let file = lua::compile(name, data).wrap_err("Failed to compile Lua")?; + let file = lua::compile(name.to_string(), data).wrap_err("Failed to compile Lua")?; mod_bundle.add_file(file); @@ -517,8 +517,8 @@ async fn patch_boot_bundle( .wrap_err("Failed to render template `mod_main.lua`")?; tracing::trace!("Main script rendered:\n===========\n{}\n=============", lua); - let file = - lua::compile(MOD_BOOT_SCRIPT, lua).wrap_err("Failed to compile mod main Lua file")?; + let file = lua::compile(MOD_BOOT_SCRIPT.to_string(), lua) + .wrap_err("Failed to compile mod main Lua file")?; boot_bundle.add_file(file); } diff --git a/crates/dtmm/src/controller/import.rs b/crates/dtmm/src/controller/import.rs index 8e47d23..2f5f90b 100644 --- a/crates/dtmm/src/controller/import.rs +++ b/crates/dtmm/src/controller/import.rs @@ -297,6 +297,7 @@ fn extract_mod_config(archive: &mut ZipArchive) -> Result<(Mo packages: Vec::new(), resources, depends: Vec::new(), + name_overrides: Default::default(), }; Ok((cfg, root)) diff --git a/crates/dtmt/src/cmd/build.rs b/crates/dtmt/src/cmd/build.rs index 77ab629..fab072b 100644 --- a/crates/dtmt/src/cmd/build.rs +++ b/crates/dtmt/src/cmd/build.rs @@ -103,38 +103,41 @@ async fn find_project_config(dir: Option) -> Result { } #[tracing::instrument(skip_all)] -async fn compile_package_files

(pkg: &Package, root: P) -> Result> -where - P: AsRef + std::fmt::Debug, -{ - let root = Arc::new(root.as_ref()); +async fn compile_package_files(pkg: &Package, cfg: &ModConfig) -> Result> { + let root = Arc::new(&cfg.dir); + let name_overrides = &cfg.name_overrides; let tasks = pkg .iter() - .flat_map(|(file_type, paths)| { - paths.iter().map(|path| { + .flat_map(|(file_type, names)| { + names.iter().map(|name| { ( *file_type, - path, + name, // Cloning the `Arc` here solves the issue that in the next `.map`, I need to // `move` the closure parameters, but can't `move` `root` before it was cloned. root.clone(), ) }) }) - .map(|(file_type, path, root)| async move { - let sjson = fs::read_to_string(&path).await?; + .map(|(file_type, name, root)| async move { + let path = PathBuf::from(name); + let sjson = fs::read_to_string(&path) + .await + .wrap_err_with(|| format!("Failed to read file '{}'", path.display()))?; - let mut path = path.clone(); - path.set_extension(""); - - BundleFile::from_sjson( - path.to_slash_lossy().to_string(), - file_type, - sjson, - root.as_ref(), - ) - .await + let name = path.with_extension("").to_slash_lossy().to_string(); + let name = if let Some(new_name) = name_overrides.get(&name) { + let new_name = match u64::from_str_radix(new_name, 16) { + Ok(hash) => IdString64::from(hash), + Err(_) => IdString64::from(new_name.clone()), + }; + tracing::info!("Overriding '{}' -> '{}'", name, new_name.display()); + new_name + } else { + IdString64::from(name.clone()) + }; + BundleFile::from_sjson(name, file_type, sjson, root.as_ref()).await }); let results = futures::stream::iter(tasks) @@ -146,12 +149,11 @@ where } #[tracing::instrument] -async fn build_package(package: P1, root: P2) -> Result -where - P1: AsRef + std::fmt::Debug, - P2: AsRef + std::fmt::Debug, -{ - let root = root.as_ref(); +async fn build_package( + cfg: &ModConfig, + package: impl AsRef + std::fmt::Debug, +) -> Result { + let root = &cfg.dir; let package = package.as_ref(); let mut path = root.join(package); @@ -165,7 +167,7 @@ where .await .wrap_err_with(|| format!("Invalid package file {}", &pkg_name))?; - let files = compile_package_files(&pkg, root).await?; + let files = compile_package_files(&pkg, cfg).await?; let mut bundle = Bundle::new(pkg_name); for file in files { bundle.add_file(file); @@ -254,14 +256,14 @@ pub(crate) async fn read_project_config(dir: Option) -> Result( +#[tracing::instrument] +pub(crate) async fn build

( cfg: &ModConfig, - out_path: P1, - game_dir: Arc>, + out_path: impl AsRef + std::fmt::Debug, + game_dir: Arc>, ) -> Result<()> where - P1: AsRef, - P2: AsRef, + P: AsRef + std::fmt::Debug, { let out_path = out_path.as_ref(); @@ -286,7 +288,7 @@ where ); } - let bundle = build_package(path, &cfg.dir).await.wrap_err_with(|| { + let bundle = build_package(&cfg, path).await.wrap_err_with(|| { format!( "Failed to build package '{}' at '{}'", path.display(), diff --git a/crates/dtmt/src/cmd/migrate.rs b/crates/dtmt/src/cmd/migrate.rs index 2eacded..d7bfa19 100644 --- a/crates/dtmt/src/cmd/migrate.rs +++ b/crates/dtmt/src/cmd/migrate.rs @@ -351,6 +351,7 @@ pub(crate) async fn run(_ctx: sdk::Context, matches: &ArgMatches) -> Result<()> }, depends: vec![ModDependency::ID(String::from("DMF"))], bundled: true, + name_overrides: HashMap::new(), }; tracing::debug!(?dtmt_cfg); diff --git a/crates/dtmt/src/cmd/watch.rs b/crates/dtmt/src/cmd/watch.rs index 79fab67..2abd0f7 100644 --- a/crates/dtmt/src/cmd/watch.rs +++ b/crates/dtmt/src/cmd/watch.rs @@ -77,17 +77,14 @@ pub(crate) fn command_definition() -> Command { ) } -async fn compile( +#[tracing::instrument] +async fn compile( cfg: &ModConfig, - out_path: P1, - archive_path: P2, - game_dir: Arc>, -) -> Result<()> -where - P1: AsRef + std::marker::Copy, - P2: AsRef, - P3: AsRef, -{ + out_path: impl AsRef + std::fmt::Debug, + archive_path: impl AsRef + std::fmt::Debug, + game_dir: Arc + std::fmt::Debug>>, +) -> Result<()> { + let out_path = out_path.as_ref(); build(cfg, out_path, game_dir) .await .wrap_err("Failed to build bundles")?; diff --git a/lib/dtmt-shared/src/lib.rs b/lib/dtmt-shared/src/lib.rs index 3c9530e..d1f69c7 100644 --- a/lib/dtmt-shared/src/lib.rs +++ b/lib/dtmt-shared/src/lib.rs @@ -1,3 +1,4 @@ +use std::collections::HashMap; use std::path::PathBuf; use color_eyre::eyre::{OptionExt as _, WrapErr as _}; @@ -67,6 +68,8 @@ pub struct ModConfig { pub depends: Vec, #[serde(default = "default_true", skip_serializing_if = "is_true")] pub bundled: bool, + #[serde(default)] + pub name_overrides: HashMap, } pub const STEAMAPP_ID: u32 = 1361210; diff --git a/lib/sdk/src/bundle/file.rs b/lib/sdk/src/bundle/file.rs index 1780a5d..f387409 100644 --- a/lib/sdk/src/bundle/file.rs +++ b/lib/sdk/src/bundle/file.rs @@ -120,7 +120,7 @@ pub struct BundleFile { } impl BundleFile { - pub fn new(name: String, file_type: BundleFileType) -> Self { + pub fn new(name: impl Into, file_type: BundleFileType) -> Self { Self { file_type, name: name.into(), @@ -252,20 +252,15 @@ impl BundleFile { Ok(w.into_inner()) } - #[tracing::instrument(name = "File::from_sjson", skip(sjson))] - pub async fn from_sjson( - name: String, + #[tracing::instrument("File::from_sjson", skip(sjson, name), fields(name = %name.display()))] + pub async fn from_sjson( + name: IdString64, file_type: BundleFileType, - sjson: S, - root: P, - ) -> Result - where - P: AsRef + std::fmt::Debug, - S: AsRef, - { + sjson: impl AsRef, + root: impl AsRef + std::fmt::Debug, + ) -> Result { match file_type { - BundleFileType::Lua => lua::compile(name.clone(), sjson) - .wrap_err_with(|| format!("Failed to compile Lua file '{}'", name)), + BundleFileType::Lua => lua::compile(name, sjson).wrap_err("Failed to compile Lua file"), BundleFileType::Unknown(_) => { eyre::bail!("Unknown file type. Cannot compile from SJSON"); } @@ -304,10 +299,7 @@ impl BundleFile { s } - pub fn matches_name(&self, name: S) -> bool - where - S: Into, - { + pub fn matches_name(&self, name: impl Into) -> bool { let name = name.into(); if self.name == name { return true; diff --git a/lib/sdk/src/filetype/lua.rs b/lib/sdk/src/filetype/lua.rs index bfe6de7..14f0d6b 100644 --- a/lib/sdk/src/filetype/lua.rs +++ b/lib/sdk/src/filetype/lua.rs @@ -15,6 +15,7 @@ use tokio::fs; use crate::binary::sync::ReadExt; use crate::binary::sync::WriteExt; use crate::bundle::file::{BundleFileVariant, UserFile}; +use crate::murmur::IdString64; use crate::{BundleFile, BundleFileType}; const BITSQUID_LUAJIT_HEADER: u32 = 0x8253461B; @@ -117,17 +118,13 @@ where } #[tracing::instrument(skip_all)] -pub fn compile(name: S, code: C) -> Result -where - S: Into, - C: AsRef, -{ +pub fn compile(name: impl Into, code: impl AsRef) -> Result { let name = name.into(); let code = code.as_ref(); tracing::trace!( "Compiling '{}', {} bytes of code", - name, + name.display(), code.as_bytes().len() ); @@ -135,8 +132,8 @@ where let state = lua::luaL_newstate(); lua::luaL_openlibs(state); - let name = CString::new(format!("@{name}").into_bytes()) - .wrap_err_with(|| format!("Cannot convert name into CString: {}", name))?; + let name = CString::new(format!("@{}", name.display()).into_bytes()) + .wrap_err_with(|| format!("Cannot convert name into CString: {}", name.display()))?; match lua::luaL_loadbuffer( state, code.as_ptr() as _, diff --git a/lib/sdk/src/filetype/package.rs b/lib/sdk/src/filetype/package.rs index a36719e..6394e42 100644 --- a/lib/sdk/src/filetype/package.rs +++ b/lib/sdk/src/filetype/package.rs @@ -7,13 +7,12 @@ use std::str::FromStr; use async_recursion::async_recursion; use color_eyre::eyre::{self, Context}; use color_eyre::Result; -use path_slash::PathBufExt; use tokio::fs; use crate::binary::sync::{ReadExt, WriteExt}; use crate::bundle::file::UserFile; use crate::bundle::filetype::BundleFileType; -use crate::murmur::{HashGroup, Murmur64}; +use crate::murmur::{HashGroup, IdString64, Murmur64}; #[tracing::instrument] #[async_recursion] @@ -91,12 +90,12 @@ where Ok(paths) } -type PackageType = HashMap>; +type PackageType = HashMap>; type PackageDefinition = HashMap>; #[derive(Default)] pub struct Package { - _name: String, + _name: IdString64, _root: PathBuf, inner: PackageType, flags: u8, @@ -117,9 +116,9 @@ impl DerefMut for Package { } impl Package { - pub fn new(name: String, root: PathBuf) -> Self { + pub fn new(name: impl Into, root: PathBuf) -> Self { Self { - _name: name, + _name: name.into(), _root: root, inner: Default::default(), flags: 1, @@ -130,17 +129,22 @@ impl Package { self.values().fold(0, |total, files| total + files.len()) } - pub fn add_file>(&mut self, file_type: BundleFileType, name: P) { + pub fn add_file(&mut self, file_type: BundleFileType, name: impl Into) { self.inner.entry(file_type).or_default().insert(name.into()); } #[tracing::instrument("Package::from_sjson", skip(sjson), fields(sjson_len = sjson.as_ref().len()))] - pub async fn from_sjson(sjson: S, name: String, root: P) -> Result + pub async fn from_sjson( + sjson: S, + name: impl Into + std::fmt::Debug, + root: P, + ) -> Result where P: AsRef + std::fmt::Debug, S: AsRef, { let root = root.as_ref(); + let name = name.into(); let definition: PackageDefinition = serde_sjson::from_str(sjson.as_ref())?; let mut inner: PackageType = Default::default(); @@ -174,7 +178,11 @@ impl Package { continue; }; - inner.entry(t).or_default().insert(path); + tracing::debug!("Adding file {}", path.display()); + inner + .entry(t) + .or_default() + .insert(path.display().to_string()); } } } @@ -193,11 +201,9 @@ impl Package { pub fn to_sjson(&self) -> Result { let mut map: PackageDefinition = Default::default(); - for (t, paths) in self.iter() { - for path in paths.iter() { - map.entry(t.ext_name()) - .or_default() - .insert(path.display().to_string()); + for (t, names) in self.iter() { + for name in names.iter() { + map.entry(t.ext_name()).or_default().insert(name.clone()); } } @@ -223,11 +229,11 @@ impl Package { for _ in 0..file_count { let t = BundleFileType::from(r.read_u64()?); let hash = Murmur64::from(r.read_u64()?); - let path = ctx.lookup_hash(hash, HashGroup::Filename); + let name = ctx.lookup_hash(hash, HashGroup::Filename); inner .entry(t) .or_default() - .insert(PathBuf::from(path.display().to_string())); + .insert(name.display().to_string()); } let flags = r.read_u8()?; @@ -240,7 +246,7 @@ impl Package { let pkg = Self { inner, - _name: name, + _name: name.into(), _root: PathBuf::new(), flags, }; @@ -256,12 +262,10 @@ impl Package { w.write_u32(0x2b)?; w.write_u32(self.values().flatten().count() as u32)?; - for (t, paths) in self.iter() { - for path in paths.iter() { + for (t, names) in self.iter() { + for name in names.iter() { w.write_u64(t.hash().into())?; - - let hash = Murmur64::hash(path.to_slash_lossy().as_bytes()); - w.write_u64(hash.into())?; + w.write_u64(Murmur64::hash(name.as_bytes()).into())?; } } diff --git a/lib/sdk/src/murmur/types.rs b/lib/sdk/src/murmur/types.rs index e96b992..20bf46a 100644 --- a/lib/sdk/src/murmur/types.rs +++ b/lib/sdk/src/murmur/types.rs @@ -1,3 +1,7 @@ +use std::path::Path; + +use path_slash::PathExt; + use self::util::{parse_hex32, parse_hex64}; use super::*; @@ -263,11 +267,23 @@ impl IdString64 { IdString64::String(_) => false, } } + + // Would love to have this as a proper `impl From`, but + // rustc will complain that it overlaps with the `impl From>`. + pub fn from_path(p: impl AsRef) -> Self { + Self::String(p.as_ref().to_slash_lossy().to_string()) + } } -impl> From for IdString64 { - fn from(value: S) -> Self { - Self::String(value.into()) +impl From for IdString64 { + fn from(value: String) -> Self { + Self::String(value) + } +} + +impl From for IdString64 { + fn from(value: u64) -> Self { + Self::Hash(value.into()) } } @@ -283,6 +299,12 @@ impl From for Murmur64 { } } +impl Default for IdString64 { + fn default() -> Self { + Self::Hash(0.into()) + } +} + impl PartialEq for IdString64 { fn eq(&self, other: &Self) -> bool { self.to_murmur64() == other.to_murmur64()