2021-08-19 13:33:56 +00:00
|
|
|
//go:build cgo && (linux || darwin || freebsd)
|
|
|
|
|
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
2024-01-01 16:52:18 +00:00
|
|
|
"fmt"
|
2021-08-19 13:33:56 +00:00
|
|
|
"os"
|
|
|
|
"path"
|
|
|
|
"path/filepath"
|
|
|
|
"plugin"
|
|
|
|
"strings"
|
|
|
|
|
|
|
|
"github.com/pkg/errors"
|
|
|
|
log "github.com/sirupsen/logrus"
|
2021-11-25 22:48:16 +00:00
|
|
|
|
2022-11-02 21:38:14 +00:00
|
|
|
"github.com/Luzifer/twitch-bot/v3/plugins"
|
2021-08-19 13:33:56 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
func loadPlugins(pluginDir string) error {
|
|
|
|
logger := log.WithField("plugin_dir", pluginDir)
|
|
|
|
|
|
|
|
d, err := os.Stat(pluginDir)
|
|
|
|
if err != nil {
|
|
|
|
if os.IsNotExist(err) {
|
2021-10-23 15:38:46 +00:00
|
|
|
logger.Debug("Plugin directory not found, skipping")
|
2021-08-19 13:33:56 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
return errors.Wrap(err, "getting plugin-dir info")
|
|
|
|
}
|
|
|
|
|
|
|
|
if !d.IsDir() {
|
|
|
|
return errors.New("plugin-dir is not a directory")
|
|
|
|
}
|
|
|
|
|
|
|
|
args := getRegistrationArguments()
|
|
|
|
|
|
|
|
return errors.Wrap(filepath.Walk(pluginDir, func(currentPath string, info os.FileInfo, err error) error {
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
if !strings.HasSuffix(currentPath, ".so") {
|
|
|
|
// Ignore that file, is not a plugin
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
logger := log.WithField("plugin", path.Base(currentPath))
|
|
|
|
|
|
|
|
p, err := plugin.Open(currentPath)
|
|
|
|
if err != nil {
|
|
|
|
logger.WithError(err).Error("Unable to open plugin")
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
f, err := p.Lookup("Register")
|
|
|
|
if err != nil {
|
|
|
|
logger.WithError(err).Error("Unable to find register function")
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2024-01-01 16:52:18 +00:00
|
|
|
if err = f.(func(plugins.RegistrationArguments) error)(args); err != nil {
|
|
|
|
return fmt.Errorf("registering plugin: %w", err)
|
|
|
|
}
|
2021-08-19 13:33:56 +00:00
|
|
|
|
|
|
|
return nil
|
|
|
|
}), "loading plugins")
|
|
|
|
}
|