residentsleeper/scripting.go

56 lines
1.1 KiB
Go

package main
import (
"github.com/yuin/gopher-lua"
"io/ioutil"
luajson "layeh.com/gopher-json"
"layeh.com/gopher-luar"
"log"
"meow.tf/residentsleeper/scripting/commands"
"meow.tf/residentsleeper/scripting/config"
"meow.tf/residentsleeper/scripting/event"
"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.Println("Loading script", file.Name())
err = loadScript(path.Join(scriptPath, file.Name()))
if err != nil {
return err
}
}
return nil
}
func loadScript(scriptFile string) error {
L := lua.NewState()
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))
return L.DoFile(scriptFile)
}