maa-framework 1.24.0

Rust bindings for MaaFramework
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
//! Custom recognizer and action components.
//!
//! This module provides traits for implementing custom recognition algorithms
//! and actions that integrate with the MaaFramework pipeline system.
//!
//! # Usage
//!
//! 1. Implement [`CustomRecognition`] or [`CustomAction`] trait
//! 2. Register with [`Resource::register_custom_recognition`] or [`Resource::register_custom_action`]
//! 3. Reference in pipeline JSON via `"recognition": "Custom"` or `"action": "Custom"`

use crate::context::Context;
use crate::resource::Resource;
use crate::{MaaError, MaaResult, common, sys};
use std::ffi::{CStr, CString};
use std::os::raw::c_void;

// === Trait Definitions ===

/// Trait for implementing custom actions.
///
/// Actions are executed after a successful recognition and can perform
/// clicks, swipes, or custom logic.
pub trait CustomAction: Send + Sync {
    /// Execute the custom action.
    ///
    /// # Arguments
    /// * `context` - The current execution context
    /// * `task_id` - ID of the current task
    /// * `node_name` - Name of the current node
    /// * `custom_action_name` - Name of this action (as registered)
    /// * `custom_action_param` - JSON parameters for this action
    /// * `reco_id` - ID of the preceding recognition result
    /// * `box_rect` - Target region found by recognition
    ///
    /// # Returns
    /// `true` if the action succeeded, `false` otherwise
    fn run(
        &self,
        context: &Context,
        task_id: common::MaaId,
        node_name: &str,
        custom_action_name: &str,
        custom_action_param: &str,
        reco_id: common::MaaId,
        box_rect: &common::Rect,
    ) -> bool;
}

/// Trait for implementing custom recognizers.
///
/// Recognizers analyze screenshots to find targets in the UI.
pub trait CustomRecognition: Send + Sync {
    /// Analyze an image to find the target.
    ///
    /// # Arguments
    /// * `context` - The current execution context
    /// * `task_id` - ID of the current task
    /// * `node_name` - Name of the current node
    /// * `custom_recognition_name` - Name of this recognizer
    /// * `custom_recognition_param` - JSON parameters for this recognizer
    /// * `image` - The image to analyze
    /// * `roi` - Region of Interest to restrict analysis
    ///
    /// # Returns
    /// `Some((rect, detail))` if target found, `None` otherwise
    fn analyze(
        &self,
        context: &Context,
        task_id: common::MaaId,
        node_name: &str,
        custom_recognition_name: &str,
        custom_recognition_param: &str,
        image: &crate::buffer::MaaImageBuffer,
        roi: &common::Rect,
    ) -> Option<(common::Rect, String)>;
}

// === Function Wrapper for Recognition ===

/// Wrapper to use a closure as a custom recognition.
///
/// This allows using a simple closure instead of implementing a full trait.
pub struct FnRecognition<F>
where
    F: Fn(&Context, &RecognitionArgs) -> Option<(common::Rect, String)> + Send + Sync,
{
    func: F,
}

/// Arguments bundle for custom recognition closure.
pub struct RecognitionArgs<'a> {
    pub task_id: common::MaaId,
    pub node_name: &'a str,
    pub name: &'a str,
    pub param: &'a str,
    pub image: &'a crate::buffer::MaaImageBuffer,
    pub roi: &'a common::Rect,
}

impl<F> FnRecognition<F>
where
    F: Fn(&Context, &RecognitionArgs) -> Option<(common::Rect, String)> + Send + Sync,
{
    pub fn new(func: F) -> Self {
        Self { func }
    }
}

impl<F> CustomRecognition for FnRecognition<F>
where
    F: Fn(&Context, &RecognitionArgs) -> Option<(common::Rect, String)> + Send + Sync,
{
    fn analyze(
        &self,
        context: &Context,
        task_id: common::MaaId,
        node_name: &str,
        custom_recognition_name: &str,
        custom_recognition_param: &str,
        image: &crate::buffer::MaaImageBuffer,
        roi: &common::Rect,
    ) -> Option<(common::Rect, String)> {
        let args = RecognitionArgs {
            task_id,
            node_name,
            name: custom_recognition_name,
            param: custom_recognition_param,
            image,
            roi,
        };
        (self.func)(context, &args)
    }
}

// === Function Wrapper for Action ===

/// Wrapper to use a closure as a custom action.
pub struct FnAction<F>
where
    F: Fn(&Context, &ActionArgs) -> bool + Send + Sync,
{
    func: F,
}

/// Arguments bundle for custom action closure.
pub struct ActionArgs<'a> {
    pub task_id: common::MaaId,
    pub node_name: &'a str,
    pub name: &'a str,
    pub param: &'a str,
    pub reco_id: common::MaaId,
    pub box_rect: &'a common::Rect,
}

impl<F> FnAction<F>
where
    F: Fn(&Context, &ActionArgs) -> bool + Send + Sync,
{
    pub fn new(func: F) -> Self {
        Self { func }
    }
}

impl<F> CustomAction for FnAction<F>
where
    F: Fn(&Context, &ActionArgs) -> bool + Send + Sync,
{
    fn run(
        &self,
        context: &Context,
        task_id: common::MaaId,
        node_name: &str,
        custom_action_name: &str,
        custom_action_param: &str,
        reco_id: common::MaaId,
        box_rect: &common::Rect,
    ) -> bool {
        let args = ActionArgs {
            task_id,
            node_name,
            name: custom_action_name,
            param: custom_action_param,
            reco_id,
            box_rect,
        };
        (self.func)(context, &args)
    }
}

// === Trampolines ===

pub(crate) unsafe extern "C" fn custom_action_trampoline(
    context: *mut sys::MaaContext,
    task_id: sys::MaaTaskId,
    node_name: *const std::os::raw::c_char,
    custom_action_name: *const std::os::raw::c_char,
    custom_action_param: *const std::os::raw::c_char,
    reco_id: sys::MaaRecoId,
    box_: *const sys::MaaRect,
    trans_arg: *mut std::os::raw::c_void,
) -> sys::MaaBool {
    if trans_arg.is_null() {
        return 0; // Failure
    }

    let action = unsafe { &*(trans_arg as *mut Box<dyn CustomAction>) };

    let ctx = match unsafe { Context::from_raw(context) } {
        Some(c) => c,
        None => return 0,
    };

    let get_str = |ptr: *const std::os::raw::c_char| -> &str {
        if ptr.is_null() {
            ""
        } else {
            unsafe { CStr::from_ptr(ptr).to_str().unwrap_or("") }
        }
    };

    let node = get_str(node_name);
    let name = get_str(custom_action_name);
    let param = get_str(custom_action_param);

    let rect = if !box_.is_null() {
        crate::buffer::MaaRectBuffer::from_handle(box_ as *mut sys::MaaRect)
            .map(|buf| buf.get())
            .unwrap_or(common::Rect::default())
    } else {
        common::Rect::default()
    };

    let result = std::panic::catch_unwind(std::panic::AssertUnwindSafe(|| {
        action.run(&ctx, task_id, node, name, param, reco_id, &rect)
    }));

    match result {
        Ok(true) => 1,
        Ok(false) => 0,
        Err(_) => {
            eprintln!("MaaFramework Rust Binding: Panic caught in custom action callback");
            0
        }
    }
}

pub(crate) unsafe extern "C" fn custom_recognition_trampoline(
    context: *mut sys::MaaContext,
    task_id: sys::MaaTaskId,
    node_name: *const std::os::raw::c_char,
    custom_recognition_name: *const std::os::raw::c_char,
    custom_recognition_param: *const std::os::raw::c_char,
    image: *const sys::MaaImageBuffer,
    roi: *const sys::MaaRect,
    trans_arg: *mut std::os::raw::c_void,
    out_box: *mut sys::MaaRect,
    out_detail: *mut sys::MaaStringBuffer,
) -> sys::MaaBool {
    if trans_arg.is_null() {
        return 0;
    }
    let reco = unsafe { &*(trans_arg as *mut Box<dyn CustomRecognition>) };

    let ctx = match unsafe { Context::from_raw(context) } {
        Some(c) => c,
        None => return 0,
    };

    let get_str = |ptr: *const std::os::raw::c_char| -> &str {
        if ptr.is_null() {
            ""
        } else {
            unsafe { CStr::from_ptr(ptr).to_str().unwrap_or("") }
        }
    };

    let node = get_str(node_name);
    let name = get_str(custom_recognition_name);
    let param = get_str(custom_recognition_param);

    // Image logic: the pointer passed by C is valid for the duration of the call.
    // We wrap it safely without taking ownership.
    let img_buf = crate::buffer::MaaImageBuffer::from_handle(image as *mut sys::MaaImageBuffer);
    if img_buf.is_none() {
        return 0;
    }
    let img_buf = img_buf.unwrap();

    // Rect logic
    let roi_rect = if !roi.is_null() {
        crate::buffer::MaaRectBuffer::from_handle(roi as *mut sys::MaaRect)
            .map(|buf| buf.get())
            .unwrap_or(common::Rect::default())
    } else {
        common::Rect::default()
    };

    let result = std::panic::catch_unwind(std::panic::AssertUnwindSafe(|| {
        reco.analyze(&ctx, task_id, node, name, param, &img_buf, &roi_rect)
    }));

    match result {
        Ok(Some((res_rect, res_detail))) => {
            if !out_box.is_null() {
                if let Some(mut out_rect_buf) = crate::buffer::MaaRectBuffer::from_handle(out_box) {
                    let _ = out_rect_buf.set(&res_rect);
                }
            }
            if !out_detail.is_null() {
                if let Some(mut out_str_buf) =
                    crate::buffer::MaaStringBuffer::from_handle(out_detail)
                {
                    let _ = out_str_buf.set(&res_detail);
                }
            }
            1
        }
        Ok(None) => 0,
        Err(_) => {
            eprintln!("MaaFramework Rust Binding: Panic caught in custom recognition callback");
            0
        }
    }
}

// === Resource Extension impls ===

impl Resource {
    /// Register a custom action implementation.
    ///
    /// The action will be kept alive as long as it is registered in the Resource.
    ///
    /// The framework rejects an empty name and any name already taken by a custom
    /// recognition or action on this Resource; in that case the previous registration is
    /// kept and `Err(MaaError::InvalidArgument)` is returned. Unregister first to replace.
    pub fn register_custom_action(
        &self,
        name: &str,
        action: Box<dyn CustomAction>,
    ) -> MaaResult<()> {
        let c_name = CString::new(name)?;
        let action_ptr = Box::into_raw(Box::new(action));
        let action_ptr_void = action_ptr as *mut c_void;

        unsafe {
            let ret = sys::MaaResourceRegisterCustomAction(
                self.raw(),
                c_name.as_ptr(),
                Some(custom_action_trampoline),
                action_ptr_void,
            );
            if ret == 0 {
                let _ = Box::from_raw(action_ptr);
                return Err(MaaError::InvalidArgument(format!(
                    "custom action name '{name}' is empty or already registered"
                )));
            }
        }

        let mut map = self.custom_actions().lock().unwrap();
        if let Some(old_ptr) = map.insert(name.to_string(), action_ptr as usize) {
            unsafe {
                let _ = Box::from_raw(old_ptr as *mut Box<dyn CustomAction>);
            }
        }

        Ok(())
    }

    /// Register a custom recognition implementation.
    ///
    /// The recognizer will be kept alive as long as it is registered in the Resource.
    ///
    /// The framework rejects an empty name and any name already taken by a custom
    /// recognition or action on this Resource; in that case the previous registration is
    /// kept and `Err(MaaError::InvalidArgument)` is returned. Unregister first to replace.
    pub fn register_custom_recognition(
        &self,
        name: &str,
        reco: Box<dyn CustomRecognition>,
    ) -> MaaResult<()> {
        let c_name = CString::new(name)?;
        let reco_ptr = Box::into_raw(Box::new(reco));
        let reco_ptr_void = reco_ptr as *mut c_void;

        unsafe {
            let ret = sys::MaaResourceRegisterCustomRecognition(
                self.raw(),
                c_name.as_ptr(),
                Some(custom_recognition_trampoline),
                reco_ptr_void,
            );
            if ret == 0 {
                let _ = Box::from_raw(reco_ptr);
                return Err(MaaError::InvalidArgument(format!(
                    "custom recognition name '{name}' is empty or already registered"
                )));
            }
        }

        let mut map = self.custom_recognitions().lock().unwrap();
        if let Some(old_ptr) = map.insert(name.to_string(), reco_ptr as usize) {
            unsafe {
                let _ = Box::from_raw(old_ptr as *mut Box<dyn CustomRecognition>);
            }
        }

        Ok(())
    }
}