2017-11-10 03:33:11 +00:00
|
|
|
package common
|
2017-09-28 22:16:34 +00:00
|
|
|
|
|
|
|
import (
|
2022-02-21 03:32:53 +00:00
|
|
|
"encoding/json"
|
|
|
|
"errors"
|
|
|
|
"io/ioutil"
|
|
|
|
"path/filepath"
|
2017-09-28 22:16:34 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
var pluginLangs = make(map[string]PluginLang)
|
|
|
|
|
|
|
|
// For non-native plugins to bind JSON files to. E.g. JS and Lua
|
|
|
|
type PluginMeta struct {
|
2022-02-21 03:32:53 +00:00
|
|
|
UName string
|
|
|
|
Name string
|
|
|
|
Author string
|
|
|
|
URL string
|
|
|
|
Settings string
|
|
|
|
Tag string
|
2017-09-28 22:16:34 +00:00
|
|
|
|
2022-02-21 03:32:53 +00:00
|
|
|
Skip bool // Skip this folder?
|
|
|
|
Main string // The main file
|
|
|
|
Hooks map[string]string // Hooks mapped to functions
|
2017-09-28 22:16:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
type PluginLang interface {
|
2022-02-21 03:32:53 +00:00
|
|
|
GetName() string
|
|
|
|
GetExts() []string
|
2017-09-28 22:16:34 +00:00
|
|
|
|
2022-02-21 03:32:53 +00:00
|
|
|
Init() error
|
|
|
|
AddPlugin(meta PluginMeta) (*Plugin, error)
|
|
|
|
//AddHook(name string, handler interface{}) error
|
|
|
|
//RemoveHook(name string, handler interface{})
|
|
|
|
//RunHook(name string, data interface{}) interface{}
|
|
|
|
//RunVHook(name string data ...interface{}) interface{}
|
2017-09-28 22:16:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
var ext = filepath.Ext(pluginFile.Name())
|
|
|
|
if ext == ".txt" || ext == ".go" {
|
2022-02-21 03:32:53 +00:00
|
|
|
continue
|
2017-09-28 22:16:34 +00:00
|
|
|
}
|
|
|
|
*/
|
|
|
|
|
|
|
|
func InitPluginLangs() error {
|
2022-02-21 03:32:53 +00:00
|
|
|
for _, pluginLang := range pluginLangs {
|
|
|
|
pluginLang.Init()
|
|
|
|
}
|
|
|
|
pluginList, err := GetPluginFiles()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2017-09-28 22:16:34 +00:00
|
|
|
|
2022-02-21 03:32:53 +00:00
|
|
|
for _, pluginItem := range pluginList {
|
|
|
|
pluginFile, err := ioutil.ReadFile("./extend/" + pluginItem + "/plugin.json")
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2017-09-28 22:16:34 +00:00
|
|
|
|
2022-02-21 03:32:53 +00:00
|
|
|
var plugin PluginMeta
|
|
|
|
err = json.Unmarshal(pluginFile, &plugin)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if plugin.Skip {
|
|
|
|
continue
|
|
|
|
}
|
2017-09-28 22:16:34 +00:00
|
|
|
|
2022-02-21 03:32:53 +00:00
|
|
|
e := func(field, name string) error {
|
|
|
|
return errors.New("The " + field + " field must not be blank on plugin '" + name + "'")
|
|
|
|
}
|
|
|
|
if plugin.UName == "" {
|
|
|
|
return e("UName", pluginItem)
|
|
|
|
}
|
|
|
|
if plugin.Name == "" {
|
|
|
|
return e("Name", pluginItem)
|
|
|
|
}
|
|
|
|
if plugin.Author == "" {
|
|
|
|
return e("Author", pluginItem)
|
|
|
|
}
|
|
|
|
if plugin.Main == "" {
|
|
|
|
return errors.New("Couldn't find a main file for plugin '" + pluginItem + "'")
|
|
|
|
}
|
2017-09-28 22:16:34 +00:00
|
|
|
|
2022-02-21 03:32:53 +00:00
|
|
|
ext := filepath.Ext(plugin.Main)
|
|
|
|
pluginLang, err := ExtToPluginLang(ext)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
pplugin, err := pluginLang.AddPlugin(plugin)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
Plugins[plugin.UName] = pplugin
|
|
|
|
}
|
|
|
|
return nil
|
2017-09-28 22:16:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func GetPluginFiles() (pluginList []string, err error) {
|
2022-02-21 03:32:53 +00:00
|
|
|
pluginFiles, err := ioutil.ReadDir("./extend")
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
for _, pluginFile := range pluginFiles {
|
|
|
|
if !pluginFile.IsDir() {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
pluginList = append(pluginList, pluginFile.Name())
|
|
|
|
}
|
|
|
|
return pluginList, nil
|
2017-09-28 22:16:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func ExtToPluginLang(ext string) (PluginLang, error) {
|
2022-02-21 03:32:53 +00:00
|
|
|
for _, pluginLang := range pluginLangs {
|
|
|
|
for _, registeredExt := range pluginLang.GetExts() {
|
|
|
|
if registeredExt == ext {
|
|
|
|
return pluginLang, nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil, errors.New("No plugin lang handlers are capable of handling extension '" + ext + "'")
|
2017-09-28 22:16:34 +00:00
|
|
|
}
|