-
Notifications
You must be signed in to change notification settings - Fork 0
/
resource.go
221 lines (188 loc) · 5.88 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
package maa
import (
"unsafe"
"github.com/MaaXYZ/maa-framework-go/internal/buffer"
"github.com/MaaXYZ/maa-framework-go/internal/maa"
"github.com/MaaXYZ/maa-framework-go/internal/store"
)
type resourceStoreValue struct {
NotificationCallbackID uint64
CustomRecognizersCallbackID map[string]uint64
CustomActionsCallbackID map[string]uint64
}
var resourceStore = store.New[resourceStoreValue]()
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
}
resourceStore.Set(handle, resourceStoreValue{
NotificationCallbackID: id,
CustomRecognizersCallbackID: make(map[string]uint64),
CustomActionsCallbackID: make(map[string]uint64),
})
return &Resource{
handle: handle,
}
}
// Destroy frees the resource.
func (r *Resource) Destroy() {
value := resourceStore.Get(r.handle)
unregisterNotificationCallback(value.NotificationCallbackID)
resourceStore.Del(r.handle)
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),
)
}
type InterfaceDevice int32
// InterfaceDevice
const (
InterfaceDeviceCPU InterfaceDevice = -2
InterfaceDeviceAuto InterfaceDevice = -1
InterfaceDeviceGPU0 InterfaceDevice = 0
InterfaceDeviceGPU1 InterfaceDevice = 1
// and more gpu id...
)
func (r *Resource) SetInterfaceDevice(device InterfaceDevice) bool {
return r.setOption(
maa.MaaResOption_InterfaceDevice,
unsafe.Pointer(&device),
unsafe.Sizeof(device),
)
}
// RegisterCustomRecognition registers a custom recognition to the resource.
func (r *Resource) RegisterCustomRecognition(name string, recognition CustomRecognition) bool {
id := registerCustomRecognition(recognition)
value := resourceStore.Get(r.handle)
if oldID, ok := value.CustomRecognizersCallbackID[name]; ok {
unregisterCustomRecognition(oldID)
}
value.CustomRecognizersCallbackID[name] = id
resourceStore.Set(r.handle, value)
got := 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),
)
return got
}
// UnregisterCustomRecognition unregisters a custom recognition from the resource.
func (r *Resource) UnregisterCustomRecognition(name string) bool {
value := resourceStore.Get(r.handle)
if id, ok := value.CustomRecognizersCallbackID[name]; ok {
unregisterCustomRecognition(id)
} else {
return false
}
got := maa.MaaResourceUnregisterCustomRecognition(r.handle, name)
return got
}
// ClearCustomRecognition clears all custom recognitions registered from the resource.
func (r *Resource) ClearCustomRecognition() bool {
value := resourceStore.Get(r.handle)
for _, id := range value.CustomRecognizersCallbackID {
unregisterCustomRecognition(id)
}
got := maa.MaaResourceClearCustomRecognition(r.handle)
return got
}
// RegisterCustomAction registers a custom action to the resource.
func (r *Resource) RegisterCustomAction(name string, action CustomAction) bool {
id := registerCustomAction(action)
value := resourceStore.Get(r.handle)
if oldID, ok := value.CustomActionsCallbackID[name]; ok {
unregisterCustomAction(oldID)
}
value.CustomActionsCallbackID[name] = id
resourceStore.Set(r.handle, value)
got := 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),
)
return got
}
// UnregisterCustomAction unregisters a custom action from the resource.
func (r *Resource) UnregisterCustomAction(name string) bool {
value := resourceStore.Get(r.handle)
if id, ok := value.CustomActionsCallbackID[name]; ok {
unregisterCustomAction(id)
} else {
return false
}
got := maa.MaaResourceUnregisterCustomAction(r.handle, name)
return got
}
// ClearCustomAction clears all custom actions registered from the resource.
func (r *Resource) ClearCustomAction() bool {
value := resourceStore.Get(r.handle)
for _, id := range value.CustomActionsCallbackID {
unregisterCustomAction(id)
}
got := maa.MaaResourceClearCustomAction(r.handle)
return got
}
// PostPath adds a path to the resource loading paths.
// Return id of the resource.
func (r *Resource) PostPath(path string) *Job {
id := maa.MaaResourcePostPath(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
}
// GetTaskList returns the task list of the resource.
func (r *Resource) GetTaskList() ([]string, bool) {
taskList := buffer.NewStringListBuffer()
defer taskList.Destroy()
got := maa.MaaResourceGetTaskList(r.handle, uintptr(taskList.Handle()))
if !got {
return []string{}, false
}
taskListArr := taskList.GetAll()
return taskListArr, true
}