-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtoolkit.go
173 lines (154 loc) · 5.24 KB
/
toolkit.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
package maa
import (
"unsafe"
"github.com/MaaXYZ/maa-framework-go/v2/internal/maa"
)
// AdbDevice represents a single ADB device with various properties about its information.
type AdbDevice struct {
Name string
AdbPath string
Address string
ScreencapMethod AdbScreencapMethod
InputMethod AdbInputMethod
Config string
}
// DesktopWindow represents a single desktop window with various properties about its information.
type DesktopWindow struct {
Handle unsafe.Pointer
ClassName string
WindowName string
}
type Toolkit struct{}
// NewToolkit creates a new toolkit instance.
func NewToolkit() *Toolkit {
return &Toolkit{}
}
// ConfigInitOption inits the toolkit config option.
func (t *Toolkit) ConfigInitOption(userPath, defaultJson string) bool {
return maa.MaaToolkitConfigInitOption(userPath, defaultJson)
}
// FindAdbDevices finds adb devices.
func (t *Toolkit) FindAdbDevices(specifiedAdb ...string) []*AdbDevice {
listHandle := maa.MaaToolkitAdbDeviceListCreate()
defer maa.MaaToolkitAdbDeviceListDestroy(listHandle)
var got bool
if len(specifiedAdb) > 0 {
got = maa.MaaToolkitAdbDeviceFindSpecified(specifiedAdb[0], listHandle)
} else {
got = maa.MaaToolkitAdbDeviceFind(listHandle)
}
if !got {
return nil
}
size := maa.MaaToolkitAdbDeviceListSize(listHandle)
list := make([]*AdbDevice, size)
for i := uint64(0); i < size; i++ {
deviceHandle := maa.MaaToolkitAdbDeviceListAt(listHandle, i)
name := maa.MaaToolkitAdbDeviceGetName(deviceHandle)
adbPath := maa.MaaToolkitAdbDeviceGetAdbPath(deviceHandle)
address := maa.MaaToolkitAdbDeviceGetAddress(deviceHandle)
screencapMethod := AdbScreencapMethod(maa.MaaToolkitAdbDeviceGetScreencapMethods(deviceHandle))
inputMethod := AdbInputMethod(maa.MaaToolkitAdbDeviceGetInputMethods(deviceHandle))
config := maa.MaaToolkitAdbDeviceGetConfig(deviceHandle)
list[i] = &AdbDevice{
Name: name,
AdbPath: adbPath,
Address: address,
ScreencapMethod: screencapMethod,
InputMethod: inputMethod,
Config: config,
}
}
return list
}
// FindDesktopWindows finds desktop windows.
func (t *Toolkit) FindDesktopWindows() []*DesktopWindow {
listHandle := maa.MaaToolkitDesktopWindowListCreate()
defer maa.MaaToolkitDesktopWindowListDestroy(listHandle)
got := maa.MaaToolkitDesktopWindowFindAll(listHandle)
if !got {
return nil
}
size := maa.MaaToolkitDesktopWindowListSize(listHandle)
list := make([]*DesktopWindow, size)
for i := uint64(0); i < size; i++ {
windowHandle := maa.MaaToolkitDesktopWindowListAt(listHandle, i)
handle := maa.MaaToolkitDesktopWindowGetHandle(windowHandle)
className := maa.MaaToolkitDesktopWindowGetClassName(windowHandle)
windowName := maa.MaaToolkitDesktopWindowGetWindowName(windowHandle)
list[i] = &DesktopWindow{
Handle: handle,
ClassName: className,
WindowName: windowName,
}
}
return list
}
type piStoreValue struct {
CustomRecognizersCallbackID map[string]uint64
CustomActionsCallbackID map[string]uint64
}
var piStore = make(map[uint64]piStoreValue)
// RegisterPICustomRecognition registers a custom recognizer.
func (t *Toolkit) RegisterPICustomRecognition(instId uint64, name string, recognition CustomRecognition) {
id := registerCustomRecognition(recognition)
if _, ok := piStore[instId]; !ok {
piStore[instId] = piStoreValue{
CustomRecognizersCallbackID: make(map[string]uint64),
CustomActionsCallbackID: make(map[string]uint64),
}
}
piStore[instId].CustomRecognizersCallbackID[name] = id
maa.MaaToolkitProjectInterfaceRegisterCustomRecognition(
instId,
name,
_MaaCustomRecognitionCallbackAgent,
// Here, we are simply passing the uint64 value as a pointer
// and will not actually dereference this pointer.
unsafe.Pointer(uintptr(id)),
)
}
// RegisterPICustomAction registers a custom action.
func (t *Toolkit) RegisterPICustomAction(instId uint64, name string, action CustomAction) {
id := registerCustomAction(action)
if _, ok := piStore[instId]; !ok {
piStore[instId] = piStoreValue{
CustomRecognizersCallbackID: make(map[string]uint64),
CustomActionsCallbackID: make(map[string]uint64),
}
}
piStore[instId].CustomActionsCallbackID[name] = id
maa.MaaToolkitProjectInterfaceRegisterCustomAction(
instId,
name,
_MaaCustomActionCallbackAgent,
// Here, we are simply passing the uint64 value as a pointer
// and will not actually dereference this pointer.
unsafe.Pointer(uintptr(id)),
)
}
// ClearPICustom unregisters all custom recognitions and actions for a given instance.
func (t *Toolkit) ClearPICustom(instId uint64) {
value := piStore[instId]
for _, id := range value.CustomRecognizersCallbackID {
unregisterCustomRecognition(id)
}
for _, id := range value.CustomActionsCallbackID {
unregisterCustomAction(id)
}
}
// RunCli runs the PI CLI.
func (t *Toolkit) RunCli(instId uint64, resourcePath, userPath string, directly bool, notify Notification) bool {
id := registerNotificationCallback(notify)
got := maa.MaaToolkitProjectInterfaceRunCli(
instId,
resourcePath,
userPath,
directly,
_MaaNotificationCallbackAgent,
// Here, we are simply passing the uint64 value as a pointer
// and will not actually dereference this pointer.
unsafe.Pointer(uintptr(id)),
)
return got
}