diff --git a/ica-rs/Cargo.lock b/ica-rs/Cargo.lock index 95687bd..5034f0c 100644 --- a/ica-rs/Cargo.lock +++ b/ica-rs/Cargo.lock @@ -569,6 +569,7 @@ dependencies = [ "ed25519", "ed25519-dalek", "hex", + "pkg-config", "pyo3", "rust_socketio", "serde", @@ -871,9 +872,9 @@ dependencies = [ [[package]] name = "pkg-config" -version = "0.3.27" +version = "0.3.29" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "26072860ba924cbfa98ea39c8c19b4dd6a4a25423dbdf219c1eca91aa0cf6964" +checksum = "2900ede94e305130c13ddd391e0ab7cbaeb783945ae07a279c268cb05109c6cb" [[package]] name = "platforms" diff --git a/ica-rs/Cargo.toml b/ica-rs/Cargo.toml index 3002232..5738b55 100644 --- a/ica-rs/Cargo.toml +++ b/ica-rs/Cargo.toml @@ -30,4 +30,4 @@ version = "0.20.2" rust_socketio = { git = "https://github.com/shenjackyuanjie/rust-socketio.git", branch = "mult_payload" } [build-dependencies] - +pkg-config = "0.3.29" diff --git a/ica-rs/build.rs b/ica-rs/build.rs index 8c3a548..87e3002 100644 --- a/ica-rs/build.rs +++ b/ica-rs/build.rs @@ -11,11 +11,29 @@ fn pyo3_config() { // wsl #[cfg(target_os = "linux")] { - env::set_var("PYO3_PRINT_CONFIG", "1"); - env::set_var("PYO3_PYTHON", "/usr/bin/python3.11"); + // env::set_var("PYO3_PRINT_CONFIG", "1"); + // env::set_var("PYO3_PYTHON", "/usr/bin/python3.10"); + + // cargo:rustc-link-lib=static= + // println!("cargo:rustc-link-lib=python3.10"); + // println!("cargo:rustc-link-arg-bins=-Wl,-Bstatic"); + // println!("cargo:rustc-link-arg-bins=-Wl,--whole-archive"); + // println!("cargo:rustc-link-arg-bins=-lpython3.10"); + // println!("cargo:rustc-link-arg-bins=-Wl,-Bdynamic"); + // println!("cargo:rustc-link-arg-bins=-Wl,--no-whole-archive"); + // println!("cargo:rustc-link-arg-bins=-lz"); + // println!("cargo:rustc-link-arg-bins=-lexpat"); + // println!("cargo:rustc-link-arg-bins=-lutil"); + // println!("cargo:rustc-link-arg-bins=-lm"); + // println!("cargo:rustc-link-arg-bins=-Wl,--export-dynamic"); + + // pkg_config::Config::new() + // .atleast_version("3.8") + // .probe("python3-embed") + // .unwrap(); } } fn main() { - // pyo3_config(); + pyo3_config(); } diff --git a/ica-rs/src/main.rs b/ica-rs/src/main.rs index e4d203e..b322618 100644 --- a/ica-rs/src/main.rs +++ b/ica-rs/src/main.rs @@ -24,6 +24,7 @@ fn any_event(event: Event, payload: Payload, _client: RawClient) { } fn ws_main() { + py::init_py(); // define a callback which is called when a payload is received // this callback gets the payload as well as an instance of the // socket to communicate with the server @@ -34,7 +35,10 @@ fn ws_main() { // println!("{}", "已经登录到 icalingua!".green()); // } match value.as_str() { - Some("authSucceed") => println!("{}", "已经登录到 icalingua!".green()), + Some("authSucceed") => { + py::run(); + println!("{}", "已经登录到 icalingua!".green()) + } Some("authFailed") => { println!("{}", "登录到 icalingua 失败!".red()); panic!("登录失败") @@ -67,7 +71,6 @@ fn ws_main() { } fn main() { - tracing_subscriber::fmt() .with_max_level(tracing::Level::DEBUG) .init(); diff --git a/ica-rs/src/py.rs b/ica-rs/src/py.rs index 42af08f..25accd2 100644 --- a/ica-rs/src/py.rs +++ b/ica-rs/src/py.rs @@ -1,14 +1,23 @@ // use inline_python::{python, Context}; -// use pyo3::prelude::*; +use pyo3::{prelude::*, types::IntoPyDict}; -// #[pyclass] -// #[pyo3(name = "BotStatus")] -// pub struct BotStatusPy {} +#[pyclass] +#[pyo3(name = "BotStatus")] +pub struct BotStatusPy {} -// pub fn run() { - // -// } +pub fn run() { + Python::with_gil(|py| { + let bot_status = BotStatusPy {}; + let _bot_status = PyCell::new(py, bot_status).unwrap(); + let locals = [("state", _bot_status)].into_py_dict(py); + py.run("print(state)", None, Some(locals)).unwrap(); + }); +} + +pub fn init_py() { + pyo3::prepare_freethreaded_python(); +} // pub fn run() { // let con: Context = python! {