-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcontext.go
179 lines (161 loc) · 5.1 KB
/
context.go
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
package maa
import (
"image"
"github.com/MaaXYZ/maa-framework-go/v2/internal/buffer"
"github.com/MaaXYZ/maa-framework-go/v2/internal/maa"
)
type Context struct {
handle uintptr
}
func (ctx *Context) handleOverride(override ...any) string {
if len(override) == 0 {
return "{}"
}
if str, ok := override[0].(string); ok {
return str
}
str, err := toJSON(override[0])
if err != nil {
str = "{}"
}
return str
}
func (ctx *Context) runTask(entry, override string) *TaskDetail {
taskId := maa.MaaContextRunTask(ctx.handle, entry, override)
if taskId == 0 {
return nil
}
tasker := ctx.GetTasker()
return tasker.getTaskDetail(taskId)
}
// RunTask runs a task and returns its detail.
// It accepts an entry string and an optional override parameter which can be
// a JSON string or any data type that can be marshaled to JSON.
// If multiple overrides are provided, only the first one will be used.
//
// Example 1:
//
// ctx.RunTask("Task", `{"Task":{"action":"Click","target":[100, 200, 100, 100]}}`)
//
// Example 2:
//
// ctx.RunTask("Task", map[string]interface{}{
// "Task": map[string]interface{}{
// "action": "Click",
// "target": []int{100, 200, 100, 100},
// }
// })
func (ctx *Context) RunTask(entry string, override ...any) *TaskDetail {
return ctx.runTask(entry, ctx.handleOverride(override...))
}
func (ctx *Context) runRecognition(entry, override string, img image.Image) *RecognitionDetail {
imgBuf := buffer.NewImageBuffer()
imgBuf.Set(img)
defer imgBuf.Destroy()
recId := maa.MaaContextRunRecognition(ctx.handle, entry, override, uintptr(imgBuf.Handle()))
if recId == 0 {
return nil
}
tasker := ctx.GetTasker()
return tasker.getRecognitionDetail(recId)
}
// RunRecognition run a recognition and return its detail.
// It accepts an entry string and an optional override parameter which can be
// a JSON string or any data type that can be marshaled to JSON.
// If multiple overrides are provided, only the first one will be used.
//
// Example 1:
//
// ctx.RunRecognition("Task", `{"Task":{"recognition":"OCR","expected":"Hello"}}`)
//
// Example 2:
//
// ctx.RunRecognition("Task", map[string]interface{}{
// "Task": map[string]interface{}{
// "recognition": "OCR",
// "expected": "Hello",
// }
// })
func (ctx *Context) RunRecognition(entry string, img image.Image, override ...any) *RecognitionDetail {
return ctx.runRecognition(entry, ctx.handleOverride(override...), img)
}
func (ctx *Context) runAction(entry, override string, box Rect, recognitionDetail string) *NodeDetail {
rectBuf := buffer.NewRectBuffer()
rectBuf.Set(box)
defer rectBuf.Destroy()
nodeId := maa.MaaContextRunAction(ctx.handle, entry, override, uintptr(rectBuf.Handle()), recognitionDetail)
if nodeId == 0 {
return nil
}
tasker := ctx.GetTasker()
return tasker.getNodeDetail(nodeId)
}
// RunAction run an action and return its detail.
// It accepts an entry string and an optional override parameter which can be
// a JSON string or any data type that can be marshaled to JSON.
// If multiple overrides are provided, only the first one will be used.
//
// Example 1:
//
// ctx.RunAction("Task", `{"Task":{"action":"Click","target":[100, 200, 100, 100]}}`)
//
// Example 2:
//
// ctx.RunAction("Task", map[string]interface{}{
// "Task": map[string]interface{}{
// "action": "Click",
// "target": []int{100, 200, 100, 100},
// }
// })
func (ctx *Context) RunAction(entry string, box Rect, recognitionDetail string, override ...any) *NodeDetail {
return ctx.runAction(entry, ctx.handleOverride(override...), box, recognitionDetail)
}
func (ctx *Context) overridePipeline(override string) bool {
return maa.MaaContextOverridePipeline(ctx.handle, override)
}
// OverridePipeline overrides pipeline.
// The `override` parameter can be a JSON string or any data type that can be marshaled to JSON.
func (ctx *Context) OverridePipeline(override any) bool {
if str, ok := override.(string); ok {
return ctx.overridePipeline(str)
}
str, err := toJSON(override)
if err != nil {
return false
}
return ctx.overridePipeline(str)
}
// OverrideNext overrides the next list of task by name.
func (ctx *Context) OverrideNext(name string, nextList []string) bool {
list := buffer.NewStringListBuffer()
defer list.Destroy()
size := len(nextList)
items := make([]*buffer.StringBuffer, size)
for i := 0; i < size; i++ {
items[i] = buffer.NewStringBuffer()
items[i].Set(nextList[i])
list.Append(items[i])
}
defer func() {
for _, item := range items {
item.Destroy()
}
}()
return maa.MaaContextOverrideNext(ctx.handle, name, uintptr(list.Handle()))
}
// GetTaskJob returns current task job.
func (ctx *Context) GetTaskJob() *TaskJob {
tasker := ctx.GetTasker()
taskId := maa.MaaContextGetTaskId(ctx.handle)
return NewTaskJob(taskId, tasker.status, tasker.wait, tasker.getTaskDetail)
}
// GetTasker return current Tasker.
func (ctx *Context) GetTasker() *Tasker {
handle := maa.MaaContextGetTasker(ctx.handle)
return &Tasker{handle: handle}
}
// Clone clones current Context.
func (ctx *Context) Clone() *Context {
handle := maa.MaaContextClone(ctx.handle)
return &Context{handle: handle}
}