From d4a5f0dd11f90761a5ff44d58fb1639be0412556 Mon Sep 17 00:00:00 2001 From: Ethan O'Brien Date: Sun, 30 Nov 2025 11:55:22 -0600 Subject: [PATCH] Add wip android/ios lib builds --- Cargo.lock | 223 +++++++++++++++++++++++++++++++++++++++++++++++++ Cargo.toml | 14 ++++ build.rs | 12 +++ src/android.rs | 60 +++++++++++++ src/ios.rs | 27 ++++++ src/lib.rs | 10 ++- src/log.c | 7 ++ src/main.rs | 6 ++ 8 files changed, 358 insertions(+), 1 deletion(-) create mode 100644 build.rs create mode 100644 src/android.rs create mode 100644 src/ios.rs create mode 100644 src/log.c diff --git a/Cargo.lock b/Cargo.lock index f1840fc..5f6bc3d 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -393,6 +393,12 @@ dependencies = [ "shlex", ] +[[package]] +name = "cesu8" +version = "1.1.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "6d43a04d8753f35258c91f8ec639f792891f748a1edbd759cf1dcea3382ad83c" + [[package]] name = "cfg-if" version = "1.0.4" @@ -455,6 +461,16 @@ version = "1.0.4" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "b05b61dc5112cbb17e4b6cd61790d9845d13888356391624cbe7e41efeac1e75" +[[package]] +name = "combine" +version = "4.6.7" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ba5a308b75df32fe02788e748662718f03fde005016435c444eea572398219fd" +dependencies = [ + "bytes", + "memchr", +] + [[package]] name = "const-oid" version = "0.9.6" @@ -602,10 +618,13 @@ dependencies = [ "aes", "base64", "cbc", + "cc", "clap", "hex", "hmac", "include-flate-codegen", + "include_dir", + "jni", "json", "lazy_static", "libflate", @@ -741,6 +760,12 @@ dependencies = [ "wasip2", ] +[[package]] +name = "glob" +version = "0.3.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "0cc23270f6e1808e30a928bdc84dea0b9b4136a8bc82338574f23baf47bbd280" + [[package]] name = "h2" version = "0.3.27" @@ -977,6 +1002,25 @@ dependencies = [ "zstd", ] +[[package]] +name = "include_dir" +version = "0.7.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "923d117408f1e49d914f1a379a309cffe4f18c05cf4e3d12e613a15fc81bd0dd" +dependencies = [ + "include_dir_macros", +] + +[[package]] +name = "include_dir_macros" +version = "0.7.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7cab85a7ed0bd5f0e76d93846e0147172bed2e2d3f859bcc33a8d9699cad1a75" +dependencies = [ + "proc-macro2", + "quote", +] + [[package]] name = "indexmap" version = "2.12.1" @@ -1009,6 +1053,39 @@ version = "1.0.15" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "4a5f13b858c8d314ee3e8f639011f7ccefe71f97f96e50151fb991f267928e2c" +[[package]] +name = "java-locator" +version = "0.1.9" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "09c46c1fe465c59b1474e665e85e1256c3893dd00927b8d55f63b09044c1e64f" +dependencies = [ + "glob", +] + +[[package]] +name = "jni" +version = "0.21.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1a87aa2bb7d2af34197c04845522473242e1aa17c12f4935d5856491a7fb8c97" +dependencies = [ + "cesu8", + "cfg-if", + "combine", + "java-locator", + "jni-sys", + "libloading", + "log", + "thiserror", + "walkdir", + "windows-sys 0.45.0", +] + +[[package]] +name = "jni-sys" +version = "0.3.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8eaf4bc02d17cbdd7ff4c7438cafcdf7fb9a4613313ad11b4f8fefe7d3fa0130" + [[package]] name = "jobserver" version = "0.1.34" @@ -1080,6 +1157,16 @@ dependencies = [ "rle-decode-fast", ] +[[package]] +name = "libloading" +version = "0.7.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b67380fd3b2fbe7527a606e18729d21c6f3951633d0500574c4dc22d2d638b9f" +dependencies = [ + "cfg-if", + "winapi", +] + [[package]] name = "libm" version = "0.2.15" @@ -1602,6 +1689,15 @@ version = "1.0.20" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "28d3b2b1366ec20994f1fd18c3c594f05c5dd4bc44d8bb0c1c632c8d6829481f" +[[package]] +name = "same-file" +version = "1.0.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "93fc1dc3aaa9bfed95e02e6eadabb4baf7e3078b0bd1b4d7b6b0b68378900502" +dependencies = [ + "winapi-util", +] + [[package]] name = "scopeguard" version = "1.2.0" @@ -1823,6 +1919,26 @@ dependencies = [ "syn 2.0.110", ] +[[package]] +name = "thiserror" +version = "1.0.69" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b6aaf5339b578ea85b50e080feb250a3e8ae8cfcdff9a461c9ec2904bc923f52" +dependencies = [ + "thiserror-impl", +] + +[[package]] +name = "thiserror-impl" +version = "1.0.69" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "4fee6c4efc90059e10f81e6d42c60a18f76588c3d74cb83a0b242a2b6c7504c1" +dependencies = [ + "proc-macro2", + "quote", + "syn 2.0.110", +] + [[package]] name = "time" version = "0.3.44" @@ -2037,6 +2153,16 @@ version = "0.9.5" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "0b928f33d975fc6ad9f86c8f283853ad26bdd5b10b7f1542aa2fa15e2289105a" +[[package]] +name = "walkdir" +version = "2.5.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "29790946404f91d9c5d06f9874efddea1dc06c5efe94541a7d6863108e3a5e4b" +dependencies = [ + "same-file", + "winapi-util", +] + [[package]] name = "wasi" version = "0.11.1+wasi-snapshot-preview1" @@ -2106,12 +2232,52 @@ dependencies = [ "rustls-pki-types", ] +[[package]] +name = "winapi" +version = "0.3.9" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5c839a674fcd7a98952e593242ea400abe93992746761e38641405d28b00f419" +dependencies = [ + "winapi-i686-pc-windows-gnu", + "winapi-x86_64-pc-windows-gnu", +] + +[[package]] +name = "winapi-i686-pc-windows-gnu" +version = "0.4.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ac3b87c63620426dd9b991e5ce0329eff545bccbbb34f3be09ff6fb6ab51b7b6" + +[[package]] +name = "winapi-util" +version = "0.1.11" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c2a7b1c03c876122aa43f3020e6c3c3ee5c05081c9a00739faf7503aeba10d22" +dependencies = [ + "windows-sys 0.61.2", +] + +[[package]] +name = "winapi-x86_64-pc-windows-gnu" +version = "0.4.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "712e227841d057c1ee1cd2fb22fa7e5a5461ae8e48fa2ca79ec42cfc1931183f" + [[package]] name = "windows-link" version = "0.2.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "f0805222e57f7521d6a62e36fa9163bc891acd422f971defe97d64e70d0a4fe5" +[[package]] +name = "windows-sys" +version = "0.45.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "75283be5efb2831d37ea142365f009c02ec203cd29a3ebecbc093d52315b66d0" +dependencies = [ + "windows-targets 0.42.2", +] + [[package]] name = "windows-sys" version = "0.52.0" @@ -2139,6 +2305,21 @@ dependencies = [ "windows-link", ] +[[package]] +name = "windows-targets" +version = "0.42.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8e5180c00cd44c9b1c88adb3693291f1cd93605ded80c250a75d472756b4d071" +dependencies = [ + "windows_aarch64_gnullvm 0.42.2", + "windows_aarch64_msvc 0.42.2", + "windows_i686_gnu 0.42.2", + "windows_i686_msvc 0.42.2", + "windows_x86_64_gnu 0.42.2", + "windows_x86_64_gnullvm 0.42.2", + "windows_x86_64_msvc 0.42.2", +] + [[package]] name = "windows-targets" version = "0.52.6" @@ -2172,6 +2353,12 @@ dependencies = [ "windows_x86_64_msvc 0.53.1", ] +[[package]] +name = "windows_aarch64_gnullvm" +version = "0.42.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "597a5118570b68bc08d8d59125332c54f1ba9d9adeedeef5b99b02ba2b0698f8" + [[package]] name = "windows_aarch64_gnullvm" version = "0.52.6" @@ -2184,6 +2371,12 @@ version = "0.53.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "a9d8416fa8b42f5c947f8482c43e7d89e73a173cead56d044f6a56104a6d1b53" +[[package]] +name = "windows_aarch64_msvc" +version = "0.42.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e08e8864a60f06ef0d0ff4ba04124db8b0fb3be5776a5cd47641e942e58c4d43" + [[package]] name = "windows_aarch64_msvc" version = "0.52.6" @@ -2196,6 +2389,12 @@ version = "0.53.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "b9d782e804c2f632e395708e99a94275910eb9100b2114651e04744e9b125006" +[[package]] +name = "windows_i686_gnu" +version = "0.42.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c61d927d8da41da96a81f029489353e68739737d3beca43145c8afec9a31a84f" + [[package]] name = "windows_i686_gnu" version = "0.52.6" @@ -2220,6 +2419,12 @@ version = "0.53.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "fa7359d10048f68ab8b09fa71c3daccfb0e9b559aed648a8f95469c27057180c" +[[package]] +name = "windows_i686_msvc" +version = "0.42.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "44d840b6ec649f480a41c8d80f9c65108b92d89345dd94027bfe06ac444d1060" + [[package]] name = "windows_i686_msvc" version = "0.52.6" @@ -2232,6 +2437,12 @@ version = "0.53.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "1e7ac75179f18232fe9c285163565a57ef8d3c89254a30685b57d83a38d326c2" +[[package]] +name = "windows_x86_64_gnu" +version = "0.42.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8de912b8b8feb55c064867cf047dda097f92d51efad5b491dfb98f6bbb70cb36" + [[package]] name = "windows_x86_64_gnu" version = "0.52.6" @@ -2244,6 +2455,12 @@ version = "0.53.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "9c3842cdd74a865a8066ab39c8a7a473c0778a3f29370b5fd6b4b9aa7df4a499" +[[package]] +name = "windows_x86_64_gnullvm" +version = "0.42.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "26d41b46a36d453748aedef1486d5c7a85db22e56aff34643984ea85514e94a3" + [[package]] name = "windows_x86_64_gnullvm" version = "0.52.6" @@ -2256,6 +2473,12 @@ version = "0.53.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "0ffa179e2d07eee8ad8f57493436566c7cc30ac536a3379fdf008f47f6bb7ae1" +[[package]] +name = "windows_x86_64_msvc" +version = "0.42.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9aec5da331524158c6d1a4ac0ab1541149c0b9505fde06423b02f5ef0106b9f0" + [[package]] name = "windows_x86_64_msvc" version = "0.52.6" diff --git a/Cargo.toml b/Cargo.toml index 6f14be0..850741a 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -27,3 +27,17 @@ cbc = { version = "0.1.2", features = ["alloc"] } aes = "0.8.4" pem = "3.0.6" ureq = "3.1.4" +include_dir = { version = "0.7.4", optional = true } + +[target.aarch64-linux-android.dependencies] +jni = { version = "0.21.1", features = ["invocation", "default"] } + +[build-dependencies] +cc = "1.0" + +[features] +library = ["dep:include_dir"] + +#[lib] +#crate-type = ["cdylib"] +#required-features = ["library"] diff --git a/build.rs b/build.rs new file mode 100644 index 0000000..d8db299 --- /dev/null +++ b/build.rs @@ -0,0 +1,12 @@ +fn main() { + let target = std::env::var("TARGET").unwrap(); + if target == "aarch64-linux-android" { + println!("cargo:rerun-if-changed=build.rs"); + println!("cargo:rustc-link-arg=-Wl,-z,max-page-size=65536"); + + cc::Build::new() + .file("src/log.c") + .compile("libc_code.a"); + println!("cargo:rerun-if-changed=src/log.c"); + } +} diff --git a/src/android.rs b/src/android.rs new file mode 100644 index 0000000..6da35ff --- /dev/null +++ b/src/android.rs @@ -0,0 +1,60 @@ +use jni::JNIEnv; +use jni::objects::{JClass}; +use jni::sys::{jstring, jboolean}; +use std::thread; +use jni::objects::JString; +use crate::{run_server, stop_server}; +use std::os::raw::c_char; + +#[link(name = "c_code", kind = "static")] +unsafe extern "C" { + pub fn android_log(tag: *const c_char, message: *const c_char); +} + +#[macro_export] +macro_rules! log_to_logcat { + ($tag:expr, $($arg:tt)*) => { + let log_message = format!($($arg)*); + + let _ = std::panic::catch_unwind(|| { + let tag = std::ffi::CString::new($tag).unwrap(); + let message = std::ffi::CString::new(log_message).unwrap(); + unsafe { + crate::android::android_log(tag.as_ptr(), message.as_ptr()); + } + }); + }; +} + +#[unsafe(no_mangle)] +extern "C" fn Java_one_ethanthesleepy_androidew_BackgroundService_startServer<'local>( + mut env: JNIEnv<'local>, + _class: JClass<'local>, + data_path: JString<'local>, + easter: jboolean +) -> jstring { + //crate::runtime::set_easter_mode(easter != 0); + + let data_path: String = env.get_string(&data_path).unwrap().into(); + //crate::runtime::set_datapath(data_path); + + let output = env.new_string(String::from("Azunyannnn~")).unwrap(); + thread::spawn(|| { + run_server(true).unwrap(); + }); + log_to_logcat!("ew", "running"); + output.into_raw() +} + +#[unsafe(no_mangle)] +extern "C" fn Java_one_ethanthesleepy_androidew_BackgroundService_stopServer<'local>(env: JNIEnv<'local>, _class: JClass<'local>) -> jstring { + stop_server(); + let output = env.new_string(String::from("I like Yui!")).unwrap(); + output.into_raw() +} + + +#[unsafe(no_mangle)] +extern "C" fn Java_one_ethanthesleepy_androidew_BackgroundService_setEasterMode<'local>(_env: JNIEnv<'local>, _class: JClass<'local>, easter: jboolean) { + //crate::runtime::set_easter_mode(easter != 0); +} diff --git a/src/ios.rs b/src/ios.rs new file mode 100644 index 0000000..5b1bae2 --- /dev/null +++ b/src/ios.rs @@ -0,0 +1,27 @@ +#[cfg(target_os = "ios")] +#[unsafe(no_mangle)] +#[unsafe(link_section = "__DATA,__mod_init_func")] +pub static INITIALIZER: extern "C" fn() = main; + +#[cfg(target_os = "ios")] +#[unsafe(no_mangle)] +pub extern "C" fn main() { + let data_path = get_bundle_path().into_os_string().into_string().unwrap(); + android::set_datapath(data_path); + + std::thread::spawn(|| { + run_server(true).unwrap(); + }); +} + +#[cfg(target_os = "ios")] +use objc2_foundation::{NSFileManager, NSSearchPathDirectory, NSSearchPathDomainMask}; + +#[cfg(target_os = "ios")] +fn get_bundle_path() -> std::path::PathBuf { + unsafe { + let manager = NSFileManager::defaultManager(); + let application_support = manager.URLsForDirectory_inDomains(NSSearchPathDirectory::ApplicationSupportDirectory, NSSearchPathDomainMask::UserDomainMask); + return application_support.to_vec_unchecked()[0].to_file_path().unwrap(); + } +} diff --git a/src/lib.rs b/src/lib.rs index 6e53d36..69e96dd 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -7,6 +7,14 @@ mod runtime; #[macro_use] mod macros; +#[cfg(feature = "library")] +#[cfg(target_os = "android")] +mod android; + +#[cfg(feature = "library")] +#[cfg(target_os = "ios")] +mod ios; + use actix_web::{ rt, App, @@ -66,7 +74,7 @@ pub async fn run_server(in_thread: bool) -> std::io::Result<()> { } #[actix_web::main] -async fn stop_server() { +pub async fn stop_server() { runtime::set_running(false); println!("Stopping"); } diff --git a/src/log.c b/src/log.c new file mode 100644 index 0000000..05aa6d4 --- /dev/null +++ b/src/log.c @@ -0,0 +1,7 @@ +#include + +#define LOGI(tag, ...) __android_log_print(ANDROID_LOG_INFO, tag, __VA_ARGS__) + +void android_log(const char* tag, const char* out) { + LOGI(tag, "%s", out); +} diff --git a/src/main.rs b/src/main.rs index f8016e2..057dde9 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,4 +1,10 @@ +#[cfg(not(feature = "library"))] fn main() -> std::io::Result<()> { ew::run_server(false) } + +#[cfg(feature = "library")] +fn main() { + panic!("Compiled with the library feature! You should load the shared object library and call the exported methods there."); +}