Skip to content

Commit

Permalink
✨ dyloader 缩小插件体积
Browse files Browse the repository at this point in the history
  • Loading branch information
fumiama committed Oct 14, 2021
1 parent ec0032c commit 51ec3c3
Show file tree
Hide file tree
Showing 4 changed files with 52 additions and 15 deletions.
7 changes: 2 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
16 changes: 16 additions & 0 deletions dyloader/hook.go
Original file line number Diff line number Diff line change
@@ -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)
}
40 changes: 32 additions & 8 deletions dyloader/scan.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 (
Expand Down Expand Up @@ -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
Expand Down
4 changes: 2 additions & 2 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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词库
Expand Down Expand Up @@ -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 (
Expand Down

0 comments on commit 51ec3c3

Please sign in to comment.