2021-03-20 22:49:15 +00:00
|
|
|
package commander
|
|
|
|
|
|
|
|
import (
|
|
|
|
"github.com/chuckpreslar/emission"
|
|
|
|
"github.com/yuin/gopher-lua"
|
|
|
|
)
|
|
|
|
|
|
|
|
type Commander struct{
|
|
|
|
Events *emission.Emitter
|
|
|
|
}
|
|
|
|
|
|
|
|
func New() Commander {
|
|
|
|
return Commander{
|
|
|
|
Events: emission.NewEmitter(),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *Commander) Loader(L *lua.LState) int {
|
|
|
|
var exports = map[string]lua.LGFunction{
|
2021-10-16 14:21:05 +00:00
|
|
|
"register": c.cregister,
|
|
|
|
"deregister": c.cderegister,
|
2021-03-20 22:49:15 +00:00
|
|
|
}
|
|
|
|
mod := L.SetFuncs(L.NewTable(), exports)
|
|
|
|
|
|
|
|
L.Push(mod)
|
|
|
|
|
|
|
|
return 1
|
|
|
|
}
|
|
|
|
|
2021-10-16 14:21:05 +00:00
|
|
|
// register(name, cb)
|
|
|
|
// Register a command with `name` that runs `cb` when ran
|
|
|
|
func (c *Commander) cregister(L *lua.LState) int {
|
2021-05-01 17:42:15 +00:00
|
|
|
cmdName := L.CheckString(1)
|
|
|
|
cmd := L.CheckFunction(2)
|
2021-03-20 22:49:15 +00:00
|
|
|
|
|
|
|
c.Events.Emit("commandRegister", cmdName, cmd)
|
|
|
|
|
|
|
|
return 0
|
|
|
|
}
|
|
|
|
|
2021-10-16 14:21:05 +00:00
|
|
|
// deregister(name)
|
|
|
|
// Deregisters any command registered with `name`
|
|
|
|
func (c *Commander) cderegister(L *lua.LState) int {
|
2021-06-12 14:30:47 +00:00
|
|
|
cmdName := L.CheckString(1)
|
|
|
|
|
|
|
|
c.Events.Emit("commandDeregister", cmdName)
|
|
|
|
|
|
|
|
return 0
|
|
|
|
}
|