Skip to content

Commit

Permalink
feat: custom priority
Browse files Browse the repository at this point in the history
  • Loading branch information
fumiama committed Sep 3, 2023
1 parent c9f5a4f commit adc600c
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 3 deletions.
1 change: 0 additions & 1 deletion control/engine.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,6 @@ func newengine(service string, prio int, o *ctrl.Options[*zero.Ctx]) (e *Engine)
panic(fmt.Sprint("prio", prio, "is used by", s))
}
priomap[prio] = service
logrus.Debugln("[control]插件", service, "已设置优先级", prio)
e.en = zero.New()
e.en.UsePreHandler(
func(ctx *zero.Ctx) bool {
Expand Down
22 changes: 20 additions & 2 deletions control/register.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,34 @@ import (
"sync/atomic"

ctrl "github.com/FloatTech/zbpctrl"
"github.com/sirupsen/logrus"
zero "github.com/wdvxdr1123/ZeroBot"
)

var (
enmap = make(map[string]*Engine)
prio uint64
enmap = make(map[string]*Engine)
prio uint64
custpriomap map[string]uint64
)

// LoadCustomPriority 加载自定义优先级 map,适配 1.21 及以上版本
func LoadCustomPriority(m map[string]uint64) {
if custpriomap != nil {
panic("double-defined custpriomap")
}
custpriomap = m
prio = uint64(len(custpriomap)+1) * 10
}

// Register 注册插件控制器
func Register(service string, o *ctrl.Options[*zero.Ctx]) *Engine {
if custpriomap != nil {
logrus.Debugln("[control]插件", service, "已设置自定义优先级", prio)
engine := newengine(service, int(custpriomap[service]), o)
enmap[service] = engine
return engine
}
logrus.Debugln("[control]插件", service, "已自动设置优先级", prio)
engine := newengine(service, int(atomic.AddUint64(&prio, 10)), o)
enmap[service] = engine
return engine
Expand Down

0 comments on commit adc600c

Please sign in to comment.