Finish CLI

This commit is contained in:
Chaoscaot
2023-03-06 18:29:03 +01:00
parent d352bfaded
commit 0df001602e
9 changed files with 380 additions and 133 deletions

View File

@@ -1,5 +1,5 @@
use nbt::Map;
use schemsearch_files::{Schematic, to_varint_array};
use schemsearch_files::Schematic;
use crate::normalize_data;
fn create_reverse_palette(schem: &Schematic) -> Vec<String> {
@@ -17,7 +17,7 @@ pub fn strip_data(schem: &Schematic) -> Schematic {
let mut palette: Map<String, i32> = Map::new();
let mut palette_max: i32 = 0;
let reverse_palette = create_reverse_palette(schem);
let dat = schem.read_blockdata();
let dat = &schem.block_data;
for block in dat.iter() {
let block_name = reverse_palette[*block as usize].clone();
@@ -36,7 +36,7 @@ pub fn strip_data(schem: &Schematic) -> Schematic {
data_version: schem.data_version,
palette,
palette_max,
block_data: to_varint_array(&data),
block_data: data,
block_entities: schem.block_entities.clone(),
height: schem.height,
length: schem.length,
@@ -47,24 +47,27 @@ pub fn strip_data(schem: &Schematic) -> Schematic {
}
}
fn match_palette_adapt(schem: &Schematic, matching_palette: Map<String, i32>, ignore_data: bool) -> Vec<i32> {
fn match_palette_adapt(schem: &Schematic, matching_palette: &Map<String, i32>, ignore_data: bool) -> Option<Vec<i32>> {
let mut data: Vec<i32> = Vec::new();
for x in schem.read_blockdata().iter() {
for x in schem.block_data.iter() {
let blockname = schem.palette.iter().find(|(_, &v)| v == *x).expect("Invalid Schematic").0;
let blockname = if ignore_data { normalize_data(&blockname, ignore_data) } else { blockname.clone() };
let block_id = matching_palette.get(&blockname).unwrap_or(&-1);
let block_id = match matching_palette.get(&blockname) {
None => return None,
Some(x) => x
};
data.push(*block_id);
}
data
Some(data)
}
pub fn match_palette(
schem: &Schematic,
pattern: &Schematic,
ignore_data: bool,
) -> (Schematic, Schematic) {
) -> Option<Schematic> {
if ignore_data {
match_palette_internal(&strip_data(schem), &strip_data(pattern), ignore_data)
} else {
@@ -76,46 +79,21 @@ fn match_palette_internal(
schem: &Schematic,
pattern: &Schematic,
ignore_data: bool,
) -> (Schematic, Schematic) {
if schem.palette.len() < pattern.palette.len() {
panic!("Schematic palette is larger than pattern palette");
) -> Option<Schematic> {
if pattern.palette.iter().any(|(k, _)| schem.palette.get(k).is_none()) {
return None;
}
let mut matching_palette: Map<String, i32> = Map::new();
let mut matching_palette_max: i32 = 0;
for (block_name, _) in pattern.palette.iter() {
let block_name = normalize_data(block_name, true);
let schem_block_id = pattern.palette.get(&block_name).expect("Pattern block not found in schematic palette");
matching_palette.insert(block_name, *schem_block_id);
matching_palette_max += 1;
}
let data_schem: Vec<i32> = match_palette_adapt(&schem, matching_palette.clone(), true);
let data_pattern: Vec<i32> = match_palette_adapt(&pattern, matching_palette.clone(), true);
let schem = Schematic {
version: schem.version.clone(),
data_version: schem.data_version.clone(),
palette: matching_palette.clone(),
palette_max: matching_palette_max.clone(),
block_data: to_varint_array(&data_schem),
block_entities: schem.block_entities.clone(),
height: schem.height.clone(),
length: schem.length.clone(),
width: schem.width.clone(),
metadata: schem.metadata.clone(),
offset: schem.offset.clone(),
entities: None,
let data_pattern: Vec<i32> = match match_palette_adapt(&pattern, &schem.palette, ignore_data) {
None => return None,
Some(x) => x
};
let pattern = Schematic {
Some(Schematic {
version: pattern.version.clone(),
data_version: pattern.data_version.clone(),
palette: matching_palette.clone(),
palette_max: matching_palette_max.clone(),
block_data: to_varint_array(&data_pattern),
palette: schem.palette.clone(),
palette_max: schem.palette_max,
block_data: data_pattern,
block_entities: pattern.block_entities.clone(),
height: pattern.height.clone(),
length: pattern.length.clone(),
@@ -123,7 +101,5 @@ fn match_palette_internal(
metadata: pattern.metadata.clone(),
offset: pattern.offset.clone(),
entities: None,
};
(schem, pattern)
})
}