residentsleeper/scripting.go

65 lines
1.2 KiB
Go
Raw Normal View History

package main
import (
log "github.com/sirupsen/logrus"
"github.com/yuin/gopher-lua"
"io/ioutil"
luajson "layeh.com/gopher-json"
"layeh.com/gopher-luar"
"meow.tf/residentsleeper/scripting/commands"
"meow.tf/residentsleeper/scripting/config"
"meow.tf/residentsleeper/scripting/event"
"meow.tf/residentsleeper/scripting/eventloop"
"meow.tf/residentsleeper/scripting/minecraft"
"meow.tf/residentsleeper/scripting/regexp"
"path"
)
func loadScripts(scriptPath string) error {
files, err := ioutil.ReadDir(scriptPath)
if err != nil {
return err
}
for _, file := range files {
if file.IsDir() {
continue
}
log.WithField("file", file.Name()).Info("Loading script")
err = loadScript(path.Join(scriptPath, file.Name()))
if err != nil {
return err
}
}
return nil
}
func loadScript(scriptFile string) error {
loop := eventloop.NewEventLoop()
var err error
loop.Run(func(L *lua.LState) {
luajson.Preload(L)
L.PreloadModule("config", config.Loader)
L.PreloadModule("event", event.Loader)
L.PreloadModule("regexp", regexp.Loader)
L.PreloadModule("minecraft", minecraft.Loader)
L.PreloadModule("commands", commands.Loader)
L.SetGlobal("rcon", luar.New(L, client))
err = L.DoFile(scriptFile)
})
loop.Start()
return err
}