1
0
Mirror von https://github.com/Chaoscaot/schemsearch synchronisiert 2024-10-02 20:50:10 +02:00
Dieser Commit ist enthalten in:
Chaoscaot 2023-04-01 10:30:25 +02:00
Ursprung 8f15b42146
Commit 818de6be47
6 geänderte Dateien mit 43 neuen und 23 gelöschten Zeilen

Datei anzeigen

@ -1,5 +1,5 @@
use serde::{Deserialize, Serialize}; use serde::{Deserialize, Serialize};
use schemsearch_lib::SearchBehavior; use schemsearch_lib::{Match, SearchBehavior};
#[derive(Serialize, Deserialize, Debug)] #[derive(Serialize, Deserialize, Debug)]
#[serde(tag = "event")] #[serde(tag = "event")]
@ -12,10 +12,8 @@ pub enum JsonEvent {
#[derive(Serialize, Deserialize, Debug)] #[derive(Serialize, Deserialize, Debug)]
pub struct FoundEvent { pub struct FoundEvent {
pub name: String, pub name: String,
pub x: u16, #[serde(flatten, rename = "match")]
pub y: u16, pub match_: Match,
pub z: u16,
pub percent: f32,
} }
#[derive(Serialize, Deserialize, Debug)] #[derive(Serialize, Deserialize, Debug)]

Datei anzeigen

@ -25,7 +25,7 @@ use clap::{command, Arg, ArgAction, ValueHint};
use std::path::PathBuf; use std::path::PathBuf;
use std::str::FromStr; use std::str::FromStr;
use clap::error::ErrorKind; use clap::error::ErrorKind;
use schemsearch_lib::{search, SearchBehavior}; use schemsearch_lib::{Match, search, SearchBehavior};
use crate::types::{PathSchematicSupplier, SchematicSupplierType}; use crate::types::{PathSchematicSupplier, SchematicSupplierType};
#[cfg(feature = "sql")] #[cfg(feature = "sql")]
use futures::executor::block_on; use futures::executor::block_on;
@ -37,7 +37,7 @@ use schemsearch_sql::filter::SchematicFilter;
use schemsearch_sql::load_all_schematics; use schemsearch_sql::load_all_schematics;
#[cfg(feature = "sql")] #[cfg(feature = "sql")]
use crate::types::SqlSchematicSupplier; use crate::types::SqlSchematicSupplier;
use indicatif::{ParallelProgressIterator, ProgressStyle}; use indicatif::*;
use schemsearch_files::Schematic; use schemsearch_files::Schematic;
use crate::sinks::{OutputFormat, OutputSink}; use crate::sinks::{OutputFormat, OutputSink};
@ -253,7 +253,7 @@ fn main() {
Some(x) => x, Some(x) => x,
None => return SearchResult { None => return SearchResult {
name: schem.get_name(), name: schem.get_name(),
matches: vec![] matches: Vec::default()
} }
}; };
SearchResult { SearchResult {
@ -274,7 +274,7 @@ fn main() {
println!("Error while loading schematic ({}): {}", schem.get_name(), e.to_string()); println!("Error while loading schematic ({}): {}", schem.get_name(), e.to_string());
SearchResult { SearchResult {
name: schem.get_name(), name: schem.get_name(),
matches: vec![] matches: Vec::default()
} }
} }
} }
@ -312,6 +312,6 @@ fn load_schem(schem_path: &PathBuf) -> Option<Schematic> {
#[derive(Debug, Clone)] #[derive(Debug, Clone)]
struct SearchResult { struct SearchResult {
name: String, name: String,
matches: Vec<(u16, u16, u16, f32)>, matches: Vec<Match>,
} }

Datei anzeigen

@ -2,7 +2,7 @@ use std::fs::File;
use std::io::BufWriter; use std::io::BufWriter;
use std::str::FromStr; use std::str::FromStr;
use std::io::Write; use std::io::Write;
use schemsearch_lib::SearchBehavior; use schemsearch_lib::{Match, SearchBehavior};
use crate::json_output::{EndEvent, FoundEvent, InitEvent, JsonEvent}; use crate::json_output::{EndEvent, FoundEvent, InitEvent, JsonEvent};
#[derive(Debug, Clone)] #[derive(Debug, Clone)]
@ -52,16 +52,13 @@ impl OutputSink {
} }
impl OutputFormat { impl OutputFormat {
pub fn found_match(&self, name: &String, pos: (u16, u16, u16, f32)) -> String { pub fn found_match(&self, name: &String, pos: Match) -> String {
match self { match self {
OutputFormat::Text => format!("Found match in '{}' at x: {}, y: {}, z: {}, % = {}\n", name, pos.0, pos.1, pos.2, pos.3), OutputFormat::Text => format!("Found match in '{}' at x: {}, y: {}, z: {}, % = {}\n", name, pos.x, pos.y, pos.z, pos.percent),
OutputFormat::CSV => format!("{},{},{},{},{}\n", name, pos.0, pos.1, pos.2, pos.3), OutputFormat::CSV => format!("{},{},{},{},{}\n", name, pos.x, pos.y, pos.z, pos.percent),
OutputFormat::JSON => format!("{}\n", serde_json::to_string(&JsonEvent::Found(FoundEvent { OutputFormat::JSON => format!("{}\n", serde_json::to_string(&JsonEvent::Found(FoundEvent {
name: name.clone(), name: name.clone(),
x: pos.0, match_: pos,
y: pos.1,
z: pos.2,
percent: pos.3,
})).unwrap()) })).unwrap())
} }
} }

Datei anzeigen

@ -18,6 +18,7 @@
use std::path::PathBuf; use std::path::PathBuf;
#[cfg(feature = "sql")] #[cfg(feature = "sql")]
use futures::executor::block_on; use futures::executor::block_on;
#[allow(unused_imports)]
use schemsearch_files::Schematic; use schemsearch_files::Schematic;
#[cfg(feature = "sql")] #[cfg(feature = "sql")]
use schemsearch_sql::{load_schemdata, SchematicNode}; use schemsearch_sql::{load_schemdata, SchematicNode};

Datei anzeigen

@ -45,8 +45,8 @@ pub extern "system" fn Java_SchemSearch_search<'local>(mut env: JNIEnv<'local>,
}); });
let mut result = String::new(); let mut result = String::new();
for (x, y, z, p) in matches { for m in matches {
result.push_str(&format!("{}, {}, {}, {};", x, y, z, p)); result.push_str(&format!("{}, {}, {}, {};", m.x, m.y, m.z, m.percent));
} }
result.remove(result.len() - 1); result.remove(result.len() - 1);
let output = env.new_string(result).expect("Couldn't create java string!"); let output = env.new_string(result).expect("Couldn't create java string!");

Datei anzeigen

@ -36,7 +36,7 @@ pub fn search(
schem: Schematic, schem: Schematic,
pattern_schem: &Schematic, pattern_schem: &Schematic,
search_behavior: SearchBehavior, search_behavior: SearchBehavior,
) -> Vec<(u16, u16, u16, f32)> { ) -> Vec<Match> {
if schem.width < pattern_schem.width || schem.height < pattern_schem.height || schem.length < pattern_schem.length { if schem.width < pattern_schem.width || schem.height < pattern_schem.height || schem.length < pattern_schem.length {
return vec![]; return vec![];
} }
@ -47,7 +47,7 @@ pub fn search(
let pattern_schem = match_palette(&schem, &pattern_schem, search_behavior.ignore_block_data); let pattern_schem = match_palette(&schem, &pattern_schem, search_behavior.ignore_block_data);
let mut matches: Vec<(u16, u16, u16, f32)> = Vec::new(); let mut matches: Vec<Match> = Vec::new();
let pattern_data = pattern_schem.block_data.as_slice(); let pattern_data = pattern_schem.block_data.as_slice();
@ -90,7 +90,12 @@ pub fn search(
} }
let matching_percent = matching as f32 / pattern_blocks; let matching_percent = matching as f32 / pattern_blocks;
if matching_percent >= search_behavior.threshold { if matching_percent >= search_behavior.threshold {
matches.push((x as u16, y as u16, z as u16, matching_percent)); matches.push(Match {
x: x as u16,
y: y as u16,
z: z as u16,
percent: matching_percent,
});
} }
} }
} }
@ -99,6 +104,25 @@ pub fn search(
return matches; return matches;
} }
#[derive(Debug, Clone, Copy, Deserialize, Serialize)]
pub struct Match {
pub x: u16,
pub y: u16,
pub z: u16,
pub percent: f32,
}
impl Default for Match {
fn default() -> Self {
Self {
x: 0,
y: 0,
z: 0,
percent: 0.0,
}
}
}
#[inline] #[inline]
pub fn normalize_data(data: &str, ignore_data: bool) -> &str { pub fn normalize_data(data: &str, ignore_data: bool) -> &str {
if ignore_data { if ignore_data {