-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathMaaCustomExtensions.cs
More file actions
101 lines (98 loc) · 4.04 KB
/
MaaCustomExtensions.cs
File metadata and controls
101 lines (98 loc) · 4.04 KB
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
using MaaFramework.Binding.Buffers;
using MaaFramework.Binding.Custom;
namespace MaaFramework.Binding.Interop.Native;
/// <summary>
/// A static class providing extension methods for converting <see cref="IMaaCustomResource"/> to custom callback delegate used in interop invoking.
/// </summary>
public static class MaaCustomExtensions
{
/// <summary>
/// Converts a <see cref="IMaaCustomAction"/> to a <see cref="MaaCustomActionCallback"/>.
/// </summary>
/// <param name="resource">The custom action.</param>
/// <param name="callback">The callback.</param>
/// <returns>The callback.</returns>
/// <exception cref="MaaInteroperationException">Failed to query detail.</exception>
public static MaaCustomActionCallback Convert(this IMaaCustomAction resource, out MaaCustomActionCallback callback)
{
callback =
(
MaaContextHandle contextHandle,
MaaTaskId taskId,
string nodeName,
string customActionName,
string customActionParam,
MaaRecoId recoId,
MaaRectHandle boxHandle,
nint transArg
) =>
{
var context = new Binding.MaaContext(contextHandle);
var tasker = context.Tasker;
var taskDetail = TaskDetail.Query(taskId, tasker).ThrowIfNull();
var recognitionDetail = RecognitionDetail.Query<MaaRectBuffer, MaaImageBuffer, MaaImageListBuffer>(recoId, tasker).ThrowIfNull();
return resource.Run
(
context,
new RunArgs
(
TaskDetail: taskDetail,
NodeName: nodeName,
ActionName: customActionName,
ActionParam: customActionParam,
RecognitionDetail: recognitionDetail,
RecognitionBox: new MaaRectBuffer(boxHandle)
),
new RunResults()
);
};
return callback;
}
/// <summary>
/// Converts a <see cref="IMaaCustomRecognition"/> to a <see cref="MaaCustomRecognitionCallback"/>.
/// </summary>
/// <param name="resource">The custom recognition.</param>
/// <param name="callback"></param>
/// <returns></returns>
/// <exception cref="InvalidOperationException">Failed to query detail.</exception>
public static MaaCustomRecognitionCallback Convert(this IMaaCustomRecognition resource, out MaaCustomRecognitionCallback callback)
{
callback =
(
MaaContextHandle contextHandle,
MaaTaskId taskId,
string nodeName,
string customRecognitionName,
string customRecognitionParam,
MaaImageBufferHandle imageHandle,
MaaRectHandle roiHandle,
nint transArg,
MaaRectHandle outBoxHandle,
MaaStringBufferHandle outDetailHandle
) =>
{
var context = new Binding.MaaContext(contextHandle);
var tasker = context.Tasker;
var taskDetail = TaskDetail.Query(taskId, tasker).ThrowIfNull();
return resource.Analyze
(
context,
new AnalyzeArgs
(
TaskDetail: taskDetail,
NodeName: nodeName,
RecognitionName: customRecognitionName,
RecognitionParam: customRecognitionParam,
Image: new MaaImageBuffer(imageHandle),
Roi: new MaaRectBuffer(roiHandle)
),
new AnalyzeResults
(
Box: new MaaRectBuffer(outBoxHandle),
Detail: new MaaStringBuffer(outDetailHandle)
)
);
};
return callback;
}
}