diff --git a/.gitignore b/.gitignore index 7d11246..103174b 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,5 @@ /target /Cargo.lock /.idea/ +/SchemSearch.class +/SchemSearch.h diff --git a/Cargo.toml b/Cargo.toml index 8d20580..7e4e931 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -3,5 +3,6 @@ members = [ "schemsearch-cli", "schemsearch-lib", "schemsearch-files", - "schemsearch-sql" + "schemsearch-sql", + "schemsearch-java" ] diff --git a/SchemSearch.java b/SchemSearch.java new file mode 100644 index 0000000..6f1fdb8 --- /dev/null +++ b/SchemSearch.java @@ -0,0 +1,8 @@ +public class SchemSearch { + + public static native String search(String schematicFile, String patternFile); + + public static void init(String filePath) { + System.loadLibrary(filePath); + } +} diff --git a/schemsearch-java/Cargo.toml b/schemsearch-java/Cargo.toml new file mode 100644 index 0000000..5356419 --- /dev/null +++ b/schemsearch-java/Cargo.toml @@ -0,0 +1,15 @@ +[package] +name = "schemsearch-java" +version = "0.1.0" +edition = "2021" + +# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html + +[lib] +crate_type = ["cdylib"] + +[dependencies] +jni = "0.21.0" + +schemsearch-lib = { path = "../schemsearch-lib" } +schemsearch-files = { path = "../schemsearch-files" } \ No newline at end of file diff --git a/schemsearch-java/src/lib.rs b/schemsearch-java/src/lib.rs new file mode 100644 index 0000000..038cfd8 --- /dev/null +++ b/schemsearch-java/src/lib.rs @@ -0,0 +1,34 @@ +#![no_mangle] + +use jni::JNIEnv; + +use jni::objects::{JClass, JString}; + +use jni::sys::jstring; +use schemsearch_lib::{search, SearchBehavior}; + +pub extern "system" fn Java_SchemSearch_search<'local>(mut env: JNIEnv<'local>, + class: JClass<'local>, + schematic_path: JString<'local>, + pattern_path: JString<'local>) -> jstring { + 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 file = std::fs::File::open(schematic_path).expect("Failed to open file"); + let schematic = &std::io::Read::bytes(file).map(|b| b.unwrap()).collect(); + let file = std::fs::File::open(pattern_path).expect("Failed to open file"); + let pattern = &std::io::Read::bytes(file).map(|b| b.unwrap()).collect(); + + let matches = search(schematic, pattern, SearchBehavior { + ignore_block_data: true, + ignore_block_entities: true, + ignore_entities: true, + }); + + let mut result = String::new(); + for (x, y, z) in matches { + result.push_str(&format!("{}, {}, {};", x, y, z)); + } + result.remove(result.len() - 1); + let output = env.new_string(result).expect("Couldn't create java string!"); + output.into_raw() +} \ No newline at end of file diff --git a/schemsearch-lib/src/lib.rs b/schemsearch-lib/src/lib.rs index 1533c73..76de0bf 100644 --- a/schemsearch-lib/src/lib.rs +++ b/schemsearch-lib/src/lib.rs @@ -5,9 +5,9 @@ pub mod pattern_mapper; #[derive(Debug, Clone, Copy)] pub struct SearchBehavior { - ignore_block_data: bool, - ignore_block_entities: bool, - ignore_entities: bool, + pub ignore_block_data: bool, + pub ignore_block_entities: bool, + pub ignore_entities: bool, } pub fn search(