mirror of
https://github.com/FloatTech/ZeroBot-Plugin.git
synced 2025-12-19 22:00:11 +08:00
✨ dyloader 缩小插件体积
This commit is contained in:
parent
ec0032c5ab
commit
75d0671d9b
@ -40,11 +40,8 @@ zerobot -h -t token -u url [-d|w] [-g] qq1 qq2 qq3 ...
|
||||
- [x] /加载插件 service名
|
||||
- [x] /卸载插件 service名
|
||||
- 仅 Linux, FreeBSD, macOS 可用,默认注释不开启。
|
||||
- 每个插件的`.so`文件约 4 ~ 20 M ,如非必要建议不开启。
|
||||
- 动态加载的插件需放置在`plugins/`下,需命名为`service名.so`,编译命令如下。模版详见[ZeroBot-Hook](https://github.com/fumiama/ZeroBot-Hook)。
|
||||
```bash
|
||||
go build -ldflags "-s -w" -buildmode=plugin
|
||||
```
|
||||
- 开启后主可执行文件大约增加 2M ,每个插件的`.so`文件约为 2 ~ 10 M ,如非必要建议不开启。
|
||||
- 动态加载的插件需放置在`plugins/`下,需命名为`service名.so`,编译模版详见[ZeroBot-Hook](https://github.com/fumiama/ZeroBot-Hook)。
|
||||
- **插件控制**
|
||||
- [x] /启用 xxx
|
||||
- [x] /禁用 xxx
|
||||
|
||||
16
dyloader/hook.go
Normal file
16
dyloader/hook.go
Normal file
@ -0,0 +1,16 @@
|
||||
package dyloader
|
||||
|
||||
import zero "github.com/wdvxdr1123/ZeroBot"
|
||||
|
||||
func sendGroupMessage(ctx *zero.Ctx, groupID int64, message interface{}) int64 {
|
||||
return ctx.SendGroupMessage(groupID, message)
|
||||
}
|
||||
func sendPrivateMessage(ctx *zero.Ctx, userID int64, message interface{}) int64 {
|
||||
return ctx.SendPrivateMessage(userID, message)
|
||||
}
|
||||
func getMessage(ctx *zero.Ctx, messageID int64) zero.Message {
|
||||
return ctx.GetMessage(messageID)
|
||||
}
|
||||
func parse(ctx *zero.Ctx, model interface{}) (err error) {
|
||||
return ctx.Parse(model)
|
||||
}
|
||||
@ -23,11 +23,14 @@ var visited bool
|
||||
|
||||
//go:linkname matcherList github.com/wdvxdr1123/ZeroBot.matcherList
|
||||
//go:linkname matcherLock github.com/wdvxdr1123/ZeroBot.matcherLock
|
||||
//go:linkname defaultEngine github.com/wdvxdr1123/ZeroBot.defaultEngine
|
||||
var (
|
||||
// 所有主匹配器列表
|
||||
// matcherList 所有主匹配器列表
|
||||
matcherList []*zero.Matcher
|
||||
// Matcher 修改读写锁
|
||||
matcherLock sync.RWMutex
|
||||
// defaultEngine zero 的默认 engine
|
||||
defaultEngine *zero.Engine
|
||||
)
|
||||
|
||||
var (
|
||||
@ -113,16 +116,37 @@ func open(path, target string) error {
|
||||
pluginsMu.Unlock()
|
||||
if !ok {
|
||||
p, err := plugin.Open(path)
|
||||
var initfunc, hookfunc plugin.Symbol
|
||||
var initfunc, hookfunc, ishooked plugin.Symbol
|
||||
if err == nil {
|
||||
initfunc, err = p.Lookup("Inita")
|
||||
ishooked, err = p.Lookup("IsHooked")
|
||||
if err == nil {
|
||||
hookfunc, err = p.Lookup("Hook")
|
||||
if !*ishooked.(*bool) {
|
||||
hookfunc, err = p.Lookup("Hook")
|
||||
if err == nil {
|
||||
logrus.Debugf("[dyloader]reg: %x, del: %x\n", control.Register, control.Delete)
|
||||
logrus.Debugf("[dyloader]matlist: %p, matlock: %p\n", &matcherList, &matcherLock)
|
||||
hookfunc.(func(interface{}, interface{}, interface{},
|
||||
interface{}, interface{}, interface{},
|
||||
interface{}, interface{},
|
||||
interface{}, interface{}, interface{},
|
||||
interface{},
|
||||
interface{}, interface{}, interface{},
|
||||
))(
|
||||
&zero.BotConfig, &zero.APICallers, zero.New,
|
||||
&matcherList, &matcherLock, defaultEngine,
|
||||
control.Register, control.Delete,
|
||||
sendGroupMessage, sendPrivateMessage, getMessage,
|
||||
parse,
|
||||
message.CustomNode, message.ParseMessage, message.ParseMessageFromArray,
|
||||
)
|
||||
} else {
|
||||
_ = plugin.Close(p)
|
||||
return err
|
||||
}
|
||||
}
|
||||
initfunc, err = p.Lookup("Inita")
|
||||
if err == nil {
|
||||
logrus.Debugf("[dyloader]reg: %x, del: %x\n", control.Register, control.Delete)
|
||||
logrus.Debugf("[dyloader]matlist: %p, matlock: %p\n", &matcherList, &matcherLock)
|
||||
hookfunc.(func(interface{}, interface{}, interface{}, interface{}, interface{}))(&zero.BotConfig, &zero.APICallers, zero.New, &matcherList, &matcherLock)
|
||||
initfunc.(func(interface{}, interface{}))(control.Register, control.Delete)
|
||||
initfunc.(func())()
|
||||
logrus.Infoln("[dyloader]加载插件", path, "成功")
|
||||
pluginsMu.Lock()
|
||||
plugins[target] = p
|
||||
|
||||
4
main.go
4
main.go
@ -10,7 +10,7 @@ import (
|
||||
// 下列插件可与 wdvxdr1123/ZeroBot v1.1.2 以上配合单独使用
|
||||
|
||||
// 插件控制
|
||||
//_ "github.com/FloatTech/ZeroBot-Plugin/control/web" // web 后端控制
|
||||
// _ "github.com/FloatTech/ZeroBot-Plugin/control/web" // web 后端控制
|
||||
|
||||
// 词库类
|
||||
_ "github.com/FloatTech/ZeroBot-Plugin/plugin_atri" // ATRI词库
|
||||
@ -48,10 +48,10 @@ import (
|
||||
_ "github.com/FloatTech/ZeroBot-Plugin/plugin_setutime" // 来份涩图
|
||||
|
||||
// 以下为内置依赖,勿动
|
||||
// _ "github.com/FloatTech/ZeroBot-Plugin/dyloader"
|
||||
"github.com/sirupsen/logrus"
|
||||
zero "github.com/wdvxdr1123/ZeroBot"
|
||||
"github.com/wdvxdr1123/ZeroBot/driver"
|
||||
//_ "github.com/FloatTech/ZeroBot-Plugin/dyloader"
|
||||
)
|
||||
|
||||
var (
|
||||
|
||||
Loading…
Reference in New Issue
Block a user