package main import ( "fmt" "log" "os" "path/filepath" "plugin" "common" ) // Module 接口,所有插件必须实现该接口 type Module interface { Init() error TextHandler(opts *common.SubHandlerOpts) error InlineCommandKeyword() string InlineHandler(opts *common.SubHandlerOpts) error } // func LoadPlugins(dir string) (map[string]Module, []string, error) { var modules = make(map[string]Module) var module_names []string files, err := os.ReadDir(dir) if err != nil { return nil, nil, err } for _, file := range files { if file.IsDir() || filepath.Ext(file.Name()) != ".so" { continue } modulePath := filepath.Join(dir, file.Name()) module, err := RegisterModule(modulePath) if err != nil { log.Printf("Failed to load module %s: %v", modulePath, err) } else { modules[file.Name()] = module module_names = append(module_names, file.Name()) } } return modules, module_names, nil } func RegisterModule(modulePath string) (Module, error) { p, err := plugin.Open(modulePath) if err != nil { return nil, err } symModule, err := p.Lookup("Module") if err != nil { return nil, err } module, ok := symModule.(Module) if !ok { return nil, fmt.Errorf("plugin does not implement the Module interface") } // 注册模块 return module, nil }