diff options
Diffstat (limited to 'hydra.go')
-rw-r--r-- | hydra.go | 82 |
1 files changed, 52 insertions, 30 deletions
@@ -2,7 +2,8 @@ package main import ( _ "embed" - "github.com/Shopify/go-lua" + "github.com/dragonfireclient/hydra/tolua" + "github.com/yuin/gopher-lua" "os" "os/signal" "syscall" @@ -15,15 +16,12 @@ var canceled = false //go:embed builtin/vector.lua var vectorLibrary string -func l_dtime(l *lua.State) int { - l.PushNumber(time.Since(lastTime).Seconds()) - lastTime = time.Now() - return 1 -} - -func l_canceled(l *lua.State) int { - l.PushBoolean(canceled) - return 1 +var hydraFuncs = map[string]lua.LGFunction{ + "client": l_client, + "dtime": l_dtime, + "canceled": l_canceled, + "poll": l_poll, + "disconnect": l_disconnect, } func signalChannel() chan os.Signal { @@ -32,6 +30,36 @@ func signalChannel() chan os.Signal { return sig } +func l_dtime(l *lua.LState) int { + l.Push(lua.LNumber(time.Since(lastTime).Seconds())) + return 1 +} + +func l_canceled(l *lua.LState) int { + l.Push(lua.LBool(canceled)) + return 1 +} + +func l_poll(l *lua.LState) int { + client, pkt, timeout := doPoll(l, getClients(l)) + if client == nil { + l.Push(lua.LNil) + } else { + l.Push(client.userdata) + } + l.Push(tolua.Pkt(l, pkt)) + l.Push(lua.LBool(timeout)) + return 3 +} + +func l_disconnect(l *lua.LState) int { + for _, client := range getClients(l) { + client.disconnect() + } + + return 0 +} + func main() { if len(os.Args) < 2 { panic("missing filename") @@ -42,33 +70,27 @@ func main() { canceled = true }() - l := lua.NewState() - lua.OpenLibraries(l) - - lua.NewLibrary(l, []lua.RegistryFunction{ - {Name: "client", Function: l_client}, - {Name: "dtime", Function: l_dtime}, - {Name: "canceled", Function: l_canceled}, - {Name: "poll", Function: l_poll}, - }) + l := lua.NewState(lua.Options{IncludeGoStackTrace: true}) + defer l.Close() - l.PushNumber(10.0) - l.SetField(-2, "BS") + arg := l.NewTable() + for i, a := range os.Args { + l.RawSetInt(arg, i-1, lua.LString(a)) + } + l.SetGlobal("arg", arg) - l.SetGlobal("hydra") + hydra := l.SetFuncs(l.NewTable(), hydraFuncs) + l.SetField(hydra, "BS", lua.LNumber(10.0)) + l.SetGlobal("hydra", hydra) - l.NewTable() - for i, arg := range os.Args { - l.PushString(arg) - l.RawSetInt(-2, i - 1) - } - l.SetGlobal("arg") + l.SetField(l.NewTypeMetatable("hydra.auth"), "__index", l.SetFuncs(l.NewTable(), authFuncs)) + l.SetField(l.NewTypeMetatable("hydra.client"), "__index", l.NewFunction(l_client_index)) - if err := lua.DoString(l, vectorLibrary); err != nil { + if err := l.DoString(vectorLibrary); err != nil { panic(err) } - if err := lua.DoFile(l, os.Args[1]); err != nil { + if err := l.DoFile(os.Args[1]); err != nil { panic(err) } } |