Skip to content

Commit

Permalink
feat: allow parsing metadata separately from the content
Browse files Browse the repository at this point in the history
  • Loading branch information
RubixDev committed May 9, 2024
1 parent a2bb794 commit 67d5838
Show file tree
Hide file tree
Showing 25 changed files with 344 additions and 148 deletions.
6 changes: 3 additions & 3 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 5 additions & 3 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "rustmatica"
version = "0.3.2"
version = "0.4.0"
authors = ["RubixDev"]
categories = ["encoding", "wasm"]
edition = "2021"
Expand All @@ -25,12 +25,14 @@ image = ["dep:image"]
## Enable this when building the docs
docs = ["dep:document-features", "dep:rustc_version"]

_mcdata-all = ["mcdata/entities", "mcdata/block-entities", "mcdata/mc-all"]

[dependencies]
document-features = { version = "0.2.8", optional = true }
fastnbt = "2.5.0"
flate2 = "1.0.30"
image = { version = "0.25.1", optional = true }
mcdata = { version = "0.2.1", features = ["serde"] }
mcdata = { version = "0.2.3", features = ["serde"] }
serde = { version = "1.0.201", features = ["derive"] }
thiserror = "1.0.60"

Expand All @@ -43,7 +45,7 @@ js-sys = "0.3.69" # for current time without chrono feature

[dev-dependencies]
bounded-integer = "0.5.7"
mcdata = { version = "0.2.0", features = ["serde", "latest", "block-states"] }
mcdata = { version = "0.2.3", features = ["serde", "latest", "block-states"] }
viuer = "0.7.1"

[build-dependencies]
Expand Down
52 changes: 30 additions & 22 deletions examples/metadata.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,53 +4,61 @@ use rustmatica::Litematic;
fn main() -> Result<(), Box<dyn std::error::Error>> {
let path = std::env::args().nth(1);
let path = path.as_deref().unwrap_or("test_files/axolotl.litematic");
let donut: Litematic = Litematic::read_file(path)?;
let schem: Litematic = Litematic::read_file(path)?;

println!("Name: {}", donut.name);
println!("Description: {}", donut.description);
println!("Author: {}", donut.author);
println!("Regions: {}", donut.regions.len());
println!("Name: {}", schem.metadata.name);
println!("Description: {}", schem.metadata.description);
println!("Author: {}", schem.metadata.author);
println!("Regions: {}", schem.regions.len());
println!(
"Region Names: {}",
donut
"Region Names: {}",
schem
.regions
.iter()
.map(|r| r.name.clone())
.collect::<Vec<_>>()
.join(", ")
);
println!(
"Created at (UTC): {}",
donut.time_created.format("%H:%M %d.%m.%Y")
"Created at (UTC): {}",
schem.metadata.time_created.format("%H:%M %d.%m.%Y")
);
println!(
"Created at (Local): {}",
donut
"Created at (Local): {}",
schem
.metadata
.time_created
.with_timezone(&Local)
.format("%H:%M %d.%m.%Y")
);
println!(
"Last modified (UTC): {}",
donut.time_modified.format("%H:%M %d.%m.%Y")
"Last modified (UTC): {}",
schem.metadata.time_modified.format("%H:%M %d.%m.%Y")
);
println!(
"Last modified (Local): {}",
donut
"Last modified (Local): {}",
schem
.metadata
.time_modified
.with_timezone(&Local)
.format("%H:%M %d.%m.%Y")
);
println!(
"Size: {}x{}x{}",
donut.enclosing_size().x,
donut.enclosing_size().y,
donut.enclosing_size().z
"Size: {}x{}x{}",
schem.enclosing_size().x,
schem.enclosing_size().y,
schem.enclosing_size().z
);
println!("Blocks: {}", schem.total_blocks());
println!("Volume: {}", schem.total_volume());
println!("Format version: {}", schem.metadata.version);
println!("Format sub-version: {:?}", schem.metadata.sub_version);
println!(
"Minecraft data version: {}",
schem.metadata.minecraft_data_version
);
println!("Blocks: {}", donut.total_blocks());
println!("Volume: {}", donut.total_volume());

if let Some(img) = donut.preview_image() {
if let Some(img) = schem.metadata.preview_image() {
// display preview image using `viuer`
let config = viuer::Config {
transparent: true,
Expand Down
77 changes: 77 additions & 0 deletions examples/stress_test.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
#[cfg(feature = "_mcdata-all")]
use rustmatica::{Litematic, LitematicMetadata};

#[cfg(not(feature = "_mcdata-all"))]
fn main() {
compile_error!("this test requires the `_mcdata-all` feature to be enabled");
}

#[cfg(feature = "_mcdata-all")]
macro_rules! try_for {
($version:ident, $path:expr, $meta:expr) => {{
let schem: rustmatica::Result<
Litematic<
mcdata::$version::BlockState,
mcdata::$version::Entity,
mcdata::$version::BlockEntity,
>,
> = Litematic::read_file_with_metadata($path, $meta);
println!("\n\x1b[1m{}\x1b[0m", $path.display());

match schem {
Ok(schem) => {
for region in schem.regions {
for block in region.block_palette() {
if let mcdata::$version::BlockState::Other(generic) = block {
println!("\x1b[1;33mcontains generic block state: {generic:?}\x1b[0m");
}
}
for entity in region.entities {
if let mcdata::$version::Entity::Other(generic) = entity {
println!("\x1b[1;33mcontains generic entity: {generic:?}\x1b[0m");
}
}
for entity in region.block_entities {
if let mcdata::$version::BlockEntity::Other(generic) = entity {
println!("\x1b[1;33mcontains generic block entity: {generic:?}\x1b[0m");
}
}
}
}
Err(e) => eprintln!(
"\x1b[1;31mcould not read '{}' as litematic: {e}\x1b[0m",
$path.display()
),
}
}};
}

#[cfg(feature = "_mcdata-all")]
fn main() -> Result<(), Box<dyn std::error::Error>> {
for file in std::fs::read_dir("test_files/tmc_catalogue")? {
let file = file?;
let path = file.path();
println!("\n\x1b[1m{}\x1b[0m", path.display());
let meta = LitematicMetadata::read_file(&path)?;
match meta.minecraft_data_version {
mcdata::data_version::MC_1_20_5..=u32::MAX => try_for!(mc1_20_5, &path, meta),
mcdata::data_version::MC_1_20_3..=u32::MAX => try_for!(mc1_20_3, &path, meta),
mcdata::data_version::MC_1_20_2..=u32::MAX => try_for!(mc1_20_2, &path, meta),
mcdata::data_version::MC_1_20..=u32::MAX => try_for!(mc1_20, &path, meta),
mcdata::data_version::MC_1_19_4..=u32::MAX => try_for!(mc1_19_4, &path, meta),
mcdata::data_version::MC_1_19_3..=u32::MAX => try_for!(mc1_19_3, &path, meta),
mcdata::data_version::MC_1_19_1..=u32::MAX => try_for!(mc1_19_1, &path, meta),
mcdata::data_version::MC_1_19..=u32::MAX => try_for!(mc1_19, &path, meta),
mcdata::data_version::MC_1_18..=u32::MAX => try_for!(mc1_18, &path, meta),
mcdata::data_version::MC_1_17..=u32::MAX => try_for!(mc1_17, &path, meta),
mcdata::data_version::MC_1_16_2..=u32::MAX => try_for!(mc1_16_2, &path, meta),
mcdata::data_version::MC_1_16..=u32::MAX => try_for!(mc1_16, &path, meta),
mcdata::data_version::MC_1_15_2..=u32::MAX => try_for!(mc1_15_2, &path, meta),
mcdata::data_version::MC_1_15..=u32::MAX => try_for!(mc1_15, &path, meta),
mcdata::data_version::MC_1_14..=u32::MAX => try_for!(mc1_14, &path, meta),
v => eprintln!("\x1b[1;31mMinecraft data version {v} not supported\x1b[0m"),
}
}

Ok(())
}
Loading

0 comments on commit 67d5838

Please sign in to comment.