-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathresource.go
274 lines (226 loc) · 7.3 KB
/
resource.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
272
273
274
package maa
import (
"sync"
"unsafe"
"github.com/MaaXYZ/maa-framework-go/v2/internal/buffer"
"github.com/MaaXYZ/maa-framework-go/v2/internal/maa"
"github.com/MaaXYZ/maa-framework-go/v2/internal/store"
)
type resourceStoreValue struct {
NotificationCallbackID uint64
CustomRecognizersCallbackID map[string]uint64
CustomActionsCallbackID map[string]uint64
}
var (
resourceStore = store.New[resourceStoreValue]()
resourceStoreMutex sync.RWMutex
)
type Resource struct {
handle uintptr
}
// NewResource creates a new resource.
func NewResource(notify Notification) *Resource {
id := registerNotificationCallback(notify)
handle := maa.MaaResourceCreate(
_MaaNotificationCallbackAgent,
// Here, we are simply passing the uint64 value as a pointer
// and will not actually dereference this pointer.
uintptr(id),
)
if handle == 0 {
return nil
}
resourceStoreMutex.Lock()
resourceStore.Set(handle, resourceStoreValue{
NotificationCallbackID: id,
CustomRecognizersCallbackID: make(map[string]uint64),
CustomActionsCallbackID: make(map[string]uint64),
})
resourceStoreMutex.Unlock()
return &Resource{
handle: handle,
}
}
// Destroy frees the resource.
func (r *Resource) Destroy() {
resourceStoreMutex.Lock()
value := resourceStore.Get(r.handle)
unregisterNotificationCallback(value.NotificationCallbackID)
resourceStore.Del(r.handle)
resourceStoreMutex.Unlock()
maa.MaaResourceDestroy(r.handle)
}
func (r *Resource) setOption(key maa.MaaResOption, value unsafe.Pointer, valSize uintptr) bool {
return maa.MaaResourceSetOption(
r.handle,
key,
value,
uint64(valSize),
)
}
func (r *Resource) setInferenceDevice(device maa.MaaInferenceDevice) bool {
return r.setOption(
maa.MaaResOption_InferenceDevice,
unsafe.Pointer(&device),
unsafe.Sizeof(device),
)
}
func (r *Resource) setInferenceExecutionProvider(ep maa.MaaInferenceExecutionProvider) bool {
return r.setOption(
maa.MaaResOption_InferenceExecutionProvider,
unsafe.Pointer(&ep),
unsafe.Sizeof(ep),
)
}
func (r *Resource) setInference(ep maa.MaaInferenceExecutionProvider, deviceID maa.MaaInferenceDevice) bool {
return r.setInferenceExecutionProvider(ep) && r.setInferenceDevice(deviceID)
}
// UseCPU
func (r *Resource) UseCPU() bool {
return r.setInference(maa.MaaInferenceExecutionProvider_CPU, maa.MaaInferenceDevice_CPU)
}
type InterenceDevice = maa.MaaInferenceDevice
const (
InterenceDeviceAuto int32 = -1
InferenceDevice0 int32 = 0
InferenceDevice1 int32 = 1
// and more gpu id or flag...
)
// UseDirectml
func (r *Resource) UseDirectml(deviceID InterenceDevice) bool {
return r.setInference(maa.MaaInferenceExecutionProvider_DirectML, deviceID)
}
// UseCoreml
func (r *Resource) UseCoreml(coremlFlag InterenceDevice) bool {
return r.setInference(maa.MaaInferenceExecutionProvider_CoreML, coremlFlag)
}
// UseAutoExecutionProvider
func (r *Resource) UseAutoExecutionProvider() bool {
return r.setInference(maa.MaaInferenceExecutionProvider_Auto, maa.MaaInferenceDevice_Auto)
}
// RegisterCustomRecognition registers a custom recognition to the resource.
func (r *Resource) RegisterCustomRecognition(name string, recognition CustomRecognition) bool {
id := registerCustomRecognition(recognition)
resourceStoreMutex.Lock()
value := resourceStore.Get(r.handle)
if oldID, ok := value.CustomRecognizersCallbackID[name]; ok {
unregisterCustomRecognition(oldID)
}
value.CustomRecognizersCallbackID[name] = id
resourceStore.Set(r.handle, value)
resourceStoreMutex.Unlock()
return maa.MaaResourceRegisterCustomRecognition(
r.handle,
name,
_MaaCustomRecognitionCallbackAgent,
// Here, we are simply passing the uint64 value as a pointer
// and will not actually dereference this pointer.
uintptr(id),
)
}
// UnregisterCustomRecognition unregisters a custom recognition from the resource.
func (r *Resource) UnregisterCustomRecognition(name string) bool {
resourceStoreMutex.Lock()
defer resourceStoreMutex.Unlock()
value := resourceStore.Get(r.handle)
if id, ok := value.CustomRecognizersCallbackID[name]; ok {
unregisterCustomRecognition(id)
} else {
return false
}
return maa.MaaResourceUnregisterCustomRecognition(r.handle, name)
}
// ClearCustomRecognition clears all custom recognitions registered from the resource.
func (r *Resource) ClearCustomRecognition() bool {
resourceStoreMutex.Lock()
defer resourceStoreMutex.Unlock()
value := resourceStore.Get(r.handle)
for _, id := range value.CustomRecognizersCallbackID {
unregisterCustomRecognition(id)
}
return maa.MaaResourceClearCustomRecognition(r.handle)
}
// RegisterCustomAction registers a custom action to the resource.
func (r *Resource) RegisterCustomAction(name string, action CustomAction) bool {
id := registerCustomAction(action)
resourceStoreMutex.Lock()
value := resourceStore.Get(r.handle)
if oldID, ok := value.CustomActionsCallbackID[name]; ok {
unregisterCustomAction(oldID)
}
value.CustomActionsCallbackID[name] = id
resourceStore.Set(r.handle, value)
resourceStoreMutex.Unlock()
return maa.MaaResourceRegisterCustomAction(
r.handle,
name,
_MaaCustomActionCallbackAgent,
// Here, we are simply passing the uint64 value as a pointer
// and will not actually dereference this pointer.
uintptr(id),
)
}
// UnregisterCustomAction unregisters a custom action from the resource.
func (r *Resource) UnregisterCustomAction(name string) bool {
resourceStoreMutex.Lock()
defer resourceStoreMutex.Unlock()
value := resourceStore.Get(r.handle)
if id, ok := value.CustomActionsCallbackID[name]; ok {
unregisterCustomAction(id)
} else {
return false
}
return maa.MaaResourceUnregisterCustomAction(r.handle, name)
}
// ClearCustomAction clears all custom actions registered from the resource.
func (r *Resource) ClearCustomAction() bool {
resourceStoreMutex.Lock()
defer resourceStoreMutex.Unlock()
value := resourceStore.Get(r.handle)
for _, id := range value.CustomActionsCallbackID {
unregisterCustomAction(id)
}
return maa.MaaResourceClearCustomAction(r.handle)
}
// PostBundle adds a path to the resource loading paths.
// Return id of the resource.
func (r *Resource) PostBundle(path string) *Job {
id := maa.MaaResourcePostBundle(r.handle, path)
return NewJob(id, r.status, r.wait)
}
// Clear clears the resource loading paths.
func (r *Resource) Clear() bool {
return maa.MaaResourceClear(r.handle)
}
// status returns the loading status of a resource identified by id.
func (r *Resource) status(resId int64) Status {
return Status(maa.MaaResourceStatus(r.handle, resId))
}
func (r *Resource) wait(resId int64) Status {
return Status(maa.MaaResourceWait(r.handle, resId))
}
// Loaded checks if resources are loaded.
func (r *Resource) Loaded() bool {
return maa.MaaResourceLoaded(r.handle)
}
// GetHash returns the hash of the resource.
func (r *Resource) GetHash() (string, bool) {
hash := buffer.NewStringBuffer()
defer hash.Destroy()
got := maa.MaaResourceGetHash(r.handle, uintptr(hash.Handle()))
if !got {
return "", false
}
return hash.Get(), true
}
// GetNodeList returns the node list of the resource.
func (r *Resource) GetNodeList() ([]string, bool) {
taskList := buffer.NewStringListBuffer()
defer taskList.Destroy()
got := maa.MaaResourceGetNodeList(r.handle, uintptr(taskList.Handle()))
if !got {
return []string{}, false
}
taskListArr := taskList.GetAll()
return taskListArr, true
}