|
|
@ -15,11 +15,22 @@
|
|
|
|
* along with this program. If not, see <https://www.gnu.org/licenses/>.
|
|
|
|
* along with this program. If not, see <https://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
use std::collections::HashMap;
|
|
|
|
use std::io::Read;
|
|
|
|
use std::io::Read;
|
|
|
|
use std::path::PathBuf;
|
|
|
|
use std::path::PathBuf;
|
|
|
|
use nbt::{Error, from_gzip_reader, Map, Value};
|
|
|
|
use fastnbt::error::Error;
|
|
|
|
use serde::{Deserialize, Deserializer, Serialize, Serializer};
|
|
|
|
use fastnbt::Value;
|
|
|
|
use serde_this_or_that::as_i64;
|
|
|
|
use flate2::read::GzDecoder;
|
|
|
|
|
|
|
|
use serde::{Deserialize, Deserializer, Serialize};
|
|
|
|
|
|
|
|
use serde::de::value::MapDeserializer;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
#[derive(Clone, Serialize, Deserialize, Debug)]
|
|
|
|
|
|
|
|
#[serde(rename_all = "PascalCase")]
|
|
|
|
|
|
|
|
struct SchematicRaw {
|
|
|
|
|
|
|
|
version: i32,
|
|
|
|
|
|
|
|
#[serde(flatten)]
|
|
|
|
|
|
|
|
data: HashMap<String, Value>,
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
#[derive(Clone, Serialize, Deserialize, Debug)]
|
|
|
|
#[derive(Clone, Serialize, Deserialize, Debug)]
|
|
|
|
#[serde(untagged, rename_all = "PascalCase")]
|
|
|
|
#[serde(untagged, rename_all = "PascalCase")]
|
|
|
@ -67,7 +78,7 @@ impl SchematicVersioned {
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
#[inline]
|
|
|
|
#[inline]
|
|
|
|
pub fn get_palette(&self) -> &Map<String, i32> {
|
|
|
|
pub fn get_palette(&self) -> &HashMap<String, i32> {
|
|
|
|
return match self {
|
|
|
|
return match self {
|
|
|
|
SchematicVersioned::V1(schematic) => &schematic.palette,
|
|
|
|
SchematicVersioned::V1(schematic) => &schematic.palette,
|
|
|
|
SchematicVersioned::V2(schematic) => &schematic.palette,
|
|
|
|
SchematicVersioned::V2(schematic) => &schematic.palette,
|
|
|
@ -94,16 +105,36 @@ impl SchematicVersioned {
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
impl From<SchematicRaw> for SchematicVersioned {
|
|
|
|
|
|
|
|
fn from(value: SchematicRaw) -> Self {
|
|
|
|
|
|
|
|
match value.version {
|
|
|
|
|
|
|
|
1 => {
|
|
|
|
|
|
|
|
let schematic: SpongeV1Schematic = SpongeV1Schematic::deserialize(MapDeserializer::new(value.data.into_iter())).unwrap();
|
|
|
|
|
|
|
|
return SchematicVersioned::V1(schematic);
|
|
|
|
|
|
|
|
},
|
|
|
|
|
|
|
|
2 => {
|
|
|
|
|
|
|
|
let schematic: SpongeV2Schematic = SpongeV2Schematic::deserialize(MapDeserializer::new(value.data.into_iter())).unwrap();
|
|
|
|
|
|
|
|
return SchematicVersioned::V2(schematic);
|
|
|
|
|
|
|
|
},
|
|
|
|
|
|
|
|
3 => {
|
|
|
|
|
|
|
|
let schematic: SpongeV3Schematic = SpongeV3Schematic::deserialize(MapDeserializer::new(value.data.into_iter())).unwrap();
|
|
|
|
|
|
|
|
return SchematicVersioned::V3(schematic);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
_ => panic!("Unknown Schematic Version: {}", value.version),
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
#[derive(Clone, Serialize, Deserialize, Debug)]
|
|
|
|
#[derive(Clone, Serialize, Deserialize, Debug)]
|
|
|
|
#[serde(rename_all = "PascalCase")]
|
|
|
|
#[serde(rename_all = "PascalCase")]
|
|
|
|
pub struct SpongeV1Schematic {
|
|
|
|
pub struct SpongeV1Schematic {
|
|
|
|
pub metadata: Map<String, Value>,
|
|
|
|
pub metadata: HashMap<String, Value>,
|
|
|
|
pub width: u16,
|
|
|
|
pub width: u16,
|
|
|
|
pub height: u16,
|
|
|
|
pub height: u16,
|
|
|
|
pub length: u16,
|
|
|
|
pub length: u16,
|
|
|
|
pub offset: [i32; 3],
|
|
|
|
pub offset: [i32; 3],
|
|
|
|
pub palette_max: i32,
|
|
|
|
pub palette_max: i32,
|
|
|
|
pub palette: Map<String, i32>,
|
|
|
|
pub palette: HashMap<String, i32>,
|
|
|
|
#[serde(deserialize_with = "read_blockdata")]
|
|
|
|
#[serde(deserialize_with = "read_blockdata")]
|
|
|
|
pub block_data: Vec<i32>,
|
|
|
|
pub block_data: Vec<i32>,
|
|
|
|
pub tile_entities: Vec<BlockEntity>,
|
|
|
|
pub tile_entities: Vec<BlockEntity>,
|
|
|
@ -113,13 +144,13 @@ pub struct SpongeV1Schematic {
|
|
|
|
#[serde(rename_all = "PascalCase")]
|
|
|
|
#[serde(rename_all = "PascalCase")]
|
|
|
|
pub struct SpongeV2Schematic {
|
|
|
|
pub struct SpongeV2Schematic {
|
|
|
|
pub data_version: i32,
|
|
|
|
pub data_version: i32,
|
|
|
|
pub metadata: Map<String, Value>,
|
|
|
|
pub metadata: HashMap<String, Value>,
|
|
|
|
pub width: u16,
|
|
|
|
pub width: u16,
|
|
|
|
pub height: u16,
|
|
|
|
pub height: u16,
|
|
|
|
pub length: u16,
|
|
|
|
pub length: u16,
|
|
|
|
pub offset: [i32; 3],
|
|
|
|
pub offset: [i32; 3],
|
|
|
|
pub palette_max: i32,
|
|
|
|
pub palette_max: i32,
|
|
|
|
pub palette: Map<String, i32>,
|
|
|
|
pub palette: HashMap<String, i32>,
|
|
|
|
#[serde(deserialize_with = "read_blockdata")]
|
|
|
|
#[serde(deserialize_with = "read_blockdata")]
|
|
|
|
pub block_data: Vec<i32>,
|
|
|
|
pub block_data: Vec<i32>,
|
|
|
|
pub block_entities: Vec<BlockEntity>,
|
|
|
|
pub block_entities: Vec<BlockEntity>,
|
|
|
@ -130,7 +161,7 @@ pub struct SpongeV2Schematic {
|
|
|
|
#[serde(rename_all = "PascalCase")]
|
|
|
|
#[serde(rename_all = "PascalCase")]
|
|
|
|
pub struct SpongeV3Schematic {
|
|
|
|
pub struct SpongeV3Schematic {
|
|
|
|
pub data_version: i32,
|
|
|
|
pub data_version: i32,
|
|
|
|
pub metadata: Map<String, Value>,
|
|
|
|
pub metadata: HashMap<String, Value>,
|
|
|
|
pub width: u16,
|
|
|
|
pub width: u16,
|
|
|
|
pub height: u16,
|
|
|
|
pub height: u16,
|
|
|
|
pub length: u16,
|
|
|
|
pub length: u16,
|
|
|
@ -142,7 +173,7 @@ pub struct SpongeV3Schematic {
|
|
|
|
#[derive(Clone, Serialize, Deserialize, Debug)]
|
|
|
|
#[derive(Clone, Serialize, Deserialize, Debug)]
|
|
|
|
#[serde(rename_all = "PascalCase")]
|
|
|
|
#[serde(rename_all = "PascalCase")]
|
|
|
|
pub struct BlockContainer {
|
|
|
|
pub struct BlockContainer {
|
|
|
|
pub palette: Map<String, i32>,
|
|
|
|
pub palette: HashMap<String, i32>,
|
|
|
|
#[serde(deserialize_with = "read_blockdata")]
|
|
|
|
#[serde(deserialize_with = "read_blockdata")]
|
|
|
|
pub block_data: Vec<i32>,
|
|
|
|
pub block_data: Vec<i32>,
|
|
|
|
pub block_entities: Vec<BlockEntity>,
|
|
|
|
pub block_entities: Vec<BlockEntity>,
|
|
|
@ -168,7 +199,7 @@ pub struct BlockEntity {
|
|
|
|
pub struct BlockEntityV3 {
|
|
|
|
pub struct BlockEntityV3 {
|
|
|
|
pub id: String,
|
|
|
|
pub id: String,
|
|
|
|
pub pos: [i32; 3],
|
|
|
|
pub pos: [i32; 3],
|
|
|
|
pub data: Map<String, Value>,
|
|
|
|
pub data: HashMap<String, Value>,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
#[derive(Serialize, Deserialize, Debug, Clone)]
|
|
|
|
#[derive(Serialize, Deserialize, Debug, Clone)]
|
|
|
@ -180,8 +211,8 @@ pub struct Entity {
|
|
|
|
|
|
|
|
|
|
|
|
impl SchematicVersioned {
|
|
|
|
impl SchematicVersioned {
|
|
|
|
pub fn load_data<R>(data: R) -> Result<SchematicVersioned, Error> where R: Read {
|
|
|
|
pub fn load_data<R>(data: R) -> Result<SchematicVersioned, Error> where R: Read {
|
|
|
|
let schematic: SchematicVersioned = from_gzip_reader(data)?;
|
|
|
|
let raw: SchematicRaw = fastnbt::from_reader(GzDecoder::new(data))?;
|
|
|
|
Ok(schematic)
|
|
|
|
Ok(SchematicVersioned::from(raw))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
pub fn load(path: &PathBuf) -> Result<SchematicVersioned, Error> {
|
|
|
|
pub fn load(path: &PathBuf) -> Result<SchematicVersioned, Error> {
|
|
|
|