2020-02-12 16:39:57 +00:00
|
|
|
package core
|
2020-02-10 09:23:10 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"reflect"
|
|
|
|
"sync"
|
|
|
|
"time"
|
|
|
|
|
2020-02-12 16:39:57 +00:00
|
|
|
"github.com/ChronosX88/zirconium/core/models"
|
2020-02-10 09:23:10 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
type C2SMessageHandler struct {
|
|
|
|
HandlerFunc func(origin *OriginC2S, message models.BaseMessage)
|
|
|
|
AnonymousAllowed bool
|
|
|
|
}
|
|
|
|
|
|
|
|
type ModuleManager struct {
|
2020-02-12 16:39:57 +00:00
|
|
|
moduleMutex sync.Mutex
|
|
|
|
c2sMessageHandlers map[string][]*C2SMessageHandler
|
|
|
|
coreEventHandlers map[string][]func(sourceModuleName string, event map[string]interface{})
|
2020-02-10 09:23:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func NewModuleManager() (*ModuleManager, error) {
|
|
|
|
var mm = &ModuleManager{
|
2020-02-12 16:39:57 +00:00
|
|
|
c2sMessageHandlers: make(map[string][]*C2SMessageHandler),
|
|
|
|
coreEventHandlers: make(map[string][]func(sourceModuleName string, event map[string]interface{})),
|
2020-02-10 09:23:10 +00:00
|
|
|
}
|
|
|
|
return mm, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (mm *ModuleManager) Hook(messageType string, anonymousAllowed bool, handlerFunc func(origin *OriginC2S, message models.BaseMessage)) {
|
|
|
|
mm.moduleMutex.Lock()
|
|
|
|
mm.c2sMessageHandlers[messageType] = append(mm.c2sMessageHandlers[messageType], &C2SMessageHandler{
|
|
|
|
HandlerFunc: handlerFunc,
|
|
|
|
AnonymousAllowed: anonymousAllowed,
|
|
|
|
})
|
|
|
|
mm.moduleMutex.Unlock()
|
|
|
|
}
|
|
|
|
|
2020-02-12 16:39:57 +00:00
|
|
|
func (mm *ModuleManager) HookcoreEvent(eventName string, handlerFunc func(sourceModuleName string, event map[string]interface{})) {
|
2020-02-10 09:23:10 +00:00
|
|
|
mm.moduleMutex.Lock()
|
2020-02-12 16:39:57 +00:00
|
|
|
mm.coreEventHandlers[eventName] = append(mm.coreEventHandlers[eventName], handlerFunc)
|
2020-02-10 09:23:10 +00:00
|
|
|
mm.moduleMutex.Unlock()
|
|
|
|
}
|
|
|
|
|
|
|
|
func (mm *ModuleManager) Unhook(messageType string, handlerFunc func(origin *OriginC2S, message models.BaseMessage)) {
|
|
|
|
mm.moduleMutex.Lock()
|
|
|
|
defer mm.moduleMutex.Unlock()
|
|
|
|
var handlers = mm.c2sMessageHandlers[messageType]
|
|
|
|
if handlers != nil {
|
|
|
|
for i, v := range handlers {
|
|
|
|
if reflect.ValueOf(v.HandlerFunc).Pointer() == reflect.ValueOf(handlerFunc).Pointer() {
|
|
|
|
handlers[i] = handlers[len(handlers)-1]
|
|
|
|
handlers[len(handlers)-1] = nil
|
|
|
|
handlers = handlers[:len(handlers)-1]
|
|
|
|
mm.c2sMessageHandlers[messageType] = handlers
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-02-12 16:39:57 +00:00
|
|
|
func (mm *ModuleManager) UnhookcoreEvent(eventName string, handlerFunc func(sourceModuleName string, event map[string]interface{})) {
|
2020-02-10 09:23:10 +00:00
|
|
|
mm.moduleMutex.Lock()
|
|
|
|
defer mm.moduleMutex.Unlock()
|
2020-02-12 16:39:57 +00:00
|
|
|
var handlers = mm.coreEventHandlers[eventName]
|
2020-02-10 09:23:10 +00:00
|
|
|
if handlers != nil {
|
|
|
|
for i, v := range handlers {
|
|
|
|
if reflect.ValueOf(v).Pointer() == reflect.ValueOf(handlerFunc).Pointer() {
|
|
|
|
handlers[i] = handlers[len(handlers)-1]
|
|
|
|
handlers[len(handlers)-1] = nil
|
|
|
|
handlers = handlers[:len(handlers)-1]
|
2020-02-12 16:39:57 +00:00
|
|
|
mm.coreEventHandlers[eventName] = handlers
|
2020-02-10 09:23:10 +00:00
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (mm *ModuleManager) FireEvent(sourceModuleName string, eventName string, eventPayload map[string]interface{}) {
|
2020-02-12 16:39:57 +00:00
|
|
|
router.RoutecoreEvent(sourceModuleName, eventName, eventPayload)
|
2020-02-10 09:23:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (mm *ModuleManager) GenerateToken(entityID, deviceID string, tokenExpireTimeDuration time.Duration) (string, error) {
|
|
|
|
token, err := authManager.CreateNewToken(entityID, deviceID, tokenExpireTimeDuration)
|
|
|
|
return token, err
|
|
|
|
}
|