mirror of
https://github.com/storytold/materialcraft.git
synced 2026-11-20 22:40:59 +01:00
formats: share SBS package/schema read/write via craft-sbs (#284)
* formats: share SBS package/schema read/write via craft-sbs * chore: update craft-sbs pin and fix example ParsingOptions * formats: remove unused SBS constant re-exports * formats: remove unused parse_ints re-export * formats: fix test visibility and manifest dependencies * chore: repin craft-sbs to storytold/craft-libs main
This commit is contained in:
1 parent
64f175bcc1
commit
f5ab7bb1d8
15 files changed
+84
-375
No files matched your search
Generated
+32
-15
@@ -840,7 +840,7 @@ dependencies = [
|
||||
[[package]]
|
||||
name = "craft-codepage"
|
||||
version = "0.1.0"
|
||||
source = "git+https://github.com/storytold/craft-libs?rev=453b0da782ccef39be9a6dac8d27c80b553ef1cc#453b0da782ccef39be9a6dac8d27c80b553ef1cc"
|
||||
source = "git+https://github.com/storytold/craft-libs?rev=380a2f79f6a195c090a5372e819cbcda2b703c09#380a2f79f6a195c090a5372e819cbcda2b703c09"
|
||||
|
||||
[[package]]
|
||||
name = "craft-dds"
|
||||
@@ -941,7 +941,7 @@ name = "craft-materialx"
|
||||
version = "0.1.0"
|
||||
source = "git+https://github.com/storytold/craft-libs?rev=453b0da782ccef39be9a6dac8d27c80b553ef1cc#453b0da782ccef39be9a6dac8d27c80b553ef1cc"
|
||||
dependencies = [
|
||||
"craft-xml",
|
||||
"craft-xml 0.1.0 (git+https://github.com/storytold/craft-libs?rev=453b0da782ccef39be9a6dac8d27c80b553ef1cc)",
|
||||
"thiserror 2.0.21",
|
||||
]
|
||||
|
||||
@@ -992,7 +992,7 @@ dependencies = [
|
||||
[[package]]
|
||||
name = "craft-mime"
|
||||
version = "0.1.0"
|
||||
source = "git+https://github.com/storytold/craft-libs?rev=453b0da782ccef39be9a6dac8d27c80b553ef1cc#453b0da782ccef39be9a6dac8d27c80b553ef1cc"
|
||||
source = "git+https://github.com/storytold/craft-libs?rev=380a2f79f6a195c090a5372e819cbcda2b703c09#380a2f79f6a195c090a5372e819cbcda2b703c09"
|
||||
dependencies = [
|
||||
"chrono",
|
||||
"craft-codepage",
|
||||
@@ -1020,19 +1020,32 @@ dependencies = [
|
||||
"objc2-app-kit 0.3.2",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "craft-sbs"
|
||||
version = "0.1.0"
|
||||
source = "git+https://github.com/storytold/craft-libs?rev=380a2f79f6a195c090a5372e819cbcda2b703c09#380a2f79f6a195c090a5372e819cbcda2b703c09"
|
||||
dependencies = [
|
||||
"base64 0.23.1",
|
||||
"craft-mime",
|
||||
"craft-xml 0.1.0 (git+https://github.com/storytold/craft-libs?rev=380a2f79f6a195c090a5372e819cbcda2b703c09)",
|
||||
"flate2",
|
||||
"roxmltree",
|
||||
"thiserror 2.0.21",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "craft-svg"
|
||||
version = "0.1.0"
|
||||
source = "git+https://github.com/storytold/craft-libs?rev=453b0da782ccef39be9a6dac8d27c80b553ef1cc#453b0da782ccef39be9a6dac8d27c80b553ef1cc"
|
||||
dependencies = [
|
||||
"craft-image-codecs",
|
||||
"craft-xml",
|
||||
"craft-xml 0.1.0 (git+https://github.com/storytold/craft-libs?rev=453b0da782ccef39be9a6dac8d27c80b553ef1cc)",
|
||||
"data-url",
|
||||
"flate2",
|
||||
"kurbo",
|
||||
"quick-xml 0.41.0",
|
||||
"resvg",
|
||||
"roxmltree 0.21.2",
|
||||
"roxmltree",
|
||||
"thiserror 2.0.21",
|
||||
"usvg",
|
||||
"vectorcraft-geom",
|
||||
@@ -1118,6 +1131,16 @@ dependencies = [
|
||||
"wayland-client",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "craft-xml"
|
||||
version = "0.1.0"
|
||||
source = "git+https://github.com/storytold/craft-libs?rev=380a2f79f6a195c090a5372e819cbcda2b703c09#380a2f79f6a195c090a5372e819cbcda2b703c09"
|
||||
dependencies = [
|
||||
"quick-xml 0.41.0",
|
||||
"serde",
|
||||
"thiserror 2.0.21",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "craft-xml"
|
||||
version = "0.1.0"
|
||||
@@ -2920,10 +2943,10 @@ dependencies = [
|
||||
"craft-image-codecs",
|
||||
"craft-mcmat",
|
||||
"craft-mesh",
|
||||
"craft-mime",
|
||||
"craft-sbs",
|
||||
"craft-svg",
|
||||
"craft-texture",
|
||||
"craft-xml",
|
||||
"craft-xml 0.1.0 (git+https://github.com/storytold/craft-libs?rev=453b0da782ccef39be9a6dac8d27c80b553ef1cc)",
|
||||
"craft-zip 0.1.0 (git+https://github.com/storytold/craft-libs?rev=512a1b8e8b59a7b325e7f69f6131aa8628470288)",
|
||||
"dds",
|
||||
"flate2",
|
||||
@@ -2931,7 +2954,7 @@ dependencies = [
|
||||
"materialcraft-core",
|
||||
"materialcraft-geom",
|
||||
"materialcraft-nodes",
|
||||
"roxmltree 0.20.0",
|
||||
"roxmltree",
|
||||
"serde",
|
||||
"serde_json",
|
||||
"sha2",
|
||||
@@ -4271,12 +4294,6 @@ dependencies = [
|
||||
"unicode-ident",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "roxmltree"
|
||||
version = "0.20.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "6c20b6793b5c2fa6553b250154b78d6d0db37e72700ae35fad9387a46f487c97"
|
||||
|
||||
[[package]]
|
||||
name = "roxmltree"
|
||||
version = "0.21.2"
|
||||
@@ -5275,7 +5292,7 @@ dependencies = [
|
||||
"kurbo",
|
||||
"log",
|
||||
"pico-args",
|
||||
"roxmltree 0.21.2",
|
||||
"roxmltree",
|
||||
"simplecss",
|
||||
"siphasher",
|
||||
"skrifa",
|
||||
|
||||
+3
-1
@@ -44,6 +44,8 @@ craft-mime = { git = "https://github.com/storytold/craft-libs", rev = "453b0da78
|
||||
craft-mesh = { version = "0.1.0", git = "https://github.com/storytold/craft-libs", rev = "453b0da782ccef39be9a6dac8d27c80b553ef1cc", default-features = false, features = ["obj", "gltf"] }
|
||||
# Shared XML escaping and bounded XML parsing; SBS model and serialization policy stay app-local
|
||||
craft-xml = { version = "0.1.0", git = "https://github.com/storytold/craft-libs", rev = "453b0da782ccef39be9a6dac8d27c80b553ef1cc" }
|
||||
# Shared SBS package/schema parsing, sniffing, embedded codec, and envelope writing (storytold/craft-libs#2496)
|
||||
craft-sbs = { version = "0.1.0", git = "https://github.com/storytold/craft-libs", rev = "380a2f79f6a195c090a5372e819cbcda2b703c09" }
|
||||
# Shared bounded ZIP archive reader and deterministic writer (storytold/craft-libs#2411)
|
||||
craft-zip = { version = "0.1.0", git = "https://github.com/storytold/craft-libs", rev = "512a1b8e8b59a7b325e7f69f6131aa8628470288" }
|
||||
# Bound SVG parsing and drawing through craft-svg
|
||||
@@ -82,7 +84,7 @@ rmcp = { version = "3.5", default-features = false, features = ["server", "macro
|
||||
tokio = { version = "1", features = ["rt-multi-thread", "macros", "io-std", "sync", "net", "io-util", "time"] }
|
||||
schemars = "1"
|
||||
base64 = "0.22"
|
||||
roxmltree = "0.20"
|
||||
roxmltree = "0.21"
|
||||
flate2 = { version = "1", default-features = false, features = ["rust_backend"] }
|
||||
cap-std = "4"
|
||||
getrandom = "0.3"
|
||||
|
||||
@@ -9,8 +9,8 @@ license.workspace = true
|
||||
workspace = true
|
||||
|
||||
[dependencies]
|
||||
craft-mime = { workspace = true }
|
||||
craft-xml = { workspace = true }
|
||||
craft-sbs = { workspace = true }
|
||||
craft-zip = { workspace = true }
|
||||
craft-mcmat = { workspace = true }
|
||||
materialcraft-core = { workspace = true }
|
||||
@@ -22,7 +22,6 @@ craft-image-codecs = { workspace = true }
|
||||
serde = { workspace = true }
|
||||
serde_json = { workspace = true }
|
||||
thiserror = { workspace = true }
|
||||
zip = { workspace = true }
|
||||
roxmltree = { workspace = true }
|
||||
flate2 = { workspace = true }
|
||||
base64 = { workspace = true }
|
||||
@@ -39,3 +38,4 @@ sha2 = { workspace = true }
|
||||
[dev-dependencies]
|
||||
dds = { workspace = true }
|
||||
image = { workspace = true }
|
||||
zip = { workspace = true }
|
||||
@@ -4,7 +4,7 @@ use roxmltree::{Document, Node};
|
||||
pub fn parse(text: &str) -> Result<Document<'_>> {
|
||||
const MAX_XML_NODES: u32 = 8_000_000;
|
||||
|
||||
Ok(Document::parse_with_options(text, roxmltree::ParsingOptions { allow_dtd: false, nodes_limit: MAX_XML_NODES })?)
|
||||
Ok(Document::parse_with_options(text, roxmltree::ParsingOptions { allow_dtd: false, nodes_limit: MAX_XML_NODES, ..roxmltree::ParsingOptions::default() })?)
|
||||
}
|
||||
|
||||
pub fn child<'a, 'b>(node: Node<'a, 'b>, tag: &str) -> Option<Node<'a, 'b>> {
|
||||
|
||||
@@ -1,4 +1,5 @@
|
||||
pub(super) const MAX_XML_NODES: u32 = 8_000_000;
|
||||
pub use craft_sbs::SBS_EXT;
|
||||
pub(super) use craft_sbs::{FIRST_UID, MAX_XML_NODES, SELF_DEPENDENCY};
|
||||
|
||||
pub(crate) const MAX_GRAPHS: usize = 4096;
|
||||
|
||||
@@ -6,15 +7,8 @@ pub(crate) const MAX_NODES: usize = 100_000;
|
||||
|
||||
pub(super) const UPDATER_VERSION: &str = env!("CARGO_PKG_VERSION");
|
||||
|
||||
pub const SBS_EXT: &str = "sbs";
|
||||
pub(super) const MAX_WARNINGS: usize = 600;
|
||||
|
||||
/// Largest SBS document read (audit: MaterialCraft #2).
|
||||
pub(super) const MAX_SBS_BYTES: u64 = 256 << 20;
|
||||
|
||||
/// Most wires one SBS may carry, across all graphs (audit: MaterialCraft #2).
|
||||
pub(super) const MAX_WIRES: usize = 1_000_000;
|
||||
pub(super) const SNIFF_BYTES: usize = 4096;
|
||||
pub(super) const SELF_DEPENDENCY: &str = "?himself";
|
||||
pub(super) const FIRST_UID: u64 = 1_000_000_000;
|
||||
pub(super) const NODE_HALF_SIZE: [f32; 2] = [50.0, 40.0];
|
||||
@@ -1,81 +1,29 @@
|
||||
use super::embedded_error::EmbeddedError;
|
||||
use crate::{FormatError, Result};
|
||||
use base64::Engine as _;
|
||||
use std::{
|
||||
cell::Cell,
|
||||
io::{self, Read, Write},
|
||||
};
|
||||
|
||||
pub(super) fn base64_engine() -> base64::engine::GeneralPurpose {
|
||||
base64::engine::general_purpose::STANDARD
|
||||
}
|
||||
use std::cell::Cell;
|
||||
|
||||
pub(super) fn decode_embedded(strdata: &str) -> std::result::Result<Vec<u8>, String> {
|
||||
decode_embedded_bounded(strdata, crate::MAX_RESOURCE, &Cell::new(crate::MAX_RESOURCE + 1)).map_err(|error| error.to_string())
|
||||
}
|
||||
|
||||
pub(super) fn decode_embedded_bounded(strdata: &str, remaining_bytes: u64, inflation_work: &Cell<u64>) -> std::result::Result<Vec<u8>, EmbeddedError> {
|
||||
let work_before = inflation_work.get();
|
||||
if work_before == 0 {
|
||||
return Err(EmbeddedError::WorkBudget);
|
||||
}
|
||||
|
||||
let raw = match strdata.split_once(char::is_whitespace) {
|
||||
Some((prefix, rest)) => base64_engine().decode(std::iter::once(prefix).chain(rest.split_whitespace()).collect::<String>()),
|
||||
None => base64_engine().decode(strdata),
|
||||
}
|
||||
.map_err(|error| format!("bad base64: {error}"))?;
|
||||
let (length, compressed) = raw.split_at_checked(4).ok_or("too short")?;
|
||||
let declared_bytes = u64::from(u32::from_be_bytes([length[0], length[1], length[2], length[3]]));
|
||||
if declared_bytes > crate::MAX_RESOURCE {
|
||||
return Err("embedded resource exceeds the 256 MiB limit".into());
|
||||
}
|
||||
|
||||
// Read one probe byte beyond the tighter bound to detect overruns
|
||||
let probe_limit = declared_bytes.min(remaining_bytes) + 1;
|
||||
let mut decoder = flate2::read::ZlibDecoder::new(compressed);
|
||||
let mut limited = decoder.by_ref().take(probe_limit.min(work_before));
|
||||
let mut output = Vec::new();
|
||||
let result = if declared_bytes > remaining_bytes || declared_bytes >= work_before {
|
||||
// Discard probe output that cannot become a retained asset
|
||||
io::copy(&mut limited, &mut io::sink())
|
||||
} else {
|
||||
limited.read_to_end(&mut output).map(|bytes| bytes as u64)
|
||||
};
|
||||
|
||||
// Charge every emitted byte, including output produced before a checksum error
|
||||
inflation_work.set(work_before.saturating_sub(decoder.total_out()));
|
||||
let actual_bytes = result.map_err(|error| format!("bad compressed data: {error}"))?;
|
||||
if actual_bytes > declared_bytes {
|
||||
return Err(format!("{actual_bytes} bytes where {declared_bytes} were declared").into());
|
||||
}
|
||||
|
||||
if work_before < probe_limit && actual_bytes == work_before {
|
||||
return Err(EmbeddedError::WorkBudget);
|
||||
}
|
||||
|
||||
if actual_bytes > remaining_bytes {
|
||||
return Err(EmbeddedError::Budget);
|
||||
}
|
||||
|
||||
if actual_bytes != declared_bytes {
|
||||
return Err(format!("{actual_bytes} bytes where {declared_bytes} were declared").into());
|
||||
}
|
||||
|
||||
// ResourceBytes retains the vector and its capacity
|
||||
output.shrink_to_fit();
|
||||
Ok(output)
|
||||
craft_sbs::embedded::decode_bounded(strdata, crate::MAX_RESOURCE, remaining_bytes, inflation_work)
|
||||
}
|
||||
|
||||
pub(super) fn encode_embedded(bytes: &[u8]) -> Result<String> {
|
||||
let length = u32::try_from(bytes.len()).map_err(|_| FormatError::Io("resource too large".into()))?;
|
||||
let mut encoder = flate2::write::ZlibEncoder::new(length.to_be_bytes().to_vec(), flate2::Compression::default());
|
||||
encoder.write_all(bytes).map_err(|e| FormatError::Io(e.to_string()))?;
|
||||
let raw = encoder.finish().map_err(|e| FormatError::Io(e.to_string()))?;
|
||||
|
||||
Ok(craft_mime::transfer::base64(&raw))
|
||||
craft_sbs::embedded::encode(bytes).map_err(FormatError::Io)
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
pub(super) fn base64_engine() -> base64::engine::GeneralPurpose {
|
||||
base64::engine::general_purpose::STANDARD
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
use base64::Engine as _;
|
||||
#[cfg(test)]
|
||||
use std::io::Write;
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
|
||||
@@ -1,28 +1 @@
|
||||
#[derive(Debug)]
|
||||
pub(super) enum EmbeddedError {
|
||||
Damaged(String),
|
||||
Budget,
|
||||
WorkBudget,
|
||||
}
|
||||
|
||||
impl From<String> for EmbeddedError {
|
||||
fn from(message: String) -> Self {
|
||||
Self::Damaged(message)
|
||||
}
|
||||
}
|
||||
|
||||
impl From<&str> for EmbeddedError {
|
||||
fn from(message: &str) -> Self {
|
||||
Self::Damaged(message.into())
|
||||
}
|
||||
}
|
||||
|
||||
impl std::fmt::Display for EmbeddedError {
|
||||
fn fmt(&self, formatter: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
||||
match self {
|
||||
Self::Damaged(message) => formatter.write_str(message),
|
||||
Self::Budget => formatter.write_str("embedded resource exceeds the remaining material resource budget"),
|
||||
Self::WorkBudget => formatter.write_str("embedded resource exceeds the material inflation work budget"),
|
||||
}
|
||||
}
|
||||
}
|
||||
pub(super) use craft_sbs::embedded::EmbeddedError;
|
||||
@@ -141,18 +141,9 @@ pub(super) fn write(graph: &FunctionGraph, writer: &mut SbsWriter) -> Result<Str
|
||||
output.push_str("</connections>");
|
||||
if node.missing.is_some() && !node.missing_data.is_empty() {
|
||||
// Only a bounded data element may be emitted; command input must not inject package XML
|
||||
let options = roxmltree::ParsingOptions { allow_dtd: false, nodes_limit: 100_000 };
|
||||
if node.missing_data.len() > (1 << 20) {
|
||||
return Err("invalid preserved function data".into());
|
||||
}
|
||||
|
||||
let document = roxmltree::Document::parse_with_options(&node.missing_data, options).map_err(|_| "invalid preserved function data")?;
|
||||
let root = document.root_element();
|
||||
if !root.has_tag_name("funcDatas") {
|
||||
return Err("invalid preserved function data".into());
|
||||
}
|
||||
|
||||
output.push_str(node.missing_data.get(root.range()).ok_or("invalid preserved function data")?);
|
||||
let preserved =
|
||||
craft_sbs::xml::validate_preserved_element(&node.missing_data, "funcDatas", 1 << 20, 100_000).ok_or("invalid preserved function data")?;
|
||||
output.push_str(preserved);
|
||||
} else if let Some(instance) = &node.instance {
|
||||
output.push_str(&super::function_instance::write(instance, writer));
|
||||
} else if let Some(value) = &node.value {
|
||||
|
||||
@@ -11,49 +11,12 @@ fn is_shared_font_asset(relative: &str) -> bool {
|
||||
|
||||
/// Validate alias paths before joining a filesystem root. No package may escape the library.
|
||||
pub fn relative_path(uri: &str) -> Result<&str, String> {
|
||||
let path = uri.strip_prefix("sbs://").ok_or("not an SBS library alias")?;
|
||||
if path.is_empty()
|
||||
|| path.len() > 4096
|
||||
|| path.contains(['\\', ':', '?', '#', '%', '\0'])
|
||||
|| path.split('/').any(|part| part.is_empty() || matches!(part, "." | ".."))
|
||||
{
|
||||
return Err("invalid SBS library path".into());
|
||||
}
|
||||
|
||||
Ok(path)
|
||||
craft_sbs::uri::relative_path(uri)
|
||||
}
|
||||
|
||||
/// Resolve a package-relative resource or dependency without leaving the library root.
|
||||
pub fn resolve(owner: &str, reference: &str) -> Result<String, String> {
|
||||
let owner = relative_path(owner)?;
|
||||
if reference.starts_with("sbs://") {
|
||||
relative_path(reference)?;
|
||||
return Ok(reference.into());
|
||||
}
|
||||
|
||||
let reference = reference.trim().replace('\\', "/");
|
||||
if reference.is_empty() || reference.len() > 4096 || reference.starts_with('/') || reference.contains([':', '?', '#', '%', '\0']) {
|
||||
return Err("invalid relative SBS library path".into());
|
||||
}
|
||||
|
||||
let mut parts: Vec<_> = owner.split('/').collect();
|
||||
parts.pop();
|
||||
for part in reference.split('/') {
|
||||
match part {
|
||||
"" | "." => {}
|
||||
".." => {
|
||||
if parts.pop().is_none() {
|
||||
return Err("SBS reference escapes its library directory".into());
|
||||
}
|
||||
}
|
||||
part => parts.push(part),
|
||||
}
|
||||
}
|
||||
|
||||
let uri = format!("sbs://{}", parts.join("/"));
|
||||
relative_path(&uri)?;
|
||||
|
||||
Ok(uri)
|
||||
craft_sbs::uri::resolve(owner, reference)
|
||||
}
|
||||
|
||||
/// The native asset root may be configured for installations with a separate data directory.
|
||||
|
||||
@@ -53,12 +53,12 @@ pub(super) fn read(reader: &mut SbsReader<'_, '_>, content: Node<'_, '_>, pkg: &
|
||||
let result = (|| -> Result<(), String> {
|
||||
let bytes = linked(&file).ok_or("source package is unavailable")?;
|
||||
remaining = remaining.checked_sub(bytes.len()).ok_or("linked resource XML exceeds 64 MiB")?;
|
||||
let text = std::str::from_utf8(&bytes).map_err(|_| "source package is not UTF-8")?;
|
||||
let document = roxmltree::Document::parse_with_options(text, roxmltree::ParsingOptions { allow_dtd: false, nodes_limit: MAX_XML_NODES })
|
||||
.map_err(|_| "source package is not valid XML")?;
|
||||
if !document.root_element().has_tag_name("package") {
|
||||
return Err("source is not an SBS package".into());
|
||||
}
|
||||
let package_doc = craft_sbs::PackageDocument::parse_with_limits(&bytes, MAX_SOURCE_BYTES as u64, MAX_XML_NODES).map_err(|err| match err {
|
||||
craft_sbs::Error::InvalidUtf8(_) => "source package is not UTF-8",
|
||||
craft_sbs::Error::NotPackage => "source is not an SBS package",
|
||||
_ => "source package is not valid XML",
|
||||
})?;
|
||||
let document = package_doc.document();
|
||||
|
||||
for resource in document.descendants().filter(|node| node.has_tag_name("resource")) {
|
||||
let path = resource_path(resource);
|
||||
|
||||
@@ -1,11 +1,4 @@
|
||||
use super::{
|
||||
constant::{MAX_GRAPHS, SNIFF_BYTES},
|
||||
placeholder::report_placeholders,
|
||||
sbs_read::SbsRead,
|
||||
sbs_reader::SbsReader,
|
||||
warnings::Warnings,
|
||||
xml::{child_element, child_value, children_named},
|
||||
};
|
||||
use super::{constant::MAX_GRAPHS, placeholder::report_placeholders, sbs_read::SbsRead, sbs_reader::SbsReader, warnings::Warnings, xml::child_value};
|
||||
use crate::Result;
|
||||
use materialcraft_core::Package;
|
||||
use roxmltree::Node as XmlNode;
|
||||
@@ -16,22 +9,14 @@ pub fn read_sbs(bytes: &[u8], name: &str, linked: &dyn Fn(&str) -> Option<Vec<u8
|
||||
}
|
||||
|
||||
pub(super) fn read_document(
|
||||
doc: &roxmltree::Document<'_>,
|
||||
doc: &craft_sbs::PackageDocument<'_>,
|
||||
name: &str,
|
||||
linked: &dyn Fn(&str) -> Option<Vec<u8>>,
|
||||
resource_budget: Option<&std::cell::Cell<u64>>,
|
||||
) -> Result<SbsRead> {
|
||||
let text = doc.input_text();
|
||||
let root = doc.root_element();
|
||||
|
||||
let text = doc.text();
|
||||
let mut warnings = Warnings::default();
|
||||
let file_stem = name.rsplit(['/', '\\']).next().unwrap_or(name);
|
||||
let file_stem = file_stem.rsplit_once('.').map_or(file_stem, |(stem, _)| stem).trim();
|
||||
let package_name = match child_value(root, "identifier").map(str::trim) {
|
||||
Some(id) if !id.is_empty() && id != "Unsaved Package" => id.to_string(),
|
||||
_ if !file_stem.is_empty() => file_stem.to_string(),
|
||||
_ => "Untitled package".to_string(),
|
||||
};
|
||||
let package_name = doc.package_name(name);
|
||||
|
||||
// Include one probe byte so a valid inline asset can use the exact retained limit
|
||||
let inflation_work = std::cell::Cell::new(resource_budget.map_or(0, |budget| budget.get().saturating_add(1)));
|
||||
@@ -50,16 +35,14 @@ pub(super) fn read_document(
|
||||
wire_count: 0,
|
||||
};
|
||||
|
||||
for dependency in child_element(root, "dependencies").into_iter().flat_map(|d| children_named(d, "dependency")) {
|
||||
if let (Some(uid), Some(file)) = (child_value(dependency, "uid"), child_value(dependency, "filename")) {
|
||||
reader.dependencies.insert(uid.to_string(), file.to_string());
|
||||
}
|
||||
for (uid, file) in doc.dependencies() {
|
||||
reader.dependencies.insert(uid.to_string(), file.to_string());
|
||||
}
|
||||
|
||||
// Load resources before resolving graph references
|
||||
let content = child_element(root, "content");
|
||||
let content = doc.content();
|
||||
let mut pkg = Package::new(&package_name);
|
||||
pkg.format_version = child_value(root, "formatVersion").filter(|version| !version.trim().is_empty()).map(str::to_string);
|
||||
pkg.format_version = doc.format_version().filter(|version| !version.trim().is_empty()).map(str::to_string);
|
||||
let mut graph_elements: Vec<(String, XmlNode)> = Vec::new();
|
||||
if let Some(content) = content {
|
||||
reader.collect_items(content, "", &mut pkg, &mut graph_elements, linked);
|
||||
@@ -91,25 +74,5 @@ pub(super) fn read_document(
|
||||
}
|
||||
|
||||
pub fn is_sbs(bytes: &[u8]) -> bool {
|
||||
let head = String::from_utf8_lossy(&bytes[..bytes.len().min(SNIFF_BYTES)]);
|
||||
let mut rest = head.trim_start_matches('\u{feff}');
|
||||
|
||||
// Skip the XML declaration and comments before the root element
|
||||
loop {
|
||||
rest = rest.trim_start();
|
||||
let (skip_open, skip_close) = if rest.starts_with("<?") {
|
||||
("<?", "?>")
|
||||
} else if rest.starts_with("<!--") {
|
||||
("<!--", "-->")
|
||||
} else {
|
||||
break;
|
||||
};
|
||||
|
||||
match rest[skip_open.len()..].find(skip_close) {
|
||||
Some(end) => rest = &rest[skip_open.len() + end + skip_close.len()..],
|
||||
None => return false,
|
||||
}
|
||||
}
|
||||
|
||||
rest.strip_prefix("<package").is_some_and(|after| after.starts_with(|c: char| c == '>' || c == '/' || c.is_whitespace()))
|
||||
craft_sbs::is_sbs(bytes)
|
||||
}
|
||||
@@ -1,37 +1,21 @@
|
||||
use super::{
|
||||
LayoutFrame, SbsRead,
|
||||
constant::{MAX_SBS_BYTES, MAX_XML_NODES},
|
||||
};
|
||||
use super::{LayoutFrame, SbsRead};
|
||||
use crate::{FormatError, Result};
|
||||
|
||||
/// A validated SBS XML document that can supply layout and graph data without reparsing.
|
||||
pub struct SbsDocument<'input> {
|
||||
document: roxmltree::Document<'input>,
|
||||
document: craft_sbs::PackageDocument<'input>,
|
||||
}
|
||||
|
||||
impl<'input> SbsDocument<'input> {
|
||||
/// Parse borrowed input with the same byte, XML-node, and DTD limits as the SBS reader.
|
||||
pub fn parse(bytes: &'input [u8]) -> Result<Self> {
|
||||
if bytes.len() as u64 > MAX_SBS_BYTES {
|
||||
return Err(FormatError::BadPackage(format!("the SBS file is {} bytes, over the {MAX_SBS_BYTES} byte limit", bytes.len())));
|
||||
}
|
||||
|
||||
let text = std::str::from_utf8(bytes).map_err(|error| FormatError::BadPackage(format!("the SBS file is not UTF-8 text: {error}")))?;
|
||||
let text = text.trim_start_matches('\u{feff}');
|
||||
let options = roxmltree::ParsingOptions { allow_dtd: false, nodes_limit: MAX_XML_NODES };
|
||||
let document = roxmltree::Document::parse_with_options(text, options)
|
||||
.map_err(|error| FormatError::BadPackage(format!("the SBS file is not valid XML: {error}")))?;
|
||||
|
||||
if document.root_element().tag_name().name() != "package" {
|
||||
return Err(FormatError::BadPackage("not an SBS package (the root element is not <package>)".into()));
|
||||
}
|
||||
|
||||
let document = craft_sbs::PackageDocument::parse(bytes).map_err(|error| FormatError::BadPackage(error.to_string()))?;
|
||||
Ok(Self { document })
|
||||
}
|
||||
|
||||
/// Read the layout independently of whether its frames name library categories.
|
||||
pub fn layout(&self, uri: &str) -> Result<Vec<LayoutFrame>> {
|
||||
super::library_layout::read_document(&self.document, uri)
|
||||
super::library_layout::read_document(self.document.document(), uri)
|
||||
}
|
||||
|
||||
/// Apply one aggregate resource budget to inline data and the caller's linked reads.
|
||||
|
||||
@@ -1,72 +1,7 @@
|
||||
use super::type_code;
|
||||
use super::xml::{format_float, parse_floats, parse_ints, xml_escape};
|
||||
use super::xml::{format_float, xml_escape};
|
||||
pub(super) use craft_sbs::value::{SbsValue, sbs_constant, sbs_constant_element};
|
||||
use materialcraft_core::Value;
|
||||
use roxmltree::Node as XmlNode;
|
||||
|
||||
#[derive(Clone, Debug, PartialEq)]
|
||||
pub(super) enum SbsValue {
|
||||
Floats(Vec<f32>),
|
||||
Ints(Vec<i32>),
|
||||
Bool(bool),
|
||||
Text(String),
|
||||
}
|
||||
|
||||
impl SbsValue {
|
||||
pub(super) fn float(&self, i: usize) -> f32 {
|
||||
match self {
|
||||
SbsValue::Floats(v) => v.get(i).or(v.first()).copied().unwrap_or(0.0),
|
||||
SbsValue::Ints(v) => v.get(i).or(v.first()).copied().unwrap_or(0) as f32,
|
||||
SbsValue::Bool(b) => f32::from(u8::from(*b)),
|
||||
SbsValue::Text(_) => 0.0,
|
||||
}
|
||||
}
|
||||
|
||||
pub(super) fn int(&self, i: usize) -> i32 {
|
||||
match self {
|
||||
SbsValue::Ints(v) => v.get(i).or(v.first()).copied().unwrap_or(0),
|
||||
SbsValue::Floats(v) => v.get(i).or(v.first()).copied().unwrap_or(0.0).round() as i32,
|
||||
SbsValue::Bool(b) => i32::from(*b),
|
||||
SbsValue::Text(_) => 0,
|
||||
}
|
||||
}
|
||||
|
||||
pub(super) fn truthy(&self) -> bool {
|
||||
match self {
|
||||
SbsValue::Bool(b) => *b,
|
||||
SbsValue::Text(s) => s == "1" || s.eq_ignore_ascii_case("true"),
|
||||
_ => self.float(0) != 0.0,
|
||||
}
|
||||
}
|
||||
|
||||
pub(super) fn component_count(&self) -> usize {
|
||||
match self {
|
||||
SbsValue::Floats(v) => v.len(),
|
||||
SbsValue::Ints(v) => v.len(),
|
||||
_ => 1,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pub(super) fn sbs_constant(holder: XmlNode) -> Option<SbsValue> {
|
||||
sbs_constant_element(holder.children().find(|child| child.is_element())?)
|
||||
}
|
||||
|
||||
pub(super) fn sbs_constant_element(element: XmlNode) -> Option<SbsValue> {
|
||||
let text = element.attribute("v").unwrap_or("");
|
||||
let tag = element.tag_name().name();
|
||||
|
||||
if tag == "constantValueBool" {
|
||||
Some(SbsValue::Bool(parse_ints(text).first().is_some_and(|b| *b != 0) || text.trim().eq_ignore_ascii_case("true")))
|
||||
} else if tag == "constantValueString" {
|
||||
Some(SbsValue::Text(text.to_string()))
|
||||
} else if tag.starts_with("constantValueFloat") {
|
||||
Some(SbsValue::Floats(parse_floats(text)))
|
||||
} else if tag.starts_with("constantValueInt") {
|
||||
Some(SbsValue::Ints(parse_ints(text)))
|
||||
} else {
|
||||
None
|
||||
}
|
||||
}
|
||||
|
||||
pub(super) fn to_param_value(like: &Value, value: &SbsValue, options: usize) -> Option<Value> {
|
||||
let f = |i| value.float(i);
|
||||
|
||||
@@ -1,11 +1,10 @@
|
||||
use super::{
|
||||
constant::{FIRST_UID, SELF_DEPENDENCY, UPDATER_VERSION},
|
||||
sbs_writer::SbsWriter,
|
||||
xml::xml_escape,
|
||||
};
|
||||
use crate::Result;
|
||||
use materialcraft_core::Package;
|
||||
use std::{collections::BTreeMap, fmt::Write as _};
|
||||
use std::collections::BTreeMap;
|
||||
|
||||
pub fn write_sbs(pkg: &Package) -> Result<Vec<u8>> {
|
||||
let format_version = pkg.format_version.as_deref().filter(|version| !version.trim().is_empty()).unwrap_or(UPDATER_VERSION);
|
||||
@@ -16,28 +15,6 @@ pub fn write_sbs(pkg: &Package) -> Result<Vec<u8>> {
|
||||
super::group_layout::write(&mut content, pkg, &mut writer, self_uid)?;
|
||||
content.push_str("</content>");
|
||||
|
||||
let mut out = String::with_capacity(content.len() + 4096);
|
||||
let _ = write!(
|
||||
out,
|
||||
"<?xml version=\"1.0\" encoding=\"UTF-8\"?><package><identifier v=\"{}\"/><formatVersion v=\"{}\"/><updaterVersion v=\"{UPDATER_VERSION}\"/><fileUID v=\"0\"/><versionUID v=\"0\"/><dependencies>",
|
||||
xml_escape(&pkg.name),
|
||||
xml_escape(format_version)
|
||||
);
|
||||
let write_dependency = |out: &mut String, file: &str, uid: u64| {
|
||||
let _ = write!(
|
||||
out,
|
||||
"<dependency><filename v=\"{}\"/><uid v=\"{uid}\"/><type v=\"package\"/><fileUID v=\"0\"/><versionUID v=\"0\"/></dependency>",
|
||||
xml_escape(file)
|
||||
);
|
||||
};
|
||||
|
||||
write_dependency(&mut out, SELF_DEPENDENCY, self_uid);
|
||||
for (file, uid) in &writer.external_packages {
|
||||
write_dependency(&mut out, file, *uid);
|
||||
}
|
||||
out.push_str("</dependencies>");
|
||||
out.push_str(&content);
|
||||
|
||||
out.push_str("</package>\n");
|
||||
Ok(out.into_bytes())
|
||||
let dependencies = std::iter::once((SELF_DEPENDENCY, self_uid)).chain(writer.external_packages.iter().map(|(file, uid)| (file.as_str(), *uid)));
|
||||
Ok(craft_sbs::write_package(&pkg.name, format_version, UPDATER_VERSION, dependencies, &content))
|
||||
}
|
||||
@@ -1,42 +1,4 @@
|
||||
use roxmltree::Node as XmlNode;
|
||||
|
||||
pub(super) fn parse_floats(text: &str) -> Vec<f32> {
|
||||
text.split_whitespace().take(16).map(|t| t.parse::<f32>().ok().filter(|v| v.is_finite()).unwrap_or(0.0)).collect()
|
||||
}
|
||||
pub(super) fn parse_ints(text: &str) -> Vec<i32> {
|
||||
let parse = |t: &str| t.parse::<i64>().map(|v| v.clamp(i64::from(i32::MIN), i64::from(i32::MAX)) as i32).or_else(|_| t.parse::<f64>().map(|v| v as i32));
|
||||
text.split_whitespace().take(16).map(|t| parse(t).unwrap_or(0)).collect()
|
||||
}
|
||||
pub(super) fn format_float(v: f32) -> String {
|
||||
if v.is_finite() { format!("{v}") } else { "0".into() }
|
||||
}
|
||||
|
||||
pub(super) fn child_element<'a, 'input>(node: XmlNode<'a, 'input>, tag: &str) -> Option<XmlNode<'a, 'input>> {
|
||||
node.children().find(|c| c.is_element() && c.tag_name().name() == tag)
|
||||
}
|
||||
|
||||
pub(super) fn children_named<'a, 'input: 'a>(node: XmlNode<'a, 'input>, tag: &'a str) -> impl Iterator<Item = XmlNode<'a, 'input>> + 'a {
|
||||
node.children().filter(move |c| c.is_element() && c.tag_name().name() == tag)
|
||||
}
|
||||
pub(super) fn child_value<'a>(node: XmlNode<'a, '_>, tag: &str) -> Option<&'a str> {
|
||||
child_element(node, tag).and_then(|c| c.attribute("v"))
|
||||
}
|
||||
pub(super) fn xml_escape(text: &str) -> String {
|
||||
let mut out = String::with_capacity(text.len());
|
||||
// SBS historically escapes apostrophes too, although attributes are double quoted.
|
||||
// Keep that byte-level policy while sharing XML validity/attribute escaping.
|
||||
for (i, part) in text.split('\'').enumerate() {
|
||||
if i != 0 {
|
||||
out.push_str("'");
|
||||
}
|
||||
craft_xml::push_esc_attr(&mut out, part);
|
||||
}
|
||||
out
|
||||
}
|
||||
pub(super) fn is_parameters_element(xml: &str) -> bool {
|
||||
let options = roxmltree::ParsingOptions { allow_dtd: false, nodes_limit: 1_000_000 };
|
||||
xml.len() < (16 << 20) && roxmltree::Document::parse_with_options(xml, options).is_ok_and(|d| d.root_element().tag_name().name() == "parameters")
|
||||
}
|
||||
pub(super) use craft_sbs::xml::{child_element, child_value, children_named, format_float, is_parameters_element, parse_floats, xml_escape};
|
||||
|
||||
#[cfg(test)]
|
||||
mod shared_escape_tests {
|
||||
|
||||
Reference in new issue
Block a user