5 Commits

Author SHA1 Message Date
Chaoscaot
582079c80d Bump Version 2023-05-23 20:10:41 +02:00
Chaoscaot
e25aeab065 Fix Broken Schematics Loading 2023-05-23 20:07:23 +02:00
Chaoscaot
aee3a80267 Reduce FLOPs 2023-05-01 11:32:40 +02:00
Chaoscaot
5107e04497 Update README.md 2023-04-28 00:28:34 +02:00
Chaoscaot
a357da2ce8 Fix Tests 2023-04-24 22:52:03 +02:00
7 changed files with 37 additions and 40 deletions

View File

@@ -1,5 +1,5 @@
# schemsearch
### A *simple* CLI tool to search in Sponge V2 Schematic files
### A *simple* CLI tool to search in Sponge Schematic files
---

View File

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

View File

@@ -1,6 +1,6 @@
[package]
name = "schemsearch-files"
version = "0.1.3"
version = "0.1.5"
edition = "2021"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

View File

@@ -64,7 +64,17 @@ pub struct Entity {
impl SpongeSchematic {
pub fn load_data<R>(data: &mut R) -> Result<SpongeSchematic, 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())?;
let version = nbt.get_i32("Version").unwrap_or_else(|_| {
return if nbt.contains_key("Blocks") {
3
} else if nbt.contains_key("BlockEntities") {
2
} else if nbt.contains_key("TileEntities") {
1
} else {
-1
};
});
match version {
1 => SpongeSchematic::from_nbt_1(nbt),
@@ -90,7 +100,7 @@ impl SpongeSchematic {
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("TileEntities").map_err(|e| e.to_string())?)?,
block_entities: read_tile_entities(nbt.get_compound_tag_vec("TileEntities").unwrap_or_else(|_| vec![]))?,
entities: None,
})
}
@@ -106,7 +116,7 @@ impl SpongeSchematic {
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())?)?,
block_entities: read_tile_entities(nbt.get_compound_tag_vec("BlockEntities").unwrap_or_else(|_| vec![]))?,
entities: None,
})
}
@@ -123,7 +133,7 @@ impl SpongeSchematic {
palette_max: compute_palette_max(blocks.get_compound_tag("Palette").map_err(|e| e.to_string())?),
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())?)?,
block_entities: read_tile_entities(blocks.get_compound_tag_vec("BlockEntities").unwrap_or_else(|_| vec![]))?,
entities: None,
})
}

View File

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

View File

@@ -71,6 +71,8 @@ pub fn search(
let schem_height = schem.height as usize;
let schem_length = schem.length as usize;
let matching_needed = (pattern_blocks * search_behavior.threshold) as i32;
for y in 0..=schem_height - pattern_height {
for z in 0..=schem_length - pattern_length {
for x in 0..=schem_width - pattern_width {
@@ -88,13 +90,12 @@ pub fn search(
}
}
}
let matching_percent = matching as f32 / pattern_blocks;
if matching_percent >= search_behavior.threshold {
if matching >= matching_needed {
matches.push(Match {
x: x as u16,
y: y as u16,
z: z as u16,
percent: matching_percent,
percent: matching as f32 / pattern_blocks,
});
}
}
@@ -136,19 +137,12 @@ pub fn normalize_data(data: &str, ignore_data: bool) -> &str {
#[cfg(test)]
mod tests {
use std::path::{Path, PathBuf};
use schemsearch_files::SchematicVersioned::V2;
use schemsearch_files::SpongeV2Schematic;
use crate::pattern_mapper::strip_data;
use super::*;
#[test]
fn read_schematic() {
let schematic = SchematicVersioned::load(&PathBuf::from("../tests/simple.schem")).unwrap();
let schematic = match schematic {
V2 (schematic) => schematic,
_ => panic!("Invalid schematic version"),
};
let schematic = SpongeSchematic::load(&PathBuf::from("../tests/simple.schem")).unwrap();
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);
@@ -156,12 +150,7 @@ mod tests {
#[test]
fn test_parse_function() {
let schematic: SchematicVersioned = SchematicVersioned::load(&PathBuf::from("../tests/simple.schem")).unwrap();
let schematic = match schematic {
V2 (schematic) => schematic,
_ => panic!("Invalid schematic version"),
};
let schematic = SpongeSchematic::load(&PathBuf::from("../tests/simple.schem")).unwrap();
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);
@@ -169,32 +158,32 @@ mod tests {
#[test]
fn test_strip_schem() {
let schematic = SchematicVersioned::load(&PathBuf::from("../tests/simple.schem")).unwrap();
let schematic = SpongeSchematic::load(&PathBuf::from("../tests/simple.schem")).unwrap();
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]
fn test_match_palette() {
let schematic = SchematicVersioned::load(&PathBuf::from("../tests/simple.schem")).unwrap();
let endstone = SchematicVersioned::load(&PathBuf::from("../tests/endstone.schem")).unwrap();
let schematic = SpongeSchematic::load(&PathBuf::from("../tests/simple.schem")).unwrap();
let endstone = SpongeSchematic::load(&PathBuf::from("../tests/endstone.schem")).unwrap();
let _ = match_palette(&schematic, &endstone, true);
}
#[test]
fn test_match_palette_ignore_data() {
let schematic = SchematicVersioned::load(&PathBuf::from("../tests/simple.schem")).unwrap();
let endstone = SchematicVersioned::load(&PathBuf::from("../tests/endstone.schem")).unwrap();
let schematic = SpongeSchematic::load(&PathBuf::from("../tests/simple.schem")).unwrap();
let endstone = SpongeSchematic::load(&PathBuf::from("../tests/endstone.schem")).unwrap();
let _ = match_palette(&schematic, &endstone, false);
}
#[test]
pub fn test_big_search() {
let schematic = SchematicVersioned::load(&PathBuf::from("../tests/simple.schem")).unwrap();
let endstone = SchematicVersioned::load(&PathBuf::from("../tests/endstone.schem")).unwrap();
let schematic = SpongeSchematic::load(&PathBuf::from("../tests/simple.schem")).unwrap();
let endstone = SpongeSchematic::load(&PathBuf::from("../tests/endstone.schem")).unwrap();
let _ = search(schematic, &endstone, SearchBehavior {
ignore_block_data: true,
@@ -208,8 +197,8 @@ mod tests {
#[test]
pub fn test_search() {
let schematic = SchematicVersioned::load(&PathBuf::from("../tests/Random.schem")).unwrap();
let pattern = SchematicVersioned::load(&PathBuf::from("../tests/Pattern.schem")).unwrap();
let schematic = SpongeSchematic::load(&PathBuf::from("../tests/Random.schem")).unwrap();
let pattern = SpongeSchematic::load(&PathBuf::from("../tests/Pattern.schem")).unwrap();
let matches = search(schematic, &pattern, SearchBehavior {
ignore_block_data: true,
@@ -230,8 +219,8 @@ mod tests {
#[test]
pub fn test_search_ws() {
let schematic = SchematicVersioned::load(&PathBuf::from("../tests/warships/GreyFly-by-Bosslar.schem")).unwrap();
let pattern = SchematicVersioned::load(&PathBuf::from("../tests/gray_castle_complex.schem")).unwrap();
let schematic = SpongeSchematic::load(&PathBuf::from("../tests/warships/GreyFly-by-Bosslar.schem")).unwrap();
let pattern = SpongeSchematic::load(&PathBuf::from("../tests/gray_castle_complex.schem")).unwrap();
let matches = search(schematic, &pattern, SearchBehavior {
ignore_block_data: false,

View File

@@ -37,7 +37,7 @@ pub fn strip_data(schem: &SpongeSchematic) -> SpongeSchematic {
let reverse_palette = create_reverse_palette(schem);
for block in schem.block_data.iter() {
let block_name = reverse_palette[*block as usize].clone();
let block_name = reverse_palette[*block as usize];
let block_name = block_name.split('[').next().unwrap().to_string();
let entry = palette.entry(block_name).or_insert_with(|| {
@@ -61,8 +61,6 @@ pub fn strip_data(schem: &SpongeSchematic) -> SpongeSchematic {
offset: [0; 3],
entities: None,
}
}
pub fn match_palette_adapt(schem: &SpongeSchematic, matching_palette: &HashMap<String, i32>, ignore_data: bool) -> Vec<i32> {