From dcb9012ff6b68b821e17bb828505c8ccdd28830c Mon Sep 17 00:00:00 2001 From: Wind-Explorer Date: Tue, 17 Feb 2026 01:12:59 +0800 Subject: [PATCH] foreground app listener support --- .../src/services/presence_modules/mod.rs | 10 +----- .../src/services/presence_modules/models.rs | 7 +++++ .../src/services/presence_modules/runtime.rs | 31 ++++++++++++++++--- 3 files changed, 34 insertions(+), 14 deletions(-) diff --git a/src-tauri/src/services/presence_modules/mod.rs b/src-tauri/src/services/presence_modules/mod.rs index b375272..5d65c3c 100644 --- a/src-tauri/src/services/presence_modules/mod.rs +++ b/src-tauri/src/services/presence_modules/mod.rs @@ -1,21 +1,13 @@ use tauri::Manager; use tracing::{error, info, warn}; -use crate::{get_app_handle, lock_w}; -use serde::{Deserialize, Serialize}; +use crate::{get_app_handle, lock_w, services::presence_modules::models::ModuleMetadata}; use serde_json; use std::fs; pub mod models; pub mod runtime; -#[derive(Serialize, Deserialize, Debug)] -pub struct ModuleMetadata { - pub name: String, - pub version: String, - pub description: Option, -} - fn get_module_metadata(path: &std::path::Path) -> Option { let metadata_path = path.join("metadata.json"); if metadata_path.exists() { diff --git a/src-tauri/src/services/presence_modules/models.rs b/src-tauri/src/services/presence_modules/models.rs index c90cffc..453882c 100644 --- a/src-tauri/src/services/presence_modules/models.rs +++ b/src-tauri/src/services/presence_modules/models.rs @@ -9,3 +9,10 @@ pub struct PresenceStatus { pub subtitle: Option, pub graphics_b64: Option, } + +#[derive(Serialize, Deserialize, Debug)] +pub struct ModuleMetadata { + pub name: String, + pub version: String, + pub description: Option, +} diff --git a/src-tauri/src/services/presence_modules/runtime.rs b/src-tauri/src/services/presence_modules/runtime.rs index d757f5a..7445f3c 100644 --- a/src-tauri/src/services/presence_modules/runtime.rs +++ b/src-tauri/src/services/presence_modules/runtime.rs @@ -20,6 +20,14 @@ impl UserData for Engine { thread::sleep(Duration::from_secs(seconds)); Ok(()) }); + methods.add_method("path_from_relative", |lua, _, relative_path: String| { + let script_path: String = lua.globals().get("script_path")?; + let path = Path::new(&script_path); + let parent = path.parent().unwrap_or(path); + let full_path = parent.join(&relative_path); + Ok(full_path.to_string_lossy().to_string()) + }); + methods.add_method("get_platform", |_, _, ()| Ok(std::env::consts::OS)); methods.add_method("update_status", |lua, _, value: Value| { let status: PresenceStatus = lua.from_value(value)?; let rt = Runtime::new().unwrap(); @@ -52,13 +60,27 @@ async fn update_status_async(status: PresenceStatus) { } } -pub fn spawn_lua_runtime(script: &str) -> thread::JoinHandle<()> { - let script = script.to_string(); +pub fn spawn_lua_runtime(script_path: &Path) -> thread::JoinHandle<()> { + let script_path = script_path.to_owned(); thread::spawn(move || { - let lua = Lua::new(); + let lua = unsafe { Lua::unsafe_new() }; let globals = lua.globals(); + // Set script_path for engine methods to access + if let Err(e) = globals.set("script_path", script_path.to_string_lossy().to_string()) { + error!("Failed to set script_path global: {}", e); + return; + } + + let script = match std::fs::read_to_string(&script_path) { + Ok(s) => s, + Err(e) => { + error!("Failed to read script: {}", e); + return; + } + }; + if let Err(e) = globals.set("engine", Engine) { error!("Failed to set engine global: {}", e); return; @@ -71,6 +93,5 @@ pub fn spawn_lua_runtime(script: &str) -> thread::JoinHandle<()> { } pub fn spawn_lua_runtime_from_path(path: &Path) -> Result, std::io::Error> { - let script = std::fs::read_to_string(path)?; - Ok(spawn_lua_runtime(&script)) + Ok(spawn_lua_runtime(path)) }