Skip to content

Commit

Permalink
Reduce Boilerplate
Browse files Browse the repository at this point in the history
  • Loading branch information
Chaoscaot committed Apr 24, 2023
1 parent 25c4e97 commit eb84adb
Show file tree
Hide file tree
Showing 5 changed files with 80 additions and 171 deletions.
8 changes: 4 additions & 4 deletions schemsearch-cli/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ use schemsearch_sql::load_all_schematics;
#[cfg(feature = "sql")]
use crate::types::SqlSchematicSupplier;
use indicatif::*;
use schemsearch_files::{SchematicVersioned};
use schemsearch_files::SpongeSchematic;
use crate::sinks::{OutputFormat, OutputSink};
use crate::stderr::MaschineStdErr;

Expand Down Expand Up @@ -206,7 +206,7 @@ fn main() {
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 SpongeSchematic::load(&PathBuf::from(matches.get_one::<String>("pattern").unwrap())) {
Ok(x) => x,
Err(e) => {
cmd.error(ErrorKind::Io, format!("Error while loading Pattern: {}", e.to_string())).exit();
Expand Down Expand Up @@ -334,8 +334,8 @@ fn main() {
}
}

fn load_schem(schem_path: &PathBuf) -> Option<SchematicVersioned> {
match SchematicVersioned::load(schem_path) {
fn load_schem(schem_path: &PathBuf) -> Option<SpongeSchematic> {
match SpongeSchematic::load(schem_path) {
Ok(x) => Some(x),
Err(e) => {
println!("Error while loading schematic ({}): {}", schem_path.to_str().unwrap(), e.to_string());
Expand Down
8 changes: 5 additions & 3 deletions schemsearch-cli/src/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,13 @@
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/

#[cfg(feature = "sql")]
use std::io::Cursor;
use std::path::PathBuf;
#[cfg(feature = "sql")]
use futures::executor::block_on;
use schemsearch_files::SchematicVersioned;
#[cfg(feature = "sql")]
use schemsearch_files::SpongeSchematic;
#[cfg(feature = "sql")]
use schemsearch_sql::{load_schemdata, SchematicNode};

Expand All @@ -46,9 +48,9 @@ pub struct SqlSchematicSupplier {

#[cfg(feature = "sql")]
impl SqlSchematicSupplier {
pub fn get_schematic(&self) -> Result<SchematicVersioned, String> {
pub fn get_schematic(&self) -> Result<SpongeSchematic, String> {
let mut schemdata = block_on(load_schemdata(self.node.id));
SchematicVersioned::load_data(&mut Cursor::new(schemdata.as_mut_slice()))
SpongeSchematic::load_data(&mut Cursor::new(schemdata.as_mut_slice()))
}

pub fn get_name(&self) -> String {
Expand Down
145 changes: 26 additions & 119 deletions schemsearch-files/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,92 +21,7 @@ use std::path::PathBuf;
use nbt::{CompoundTag, Tag};

#[derive(Clone, Debug)]
pub enum SchematicVersioned {
V1(SpongeV1Schematic),
V2(SpongeV2Schematic),
V3(SpongeV3Schematic),
}

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 struct SpongeSchematic {
pub data_version: i32,
pub metadata: CompoundTag,
pub width: u16,
Expand All @@ -120,18 +35,6 @@ pub struct SpongeV2Schematic {
pub entities: Option<Vec<Entity>>,
}

#[derive(Clone, Debug)]
pub struct SpongeV3Schematic {
pub data_version: i32,
pub metadata: CompoundTag,
pub width: u16,
pub height: u16,
pub length: u16,
pub offset: [i32; 3],
pub blocks: BlockContainer,
pub entities: Option<Vec<Entity>>,
}

#[derive(Clone, Debug)]
pub struct BlockContainer {
pub palette: HashMap<String, i32>,
Expand All @@ -158,28 +61,27 @@ pub struct Entity {
pub pos: [i32; 3],
}

impl SchematicVersioned {
pub fn load_data<R>(data: &mut R) -> Result<SchematicVersioned, String> where R: Read {
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())?;

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)?)),
1 => SpongeSchematic::from_nbt_1(nbt),
2 => SpongeSchematic::from_nbt_2(nbt),
3 => SpongeSchematic::from_nbt_3(nbt),
_ => Err("Invalid schematic: Unknown Version".to_string()),
}
}

pub fn load(path: &PathBuf) -> Result<SchematicVersioned, String> {
pub fn load(path: &PathBuf) -> Result<SpongeSchematic, 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> {
pub fn from_nbt_1(nbt: CompoundTag) -> Result<Self, String> {
Ok(Self {
data_version: 0,
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,
Expand All @@ -188,13 +90,12 @@ impl SpongeV1Schematic {
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())?)?,
block_entities: read_tile_entities(nbt.get_compound_tag_vec("TileEntities").map_err(|e| e.to_string())?)?,
entities: None,
})
}
}

impl SpongeV2Schematic {
pub fn from_nbt(nbt: CompoundTag) -> Result<Self, String> {
pub fn from_nbt_2(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(),
Expand All @@ -209,10 +110,8 @@ impl SpongeV2Schematic {
entities: None,
})
}
}

impl SpongeV3Schematic {
pub fn from_nbt(nbt: CompoundTag) -> Result<Self, String> {
pub fn from_nbt_3(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())?,
Expand All @@ -221,14 +120,14 @@ impl SpongeV3Schematic {
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())?)?,
},
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())?)?,
entities: None,
})
}

}

fn read_tile_entities(tag: Vec<&CompoundTag>) -> Result<Vec<BlockEntity>, String> {
Expand Down Expand Up @@ -262,6 +161,14 @@ fn read_palette(p: &CompoundTag) -> HashMap<String, i32> {
palette
}

#[inline]
fn compute_palette_max(palette: &CompoundTag) -> i32 {
palette.iter().map(|(_, v)| v).filter_map(|v| match v {
Tag::Int(n) => Some(*n),
_ => None,
}).max().unwrap_or(0)
}

#[inline]
fn read_blocks(blockdata: &Vec<i8>) -> Vec<i32> {
read_varint_array(blockdata)
Expand Down
30 changes: 15 additions & 15 deletions schemsearch-lib/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ pub mod pattern_mapper;

use serde::{Serialize, Deserialize};
use pattern_mapper::match_palette;
use schemsearch_files::SchematicVersioned;
use schemsearch_files::SpongeSchematic;
use crate::pattern_mapper::match_palette_adapt;

#[derive(Debug, Clone, Copy, Deserialize, Serialize)]
Expand All @@ -33,43 +33,43 @@ pub struct SearchBehavior {
}

pub fn search(
schem: SchematicVersioned,
pattern_schem: &SchematicVersioned,
schem: SpongeSchematic,
pattern_schem: &SpongeSchematic,
search_behavior: SearchBehavior,
) -> 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![];
}

if pattern_schem.get_palette().len() > schem.get_palette().len() {
if pattern_schem.palette.len() > schem.palette.len() {
return vec![];
}

let pattern_schem = match_palette(&schem, &pattern_schem, search_behavior.ignore_block_data);

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 {
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 {
schem.get_block_data().clone()
schem.block_data.clone()
};

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_width = pattern_schem.get_width() as usize;
let pattern_height = pattern_schem.get_height() as usize;
let pattern_length = pattern_schem.get_length() as usize;
let pattern_width = pattern_schem.width as usize;
let pattern_height = pattern_schem.height as usize;
let pattern_length = pattern_schem.length as usize;

let schem_width = schem.get_width() as usize;
let schem_height = schem.get_height() as usize;
let schem_length = schem.get_length() as usize;
let schem_width = schem.width as usize;
let schem_height = schem.height as usize;
let schem_length = schem.length as usize;

for y in 0..=schem_height - pattern_height {
for z in 0..=schem_length - pattern_length {
Expand Down
Loading

0 comments on commit eb84adb

Please sign in to comment.