1
0
Mirror von https://github.com/Chaoscaot/schemsearch synchronisiert 2024-09-28 18:51:08 +02:00

Commits vergleichen

...

3 Commits

Autor SHA1 Nachricht Datum
Chaoscaot
5cff84538d Fix Performance 2023-04-22 21:29:18 +02:00
Chaoscaot
9a0b0535c6 remove Serde 2023-04-22 21:03:00 +02:00
Chaoscaot
a47c2f44bd Something™️ 2023-04-22 16:39:21 +02:00
5 geänderte Dateien mit 129 neuen und 107 gelöschten Zeilen

Datei anzeigen

@ -7,7 +7,5 @@ edition = "2021"
[dependencies]
flate2 = "1.0.25"
fastnbt = "2.4.3"
serde = "1.0.152"
serde_json = "1.0.84"
named-binary-tag = "0.6"

Datei anzeigen

@ -15,25 +15,12 @@
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
use std::collections::hash_map::{HashMap};
use std::collections::hash_map::HashMap;
use std::io::Read;
use std::path::PathBuf;
use fastnbt::error::Error;
use fastnbt::Value;
use flate2::read::GzDecoder;
use serde::{Deserialize, Deserializer, Serialize};
use serde::de::value::MapDeserializer;
use nbt::{CompoundTag, Tag};
#[derive(Clone, Serialize, Deserialize, Debug)]
#[serde(rename_all = "PascalCase")]
struct SchematicRaw {
version: i32,
#[serde(flatten)]
data: HashMap<String, Value>,
}
#[derive(Clone, Serialize, Deserialize, Debug)]
#[serde(untagged, rename_all = "PascalCase")]
#[derive(Clone, Debug)]
pub enum SchematicVersioned {
V1(SpongeV1Schematic),
V2(SpongeV2Schematic),
@ -105,65 +92,38 @@ impl SchematicVersioned {
}
}
impl TryFrom<SchematicRaw> for SchematicVersioned {
type Error = Error;
fn try_from(value: SchematicRaw) -> Result<Self, Self::Error> {
match value.version {
1 => {
let schematic: SpongeV1Schematic = fastnbt::from_value(&fastnbt::to_value(value.data)?)?;
return Ok(SchematicVersioned::V1(schematic));
},
2 => {
let schematic: SpongeV2Schematic = fastnbt::from_value(&fastnbt::to_value(value.data)?)?;
return Ok(SchematicVersioned::V2(schematic));
},
3 => {
let schematic: SpongeV3Schematic = fastnbt::from_value(&fastnbt::to_value(value.data)?)?;
return Ok(SchematicVersioned::V3(schematic));
}
_ => panic!("Unknown Schematic Version: {}", value.version),
}
}
}
#[derive(Clone, Serialize, Deserialize, Debug)]
#[serde(rename_all = "PascalCase")]
#[derive(Clone, Debug)]
pub struct SpongeV1Schematic {
pub metadata: HashMap<String, Value>,
pub metadata: CompoundTag,
pub width: u16,
pub height: u16,
pub length: u16,
pub offset: [i32; 3],
pub palette_max: i32,
pub palette: HashMap<String, i32>,
#[serde(deserialize_with = "read_blockdata")]
pub block_data: Vec<i32>,
pub tile_entities: Vec<BlockEntity>,
}
#[derive(Clone, Serialize, Deserialize, Debug)]
#[serde(rename_all = "PascalCase")]
#[derive(Clone, Debug)]
pub struct SpongeV2Schematic {
pub data_version: i32,
pub metadata: HashMap<String, Value>,
pub metadata: CompoundTag,
pub width: u16,
pub height: u16,
pub length: u16,
pub offset: [i32; 3],
pub palette_max: i32,
pub palette: HashMap<String, i32>,
#[serde(deserialize_with = "read_blockdata")]
pub block_data: Vec<i32>,
pub block_entities: Vec<BlockEntity>,
pub entities: Option<Vec<Entity>>,
}
#[derive(Clone, Serialize, Deserialize, Debug)]
#[serde(rename_all = "PascalCase")]
#[derive(Clone, Debug)]
pub struct SpongeV3Schematic {
pub data_version: i32,
pub metadata: HashMap<String, Value>,
pub metadata: CompoundTag,
pub width: u16,
pub height: u16,
pub length: u16,
@ -172,57 +132,142 @@ pub struct SpongeV3Schematic {
pub entities: Option<Vec<Entity>>,
}
#[derive(Clone, Serialize, Deserialize, Debug)]
#[serde(rename_all = "PascalCase")]
#[derive(Clone, Debug)]
pub struct BlockContainer {
pub palette: HashMap<String, i32>,
#[serde(deserialize_with = "read_blockdata")]
pub block_data: Vec<i32>,
pub block_entities: Vec<BlockEntity>,
}
fn read_blockdata<'de, D>(deserializer: D) -> Result<Vec<i32>, D::Error>
where
D: Deserializer<'de>,
{
let s: Vec<i8> = Deserialize::deserialize(deserializer)?;
Ok(read_varint_array(&s))
}
#[derive(Serialize, Deserialize, Debug, Clone)]
#[serde(rename_all = "PascalCase")]
#[derive(Debug, Clone)]
pub struct BlockEntity {
pub id: String,
pub pos: [i32; 3],
}
#[derive(Serialize, Deserialize, Debug, Clone)]
#[serde(rename_all = "PascalCase")]
#[derive(Debug, Clone)]
pub struct BlockEntityV3 {
pub id: String,
pub pos: [i32; 3],
pub data: HashMap<String, Value>,
pub data: HashMap<String, Tag>,
}
#[derive(Serialize, Deserialize, Debug, Clone)]
#[serde(rename_all = "PascalCase")]
#[derive(Debug, Clone)]
pub struct Entity {
pub id: String,
pub pos: [i32; 3],
}
impl SchematicVersioned {
pub fn load_data<R>(data: R) -> Result<SchematicVersioned, Error> where R: Read {
let raw: SchematicRaw = fastnbt::from_reader(GzDecoder::new(data))?;
SchematicVersioned::try_from(raw)
pub fn load_data<R>(data: &mut R) -> Result<SchematicVersioned, String> where R: Read {
let nbt: CompoundTag = nbt::decode::read_gzip_compound_tag(data).map_err(|e| e.to_string())?;
let version = nbt.get_i32("Version").map_err(|e| e.to_string())?;
match version {
1 => Ok(SchematicVersioned::V1(SpongeV1Schematic::from_nbt(nbt)?)),
2 => Ok(SchematicVersioned::V2(SpongeV2Schematic::from_nbt(nbt)?)),
3 => Ok(SchematicVersioned::V3(SpongeV3Schematic::from_nbt(nbt)?)),
_ => Err("Invalid schematic: Unknown Version".to_string()),
}
}
pub fn load(path: &PathBuf) -> Result<SchematicVersioned, Error> {
let file = std::fs::File::open(path)?;
Self::load_data(file)
pub fn load(path: &PathBuf) -> Result<SchematicVersioned, String> {
let mut file = std::fs::File::open(path).map_err(|e| e.to_string())?;
Self::load_data(&mut file)
}
}
impl SpongeV1Schematic {
pub fn from_nbt(nbt: CompoundTag) -> Result<Self, String> {
Ok(Self {
metadata: nbt.get_compound_tag("Metadata").map_err(|e| e.to_string())?.clone(),
width: nbt.get_i16("Width").map_err(|e| e.to_string())? as u16,
height: nbt.get_i16("Height").map_err(|e| e.to_string())? as u16,
length: nbt.get_i16("Length").map_err(|e| e.to_string())? as u16,
offset: read_offset(nbt.get_i32_vec("Offset").map_err(|e| e.to_string())?)?,
palette_max: nbt.get_i32("PaletteMax").map_err(|e| e.to_string())?,
palette: read_palette(nbt.get_compound_tag("Palette").map_err(|e| e.to_string())?),
block_data: read_blocks(nbt.get_i8_vec("BlockData").map_err(|e| e.to_string())?),
tile_entities: read_tile_entities(nbt.get_compound_tag_vec("TileEntities").map_err(|e| e.to_string())?)?,
})
}
}
impl SpongeV2Schematic {
pub fn from_nbt(nbt: CompoundTag) -> Result<Self, String> {
Ok(Self{
data_version: nbt.get_i32("DataVersion").map_err(|e| e.to_string())?,
metadata: nbt.get_compound_tag("Metadata").map_err(|e| e.to_string())?.clone(),
width: nbt.get_i16("Width").map_err(|e| e.to_string())? as u16,
height: nbt.get_i16("Height").map_err(|e| e.to_string())? as u16,
length: nbt.get_i16("Length").map_err(|e| e.to_string())? as u16,
offset: read_offset(nbt.get_i32_vec("Offset").map_err(|e| e.to_string())?)?,
palette_max: nbt.get_i32("PaletteMax").map_err(|e| e.to_string())?,
palette: read_palette(nbt.get_compound_tag("Palette").map_err(|e| e.to_string())?),
block_data: read_blocks(nbt.get_i8_vec("BlockData").map_err(|e| e.to_string())?),
block_entities: read_tile_entities(nbt.get_compound_tag_vec("BlockEntities").map_err(|e| e.to_string())?)?,
entities: None,
})
}
}
impl SpongeV3Schematic {
pub fn from_nbt(nbt: CompoundTag) -> Result<Self, String> {
let blocks = nbt.get_compound_tag("Blocks").map_err(|e| e.to_string())?;
Ok(Self{
data_version: nbt.get_i32("DataVersion").map_err(|e| e.to_string())?,
metadata: nbt.get_compound_tag("Metadata").map_err(|e| e.to_string())?.clone(),
width: nbt.get_i16("Width").map_err(|e| e.to_string())? as u16,
height: nbt.get_i16("Height").map_err(|e| e.to_string())? as u16,
length: nbt.get_i16("Length").map_err(|e| e.to_string())? as u16,
offset: read_offset(nbt.get_i32_vec("Offset").map_err(|e| e.to_string())?)?,
blocks: BlockContainer {
palette: read_palette(blocks.get_compound_tag("Palette").map_err(|e| e.to_string())?),
block_data: read_blocks(blocks.get_i8_vec("BlockData").map_err(|e| e.to_string())?),
block_entities: read_tile_entities(blocks.get_compound_tag_vec("BlockEntities").map_err(|e| e.to_string())?)?,
},
entities: None,
})
}
}
fn read_tile_entities(tag: Vec<&CompoundTag>) -> Result<Vec<BlockEntity>, String> {
let mut tile_entities = Vec::new();
for t in tag {
tile_entities.push(BlockEntity {
id: t.get_str("Id").map_err(|e| e.to_string())?.to_string(),
pos: read_offset(t.get("Pos").map_err(|e| e.to_string())?)?,
});
}
Ok(tile_entities)
}
#[inline]
fn read_offset(offset: &Vec<i32>) -> Result<[i32; 3], String> {
match offset.len() {
3 => Ok([offset[0], offset[1], offset[2]]),
_ => Err("Invalid schematic: read_offset wrong length".to_string()),
}
}
#[inline]
fn read_palette(p: &CompoundTag) -> HashMap<String, i32> {
let mut palette = HashMap::new();
for (key, value) in p.iter() {
match value {
Tag::Int(n) => { palette.insert(key.clone(), *n); },
_ => {},
};
}
palette
}
#[inline]
fn read_blocks(blockdata: &Vec<i8>) -> Vec<i32> {
read_varint_array(blockdata)
}
#[inline]
pub fn read_varint_array(read: &Vec<i8>) -> Vec<i32> {
let mut data = Vec::new();
let mut value: i32 = 0;

Datei anzeigen

@ -7,6 +7,6 @@ license = "AGPL-3.0-or-later"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[dependencies]
hematite-nbt = "0.5.2"
serde = "1.0.152"
serde = { version = "1.0.160", features = ["derive"] }
schemsearch-files = { path = "../schemsearch-files" }
named-binary-tag = "0.6"

Datei anzeigen

@ -17,7 +17,7 @@
pub mod pattern_mapper;
use serde::{Deserialize, Serialize};
use serde::{Serialize, Deserialize};
use pattern_mapper::match_palette;
use schemsearch_files::SchematicVersioned;
use crate::pattern_mapper::match_palette_adapt;
@ -136,7 +136,7 @@ pub fn normalize_data(data: &str, ignore_data: bool) -> &str {
#[cfg(test)]
mod tests {
use std::path::{Path, PathBuf};
use serde::de::Unexpected::Map;
use schemsearch_files::SchematicVersioned::V2;
use schemsearch_files::SpongeV2Schematic;
use crate::pattern_mapper::strip_data;
use super::*;
@ -245,26 +245,4 @@ mod tests {
println!("{:?}", matches);
assert_eq!(matches.len(), 1);
}
#[test]
pub fn testsezi() {
let schematic = SchematicVersioned::V2(SpongeV2Schematic {
data_version: 0,
metadata: nbt::Map::new(),
width: 16,
height: 16,
length: 16,
offset: [0; 3],
palette_max: 0,
palette: nbt::Map::new(),
block_data: vec![],
entities: Some(vec![]),
block_entities: vec![],
});
println!("{:?}", schematic);
println!("{}", serde_json::to_string_pretty(&schematic).unwrap());
}
}

Datei anzeigen

@ -15,7 +15,8 @@
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
use nbt::Map;
use std::collections::HashMap;
use nbt::CompoundTag;
use schemsearch_files::{SchematicVersioned, SpongeV2Schematic};
use crate::normalize_data;
@ -31,7 +32,7 @@ fn create_reverse_palette(schem: &SchematicVersioned) -> Vec<&str> {
pub fn strip_data(schem: &SchematicVersioned) -> SchematicVersioned {
let mut data: Vec<i32> = Vec::new();
let mut palette: Map<String, i32> = Map::new();
let mut palette: HashMap<String, i32> = HashMap::new();
let mut palette_max: i32 = 0;
let reverse_palette = create_reverse_palette(schem);
@ -56,7 +57,7 @@ pub fn strip_data(schem: &SchematicVersioned) -> SchematicVersioned {
height: schem.get_height(),
length: schem.get_length(),
width: schem.get_width(),
metadata: Map::new(),
metadata: CompoundTag::new(),
offset: [0; 3],
entities: None,
},)
@ -64,7 +65,7 @@ pub fn strip_data(schem: &SchematicVersioned) -> SchematicVersioned {
}
pub fn match_palette_adapt(schem: &SchematicVersioned, matching_palette: &Map<String, i32>, ignore_data: bool) -> Vec<i32> {
pub fn match_palette_adapt(schem: &SchematicVersioned, matching_palette: &HashMap<String, i32>, ignore_data: bool) -> Vec<i32> {
let mut data: Vec<i32> = Vec::new();
let reverse_palette = create_reverse_palette(schem);
@ -109,7 +110,7 @@ fn match_palette_internal(
height: pattern.get_height(),
length: pattern.get_length(),
width: pattern.get_width(),
metadata: Map::new(),
metadata: CompoundTag::new(),
offset: [0; 3],
entities: None,
})