This commit is contained in:
shenjack 2025-02-11 23:15:20 +08:00
parent 12a32da61e
commit 4eb553473d
Signed by: shenjack
GPG Key ID: 7B1134A979775551
4 changed files with 31 additions and 19 deletions

View File

@ -13,7 +13,7 @@ use crate::error::{ClientResult, IcaError};
use crate::{version_str, StopGetter};
/// icalingua 客户端的兼容版本号
pub const ICA_PROTOCOL_VERSION: &str = "2.12.26";
pub const ICA_PROTOCOL_VERSION: &str = "2.12.28";
// mod status {
// use crate::data_struct::ica::all_rooms::Room;

View File

@ -26,14 +26,17 @@ impl PyTasks {
pub fn push_ica_new_message(&mut self, handle: tokio::task::JoinHandle<()>) {
self.ica_new_message.push(handle);
self.ica_new_message.retain(|handle| !handle.is_finished());
}
pub fn push_ica_delete_message(&mut self, handle: tokio::task::JoinHandle<()>) {
self.ica_delete_message.push(handle);
self.ica_delete_message.retain(|handle| !handle.is_finished());
}
pub fn push_tailchat_new_message(&mut self, handle: tokio::task::JoinHandle<()>) {
self.tailchat_new_message.push(handle);
self.tailchat_new_message.retain(|handle| !handle.is_finished());
}
pub async fn join_all(&mut self) {
@ -221,7 +224,7 @@ pub async fn ica_new_message_py(message: &ica::messages::NewMessage, client: &Cl
let client = class::ica::IcaClientPy::new(client);
let args = (msg, client);
let task = call_py_func!(args, plugin, path, ICA_NEW_MESSAGE_FUNC, client);
PY_TASKS.lock().await.push_ica_new_message(task);
// PY_TASKS.lock().await.push_ica_new_message(task);
}
}
@ -234,7 +237,7 @@ pub async fn ica_delete_message_py(msg_id: ica::MessageId, client: &Client) {
let client = class::ica::IcaClientPy::new(client);
let args = (msg_id.clone(), client);
let task = call_py_func!(args, plugin, path, ICA_DELETE_MESSAGE_FUNC, client);
PY_TASKS.lock().await.push_ica_delete_message(task);
// PY_TASKS.lock().await.push_ica_delete_message(task);
}
}
@ -250,6 +253,6 @@ pub async fn tailchat_new_message_py(
let client = class::tailchat::TailchatClientPy::new(client);
let args = (msg, client);
let task = call_py_func!(args, plugin, path, TAILCHAT_NEW_MESSAGE_FUNC, client);
PY_TASKS.lock().await.push_tailchat_new_message(task);
// PY_TASKS.lock().await.push_tailchat_new_message(task);
}
}

View File

@ -14,7 +14,7 @@ use pyo3::prelude::*;
use pyo3::types::PyTuple;
use tracing::{event, span, warn, Level};
use crate::{MainStatus, StopGetter};
use crate::MainStatus;
#[derive(Debug, Clone)]
pub struct PyStatus {
@ -39,12 +39,18 @@ impl PyStatus {
let _ = unsafe { PyPluginStatus.get_or_init(|| status) };
}
pub fn get() -> &'static PyStatus { unsafe { PyPluginStatus.get().unwrap() } }
pub fn get() -> &'static PyStatus {
unsafe { PyPluginStatus.get().unwrap() }
}
pub fn get_mut() -> &'static mut PyStatus { unsafe { PyPluginStatus.get_mut().unwrap() } }
pub fn get_mut() -> &'static mut PyStatus {
unsafe { PyPluginStatus.get_mut().unwrap() }
}
/// 添加一个插件
pub fn add_file(&mut self, path: PathBuf, plugin: PyPlugin) { self.files.insert(path, plugin); }
pub fn add_file(&mut self, path: PathBuf, plugin: PyPlugin) {
self.files.insert(path, plugin);
}
/// 重新加载一个插件
pub fn reload_plugin(&mut self, plugin_name: &str) -> bool {
@ -64,7 +70,9 @@ impl PyStatus {
}
/// 删除一个插件
pub fn delete_file(&mut self, path: &PathBuf) -> Option<PyPlugin> { self.files.remove(path) }
pub fn delete_file(&mut self, path: &PathBuf) -> Option<PyPlugin> {
self.files.remove(path)
}
pub fn get_status(&self, pluging_id: &str) -> Option<bool> {
self.files.iter().find_map(|(_, plugin)| {
@ -187,7 +195,9 @@ impl PyPlugin {
}
}
pub fn get_id(&self) -> String { plugin_path_as_id(&self.file_path) }
pub fn get_id(&self) -> String {
plugin_path_as_id(&self.file_path)
}
}
impl Display for PyPlugin {
@ -344,7 +354,7 @@ impl TryFrom<RawPyPlugin> for PyPlugin {
}
/// 插件路径转换为 id
pub fn plugin_path_as_id(path: &PathBuf) -> String {
pub fn plugin_path_as_id(path: &Path) -> String {
path.file_name()
.unwrap_or_default()
.to_str()
@ -388,7 +398,9 @@ pub fn load_py_plugins(path: &PathBuf) {
);
}
pub fn get_change_time(path: &Path) -> Option<SystemTime> { path.metadata().ok()?.modified().ok() }
pub fn get_change_time(path: &Path) -> Option<SystemTime> {
path.metadata().ok()?.modified().ok()
}
pub fn py_module_from_code(content: &str, path: &Path) -> PyResult<Py<PyAny>> {
Python::with_gil(|py| -> PyResult<Py<PyAny>> {
@ -430,11 +442,7 @@ fn init_py_with_env_path(path: &str) {
let wide_path = OsStr::new(path).encode_wide().chain(Some(0)).collect::<Vec<u16>>();
// 设置 prefix 和 exec_prefix
pyo3::ffi::PyConfig_SetString(
config_ptr,
&mut config.prefix as *mut _,
wide_path.as_ptr(),
);
pyo3::ffi::PyConfig_SetString(config_ptr, &mut config.prefix as *mut _, wide_path.as_ptr());
pyo3::ffi::PyConfig_SetString(
config_ptr,
&mut config.exec_prefix as *mut _,
@ -505,7 +513,7 @@ pub async fn post_py() -> anyhow::Result<()> {
status.config.sync_status_to_config();
status.config.write_to_default()?;
stop_tasks().await;
// stop_tasks().await;
Ok(())
}
@ -518,7 +526,7 @@ async fn stop_tasks() {
event!(Level::INFO, "Python 任务完成");
}
_ = tokio::signal::ctrl_c() => {
call::PY_TASKS.lock().await.cancel_all();
// call::PY_TASKS.lock().await.cancel_all();
event!(Level::INFO, "Python 任务被中断");
}
}

View File

@ -2,6 +2,7 @@
## 0.8.2
- ica 兼容版本号更新到 `2.12.28`
- 现在支持通过读取环境变量里的 `VIRTUAL_ENV` 来获取虚拟环境路径
- 用于在虚拟环境中运行插件
- 添加了 `-h` 参数