package components import ( "plugin" "pro2d/common/logger" "pro2d/pb" "sync" ) type ActionMap sync.Map//map[pb.ProtoCode]ActionHandler type PluginOption func(*Plugin) type Plugin struct { pluginPath string Actions sync.Map } func NewPlugin(path string, options ...PluginOption) IPlugin{ p := &Plugin{ pluginPath: path, } for _, option := range options { option(p) } return p } func (p *Plugin) LoadPlugin() error { plu, err := plugin.Open(p.pluginPath) if err != nil { return err } logger.Debug("func LoadPlugin open success...") f, err := plu.Lookup("GetActionMap") if err != nil { return err } logger.Debug("func LoadPlugin Lookup success...") if x, ok := f.(func()map[interface{}]interface{}); ok { logger.Debug("func LoadPlugin GetActionMap success...") p.SetActions(x()) } return nil } func (p *Plugin) GetAction(cmd uint32) interface{} { f, ok := p.Actions.Load(pb.ProtoCode(cmd)) if !ok { return nil } return f } func (p *Plugin) SetActions(am map[interface{}]interface{}) { for k, v := range am { cmd := k.(pb.ProtoCode) p.Actions.Delete(cmd) p.Actions.Store(cmd, v) } }