multiserver/blockdata.go

92 lines
1.7 KiB
Go
Raw Permalink Normal View History

2021-02-09 23:35:14 -08:00
package main
import (
"bytes"
"compress/zlib"
"encoding/binary"
"io"
)
const NodeCount = 16 * 16 * 16
2021-03-29 09:57:30 -07:00
func processBlockdata(c *Conn, r *bytes.Reader) ([]byte, bool) {
srv := c.ServerName()
posData := make([]byte, 6)
r.Read(posData)
2021-02-09 23:35:14 -08:00
2021-03-29 09:57:30 -07:00
x := int16(binary.BigEndian.Uint16(posData[0:2]))
y := int16(binary.BigEndian.Uint16(posData[2:4]))
z := int16(binary.BigEndian.Uint16(posData[4:6]))
2021-03-10 10:02:09 -08:00
2021-03-29 09:57:30 -07:00
c.blocks = append(c.blocks, [3]int16{x, y, z})
2021-03-10 10:02:09 -08:00
2021-03-29 09:57:30 -07:00
r.Seek(13, io.SeekStart)
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 {
2021-03-29 09:57:30 -07:00
return nil, true
2021-02-09 23:35:14 -08:00
}
buf := &bytes.Buffer{}
_, err = io.Copy(buf, zr)
if err != nil {
2021-03-29 09:57:30 -07:00
return nil, true
2021-02-09 23:35:14 -08:00
}
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-03-29 09:57:30 -07:00
meta := make([]byte, r.Len())
r.Read(meta)
2021-02-21 23:15:49 -08:00
2021-03-29 09:57:30 -07:00
r.Seek(2, io.SeekStart)
2021-02-21 23:15:49 -08:00
2021-03-29 09:57:30 -07:00
blockMeta := make([]byte, 11)
r.Read(blockMeta)
2021-02-09 23:35:14 -08:00
2021-03-29 09:57:30 -07:00
data := make([]byte, 11+len(recompNodes)+len(meta))
copy(data[:11], blockMeta)
copy(data[11:11+len(recompNodes)], recompNodes)
copy(data[11+len(recompNodes):], meta)
2021-02-09 23:35:14 -08:00
2021-03-29 09:57:30 -07:00
return data, false
2021-02-10 22:40:42 -08:00
}
2021-03-29 09:57:30 -07:00
func processAddnode(c *Conn, r *bytes.Reader) []byte {
srv := c.ServerName()
r.Seek(8, io.SeekStart)
2021-02-10 22:40:42 -08:00
2021-03-29 09:57:30 -07:00
idBytes := make([]byte, 2)
r.Read(idBytes)
contentID := binary.BigEndian.Uint16(idBytes)
2021-03-14 01:54:17 -08:00
newID := NodeDefs()[srv][contentID].ID()
2021-02-09 23:35:14 -08:00
2021-03-29 09:57:30 -07:00
r.Seek(2, io.SeekStart)
data := make([]byte, r.Len())
r.Read(data)
binary.BigEndian.PutUint16(data[6:8], newID)
return data
2021-02-09 23:35:14 -08:00
}