Skip to main content

maa_framework/
lib.rs

1//! # MaaFramework Rust Bindings
2//!
3//! High-performance, safe Rust bindings for [MaaFramework](https://github.com/MaaXYZ/MaaFramework),
4//! a game automation framework based on image recognition.
5//!
6//! ## Quick Start
7//!
8//! ```no_run
9//! use maa_framework::toolkit::Toolkit;
10//! use maa_framework::controller::Controller;
11//! use maa_framework::resource::Resource;
12//! use maa_framework::tasker::Tasker;
13//!
14//! fn main() -> Result<(), Box<dyn std::error::Error>> {
15//!     // 0. Load library (Dynamic only)
16//!     #[cfg(feature = "dynamic")]
17//!     maa_framework::load_library(std::path::Path::new("MaaFramework.dll"))?;
18//!
19//!     // 1. Find devices
20//!     let devices = Toolkit::find_adb_devices()?;
21//!     let device = devices.first().expect("No device found");
22//!
23//!     // 2. Create controller (agent_path: "" to use MAA_AGENT_PATH or current dir)
24//!     let adb_path = device.adb_path.to_str().ok_or_else(|| {
25//!         std::io::Error::new(std::io::ErrorKind::InvalidData, "Invalid ADB path")
26//!     })?;
27//!     let controller = Controller::new_adb(adb_path, &device.address, "{}", "")?;
28//!
29//!     // 3. Create resource and tasker
30//!     let resource = Resource::new()?;
31//!     let tasker = Tasker::new()?;
32//!
33//!     // 4. Bind and run
34//!     tasker.bind(&resource, &controller)?;
35//!     let job = tasker.post_task("StartTask", "{}")?;
36//!     job.wait();
37//!
38//!     Ok(())
39//! }
40//! ```
41//!
42//! ## Core Modules
43//!
44//! | Module | Description |
45//! |--------|-------------|
46//! | [`tasker`] | Task execution and pipeline management |
47//! | [`resource`] | Resource loading (images, models, pipelines) |
48//! | [`controller`] | Device control (ADB, Win32, macOS, Android Native, PlayCover, WlRoots) |
49//! | [`context`] | Task execution context for custom components |
50//! | [`toolkit`] | Device discovery utilities |
51//! | [`pipeline`] | Pipeline configuration types |
52//! | [`job`] | Asynchronous job management |
53//! | [`event_sink`] | Event sink system for typed callbacks |
54//! | [`buffer`] | Safe data buffers for FFI |
55//! | [`custom`] | Custom recognizer and action traits |
56//! | [`custom_controller`] | Custom controller implementation |
57//! | [`notification`] | Structured event notification parsing |
58//! | [`common`] | Common types and data structures |
59//! | [`error`] | Error types and handling |
60//! | [`util`] | Miscellaneous utility functions |
61//! | [`agent_client`] | Remote custom component client |
62//! | [`agent_server`] | Remote custom component server |
63//!
64//! ## Feature Flags
65//!
66//! - `adb` - ADB controller support (default)
67//! - `win32` - Win32 controller support (Windows only)
68//! - `custom` - Custom recognizer/action/controller support
69//! - `toolkit` - Device discovery utilities
70//! - `image` - Integration with the `image` crate
71
72#![allow(non_upper_case_globals)]
73#![allow(non_camel_case_types)]
74#![allow(non_snake_case)]
75
76pub mod agent_client;
77pub mod agent_server;
78pub mod buffer;
79pub mod callback;
80pub mod common;
81pub mod context;
82pub mod controller;
83pub mod custom;
84pub mod custom_controller;
85pub mod error;
86pub mod event_sink;
87pub mod job;
88pub mod notification;
89pub mod pipeline;
90pub mod resource;
91pub mod tasker;
92pub mod toolkit;
93pub mod util;
94
95pub use common::AndroidNativeControllerConfig;
96pub use common::AndroidScreenResolution;
97pub use common::ControllerFeature;
98pub use common::MaaStatus;
99pub use error::{MaaError, MaaResult};
100
101pub use maa_framework_sys as sys;
102
103use std::ffi::CString;
104use std::sync::atomic::{AtomicU8, Ordering};
105
106const RUNTIME_CONTEXT_UNKNOWN: u8 = 0;
107#[cfg(feature = "dynamic")]
108const RUNTIME_CONTEXT_FRAMEWORK: u8 = 1;
109const RUNTIME_CONTEXT_AGENT_SERVER: u8 = 2;
110
111static RUNTIME_CONTEXT: AtomicU8 = AtomicU8::new(RUNTIME_CONTEXT_UNKNOWN);
112
113/// Get the MaaFramework version string.
114///
115/// # Example
116/// ```no_run
117/// println!("MaaFramework version: {}", maa_framework::maa_version());
118/// ```
119pub fn maa_version() -> &'static str {
120    unsafe {
121        std::ffi::CStr::from_ptr(sys::MaaVersion())
122            .to_str()
123            .unwrap_or("unknown")
124    }
125}
126
127/// Set a global framework option.
128///
129/// Low-level function for setting global options. Consider using the
130/// convenience wrappers like [`configure_logging`], [`set_debug_mode`], etc.
131pub fn set_global_option(
132    key: sys::MaaGlobalOption,
133    value: *mut std::ffi::c_void,
134    size: u64,
135) -> MaaResult<()> {
136    let ret = unsafe { sys::MaaGlobalSetOption(key, value, size) };
137    common::check_bool(ret)
138}
139
140/// Configure the log output directory.
141///
142/// # Arguments
143/// * `log_dir` - Path to the directory where logs should be stored
144pub fn configure_logging(log_dir: &str) -> MaaResult<()> {
145    let c_dir = CString::new(log_dir)?;
146    set_global_option(
147        sys::MaaGlobalOptionEnum_MaaGlobalOption_LogDir as i32,
148        c_dir.as_ptr() as *mut _,
149        c_dir.as_bytes().len() as u64,
150    )
151}
152
153/// Enable or disable debug mode.
154///
155/// In debug mode:
156/// - Recognition details include raw images and draws
157/// - All tasks are treated as focus tasks and produce callbacks
158///
159/// # Arguments
160/// * `enable` - `true` to enable debug mode
161pub fn set_debug_mode(enable: bool) -> MaaResult<()> {
162    let mut val_bool = if enable { 1u8 } else { 0u8 };
163    set_global_option(
164        sys::MaaGlobalOptionEnum_MaaGlobalOption_DebugMode as i32,
165        &mut val_bool as *mut _ as *mut _,
166        std::mem::size_of::<u8>() as u64,
167    )
168}
169
170/// Set the log level for stdout output.
171///
172/// # Arguments
173/// * `level` - Logging level (use `sys::MaaLoggingLevel*` constants)
174pub fn set_stdout_level(level: sys::MaaLoggingLevel) -> MaaResult<()> {
175    let mut val = level;
176    set_global_option(
177        sys::MaaGlobalOptionEnum_MaaGlobalOption_StdoutLevel as i32,
178        &mut val as *mut _ as *mut _,
179        std::mem::size_of::<sys::MaaLoggingLevel>() as u64,
180    )
181}
182
183/// Enable/disable saving recognition visualizations to log directory.
184pub fn set_save_draw(enable: bool) -> MaaResult<()> {
185    let mut val: u8 = if enable { 1 } else { 0 };
186    set_global_option(
187        sys::MaaGlobalOptionEnum_MaaGlobalOption_SaveDraw as i32,
188        &mut val as *mut _ as *mut _,
189        std::mem::size_of::<u8>() as u64,
190    )
191}
192
193/// Enable/disable saving screenshots on error.
194pub fn set_save_on_error(enable: bool) -> MaaResult<()> {
195    let mut val: u8 = if enable { 1 } else { 0 };
196    set_global_option(
197        sys::MaaGlobalOptionEnum_MaaGlobalOption_SaveOnError as i32,
198        &mut val as *mut _ as *mut _,
199        std::mem::size_of::<u8>() as u64,
200    )
201}
202
203/// Set JPEG quality for saved draw images (0-100, default 85).
204pub fn set_draw_quality(quality: i32) -> MaaResult<()> {
205    let mut val = quality;
206    set_global_option(
207        sys::MaaGlobalOptionEnum_MaaGlobalOption_DrawQuality as i32,
208        &mut val as *mut _ as *mut _,
209        std::mem::size_of::<i32>() as u64,
210    )
211}
212
213/// Set the recognition image cache limit (default 4096).
214pub fn set_reco_image_cache_limit(limit: u64) -> MaaResult<()> {
215    let mut val = limit;
216    set_global_option(
217        sys::MaaGlobalOptionEnum_MaaGlobalOption_RecoImageCacheLimit as i32,
218        &mut val as *mut _ as *mut _,
219        std::mem::size_of::<u64>() as u64,
220    )
221}
222
223/// Load a plugin from the specified path.
224pub fn load_plugin(path: &str) -> MaaResult<()> {
225    let c_path = CString::new(path)?;
226    let ret = unsafe { sys::MaaGlobalLoadPlugin(c_path.as_ptr()) };
227    common::check_bool(ret)
228}
229
230/// Loads the MaaFramework dynamic library.
231///
232/// You **must** call this function successfully before using any other APIs when the `dynamic`
233/// feature is enabled.
234///
235/// # Arguments
236///
237/// * `path` - Path to the dynamic library file (e.g., `MaaFramework.dll`, `libMaaFramework.so`).
238///
239/// # Errors
240///
241/// Returns an error if:
242/// * The library file cannot be found or loaded.
243/// * The library has already been loaded (multiple initialization is not supported).
244/// * Required symbols are missing from the library.
245///
246/// # Panics
247///
248/// Subsequent calls to any MaaFramework API will panic if the library has not been initialized.
249///
250/// # Safety
251///
252/// This function is `unsafe` because:
253/// * It executes arbitrary initialization code (e.g., `DllMain`) inside the loaded library.
254/// * The caller must ensure `path` points to a valid MaaFramework binary compatible with these bindings.
255#[cfg(feature = "dynamic")]
256pub fn load_library(path: &std::path::Path) -> Result<(), String> {
257    let context = runtime_context_from_library_path(path);
258    unsafe { sys::load_library(path) }?;
259    RUNTIME_CONTEXT.store(context, Ordering::Relaxed);
260    Ok(())
261}
262
263/// Finds and loads the MaaFramework dynamic library when using the `dynamic` feature.
264///
265/// Tries, in order: `MAA_SDK_PATH` (bin/lib), project `MAA-*` dirs (from `CARGO_MANIFEST_DIR`
266/// or current dir), `target/debug` or `target/release`, then current dir. Use this in examples
267/// or apps so that `cargo run --example main` works without setting env vars.
268///
269/// # Errors
270///
271/// Returns an error if no library file is found or loading fails.
272#[cfg(feature = "dynamic")]
273pub fn ensure_library_loaded() -> Result<(), String> {
274    let lib_name = if cfg!(target_os = "windows") {
275        "MaaFramework.dll"
276    } else if cfg!(target_os = "macos") {
277        "libMaaFramework.dylib"
278    } else {
279        "libMaaFramework.so"
280    };
281
282    let mut candidates: Vec<std::path::PathBuf> = Vec::new();
283
284    if let Ok(sdk) = std::env::var("MAA_SDK_PATH") {
285        let sdk = std::path::PathBuf::from(sdk);
286        candidates.push(sdk.join("bin").join(lib_name));
287        candidates.push(sdk.join("lib").join(lib_name));
288    }
289
290    let search_roots: Vec<std::path::PathBuf> = std::env::var("CARGO_MANIFEST_DIR")
291        .map(std::path::PathBuf::from)
292        .into_iter()
293        .chain(std::env::current_dir().ok())
294        .collect();
295
296    for root in &search_roots {
297        if let Ok(entries) = std::fs::read_dir(root) {
298            for e in entries.flatten() {
299                let p = e.path();
300                if p.is_dir() {
301                    if let Some(name) = p.file_name() {
302                        if name.to_string_lossy().starts_with("MAA-") {
303                            candidates.push(p.join("bin").join(lib_name));
304                        }
305                    }
306                }
307            }
308        }
309    }
310
311    if let Ok(cwd) = std::env::current_dir() {
312        candidates.push(cwd.join("target/debug").join(lib_name));
313        candidates.push(cwd.join("target/release").join(lib_name));
314        candidates.push(cwd.join(lib_name));
315    }
316
317    let chosen = candidates.into_iter().find(|p| p.exists());
318    match chosen {
319        Some(path) => load_library(&path),
320        None => Err(
321            "MaaFramework library not found. Set MAA_SDK_PATH or place SDK (e.g. MAA-*/bin/)."
322                .to_string(),
323        ),
324    }
325}
326
327pub(crate) fn mark_agent_server_context() {
328    RUNTIME_CONTEXT.store(RUNTIME_CONTEXT_AGENT_SERVER, Ordering::Relaxed);
329}
330
331pub(crate) fn is_agent_server_context() -> bool {
332    RUNTIME_CONTEXT.load(Ordering::Relaxed) == RUNTIME_CONTEXT_AGENT_SERVER
333}
334
335#[cfg(feature = "dynamic")]
336fn runtime_context_from_library_path(path: &std::path::Path) -> u8 {
337    let Some(file_name) = path.file_name() else {
338        return RUNTIME_CONTEXT_UNKNOWN;
339    };
340    let file_name = file_name.to_string_lossy();
341    if file_name.is_empty() {
342        return RUNTIME_CONTEXT_UNKNOWN;
343    }
344    if file_name.contains("MaaAgentServer") {
345        RUNTIME_CONTEXT_AGENT_SERVER
346    } else {
347        RUNTIME_CONTEXT_FRAMEWORK
348    }
349}