From 0724b1df2002e64532a2b88e096ddde64e3b5122 Mon Sep 17 00:00:00 2001 From: raffitz Date: Fri, 26 Mar 2021 21:22:29 +0000 Subject: [PATCH] Add Sponge Schematic .schem output --- .gitignore | 2 ++ Cargo.lock | 24 ++++++++++++++++++++++++ Cargo.toml | 1 + src/main.rs | 36 ++++++++++++++++++++++++++++++++++-- 4 files changed, 61 insertions(+), 2 deletions(-) diff --git a/.gitignore b/.gitignore index 4cdab27..eea47b7 100644 --- a/.gitignore +++ b/.gitignore @@ -1,2 +1,4 @@ /target */*.png +*/*.schem +*/*.litematic diff --git a/Cargo.lock b/Cargo.lock index b36cc03..63443e6 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -56,6 +56,12 @@ version = "1.5.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "bed57e2090563b83ba8f83366628ce535a7584c9afa4c9fc0612a03925c6df58" +[[package]] +name = "byteorder" +version = "1.4.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "14c189c53d098945499cdfa7ecc63567cf3886b3332b312a5b4585d8d3a6a610" + [[package]] name = "cfg-if" version = "1.0.0" @@ -137,6 +143,12 @@ version = "0.2.91" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "8916b1f6ca17130ec6568feccee27c156ad12037880833a3b842a823236502e7" +[[package]] +name = "linked-hash-map" +version = "0.5.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7fb9b38af92608140b86b693604b9ffcc5824240a484d1ecd4795bacb2fe88f3" + [[package]] name = "lodepng" version = "3.4.5" @@ -183,6 +195,17 @@ dependencies = [ "autocfg", ] +[[package]] +name = "named-binary-tag" +version = "0.6.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "523298fac63bd954f9a2e03b962b8a4a0e95110ad1b2fa3e0d7048660ffecec3" +dependencies = [ + "byteorder", + "flate2", + "linked-hash-map", +] + [[package]] name = "pomelo" version = "0.1.5" @@ -293,6 +316,7 @@ dependencies = [ "clap", "lodepng", "logos", + "named-binary-tag", "pomelo", "rgb", ] diff --git a/Cargo.toml b/Cargo.toml index 7bb9d7f..5ab8166 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -11,3 +11,4 @@ logos = "0.12" pomelo = "0.1.5" lodepng = "3.4" rgb = "0.8" +named-binary-tag = "0.6" diff --git a/src/main.rs b/src/main.rs index ed898a6..ac58c41 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,5 +1,7 @@ use clap::{crate_authors, crate_description, crate_name, crate_version, App, Arg}; use logos::Logos; +use nbt::encode::write_gzip_compound_tag; +use nbt::CompoundTag; use pomelo::pomelo; use rgb::*; use std::collections::HashMap; @@ -449,11 +451,16 @@ fn main() -> Result<(), error::Error> { let width: i64 = 1 + max_x - min_x; let height: i64 = 1 + max_y - min_y; + let depth: i64 = 1 + max_z - min_z; let pix_width: usize = 6 * (width as usize) + 1; let pix_height: usize = 6 * (height as usize) + 1; let pix_size: usize = pix_width * pix_height; + let schem_size: usize = (width as usize) * (height as usize) * (depth as usize); + + let mut schem_block_data: Vec = Vec::with_capacity(schem_size); + for z in min_z..=max_z { let name = format! {"{}/layer{:04}.png",output_folder,1 + z - min_z}; @@ -518,12 +525,37 @@ fn main() -> Result<(), error::Error> { + pix_x] = color; } } + + schem_block_data.push(1); + schem_block_data.push(if is_filled { 1 } else { 0 }); } } lodepng::encode32_file(name, &pixels, pix_width, pix_height)?; } - Ok(()) + // Schematic + let mut schem = CompoundTag::new(); + schem.insert_i32("Version", 2); + schem.insert_i32("Data Version", 1343); + + schem.insert_i16("Width", width as i16); + schem.insert_i16("Height", depth as i16); + schem.insert_i16("Length", height as i16); + schem.insert_i32_vec("Offset", vec![min_x as i32, min_z as i32, min_y as i32]); + + schem.insert_i32("PaletteMax", 2); - //println!("Scale was read and is <{}>", scale.unwrap_or(1)); + let mut palette_obj = CompoundTag::new(); + palette_obj.insert_i32("minecraft:air", 0); + palette_obj.insert_i32("minecraft:stone", 1); + + schem.insert("Palette", palette_obj); + + schem.insert_i8_vec("BlockData", schem_block_data); + + let mut schem_file = fs::File::create(format!("{}/{}.schem", output_folder, output_folder))?; + + write_gzip_compound_tag(&mut schem_file, &schem)?; + + Ok(()) }