Skip to content

Commit

Permalink
Fix ServerLoginSuccess in GreaterEqual Minecraft_1_16 to use ReadUUID…
Browse files Browse the repository at this point in the history
…IntArray
  • Loading branch information
robinbraemer committed Dec 28, 2023
1 parent 476a8e2 commit 59b0b0e
Show file tree
Hide file tree
Showing 4 changed files with 60 additions and 34 deletions.
2 changes: 1 addition & 1 deletion pkg/edition/java/proto/packet/login.go
Original file line number Diff line number Diff line change
Expand Up @@ -292,7 +292,7 @@ func (s *ServerLoginSuccess) Decode(c *proto.PacketContext, rd io.Reader) (err e
if c.Protocol.GreaterEqual(version.Minecraft_1_19) {
s.UUID, err = util.ReadUUID(rd)
} else if c.Protocol.GreaterEqual(version.Minecraft_1_16) {
s.UUID, err = util.ReadUUID(rd) // readUUIDIntArray?
s.UUID, err = util.ReadUUIDIntArray(rd)
} else {
var uuidString string
if c.Protocol.GreaterEqual(version.Minecraft_1_7_6) {
Expand Down
29 changes: 29 additions & 0 deletions pkg/edition/java/proto/util/reader.go
Original file line number Diff line number Diff line change
Expand Up @@ -328,6 +328,35 @@ func ReadUUID(rd io.Reader) (id uuid.UUID, err error) {
return uuid.FromBytes(b)
}

func ReadUUIDIntArray(rd io.Reader) (uuid.UUID, error) {
msbHigh, err := ReadInt(rd)
if err != nil {
return uuid.UUID{}, err
}
msbLow, err := ReadInt(rd)
if err != nil {
return uuid.UUID{}, err
}
lsbHigh, err := ReadInt(rd)
if err != nil {
return uuid.UUID{}, err
}
lsbLow, err := ReadInt(rd)
if err != nil {
return uuid.UUID{}, err
}
msb := int64(msbHigh)<<32 | int64(msbLow)&0xFFFFFFFF
lsb := int64(lsbHigh)<<32 | int64(lsbLow)&0xFFFFFFFF

return uuid.FromBytes(append(int64ToBytes(msb), int64ToBytes(lsb)...))
}

func int64ToBytes(i int64) []byte {
var buf [8]byte
binary.BigEndian.PutUint64(buf[:], uint64(i))
return buf[:]
}

func ReadProperties(rd io.Reader) (props []profile.Property, err error) {
var size int
size, err = ReadVarInt(rd)
Expand Down
18 changes: 18 additions & 0 deletions pkg/edition/java/proto/util/util_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package util
import (
"bytes"
"github.com/stretchr/testify/require"
"go.minekube.com/gate/pkg/util/uuid"
"testing"
)

Expand All @@ -21,3 +22,20 @@ func TestUTF(t *testing.T) {
require.NoError(t, err)
require.Equal(t, "test", s)
}

func TestUUIDIntArray(t *testing.T) {
// Generate a random UUID
id := uuid.New()

// Create a buffer and write the UUID to it as an integer array
buf := new(bytes.Buffer)
err := WriteUUIDIntArray(buf, id)
require.NoError(t, err)

// Read the UUID from the buffer
readID, err := ReadUUIDIntArray(buf)
require.NoError(t, err)

// The read UUID should be the same as the original UUID
require.Equal(t, id, readID)
}
45 changes: 12 additions & 33 deletions pkg/edition/java/proto/util/writer.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,13 @@ package util
import (
"encoding/binary"
"fmt"
"io"
"math"
"strings"

"go.minekube.com/common/minecraft/component"
"go.minekube.com/gate/pkg/edition/java/profile"
"go.minekube.com/gate/pkg/gate/proto"
"go.minekube.com/gate/pkg/util/uuid"
"io"
"math"
"strings"
)

func WriteString(writer io.Writer, val string) (err error) {
Expand Down Expand Up @@ -193,46 +192,26 @@ func WriteProperties(wr io.Writer, properties []profile.Property) error {
return nil
}

/*
func WriteUUIDIntArray(wr io.Writer, uuid [16]byte) (err error) {
err = WriteInt32(wr, ByteArrayToInt32(uuid[:3]))
if err != nil {
return err
}
err = WriteInt32(wr, ByteArrayToInt32(uuid[4:7]))
func WriteUUIDIntArray(wr io.Writer, id uuid.UUID) error {
msb := binary.BigEndian.Uint64(id[:8])
lsb := binary.BigEndian.Uint64(id[8:])

err := WriteUint32(wr, uint32(msb>>32))
if err != nil {
return err
}
err = WriteInt32(wr, ByteArrayToInt32(uuid[8:10]))
err = WriteUint32(wr, uint32(msb))
if err != nil {
return err
}
err = WriteInt32(wr, ByteArrayToInt32(uuid[11:13]))
err = WriteUint32(wr, uint32(lsb>>32))
if err != nil {
return err
}
return
}
func Int32ToByteArray(num int32) []byte {
size := int(unsafe.Sizeof(num))
arr := make([]byte, size)
for i := 0 ; i < size ; i++ {
byt := *(*uint8)(unsafe.Pointer(uintptr(unsafe.Pointer(&num)) + uintptr(i)))
arr[i] = byt
}
return arr
err = WriteUint32(wr, uint32(lsb))
return err
}

func ByteArrayToInt32(arr []byte) int32{
val := int32(0)
size := len(arr)
for i := 0 ; i < size ; i++ {
*(*uint8)(unsafe.Pointer(uintptr(unsafe.Pointer(&val)) + uintptr(i))) = arr[i]
}
return val
}*/

func WriteBytes17(wr io.Writer, b []byte, allowExtended bool) error {
if allowExtended {
if len(b) > ForgeMaxArrayLength {
Expand Down

0 comments on commit 59b0b0e

Please sign in to comment.