mirror of
https://github.com/storytold/craft-libs.git
synced 2026-11-16 20:25:02 +00:00
Bound portable material detection
This commit is contained in:
@@ -5,7 +5,7 @@ contains `material.json`, named texture maps and a source package. Applications
|
||||
parameter values and cook or render maps themselves; this crate does not depend on an app's
|
||||
model. It supports callers that read either version 1 or version 2 of the manifest, or both.
|
||||
|
||||
`Material::open` takes caller-provided version, entry-count, map-count, per-entry and total
|
||||
`is_material` checks for a manifest with a bounded ZIP index; `Material::open` takes caller-provided version, entry-count, map-count, per-entry and total
|
||||
expanded-byte limits. Each subsequent `read` and `read_source` shares that budget, including
|
||||
repeated references to the same ZIP member. Duplicate member names are refused. The manifest
|
||||
preserves unknown fields at the top level and on maps; parameter, input and preset objects are
|
||||
|
||||
@@ -69,6 +69,12 @@ pub enum Error {
|
||||
Archive(#[from] craft_zip::Error),
|
||||
}
|
||||
|
||||
/// Whether a bounded ZIP index contains a material manifest. This does not validate the manifest.
|
||||
pub fn is_material(bytes: &[u8], max_entries: u64) -> bool {
|
||||
Archive::open(bytes, craft_zip::Limits { max_entries, max_total_bytes: 0 })
|
||||
.is_ok_and(|archive| !archive.has_duplicate_names() && archive.contains(MANIFEST_ENTRY))
|
||||
}
|
||||
|
||||
/// Manifest and original archive, retaining one shared expanded-byte budget across reads.
|
||||
pub struct Material<'a> {
|
||||
pub manifest: Manifest,
|
||||
@@ -157,6 +163,8 @@ mod tests {
|
||||
for version in [1, 2] {
|
||||
let manifest = sample(version);
|
||||
let bytes = write(&manifest, &[("maps/base.png".into(), vec![1, 2, 3])], b"source").unwrap();
|
||||
assert!(is_material(&bytes, 100));
|
||||
assert!(!is_material(&bytes, 1));
|
||||
let mut material = Material::open(&bytes, limits()).unwrap();
|
||||
assert_eq!(material.manifest, manifest);
|
||||
assert_eq!(material.read("maps/base.png").unwrap(), [1, 2, 3]);
|
||||
|
||||
Reference in New Issue
Block a user