diff options
author | Elias Fleckenstein <eliasfleckenstein@web.de> | 2022-05-28 23:16:55 +0200 |
---|---|---|
committer | Elias Fleckenstein <eliasfleckenstein@web.de> | 2022-05-28 23:16:55 +0200 |
commit | f0318bd020abe57c0cf365b0479b5d14b95ff07a (patch) | |
tree | ed2d89cda1adeb6f3ed6a6da98e054e2b7519c37 /callbacks.go | |
parent | fea98ddbbe886845ed41ab87d9a2d24323c8de82 (diff) | |
download | hydra-dragonfire-f0318bd020abe57c0cf365b0479b5d14b95ff07a.tar.xz |
Migrate to gopher-lua
Diffstat (limited to 'callbacks.go')
-rw-r--r-- | callbacks.go | 85 |
1 files changed, 0 insertions, 85 deletions
diff --git a/callbacks.go b/callbacks.go deleted file mode 100644 index e16aec7..0000000 --- a/callbacks.go +++ /dev/null @@ -1,85 +0,0 @@ -package main - -import ( - "github.com/Shopify/go-lua" - "github.com/anon55555/mt" -) - -type Callbacks struct { - wildcard bool - subscribed map[string]struct{} -} - -func getCallbacks(l *lua.State) *Callbacks { - return lua.CheckUserData(l, 1, "hydra.callbacks").(*Callbacks) -} - -func (handler *Callbacks) create(client *Client) { - handler.subscribed = map[string]struct{}{} -} - -func (handler *Callbacks) push(l *lua.State) { - l.PushUserData(handler) - - if lua.NewMetaTable(l, "hydra.callbacks") { - lua.NewLibrary(l, []lua.RegistryFunction{ - {Name: "wildcard", Function: l_callbacks_wildcard}, - {Name: "subscribe", Function: l_callbacks_subscribe}, - {Name: "unsubscribe", Function: l_callbacks_unsubscribe}, - }) - l.SetField(-2, "__index") - } - l.SetMetaTable(-2) -} - -func (handler *Callbacks) canConnect() (bool, string) { - return true, "" -} - -func (handler *Callbacks) connect() { -} - -func (handler *Callbacks) handle(pkt *mt.Pkt, l *lua.State, idx int) { - if !handler.wildcard && pkt != nil { - if _, exists := handler.subscribed[pktToString(pkt)]; !exists { - return - } - } - - if !l.IsFunction(2) { - return - } - - l.PushValue(2) // callback - l.RawGetInt(1, idx) // arg 1: client - pktToLua(l, pkt) // arg 2: pkt - l.Call(2, 0) -} - -func l_callbacks_wildcard(l *lua.State) int { - handler := getCallbacks(l) - handler.wildcard = l.ToBoolean(2) - return 0 -} - -func l_callbacks_subscribe(l *lua.State) int { - handler := getCallbacks(l) - - n := l.Top() - for i := 2; i <= n; i++ { - handler.subscribed[lua.CheckString(l, i)] = struct{}{} - } - - return 0 -} - -func l_callbacks_unsubscribe(l *lua.State) int { - handler := getCallbacks(l) - - n := l.Top() - for i := 2; i <= n; i++ { - delete(handler.subscribed, lua.CheckString(l, i)) - } - - return 0 -} |