-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcustom_recognition.go
102 lines (86 loc) · 2.62 KB
/
custom_recognition.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
package maa
import (
"image"
"sync"
"sync/atomic"
"github.com/MaaXYZ/maa-framework-go/internal/buffer"
)
var (
customRecognitionCallbackID uint64
customRecognitionCallbackAgents = make(map[uint64]CustomRecognition)
customRecognitionCallbackAgentsMutex sync.RWMutex
)
func registerCustomRecognition(recognizer CustomRecognition) uint64 {
id := atomic.AddUint64(&customRecognitionCallbackID, 1)
customRecognitionCallbackAgentsMutex.Lock()
customRecognitionCallbackAgents[id] = recognizer
customRecognitionCallbackAgentsMutex.Unlock()
return id
}
func unregisterCustomRecognition(id uint64) bool {
customRecognitionCallbackAgentsMutex.Lock()
defer customRecognitionCallbackAgentsMutex.Unlock()
if _, ok := customRecognitionCallbackAgents[id]; !ok {
return false
}
delete(customRecognitionCallbackAgents, id)
return true
}
type CustomRecognitionArg struct {
TaskDetail *TaskDetail
CurrentTaskName string
CustomRecognitionName string
CustomRecognitionParam string
Img image.Image
Roi Rect
}
type CustomRecognition interface {
Run(ctx *Context, arg *CustomRecognitionArg) (*CustomRecognitionResult, bool)
}
type CustomRecognitionResult struct {
Box Rect
Detail string
}
func _MaaCustomRecognitionCallbackAgent(
context uintptr,
taskId int64,
currentTaskName, customRecognitionName, customRecognitionParam *byte,
image, roi uintptr,
transArg uintptr,
outBox, outDetail uintptr,
) uint64 {
// Here, we are simply passing the uint64 value as a pointer
// and will not actually dereference this pointer.
id := uint64(transArg)
customRecognitionCallbackAgentsMutex.RLock()
recognition, exists := customRecognitionCallbackAgents[id]
customRecognitionCallbackAgentsMutex.RUnlock()
if !exists || recognition == nil {
return 0
}
ctx := Context{handle: context}
tasker := ctx.GetTasker()
taskDetail := tasker.getTaskDetail(taskId)
imgBuffer := buffer.NewImageBufferByHandle(image)
imgImg := imgBuffer.Get()
ret, ok := recognition.Run(
&Context{handle: context},
&CustomRecognitionArg{
TaskDetail: taskDetail,
CurrentTaskName: bytePtrToString(currentTaskName),
CustomRecognitionName: bytePtrToString(customRecognitionName),
CustomRecognitionParam: bytePtrToString(customRecognitionParam),
Img: imgImg,
Roi: buffer.NewRectBufferByHandle(roi).Get(),
},
)
if ok {
box := ret.Box
outBoxRect := buffer.NewRectBufferByHandle(outBox)
outBoxRect.Set(box)
outDetailString := buffer.NewStringBufferByHandle(outDetail)
outDetailString.Set(ret.Detail)
return 1
}
return 0
}