multiserver/blockdata.go

82 lines
1.6 KiB
Go
Raw Normal View History

2021-02-09 23:35:14 -08:00
package main
import (
"bytes"
"compress/zlib"
"encoding/binary"
"io"
"github.com/anon55555/mt/rudp"
)
const NodeCount = 16 * 16 * 16
func processBlockdata(p *Peer, pkt *rudp.Pkt) bool {
srv := p.ServerName()
2021-03-10 10:02:09 -08:00
x := int16(binary.BigEndian.Uint16(pkt.Data[2:4]))
y := int16(binary.BigEndian.Uint16(pkt.Data[4:6]))
z := int16(binary.BigEndian.Uint16(pkt.Data[6:8]))
p.blocks = append(p.blocks, [3]int16{x, y, z})
2021-02-21 23:15:49 -08:00
r := bytes.NewReader(pkt.Data[13:])
2021-02-09 23:35:14 -08:00
2021-02-21 23:15:49 -08:00
zr, err := zlib.NewReader(r)
2021-02-09 23:35:14 -08:00
if err != nil {
return true
}
buf := &bytes.Buffer{}
_, err = io.Copy(buf, zr)
if err != nil {
return true
}
zr.Close()
nodes := buf.Bytes()
for i := uint32(0); i < NodeCount; i++ {
contentID := binary.BigEndian.Uint16(nodes[2*i : 2+2*i])
if contentID >= ContentUnknown && contentID <= ContentIgnore {
continue
}
2021-03-14 01:54:17 -08:00
newID := NodeDefs()[srv][contentID].ID()
2021-02-09 23:35:14 -08:00
binary.BigEndian.PutUint16(nodes[2*i:2+2*i], newID)
}
var recompBuf bytes.Buffer
zw := zlib.NewWriter(&recompBuf)
zw.Write(nodes)
zw.Close()
recompNodes := recompBuf.Bytes()
2021-02-21 23:15:49 -08:00
meta := make([]byte, 65536)
n, err := r.Read(meta)
if err != nil {
return true
}
meta = meta[:n]
data := make([]byte, 13+len(recompNodes)+len(meta))
2021-02-10 22:40:42 -08:00
copy(data[:13], pkt.Data[:13])
copy(data[13:13+len(recompNodes)], recompNodes)
2021-02-21 23:15:49 -08:00
copy(data[13+len(recompNodes):], meta)
2021-02-09 23:35:14 -08:00
2021-02-10 22:40:42 -08:00
pkt.Data = data
2021-02-09 23:35:14 -08:00
2021-02-10 22:40:42 -08:00
return false
}
func processAddnode(p *Peer, pkt *rudp.Pkt) bool {
srv := p.ServerName()
contentID := binary.BigEndian.Uint16(pkt.Data[8:10])
2021-03-14 01:54:17 -08:00
newID := NodeDefs()[srv][contentID].ID()
2021-02-10 22:40:42 -08:00
binary.BigEndian.PutUint16(pkt.Data[8:10], newID)
2021-02-09 23:35:14 -08:00
return false
}