mirror of
https://github.com/storytold/materialcraft.git
synced 2026-11-30 01:53:54 +01:00
formats: use a set for unique SBS resource names (#242)
Every resource rebuilt a vector of all existing names and scanned it linearly, making duplicate-heavy packages quadratic on open. Track used names in a HashSet and add a duplicate-name regression test.
This commit is contained in:
1 parent
3a08213c55
commit
cd9addceb4
3 files changed
+21
-8
No files matched your search
@@ -9,7 +9,7 @@ use super::{
|
||||
use crate::{FormatError, Result};
|
||||
use materialcraft_core::Package;
|
||||
use roxmltree::Node as XmlNode;
|
||||
use std::collections::HashMap;
|
||||
use std::collections::{HashMap, HashSet};
|
||||
|
||||
pub fn read_sbs(bytes: &[u8], name: &str, linked: &dyn Fn(&str) -> Option<Vec<u8>>) -> Result<SbsRead> {
|
||||
if bytes.len() as u64 > MAX_SBS_BYTES {
|
||||
@@ -39,6 +39,7 @@ pub fn read_sbs(bytes: &[u8], name: &str, linked: &dyn Fn(&str) -> Option<Vec<u8
|
||||
dependencies: HashMap::new(),
|
||||
graph_names: HashMap::new(),
|
||||
resource_names: HashMap::new(),
|
||||
used_resource_names: HashSet::new(),
|
||||
node_count: 0,
|
||||
wire_count: 0,
|
||||
};
|
||||
|
||||
@@ -18,7 +18,7 @@ use materialcraft_core::{
|
||||
};
|
||||
use materialcraft_nodes::find;
|
||||
use roxmltree::Node as XmlNode;
|
||||
use std::collections::HashMap;
|
||||
use std::collections::{HashMap, HashSet};
|
||||
|
||||
pub(super) struct SbsReader<'text, 'warnings> {
|
||||
pub(super) text: &'text str,
|
||||
@@ -26,6 +26,7 @@ pub(super) struct SbsReader<'text, 'warnings> {
|
||||
pub(super) dependencies: HashMap<String, String>,
|
||||
pub(super) graph_names: HashMap<String, String>,
|
||||
pub(super) resource_names: HashMap<String, String>,
|
||||
pub(super) used_resource_names: HashSet<String>,
|
||||
pub(super) node_count: usize,
|
||||
pub(super) wire_count: usize,
|
||||
}
|
||||
@@ -114,8 +115,7 @@ impl<'text> SbsReader<'text, '_> {
|
||||
}
|
||||
}
|
||||
|
||||
let taken: Vec<String> = pkg.resources.iter().map(|r| r.name.clone()).collect();
|
||||
let name = unique_name(&taken, if identifier.is_empty() { "resource" } else { identifier });
|
||||
let name = unique_name(&mut self.used_resource_names, if identifier.is_empty() { "resource" } else { identifier });
|
||||
self.resource_names.insert(path.to_string(), name.clone());
|
||||
match bytes {
|
||||
Some(data) => {
|
||||
@@ -585,12 +585,11 @@ impl<'text> SbsReader<'text, '_> {
|
||||
}
|
||||
}
|
||||
}
|
||||
pub(super) fn unique_name(taken: &[String], base: &str) -> String {
|
||||
let free = |name: &str| !taken.iter().any(|t| t == name);
|
||||
if free(base) {
|
||||
pub(super) fn unique_name(taken: &mut HashSet<String>, base: &str) -> String {
|
||||
if taken.insert(base.to_string()) {
|
||||
return base.to_string();
|
||||
}
|
||||
(1..).map(|i| format!("{base}_{i}")).find(|name| free(name)).unwrap_or_else(|| base.to_string())
|
||||
(1..).map(|i| format!("{base}_{i}")).find(|name| taken.insert(name.clone())).unwrap_or_else(|| base.to_string())
|
||||
}
|
||||
pub(super) fn push_node<'g>(graph: &'g mut Graph, def: &str, pos: [f32; 2]) -> (NodeId, usize, Option<&'g mut Node>) {
|
||||
let id = graph.add_node(def, pos);
|
||||
|
||||
@@ -254,6 +254,19 @@ fn linked_resources_load_through_the_callback() {
|
||||
assert!(not_found.warnings.iter().any(|w| w.contains("img")), "{:?}", not_found.warnings);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn duplicate_resource_identifiers_get_unique_names() {
|
||||
let resources: String = (0..2_000)
|
||||
.map(|_| "<resource><identifier v=\"img\"/><type v=\"bitmap\"/><format v=\"png\"/><source><binembedded><strdata v=\"AAAA\"/></binembedded></source></resource>")
|
||||
.collect();
|
||||
let xml = format!("<package><content>{resources}</content></package>");
|
||||
let read = read_sbs(xml.as_bytes(), "many.sbs", &no_linked_files).unwrap();
|
||||
let names: std::collections::HashSet<&str> = read.pkg.resources.iter().map(|resource| resource.name.as_str()).collect();
|
||||
assert_eq!(names.len(), read.pkg.resources.len(), "resource names stay unique");
|
||||
assert!(read.pkg.resources[0].name == "img");
|
||||
assert!(read.pkg.resources.iter().skip(1).all(|resource| resource.name.starts_with("img_")));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn embedded_data_round_trips_and_checks_its_length() {
|
||||
let data: Vec<u8> = (0..5000u32).map(|i| (i * 7 % 251) as u8).collect();
|
||||
|
||||
Reference in new issue
Block a user