plugin_interfaces/
api.rs

1use std::ffi::CString;
2use crate::callbacks::get_host_callbacks;
3
4/// 向前端发送消息(供插件使用)
5/// 内部调用 host_send_to_frontend 函数
6pub fn send_to_frontend(event: &str, payload: &str) -> bool {
7    host_send_to_frontend(event, payload)
8}
9
10/// 调用主程序的 host_send_to_frontend 函数
11/// 这是实际执行向前端发送消息的函数
12pub fn host_send_to_frontend(event: &str, payload: &str) -> bool {
13    if let Some(callbacks) = get_host_callbacks() {
14        if let (Ok(event_str), Ok(payload_str)) = (CString::new(event), CString::new(payload)) {
15            // 调用主程序提供的 host_send_to_frontend 函数
16            return (callbacks.send_to_frontend)(event_str.as_ptr(), payload_str.as_ptr());
17        }
18    }
19    false
20}
21
22/// 获取应用配置(供插件使用)
23pub fn get_app_config(key: &str) -> Option<String> {
24    if let Some(callbacks) = get_host_callbacks() {
25        if let Ok(key_str) = CString::new(key) {
26            let result_ptr = (callbacks.get_app_config)(key_str.as_ptr());
27            if !result_ptr.is_null() {
28                unsafe {
29                    let c_str = std::ffi::CStr::from_ptr(result_ptr);
30                    return c_str.to_str().ok().map(|s| s.to_string());
31                }
32            }
33        }
34    }
35    None
36}
37
38/// 调用其他插件(供插件使用)
39pub fn call_other_plugin(plugin_id: &str, message: &str) -> Option<String> {
40    if let Some(callbacks) = get_host_callbacks() {
41        if let (Ok(id_str), Ok(msg_str)) = (CString::new(plugin_id), CString::new(message)) {
42            let result_ptr = (callbacks.call_other_plugin)(id_str.as_ptr(), msg_str.as_ptr());
43            if !result_ptr.is_null() {
44                unsafe {
45                    let c_str = std::ffi::CStr::from_ptr(result_ptr);
46                    return c_str.to_str().ok().map(|s| s.to_string());
47                }
48            }
49        }
50    }
51    None
52}