Skip to content

Commit

Permalink
minecraft/protocol: Added ability to implement your own reader and wr…
Browse files Browse the repository at this point in the history
…iter (#183)
  • Loading branch information
Flonja committed Jun 10, 2023
1 parent fa4fd39 commit 07032c1
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 6 deletions.
2 changes: 1 addition & 1 deletion minecraft/conn.go
Original file line number Diff line number Diff line change
Expand Up @@ -319,7 +319,7 @@ func (conn *Conn) WritePacket(pk packet.Packet) error {
l := buf.Len()

for _, converted := range conn.proto.ConvertFromLatest(pk, conn) {
converted.Marshal(protocol.NewWriter(buf, conn.shieldID.Load()))
converted.Marshal(conn.proto.NewWriter(buf, conn.shieldID.Load()))

if conn.packetFunc != nil {
conn.packetFunc(*conn.hdr, buf.Bytes()[l:], conn.LocalAddr(), conn.RemoteAddr())
Expand Down
3 changes: 1 addition & 2 deletions minecraft/packet.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package minecraft
import (
"bytes"
"fmt"
"github.com/sandertv/gophertunnel/minecraft/protocol"
"github.com/sandertv/gophertunnel/minecraft/protocol/packet"
)

Expand Down Expand Up @@ -42,7 +41,7 @@ func (p *packetData) decode(conn *Conn) (pks []packet.Packet, err error) {
pk = pkFunc()
}

r := protocol.NewReader(p.payload, conn.shieldID.Load())
r := conn.proto.NewReader(p.payload, conn.shieldID.Load())
defer func() {
if recoveredErr := recover(); recoveredErr != nil {
err = fmt.Errorf("%T: %w", pk, recoveredErr.(error))
Expand Down
29 changes: 26 additions & 3 deletions minecraft/protocol.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package minecraft
import (
"github.com/sandertv/gophertunnel/minecraft/protocol"
"github.com/sandertv/gophertunnel/minecraft/protocol/packet"
"io"
)

// Protocol represents the Minecraft protocol used to communicate over network. It comprises a unique set of packets
Expand All @@ -17,6 +18,12 @@ type Protocol interface {
// Packets returns a packet.Pool with all packets registered for this Protocol. It is used to lookup packets by a
// packet ID.
Packets() packet.Pool
// NewReader returns a protocol.IO that implements reading operations for reading types
// that are used for this Protocol.
NewReader(r reader, shieldID int32) protocol.IO
// NewWriter returns a protocol.IO that implements writing operations for writing types
// that are used for this Protocol.
NewWriter(w writer, shieldID int32) protocol.IO
// ConvertToLatest converts a packet.Packet obtained from the other end of a Conn to a slice of packet.Packets from
// the latest protocol. Any packet.Packet implementation in the packet.Pool obtained through a call to Packets that
// is not identical to the most recent version of that packet.Packet must be converted to the most recent version of
Expand All @@ -30,13 +37,29 @@ type Protocol interface {
ConvertFromLatest(pk packet.Packet, conn *Conn) []packet.Packet
}

type reader interface {
io.Reader
io.ByteReader
}

type writer interface {
io.Writer
io.ByteWriter
}

// proto is the default Protocol implementation. It returns the current protocol, version and packet pool and does not
// convert any packets, as they are already of the right type.
type proto struct{}

func (proto) ID() int32 { return protocol.CurrentProtocol }
func (p proto) Ver() string { return protocol.CurrentVersion }
func (p proto) Packets() packet.Pool { return packet.NewPool() }
func (proto) ID() int32 { return protocol.CurrentProtocol }
func (p proto) Ver() string { return protocol.CurrentVersion }
func (p proto) Packets() packet.Pool { return packet.NewPool() }
func (p proto) NewReader(r reader, shieldID int32) protocol.IO {
return protocol.NewReader(r, shieldID)
}
func (p proto) NewWriter(w writer, shieldID int32) protocol.IO {
return protocol.NewWriter(w, shieldID)
}
func (p proto) ConvertToLatest(pk packet.Packet, _ *Conn) []packet.Packet { return []packet.Packet{pk} }
func (p proto) ConvertFromLatest(pk packet.Packet, _ *Conn) []packet.Packet {
return []packet.Packet{pk}
Expand Down

0 comments on commit 07032c1

Please sign in to comment.