mirror of
https://github.com/storytold/texturecraft.git
synced 2026-11-29 17:44:09 +01:00
Write TextureCraft browser downloads through craft-zip (#792)
This commit is contained in:
1 parent
2037b6b823
commit
9f955aabb6
3 files changed
+25
-14
No files matched your search
Generated
+3
-2
@@ -5293,6 +5293,7 @@ dependencies = [
|
||||
"craft-ui",
|
||||
"craft-update",
|
||||
"craft-window",
|
||||
"craft-zip 0.1.0 (git+https://github.com/storytold/craft-libs?rev=c376774f15ae7cd3b06a7ac664f01a8645af4b83)",
|
||||
"eframe",
|
||||
"egui",
|
||||
"egui-phosphor",
|
||||
@@ -7079,9 +7080,9 @@ dependencies = [
|
||||
|
||||
[[package]]
|
||||
name = "zip"
|
||||
version = "9.8.1"
|
||||
version = "9.8.2"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "a9534624d547e3f516ecde0ed9a70d37788f01d340efcfdd03bf3ea8e163c1d0"
|
||||
checksum = "2d8dbe089cf6b0448b78f5fc50139ab5aaa40d08ac07cdc1baf2c13add35fcce"
|
||||
dependencies = [
|
||||
"constant_time_eq",
|
||||
"crc32fast",
|
||||
|
||||
@@ -27,7 +27,6 @@ log = { workspace = true }
|
||||
base64 = "0.22"
|
||||
image = { workspace = true }
|
||||
craft-image-codecs = { workspace = true }
|
||||
zip = { workspace = true }
|
||||
|
||||
# The OS UI language for the first run (MIT OR Apache-2.0; the browser asks `navigator.language`).
|
||||
[target.'cfg(not(target_arch = "wasm32"))'.dependencies]
|
||||
@@ -62,12 +61,17 @@ skrifa = { version = "0.48", default-features = false, features = ["std"] }
|
||||
|
||||
# Browser file I/O (web_files.rs): async pickers and Blob downloads.
|
||||
[target.'cfg(target_arch = "wasm32")'.dependencies]
|
||||
craft-zip = { workspace = true }
|
||||
rfd = { workspace = true }
|
||||
wasm-bindgen = "0.2"
|
||||
wasm-bindgen-futures = "0.4"
|
||||
js-sys = "0.3"
|
||||
web-sys = { version = "0.3", features = ["Window", "Document", "HtmlElement", "HtmlAnchorElement", "Blob", "BlobPropertyBag", "Url", "CssStyleDeclaration", "Navigator"] }
|
||||
|
||||
[dev-dependencies]
|
||||
craft-zip = { workspace = true }
|
||||
zip = { workspace = true }
|
||||
|
||||
# winit needs a window system on Linux, so the crate's own tests build with X11.
|
||||
[target.'cfg(not(target_arch = "wasm32"))'.dev-dependencies]
|
||||
eframe = { workspace = true, features = ["x11"] }
|
||||
|
||||
@@ -9,6 +9,7 @@ use std::sync::{Mutex, PoisonError};
|
||||
|
||||
use texturecraft_engine::Session;
|
||||
use texturecraft_engine::core::PROJECT_EXT;
|
||||
#[cfg(any(target_arch = "wasm32", test))]
|
||||
use texturecraft_engine::formats::ExportedFile;
|
||||
use texturecraft_engine::formats::export::sanitize_file_stem;
|
||||
use texturecraft_engine::formats::mesh_import::MESH_EXTENSIONS;
|
||||
@@ -228,16 +229,14 @@ pub fn project_file_name(app: &TextureCraftApp) -> String {
|
||||
}
|
||||
|
||||
/// Pack exported textures into one zip archive.
|
||||
#[cfg_attr(not(target_arch = "wasm32"), allow(dead_code))]
|
||||
#[cfg(any(target_arch = "wasm32", test))]
|
||||
fn zip_files(files: Vec<ExportedFile>) -> Result<Vec<u8>, String> {
|
||||
use std::io::Write as _;
|
||||
let mut zip = zip::ZipWriter::new(std::io::Cursor::new(Vec::new()));
|
||||
let opts = zip::write::SimpleFileOptions::default().compression_method(zip::CompressionMethod::Deflated).large_file(true);
|
||||
let mut zip = craft_zip::Writer::new();
|
||||
for f in files {
|
||||
zip.start_file(f.name.as_str(), opts).map_err(|e| e.to_string())?;
|
||||
zip.write_all(&f.bytes).map_err(|e| e.to_string())?;
|
||||
// add_with reserves ZIP64 headers up front, even for small entries.
|
||||
zip.add_with(&f.name, craft_zip::Compression::Deflated, |out| out.write_all(&f.bytes)).map_err(|e| e.to_string())?;
|
||||
}
|
||||
Ok(zip.finish().map_err(|e| e.to_string())?.into_inner())
|
||||
zip.finish().map_err(|e| e.to_string())
|
||||
}
|
||||
|
||||
#[cfg(target_arch = "wasm32")]
|
||||
@@ -525,16 +524,23 @@ mod tests {
|
||||
fn zip_round_trips_every_export_file() {
|
||||
let file = |name: &str, bytes: &[u8]| ExportedFile { name: name.into(), bytes: bytes.to_vec(), width: 1, height: 1, warnings: Vec::new() };
|
||||
let normal = [7u8; 4096];
|
||||
let expected = [("a_BaseColor.png", b"one".as_slice()), ("a_Normal.png", normal.as_slice())];
|
||||
let expected = [("z_BaseColor.png", b"one".as_slice()), ("a_Normal.png", normal.as_slice())];
|
||||
let files = expected.iter().map(|(name, bytes)| file(name, bytes)).collect();
|
||||
|
||||
let bytes = zip_files(files).unwrap();
|
||||
let mut zip = zip::ZipArchive::new(std::io::Cursor::new(bytes)).unwrap();
|
||||
let mut zip = zip::ZipArchive::new(std::io::Cursor::new(&bytes)).unwrap();
|
||||
|
||||
assert_eq!(zip.len(), expected.len());
|
||||
for (name, expected_bytes) in expected {
|
||||
for (index, (name, expected_bytes)) in expected.into_iter().enumerate() {
|
||||
let mut entry = zip.by_index(index).unwrap();
|
||||
assert_eq!(entry.name().unwrap(), name);
|
||||
assert_eq!(entry.compression(), zip::CompressionMethod::Deflated);
|
||||
let header = entry.header_start() as usize;
|
||||
let name_len = u16::from_le_bytes(bytes[header + 26..header + 28].try_into().unwrap()) as usize;
|
||||
let extra = header + 30 + name_len;
|
||||
assert_eq!(&bytes[extra..extra + 4], &[1, 0, 16, 0], "expected a ZIP64 size extra field");
|
||||
let mut out = Vec::new();
|
||||
std::io::Read::read_to_end(&mut zip.by_name(name).unwrap(), &mut out).unwrap();
|
||||
std::io::Read::read_to_end(&mut entry, &mut out).unwrap();
|
||||
assert_eq!(out, expected_bytes);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user