Files
materialcraft/crates/formats/build.rs
T
chasse20 c469b530b6 Auto: Align normal previews and reduce fallback image buffers
Match preview defaults to the accepted DirectX normal convention. Preserve finite HDR blur values and infer missing dot operands from the connected vector width.

Avoid full-size fallback images in channel shuffle and raster compositing, skip incompatible background resampling, and return early for missing gradients. Complete scoped naming, spacing and comment cleanup and remove the unused workspace bytemuck declaration.

Integrate main's shared format readers and external web library resources.

Validated with affected-package release tests, Clippy, layer and wasm checks, panic_hunt, offscreen snapshots, and comparable resource probes.
2026-11-16 23:03:07 +00:00

101 lines
4.3 KiB
Rust

//! Bundle the same SBS library and original resources used by native disk installations. The
//! browser build leaves `resources/` out and lists it for download instead.
#![forbid(unsafe_code)]
#![deny(clippy::unwrap_used, clippy::expect_used, clippy::panic, clippy::unimplemented, clippy::todo, clippy::unreachable)]
use std::{
io::{Read, Write},
path::{Path, PathBuf},
};
type Result<T> = std::result::Result<T, Box<dyn std::error::Error>>;
const MAX_DIRECTORY_DEPTH: usize = 6;
const MAX_BUNDLED_FILES: usize = 4096;
const MAX_ASSET_BYTES: usize = 256 << 20;
#[path = "build/sbs_font.rs"]
mod sbs_font;
fn scan(root: &Path, folder: &Path, files: &mut Vec<PathBuf>) -> Result<()> {
if folder.components().count() > MAX_DIRECTORY_DEPTH || files.len() > MAX_BUNDLED_FILES {
return Err("bundled library exceeds its scan limits".into());
}
let path = root.join(folder);
println!("cargo:rerun-if-changed={}", path.display());
for entry in std::fs::read_dir(path)? {
let entry = entry?;
let name = entry.file_name();
let name = name.to_str().ok_or("non-UTF-8 library filename")?;
if name.starts_with('.') {
continue;
}
let kind = entry.file_type()?;
let relative = folder.join(name);
if kind.is_dir() {
scan(root, &relative, files)?;
} else if kind.is_file() && (name.ends_with(".sbs") || relative.starts_with("resources")) {
if files.len() >= MAX_BUNDLED_FILES {
return Err("bundled library exceeds its file limit".into());
}
files.push(relative);
} else if kind.is_symlink() {
return Err("symlinks are not bundled library assets".into());
}
}
Ok(())
}
fn main() -> Result<()> {
let root = PathBuf::from(std::env::var_os("CARGO_MANIFEST_DIR").ok_or("missing manifest directory")?).join("../../assets/sbs");
let out = PathBuf::from(std::env::var_os("OUT_DIR").ok_or("missing build output directory")?);
let compressed = out.join("sbs-assets");
std::fs::create_dir_all(&compressed)?;
let mut files = Vec::new();
scan(&root, Path::new(""), &mut files)?;
// Keep already-compressed bitmaps out of the size-capped wasm
// The browser downloads them from beside the wasm
let browser = std::env::var("CARGO_CFG_TARGET_ARCH").is_ok_and(|arch| arch == "wasm32");
let (downloaded, files): (Vec<_>, Vec<_>) = files.into_iter().partition(|relative| browser && relative.starts_with("resources"));
let mut external = Vec::new();
for relative in &downloaded {
external.push(relative.to_str().ok_or("non-UTF-8 asset path")?.replace('\\', "/"));
}
external.sort();
let mut files: Vec<_> = files.into_iter().map(|relative| (relative.clone(), root.join(relative))).collect();
files.extend(sbs_font::assets()?);
files.sort_by_key(|(relative, _)| relative.to_string_lossy().replace('\\', "/"));
let mut assets = String::from("const EMBEDDED_ASSETS: &[(&str, &[u8], usize)] = &[\n");
let mut packages = String::from("pub const BUNDLED: &[&str] = &[\n");
for (index, (relative, file)) in files.iter().enumerate() {
let mut bytes = Vec::new();
std::fs::File::open(file)?.take(MAX_ASSET_BYTES as u64 + 1).read_to_end(&mut bytes)?;
if bytes.len() > MAX_ASSET_BYTES {
return Err("bundled asset exceeds 256 MiB".into());
}
let mut encoder = flate2::write::DeflateEncoder::new(Vec::new(), flate2::Compression::best());
encoder.write_all(&bytes)?;
std::fs::write(compressed.join(format!("{index}.deflate")), encoder.finish()?)?;
let name = relative.to_str().ok_or("non-UTF-8 asset path")?.replace('\\', "/");
assets.push_str(&format!("({name:?}, include_bytes!(concat!(env!(\"OUT_DIR\"), \"/sbs-assets/{index}.deflate\")), {}),\n", bytes.len()));
if name.ends_with(".sbs") {
packages.push_str(&format!("{:?},\n", format!("sbs://{name}")));
}
}
assets.push_str("];\n");
packages.push_str("];\n");
let mut downloads = String::from("const EXTERNAL_ASSETS: &[&str] = &[\n");
for name in &external {
downloads.push_str(&format!("{name:?},\n"));
}
downloads.push_str("];\n");
std::fs::write(out.join("sbs_assets.rs"), format!("{assets}{packages}{downloads}"))?;
Ok(())
}