Skip to content

Commit

Permalink
ticked movement (untested)
Browse files Browse the repository at this point in the history
  • Loading branch information
oq-x committed Sep 1, 2024
1 parent 4f7f0b4 commit 05f5ffe
Show file tree
Hide file tree
Showing 23 changed files with 492 additions and 148 deletions.
4 changes: 3 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -20,4 +20,6 @@ server.properties
data.qfg
/blockstates
server-icon.png
/resources
/resources
/server/session/atomicSlice.go
/server/session/a_test.go
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
module github.com/zeppelinmc/zeppelin

go 1.22.5
go 1.23

require (
github.com/fatih/color v1.17.0
Expand Down
12 changes: 11 additions & 1 deletion main.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"net/http"
"os"
"runtime"
"runtime/debug"
"runtime/pprof"
"slices"
"strconv"
Expand All @@ -17,6 +18,7 @@ import (
"github.com/zeppelinmc/zeppelin/server/command"
"github.com/zeppelinmc/zeppelin/server/world"
"github.com/zeppelinmc/zeppelin/server/world/chunk/section"
"github.com/zeppelinmc/zeppelin/util"
"github.com/zeppelinmc/zeppelin/util/console"
"github.com/zeppelinmc/zeppelin/util/log"
"golang.org/x/term"
Expand All @@ -26,11 +28,19 @@ var timeStart = time.Now()

func main() {
log.Infolnf("Zeppelin 1.21 Minecraft server with %s on platform %s-%s", runtime.Version(), runtime.GOOS, runtime.GOARCH)

if !loadStates() {
return
}

max, ok := console.GetFlag("xmem")
if ok {
m, err := util.ParseSizeUnit(max)
if err == nil {
debug.SetMemoryLimit(int64(m))
log.Infolnf("Memory usage is limited to %s", util.FormatSizeUnit(m))
}
}

if slices.Index(os.Args, "--cpuprof") != -1 {
if f, err := os.Create("zeppelin-cpu-profile"); err == nil {
pprof.StartCPUProfile(f)
Expand Down
4 changes: 0 additions & 4 deletions protocol/nbt/qnbt/primitive.go
Original file line number Diff line number Diff line change
Expand Up @@ -247,10 +247,6 @@ func (d *Decoder) decodeIntArray(len int, ptr unsafe.Pointer) error {
return err
}

if !native.SystemBigEndian {
native.Convert32(unsafe.Slice((*byte)(ptr), 4))
}

ptr = unsafe.Add(ptr, 4)
}

Expand Down
6 changes: 6 additions & 0 deletions protocol/nbt/qnbt/qnbt_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -103,11 +103,17 @@ type chunk struct {
}

func BenchmarkXxx(b *testing.B) {
f1, _ := os.Create("testdata/cpu.prof")
pprof.StartCPUProfile(f1)

for i := 0; i < b.N; i++ {
Unmarshal(chunkData, &chunk{})
}
b.ReportAllocs()
f, _ := os.Create("testdata/mem.prof")
pprof.Lookup("allocs").WriteTo(f, 0)
f.Close()

pprof.StopCPUProfile()
f1.Close()
}
5 changes: 3 additions & 2 deletions protocol/nbt/qnbt/struct.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,11 @@ var structs = sync.Pool{

func newStruct(t *structType, ptr unsafe.Pointer) *struct_t {
s := structs.Get().(*struct_t)
clear(s.names)

s.t = t
s.ptr = ptr

clear(s.names)

for i, f := range t.fields {
if !f.name.exported() {
continue
Expand Down Expand Up @@ -68,6 +68,7 @@ type struct_t struct {

func (s *struct_t) field(n string) (f *structField, ptr unsafe.Pointer, ok bool) {
i, ok := s.names[n]

if !ok {
return nil, nil, ok
}
Expand Down
Binary file added protocol/nbt/qnbt/testdata/cpu.prof
Binary file not shown.
Binary file modified protocol/nbt/qnbt/testdata/mem.prof
Binary file not shown.
52 changes: 47 additions & 5 deletions protocol/net/conn.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,12 @@ import (
"github.com/google/uuid"
)

var PacketEncodeInterceptor func(c *Conn, pk packet.Encodeable) (stop bool)
var PacketDecodeInterceptor func(c *Conn, pk packet.Decodeable) (stop bool)

var PacketWriteInterceptor func(c *Conn, pk *bytes.Buffer) (stop bool)
var PacketReadInterceptor func(c *Conn, pk *bytes.Reader) (stop bool)

const (
clientVeryOldMsg = "Your client is WAYYYYYY too old!!! this server supports MC 1.21"
clientTooOldMsg = "Your client is too old! this server supports MC 1.21"
Expand Down Expand Up @@ -87,6 +93,12 @@ var pkpool = sync.Pool{
}

func (conn *Conn) WritePacket(pk packet.Encodeable) error {
if PacketEncodeInterceptor != nil {
if PacketEncodeInterceptor(conn, pk) {
return nil
}
}

conn.write_mu.Lock()
defer conn.write_mu.Unlock()

Expand All @@ -103,6 +115,12 @@ func (conn *Conn) WritePacket(pk packet.Encodeable) error {
return err
}

if PacketWriteInterceptor != nil {
if PacketWriteInterceptor(conn, packetBuf) {
return nil
}
}

if conn.listener.cfg.CompressionThreshold < 0 || !conn.compressionSet { // no compression
if err := io.WriteVarInt(conn, int32(packetBuf.Len())); err != nil {
return err
Expand Down Expand Up @@ -202,7 +220,7 @@ func (conn *Conn) ReadPacket() (packet.Decodeable, error) {
if _, err := conn.Read(packet); err != nil {
return nil, err
}
id, data, err := io.ReadVarInt(packet)
id, data, err := io.VarInt(packet)
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -238,15 +256,23 @@ func (conn *Conn) ReadPacket() (packet.Decodeable, error) {
return nil, err
}

id, data, err := io.ReadVarInt(packet)
id, data, err := io.VarInt(packet)
if err != nil {
return nil, err
}
packetId = id
packet = data
length = int32(len(data))

rd = io.NewReader(bytes.NewReader(packet), int(length))
r := bytes.NewReader(packet)

if PacketReadInterceptor != nil {
if PacketReadInterceptor(conn, r) {
return nil, fmt.Errorf("stopped by interceptor")
}
}

rd = io.NewReader(r, int(length))
}
} else { //packet is compressed
length = dataLength
Expand All @@ -264,20 +290,29 @@ func (conn *Conn) ReadPacket() (packet.Decodeable, error) {
return nil, err
}

id, data, err := io.ReadVarInt(uncompressedPacket)
id, data, err := io.VarInt(uncompressedPacket)
if err != nil {
return nil, err
}
packetId = id
uncompressedPacket = data
length = int32(len(data))

rd = io.NewReader(bytes.NewReader(uncompressedPacket), int(length))
r := bytes.NewReader(uncompressedPacket)

if PacketReadInterceptor != nil {
if PacketReadInterceptor(conn, r) {
return nil, fmt.Errorf("stopped by interceptor")
}
}

rd = io.NewReader(r, int(length))
}
}

var pk packet.Decodeable
pc, ok := ServerboundPool[conn.state.Load()][packetId]

if !ok {
return packet.UnknownPacket{
Id: packetId,
Expand All @@ -286,6 +321,13 @@ func (conn *Conn) ReadPacket() (packet.Decodeable, error) {
}, nil
} else {
pk = pc()

if PacketDecodeInterceptor != nil {
if PacketDecodeInterceptor(conn, pk) {
return nil, fmt.Errorf("stopped by interceptor")
}
}

err := pk.Decode(rd)
return pk, err
}
Expand Down
34 changes: 33 additions & 1 deletion protocol/net/io/io.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package io
import (
"fmt"
"io"
"unsafe"
)

func AppendByte(data []byte, b int8) []byte {
Expand Down Expand Up @@ -52,7 +53,7 @@ func WriteVarInt(w io.Writer, value int32) error {
return err
}

func ReadVarInt(data []byte) (int32, []byte, error) {
func VarInt(data []byte) (int32, []byte, error) {
var (
position int
currentByte byte
Expand Down Expand Up @@ -83,6 +84,37 @@ func ReadVarInt(data []byte) (int32, []byte, error) {
return value, data, nil
}

func ReadVarInt(r io.Reader) (int32, error) {
var (
position int32
currentByte byte
CONTINUE_BIT byte = 128
SEGMENT_BITS byte = 127

value int32
)

for {
if _, err := r.Read(unsafe.Slice(&currentByte, 1)); err != nil {
return value, err
}

value |= int32((currentByte & SEGMENT_BITS)) << position

if (currentByte & CONTINUE_BIT) == 0 {
break
}

position += 7

if position >= 32 {
return value, fmt.Errorf("VarInt is too big")
}
}

return value, nil
}

func AppendVarLong(data []byte, value int64) []byte {
var (
CONTINUE_BIT int64 = 128
Expand Down
43 changes: 43 additions & 0 deletions server/command/builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,12 @@ const (
UUID
)

const (
StringSingleWord = iota
StringQuotablePhrase
StringGreedyPhrase
)

type Node struct {
play.Node
children []Node
Expand Down Expand Up @@ -89,3 +95,40 @@ func NewBoolArgument(name string, nodes ...Node) Node {
children: nodes,
}
}

func NewIntegerArgument(name string, min, max *int32, nodes ...Node) Node {
flags := byte(0)

var m, x int32

if min != nil {
flags &= 0x01
m = *min
}
if max != nil {
flags &= 0x02
x = *max
}

return Node{
Node: play.Node{
Flags: play.NodeArgument,
Name: name,
ParserId: Integer,
Properties: []any{flags, m, x},
},
children: nodes,
}
}

func NewStringArgument(name string, typ int32, nodes ...Node) Node {
return Node{
Node: play.Node{
Flags: play.NodeArgument,
Name: name,
ParserId: Integer,
Properties: []any{typ},
},
children: nodes,
}
}
44 changes: 44 additions & 0 deletions server/player/atomic.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
package player

import (
"sync/atomic"
"unsafe"
)

func i64f(i int64) float64 {
return *(*float64)(unsafe.Pointer(&i))
}
func f64i(f float64) int64 {
return *(*int64)(unsafe.Pointer(&f))
}

func i32f(i int32) float32 {
return *(*float32)(unsafe.Pointer(&i))
}
func f32i(f float32) int32 {
return *(*int32)(unsafe.Pointer(&f))
}

func atomicFloat64(f float64) *atomic.Int64 {
var v *atomic.Int64
v.Store(f64i(f))

return v
}
func atomicFloat32(f float32) *atomic.Int32 {
return atomicInt32(f32i(f))
}

func atomicInt32(i int32) *atomic.Int32 {
var v *atomic.Int32
v.Store(i)

return v
}

func atomicBool(b bool) *atomic.Bool {
var v *atomic.Bool
v.Store(b)

return v
}
Loading

0 comments on commit 05f5ffe

Please sign in to comment.