From e91e25d9eee94989e30c6a90ae31ae63b8ea3a41 Mon Sep 17 00:00:00 2001 From: Edzell Date: Sun, 15 May 2022 19:04:23 +0200 Subject: [PATCH 01/18] plugin_map --- content.go | 4 +++ plugin_map.go | 74 +++++++++++++++++++++++++++++++++++++++++++++++++++ process.go | 4 +++ 3 files changed, 82 insertions(+) create mode 100644 plugin_map.go diff --git a/content.go b/content.go index c8e3fe6..532fc06 100644 --- a/content.go +++ b/content.go @@ -431,6 +431,7 @@ func muxNodeDefs(conns []*contentConn) (nodeDefs []mt.NodeDef, p0Map param0Map, } def.Param0 = param0 + oldName := def.Name // copy string to use later prepend(cc.mediaPool, &def.Name) prepend(cc.mediaPool, &def.Mesh) for i := range def.Tiles { @@ -456,6 +457,9 @@ func muxNodeDefs(conns []*contentConn) (nodeDefs []mt.NodeDef, p0Map param0Map, if param0 >= mt.Unknown && param0 <= mt.Ignore { param0 = mt.Ignore + 1 } + + // add nodeid (if reqested) + addNodeId(oldName, def.Param0) } } diff --git a/plugin_map.go b/plugin_map.go new file mode 100644 index 0000000..61b4770 --- /dev/null +++ b/plugin_map.go @@ -0,0 +1,74 @@ +package proxy + +import ( + "sync" + + "github.com/anon55555/mt" +) + +type BlkDataHandler struct { + Pos [3]int16 // optional TODO: implement + Handler func(*ClientConn, *mt.ToCltBlkData) bool +} + +var BlkDataHandlers []BlkDataHandler +var BlkDataHandlersMu sync.RWMutex + +var neededNodes = map[string]map[mt.Content]bool{} +var neededNodesMu sync.RWMutex + +// RegisterNeedNode tells server, that nodename is needed +func RegisterNeedNode(nodename string) { + neededNodesMu.Lock() + defer neededNodesMu.Unlock() + + if neededNodes[nodename] == nil { + neededNodes[nodename] = map[mt.Content]bool{} // default value, empty map + } +} + +// GetNodeId gets the nodeid of a +// If not registerd returns map[mt.Content]bool{mt.Ignore:true} +func GetNodeId(nodename string) map[mt.Content]bool { + neededNodesMu.RLock() + defer neededNodesMu.RUnlock() + + if neededNodes[nodename] != nil { + return neededNodes[nodename] + } else { + return map[mt.Content]bool{mt.Ignore: true} + } +} + +// addNodeId sets node id, if allready set, ignore +func addNodeId(nodename string, id mt.Content) { + neededNodesMu.Lock() + defer neededNodesMu.Unlock() + + if neededNodes[nodename] != nil { + neededNodes[nodename][id] = true + } +} + +// RegisterBlkDataHandler registers a BlkDataHande +func RegisterBlkDataHandler(handler BlkDataHandler) { + BlkDataHandlersMu.Lock() + defer BlkDataHandlersMu.Unlock() + + BlkDataHandlers = append(BlkDataHandlers, handler) +} + +func handleBlkData(cc *ClientConn, cmd *mt.ToCltBlkData) bool { + BlkDataHandlersMu.RLock() + defer BlkDataHandlersMu.RUnlock() + + handled := false + + for _, handler := range BlkDataHandlers { + if handler.Handler(cc, cmd) { + handled = true + } + } + + return handled +} diff --git a/process.go b/process.go index ed8004d..7666c5a 100644 --- a/process.go +++ b/process.go @@ -807,6 +807,10 @@ func (sc *ServerConn) process(pkt mt.Pkt) { } sc.prependInv(cmd.Blk.NodeMetas[k].Inv) } + + if handleBlkData(sc.client(), cmd) { + return + } case *mt.ToCltAddNode: sc.globalParam0(&cmd.Node.Param0) case *mt.ToCltAddParticleSpawner: From ab21ae3522d72930553e6edbb8a79400445b5315 Mon Sep 17 00:00:00 2001 From: Edzell Date: Sun, 15 May 2022 19:08:57 +0200 Subject: [PATCH 02/18] unexported blkdatahandlers --- plugin_map.go | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/plugin_map.go b/plugin_map.go index 61b4770..6a439cf 100644 --- a/plugin_map.go +++ b/plugin_map.go @@ -11,8 +11,8 @@ type BlkDataHandler struct { Handler func(*ClientConn, *mt.ToCltBlkData) bool } -var BlkDataHandlers []BlkDataHandler -var BlkDataHandlersMu sync.RWMutex +var blkDataHandlers []BlkDataHandler +var blkDataHandlersMu sync.RWMutex var neededNodes = map[string]map[mt.Content]bool{} var neededNodesMu sync.RWMutex @@ -52,19 +52,19 @@ func addNodeId(nodename string, id mt.Content) { // RegisterBlkDataHandler registers a BlkDataHande func RegisterBlkDataHandler(handler BlkDataHandler) { - BlkDataHandlersMu.Lock() - defer BlkDataHandlersMu.Unlock() + blkDataHandlersMu.Lock() + defer blkDataHandlersMu.Unlock() - BlkDataHandlers = append(BlkDataHandlers, handler) + blkDataHandlers = append(blkDataHandlers, handler) } func handleBlkData(cc *ClientConn, cmd *mt.ToCltBlkData) bool { - BlkDataHandlersMu.RLock() - defer BlkDataHandlersMu.RUnlock() + blkDataHandlersMu.RLock() + defer blkDataHandlersMu.RUnlock() handled := false - for _, handler := range BlkDataHandlers { + for _, handler := range blkDataHandlers { if handler.Handler(cc, cmd) { handled = true } From 5ea3a8c0cae78fa4ff75d6b6218aa1cf169bebee Mon Sep 17 00:00:00 2001 From: Riley Date: Sun, 15 May 2022 21:31:32 +0200 Subject: [PATCH 03/18] changed deault return value of func --- plugin_map.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/plugin_map.go b/plugin_map.go index 6a439cf..b78505d 100644 --- a/plugin_map.go +++ b/plugin_map.go @@ -28,7 +28,7 @@ func RegisterNeedNode(nodename string) { } // GetNodeId gets the nodeid of a -// If not registerd returns map[mt.Content]bool{mt.Ignore:true} +// If not registerd returns map[mt.Content]bool{} func GetNodeId(nodename string) map[mt.Content]bool { neededNodesMu.RLock() defer neededNodesMu.RUnlock() @@ -36,7 +36,7 @@ func GetNodeId(nodename string) map[mt.Content]bool { if neededNodes[nodename] != nil { return neededNodes[nodename] } else { - return map[mt.Content]bool{mt.Ignore: true} + return map[mt.Content]bool{} } } From 420bddf16255fe28102ee4664f070f09477a0565 Mon Sep 17 00:00:00 2001 From: Edzell Date: Thu, 19 May 2022 06:51:17 +0200 Subject: [PATCH 04/18] plugin node --- plugin_map.go | 2 +- plugin_node.go | 182 +++++++++++++++++++++++++++++++++++++++++++++++++ run.go | 3 + 3 files changed, 186 insertions(+), 1 deletion(-) create mode 100644 plugin_node.go diff --git a/plugin_map.go b/plugin_map.go index b78505d..49e11fe 100644 --- a/plugin_map.go +++ b/plugin_map.go @@ -36,7 +36,7 @@ func GetNodeId(nodename string) map[mt.Content]bool { if neededNodes[nodename] != nil { return neededNodes[nodename] } else { - return map[mt.Content]bool{} + return nil } } diff --git a/plugin_node.go b/plugin_node.go new file mode 100644 index 0000000..9ab0a52 --- /dev/null +++ b/plugin_node.go @@ -0,0 +1,182 @@ +package proxy + +import ( + "sync" + + "github.com/anon55555/mt" +) + +type NodeHandler struct { + Node string + nodeIds map[mt.Content]bool + OnDig func(*ClientConn, *[3]int16) bool + OnStopDigging func(*ClientConn, *[3]int16) bool + OnDug func(*ClientConn, *[3]int16) bool + OnPlace func(*ClientConn, *[3]int16) bool + OnUse func(*ClientConn, *[3]int16) bool + OnActivate func(*ClientConn, *[3]int16) bool +} + +var NodeHandlers []*NodeHandler +var NodeHandlersMu sync.RWMutex + +var mapCache map[[3]int16]*[4096]mt.Content +var mapCacheMu sync.RWMutex +var mapCacheOnce sync.Once + +func initMapCache() { + mapCacheOnce.Do(func() { + mapCache = map[[3]int16]*[4096]mt.Content{} + }) +} + +func RegisterNodeHandler(handler *NodeHandler) { + + NodeHandlersMu.Lock() + defer NodeHandlersMu.Unlock() + + RegisterNeedNode(handler.Node) + NodeHandlers = append(NodeHandlers, handler) +} + +func initNodeHandlerNodeIds() { + NodeHandlersMu.RLock() + defer NodeHandlersMu.RUnlock() + + for _, h := range NodeHandlers { + if h.nodeIds == nil { + id := GetNodeId(h.Node) + + if id != nil { + h.nodeIds = id + } + } + } +} + +func GetMapCache() map[[3]int16]*[4096]mt.Content { + initMapCache() + mapCacheMu.RLock() + defer mapCacheMu.RUnlock() + + return mapCache +} + +func IsCached(pos [3]int16) bool { + initMapCache() + mapCacheMu.RLock() + defer mapCacheMu.RUnlock() + + blkpos, i := mt.Pos2Blkpos(pos) + if mapCache[blkpos] == nil { + return false + } else { + return mapCache[blkpos][i] != 0 + } +} + +func handleNodeInteraction(cc *ClientConn, pointedNode *mt.PointedNode, cmd *mt.ToSrvInteract) bool { + NodeHandlersMu.RLock() + defer NodeHandlersMu.RUnlock() + + var handled bool + + for _, handler := range NodeHandlers { + var h bool + + switch cmd.Action { + case mt.Dig: + if handler.OnDig != nil { + h = handler.OnDig(cc, &pointedNode.Under) + } + case mt.StopDigging: + if handler.OnStopDigging != nil { + h = handler.OnStopDigging(cc, &pointedNode.Under) + } + case mt.Dug: + if handler.OnDug != nil { + h = handler.OnDug(cc, &pointedNode.Under) + } + case mt.Place: + if handler.OnPlace != nil { + h = handler.OnPlace(cc, &pointedNode.Under) + } + case mt.Use: + if handler.OnUse != nil { + h = handler.OnUse(cc, &pointedNode.Under) + } + case mt.Activate: + if handler.OnActivate != nil { + h = handler.OnActivate(cc, &pointedNode.Under) + } + } + + if h { + handled = h + } + } + + return handled +} + +func initPluginNode() { + RegisterBlkDataHandler(BlkDataHandler{ + Handler: func(cc *ClientConn, cmd *mt.ToCltBlkData) bool { + initMapCache() + + initNodeHandlerNodeIds() + mapCacheMu.Lock() + defer mapCacheMu.Unlock() + + for i, node := range cmd.Blk.Param0 { + // check if node is interesting + interesting := false + for _, h := range NodeHandlers { + if h.nodeIds[node] { + interesting = true + break + } + } + + // if it changed + // if !interesting && mapCache[cmd.Blkpos][i] != 0 && mapCache[cmd.Blkpos][i] != node { + if !interesting { + if mapCache[cmd.Blkpos] != nil { + if mapCache[cmd.Blkpos][i] != 0 && mapCache[cmd.Blkpos][i] != node { + interesting = true + } + } + } + + if interesting { + //cc.Log("<>", "interesting mapBlock") + if mapCache[cmd.Blkpos] == nil { + mapCache[cmd.Blkpos] = &[4096]mt.Content{} + } + mapCache[cmd.Blkpos][i] = node + } + } + + return false + }, + }) + + RegisterInteractionHandler(InteractionHandler{ + Type: AnyInteraction, + Handler: func(cc *ClientConn, cmd *mt.ToSrvInteract) bool { + handled := false + + if pointedNode, ok := cmd.Pointed.(*mt.PointedNode); ok { + if IsCached(pointedNode.Under) { + // is a interesting node + if handleNodeInteraction(cc, pointedNode, cmd) { + handled = true + } + } + } + + return handled + }, + }) +} + diff --git a/run.go b/run.go index 451b531..c9f3c30 100644 --- a/run.go +++ b/run.go @@ -54,6 +54,9 @@ func runFunc() { log.Println("listen", l.Addr()) + // plugin_node.go + initPluginNode() + go func() { sig := make(chan os.Signal, 1) signal.Notify(sig, os.Interrupt, syscall.SIGTERM, syscall.SIGHUP) From 993bb914ddbc669f53573e84cf95b8b6b31b1b83 Mon Sep 17 00:00:00 2001 From: Edzell Date: Sun, 22 May 2022 21:53:34 +0200 Subject: [PATCH 05/18] sine fukes --- plugin_node.go | 89 ++++++++++++++++++++++++++------------------------ 1 file changed, 47 insertions(+), 42 deletions(-) diff --git a/plugin_node.go b/plugin_node.go index 9ab0a52..bd4b339 100644 --- a/plugin_node.go +++ b/plugin_node.go @@ -9,19 +9,19 @@ import ( type NodeHandler struct { Node string nodeIds map[mt.Content]bool - OnDig func(*ClientConn, *[3]int16) bool - OnStopDigging func(*ClientConn, *[3]int16) bool - OnDug func(*ClientConn, *[3]int16) bool - OnPlace func(*ClientConn, *[3]int16) bool - OnUse func(*ClientConn, *[3]int16) bool - OnActivate func(*ClientConn, *[3]int16) bool + OnDig func(*ClientConn, *mt.ToSrvInteract) bool + OnStopDigging func(*ClientConn, *mt.ToSrvInteract) bool + OnDug func(*ClientConn, *mt.ToSrvInteract) bool + OnPlace func(*ClientConn, *mt.ToSrvInteract) bool + OnUse func(*ClientConn, *mt.ToSrvInteract) bool + OnActivate func(*ClientConn, *mt.ToSrvInteract) bool } var NodeHandlers []*NodeHandler var NodeHandlersMu sync.RWMutex -var mapCache map[[3]int16]*[4096]mt.Content -var mapCacheMu sync.RWMutex +var mapCache map[[3]int16]*[4096]mt.Content +var mapCacheMu sync.RWMutex var mapCacheOnce sync.Once func initMapCache() { @@ -42,7 +42,7 @@ func RegisterNodeHandler(handler *NodeHandler) { func initNodeHandlerNodeIds() { NodeHandlersMu.RLock() defer NodeHandlersMu.RUnlock() - + for _, h := range NodeHandlers { if h.nodeIds == nil { id := GetNodeId(h.Node) @@ -78,41 +78,47 @@ func IsCached(pos [3]int16) bool { func handleNodeInteraction(cc *ClientConn, pointedNode *mt.PointedNode, cmd *mt.ToSrvInteract) bool { NodeHandlersMu.RLock() defer NodeHandlersMu.RUnlock() + mapCacheMu.RLock() + defer mapCacheMu.RUnlock() var handled bool for _, handler := range NodeHandlers { - var h bool - - switch cmd.Action { - case mt.Dig: - if handler.OnDig != nil { - h = handler.OnDig(cc, &pointedNode.Under) - } - case mt.StopDigging: - if handler.OnStopDigging != nil { - h = handler.OnStopDigging(cc, &pointedNode.Under) - } - case mt.Dug: - if handler.OnDug != nil { - h = handler.OnDug(cc, &pointedNode.Under) - } - case mt.Place: - if handler.OnPlace != nil { - h = handler.OnPlace(cc, &pointedNode.Under) - } - case mt.Use: - if handler.OnUse != nil { - h = handler.OnUse(cc, &pointedNode.Under) - } - case mt.Activate: - if handler.OnActivate != nil { - h = handler.OnActivate(cc, &pointedNode.Under) + // check if nodeId is right + pos, i := mt.Pos2Blkpos(pointedNode.Under) + if handler.nodeIds[mapCache[pos][i]] { + var h bool + + switch cmd.Action { + case mt.Dig: + if handler.OnDig != nil { + h = handler.OnDig(cc, cmd) + } + case mt.StopDigging: + if handler.OnStopDigging != nil { + h = handler.OnStopDigging(cc, cmd) + } + case mt.Dug: + if handler.OnDug != nil { + h = handler.OnDug(cc, cmd) + } + case mt.Place: + if handler.OnPlace != nil { + h = handler.OnPlace(cc, cmd) + } + case mt.Use: + if handler.OnUse != nil { + h = handler.OnUse(cc, cmd) + } + case mt.Activate: + if handler.OnActivate != nil { + h = handler.OnActivate(cc, cmd) + } } - } - if h { - handled = h + if h { + handled = h + } } } @@ -127,7 +133,7 @@ func initPluginNode() { initNodeHandlerNodeIds() mapCacheMu.Lock() defer mapCacheMu.Unlock() - + for i, node := range cmd.Blk.Param0 { // check if node is interesting interesting := false @@ -165,7 +171,7 @@ func initPluginNode() { Type: AnyInteraction, Handler: func(cc *ClientConn, cmd *mt.ToSrvInteract) bool { handled := false - + if pointedNode, ok := cmd.Pointed.(*mt.PointedNode); ok { if IsCached(pointedNode.Under) { // is a interesting node @@ -174,9 +180,8 @@ func initPluginNode() { } } } - + return handled }, }) } - From 6de7c6908d2c3a51b3b613d93302d50a2737dc4a Mon Sep 17 00:00:00 2001 From: Riley Date: Tue, 24 May 2022 19:58:37 +0200 Subject: [PATCH 06/18] Some tests with overwriting AOs --- plugin_AO.go | 108 +++++++++++++++++++++++++++++++++++++++++++++++++++ process.go | 9 +++++ 2 files changed, 117 insertions(+) create mode 100644 plugin_AO.go diff --git a/plugin_AO.go b/plugin_AO.go new file mode 100644 index 0000000..5007549 --- /dev/null +++ b/plugin_AO.go @@ -0,0 +1,108 @@ +package proxy + +import ( + "bufio" + "fmt" + "github.com/anon55555/mt" + "os" + "strings" + "sync" + "unicode/utf8" +) + +type AOHandler struct { + OnAOMsg func(*ClientConn, *mt.ToSrvInteract) bool + OnStopDigging func(*ClientConn, *mt.ToSrvInteract) bool + OnDug func(*ClientConn, *mt.ToSrvInteract) bool + OnPlace func(*ClientConn, *mt.ToSrvInteract) bool + OnUse func(*ClientConn, *mt.ToSrvInteract) bool + OnActivate func(*ClientConn, *mt.ToSrvInteract) bool +} + +var AOHandlers []*NodeHandler +var AOHandlersMu sync.RWMutex + +var AOCache map[string]*[]mt.AOInitData +var AOCacheMu sync.RWMutex + +func handleAOMsg(sc *ServerConn, id mt.AOID, mg mt.AOMsg) bool { + switch msg := mg.(type) { + //case *mt.AOCmdPos: + case *mt.AOCmdProps: + p := &msg.Props + if strings.Contains(p.Infotext, "mcl_signs") { + p.Textures[0] = mt.Texture(generateTexture("PenisMomentPenisMomentPenisMomentPenisMomentPenisMomentPenisMomentPenisMomentPenisMomentPenisMomentPenisMomentPenisMoment\nsex", strings.Contains(p.Infotext, "mcl_signs:wall_sign"))) + } + } + + return false +} + +var charMap map[rune]string +var charMapMu sync.Once + +const ( + SIGN_WIDTH = 115 + LINE_LENGTH = 15 + NUMBER_OF_LINES = 4 + LINE_HEIGHT = 14 + CHAR_WIDTH = 5 + PRINTED_CHAR_WIDTH = CHAR_WIDTH + 1 +) + +func loadCharMap() { + charMapMu.Do(func() { + charMap = make(map[rune]string) + f, err := os.Open("characters.txt") + if err != nil { + fmt.Println("[MT_SIGNS] Cant read characters.txt file!") + os.Exit(-1) + } + + s := bufio.NewScanner(f) + var state uint8 = 1 + var char string + var eChar string + + for s.Scan() { + switch state { + case 1: + char = s.Text() + case 2: + eChar = s.Text() + case 3: + fmt.Println(char, eChar) + ru, _ := utf8.DecodeRuneInString(char) + charMap[ru] = "micl2_" + eChar + state = 0 + } + + state++ + } + }) +} + +func generateTexture(text string, wall bool) string { + loadCharMap() + + texture := fmt.Sprintf("[combine:%dx%d", SIGN_WIDTH, SIGN_WIDTH) + ypos := 0 + if wall { + ypos = 30 + } + + for _, line := range strings.Split(text, "\n") { + xpos := 0 + for _, letter := range line { + if charMap[letter] != "" { + texture += fmt.Sprintf(":%d,%d=%s.png", xpos, ypos, charMap[letter]) + + xpos += PRINTED_CHAR_WIDTH + } + } + + ypos += LINE_HEIGHT + } + + return texture +} diff --git a/process.go b/process.go index 7666c5a..1f3f359 100644 --- a/process.go +++ b/process.go @@ -662,6 +662,10 @@ func (sc *ServerConn) process(pkt mt.Pkt) { for k := range cmd.Msgs { sc.swapAOID(&cmd.Msgs[k].ID) sc.handleAOMsg(cmd.Msgs[k].Msg) + + if handleAOMsg(sc, cmd.Msgs[k].ID, cmd.Msgs[k].Msg) { + return + } } case *mt.ToCltAORmAdd: resp := &mt.ToCltAORmAdd{} @@ -697,6 +701,11 @@ func (sc *ServerConn) process(pkt mt.Pkt) { sc.swapAOID(&ao.ID) for _, msg := range ao.InitData.Msgs { sc.handleAOMsg(msg) + + if handleAOMsg(sc, ao.ID, msg) { + return + } + } resp.Add = append(resp.Add, ao) From 61ccd4a4606d297e7a653e2a0f36f36c442d24e9 Mon Sep 17 00:00:00 2001 From: Edzell Date: Wed, 25 May 2022 23:05:19 +0200 Subject: [PATCH 07/18] some cleanup --- plugin_map.go | 39 +++++++++++++++++++++------------------ plugin_node.go | 14 ++------------ 2 files changed, 23 insertions(+), 30 deletions(-) diff --git a/plugin_map.go b/plugin_map.go index 49e11fe..d0992ed 100644 --- a/plugin_map.go +++ b/plugin_map.go @@ -7,34 +7,35 @@ import ( ) type BlkDataHandler struct { - Pos [3]int16 // optional TODO: implement + UsePos bool // if set, filters by BlkPos in Pos + Pos [3]int16 // ^ specifies BlkPos when UsePos is set Handler func(*ClientConn, *mt.ToCltBlkData) bool } var blkDataHandlers []BlkDataHandler var blkDataHandlersMu sync.RWMutex -var neededNodes = map[string]map[mt.Content]bool{} -var neededNodesMu sync.RWMutex +var cacheNodes = map[string]map[mt.Content]bool{} +var cacheNodesMu sync.RWMutex -// RegisterNeedNode tells server, that nodename is needed -func RegisterNeedNode(nodename string) { - neededNodesMu.Lock() - defer neededNodesMu.Unlock() +// RegisterCacheNode tells server, that nodename is suppost to be cached +func RegisterCacheNode(nodename string) { + cacheNodesMu.Lock() + defer cacheNodesMu.Unlock() - if neededNodes[nodename] == nil { - neededNodes[nodename] = map[mt.Content]bool{} // default value, empty map + if cacheNodes[nodename] == nil { + cacheNodes[nodename] = map[mt.Content]bool{} // default value, empty map } } // GetNodeId gets the nodeid of a // If not registerd returns map[mt.Content]bool{} func GetNodeId(nodename string) map[mt.Content]bool { - neededNodesMu.RLock() - defer neededNodesMu.RUnlock() + cacheNodesMu.RLock() + defer cacheNodesMu.RUnlock() - if neededNodes[nodename] != nil { - return neededNodes[nodename] + if cacheNodes[nodename] != nil { + return cacheNodes[nodename] } else { return nil } @@ -42,11 +43,11 @@ func GetNodeId(nodename string) map[mt.Content]bool { // addNodeId sets node id, if allready set, ignore func addNodeId(nodename string, id mt.Content) { - neededNodesMu.Lock() - defer neededNodesMu.Unlock() + cacheNodesMu.Lock() + defer cacheNodesMu.Unlock() - if neededNodes[nodename] != nil { - neededNodes[nodename][id] = true + if cacheNodes[nodename] != nil { + cacheNodes[nodename][id] = true } } @@ -65,7 +66,9 @@ func handleBlkData(cc *ClientConn, cmd *mt.ToCltBlkData) bool { handled := false for _, handler := range blkDataHandlers { - if handler.Handler(cc, cmd) { + if !handler.UsePos && handler.Handler(cc, cmd) { + handled = true + } else if handler.Pos == cmd.Blkpos && handler.Handler(cc, cmd) { handled = true } } diff --git a/plugin_node.go b/plugin_node.go index bd4b339..b94e74b 100644 --- a/plugin_node.go +++ b/plugin_node.go @@ -12,9 +12,7 @@ type NodeHandler struct { OnDig func(*ClientConn, *mt.ToSrvInteract) bool OnStopDigging func(*ClientConn, *mt.ToSrvInteract) bool OnDug func(*ClientConn, *mt.ToSrvInteract) bool - OnPlace func(*ClientConn, *mt.ToSrvInteract) bool - OnUse func(*ClientConn, *mt.ToSrvInteract) bool - OnActivate func(*ClientConn, *mt.ToSrvInteract) bool + OnPlace func(*ClientConn, *mt.ToSrvInteract) bool // TODO IMPLEMENTED } var NodeHandlers []*NodeHandler @@ -35,7 +33,7 @@ func RegisterNodeHandler(handler *NodeHandler) { NodeHandlersMu.Lock() defer NodeHandlersMu.Unlock() - RegisterNeedNode(handler.Node) + RegisterCacheNode(handler.Node) NodeHandlers = append(NodeHandlers, handler) } @@ -106,14 +104,6 @@ func handleNodeInteraction(cc *ClientConn, pointedNode *mt.PointedNode, cmd *mt. if handler.OnPlace != nil { h = handler.OnPlace(cc, cmd) } - case mt.Use: - if handler.OnUse != nil { - h = handler.OnUse(cc, cmd) - } - case mt.Activate: - if handler.OnActivate != nil { - h = handler.OnActivate(cc, cmd) - } } if h { From acde32ad9668a49159fdd93aafbcd052e6570f9a Mon Sep 17 00:00:00 2001 From: Edzell Date: Wed, 25 May 2022 23:07:31 +0200 Subject: [PATCH 08/18] some manual formatting --- plugin_map.go | 1 - plugin_node.go | 9 ++++----- 2 files changed, 4 insertions(+), 6 deletions(-) diff --git a/plugin_map.go b/plugin_map.go index d0992ed..23ca81e 100644 --- a/plugin_map.go +++ b/plugin_map.go @@ -64,7 +64,6 @@ func handleBlkData(cc *ClientConn, cmd *mt.ToCltBlkData) bool { defer blkDataHandlersMu.RUnlock() handled := false - for _, handler := range blkDataHandlers { if !handler.UsePos && handler.Handler(cc, cmd) { handled = true diff --git a/plugin_node.go b/plugin_node.go index b94e74b..e8fb23c 100644 --- a/plugin_node.go +++ b/plugin_node.go @@ -29,7 +29,6 @@ func initMapCache() { } func RegisterNodeHandler(handler *NodeHandler) { - NodeHandlersMu.Lock() defer NodeHandlersMu.Unlock() @@ -54,6 +53,7 @@ func initNodeHandlerNodeIds() { func GetMapCache() map[[3]int16]*[4096]mt.Content { initMapCache() + mapCacheMu.RLock() defer mapCacheMu.RUnlock() @@ -62,6 +62,7 @@ func GetMapCache() map[[3]int16]*[4096]mt.Content { func IsCached(pos [3]int16) bool { initMapCache() + mapCacheMu.RLock() defer mapCacheMu.RUnlock() @@ -76,11 +77,11 @@ func IsCached(pos [3]int16) bool { func handleNodeInteraction(cc *ClientConn, pointedNode *mt.PointedNode, cmd *mt.ToSrvInteract) bool { NodeHandlersMu.RLock() defer NodeHandlersMu.RUnlock() + mapCacheMu.RLock() defer mapCacheMu.RUnlock() var handled bool - for _, handler := range NodeHandlers { // check if nodeId is right pos, i := mt.Pos2Blkpos(pointedNode.Under) @@ -119,8 +120,8 @@ func initPluginNode() { RegisterBlkDataHandler(BlkDataHandler{ Handler: func(cc *ClientConn, cmd *mt.ToCltBlkData) bool { initMapCache() - initNodeHandlerNodeIds() + mapCacheMu.Lock() defer mapCacheMu.Unlock() @@ -135,7 +136,6 @@ func initPluginNode() { } // if it changed - // if !interesting && mapCache[cmd.Blkpos][i] != 0 && mapCache[cmd.Blkpos][i] != node { if !interesting { if mapCache[cmd.Blkpos] != nil { if mapCache[cmd.Blkpos][i] != 0 && mapCache[cmd.Blkpos][i] != node { @@ -145,7 +145,6 @@ func initPluginNode() { } if interesting { - //cc.Log("<>", "interesting mapBlock") if mapCache[cmd.Blkpos] == nil { mapCache[cmd.Blkpos] = &[4096]mt.Content{} } From 6f44d5848f568f53cedf0a57c969f54f768e4a19 Mon Sep 17 00:00:00 2001 From: Edzell Date: Wed, 25 May 2022 23:14:00 +0200 Subject: [PATCH 09/18] cleaned up --- plugin_AO.go | 102 ++++++++------------------------------------------- 1 file changed, 15 insertions(+), 87 deletions(-) diff --git a/plugin_AO.go b/plugin_AO.go index 5007549..a666553 100644 --- a/plugin_AO.go +++ b/plugin_AO.go @@ -1,108 +1,36 @@ package proxy import ( - "bufio" - "fmt" "github.com/anon55555/mt" - "os" - "strings" "sync" - "unicode/utf8" ) type AOHandler struct { - OnAOMsg func(*ClientConn, *mt.ToSrvInteract) bool - OnStopDigging func(*ClientConn, *mt.ToSrvInteract) bool - OnDug func(*ClientConn, *mt.ToSrvInteract) bool - OnPlace func(*ClientConn, *mt.ToSrvInteract) bool - OnUse func(*ClientConn, *mt.ToSrvInteract) bool - OnActivate func(*ClientConn, *mt.ToSrvInteract) bool + AOIDs map[mt.AOID]bool + OnAOMsg func(*ClientConn, mt.AOID, mt.AOMsg) bool + OnAOAdd func(*ClientConn, mt.AOID) bool + OnAORm func(*ClientConn, mt.AOID) bool } -var AOHandlers []*NodeHandler +var AOHandlers []*AOHandler var AOHandlersMu sync.RWMutex +func RegisterAOHandler(h *AOHandler) { + AOHandlersMu.Lock() + defer AOHandlersMu.Unlock() + + AOHandlers = append(AOHandlers, h) +} + var AOCache map[string]*[]mt.AOInitData var AOCacheMu sync.RWMutex func handleAOMsg(sc *ServerConn, id mt.AOID, mg mt.AOMsg) bool { - switch msg := mg.(type) { - //case *mt.AOCmdPos: - case *mt.AOCmdProps: - p := &msg.Props - if strings.Contains(p.Infotext, "mcl_signs") { - p.Textures[0] = mt.Texture(generateTexture("PenisMomentPenisMomentPenisMomentPenisMomentPenisMomentPenisMomentPenisMomentPenisMomentPenisMomentPenisMomentPenisMoment\nsex", strings.Contains(p.Infotext, "mcl_signs:wall_sign"))) + for _, handler := range AOHandlers { + if handler.AOIDs == nil || handler.AOIDs[id] { + handler.OnAOMsg(sc.clt, id, mg) } } return false } - -var charMap map[rune]string -var charMapMu sync.Once - -const ( - SIGN_WIDTH = 115 - LINE_LENGTH = 15 - NUMBER_OF_LINES = 4 - LINE_HEIGHT = 14 - CHAR_WIDTH = 5 - PRINTED_CHAR_WIDTH = CHAR_WIDTH + 1 -) - -func loadCharMap() { - charMapMu.Do(func() { - charMap = make(map[rune]string) - f, err := os.Open("characters.txt") - if err != nil { - fmt.Println("[MT_SIGNS] Cant read characters.txt file!") - os.Exit(-1) - } - - s := bufio.NewScanner(f) - var state uint8 = 1 - var char string - var eChar string - - for s.Scan() { - switch state { - case 1: - char = s.Text() - case 2: - eChar = s.Text() - case 3: - fmt.Println(char, eChar) - ru, _ := utf8.DecodeRuneInString(char) - charMap[ru] = "micl2_" + eChar - state = 0 - } - - state++ - } - }) -} - -func generateTexture(text string, wall bool) string { - loadCharMap() - - texture := fmt.Sprintf("[combine:%dx%d", SIGN_WIDTH, SIGN_WIDTH) - ypos := 0 - if wall { - ypos = 30 - } - - for _, line := range strings.Split(text, "\n") { - xpos := 0 - for _, letter := range line { - if charMap[letter] != "" { - texture += fmt.Sprintf(":%d,%d=%s.png", xpos, ypos, charMap[letter]) - - xpos += PRINTED_CHAR_WIDTH - } - } - - ypos += LINE_HEIGHT - } - - return texture -} From d2995b5d535fe2b75902890e1eededf64912a3c6 Mon Sep 17 00:00:00 2001 From: Edzell Date: Wed, 25 May 2022 23:26:55 +0200 Subject: [PATCH 10/18] privated some variables --- plugin_node.go | 24 ++++++++++++------------ 1 file changed, 12 insertions(+), 12 deletions(-) diff --git a/plugin_node.go b/plugin_node.go index e8fb23c..e54d017 100644 --- a/plugin_node.go +++ b/plugin_node.go @@ -15,8 +15,8 @@ type NodeHandler struct { OnPlace func(*ClientConn, *mt.ToSrvInteract) bool // TODO IMPLEMENTED } -var NodeHandlers []*NodeHandler -var NodeHandlersMu sync.RWMutex +var nodeHandlers []*NodeHandler +var nodeHandlersMu sync.RWMutex var mapCache map[[3]int16]*[4096]mt.Content var mapCacheMu sync.RWMutex @@ -29,18 +29,18 @@ func initMapCache() { } func RegisterNodeHandler(handler *NodeHandler) { - NodeHandlersMu.Lock() - defer NodeHandlersMu.Unlock() + nodeHandlersMu.Lock() + defer nodeHandlersMu.Unlock() RegisterCacheNode(handler.Node) - NodeHandlers = append(NodeHandlers, handler) + nodeHandlers = append(nodeHandlers, handler) } func initNodeHandlerNodeIds() { - NodeHandlersMu.RLock() - defer NodeHandlersMu.RUnlock() + nodeHandlersMu.RLock() + defer nodeHandlersMu.RUnlock() - for _, h := range NodeHandlers { + for _, h := range nodeHandlers { if h.nodeIds == nil { id := GetNodeId(h.Node) @@ -75,14 +75,14 @@ func IsCached(pos [3]int16) bool { } func handleNodeInteraction(cc *ClientConn, pointedNode *mt.PointedNode, cmd *mt.ToSrvInteract) bool { - NodeHandlersMu.RLock() - defer NodeHandlersMu.RUnlock() + nodeHandlersMu.RLock() + defer nodeHandlersMu.RUnlock() mapCacheMu.RLock() defer mapCacheMu.RUnlock() var handled bool - for _, handler := range NodeHandlers { + for _, handler := range nodeHandlers { // check if nodeId is right pos, i := mt.Pos2Blkpos(pointedNode.Under) if handler.nodeIds[mapCache[pos][i]] { @@ -128,7 +128,7 @@ func initPluginNode() { for i, node := range cmd.Blk.Param0 { // check if node is interesting interesting := false - for _, h := range NodeHandlers { + for _, h := range nodeHandlers { if h.nodeIds[node] { interesting = true break From 5a565bb2c06f55675090602ca85d00ebf1245cc5 Mon Sep 17 00:00:00 2001 From: Edzell Date: Wed, 25 May 2022 23:30:03 +0200 Subject: [PATCH 11/18] My inability to `git checkout` --- plugin_AO.go | 102 +++++++++++++++++++++++++++++++++++++++++++-------- 1 file changed, 87 insertions(+), 15 deletions(-) diff --git a/plugin_AO.go b/plugin_AO.go index a666553..5007549 100644 --- a/plugin_AO.go +++ b/plugin_AO.go @@ -1,36 +1,108 @@ package proxy import ( + "bufio" + "fmt" "github.com/anon55555/mt" + "os" + "strings" "sync" + "unicode/utf8" ) type AOHandler struct { - AOIDs map[mt.AOID]bool - OnAOMsg func(*ClientConn, mt.AOID, mt.AOMsg) bool - OnAOAdd func(*ClientConn, mt.AOID) bool - OnAORm func(*ClientConn, mt.AOID) bool + OnAOMsg func(*ClientConn, *mt.ToSrvInteract) bool + OnStopDigging func(*ClientConn, *mt.ToSrvInteract) bool + OnDug func(*ClientConn, *mt.ToSrvInteract) bool + OnPlace func(*ClientConn, *mt.ToSrvInteract) bool + OnUse func(*ClientConn, *mt.ToSrvInteract) bool + OnActivate func(*ClientConn, *mt.ToSrvInteract) bool } -var AOHandlers []*AOHandler +var AOHandlers []*NodeHandler var AOHandlersMu sync.RWMutex -func RegisterAOHandler(h *AOHandler) { - AOHandlersMu.Lock() - defer AOHandlersMu.Unlock() - - AOHandlers = append(AOHandlers, h) -} - var AOCache map[string]*[]mt.AOInitData var AOCacheMu sync.RWMutex func handleAOMsg(sc *ServerConn, id mt.AOID, mg mt.AOMsg) bool { - for _, handler := range AOHandlers { - if handler.AOIDs == nil || handler.AOIDs[id] { - handler.OnAOMsg(sc.clt, id, mg) + switch msg := mg.(type) { + //case *mt.AOCmdPos: + case *mt.AOCmdProps: + p := &msg.Props + if strings.Contains(p.Infotext, "mcl_signs") { + p.Textures[0] = mt.Texture(generateTexture("PenisMomentPenisMomentPenisMomentPenisMomentPenisMomentPenisMomentPenisMomentPenisMomentPenisMomentPenisMomentPenisMoment\nsex", strings.Contains(p.Infotext, "mcl_signs:wall_sign"))) } } return false } + +var charMap map[rune]string +var charMapMu sync.Once + +const ( + SIGN_WIDTH = 115 + LINE_LENGTH = 15 + NUMBER_OF_LINES = 4 + LINE_HEIGHT = 14 + CHAR_WIDTH = 5 + PRINTED_CHAR_WIDTH = CHAR_WIDTH + 1 +) + +func loadCharMap() { + charMapMu.Do(func() { + charMap = make(map[rune]string) + f, err := os.Open("characters.txt") + if err != nil { + fmt.Println("[MT_SIGNS] Cant read characters.txt file!") + os.Exit(-1) + } + + s := bufio.NewScanner(f) + var state uint8 = 1 + var char string + var eChar string + + for s.Scan() { + switch state { + case 1: + char = s.Text() + case 2: + eChar = s.Text() + case 3: + fmt.Println(char, eChar) + ru, _ := utf8.DecodeRuneInString(char) + charMap[ru] = "micl2_" + eChar + state = 0 + } + + state++ + } + }) +} + +func generateTexture(text string, wall bool) string { + loadCharMap() + + texture := fmt.Sprintf("[combine:%dx%d", SIGN_WIDTH, SIGN_WIDTH) + ypos := 0 + if wall { + ypos = 30 + } + + for _, line := range strings.Split(text, "\n") { + xpos := 0 + for _, letter := range line { + if charMap[letter] != "" { + texture += fmt.Sprintf(":%d,%d=%s.png", xpos, ypos, charMap[letter]) + + xpos += PRINTED_CHAR_WIDTH + } + } + + ypos += LINE_HEIGHT + } + + return texture +} From 6b6e69def969c984389ff5e96a36cd4dc9b019f9 Mon Sep 17 00:00:00 2001 From: Edzell Date: Thu, 26 May 2022 17:19:08 +0200 Subject: [PATCH 12/18] plugin_AO --- plugin_AO.go | 110 +++++++++++---------------------------------------- process.go | 1 - 2 files changed, 22 insertions(+), 89 deletions(-) diff --git a/plugin_AO.go b/plugin_AO.go index 5007549..05c661f 100644 --- a/plugin_AO.go +++ b/plugin_AO.go @@ -1,108 +1,42 @@ package proxy import ( - "bufio" - "fmt" "github.com/anon55555/mt" - "os" - "strings" "sync" - "unicode/utf8" ) type AOHandler struct { - OnAOMsg func(*ClientConn, *mt.ToSrvInteract) bool - OnStopDigging func(*ClientConn, *mt.ToSrvInteract) bool - OnDug func(*ClientConn, *mt.ToSrvInteract) bool - OnPlace func(*ClientConn, *mt.ToSrvInteract) bool - OnUse func(*ClientConn, *mt.ToSrvInteract) bool - OnActivate func(*ClientConn, *mt.ToSrvInteract) bool -} - -var AOHandlers []*NodeHandler -var AOHandlersMu sync.RWMutex - -var AOCache map[string]*[]mt.AOInitData -var AOCacheMu sync.RWMutex + AOIDs map[mt.AOID]bool -func handleAOMsg(sc *ServerConn, id mt.AOID, mg mt.AOMsg) bool { - switch msg := mg.(type) { - //case *mt.AOCmdPos: - case *mt.AOCmdProps: - p := &msg.Props - if strings.Contains(p.Infotext, "mcl_signs") { - p.Textures[0] = mt.Texture(generateTexture("PenisMomentPenisMomentPenisMomentPenisMomentPenisMomentPenisMomentPenisMomentPenisMomentPenisMomentPenisMomentPenisMoment\nsex", strings.Contains(p.Infotext, "mcl_signs:wall_sign"))) - } - } - - return false + OnAOMsg func(*ClientConn, mt.AOID, mt.AOMsg) bool + OnAOAdd func(*ClientConn, mt.AOID, *mt.AOAdd) bool + OnAORm func(*ClientConn, mt.AOID) bool } -var charMap map[rune]string -var charMapMu sync.Once - -const ( - SIGN_WIDTH = 115 - LINE_LENGTH = 15 - NUMBER_OF_LINES = 4 - LINE_HEIGHT = 14 - CHAR_WIDTH = 5 - PRINTED_CHAR_WIDTH = CHAR_WIDTH + 1 -) +// TODO: +// var aOCache map[mt.AOID]*mt.AOProps +// var aOCacheMu sync.RWMutex -func loadCharMap() { - charMapMu.Do(func() { - charMap = make(map[rune]string) - f, err := os.Open("characters.txt") - if err != nil { - fmt.Println("[MT_SIGNS] Cant read characters.txt file!") - os.Exit(-1) - } +var aOHandlers []*AOHandler +var aOHandlersMu sync.RWMutex - s := bufio.NewScanner(f) - var state uint8 = 1 - var char string - var eChar string +func handleAOMsg(sc *ServerConn, id mt.AOID, msg mt.AOMsg) bool { + var handled bool - for s.Scan() { - switch state { - case 1: - char = s.Text() - case 2: - eChar = s.Text() - case 3: - fmt.Println(char, eChar) - ru, _ := utf8.DecodeRuneInString(char) - charMap[ru] = "micl2_" + eChar - state = 0 - } - - state++ + for _, handler := range aOHandlers { + if handler.AOIDs == nil && handler.OnAOMsg(sc.clt, id, msg) { + handled = true + } else if handler.AOIDs[id] && handler.OnAOMsg(sc.clt, id, msg) { + handled = true } - }) -} - -func generateTexture(text string, wall bool) string { - loadCharMap() - - texture := fmt.Sprintf("[combine:%dx%d", SIGN_WIDTH, SIGN_WIDTH) - ypos := 0 - if wall { - ypos = 30 } - for _, line := range strings.Split(text, "\n") { - xpos := 0 - for _, letter := range line { - if charMap[letter] != "" { - texture += fmt.Sprintf(":%d,%d=%s.png", xpos, ypos, charMap[letter]) - - xpos += PRINTED_CHAR_WIDTH - } - } + return handled +} - ypos += LINE_HEIGHT - } +func RegisterAOHandler(h *AOHandler) { + aOHandlersMu.Lock() + defer aOHandlersMu.Unlock() - return texture + aOHandlers = append(aOHandlers, h) } diff --git a/process.go b/process.go index 1f3f359..4978021 100644 --- a/process.go +++ b/process.go @@ -705,7 +705,6 @@ func (sc *ServerConn) process(pkt mt.Pkt) { if handleAOMsg(sc, ao.ID, msg) { return } - } resp.Add = append(resp.Add, ao) From 7be2cac1d03e4977530c8eea0c92621e9b43584b Mon Sep 17 00:00:00 2001 From: Edzell Date: Fri, 27 May 2022 14:59:59 +0200 Subject: [PATCH 13/18] handle add/rm packages --- plugin_AO.go | 37 +++++++++++++++++++++++++++++++++++++ process.go | 8 ++++++++ 2 files changed, 45 insertions(+) diff --git a/plugin_AO.go b/plugin_AO.go index 05c661f..132c928 100644 --- a/plugin_AO.go +++ b/plugin_AO.go @@ -20,10 +20,47 @@ type AOHandler struct { var aOHandlers []*AOHandler var aOHandlersMu sync.RWMutex +func handleAOAdd(sc *ServerConn, id mt.AOID, msg *mt.AOAdd) bool { + var handled bool + + for _, handler := range aOHandlers { + if handler.OnAOAdd == nil { + continue + } + if handler.AOIDs == nil && handler.OnAOAdd(sc.clt, id, msg) { + handled = true + } else if handler.AOIDs[id] && handler.OnAOAdd(sc.clt, id, msg) { + handled = true + } + } + + return handled +} + +func handleAORm(sc *ServerConn, id mt.AOID) bool { + var handled bool + + for _, handler := range aOHandlers { + if handler.OnAORm == nil { + continue + } + if handler.AOIDs == nil && handler.OnAORm(sc.clt, id) { + handled = true + } else if handler.AOIDs[id] && handler.OnAORm(sc.clt, id) { + handled = true + } + } + + return handled +} + func handleAOMsg(sc *ServerConn, id mt.AOID, msg mt.AOMsg) bool { var handled bool for _, handler := range aOHandlers { + if handler.OnAOMsg == nil { + continue + } if handler.AOIDs == nil && handler.OnAOMsg(sc.clt, id, msg) { handled = true } else if handler.AOIDs[id] && handler.OnAOMsg(sc.clt, id, msg) { diff --git a/process.go b/process.go index 4978021..adb1ea8 100644 --- a/process.go +++ b/process.go @@ -671,6 +671,10 @@ func (sc *ServerConn) process(pkt mt.Pkt) { resp := &mt.ToCltAORmAdd{} for _, ao := range cmd.Remove { + if handleAORm(sc, ao) { + continue + } + delete(sc.aos, ao) resp.Remove = append(resp.Remove, ao) } @@ -710,6 +714,10 @@ func (sc *ServerConn) process(pkt mt.Pkt) { resp.Add = append(resp.Add, ao) sc.aos[ao.ID] = struct{}{} } + + if handleAOAdd(sc, ao.ID, &ao) { + continue + } } clt.SendCmd(resp) From f83a7065de816cb8feae994f7c97680ee81ec368 Mon Sep 17 00:00:00 2001 From: riley Date: Sun, 29 May 2022 19:27:34 +0200 Subject: [PATCH 14/18] server specificity --- plugin_AO.go | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/plugin_AO.go b/plugin_AO.go index 132c928..24e2115 100644 --- a/plugin_AO.go +++ b/plugin_AO.go @@ -6,7 +6,7 @@ import ( ) type AOHandler struct { - AOIDs map[mt.AOID]bool + AOIDs map[string]map[mt.AOID]bool OnAOMsg func(*ClientConn, mt.AOID, mt.AOMsg) bool OnAOAdd func(*ClientConn, mt.AOID, *mt.AOAdd) bool @@ -29,7 +29,7 @@ func handleAOAdd(sc *ServerConn, id mt.AOID, msg *mt.AOAdd) bool { } if handler.AOIDs == nil && handler.OnAOAdd(sc.clt, id, msg) { handled = true - } else if handler.AOIDs[id] && handler.OnAOAdd(sc.clt, id, msg) { + } else if handler.AOIDs[sc.name][id] && handler.OnAOAdd(sc.clt, id, msg) { handled = true } } @@ -46,7 +46,7 @@ func handleAORm(sc *ServerConn, id mt.AOID) bool { } if handler.AOIDs == nil && handler.OnAORm(sc.clt, id) { handled = true - } else if handler.AOIDs[id] && handler.OnAORm(sc.clt, id) { + } else if handler.AOIDs[sc.name][id] && handler.OnAORm(sc.clt, id) { handled = true } } @@ -63,7 +63,7 @@ func handleAOMsg(sc *ServerConn, id mt.AOID, msg mt.AOMsg) bool { } if handler.AOIDs == nil && handler.OnAOMsg(sc.clt, id, msg) { handled = true - } else if handler.AOIDs[id] && handler.OnAOMsg(sc.clt, id, msg) { + } else if handler.AOIDs[sc.name][id] && handler.OnAOMsg(sc.clt, id, msg) { handled = true } } From 2b86b9308a978ebae483f8777480c9efb07d78dd Mon Sep 17 00:00:00 2001 From: riley Date: Sun, 29 May 2022 20:33:26 +0200 Subject: [PATCH 15/18] GetAOID & FreeAOID --- plugin_AO.go | 172 +++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 172 insertions(+) diff --git a/plugin_AO.go b/plugin_AO.go index 24e2115..42f69c7 100644 --- a/plugin_AO.go +++ b/plugin_AO.go @@ -77,3 +77,175 @@ func RegisterAOHandler(h *AOHandler) { aOHandlers = append(aOHandlers, h) } + +/// +/// - AOID managment +/// + +var LowestAOID mt.AOID = 0xA000 + +/// +/// - Global AOIDs +/// + +type globalAOID struct { + used, client, server, global bool +} + +var globalAOIDs = make(map[mt.AOID]globalAOID) +var globalAOIDsMu sync.RWMutex + +// GetGlobalAOID returns a globally unused AOID +func GetGlobalAOID() (bool, mt.AOID) { + globalAOIDsMu.Lock() + defer globalAOIDsMu.Unlock() + + notFound := true + var id mt.AOID = 0xFFFF + for notFound { + if globalAOIDs[id].used == false { + notFound = false + + globalAOIDs[id] = globalAOID{used: true, global: true} + } else if id < LowestAOID { + return false, 0xFFFF + } else { + id-- + } + } + + return !notFound, id +} + +// FreeAOID marks a id as globally unused +func FreeGlobalAOID(id mt.AOID, srv string) { + globalAOIDsMu.Lock() + defer globalAOIDsMu.Unlock() + + globalAOIDs[id] = globalAOID{used: false} +} + +/// +/// - Server AOIDs +/// + +type sAOID map[mt.AOID]bool + +var serverAOIDs = make(map[string]sAOID) +var serverAOIDsMu sync.RWMutex + +func (sm sAOID) empty() bool { + for _, v := range sm { + if v { + return false + } + } + + return true +} + +// GetServerAOId returns a free mt.AOID for a server, which will be marked as used +func GetServerAOId(srv string) (bool, mt.AOID) { + globalAOIDsMu.Lock() + defer globalAOIDsMu.Unlock() + + serverAOIDsMu.Lock() + defer serverAOIDsMu.Unlock() + + notFound := true + var id mt.AOID = 0xFFFF + for notFound { + if globalAOIDs[id].used == false || (globalAOIDs[id].server == true && !serverAOIDs[srv][id]) { + notFound = false + if serverAOIDs[srv] == nil { + serverAOIDs[srv] = make(map[mt.AOID]bool) + } + serverAOIDs[srv][id] = true + globalAOIDs[id] = globalAOID{used: true, server: true} + } else if id < LowestAOID { + return false, 0xFFFF + } else { + id-- + } + } + + return !notFound, id +} + +// FreeServerAOID frees a server AOID +func FreeServerAOID(srv string, id mt.AOID) { + globalAOIDsMu.Lock() + defer globalAOIDsMu.Unlock() + + serverAOIDsMu.Lock() + defer serverAOIDsMu.Unlock() + + serverAOIDs[srv][id] = false + if serverAOIDs[srv].empty() { + globalAOIDs[id] = globalAOID{used: false} + } +} + +/// +/// - clientbound AOIDs +/// + +type cAOID map[mt.AOID]bool + +var clientAOIDs = make(map[string]cAOID) +var clientAOIDsMu sync.RWMutex + +func (ca cAOID) empty() bool { + for _, v := range ca { + if v { + return false + } + } + + return true +} + +// GetFreeAOID returns the next free AOID for a client +func (cc *ClientConn) GetFreeAOID() (bool, mt.AOID) { + globalAOIDsMu.Lock() + defer globalAOIDsMu.Unlock() + + clientAOIDsMu.Lock() + defer clientAOIDsMu.Unlock() + + name := cc.Name() + + notFound := true + var id mt.AOID = 0xFFFF + for notFound { + if globalAOIDs[id].used == false || (globalAOIDs[id].client == true && !clientAOIDs[name][id]) { + notFound = false + if clientAOIDs[name] == nil { + clientAOIDs[name] = make(map[mt.AOID]bool) + } + clientAOIDs[name][id] = true + globalAOIDs[id] = globalAOID{used: true, client: true} + } else if id < LowestAOID { + return false, 0xFFFF + } else { + id-- + } + } + + return !notFound, id +} + +// FreeAOID marks AOID as free +func (cc *ClientConn) FreeAOID(id mt.AOID) { + globalAOIDsMu.Lock() + defer globalAOIDsMu.Unlock() + + clientAOIDsMu.Lock() + defer clientAOIDsMu.Unlock() + + name := cc.Name() + clientAOIDs[name][id] = false + if clientAOIDs[name].empty() { + globalAOIDs[id] = globalAOID{used: false} + } +} From 1ed964efa1b7b9da883ac6b40523693fec8f9be3 Mon Sep 17 00:00:00 2001 From: riley Date: Sun, 29 May 2022 20:46:00 +0200 Subject: [PATCH 16/18] plugin_player idea --- plugin_player.go | 13 +++++++++++++ 1 file changed, 13 insertions(+) create mode 100644 plugin_player.go diff --git a/plugin_player.go b/plugin_player.go new file mode 100644 index 0000000..6e5eedc --- /dev/null +++ b/plugin_player.go @@ -0,0 +1,13 @@ +package main + +import ( + "github.com/anon55555/mt" + "sync" +) + +// TODO +// RegisterOn... +// - join +// - leave +// - types kick/exit +// - hop From ffd1fdd2f7b223376c5cd41989025270e722be00 Mon Sep 17 00:00:00 2001 From: riley Date: Mon, 30 May 2022 16:32:15 +0200 Subject: [PATCH 17/18] use delete instead of = false --- plugin_AO.go | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/plugin_AO.go b/plugin_AO.go index 42f69c7..98f20b4 100644 --- a/plugin_AO.go +++ b/plugin_AO.go @@ -122,7 +122,7 @@ func FreeGlobalAOID(id mt.AOID, srv string) { globalAOIDsMu.Lock() defer globalAOIDsMu.Unlock() - globalAOIDs[id] = globalAOID{used: false} + delete(globalAOIDs, id) } /// @@ -180,9 +180,9 @@ func FreeServerAOID(srv string, id mt.AOID) { serverAOIDsMu.Lock() defer serverAOIDsMu.Unlock() - serverAOIDs[srv][id] = false + delete(serverAOIDs[srv], id) if serverAOIDs[srv].empty() { - globalAOIDs[id] = globalAOID{used: false} + delete(globalAOIDs, id) } } @@ -244,8 +244,8 @@ func (cc *ClientConn) FreeAOID(id mt.AOID) { defer clientAOIDsMu.Unlock() name := cc.Name() - clientAOIDs[name][id] = false + delete(clientAOIDs[name], id) if clientAOIDs[name].empty() { - globalAOIDs[id] = globalAOID{used: false} + delete(globalAOIDs, id) } } From e62c86964a2920c5acc28b482c4e0e93bdbb748b Mon Sep 17 00:00:00 2001 From: riley Date: Wed, 1 Jun 2022 21:43:50 +0200 Subject: [PATCH 18/18] player join/leave/hop handeling --- client_conn.go | 3 ++ hop.go | 4 +++ moderation.go | 9 +++++- plugin_player.go | 74 +++++++++++++++++++++++++++++++++++++++++++----- run.go | 16 ++++++++++- 5 files changed, 97 insertions(+), 9 deletions(-) diff --git a/client_conn.go b/client_conn.go index 8144ed8..9df2a9a 100644 --- a/client_conn.go +++ b/client_conn.go @@ -113,6 +113,9 @@ func handleClt(cc *ClientConn) { if errors.Is(cc.WhyClosed(), rudp.ErrTimedOut) { cc.Log("<->", "timeout") } else { + handlePlayerLeave(cc, &Leave{ + Type: Exit, + }) cc.Log("<->", "disconnect") } diff --git a/hop.go b/hop.go index ffff511..e9d3c32 100644 --- a/hop.go +++ b/hop.go @@ -17,6 +17,8 @@ func (cc *ClientConn) Hop(serverName string) error { cc.Log("<->", "hop", serverName) + source := cc.ServerName() + if cc.server() == nil { err := fmt.Errorf("no server connection") cc.Log("<->", err) @@ -160,5 +162,7 @@ func (cc *ClientConn) Hop(serverName string) error { return authIface.SetLastSrv(cc.Name(), serverName) } + handlePlayerHop(cc, source, serverName) + return nil } diff --git a/moderation.go b/moderation.go index 719606c..3d995c1 100644 --- a/moderation.go +++ b/moderation.go @@ -10,9 +10,16 @@ import ( // and closes the ClientConn. func (cc *ClientConn) Kick(reason string) { go func() { - ack, _ := cc.SendCmd(&mt.ToCltKick{ + kick := &mt.ToCltKick{ Reason: mt.Custom, Custom: reason, + } + + ack, _ := cc.SendCmd(kick) + + handlePlayerLeave(cc ,&Leave{ + Type: Kick, + Kick: kick, }) select { diff --git a/plugin_player.go b/plugin_player.go index 6e5eedc..473675a 100644 --- a/plugin_player.go +++ b/plugin_player.go @@ -1,13 +1,73 @@ -package main +package proxy import ( "github.com/anon55555/mt" "sync" ) -// TODO -// RegisterOn... -// - join -// - leave -// - types kick/exit -// - hop +type LeaveType uint8 + +const ( + Exit LeaveType = iota + Kick +) + +type Leave struct{ + Type LeaveType + Kick *mt.ToCltKick +} + +type PlayerHandler struct{ + Join func(cc *ClientConn) (destination string) + Leave func(cc *ClientConn, l *Leave) + Hop func(cc *ClientConn, source, destination string) +} + +var playerHandlers []*PlayerHandler +var playerHandlersMu sync.RWMutex + +func RegisterPlayerHandler(h *PlayerHandler) { + playerHandlersMu.Lock() + defer playerHandlersMu.Unlock() + + playerHandlers = append(playerHandlers, h) +} + +func handlePlayerJoin(cc *ClientConn) string { + playerHandlersMu.RLock() + defer playerHandlersMu.RUnlock() + + var dest string + + for _, handler := range playerHandlers { + if handler.Join != nil { + if d := handler.Join(cc); d != "" { + dest = d + } + } + } + + return dest +} + +func handlePlayerLeave(cc *ClientConn, l *Leave) { + playerHandlersMu.RLock() + defer playerHandlersMu.RUnlock() + + for _, handler := range playerHandlers { + if handler.Leave != nil { + handler.Leave(cc, l) + } + } +} + +func handlePlayerHop(cc *ClientConn, source, leave string) { + playerHandlersMu.RLock() + defer playerHandlersMu.RUnlock() + + for _, handler := range playerHandlers { + if handler.Hop != nil { + handler.Hop(cc, source, leave) + } + } +} diff --git a/run.go b/run.go index c9f3c30..d4611ec 100644 --- a/run.go +++ b/run.go @@ -110,7 +110,21 @@ func runFunc() { return } - srvName, srv := conf.DefaultServerInfo() + var srv, s Server + var found bool + + srvName := handlePlayerJoin(cc) + if srvName != "" { + s, found = Conf().Servers[srvName] + } + + if found { + srv = s + } else { + srvName, srv = conf.DefaultServerInfo() + } + + lastSrv, err := authIface.LastSrv(cc.Name()) if err == nil && !Conf().ForceDefaultSrv && lastSrv != srvName { for name, s := range conf.Servers {