From 4e602d1c73d738e3e7f3071a265b6bcf408342e1 Mon Sep 17 00:00:00 2001 From: Edzell Date: Tue, 10 May 2022 18:28:34 +0200 Subject: [PATCH 1/2] Plugin interact --- plugin_interact.go | 57 ++++++++++++++++++++++++++++++++++++++++++++++ process.go | 12 +++++++--- 2 files changed, 66 insertions(+), 3 deletions(-) create mode 100644 plugin_interact.go diff --git a/plugin_interact.go b/plugin_interact.go new file mode 100644 index 0000000..6b4b146 --- /dev/null +++ b/plugin_interact.go @@ -0,0 +1,57 @@ +package proxy + +import ( + "sync" + + "github.com/anon55555/mt" +) + +// A InteractionHandler holds information on how to handle a Minetest Interaction. +type InteractionHandler struct { + Type mt.Interaction // can be 255 to register on all interactions + Handler func(*ClientConn, *mt.ToSrvInteract) bool +} + +var interactionHandlers []InteractionHandler +var interactionHandlerMu sync.RWMutex +var interactionHandlerOnce sync.Once + +// RegisterInteractionHandler adds a new InteractionHandler. +func RegisterInteractionHandler(handler InteractionHandler) { + initInteractionHandlers() + + chatCmdsMu.Lock() + defer chatCmdsMu.Unlock() + + interactionHandlers = append(interactionHandlers, handler) +} + +func initInteractionHandlers() { + chatCmdsOnce.Do(func() { + interactionHandlerMu.Lock() + defer interactionHandlerMu.Unlock() + + interactionHandlers = make([]InteractionHandler, 0) + }) +} + +func handleInteraction(cmd *mt.ToSrvInteract, cc *ClientConn) bool { + handled := false + handle := func(cond bool) { + if(cond) { + handled = true + } + } + + for _, handler := range interactionHandlers { + if handler.Type == 255 { + handle(handler.Handler(cc, cmd)) + } + if cmd.Action == handler.Type { + handle(handler.Handler(cc, cmd)) + } + + } + + return handled +} diff --git a/process.go b/process.go index 60f4f1a..7c5b71e 100644 --- a/process.go +++ b/process.go @@ -33,7 +33,7 @@ func (cc *ClientConn) process(pkt mt.Pkt) { } cc.setState(csInit) - if cmd.SerializeVer <= latestSerializeVer { + /*if cmd.SerializeVer <= latestSerializeVer { cc.Log("<-", "invalid serializeVer", cmd.SerializeVer) ack, _ := cc.SendCmd(&mt.ToCltKick{Reason: mt.UnsupportedVer}) @@ -57,7 +57,7 @@ func (cc *ClientConn) process(pkt mt.Pkt) { } return - } + }*/ if len(cmd.PlayerName) == 0 || len(cmd.PlayerName) > maxPlayerNameLen { cc.Log("<-", "invalid player name length") @@ -438,6 +438,10 @@ func (cc *ClientConn) process(pkt mt.Pkt) { return } + if handleInteraction(cmd, cc) { // if return true: already handled + return + } + if _, ok := cmd.Pointed.(*mt.PointedAO); ok { srv.swapAOID(&cmd.Pointed.(*mt.PointedAO).ID) } @@ -445,6 +449,7 @@ func (cc *ClientConn) process(pkt mt.Pkt) { done := make(chan struct{}) go func(done chan<- struct{}) { + result, isCmd := onChatMsg(cc, cmd) if !isCmd { forward(pkt) @@ -736,6 +741,8 @@ func (sc *ServerConn) process(pkt mt.Pkt) { return case *mt.ToCltMediaPush: + prepend(sc.mediaPool, &cmd.Filename) + var exit bool for _, f := range clt.media { if f.name == cmd.Filename { @@ -748,7 +755,6 @@ func (sc *ServerConn) process(pkt mt.Pkt) { break } - prepend(sc.mediaPool, &cmd.Filename) if cmd.ShouldCache { cacheMedia(mediaFile{ name: cmd.Filename, From 308b7ae083264c2d3a6411335f8e60921173c370 Mon Sep 17 00:00:00 2001 From: Riley Date: Tue, 10 May 2022 18:33:00 +0200 Subject: [PATCH 2/2] some revert things, because limited scope --- process.go | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/process.go b/process.go index 7c5b71e..b576143 100644 --- a/process.go +++ b/process.go @@ -33,7 +33,7 @@ func (cc *ClientConn) process(pkt mt.Pkt) { } cc.setState(csInit) - /*if cmd.SerializeVer <= latestSerializeVer { + if cmd.SerializeVer <= latestSerializeVer { cc.Log("<-", "invalid serializeVer", cmd.SerializeVer) ack, _ := cc.SendCmd(&mt.ToCltKick{Reason: mt.UnsupportedVer}) @@ -57,7 +57,7 @@ func (cc *ClientConn) process(pkt mt.Pkt) { } return - }*/ + } if len(cmd.PlayerName) == 0 || len(cmd.PlayerName) > maxPlayerNameLen { cc.Log("<-", "invalid player name length") @@ -449,7 +449,6 @@ func (cc *ClientConn) process(pkt mt.Pkt) { done := make(chan struct{}) go func(done chan<- struct{}) { - result, isCmd := onChatMsg(cc, cmd) if !isCmd { forward(pkt)