diff --git a/projects/kitten-heart/src/main/java/net/pixaurora/kitten_heart/impl/KitTunes.java b/projects/kitten-heart/src/main/java/net/pixaurora/kitten_heart/impl/KitTunes.java index f757d58..255e66c 100644 --- a/projects/kitten-heart/src/main/java/net/pixaurora/kitten_heart/impl/KitTunes.java +++ b/projects/kitten-heart/src/main/java/net/pixaurora/kitten_heart/impl/KitTunes.java @@ -1,5 +1,6 @@ package net.pixaurora.kitten_heart.impl; +import java.io.IOException; import java.util.Arrays; import java.util.List; import java.util.concurrent.Executor; @@ -19,7 +20,7 @@ import net.pixaurora.kitten_heart.impl.service.MinecraftUICompat; import net.pixaurora.kitten_heart.impl.service.ServiceLoading; import net.pixaurora.kitten_thoughts.impl.KittenThoughts; -import net.pixaurora.kitten_thoughts.impl.scrobbler.ScrobblerSetup; +import net.pixaurora.kitten_thoughts.impl.http.server.Server; public class KitTunes { public static final Logger LOGGER = LoggerFactory.getLogger(Constants.MOD_ID); @@ -46,8 +47,13 @@ public static void init() { KittenThoughts.init(); - String helloMessage = ScrobblerSetup.hello("CoolCat"); - KitTunes.LOGGER.info(helloMessage); + try { + String helloMessage = Server.create("token").runServer(); + + KitTunes.LOGGER.info(helloMessage); + } catch (IOException e) { + KitTunes.LOGGER.info("Failed to run native code!", e); + } } public static void tick() { diff --git a/projects/kitten-thoughts/src/main/java/net/pixaurora/kitten_thoughts/impl/http/server/Server.java b/projects/kitten-thoughts/src/main/java/net/pixaurora/kitten_thoughts/impl/http/server/Server.java new file mode 100644 index 0000000..dde3521 --- /dev/null +++ b/projects/kitten-thoughts/src/main/java/net/pixaurora/kitten_thoughts/impl/http/server/Server.java @@ -0,0 +1,11 @@ +package net.pixaurora.kitten_thoughts.impl.http.server; + +import java.io.IOException; + +public interface Server { + public static Server create(String tokenArgName) { + return new ServerImpl(tokenArgName); + } + + public String runServer() throws IOException; +} diff --git a/projects/kitten-thoughts/src/main/java/net/pixaurora/kitten_thoughts/impl/http/server/ServerImpl.java b/projects/kitten-thoughts/src/main/java/net/pixaurora/kitten_thoughts/impl/http/server/ServerImpl.java new file mode 100644 index 0000000..838c58b --- /dev/null +++ b/projects/kitten-thoughts/src/main/java/net/pixaurora/kitten_thoughts/impl/http/server/ServerImpl.java @@ -0,0 +1,18 @@ +package net.pixaurora.kitten_thoughts.impl.http.server; + +public class ServerImpl implements Server { + private final long pointer; + + public ServerImpl(String tokenArgName) { + this.pointer = create(tokenArgName); + } + + private static native long create(String tokenArgName); + + @Override + public String runServer() { + return this.runServer0(); + } + + private native String runServer0(); +} diff --git a/projects/kitten-thoughts/src/main/java/net/pixaurora/kitten_thoughts/impl/scrobbler/ScrobblerSetup.java b/projects/kitten-thoughts/src/main/java/net/pixaurora/kitten_thoughts/impl/scrobbler/ScrobblerSetup.java deleted file mode 100644 index b7502ad..0000000 --- a/projects/kitten-thoughts/src/main/java/net/pixaurora/kitten_thoughts/impl/scrobbler/ScrobblerSetup.java +++ /dev/null @@ -1,5 +0,0 @@ -package net.pixaurora.kitten_thoughts.impl.scrobbler; - -public class ScrobblerSetup { - public static native String hello(String input); -} diff --git a/projects/kitten-thoughts/src/main/java/net/pixaurora/kitten_thoughts/impl/util/CryptoUtil.java b/projects/kitten-thoughts/src/main/java/net/pixaurora/kitten_thoughts/impl/util/CryptoUtil.java index 337f1af..751176e 100644 --- a/projects/kitten-thoughts/src/main/java/net/pixaurora/kitten_thoughts/impl/util/CryptoUtil.java +++ b/projects/kitten-thoughts/src/main/java/net/pixaurora/kitten_thoughts/impl/util/CryptoUtil.java @@ -1,7 +1,6 @@ package net.pixaurora.kitten_thoughts.impl.util; import java.io.IOException; -import java.nio.charset.StandardCharsets; import java.nio.file.Files; import java.nio.file.Path; import java.security.MessageDigest; diff --git a/projects/kitten-thoughts/src/main/rust/bridge.rs b/projects/kitten-thoughts/src/main/rust/bridge.rs new file mode 100644 index 0000000..b187a40 --- /dev/null +++ b/projects/kitten-thoughts/src/main/rust/bridge.rs @@ -0,0 +1,8 @@ +use crate::Result; +use jni::{objects::JObject, sys::jlong, JNIEnv}; + +mod http; + +fn get_rust_pointer<'local>(env: &mut JNIEnv<'local>, object: &JObject<'local>) -> Result { + Ok(env.get_field(object, "pointer", "J")?.try_into()?) +} diff --git a/projects/kitten-thoughts/src/main/rust/bridge/http.rs b/projects/kitten-thoughts/src/main/rust/bridge/http.rs new file mode 100644 index 0000000..df4adf9 --- /dev/null +++ b/projects/kitten-thoughts/src/main/rust/bridge/http.rs @@ -0,0 +1 @@ +mod server; diff --git a/projects/kitten-thoughts/src/main/rust/bridge/http/server.rs b/projects/kitten-thoughts/src/main/rust/bridge/http/server.rs new file mode 100644 index 0000000..c92d95a --- /dev/null +++ b/projects/kitten-thoughts/src/main/rust/bridge/http/server.rs @@ -0,0 +1,62 @@ +use crate::Result; + +use jni::{ + objects::{JClass, JObject, JString}, + sys::jlong, + JNIEnv, +}; + +use crate::{bridge::get_rust_pointer, http::server::Server}; + +fn create<'local>(token_arg_name: JString<'local>, env: &mut JNIEnv<'local>) -> Result { + let token_arg_name: String = env.get_string(&token_arg_name)?.into(); + + let server = Server::new(token_arg_name); + + Ok(Box::into_raw(Box::from(server)) as jlong) +} + +fn run_server<'local>( + object: &JObject<'local>, + env: &mut JNIEnv<'local>, +) -> Result> { + let pointer = get_rust_pointer(env, object)?; + let server = unsafe { &*(pointer as *mut Server) }; + + let token = server.run_server()?; + + Ok(env.new_string(token)?) +} + +#[no_mangle] +pub extern "system" fn Java_net_pixaurora_kitten_1thoughts_impl_http_server_ServerImpl_create< + 'local, +>( + mut env: JNIEnv<'local>, + _class: JClass<'local>, + token_arg_name: JString<'local>, +) -> jlong { + match create(token_arg_name, &mut env) { + Ok(pointer) => pointer, + Err(b) => { + b.throw(&mut env); + jlong::default() + } + } +} + +#[no_mangle] +pub extern "system" fn Java_net_pixaurora_kitten_1thoughts_impl_http_server_ServerImpl_runServer0< + 'local, +>( + mut env: JNIEnv<'local>, + object: JObject<'local>, +) -> JString<'local> { + match run_server(&object, &mut env) { + Ok(token) => token, + Err(error) => { + error.throw(&mut env); + JString::default().into() + } + } +} diff --git a/projects/kitten-thoughts/src/main/rust/errors.rs b/projects/kitten-thoughts/src/main/rust/errors.rs new file mode 100644 index 0000000..20404fd --- /dev/null +++ b/projects/kitten-thoughts/src/main/rust/errors.rs @@ -0,0 +1,44 @@ +use std::fmt::Display; + +use jni::errors::Error as JNIError; +use jni::JNIEnv; +use std::io::Error as IOError; + +#[derive(Debug)] +pub enum Error { + JNI(JNIError), + IO(IOError), +} + +pub type Result = core::result::Result; + +impl Error { + pub fn throw(&self, env: &mut JNIEnv) { + let a = env + .throw_new("java/io/IOException", self.to_string()) + .unwrap(); + } +} + +impl std::error::Error for Error {} + +impl Display for Error { + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { + match self { + Error::JNI(error) => error.fmt(f), + Error::IO(error) => error.fmt(f), + } + } +} + +impl From for Error { + fn from(value: JNIError) -> Self { + Error::JNI(value) + } +} + +impl From for Error { + fn from(value: IOError) -> Self { + Error::IO(value) + } +} diff --git a/projects/kitten-thoughts/src/main/rust/hello.rs b/projects/kitten-thoughts/src/main/rust/hello.rs deleted file mode 100644 index 6da8742..0000000 --- a/projects/kitten-thoughts/src/main/rust/hello.rs +++ /dev/null @@ -1,6 +0,0 @@ -pub fn hello(input: String) -> String { - format!( - "Hello, {}! This message is from Rust of all programming languages, woo hoo!", - input - ) -} diff --git a/projects/kitten-thoughts/src/main/rust/http.rs b/projects/kitten-thoughts/src/main/rust/http.rs new file mode 100644 index 0000000..74f47ad --- /dev/null +++ b/projects/kitten-thoughts/src/main/rust/http.rs @@ -0,0 +1 @@ +pub mod server; diff --git a/projects/kitten-thoughts/src/main/rust/http/server.rs b/projects/kitten-thoughts/src/main/rust/http/server.rs new file mode 100644 index 0000000..16c8bcc --- /dev/null +++ b/projects/kitten-thoughts/src/main/rust/http/server.rs @@ -0,0 +1,18 @@ +use crate::Result; + +pub struct Server { + token_arg_name: String, +} + +impl Server { + pub fn new(token_arg_name: String) -> Self { + Server { token_arg_name } + } + + pub fn run_server(&self) -> Result { + Ok(format!( + "Wait... this isn't a token! Token argument name is {}", + self.token_arg_name + )) + } +} diff --git a/projects/kitten-thoughts/src/main/rust/lib.rs b/projects/kitten-thoughts/src/main/rust/lib.rs index fc7e151..7408fc5 100644 --- a/projects/kitten-thoughts/src/main/rust/lib.rs +++ b/projects/kitten-thoughts/src/main/rust/lib.rs @@ -1,40 +1,6 @@ -use jni::{ - objects::{JClass, JString}, - sys::jstring, - JNIEnv, -}; +mod bridge; +pub mod errors; +pub mod http; -pub mod hello; - -#[no_mangle] -pub extern "system" fn Java_net_pixaurora_kitten_1thoughts_impl_scrobbler_ScrobblerSetup_hello< - 'local, ->( - mut env: JNIEnv<'local>, - _class: JClass<'local>, - input: JString<'local>, -) -> jstring { - let input: String = env - .get_string(&input) - .expect("Couldn't get java string!") - .into(); - - let output = hello::hello(input); - let output = env - .new_string(output) - .expect("Couldn't create java string!"); - - output.into_raw() -} - -#[cfg(test)] -mod tests { - use super::hello::hello; - - #[test] - fn hello_message() { - let output = hello("Coolcat".into()); - - assert_eq!(output, "Hello, Coolcat!"); - } -} +pub use errors::Error; +pub use errors::Result;