Merge pull request #43 from simonhull/fix/correct-cache-keys

Eval: key resources on content and graph instances on their whole sub-graph tree
This commit is contained in:
Rafael Mendes authored and GitHub committed 2026-11-05 11:22:32 -03:00
commit c987aaa3fa
8 files changed
+302 -36

No files matched your search

+1 -1
View File
@@ -17,7 +17,7 @@ pub use graph::{
Attributes, BaseParams, Comment, Connection, ExposedParam, Frame, Graph, GraphKind, MAX_SIZE_LOG2, MIN_SIZE_LOG2, MISSING_NODE, Node, NodeId, OutputFormat,
Pin, Preset, SizeMode, missing_ports,
};
pub use package::{PACKAGE_EXT, Package, Resource};
pub use package::{PACKAGE_EXT, Package, Resource, ResourceBytes};
pub use value::{Curve, CurvePoint, Gradient, GradientKey, Value, linear_to_srgb, parse_color, srgb_to_linear};
#[derive(Debug, thiserror::Error)]
+80 -1
View File
@@ -1,5 +1,6 @@
//! Packages: the document. A package holds graphs and the resources (bitmaps) they use.
use std::hash::{DefaultHasher, Hash, Hasher};
use std::sync::Arc;
use serde::{Deserialize, Serialize};
@@ -18,7 +19,68 @@ pub struct Resource {
pub file: String,
/// The file's bytes (stored in the zip, not in `package.json`).
#[serde(skip)]
pub bytes: Arc<Vec<u8>>,
pub bytes: ResourceBytes,
}
impl Resource {
/// A resource named `name`, stored as `file` in the package.
pub fn new(name: impl Into<String>, file: impl Into<String>, bytes: ResourceBytes) -> Resource {
Resource { name: name.into(), file: file.into(), bytes }
}
}
/// A resource's bytes with a hash of their whole content, computed once when the bytes are
/// set, so caches can key on content (two files of one size are not the same image).
#[derive(Clone, Debug)]
pub struct ResourceBytes {
bytes: Arc<Vec<u8>>,
hash: u64,
}
impl ResourceBytes {
/// Take ownership of `bytes` and hash them.
pub fn new(bytes: Vec<u8>) -> ResourceBytes {
let mut h = DefaultHasher::new();
bytes.hash(&mut h);
ResourceBytes { hash: h.finish(), bytes: Arc::new(bytes) }
}
/// The bytes.
pub fn as_slice(&self) -> &[u8] {
&self.bytes
}
/// Number of bytes.
pub fn len(&self) -> usize {
self.bytes.len()
}
/// Whether there are no bytes.
pub fn is_empty(&self) -> bool {
self.bytes.is_empty()
}
/// Hash of the whole content.
pub fn content_hash(&self) -> u64 {
self.hash
}
/// Whether both values share one allocation (cheap clones of one file).
pub fn ptr_eq(&self, other: &ResourceBytes) -> bool {
Arc::ptr_eq(&self.bytes, &other.bytes)
}
}
impl Default for ResourceBytes {
fn default() -> Self {
ResourceBytes::new(Vec::new())
}
}
impl PartialEq for ResourceBytes {
fn eq(&self, other: &Self) -> bool {
self.hash == other.hash && self.bytes == other.bytes
}
}
#[derive(Clone, Debug, Default, PartialEq, Serialize, Deserialize)]
@@ -66,3 +128,20 @@ impl Package {
(1..).map(|i| format!("{base}_{i}")).find(|n| self.graph_by_name(n).is_none()).unwrap_or_else(|| base.to_string())
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn same_length_same_prefix_bytes_hash_differently() {
let a = vec![7u8; 200];
let mut b = a.clone();
b[150] = 8;
let (a, b) = (ResourceBytes::new(a), ResourceBytes::new(b));
assert_eq!(a.len(), b.len());
assert_ne!(a.content_hash(), b.content_hash());
assert_ne!(a, b);
assert_eq!(ResourceBytes::default(), ResourceBytes::new(Vec::new()));
}
}
+3 -2
View File
@@ -7,7 +7,7 @@
use serde_json::{Value as J, json};
use materialcraft_core::{Package, Resource};
use materialcraft_core::{Package, Resource, ResourceBytes};
use materialcraft_formats::{Depth, encode, output_name};
use crate::commands::{CommandSpec, always, bad, has_graph, strp, u64p};
@@ -95,6 +95,7 @@ fn new_package(s: &mut Session, p: &J) -> Result<J> {
/// Make `pkg` the session's document (after open/new): history and views reset.
pub fn replace_package(s: &mut Session, pkg: Package, path: Option<String>) {
s.pkg = pkg;
s.clear_decoded();
s.path = path;
s.graph = 0;
s.selection.clear();
@@ -355,7 +356,7 @@ pub(crate) fn push_resource(pkg: &mut Package, file: &str, bytes: Vec<u8>, name:
name = format!("{stem}_{i}");
i += 1;
}
pkg.resources.push(Resource { name: name.clone(), file: format!("{name}_{file}"), bytes: std::sync::Arc::new(bytes) });
pkg.resources.push(Resource::new(name.clone(), format!("{name}_{file}"), ResourceBytes::new(bytes)));
name
}
+12 -5
View File
@@ -4,7 +4,7 @@
use std::collections::HashMap;
use std::sync::{Arc, Mutex, PoisonError};
use materialcraft_core::{Graph, NodeId, Package};
use materialcraft_core::{Graph, NodeId, Package, ResourceBytes};
use materialcraft_eval::{Cache, Cooked, Options};
use materialcraft_image::Image;
use serde::{Deserialize, Serialize};
@@ -158,7 +158,7 @@ pub struct Session {
redo: Vec<Snapshot>,
/// Edits with the same key coalesce into one undo step (slider drags).
merge_key: Option<String>,
decoded: Arc<Mutex<HashMap<String, Image>>>,
decoded: Arc<Mutex<HashMap<u64, Image>>>,
}
impl Session {
@@ -298,20 +298,27 @@ impl Session {
/// A decoder for the package's bitmap resources (memoized, shareable with worker threads).
pub fn resource_decoder(&self) -> impl Fn(&str) -> Option<Image> + Sync + Send + 'static {
let res: Vec<(String, Arc<Vec<u8>>)> = self.pkg.resources.iter().map(|r| (r.name.clone(), r.bytes.clone())).collect();
let res: Vec<(String, ResourceBytes)> = self.pkg.resources.iter().map(|r| (r.name.clone(), r.bytes.clone())).collect();
let memo = self.decoded.clone();
// Free images of resources that were deleted or replaced.
memo.lock().unwrap_or_else(PoisonError::into_inner).retain(|k, _| res.iter().any(|(_, b)| b.content_hash() == *k));
move |name: &str| {
let (_, bytes) = res.iter().find(|(n, _)| n == name)?;
let key = format!("{name}:{}", bytes.len());
let key = bytes.content_hash();
if let Some(i) = memo.lock().unwrap_or_else(PoisonError::into_inner).get(&key) {
return Some(i.clone());
}
let img = materialcraft_formats::decode(bytes).ok()?;
let img = materialcraft_formats::decode(bytes.as_slice()).ok()?;
memo.lock().unwrap_or_else(PoisonError::into_inner).insert(key, img.clone());
Some(img)
}
}
/// Forget every decoded resource image (the package was replaced).
pub(crate) fn clear_decoded(&self) {
self.decoded.lock().unwrap_or_else(PoisonError::into_inner).clear();
}
/// Cook the open graph now (at the preview size), on this thread.
pub fn cook(&mut self) -> &Cooked {
let dec = self.resource_decoder();
+16
View File
@@ -343,6 +343,22 @@ fn camera_prefs_bound_the_pan_and_survive_hostile_values() {
assert_eq!(CameraPrefs { pan_limit: f32::INFINITY, pan_speed: 2.0 }.sanitized().pan_speed, 2.0);
}
#[test]
fn reimported_bitmap_shows_new_pixels() {
use materialcraft_formats::{Depth, encode};
use materialcraft_image::{Image, Kind};
let bmp = |v: f32| encode(&Image::filled(4, 4, Kind::Color, [v, v, v, 1.0]), "bmp", Depth::U8, true).unwrap();
let (dark, light) = (bmp(0.1), bmp(0.9));
assert_eq!(dark.len(), light.len(), "same-size files are the case under test");
let mut s = sample_session();
materialcraft_engine::file_cmds::add_resource(&mut s, "tex.bmp", dark, Some("tex")).unwrap();
let before = s.resource_decoder()("tex").unwrap().data[0];
s.undo();
materialcraft_engine::file_cmds::add_resource(&mut s, "tex.bmp", light, Some("tex")).unwrap();
let after = s.resource_decoder()("tex").unwrap().data[0];
assert!(after - before > 0.5, "{before} -> {after}");
}
#[test]
fn a_wire_splits_with_a_reroute_dot_and_deletes() {
let mut s = sample_session();
+176 -12
View File
@@ -9,7 +9,7 @@
#![forbid(unsafe_code)]
#![deny(clippy::unwrap_used, clippy::expect_used, clippy::panic, clippy::unimplemented, clippy::todo, clippy::unreachable)]
use std::collections::{BTreeMap, HashMap};
use std::collections::{BTreeMap, HashMap, HashSet};
use std::hash::{DefaultHasher, Hash, Hasher};
use std::sync::Arc;
use std::sync::atomic::{AtomicBool, Ordering};
@@ -184,11 +184,13 @@ pub struct Options<'a> {
/// Set from another thread to stop between nodes.
pub cancel: Option<&'a AtomicBool>,
pub depth: u32,
/// Graphs being cooked around this one, outermost first (cycle detection).
pub graph_stack: Vec<usize>,
}
impl<'a> Options<'a> {
pub fn new(resource: &'a (dyn Fn(&str) -> Option<Image> + Sync)) -> Options<'a> {
Options { max_size: None, only: None, inputs: BTreeMap::new(), overrides: BTreeMap::new(), resource, cancel: None, depth: 0 }
Options { max_size: None, only: None, inputs: BTreeMap::new(), overrides: BTreeMap::new(), resource, cancel: None, depth: 0, graph_stack: Vec::new() }
}
}
@@ -267,7 +269,7 @@ pub fn cook(pkg: &Package, gi: usize, opts: &Options, cache: &mut Cache) -> Cook
let opts = if opts.depth == 0 && !graph.input_images.is_empty() {
let mut inputs: BTreeMap<String, Image> = graph.input_images.iter().filter_map(|(id, res)| Some((id.clone(), (opts.resource)(res)?))).collect();
inputs.extend(opts.inputs.iter().map(|(k, v)| (k.clone(), v.clone())));
bound = Options { only: opts.only.clone(), inputs, overrides: opts.overrides.clone(), ..*opts };
bound = Options { only: opts.only.clone(), inputs, overrides: opts.overrides.clone(), graph_stack: opts.graph_stack.clone(), ..*opts };
&bound
} else {
opts
@@ -294,14 +296,50 @@ pub fn cook(pkg: &Package, gi: usize, opts: &Options, cache: &mut Cache) -> Cook
break;
}
let Ok(node) = graph.node(id) else { continue };
let r = cook_node(pkg, graph, node, &out, &vars, opts, cache);
let r = cook_node(pkg, gi, graph, node, &out, &vars, opts, cache);
out.nodes.insert(id, r);
}
out.ms = (now_ms() - t0) as f32;
out
}
fn cook_node(pkg: &Package, graph: &Graph, node: &Node, done: &Cooked, vars: &BTreeMap<String, Value>, opts: &Options, cache: &mut Cache) -> NodeResult {
/// Hash each reachable graph and its resource content once. An iterative walk keeps
/// deep chains and graphs with many shared instances bounded by the package size.
fn graph_tree_hash(pkg: &Package, gi: usize) -> u64 {
let mut h = DefaultHasher::new();
let mut pending = vec![gi];
let mut seen = HashSet::new();
while let Some(index) = pending.pop() {
if !seen.insert(index) {
continue;
}
let Some(graph) = pkg.graphs.get(index) else { continue };
index.hash(&mut h);
serde_json::to_string(graph).unwrap_or_default().hash(&mut h);
for n in &graph.nodes {
if let Some(Value::Text(name)) = n.params.get("resource") {
pkg.resource(name).map(|r| r.bytes.content_hash()).hash(&mut h);
}
if n.def == "graph_instance"
&& let Some((si, _)) = instance_graph(pkg, n)
{
pending.push(si);
}
}
}
h.finish()
}
fn cook_node(
pkg: &Package,
gi: usize,
graph: &Graph,
node: &Node,
done: &Cooked,
vars: &BTreeMap<String, Value>,
opts: &Options,
cache: &mut Cache,
) -> NodeResult {
let (in_ports, out_ports) = ports(pkg, node);
let def = find(&node.def);
// Gather inputs by port order, with their keys.
@@ -362,11 +400,10 @@ fn cook_node(pkg: &Package, graph: &Graph, node: &Node, done: &Cooked, vars: &BT
// Input nodes read the caller's image for their identifier.
let input_img = node.def.starts_with("input_").then(|| opts.inputs.get(&identifier(node))).flatten();
let res_hash = match params.get("resource") {
Some(Value::Text(name)) => pkg.resource(name).map_or(0, |r| hash_of(&(r.bytes.len(), r.bytes.get(..64.min(r.bytes.len())).map(|b| b.to_vec())))),
Some(Value::Text(name)) => pkg.resource(name).map_or(0, |r| r.bytes.content_hash()),
_ => 0,
};
let sub_hash =
if node.def == "graph_instance" { instance_graph(pkg, node).map_or(0, |(_, g)| hash_of(&serde_json::to_string(g).unwrap_or_default())) } else { 0 };
let sub_hash = if node.def == "graph_instance" { instance_graph(pkg, node).map_or(0, |(si, _)| graph_tree_hash(pkg, si)) } else { 0 };
let key = hash_of(&(
&node.def,
serde_json::to_string(&params).unwrap_or_default(),
@@ -390,7 +427,7 @@ fn cook_node(pkg: &Package, graph: &Graph, node: &Node, done: &Cooked, vars: &BT
let name = if node.label.is_empty() { "this node" } else { node.label.as_str() };
Err(format!("`{name}` is not in MaterialCraft's library yet: kept as a placeholder from the SBS file"))
} else if node.def == "graph_instance" {
cook_instance(pkg, node, &in_ports, &out_ports, &inputs, &params, (w, h), opts, cache)
cook_instance(pkg, gi, node, &in_ports, &out_ports, &inputs, &params, (w, h), opts, cache)
} else if let Some(d) = def {
let ctx = Ctx { w, h, seed, tile, inputs: &inputs, params: &params, resource: opts.resource, vars };
(d.cook)(&ctx)
@@ -408,8 +445,28 @@ fn cook_node(pkg: &Package, graph: &Graph, node: &Node, done: &Cooked, vars: &BT
}
}
/// Whether graph `from` reaches `target`, without recursive stack growth.
fn instances_reach(pkg: &Package, from: usize, target: usize) -> bool {
let mut pending = vec![from];
let mut seen = HashSet::new();
while let Some(index) = pending.pop() {
if !seen.insert(index) {
continue;
}
let Some(graph) = pkg.graphs.get(index) else { continue };
for (si, _) in graph.nodes.iter().filter(|n| n.def == "graph_instance").filter_map(|n| instance_graph(pkg, n)) {
if si == target {
return true;
}
pending.push(si);
}
}
false
}
fn cook_instance(
pkg: &Package,
gi: usize,
node: &Node,
in_ports: &[Port],
out_ports: &[Port],
@@ -422,14 +479,19 @@ fn cook_instance(
if opts.depth >= MAX_DEPTH {
return Err("graph instances nest too deep (does a graph use itself?)".into());
}
let (gi, sub) = instance_graph(pkg, node).ok_or("the instanced graph does not exist")?;
let (sub_gi, sub) = instance_graph(pkg, node).ok_or("the instanced graph does not exist")?;
if sub_gi == gi || opts.graph_stack.contains(&sub_gi) || instances_reach(pkg, sub_gi, gi) {
return Err(format!("graph `{}` uses itself through a graph instance", sub.name));
}
let mut o = Options::new(opts.resource);
o.max_size = opts.max_size;
o.cancel = opts.cancel;
o.depth = opts.depth + 1;
o.graph_stack = opts.graph_stack.clone();
o.graph_stack.push(gi);
o.inputs = in_ports.iter().zip(inputs).filter_map(|(p, i)| Some((p.id.clone(), i.clone()?))).collect();
o.overrides = params.iter().filter(|(k, _)| sub.exposed.iter().any(|e| &e.id == *k)).map(|(k, v)| (k.clone(), v.clone())).collect();
let cooked = cook(pkg, gi, &o, cache);
let cooked = cook(pkg, sub_gi, &o, cache);
if cooked.cancelled {
return Err("cancelled".into());
}
@@ -561,7 +623,109 @@ mod tests {
let c = cook(&p, 0, &Options::new(&no_res), &mut cache);
let img = c.image(inst, 0).unwrap();
assert!((img.data[0] - (1.0 - 0.0722 * 0.25 - 0.2126 * 0.25 - 0.7152 * 0.25)).abs() < 1e-4, "{}", img.data[0]);
assert!(c.nodes[&selfi].error.is_some() || c.nodes[&selfi].outputs.len() == 1);
assert!(c.nodes[&selfi].error.as_deref().is_some_and(|e| e.contains("uses itself")), "{:?}", c.nodes[&selfi].error);
}
/// A graph `name` holding one instance of `target` wired to an output.
fn instancing(name: &str, target: &str) -> (Graph, NodeId) {
let mut g = Graph::new(name);
g.size = [4, 4];
let inst = g.add_node("graph_instance", [0.0, 0.0]);
g.node_mut(inst).unwrap().params.insert("graph".into(), Value::Text(target.into()));
let o = g.add_node("output", [100.0, 0.0]);
g.connect(inst, "output", o, "input").unwrap();
(g, inst)
}
#[test]
fn editing_a_nested_graph_recooks_the_top() {
let mut c = Graph::new("c");
c.size = [4, 4];
let u = c.add_node("uniform", [0.0, 0.0]);
c.node_mut(u).unwrap().params.insert("mode".into(), Value::Enum(1));
c.node_mut(u).unwrap().params.insert("color".into(), Value::Color([0.2, 0.2, 0.2, 1.0]));
let o = c.add_node("output", [100.0, 0.0]);
c.connect(u, "output", o, "input").unwrap();
let (b, _) = instancing("b", "c");
let (a, top) = instancing("a", "b");
let mut p = Package::new("p");
p.graphs = vec![a, b, c];
let mut cache = Cache::default();
let opts = Options::new(&no_res);
let first = cook(&p, 0, &opts, &mut cache);
let out_a = p.graphs[0].nodes.iter().find(|n| n.def == "output").unwrap().id;
assert!((first.image(out_a, 0).unwrap().mean()[0] - 0.2).abs() < 0.01);
p.graphs[2].node_mut(u).unwrap().params.insert("color".into(), Value::Color([0.8, 0.8, 0.8, 1.0]));
let second = cook(&p, 0, &opts, &mut cache);
assert!(!second.nodes[&top].cached);
assert!((second.image(out_a, 0).unwrap().mean()[0] - 0.8).abs() < 0.01);
}
#[test]
fn deep_shared_instance_walks_stay_bounded() {
std::thread::Builder::new()
.stack_size(128 * 1024)
.spawn(|| {
let mut pkg = Package::new("deep");
for index in 0..2000 {
let mut graph = Graph::new(&format!("g{index}"));
// Repeated references must visit the next graph only once.
for _ in 0..4 {
let id = graph.add_node("graph_instance", [0.0; 2]);
graph.node_mut(id).unwrap().params.insert("graph".into(), Value::Text(format!("g{}", index + 1)));
}
pkg.graphs.push(graph);
}
assert!(instances_reach(&pkg, 0, 1999));
assert!(!instances_reach(&pkg, 0, 2000));
let before = graph_tree_hash(&pkg, 0);
pkg.graphs[1999].seed = 42;
assert_ne!(before, graph_tree_hash(&pkg, 0));
// A back edge must terminate both traversals too.
pkg.graphs[1999].nodes[0].params.insert("graph".into(), Value::Text("g0".into()));
assert!(instances_reach(&pkg, 1, 0));
assert!(!instances_reach(&pkg, 0, 2000));
assert_ne!(before, graph_tree_hash(&pkg, 0));
})
.unwrap()
.join()
.unwrap();
}
#[test]
fn mutual_instances_are_an_error() {
let (x, xi) = instancing("x", "y");
let (y, _) = instancing("y", "x");
let mut p = Package::new("p");
p.graphs = vec![x, y];
let mut cache = Cache::default();
let c = cook(&p, 0, &Options::new(&no_res), &mut cache);
assert!(c.nodes[&xi].error.as_deref().is_some_and(|e| e.contains("uses itself")), "{:?}", c.nodes[&xi].error);
}
#[test]
fn same_size_resources_key_differently() {
use materialcraft_core::{Resource, ResourceBytes};
use std::sync::atomic::AtomicU8;
let mut g = Graph::new("g");
g.size = [4, 4];
let bm = g.add_node("bitmap", [0.0, 0.0]);
g.node_mut(bm).unwrap().params.insert("resource".into(), Value::Text("tex".into()));
let mut p = Package::new("p");
p.graphs.push(g);
p.resources.push(Resource::new("tex", "tex.bin", ResourceBytes::new(vec![1; 100])));
// Stands in for decoding: the first byte picks the shade.
let shade = AtomicU8::new(1);
let res = |_: &str| Some(Image::filled(4, 4, Kind::Gray, [f32::from(shade.load(Ordering::Relaxed)) / 10.0; 4]));
let mut cache = Cache::default();
let opts = Options::new(&res);
let first = cook(&p, 0, &opts, &mut cache);
let mean = first.image(bm, 0).unwrap().mean()[0];
p.resources[0].bytes = ResourceBytes::new(vec![2; 100]);
shade.store(2, Ordering::Relaxed);
let second = cook(&p, 0, &opts, &mut cache);
assert!(!second.nodes[&bm].cached);
assert!((second.image(bm, 0).unwrap().mean()[0] - mean).abs() > 0.05);
}
#[test]
+9 -9
View File
@@ -16,7 +16,7 @@ use std::collections::HashMap;
use std::io::{Cursor, Read, Write};
use std::sync::Arc;
use materialcraft_core::{OutputFormat, Package};
use materialcraft_core::{OutputFormat, Package, ResourceBytes};
use materialcraft_image::{Image, Kind, MAX_SIDE, linear_to_srgb, srgb_to_linear};
#[derive(Debug, thiserror::Error)]
@@ -60,7 +60,7 @@ pub fn save_package(pkg: &Package) -> Result<Vec<u8>> {
// Images are already compressed.
let stored = zip::write::SimpleFileOptions::default().compression_method(zip::CompressionMethod::Stored);
z.start_file(format!("resources/{}", safe_file_name(&r.file)), stored).map_err(io)?;
z.write_all(&r.bytes).map_err(|e| FormatError::Io(e.to_string()))?;
z.write_all(r.bytes.as_slice()).map_err(|e| FormatError::Io(e.to_string()))?;
}
z.finish().map_err(io)?;
}
@@ -86,11 +86,11 @@ fn load_package_within(bytes: &[u8], max_total: u64) -> Result<Package> {
return Err(FormatError::BadPackage(format!("{} resources (limit {MAX_RESOURCES})", pkg.resources.len())));
}
let mut total = json.len() as u64;
let mut read: HashMap<String, Arc<Vec<u8>>> = HashMap::new();
let mut read: HashMap<String, ResourceBytes> = HashMap::new();
for r in &mut pkg.resources {
let name = format!("resources/{}", safe_file_name(&r.file));
if let Some(shared) = read.get(&name) {
r.bytes = Arc::clone(shared);
r.bytes = shared.clone();
continue;
}
let mut data = Vec::new();
@@ -101,8 +101,8 @@ fn load_package_within(bytes: &[u8], max_total: u64) -> Result<Package> {
if total > max_total {
return Err(FormatError::BadPackage(format!("package resources exceed {max_total} bytes")));
}
let data = Arc::new(data);
read.insert(name, Arc::clone(&data));
let data = ResourceBytes::new(data);
read.insert(name, data.clone());
r.bytes = data;
}
Ok(pkg)
@@ -285,12 +285,12 @@ mod tests {
g.connect(a, "output", o, "input").unwrap();
p.graphs.push(g);
let png = encode(&Image::filled(4, 4, Kind::Color, [1.0, 0.5, 0.0, 1.0]), "png", Depth::U8, true).unwrap();
p.resources.push(Resource { name: "tex".into(), file: "../../evil/tex.png".into(), bytes: Arc::new(png) });
p.resources.push(Resource::new("tex", "../../evil/tex.png", ResourceBytes::new(png)));
let bytes = save_package(&p).unwrap();
let q = load_package(&bytes).unwrap();
assert_eq!(q.graphs, p.graphs);
assert_eq!(q.resources[0].bytes, p.resources[0].bytes);
let img = decode(&q.resources[0].bytes).unwrap();
let img = decode(q.resources[0].bytes.as_slice()).unwrap();
assert!((img.get(0, 0, false)[1] - 0.5).abs() < 0.01);
assert!(load_package(b"not a zip").is_err());
}
@@ -349,7 +349,7 @@ mod tests {
let pkg = load_package(&bytes).unwrap();
assert_eq!(pkg.resources.len(), 1000);
assert_eq!(pkg.resources[0].bytes.len(), 1 << 20);
assert!(Arc::ptr_eq(&pkg.resources[0].bytes, &pkg.resources[999].bytes));
assert!(pkg.resources[0].bytes.ptr_eq(&pkg.resources[999].bytes));
}
#[test]
+5 -6
View File
@@ -5,12 +5,11 @@
use std::collections::{BTreeMap, BTreeSet, HashMap, HashSet, VecDeque};
use std::fmt::Write as _;
use std::io::{Read, Write};
use std::sync::Arc;
use base64::Engine as _;
use materialcraft_core::{
BaseParams, Comment, Connection, ExposedParam, Frame, Graph, MAX_SIZE_LOG2, MIN_SIZE_LOG2, MISSING_NODE, Node, NodeId, OutputFormat, Package, Resource,
SizeMode, Value, linear_to_srgb, missing_ports, srgb_to_linear,
ResourceBytes, SizeMode, Value, linear_to_srgb, missing_ports, srgb_to_linear,
};
use materialcraft_nodes::{PortKind, find};
use roxmltree::Node as XmlNode;
@@ -776,7 +775,7 @@ impl<'text> Reader<'text, '_> {
match bytes {
Some(data) => {
let ext = if kind == "svg" { "svg".to_string() } else { format };
pkg.resources.push(Resource { name: name.clone(), file: crate::safe_file_name(&format!("{name}.{ext}")), bytes: Arc::new(data) });
pkg.resources.push(Resource::new(name.clone(), crate::safe_file_name(&format!("{name}.{ext}")), ResourceBytes::new(data)));
}
None => {
let why = if why_missing.is_empty() { "it has no data".to_string() } else { why_missing };
@@ -1369,7 +1368,7 @@ pub fn write_sbs(pkg: &Package) -> Result<Vec<u8>> {
if svg { "svg" } else { "bitmap" },
xml_escape(ext),
resource.bytes.len(),
encode_embedded(&resource.bytes)?
encode_embedded(resource.bytes.as_slice())?
);
}
content.push_str("</content></group>");
@@ -1853,7 +1852,7 @@ mod tests {
let orange = materialcraft_image::Image::filled(4, 4, materialcraft_image::Kind::Color, [1.0, 0.5, 0.0, 1.0]);
let png = crate::encode(&orange, "png", crate::Depth::U8, true).unwrap();
pkg.resources.push(Resource { name: "tex".into(), file: "tex.png".into(), bytes: Arc::new(png) });
pkg.resources.push(Resource::new("tex", "tex.png", ResourceBytes::new(png)));
pkg
}
@@ -2076,7 +2075,7 @@ mod tests {
assert!(read.warnings.iter().any(|w| w.contains("missing node `3D_perlin_noise`")), "{:?}", &read.warnings[..5]);
assert!(read.pkg.resources.iter().any(|r| r.name == "Untitled_Bitmap" && !r.bytes.is_empty()));
let svg = read.pkg.resources.iter().find(|r| r.name == "Untitled_Vector_Graphics").unwrap();
assert!(String::from_utf8_lossy(&svg.bytes).contains("<svg"));
assert!(String::from_utf8_lossy(svg.bytes.as_slice()).contains("<svg"));
assert_eq!(graph.frames.len(), 26);
assert_eq!(graph.size, [11, 11]);