Skip to content

Commit

Permalink
perf: release notificationCallback and customControllerCallbacks Agent
Browse files Browse the repository at this point in the history
  • Loading branch information
dongwlin committed Sep 11, 2024
1 parent 5c935a9 commit 4bd609b
Show file tree
Hide file tree
Showing 6 changed files with 74 additions and 0 deletions.
24 changes: 24 additions & 0 deletions controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,13 @@ type Controller interface {
GetUUID() (string, bool)
}

type controllerStoreValue struct {
NotificationCallbackID uint64
CustomControllerCallbacksID uint64
}

var controllerStore = newStore[controllerStoreValue]()

// controller is a concrete implementation of the Controller interface.
type controller struct {
handle *C.MaaController
Expand Down Expand Up @@ -120,6 +127,9 @@ func NewAdbController(
if handle == nil {
return nil
}
controllerStore.set(unsafe.Pointer(handle), controllerStoreValue{
NotificationCallbackID: id,
})
return &controller{handle: handle}
}

Expand Down Expand Up @@ -168,6 +178,9 @@ func NewWin32Controller(
if handle == nil {
return nil
}
controllerStore.set(unsafe.Pointer(handle), controllerStoreValue{
NotificationCallbackID: id,
})
return &controller{handle: handle}
}

Expand Down Expand Up @@ -213,6 +226,9 @@ func NewDbgController(
if handle == nil {
return nil
}
controllerStore.set(unsafe.Pointer(handle), controllerStoreValue{
NotificationCallbackID: id,
})
return &controller{handle: handle}
}

Expand All @@ -236,11 +252,19 @@ func NewCustomController(
if handle == nil {
return nil
}
controllerStore.set(unsafe.Pointer(handle), controllerStoreValue{
NotificationCallbackID: cbID,
CustomControllerCallbacksID: ctrlID,
})
return &controller{handle: handle}
}

// Destroy frees the controller instance.
func (c *controller) Destroy() {
value := controllerStore.get(c.Handle())
unregisterNotificationCallback(value.NotificationCallbackID)
unregisterCustomControllerCallbacks(value.CustomControllerCallbacksID)
controllerStore.del(c.Handle())
C.MaaControllerDestroy(c.handle)
}

Expand Down
4 changes: 4 additions & 0 deletions custom_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,10 @@ func registerCustomControllerCallbacks(ctrl CustomController) uint64 {
return id
}

func unregisterCustomControllerCallbacks(id uint64) {
delete(customControllerCallbacksAgents, id)
}

// CustomController defines an interface for custom controller.
// Implementers of this interface must embed a CustomControllerHandler struct
// and provide implementations for the following methods:
Expand Down
4 changes: 4 additions & 0 deletions notification.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,10 @@ func registerNotificationCallback(callback func(msg, detailsJson string)) uint64
return id
}

func unregisterNotificationCallback(id uint64) {
delete(notificationCallbackAgents, id)
}

//export _MaaNotificationCallbackAgent
func _MaaNotificationCallbackAgent(msg, detailsJson C.StringView, callbackArg unsafe.Pointer) {
// Here, we are simply passing the uint64 value as a pointer
Expand Down
10 changes: 10 additions & 0 deletions resource.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@ import (
"unsafe"
)

var resourceStore = newStore[uint64]()

type Resource struct {
handle *C.MaaResource
}
Expand All @@ -47,16 +49,24 @@ func NewResource(callback func(msg, detailsJson string)) *Resource {
if handle == nil {
return nil
}
resourceStore.set(unsafe.Pointer(handle), id)
return &Resource{
handle: handle,
}
}

// Destroy frees the resource.
func (r *Resource) Destroy() {
id := resourceStore.get(r.Handle())
unregisterNotificationCallback(id)
resourceStore.del(r.Handle())
C.MaaResourceDestroy(r.handle)
}

func (r *Resource) Handle() unsafe.Pointer {
return unsafe.Pointer(r.handle)
}

// RegisterCustomRecognizer registers a custom recognizer to the resource.
func (r *Resource) RegisterCustomRecognizer(name string, recognizer CustomRecognizer) bool {
id := registerCustomRecognizer(name, recognizer)
Expand Down
26 changes: 26 additions & 0 deletions store.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package maa

import "unsafe"

type store[T any] struct {
data map[uintptr]T
}

func newStore[T any]() *store[T] {
return &store[T]{data: make(map[uintptr]T)}
}

func (s *store[T]) set(handle unsafe.Pointer, value T) {
key := uintptr(handle)
s.data[key] = value
}

func (s *store[T]) get(handle unsafe.Pointer) T {
key := uintptr(handle)
return s.data[key]
}

func (s *store[T]) del(handle unsafe.Pointer) {
key := uintptr(handle)
delete(s.data, key)
}
6 changes: 6 additions & 0 deletions tasker.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ import (
"unsafe"
)

var taskerStore = newStore[uint64]()

type Tasker struct {
handle *C.MaaTasker
}
Expand All @@ -30,11 +32,15 @@ func NewTasker(callback func(msg, detailsJson string)) *Tasker {
if handle == nil {
return nil
}
taskerStore.set(unsafe.Pointer(handle), id)
return &Tasker{handle: handle}
}

// Destroy free the tasker.
func (t *Tasker) Destroy() {
id := taskerStore.get(t.Handle())
unregisterNotificationCallback(id)
taskerStore.del(t.Handle())
C.MaaTaskerDestroy(t.handle)
}

Expand Down

0 comments on commit 4bd609b

Please sign in to comment.