Refactor: Remove VarInt type, replace with int32, and consolidate encoding/decoding logic

This commit is contained in:
adman
2025-12-24 00:29:43 +01:00
parent 2f81976fe8
commit dd41115ca2
10 changed files with 41 additions and 32 deletions
+6 -7
View File
@@ -53,7 +53,7 @@ func (c *Conn) Send(packet any) (err error) {
if c.CompressionThreshold > 0 {
packetBuffer := bytes.NewBuffer(nil)
if int32(len(packetBytes)) >= c.CompressionThreshold {
err = proto_base.VarInt(len(packetBytes)).Encode(packetBuffer)
err = proto_base.EncodeVarInt(packetBuffer, int32(len(packetBytes)))
if err != nil {
return
}
@@ -62,7 +62,7 @@ func (c *Conn) Send(packet any) (err error) {
return
}
} else {
err = proto_base.VarInt(0).Encode(packetBuffer)
err = proto_base.EncodeVarInt(packetBuffer, 0)
if err != nil {
return
}
@@ -75,7 +75,7 @@ func (c *Conn) Send(packet any) (err error) {
} else {
packetBytes = rawPacketBytes
}
err = proto_base.VarInt(len(packetBytes)).Encode(c.W)
err = proto_base.EncodeVarInt(c.W, int32(len(packetBytes)))
if err != nil {
return
}
@@ -84,8 +84,7 @@ func (c *Conn) Send(packet any) (err error) {
}
func (c *Conn) Receive() (packet any, err error) {
var rawPacketLen proto_base.VarInt
rawPacketLen, err = rawPacketLen.Decode(c.R)
rawPacketLen, err := proto_base.DecodeVarInt(c.R)
if err != nil {
return
}
@@ -96,8 +95,8 @@ func (c *Conn) Receive() (packet any, err error) {
rawPacketBuffer := bytes.NewBuffer(rawPacketBytes)
var packetBytes []byte
if c.CompressionThreshold > 0 {
var packetLen proto_base.VarInt
packetLen, err = packetLen.Decode(rawPacketBuffer)
var packetLen int32
packetLen, err = proto_base.DecodeVarInt(rawPacketBuffer)
if err != nil {
return
}
+2 -2
View File
@@ -25,9 +25,9 @@ var nanIgnorer64 = cmp.Comparer(func(x, y float64) bool {
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
Id int32
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)}
}{}}, UnsignedChatContent: (*nbt.Anon)(nil), FilterType: 0, FilterTypeMask: proto_base.Void{}, Type: v1_21_8.PlayToClientChatTypesHolder{Val: 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 {