commit d3f9d03dcfd8631d79903abe46b1dc93c46e79b6
parent b18c96912524e433cb1877120b492c8d212bfa98
Author: rhunk <101876869+rhunk@users.noreply.github.com>
Date:   Sat, 27 Jul 2024 19:51:41 +0200

feat(native): custom font hook

Diffstat:
Mnative/rust/src/lib.rs | 5+++--
Mnative/rust/src/modules/custom_font_hook.rs | 32++++++++++++++++++++++++++++++++
Mnative/rust/src/modules/linker_hook.rs | 2+-
Mnative/rust/src/modules/mod.rs | 5+++--
4 files changed, 39 insertions(+), 5 deletions(-)

diff --git a/native/rust/src/lib.rs b/native/rust/src/lib.rs @@ -13,7 +13,7 @@ mod modules; use android_logger::Config; use log::LevelFilter; -use modules::{composer_hook, duplex_hook, fstat_hook, linker_hook, sqlite_hook, unary_call_hook}; +use modules::{composer_hook, custom_font_hook, duplex_hook, fstat_hook, linker_hook, sqlite_hook, unary_call_hook}; use jni::objects::{JObject, JString}; use jni::sys::{jint, jstring, JNI_VERSION_1_6}; @@ -67,7 +67,8 @@ fn init(mut env: JNIEnv, _class: JObject, signature_cache: JString) -> jstring { unary_call_hook::init(), composer_hook::init(), fstat_hook::init(), - sqlite_hook::init() + sqlite_hook::init(), + custom_font_hook::init() ); threads.into_iter().for_each(|t| t.join().unwrap()); diff --git a/native/rust/src/modules/custom_font_hook.rs b/native/rust/src/modules/custom_font_hook.rs @@ -0,0 +1,31 @@ +use std::{ffi::CStr, fs}; + +use crate::{config, def_hook, dobby_hook_sym}; + +def_hook!( + open_hook, + i32, + |path: *const u8, flags: i32| { + let mut path = path; + + if let Ok(pathname) = CStr::from_ptr(path).to_str() { + if pathname == "/system/fonts/NotoColorEmoji.ttf" { + if let Some(font_path) = config::native_config().custom_emoji_font_path { + if fs::metadata(&font_path).is_ok() { + path = (font_path.to_owned() + "\0").as_ptr(); + debug!("open {}", font_path); + } else { + warn!("custom emoji font path does not exist: {}", font_path); + } + } + } + } + + open_hook_original.unwrap()(path, flags) + } +); + + +pub fn init() { + dobby_hook_sym!("libc.so", "open", open_hook); +}+ \ No newline at end of file diff --git a/native/rust/src/modules/linker_hook.rs b/native/rust/src/modules/linker_hook.rs @@ -15,7 +15,7 @@ def_hook!( let pathname_str = CStr::from_ptr(pathname).to_str().unwrap().to_string(); if let Some(content) = SHARED_LIBRARIES.lock().unwrap().remove(&pathname_str) { - let memfd = libc::syscall(libc::SYS_memfd_create, "me.rhunk.snapenhance\0".as_ptr(), 0) as i32; + let memfd = libc::syscall(libc::SYS_memfd_create, "jit-cache\0".as_ptr(), 0) as i32; let content = content.into_boxed_slice(); if libc::write(memfd, content.as_ptr() as *const c_void, content.len() as libc::size_t) == -1 { diff --git a/native/rust/src/modules/mod.rs b/native/rust/src/modules/mod.rs @@ -3,4 +3,5 @@ pub mod duplex_hook; pub mod sqlite_hook; pub mod fstat_hook; pub mod unary_call_hook; -pub mod composer_hook;- \ No newline at end of file +pub mod composer_hook; +pub mod custom_font_hook;+ \ No newline at end of file