1
0
Mirror von https://github.com/Chaoscaot/schemsearch synchronisiert 2024-11-16 17:00:14 +01:00

Commits vergleichen

..

2 Commits

Autor SHA1 Nachricht Datum
Chaoscaot
d1a01dc0c1 Faster but not working 2023-04-13 23:49:39 +02:00
Chaoscaot
e03a805bdb Something Working :D 2023-04-13 23:16:12 +02:00
4 geänderte Dateien mit 47 neuen und 22 gelöschten Zeilen

Datei anzeigen

@ -7,8 +7,7 @@ edition = "2021"
[dependencies] [dependencies]
flate2 = "1.0.25" flate2 = "1.0.25"
hematite-nbt = "0.5.2" fastnbt = "2.4.3"
serde = "1.0.152" serde = "1.0.152"
serde-this-or-that = "0.4.2" serde_json = "1.0.84"
serde_repr = "0.1.12"

Datei anzeigen

@ -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> {

Datei anzeigen

@ -10,6 +10,3 @@ license = "AGPL-3.0-or-later"
hematite-nbt = "0.5.2" hematite-nbt = "0.5.2"
serde = "1.0.152" serde = "1.0.152"
schemsearch-files = { path = "../schemsearch-files" } schemsearch-files = { path = "../schemsearch-files" }
[dev-dependencies]
serde_json = "1.0.95"

Datei anzeigen

@ -18,10 +18,8 @@
pub mod pattern_mapper; pub mod pattern_mapper;
use serde::{Deserialize, Serialize}; use serde::{Deserialize, Serialize};
use nbt::Map;
use pattern_mapper::match_palette; use pattern_mapper::match_palette;
use schemsearch_files::{SchematicVersioned, SpongeV2Schematic}; use schemsearch_files::SchematicVersioned;
use schemsearch_files::SchematicVersioned::V2;
use crate::pattern_mapper::match_palette_adapt; use crate::pattern_mapper::match_palette_adapt;
#[derive(Debug, Clone, Copy, Deserialize, Serialize)] #[derive(Debug, Clone, Copy, Deserialize, Serialize)]