Skip to content

Commit

Permalink
command namespaces
Browse files Browse the repository at this point in the history
  • Loading branch information
oq-x committed Aug 20, 2024
1 parent b0eeaae commit 173b502
Show file tree
Hide file tree
Showing 13 changed files with 166 additions and 202 deletions.
5 changes: 3 additions & 2 deletions core_commands/debug.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,9 @@ import (
)

var debug = command.Command{
Node: command.NewCommand("debug"),
Aliases: []string{"f3"},
Node: command.NewCommand("debug"),
Aliases: []string{"f3"},
Namespace: "zeppelin",
Callback: func(ccc command.CommandCallContext) {
player := ccc.Executor.Player()
if player == nil {
Expand Down
3 changes: 2 additions & 1 deletion core_commands/mem.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@ import (
var memStats runtime.MemStats

var mem = command.Command{
Node: command.NewCommand("mem"),
Node: command.NewCommand("mem"),
Namespace: "zeppelin",
Callback: func(ccc command.CommandCallContext) {
runtime.ReadMemStats(&memStats)
loaded := ccc.Server.(*server.Server).World.LoadedChunks()
Expand Down
7 changes: 3 additions & 4 deletions net/conn.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ import (
"sync/atomic"
"unicode/utf16"

"github.com/zeppelinmc/zeppelin/log"
"github.com/zeppelinmc/zeppelin/net/cfb8"
"github.com/zeppelinmc/zeppelin/net/io"
"github.com/zeppelinmc/zeppelin/net/io/compress"
Expand Down Expand Up @@ -130,7 +129,7 @@ func (conn *Conn) WritePacket(pk packet.Packet) error {

dataLength := io.AppendVarInt(nil, int32(packetBuf.Len()))

compressedPacket, err := compress.CompressZlib(packetBuf, packetBuf.Len(), &MaxCompressedPacketSize)
compressedPacket, err := compress.CompressZlib(packetBuf.Bytes(), &MaxCompressedPacketSize)
if err != nil {
return err
}
Expand Down Expand Up @@ -245,7 +244,7 @@ func (conn *Conn) ReadPacket() (packet.Packet, error) {
rd = io.NewReader(bytes.NewReader(packet), int(length))
}
} else { //packet is compressed
length = dataLength
/*length = dataLength
compressedLength := packetLength - int32(dataLengthSize)
var ilength = int(length)
Expand All @@ -263,7 +262,7 @@ func (conn *Conn) ReadPacket() (packet.Packet, error) {
uncompressedPacket = data
length = int32(len(data))
rd = io.NewReader(bytes.NewReader(uncompressedPacket), int(length))
rd = io.NewReader(bytes.NewReader(uncompressedPacket), int(length))*/
}
}

Expand Down
38 changes: 4 additions & 34 deletions net/io/compress/gzip.go
Original file line number Diff line number Diff line change
@@ -1,28 +1,11 @@
package compress

import (
"bytes"
"io"

"github.com/4kills/go-libdeflate/v2"
"github.com/zeppelinmc/zeppelin/net/io/buffers"
)

// Decompress gunzip. The data returned is only safe to use until the next operation
func DecompressGzip(data io.Reader, compressedLength int, decompressedLength *int) ([]byte, error) {
var compressedBuffer = buffers.Buffers.Get().(*bytes.Buffer)
defer buffers.Buffers.Put(compressedBuffer)
compressedBuffer.Reset()
if compressedBuffer.Len() < compressedLength {
compressedBuffer.Grow(compressedLength)
}

if _, err := data.Read(compressedBuffer.Bytes()[:compressedLength]); err != nil {
return nil, err
}

compressed := compressedBuffer.Bytes()[:compressedLength]

func DecompressGzip(compressed []byte, decompressedLength *int) ([]byte, error) {
dc := decompressors.Get().(libdeflate.Decompressor)
defer decompressors.Put(dc)

Expand All @@ -44,20 +27,7 @@ func DecompressGzip(data io.Reader, compressedLength int, decompressedLength *in

// Compresses gunzip. If compressedLength is provided, data returned will only be safe to use until the next operation.
// It is recommmended to provide the compressed length to avoid allocation. If you don't know it, provide a number likely bigger than the compressed length.
func CompressGzip(decompressedData io.Reader, decompressedLength int, compressedLength *int) (compressed []byte, err error) {
var decompressedBuffer = buffers.Buffers.Get().(*bytes.Buffer)
defer buffers.Buffers.Put(decompressedBuffer)
decompressedBuffer.Reset()
if decompressedBuffer.Len() < decompressedLength {
decompressedBuffer.Grow(decompressedLength)
}

if _, err := decompressedData.Read(decompressedBuffer.Bytes()[:decompressedLength]); err != nil {
return nil, err
}

decompressed := decompressedBuffer.Bytes()[:decompressedLength]

func CompressGzip(decompressedData []byte, compressedLength *int) (compressed []byte, err error) {
c := compressors.Get().(libdeflate.Compressor)
defer compressors.Put(c)

Expand All @@ -68,11 +38,11 @@ func CompressGzip(decompressedData io.Reader, decompressedLength int, compressed
}
defer bufs.Put(dst)

_, compressedResult, err := c.Compress(decompressed, dst[:*compressedLength], libdeflate.ModeGzip)
_, compressedResult, err := c.Compress(decompressedData, dst[:*compressedLength], libdeflate.ModeGzip)

return compressedResult, err
} else {
_, compressedResult, err := c.Compress(decompressed, nil, libdeflate.ModeGzip)
_, compressedResult, err := c.Compress(decompressedData, nil, libdeflate.ModeGzip)

return compressedResult, err
}
Expand Down
38 changes: 4 additions & 34 deletions net/io/compress/zlib.go
Original file line number Diff line number Diff line change
@@ -1,29 +1,12 @@
package compress

import (
"bytes"
"io"

"github.com/4kills/go-libdeflate/v2"
"github.com/zeppelinmc/zeppelin/net/io/buffers"
)

// Decompress zlib. If decompressedLength is provided, the data returned will only be safe to use until the next operation
// It is recommmended to provide the decompressed length to avoid allocation. If you don't know it, provide a number likely bigger than the decompressed length.
func DecompressZlib(data io.Reader, compressedLength int, decompressedLength *int) ([]byte, error) {
var compressedBuffer = buffers.Buffers.Get().(*bytes.Buffer)
defer buffers.Buffers.Put(compressedBuffer)
compressedBuffer.Reset()
if compressedBuffer.Len() < compressedLength {
compressedBuffer.Grow(compressedLength)
}

if _, err := data.Read(compressedBuffer.Bytes()[:compressedLength]); err != nil {
return nil, err
}

compressed := compressedBuffer.Bytes()[:compressedLength]

func DecompressZlib(compressed []byte, decompressedLength *int) ([]byte, error) {
dc := decompressors.Get().(libdeflate.Decompressor)
defer decompressors.Put(dc)

Expand All @@ -45,20 +28,7 @@ func DecompressZlib(data io.Reader, compressedLength int, decompressedLength *in

// Compresses zlib. If compressedLength is provided, data returned will only be safe to use until the next operation.
// It is recommmended to provide the compressed length to avoid allocation. If you don't know it, provide a number likely bigger than the compressed length.
func CompressZlib(decompressedData io.Reader, decompressedLength int, compressedLength *int) (compressed []byte, err error) {
var decompressedBuffer = buffers.Buffers.Get().(*bytes.Buffer)
defer buffers.Buffers.Put(decompressedBuffer)
decompressedBuffer.Reset()
if decompressedBuffer.Len() < decompressedLength {
decompressedBuffer.Grow(decompressedLength)
}

if _, err := decompressedData.Read(decompressedBuffer.Bytes()[:decompressedLength]); err != nil {
return nil, err
}

decompressed := decompressedBuffer.Bytes()[:decompressedLength]

func CompressZlib(decompressedData []byte, compressedLength *int) (compressed []byte, err error) {
c := compressors.Get().(libdeflate.Compressor)
defer compressors.Put(c)

Expand All @@ -69,11 +39,11 @@ func CompressZlib(decompressedData io.Reader, decompressedLength int, compressed
}
defer bufs.Put(dst)

_, compressedResult, err := c.CompressZlib(decompressed, dst[:*compressedLength])
_, compressedResult, err := c.CompressZlib(decompressedData, dst[:*compressedLength])

return compressedResult, err
} else {
_, compressedResult, err := c.CompressZlib(decompressed, nil)
_, compressedResult, err := c.CompressZlib(decompressedData, nil)

return compressedResult, err
}
Expand Down
2 changes: 1 addition & 1 deletion net/io/util/readerAtMaxxer.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ type ReaderAtMaxxer struct {
}

func (r *ReaderAtMaxxer) Read(data []byte) (i int, err error) {
if r.read > r.max {
if r.read >= r.max {
return 0, io.EOF
}

Expand Down
5 changes: 3 additions & 2 deletions server/command/command.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,9 @@ import (
)

type Command struct {
Node Node
Aliases []string
Node Node
Aliases []string
Namespace string

Callback func(CommandCallContext)
SuggestionCallback func()
Expand Down
18 changes: 18 additions & 0 deletions server/command/graph.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,31 @@ func (mgr *Manager) Encode() *play.Commands {
pk.Nodes[0].Children = append(pk.Nodes[0].Children, commandNodeIndex)
pk.Nodes = append(pk.Nodes, cmd.Node.Node)

if cmd.Namespace != "" {
pk.Nodes[0].Children = append(pk.Nodes[0].Children, int32(len(pk.Nodes)))
pk.Nodes = append(pk.Nodes, play.Node{
Flags: play.NodeLiteral | play.NodeRedirect,
Name: cmd.Namespace + ":" + cmd.Node.Name,
RedirectNode: commandNodeIndex,
})
}

for _, alias := range cmd.Aliases {
pk.Nodes[0].Children = append(pk.Nodes[0].Children, int32(len(pk.Nodes)))
pk.Nodes = append(pk.Nodes, play.Node{
Flags: play.NodeLiteral | play.NodeRedirect,
Name: alias,
RedirectNode: commandNodeIndex,
})

if cmd.Namespace != "" {
pk.Nodes[0].Children = append(pk.Nodes[0].Children, int32(len(pk.Nodes)))
pk.Nodes = append(pk.Nodes, play.Node{
Flags: play.NodeLiteral | play.NodeRedirect,
Name: cmd.Namespace + ":" + alias,
RedirectNode: commandNodeIndex,
})
}
}

for _, child := range cmd.Node.children {
Expand Down
9 changes: 9 additions & 0 deletions server/command/manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,16 @@ func (mgr *Manager) Call(command string, caller session.Session) {
func (mgr *Manager) findCommand(name string) *Command {
mgr.mu.RLock()
defer mgr.mu.RUnlock()

var namespace string
if i := strings.Index(name, ":"); i != -1 && i != len(name) {
namespace = name[:i]
name = name[i+1:]
}
for _, cmd := range mgr.commands {
if namespace != "" && cmd.Namespace != namespace {
continue
}
if cmd.Node.Name == name {
return &cmd
}
Expand Down
106 changes: 106 additions & 0 deletions server/world/chunk/section/blockstates/dec.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@
// package blockstates provides parsing of the custom format for storing block states
package blockstates

import (
"encoding/binary"
"io"
"unsafe"

"github.com/zeppelinmc/zeppelin/net/io/util"
)

type BlockLocation struct {
Offset, Size int32
}

type BlockState struct {
Id int32
Properties map[string]string
}

type Block []BlockState

// ReadBlock reads the block states at the offset and size
func ReadBlock(f io.ReaderAt, loc BlockLocation) (Block, error) {
var blockStates Block

maxxer := util.NewReaderAtMaxxer(f, int(loc.Size), int64(loc.Offset))

var stateCount uint16
if err := binary.Read(maxxer, binary.BigEndian, &stateCount); err != nil {
return blockStates, err
}

blockStates = make(Block, stateCount)

var stringlen = make([]byte, 1)

for i := range blockStates {
var (
blockStateId int32
propertyCount uint8
)
if err := binary.Read(maxxer, binary.BigEndian, &blockStateId); err != nil {
return blockStates, err
}
if err := binary.Read(maxxer, binary.BigEndian, &propertyCount); err != nil {
return blockStates, err
}

blockStates[i].Id = blockStateId
blockStates[i].Properties = make(map[string]string, propertyCount)

for i := 0; i < int(propertyCount); i++ {
propertyName, err := readString(maxxer, stringlen)
if err != nil {
return blockStates, nil
}
propertyValue, err := readString(maxxer, stringlen)
if err != nil {
return blockStates, nil
}
blockStates[i].Properties[propertyName] = propertyValue
}
}

return blockStates, nil
}

// ReadHeader reads the header of the block states format. The header contains locations for each block
func ReadHeader(f io.Reader) (map[string]BlockLocation, error) {
var blockCount int32
if err := binary.Read(f, binary.BigEndian, &blockCount); err != nil {
return nil, err
}

var locations = make(map[string]BlockLocation, blockCount)

var strlen = make([]byte, 1)
for i := 0; i < int(blockCount); i++ {
name, err := readString(f, strlen)
if err != nil {
return nil, err
}

var location BlockLocation
if err := binary.Read(f, binary.BigEndian, location); err != nil {
return nil, err
}

locations[name] = location
}

return locations, nil
}

// reads a string prepended by a byte length, l should be a byte slice with a length of one, so make([]byte, 1) should work
func readString(f io.Reader, l []byte) (string, error) {
if _, err := f.Read(l); err != nil {
return "", err
}
var strdata = make([]byte, l[0])
if _, err := f.Read(strdata); err != nil {
return "", err
}
return unsafe.String(unsafe.SliceData(strdata), len(strdata)), nil
}
1 change: 1 addition & 0 deletions server/world/chunk/section/blockstates/enc.go
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
package blockstates
Loading

0 comments on commit 173b502

Please sign in to comment.