forked from MaaAssistantArknights/MaaAssistantArknights
-
Notifications
You must be signed in to change notification settings - Fork 1
/
maa.go
271 lines (243 loc) · 5.38 KB
/
maa.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
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
package maa
import (
"MaaAssistantArknights/conf"
"errors"
"fmt"
"os"
"sync"
"syscall"
log "github.com/sirupsen/logrus"
)
var (
ErrorMAAFalse = errors.New("MAA return false")
ErrorAsstCreateEx = errors.New("AsstCreateEx failed")
ErrorAsstLoadSource = errors.New("AsstLoadSource failed")
ErrorAsstConnect = errors.New("AsstConnect failed")
ErrorAsstCreate = errors.New("AsstCreate failed")
ErrorAsstGetVersion = errors.New("AsstGetVersion failed")
ErrorAsstAppendTask = errors.New("AsstAppendTask failed")
ErrorAsstSetTaskParams = errors.New("AsstSetTaskParams failed")
ErrorAsstStart = errors.New("AsstStart failed")
ErrorAsstStop = errors.New("AsstStop failed")
ErrorAsstRunning = errors.New("AsstRunning failed")
ErrorAsstDestroy = errors.New("AsstDestroy failed")
)
type AsstCallback func(message int, details uintptr, arg uintptr) uintptr
type Maa struct {
asstHandle uintptr
version string
hooks []Hook
mu sync.Mutex
cfg *conf.Asst
}
func New(cfg *conf.Asst) (maa *Maa, err error) {
if len(cfg.ResourcePath) == 0 {
cfg.ResourcePath, err = os.Getwd()
if err != nil {
return
}
}
maa = &Maa{
hooks: make([]Hook, 0),
cfg: cfg,
}
err = maa.initasst()
if err != nil {
return
}
maa.setDefaultHook()
return
}
func (m *Maa) initasst() (err error) {
cfg := m.cfg
err = newSyscall(cfg.ResourcePath)
if err != nil {
return
}
err = m.asstLoadResource(cfg.ResourcePath)
if err != nil {
return
}
err = m.AsstCreateEx()
if err != nil {
return
}
err = m.AsstConnect(cfg.ADBPath, cfg.ADBAddress, cfg.ADBType)
if err != nil {
return
}
m.SetVersion()
return
}
func (m *Maa) callback(message int, details uintptr, arg uintptr) uintptr {
m.mu.Lock()
hooks := m.hooks
m.mu.Unlock()
for _, hook := range hooks {
hook.Do(message, CharPtr2String(details), arg)
}
return 0
}
func (m *Maa) AppendHook(hooks ...Hook) {
if len(hooks) == 0 {
return
}
m.mu.Lock()
defer m.mu.Unlock()
m.hooks = append(m.hooks, hooks...)
}
func (m *Maa) asstLoadResource(path string) error {
a1, err := UintPtrFromString(path)
if err != nil {
return err
}
r1, _, err := asstLoadResource(a1)
log.WithFields(log.Fields{
"path": path,
"r1": r1,
"err": err,
}).Println("asstLoadResource")
if !errors.Is(err, syscall.Errno(0)) {
return ErrorAsstLoadSource
}
if r1 == 0 {
return ErrorMAAFalse
}
return nil
}
func (m *Maa) AsstCreateEx() error {
a1 := syscall.NewCallback(m.callback)
a2 := uintptr(0) // 这里的arg之后看有没有需要再更新
r1, _, err := asstCreateEx(a1, a2)
log.WithFields(log.Fields{
"r1": r1,
"err": err,
}).Println("AsstCreateEx")
if !errors.Is(err, syscall.Errno(0)) {
return ErrorAsstCreateEx
}
m.asstHandle = r1
return nil
}
func (m *Maa) AsstCreate() error {
r1, _, err := asstCreate()
log.WithFields(log.Fields{
"r1": r1,
"err": err,
}).Println("AsstCreate")
if !errors.Is(err, syscall.Errno(0)) {
return ErrorAsstCreate
}
m.asstHandle = r1
return nil
}
func (m *Maa) AsstConnect(path string, address string, config string) error {
fmt.Println("adb start connect")
a1, err := UintPtrFromString(path)
if err != nil {
return err
}
a2, err := UintPtrFromString(address)
if err != nil {
return err
}
a3, err := UintPtrFromString(config)
if err != nil {
return err
}
r1, _, err := asstConnect(m.asstHandle, a1, a2, a3)
log.WithFields(log.Fields{
"r1": r1,
"err": err,
}).Println("AsstConnect")
if r1 == 0 {
return ErrorAsstConnect
}
return nil
}
func (m *Maa) setDefaultHook() {
m.hooks = append(m.hooks, &sampleHook{})
}
func (m *Maa) SetVersion() {
r1, _, err := asstGetVersion(m.asstHandle)
log.WithFields(log.Fields{
"r1": r1,
"err": err,
}).Println("SetVersion")
if !errors.Is(err, syscall.Errno(0)) {
return
}
if r1 > 0 {
m.version = CharPtr2String(r1)
}
}
func (m *Maa) GetVersion() string {
return m.version
}
func (m *Maa) AsstAppendTask(typ TaskType, task Tasker) (TaskID, error) {
a1, err := UintPtrFromString(typ.String())
if err != nil {
return 0, err
}
a2, err := UintPtrFromString(task.Serialize())
if err != nil {
return 0, err
}
r1, _, err := asstAppendTask(m.asstHandle, a1, a2)
log.WithFields(log.Fields{
"r1": r1,
"err": err,
}).Println("asstAppendTask")
if r1 == 0 {
return 0, ErrorAsstAppendTask
}
return TaskID(r1), nil
}
func (m *Maa) AsstSetTaskParams(taskID TaskID, task Tasker) bool {
a1 := uintptr(taskID)
a2, err := UintPtrFromString(task.Serialize())
if err != nil {
return false
}
r1, _, err := asstSetTaskParams(m.asstHandle, a1, a2)
log.WithFields(log.Fields{
"r1": r1,
"err": err,
}).Println("AsstSetTaskParams")
return r1 > 0
}
func (m *Maa) AsstStart() bool {
r1, _, err := asstStart(m.asstHandle)
log.WithFields(log.Fields{
"r1": r1,
"err": err,
}).Println("AsstStart")
return r1 > 0
}
func (m *Maa) AsstStop() bool {
r1, _, err := asstStop(m.asstHandle)
log.WithFields(log.Fields{
"r1": r1,
"err": err,
}).Println("AsstStop")
return r1 > 0
}
func (m *Maa) AsstRunning() bool {
r1, _, err := asstRunning(m.asstHandle)
log.WithFields(log.Fields{
"r1": r1,
"err": err,
}).Println("AsstRunning")
return r1 > 0
}
func (m *Maa) AsstDestroy() error {
r1, _, err := asstDestroy(m.asstHandle)
log.WithFields(log.Fields{
"r1": r1,
"err": err,
}).Println("AsstDestroy")
if r1 == 0 {
return ErrorAsstDestroy
}
return nil
}