monorepo fixes
This commit is contained in:
@@ -0,0 +1,71 @@
|
||||
package proto_base_test
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"math"
|
||||
|
||||
"testing"
|
||||
|
||||
"github.com/admin-else/strom/nbt"
|
||||
"github.com/admin-else/strom/proto_base"
|
||||
"github.com/admin-else/strom/proto_generated/v1_21_8"
|
||||
"github.com/google/uuid"
|
||||
|
||||
"github.com/google/go-cmp/cmp"
|
||||
)
|
||||
|
||||
var nanIgnorer32 = cmp.Comparer(func(x, y float32) bool {
|
||||
return (math.IsNaN(float64(x)) && math.IsNaN(float64(y))) || x == y
|
||||
})
|
||||
|
||||
var nanIgnorer64 = cmp.Comparer(func(x, y float64) bool {
|
||||
return (math.IsNaN(x) && math.IsNaN(y)) || x == y
|
||||
})
|
||||
|
||||
func TestChat(t *testing.T) {
|
||||
//p := v1_21_8.PlayToServerPacketChatMessage{Message: "dfs", Timestamp: 1764153707364, Salt: -5634100397920314628, Signature: (*[256]uint8)(nil), Offset: 0, Acknowledged: [3]uint8{0x0, 0x0, 0x0}, Checksum: 0x1}
|
||||
p := v1_21_8.PlayToClientPacketPlayerChat{GlobalIndex: 0, SenderUuid: uuid.UUID{0x1d, 0xc5, 0x57, 0x82, 0x3c, 0x0, 0x3e, 0xd4, 0xbf, 0x8b, 0x76, 0xd4, 0xc6, 0xd9, 0xcc, 0x47}, Index: 0, Signature: (*[256]uint8)(nil), PlainMessage: "csdf", Timestamp: 1764153823132, Salt: 0, PreviousMessages: v1_21_8.PreviousMessages{Val: []struct {
|
||||
Id proto_base.VarInt
|
||||
Signature interface{}
|
||||
}{}}, UnsignedChatContent: (*nbt.Anon)(nil), FilterType: 0, FilterTypeMask: proto_base.Void{}, Type: v1_21_8.PlayToClientChatTypesHolder{Val: proto_base.VarInt(1)}, NetworkName: nbt.Anon{Value: map[string]interface{}{"click_event": map[string]interface{}{"action": "suggest_command", "command": "/tell Player659 "}, "hover_event": map[string]interface{}{"action": "show_entity", "id": "minecraft:player", "name": "Player659", "uuid": []int32{499472258, 1006649044, -1081379116, -958804921}}, "insertion": "Player659", "text": "Player659"}}, NetworkTargetName: (*nbt.Anon)(nil)}
|
||||
b := bytes.NewBuffer(nil)
|
||||
err := p.Encode(b)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
t.Logf("%q", b.Bytes())
|
||||
}
|
||||
|
||||
func FuzzPlayPackets(f *testing.F) {
|
||||
f.Fuzz(func(t *testing.T, b []byte) {
|
||||
t.Logf("Fuzz data %q\n", b)
|
||||
p := v1_21_8.PlayToClientPacket{}
|
||||
b1 := bytes.NewBuffer(b)
|
||||
p, err := p.Decode(b1)
|
||||
if err != nil {
|
||||
t.Skipf("failed to decode %q because %v (partial %v)", b, err, p)
|
||||
return
|
||||
}
|
||||
if b1.Len() != 0 {
|
||||
t.Skipf("there are %d bytes left it should be read entirely", b1.Len())
|
||||
return
|
||||
}
|
||||
t.Logf("decoded %#v\n", p)
|
||||
b2 := bytes.NewBuffer(nil)
|
||||
err = p.Encode(b2)
|
||||
if err != nil {
|
||||
t.Errorf("failed to encode %q because %v", b, err)
|
||||
return
|
||||
}
|
||||
t.Logf("encoded %q\n", b2.Bytes())
|
||||
p2 := v1_21_8.PlayToClientPacket{}
|
||||
p2, err = p2.Decode(b2)
|
||||
if err != nil {
|
||||
t.Errorf("failed to decode encoded data: %v (partial %#v)", err, p2)
|
||||
return
|
||||
}
|
||||
if !cmp.Equal(p, p2, nanIgnorer32, nanIgnorer64) {
|
||||
t.Errorf("decoded and encoded decoded data are not equal: %#v", p2)
|
||||
}
|
||||
})
|
||||
}
|
||||
@@ -0,0 +1,144 @@
|
||||
package proto_base
|
||||
|
||||
import (
|
||||
"encoding/binary"
|
||||
"errors"
|
||||
"io"
|
||||
)
|
||||
|
||||
type Direction int
|
||||
|
||||
const (
|
||||
ToServer Direction = iota
|
||||
ToClient
|
||||
)
|
||||
|
||||
type State int
|
||||
|
||||
const (
|
||||
Handshaking State = iota
|
||||
Status
|
||||
Login
|
||||
Configuration
|
||||
Play
|
||||
)
|
||||
|
||||
type PacketIdentifier struct {
|
||||
Direction Direction
|
||||
State State
|
||||
Name string
|
||||
}
|
||||
|
||||
type (
|
||||
VarInt int32
|
||||
ToDo struct{}
|
||||
Void struct{}
|
||||
RestBuffer []byte
|
||||
)
|
||||
|
||||
func (v Void) Encode(_ io.Writer) (err error) {
|
||||
return
|
||||
}
|
||||
|
||||
func (v Void) Decode(_ io.Reader) (ret Void, err error) {
|
||||
return
|
||||
}
|
||||
|
||||
var ToDoError = errors.New("to do")
|
||||
var BadTypeError = errors.New("bad type")
|
||||
|
||||
func (r RestBuffer) Encode(w io.Writer) (err error) {
|
||||
_, err = w.Write(r)
|
||||
return
|
||||
}
|
||||
|
||||
func (RestBuffer) Decode(r io.Reader) (ret RestBuffer, err error) {
|
||||
b, err := io.ReadAll(r)
|
||||
ret = b
|
||||
return
|
||||
}
|
||||
|
||||
func EncodeString(w io.Writer, s string) (err error) {
|
||||
err = VarInt(len(s)).Encode(w)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
_, err = w.Write([]byte(s))
|
||||
return
|
||||
}
|
||||
|
||||
func DecodeString(r io.Reader) (ret string, err error) {
|
||||
var l VarInt
|
||||
l, err = l.Decode(r)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
rawString, err := io.ReadAll(io.LimitReader(r, int64(l)))
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
if len(rawString) != int(l) {
|
||||
err = errors.New("bad string length")
|
||||
return
|
||||
}
|
||||
return string(rawString), nil
|
||||
}
|
||||
|
||||
func (t ToDo) Encode(_ io.Writer) (err error) {
|
||||
err = ToDoError
|
||||
return
|
||||
}
|
||||
|
||||
func (t ToDo) Decode(_ io.Reader) (ret ToDo, err error) {
|
||||
err = ToDoError
|
||||
return
|
||||
}
|
||||
|
||||
func (v VarInt) Encode(w io.Writer) (err error) {
|
||||
uv := uint32(v)
|
||||
for {
|
||||
b := uint8(uv & 0x7F)
|
||||
uv >>= 7
|
||||
if uv != 0 {
|
||||
b |= 0x80
|
||||
}
|
||||
err := binary.Write(w, binary.BigEndian, b)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if uv == 0 {
|
||||
break
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (v VarInt) Decode(r io.Reader) (ret VarInt, err error) {
|
||||
for i := range 5 { // 32/7
|
||||
var b uint8 = 0
|
||||
err = binary.Read(r, binary.BigEndian, &b)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
ret |= VarInt(uint32(b&0x7F) << (7 * uint32(i)))
|
||||
if b&0x80 == 0 {
|
||||
return
|
||||
}
|
||||
}
|
||||
err = errors.New("VarInt too long")
|
||||
return
|
||||
}
|
||||
|
||||
func ErroringIndex[K comparable, V any, M map[K]V](m M, i K) (v V, err error) {
|
||||
var ok bool
|
||||
v, ok = m[i]
|
||||
if !ok {
|
||||
err = errors.New("index not found")
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
type EncodeDecodeAble[Self any] interface {
|
||||
Encode(w io.Writer) (err error)
|
||||
Decode(r io.Reader) (ret Self, err error)
|
||||
}
|
||||
Reference in New Issue
Block a user