Move skinstore loading to seperate file

This commit is contained in:
Layla 2020-12-26 19:57:31 -05:00
parent 99594330b6
commit 5102cf73b8
No known key found for this signature in database
GPG Key ID: A494D9357BA1BE31
2 changed files with 50 additions and 21 deletions

32
main.go
View File

@ -2,11 +2,10 @@ package main
import (
"fmt"
"github.com/josephbmanley/OpenSkins-Common/datastore"
"github.com/josephbmanley/OpenSkins/pluginmanager"
log "github.com/sirupsen/logrus"
"os"
pas "plugin"
"plugin"
)
const plugindirectory = "./plugins"
@ -18,33 +17,24 @@ func main() {
log.Warningln(fmt.Sprintf("Failed to read plugins directory: %v", err.Error()))
}
loadedPlugins := []*plugin.Plugin{}
for _, file := range pluginFiles {
log.Infoln(fmt.Sprintf("Loading plugin: %v", file))
plugin, err := pas.Open(file)
plugin, err := plugin.Open(file)
if err != nil {
log.Fatalln(fmt.Sprintf("Failed to load plugin '%v': %v", file, err.Error()))
os.Exit(1)
}
symSkinstore, err := plugin.Lookup("SkinstoreModule")
if err != nil {
log.Fatalln(fmt.Sprintf("Failed to load plugin '%v': %v", file, err.Error()))
os.Exit(1)
}
var skinstore datastore.Skinstore
skinstore, ok := symSkinstore.(datastore.Skinstore)
if !ok {
log.Fatalln(fmt.Sprintf("Invalid type for Skinstore in plugin '%v'", file))
os.Exit(1)
}
err = skinstore.Initialize()
if err != nil {
log.Fatalln(fmt.Sprintf("Failed to intialize Skinstore in plugin '%v'", file))
os.Exit(1)
}
loadedPlugins = append(loadedPlugins, plugin)
}
err = pluginmanager.LoadSkinstores(loadedPlugins)
if err != nil {
log.Fatalln(fmt.Sprintf("Failed to load skinstores '%v'", err.Error()))
os.Exit(1)
}
}

View File

@ -0,0 +1,39 @@
package pluginmanager
import (
"errors"
"fmt"
"github.com/josephbmanley/OpenSkins-Common/datastore"
log "github.com/sirupsen/logrus"
"plugin"
)
// LoadedSkinstores is a list of all loaded skinstores
var LoadedSkinstores []datastore.Skinstore = []datastore.Skinstore{}
// LoadSkinstores searches plugins for a skinstore object
// and adds it to the 'skinstores` array
func LoadSkinstores(plugins []*plugin.Plugin) error {
for _, plugin := range plugins {
symSkinstore, err := plugin.Lookup("SkinstoreModule")
if err != nil {
log.Fatalln(fmt.Sprintf("Failed to load plugin: %v", err.Error()))
}
var skinstore datastore.Skinstore
skinstore, ok := symSkinstore.(datastore.Skinstore)
if !ok {
return errors.New("Invalid type for Skinstore in plugin")
}
err = skinstore.Initialize()
if err != nil {
return err
}
LoadedSkinstores = append(LoadedSkinstores, skinstore)
}
return nil
}