mirror of
https://github.com/storytold/materialcraft.git
synced 2026-11-22 07:20:57 +01:00
Integrate main's read limits and preserve the font, evaluator, converter, and measured preview repairs. Complete the scoped type, comment, and spacing cleanup. Reuse one validated XML document for library layout and graph reads. Associate cache maintenance completion with its submitted revision so old failures cannot consume new settings or clear requests.
84 lines
3.5 KiB
Rust
84 lines
3.5 KiB
Rust
//! Bundle the same SBS library and original resources used by native disk installations.
|
|
#![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)?;
|
|
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");
|
|
std::fs::write(out.join("sbs_assets.rs"), format!("{assets}{packages}"))?;
|
|
Ok(())
|
|
}
|