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

Commits vergleichen

..

Keine gemeinsamen Commits. "ef1f8ed38f6cdfb428eac7b9c5348ac02b40234d" und "3f20cbc17f61190ebda30e04629b25c9ab1d317f" haben vollständig unterschiedliche Historien.

14 geänderte Dateien mit 157 neuen und 338 gelöschten Zeilen

Datei anzeigen

@ -43,6 +43,7 @@ schemsearch-cli --help
## Roadmap ## Roadmap
A list of features that are planned to be implemented in the future. In order of priority. A list of features that are planned to be implemented in the future. In order of priority.
- [ ] Full JSON output (Progressbar)
- [ ] Use AVX2 for faster search - [ ] Use AVX2 for faster search
- [ ] Tile entities data search - [ ] Tile entities data search
- [ ] Entities search - [ ] Entities search

Datei anzeigen

@ -1,6 +1,6 @@
[package] [package]
name = "schemsearch-cli" name = "schemsearch-cli"
version = "0.1.3" version = "0.1.2"
edition = "2021" edition = "2021"
license = "AGPL-3.0-or-later" license = "AGPL-3.0-or-later"

Datei anzeigen

@ -39,7 +39,7 @@ use schemsearch_sql::load_all_schematics;
#[cfg(feature = "sql")] #[cfg(feature = "sql")]
use crate::types::SqlSchematicSupplier; use crate::types::SqlSchematicSupplier;
use indicatif::*; use indicatif::*;
use schemsearch_files::{SchematicVersioned}; use schemsearch_files::Schematic;
use crate::sinks::{OutputFormat, OutputSink}; use crate::sinks::{OutputFormat, OutputSink};
use crate::stderr::MaschineStdErr; use crate::stderr::MaschineStdErr;
@ -206,7 +206,7 @@ fn main() {
threshold: *matches.get_one::<f32>("threshold").expect("Couldn't get threshold"), threshold: *matches.get_one::<f32>("threshold").expect("Couldn't get threshold"),
}; };
let pattern = match SchematicVersioned::load(&PathBuf::from(matches.get_one::<String>("pattern").unwrap())) { let pattern = match Schematic::load(&PathBuf::from(matches.get_one::<String>("pattern").unwrap())) {
Ok(x) => x, Ok(x) => x,
Err(e) => { Err(e) => {
cmd.error(ErrorKind::Io, format!("Error while loading Pattern: {}", e.to_string())).exit(); cmd.error(ErrorKind::Io, format!("Error while loading Pattern: {}", e.to_string())).exit();
@ -334,8 +334,8 @@ fn main() {
} }
} }
fn load_schem(schem_path: &PathBuf) -> Option<SchematicVersioned> { fn load_schem(schem_path: &PathBuf) -> Option<Schematic> {
match SchematicVersioned::load(schem_path) { match Schematic::load(schem_path) {
Ok(x) => Some(x), Ok(x) => Some(x),
Err(e) => { Err(e) => {
println!("Error while loading schematic ({}): {}", schem_path.to_str().unwrap(), e.to_string()); println!("Error while loading schematic ({}): {}", schem_path.to_str().unwrap(), e.to_string());

Datei anzeigen

@ -19,7 +19,7 @@ use std::path::PathBuf;
#[cfg(feature = "sql")] #[cfg(feature = "sql")]
use futures::executor::block_on; use futures::executor::block_on;
#[allow(unused_imports)] #[allow(unused_imports)]
use schemsearch_files::SpongeV2Schematic; use schemsearch_files::Schematic;
#[cfg(feature = "sql")] #[cfg(feature = "sql")]
use schemsearch_sql::{load_schemdata, SchematicNode}; use schemsearch_sql::{load_schemdata, SchematicNode};
@ -46,9 +46,9 @@ pub struct SqlSchematicSupplier {
#[cfg(feature = "sql")] #[cfg(feature = "sql")]
impl SqlSchematicSupplier { impl SqlSchematicSupplier {
pub fn get_schematic(&self) -> Result<SchematicVersioned, String> { pub fn get_schematic(&self) -> Result<Schematic, String> {
let schemdata = block_on(load_schemdata(self.node.id)); let schemdata = block_on(load_schemdata(self.node.id));
SchematicVersioned::load_data(schemdata.as_slice()) Schematic::load_data(schemdata.as_slice())
} }
pub fn get_name(&self) -> String { pub fn get_name(&self) -> String {

Datei anzeigen

@ -1,6 +1,6 @@
[package] [package]
name = "schemsearch_faster" name = "schemsearch_faster"
version = "0.1.3" version = "0.1.1"
edition = "2021" edition = "2021"
license = "AGPL-3.0-or-later" license = "AGPL-3.0-or-later"

Datei anzeigen

@ -16,9 +16,9 @@
*/ */
use nbt::Map; use nbt::Map;
use schemsearch_files::SpongeV2Schematic; use schemsearch_files::Schematic;
pub fn convert_to_search_space(schem: &SpongeV2Schematic, palette: &Vec<String>) -> Vec<Vec<u8>> { pub fn convert_to_search_space(schem: &Schematic, palette: &Vec<String>) -> Vec<Vec<u8>> {
let mut data: Vec<Vec<u8>> = Vec::with_capacity(palette.len()); let mut data: Vec<Vec<u8>> = Vec::with_capacity(palette.len());
let block_data = &schem.block_data; let block_data = &schem.block_data;
for name in palette { for name in palette {
@ -48,26 +48,26 @@ pub fn unwrap_palette(palette: &Map<String, i32>) -> Vec<String> {
#[cfg(test)] #[cfg(test)]
mod tests { mod tests {
use std::path::{Path, PathBuf}; use std::path::{Path, PathBuf};
use schemsearch_files::SpongeV2Schematic; use schemsearch_files::Schematic;
use crate::{convert_to_search_space, unwrap_palette}; use crate::{convert_to_search_space, unwrap_palette};
//#[test] //#[test]
pub fn test() { pub fn test() {
let schematic = SpongeV2Schematic::load(&PathBuf::from("../tests/Pattern.schem")).unwrap(); let schematic = Schematic::load(&PathBuf::from("../tests/Pattern.schem")).unwrap();
dbg!(convert_to_search_space(&schematic, &unwrap_palette(&schematic.palette))); dbg!(convert_to_search_space(&schematic, &unwrap_palette(&schematic.palette)));
} }
//#[test] //#[test]
pub fn test_2() { pub fn test_2() {
let schematic = SpongeV2Schematic::load(&PathBuf::from("../tests/Pattern.schem")).unwrap(); let schematic = Schematic::load(&PathBuf::from("../tests/Pattern.schem")).unwrap();
let schematic2 = SpongeV2Schematic::load(&PathBuf::from("../tests/Random.schem")).unwrap(); let schematic2 = Schematic::load(&PathBuf::from("../tests/Random.schem")).unwrap();
println!("{:?}", convert_to_search_space(&schematic2, &unwrap_palette(&schematic.palette))); println!("{:?}", convert_to_search_space(&schematic2, &unwrap_palette(&schematic.palette)));
} }
//#[test] //#[test]
pub fn test_big() { pub fn test_big() {
let schematic = SpongeV2Schematic::load(&PathBuf::from("../tests/endstone.schem")).unwrap(); let schematic = Schematic::load(&PathBuf::from("../tests/endstone.schem")).unwrap();
let schematic2 = SpongeV2Schematic::load(&PathBuf::from("../tests/simple.schem")).unwrap(); let schematic2 = Schematic::load(&PathBuf::from("../tests/simple.schem")).unwrap();
let _ = convert_to_search_space(&schematic2, &unwrap_palette(&schematic.palette)); let _ = convert_to_search_space(&schematic2, &unwrap_palette(&schematic.palette));
} }
} }

Datei anzeigen

@ -1,11 +1,10 @@
[package] [package]
name = "schemsearch-files" name = "schemsearch-files"
version = "0.1.3" version = "0.1.2"
edition = "2021" edition = "2021"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[dependencies] [dependencies]
flate2 = "1.0.25" hematite-nbt = "0.5.2"
named-binary-tag = "0.6" serde = "1.0.152"

Datei anzeigen

@ -15,259 +15,81 @@
* 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::hash_map::HashMap;
use std::io::Read; use std::io::Read;
use std::path::PathBuf; use std::path::PathBuf;
use nbt::{CompoundTag, Tag}; use nbt::{Map, Value};
use serde::{Deserialize, Deserializer, Serialize};
#[derive(Clone, Debug)] #[derive(Serialize, Deserialize, Debug)]
pub enum SchematicVersioned { pub struct Schematic {
V1(SpongeV1Schematic), #[serde(rename = "Version")]
V2(SpongeV2Schematic), pub version: i32,
V3(SpongeV3Schematic), #[serde(rename = "DataVersion")]
}
impl SchematicVersioned {
#[inline]
pub fn get_width(&self) -> u16 {
return match self {
SchematicVersioned::V1(schematic) => schematic.width,
SchematicVersioned::V2(schematic) => schematic.width,
SchematicVersioned::V3(schematic) => schematic.width,
};
}
#[inline]
pub fn get_height(&self) -> u16 {
return match self {
SchematicVersioned::V1(schematic) => schematic.height,
SchematicVersioned::V2(schematic) => schematic.height,
SchematicVersioned::V3(schematic) => schematic.height,
};
}
#[inline]
pub fn get_length(&self) -> u16 {
return match self {
SchematicVersioned::V1(schematic) => schematic.length,
SchematicVersioned::V2(schematic) => schematic.length,
SchematicVersioned::V3(schematic) => schematic.length,
};
}
#[inline]
pub fn get_palette_max(&self) -> i32 {
return match self {
SchematicVersioned::V1(schematic) => schematic.palette_max,
SchematicVersioned::V2(schematic) => schematic.palette_max,
SchematicVersioned::V3(schematic) => schematic.blocks.palette.len() as i32,
};
}
#[inline]
pub fn get_palette(&self) -> &HashMap<String, i32> {
return match self {
SchematicVersioned::V1(schematic) => &schematic.palette,
SchematicVersioned::V2(schematic) => &schematic.palette,
SchematicVersioned::V3(schematic) => &schematic.blocks.palette,
};
}
#[inline]
pub fn get_block_data(&self) -> &Vec<i32> {
return match self {
SchematicVersioned::V1(schematic) => &schematic.block_data,
SchematicVersioned::V2(schematic) => &schematic.block_data,
SchematicVersioned::V3(schematic) => &schematic.blocks.block_data,
};
}
#[inline]
pub fn get_block_entities(&self) -> &Vec<BlockEntity> {
return match self {
SchematicVersioned::V1(schematic) => &schematic.tile_entities,
SchematicVersioned::V2(schematic) => &schematic.block_entities,
SchematicVersioned::V3(schematic) => &schematic.blocks.block_entities,
};
}
}
#[derive(Clone, Debug)]
pub struct SpongeV1Schematic {
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>,
pub block_data: Vec<i32>,
pub tile_entities: Vec<BlockEntity>,
}
#[derive(Clone, Debug)]
pub struct SpongeV2Schematic {
pub data_version: i32, pub data_version: i32,
pub metadata: CompoundTag, #[serde(rename = "Metadata")]
pub metadata: Map<String, Value>,
#[serde(rename = "Width")]
pub width: u16, pub width: u16,
#[serde(rename = "Height")]
pub height: u16, pub height: u16,
#[serde(rename = "Length")]
pub length: u16, pub length: u16,
#[serde(rename = "Offset")]
pub offset: [i32; 3], pub offset: [i32; 3],
#[serde(rename = "PaletteMax")]
pub palette_max: i32, pub palette_max: i32,
pub palette: HashMap<String, i32>, #[serde(rename = "Palette")]
pub palette: Map<String, i32>,
#[serde(rename = "BlockData", deserialize_with = "read_blockdata")]
pub block_data: Vec<i32>, pub block_data: Vec<i32>,
#[serde(rename = "BlockEntities")]
pub block_entities: Vec<BlockEntity>, pub block_entities: Vec<BlockEntity>,
#[serde(rename = "Entities")]
pub entities: Option<Vec<Entity>>, pub entities: Option<Vec<Entity>>,
} }
#[derive(Clone, Debug)] fn read_blockdata<'de, D>(deserializer: D) -> Result<Vec<i32>, D::Error>
pub struct SpongeV3Schematic { where
pub data_version: i32, D: Deserializer<'de>,
pub metadata: CompoundTag, {
pub width: u16, let s: Vec<i8> = Deserialize::deserialize(deserializer)?;
pub height: u16, Ok(read_varint_array(&s))
pub length: u16,
pub offset: [i32; 3],
pub blocks: BlockContainer,
pub entities: Option<Vec<Entity>>,
} }
#[derive(Clone, Debug)] #[derive(Serialize, Deserialize, Debug, Clone)]
pub struct BlockContainer {
pub palette: HashMap<String, i32>,
pub block_data: Vec<i32>,
pub block_entities: Vec<BlockEntity>,
}
#[derive(Debug, Clone)]
pub struct BlockEntity { pub struct BlockEntity {
#[serde(rename = "Id")]
pub id: String, pub id: String,
#[serde(rename = "Pos")]
pub pos: [i32; 3], pub pos: [i32; 3],
} }
#[derive(Debug, Clone)] #[derive(Serialize, Deserialize, Debug, Clone)]
pub struct BlockEntityV3 {
pub id: String,
pub pos: [i32; 3],
pub data: HashMap<String, Tag>,
}
#[derive(Debug, Clone)]
pub struct Entity { pub struct Entity {
#[serde(rename = "Id")]
pub id: String, pub id: String,
#[serde(rename = "Pos")]
pub pos: [i32; 3], pub pos: [i32; 3],
} }
impl SchematicVersioned { impl Schematic {
pub fn load_data<R>(data: &mut R) -> Result<SchematicVersioned, String> where R: Read { pub fn load_data<R>(data: R) -> Result<Schematic, String> where R: Read {
let nbt: CompoundTag = nbt::decode::read_gzip_compound_tag(data).map_err(|e| e.to_string())?; let schematic: Schematic = match nbt::from_gzip_reader(data) {
let version = nbt.get_i32("Version").map_err(|e| e.to_string())?; Ok(schem) => schem,
Err(e) => return Err(format!("Failed to parse schematic: {}", e))
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, 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); },
_ => {},
}; };
Ok(schematic)
}
pub fn load(path: &PathBuf) -> Result<Schematic, String> {
let file = match std::fs::File::open(path) {
Ok(x) => x,
Err(_) => return Err(format!("Failed to open file: {}", path.to_str().unwrap()))
};
Schematic::load_data(file)
} }
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> { pub fn read_varint_array(read: &Vec<i8>) -> Vec<i32> {
let mut data = Vec::new(); let mut data = Vec::new();
let mut value: i32 = 0; let mut value: i32 = 0;

Datei anzeigen

@ -1,6 +1,6 @@
[package] [package]
name = "schemsearch-java" name = "schemsearch-java"
version = "0.1.3" version = "0.1.1"
edition = "2021" edition = "2021"
license = "AGPL-3.0-or-later" license = "AGPL-3.0-or-later"

Datei anzeigen

@ -21,7 +21,7 @@ use jni::JNIEnv;
use jni::objects::{JClass, JString}; use jni::objects::{JClass, JString};
use jni::sys::jstring; use jni::sys::jstring;
use schemsearch_files::SpongeV2Schematic; use schemsearch_files::Schematic;
use schemsearch_lib::{search, SearchBehavior}; use schemsearch_lib::{search, SearchBehavior};
#[no_mangle] #[no_mangle]
@ -32,8 +32,8 @@ pub extern "system" fn Java_SchemSearch_search<'local>(mut env: JNIEnv<'local>,
pattern_path: JString<'local>) -> jstring { pattern_path: JString<'local>) -> jstring {
let schematic_path: String = env.get_string(&schematic_path).expect("Couldn't get java string!").into(); let schematic_path: String = env.get_string(&schematic_path).expect("Couldn't get java string!").into();
let pattern_path: String = env.get_string(&pattern_path).expect("Couldn't get java string!").into(); let pattern_path: String = env.get_string(&pattern_path).expect("Couldn't get java string!").into();
let schematic = SpongeV2Schematic::load(&PathBuf::from(&schematic_path)).unwrap(); let schematic = Schematic::load(&PathBuf::from(&schematic_path)).unwrap();
let pattern = SpongeV2Schematic::load(&PathBuf::from(&pattern_path)).unwrap(); let pattern = Schematic::load(&PathBuf::from(&pattern_path)).unwrap();
let matches = search(schematic, &pattern, SearchBehavior { let matches = search(schematic, &pattern, SearchBehavior {
ignore_block_data: true, ignore_block_data: true,

Datei anzeigen

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

Datei anzeigen

@ -17,9 +17,9 @@
pub mod pattern_mapper; pub mod pattern_mapper;
use serde::{Serialize, Deserialize}; use serde::{Deserialize, Serialize};
use pattern_mapper::match_palette; use pattern_mapper::match_palette;
use schemsearch_files::SchematicVersioned; use schemsearch_files::Schematic;
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)]
@ -33,15 +33,15 @@ pub struct SearchBehavior {
} }
pub fn search( pub fn search(
schem: SchematicVersioned, schem: Schematic,
pattern_schem: &SchematicVersioned, pattern_schem: &Schematic,
search_behavior: SearchBehavior, search_behavior: SearchBehavior,
) -> Vec<Match> { ) -> Vec<Match> {
if schem.get_width() < pattern_schem.get_width() || schem.get_height() < pattern_schem.get_height() || schem.get_length() < pattern_schem.get_length() { if schem.width < pattern_schem.width || schem.height < pattern_schem.height || schem.length < pattern_schem.length {
return vec![]; return vec![];
} }
if pattern_schem.get_palette().len() > schem.get_palette().len() { if pattern_schem.palette.len() > schem.palette.len() {
return vec![]; return vec![];
} }
@ -49,27 +49,27 @@ pub fn search(
let mut matches: Vec<Match> = Vec::new(); let mut matches: Vec<Match> = Vec::new();
let pattern_data = pattern_schem.get_block_data().as_slice(); let pattern_data = pattern_schem.block_data.as_slice();
let schem_data = if search_behavior.ignore_block_data { let schem_data = if search_behavior.ignore_block_data {
match_palette_adapt(&schem, &pattern_schem.get_palette(), search_behavior.ignore_block_data) match_palette_adapt(&schem, &pattern_schem.palette, search_behavior.ignore_block_data)
} else { } else {
schem.get_block_data().clone() schem.block_data
}; };
let schem_data = schem_data.as_slice(); let schem_data = schem_data.as_slice();
let air_id = if search_behavior.ignore_air || search_behavior.air_as_any { pattern_schem.get_palette().get("minecraft:air").unwrap_or(&-1) } else { &-1}; let air_id = if search_behavior.ignore_air || search_behavior.air_as_any { pattern_schem.palette.get("minecraft:air").unwrap_or(&-1) } else { &-1};
let pattern_blocks = pattern_data.len() as f32; let pattern_blocks = pattern_data.len() as f32;
let pattern_width = pattern_schem.get_width() as usize; let pattern_width = pattern_schem.width as usize;
let pattern_height = pattern_schem.get_height() as usize; let pattern_height = pattern_schem.height as usize;
let pattern_length = pattern_schem.get_length() as usize; let pattern_length = pattern_schem.length as usize;
let schem_width = schem.get_width() as usize; let schem_width = schem.width as usize;
let schem_height = schem.get_height() as usize; let schem_height = schem.height as usize;
let schem_length = schem.get_length() as usize; let schem_length = schem.length as usize;
for y in 0..=schem_height - pattern_height { for y in 0..=schem_height - pattern_height {
for z in 0..=schem_length - pattern_length { for z in 0..=schem_length - pattern_length {
@ -132,69 +132,67 @@ pub fn normalize_data(data: &str, ignore_data: bool) -> &str {
} }
} }
pub fn parse_schematic(data: &Vec<u8>) -> Schematic {
if data[0] == 0x1f && data[1] == 0x8b {
// gzip
nbt::from_gzip_reader(data.as_slice()).unwrap()
} else {
// uncompressed
nbt::from_reader(data.as_slice()).unwrap()
}
}
#[allow(unused_imports)] #[allow(unused_imports)]
#[cfg(test)] #[cfg(test)]
mod tests { mod tests {
use std::path::{Path, PathBuf}; use std::path::{Path, PathBuf};
use schemsearch_files::SchematicVersioned::V2; use schemsearch_files::Schematic;
use schemsearch_files::SpongeV2Schematic;
use crate::pattern_mapper::strip_data; use crate::pattern_mapper::strip_data;
use super::*; use super::*;
#[test] #[test]
fn read_schematic() { fn read_schematic() {
let schematic = SchematicVersioned::load(&PathBuf::from("../tests/simple.schem")).unwrap(); let schematic = Schematic::load(&PathBuf::from("../tests/simple.schem")).unwrap();
let schematic = match schematic {
V2 (schematic) => schematic,
_ => panic!("Invalid schematic version"),
};
assert_eq!(schematic.width as usize * schematic.height as usize * schematic.length as usize, schematic.block_data.len()); assert_eq!(schematic.width as usize * schematic.height as usize * schematic.length as usize, schematic.block_data.len());
assert_eq!(schematic.palette_max, schematic.palette.len() as i32); assert_eq!(schematic.palette_max, schematic.palette.len() as i32);
} }
#[test] #[test]
fn test_parse_function() { fn test_parse_function() {
let schematic: SchematicVersioned = SchematicVersioned::load(&PathBuf::from("../tests/simple.schem")).unwrap(); let file = std::fs::File::open("../tests/simple.schem").expect("Failed to open file");
let schematic: Schematic = parse_schematic(&std::io::Read::bytes(file).map(|b| b.unwrap()).collect());
let schematic = match schematic {
V2 (schematic) => schematic,
_ => panic!("Invalid schematic version"),
};
assert_eq!(schematic.width as usize * schematic.height as usize * schematic.length as usize, schematic.block_data.len()); assert_eq!(schematic.width as usize * schematic.height as usize * schematic.length as usize, schematic.block_data.len());
assert_eq!(schematic.palette_max, schematic.palette.len() as i32); assert_eq!(schematic.palette_max, schematic.palette.len() as i32);
} }
#[test] #[test]
fn test_strip_schem() { fn test_strip_schem() {
let schematic = SchematicVersioned::load(&PathBuf::from("../tests/simple.schem")).unwrap(); let schematic = Schematic::load(&PathBuf::from("../tests/simple.schem")).unwrap();
let stripped = strip_data(&schematic); let stripped = strip_data(&schematic);
assert_eq!(stripped.get_palette().keys().any(|k| k.contains('[')), false); assert_eq!(stripped.palette.keys().any(|k| k.contains('[')), false);
} }
#[test] #[test]
fn test_match_palette() { fn test_match_palette() {
let schematic = SchematicVersioned::load(&PathBuf::from("../tests/simple.schem")).unwrap(); let schematic = Schematic::load(&PathBuf::from("../tests/simple.schem")).unwrap();
let endstone = SchematicVersioned::load(&PathBuf::from("../tests/endstone.schem")).unwrap(); let endstone = Schematic::load(&PathBuf::from("../tests/endstone.schem")).unwrap();
let _ = match_palette(&schematic, &endstone, true); let _ = match_palette(&schematic, &endstone, true);
} }
#[test] #[test]
fn test_match_palette_ignore_data() { fn test_match_palette_ignore_data() {
let schematic = SchematicVersioned::load(&PathBuf::from("../tests/simple.schem")).unwrap(); let schematic = Schematic::load(&PathBuf::from("../tests/simple.schem")).unwrap();
let endstone = SchematicVersioned::load(&PathBuf::from("../tests/endstone.schem")).unwrap(); let endstone = Schematic::load(&PathBuf::from("../tests/endstone.schem")).unwrap();
let _ = match_palette(&schematic, &endstone, false); let _ = match_palette(&schematic, &endstone, false);
} }
#[test] #[test]
pub fn test_big_search() { pub fn test_big_search() {
let schematic = SchematicVersioned::load(&PathBuf::from("../tests/simple.schem")).unwrap(); let schematic = Schematic::load(&PathBuf::from("../tests/simple.schem")).unwrap();
let endstone = SchematicVersioned::load(&PathBuf::from("../tests/endstone.schem")).unwrap(); let endstone = Schematic::load(&PathBuf::from("../tests/endstone.schem")).unwrap();
let _ = search(schematic, &endstone, SearchBehavior { let _ = search(schematic, &endstone, SearchBehavior {
ignore_block_data: true, ignore_block_data: true,
@ -208,8 +206,8 @@ mod tests {
#[test] #[test]
pub fn test_search() { pub fn test_search() {
let schematic = SchematicVersioned::load(&PathBuf::from("../tests/Random.schem")).unwrap(); let schematic = Schematic::load(&PathBuf::from("../tests/Random.schem")).unwrap();
let pattern = SchematicVersioned::load(&PathBuf::from("../tests/Pattern.schem")).unwrap(); let pattern = Schematic::load(&PathBuf::from("../tests/Pattern.schem")).unwrap();
let matches = search(schematic, &pattern, SearchBehavior { let matches = search(schematic, &pattern, SearchBehavior {
ignore_block_data: true, ignore_block_data: true,
@ -230,8 +228,8 @@ mod tests {
#[test] #[test]
pub fn test_search_ws() { pub fn test_search_ws() {
let schematic = SchematicVersioned::load(&PathBuf::from("../tests/warships/GreyFly-by-Bosslar.schem")).unwrap(); let schematic = Schematic::load(&PathBuf::from("../tests/warships/GreyFly-by-Bosslar.schem")).unwrap();
let pattern = SchematicVersioned::load(&PathBuf::from("../tests/gray_castle_complex.schem")).unwrap(); let pattern = Schematic::load(&PathBuf::from("../tests/gray_castle_complex.schem")).unwrap();
let matches = search(schematic, &pattern, SearchBehavior { let matches = search(schematic, &pattern, SearchBehavior {
ignore_block_data: false, ignore_block_data: false,

Datei anzeigen

@ -15,28 +15,27 @@
* 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 nbt::Map;
use nbt::CompoundTag; use schemsearch_files::Schematic;
use schemsearch_files::{SchematicVersioned, SpongeV2Schematic};
use crate::normalize_data; use crate::normalize_data;
fn create_reverse_palette(schem: &SchematicVersioned) -> Vec<&str> { fn create_reverse_palette(schem: &Schematic) -> Vec<&str> {
let mut reverse_palette = Vec::with_capacity(schem.get_palette_max() as usize); let mut reverse_palette = Vec::with_capacity(schem.palette_max as usize);
(0..schem.get_palette_max()).for_each(|_| reverse_palette.push("")); (0..schem.palette_max).for_each(|_| reverse_palette.push(""));
for (key, value) in schem.get_palette().iter() { for (key, value) in schem.palette.iter() {
reverse_palette[*value as usize] = key; reverse_palette[*value as usize] = key;
} }
reverse_palette reverse_palette
} }
pub fn strip_data(schem: &SchematicVersioned) -> SchematicVersioned { pub fn strip_data(schem: &Schematic) -> Schematic {
let mut data: Vec<i32> = Vec::new(); let mut data: Vec<i32> = Vec::new();
let mut palette: HashMap<String, i32> = HashMap::new(); let mut palette: Map<String, i32> = Map::new();
let mut palette_max: i32 = 0; let mut palette_max: i32 = 0;
let reverse_palette = create_reverse_palette(schem); let reverse_palette = create_reverse_palette(schem);
for block in schem.get_block_data().iter() { for block in schem.block_data.iter() {
let block_name = reverse_palette[*block as usize].clone(); let block_name = reverse_palette[*block as usize].clone();
let block_name = block_name.split('[').next().unwrap().to_string(); let block_name = block_name.split('[').next().unwrap().to_string();
@ -48,28 +47,27 @@ pub fn strip_data(schem: &SchematicVersioned) -> SchematicVersioned {
data.push(*entry); data.push(*entry);
} }
SchematicVersioned::V2(SpongeV2Schematic { Schematic {
data_version: 1, version: schem.version,
data_version: schem.data_version,
palette, palette,
palette_max, palette_max,
block_data: data, block_data: data,
block_entities: schem.get_block_entities().clone(), block_entities: schem.block_entities.clone(),
height: schem.get_height(), height: schem.height,
length: schem.get_length(), length: schem.length,
width: schem.get_width(), width: schem.width,
metadata: CompoundTag::new(), metadata: schem.metadata.clone(),
offset: [0; 3], offset: schem.offset.clone(),
entities: None, entities: None,
},) }
} }
pub fn match_palette_adapt(schem: &SchematicVersioned, matching_palette: &HashMap<String, i32>, ignore_data: bool) -> Vec<i32> { pub fn match_palette_adapt(schem: &Schematic, matching_palette: &Map<String, i32>, ignore_data: bool) -> Vec<i32> {
let mut data: Vec<i32> = Vec::new(); let mut data: Vec<i32> = Vec::new();
let reverse_palette = create_reverse_palette(schem); let reverse_palette = create_reverse_palette(schem);
for x in schem.get_block_data() { for x in &schem.block_data {
let blockname = reverse_palette[*x as usize]; let blockname = reverse_palette[*x as usize];
let blockname = if ignore_data { normalize_data(blockname, ignore_data) } else { blockname }; let blockname = if ignore_data { normalize_data(blockname, ignore_data) } else { blockname };
let block_id = match matching_palette.get(&*blockname) { let block_id = match matching_palette.get(&*blockname) {
@ -83,10 +81,10 @@ pub fn match_palette_adapt(schem: &SchematicVersioned, matching_palette: &HashMa
} }
pub fn match_palette( pub fn match_palette(
schem: &SchematicVersioned, schem: &Schematic,
pattern: &SchematicVersioned, pattern: &Schematic,
ignore_data: bool, ignore_data: bool,
) -> SchematicVersioned { ) -> Schematic {
if ignore_data { if ignore_data {
match_palette_internal(&strip_data(schem), &strip_data(pattern), ignore_data) match_palette_internal(&strip_data(schem), &strip_data(pattern), ignore_data)
} else { } else {
@ -95,23 +93,24 @@ pub fn match_palette(
} }
fn match_palette_internal( fn match_palette_internal(
schem: &SchematicVersioned, schem: &Schematic,
pattern: &SchematicVersioned, pattern: &Schematic,
ignore_data: bool, ignore_data: bool,
) -> SchematicVersioned { ) -> Schematic {
let data_pattern: Vec<i32> = match_palette_adapt(&pattern, schem.get_palette(), ignore_data); let data_pattern: Vec<i32> = match_palette_adapt(&pattern, &schem.palette, ignore_data);
SchematicVersioned::V2(SpongeV2Schematic { Schematic {
data_version: 0, version: pattern.version.clone(),
palette: schem.get_palette().clone(), data_version: pattern.data_version.clone(),
palette_max: schem.get_palette_max(), palette: schem.palette.clone(),
palette_max: schem.palette_max,
block_data: data_pattern, block_data: data_pattern,
block_entities: pattern.get_block_entities().clone(), block_entities: pattern.block_entities.clone(),
height: pattern.get_height(), height: pattern.height.clone(),
length: pattern.get_length(), length: pattern.length.clone(),
width: pattern.get_width(), width: pattern.width.clone(),
metadata: CompoundTag::new(), metadata: pattern.metadata.clone(),
offset: [0; 3], offset: pattern.offset.clone(),
entities: None, entities: None,
}) }
} }

Datei anzeigen

@ -1,6 +1,6 @@
[package] [package]
name = "schemsearch-sql" name = "schemsearch-sql"
version = "0.1.3" version = "0.1.1"
edition = "2021" edition = "2021"
license = "AGPL-3.0-or-later" license = "AGPL-3.0-or-later"