packet

package
v0.0.0-...-18e7427 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Dec 10, 2023 License: MIT Imports: 11 Imported by: 0

Documentation

Index

Examples

Constants

View Source
const MaxDataLength = 2097152
View Source
const MaxVarIntLen = 5
View Source
const MaxVarLongLen = 10

Variables

This section is empty.

Functions

This section is empty.

Types

type Angle

type Angle Byte

Angle is rotation angle in steps of 1/256 of a full turn

func (*Angle) ReadFrom

func (a *Angle) ReadFrom(r io.Reader) (int64, error)

func (Angle) ToDeg

func (a Angle) ToDeg() float64

ToDeg convert Angle to Degree

func (Angle) ToRad

func (a Angle) ToRad() float64

ToRad convert Angle to Radian

func (Angle) WriteTo

func (a Angle) WriteTo(w io.Writer) (int64, error)

type Ary

type Ary[T VarInt | VarLong | Byte | UnsignedByte | Short | UnsignedShort | Int | Long] struct {
	Ary interface{} // Slice or Pointer of Slice of FieldEncoder, FieldDecoder or both (Field)
}

Ary is used to send or receive the packet field like "Array of X" which has a count must be known from the context.

Typically, you must decode an integer representing the length. Then receive the corresponding amount of data according to the length. In this case, the field Len should be a pointer of integer type so the value can be updating when Packet.Scan() method is decoding the previous field. In some special cases, you might want to read an "Array of X" with a fix length. So it's allowed to directly set an integer type Len, but not a pointer.

Note that Ary DO read or write the Len. You aren't need to do so by your self.

func (Ary[T]) ReadFrom

func (a Ary[T]) ReadFrom(r io.Reader) (n int64, err error)
Example
package main

import (
	pk "github.com/Edouard127/go-mc/net/packet"
)

func main() {
	var data []pk.String

	var p pk.Packet // = conn.ReadPacket()
	if err := p.Scan(
		pk.Ary[pk.VarInt]{ // then decode Ary according to length
			Ary: &data,
		},
	); err != nil {
		panic(err)
	}
}
Output:

func (Ary[T]) WriteTo

func (a Ary[T]) WriteTo(w io.Writer) (n int64, err error)
Example
package main

import (
	pk "github.com/Edouard127/go-mc/net/packet"
)

func main() {
	data := []pk.Int{0, 1, 2, 3, 4, 5, 6}
	// Len is completely ignored by WriteTo method.
	// The length is inferred from the length of Ary.
	pk.Marshal(
		0x00,
		pk.Ary[pk.VarInt]{
			Ary: data,
		},
	)
}
Output:

type BitSet

type BitSet []int64

BitSet represents Java's BitSet, a list of bits.

func (BitSet) Get

func (b BitSet) Get(index int) bool

func (*BitSet) ReadFrom

func (b *BitSet) ReadFrom(r io.Reader) (n int64, err error)

func (BitSet) Set

func (b BitSet) Set(index int, value bool)

func (BitSet) WriteTo

func (b BitSet) WriteTo(w io.Writer) (n int64, err error)

type Boolean

type Boolean bool

Boolean of True is encoded as 0x01, false as 0x00.

func (*Boolean) ReadFrom

func (b *Boolean) ReadFrom(r io.Reader) (n int64, err error)

func (Boolean) WriteTo

func (b Boolean) WriteTo(w io.Writer) (int64, error)

type Builder

type Builder struct {
	// contains filtered or unexported fields
}

func (*Builder) Packet

func (p *Builder) Packet(id int32) Packet

func (*Builder) WriteField

func (p *Builder) WriteField(fields ...FieldEncoder)

type Byte

type Byte int8

Byte is signed 8-bit integer, two's complement

func (*Byte) ReadFrom

func (b *Byte) ReadFrom(r io.Reader) (n int64, err error)

func (Byte) WriteTo

func (b Byte) WriteTo(w io.Writer) (n int64, err error)

type ByteArray

type ByteArray []byte

ByteArray is []byte with prefix VarInt as length

func (*ByteArray) ReadFrom

func (b *ByteArray) ReadFrom(r io.Reader) (n int64, err error)

func (ByteArray) WriteTo

func (b ByteArray) WriteTo(w io.Writer) (n int64, err error)

type Chat

type Chat = String

Chat is encoded as a String with max length of 32767. Deprecated: Use chat.Message

type Double

type Double float64

A Double is a double-precision 64-bit IEEE 754 floating point number

func (*Double) ReadFrom

func (d *Double) ReadFrom(r io.Reader) (n int64, err error)

func (Double) WriteTo

func (d Double) WriteTo(w io.Writer) (n int64, err error)

type Field

type Field interface {
	FieldEncoder
	FieldDecoder
}

A Field is both FieldEncoder and FieldDecoder

func Array

func Array(ary any) Field

type FieldDecoder

type FieldDecoder io.ReaderFrom

A FieldDecoder can Decode from minecraft protocol

type FieldEncoder

type FieldEncoder io.WriterTo

A FieldEncoder can be encoded as minecraft protocol used.

type FixedBitSet

type FixedBitSet []byte

FixedBitSet is a fixed size BitSet

func NewFixedBitSet

func NewFixedBitSet(n int64) FixedBitSet

NewFixedBitSet make a FixedBitSet which can store n bits at least. If n <= 0, return nil

func (FixedBitSet) Get

func (f FixedBitSet) Get(index int) bool

func (FixedBitSet) ReadFrom

func (f FixedBitSet) ReadFrom(r io.Reader) (n int64, err error)

func (FixedBitSet) Set

func (f FixedBitSet) Set(index int, value bool)

func (FixedBitSet) WriteTo

func (f FixedBitSet) WriteTo(w io.Writer) (n int64, err error)

type Float

type Float float32

A Float is a single-precision 32-bit IEEE 754 floating point number

func (*Float) ReadFrom

func (f *Float) ReadFrom(r io.Reader) (n int64, err error)

func (Float) WriteTo

func (f Float) WriteTo(w io.Writer) (n int64, err error)

type Identifier

type Identifier = String

Identifier is encoded as a String with max length of 32767.

type Int

type Int int32

Int is signed 32-bit integer, two's complement

func (*Int) ReadFrom

func (i *Int) ReadFrom(r io.Reader) (n int64, err error)

func (Int) WriteTo

func (i Int) WriteTo(w io.Writer) (int64, error)

type Long

type Long int64

Long is signed 64-bit integer, two's complement

func (*Long) ReadFrom

func (l *Long) ReadFrom(r io.Reader) (n int64, err error)

func (Long) WriteTo

func (l Long) WriteTo(w io.Writer) (int64, error)

type NbtField

type NbtField struct {
	V         any
	FieldName string
}

func NBT

func NBT(v any, optionalTagName ...string) NbtField

NBT encode a value as Named Binary Tag

func (NbtField) ReadFrom

func (n NbtField) ReadFrom(r io.Reader) (int64, error)

func (NbtField) WriteTo

func (n NbtField) WriteTo(w io.Writer) (int64, error)

type Optional

type Optional[T FieldEncoder, P fieldPointer[T]] struct {
	Has   any // Pointer of bool, or `func() bool`
	Value T
}

Optional is used to send or receive the packet field like "Optional X" which has a bool must be known from the context. It is only used for buffer writing/reader. Not for code logic.

Typically, you must decode a boolean representing the existence of the field. Then receive the corresponding amount of data according to the boolean. In this case, the field Has should be a pointer of bool type so the value can be updating when Packet.Scan() method is decoding the previous field. In some special cases, you might want to read an "Optional X" with a fix length. So it's allowed to directly set a bool type Has, but not a pointer.

Note that Optional DO read or write the Has. You aren't need to do so by your self. But if you do, you might get undefined behavior.

func (Optional[T, P]) ReadFrom

func (o Optional[T, P]) ReadFrom(r io.Reader) (n int64, err error)
Example
package main

import (
	pk "github.com/Edouard127/go-mc/net/packet"
)

func main() {
	var str pk.String
	p1 := pk.Packet{Data: []byte{
		0x01,                  // pk.Boolean(true)
		4, 'T', 'n', 'z', 'e', // pk.String
	}}
	var data = pk.Optional[pk.String, *pk.String, *pk.Boolean]{Value: str}
	if err := p1.Scan(data); err != nil {
		panic(err)
	}

	var data2 pk.String = "WILL NOT BE READ, WILL NOT BE COVERED"
	p2 := pk.Packet{Data: []byte{
		0x00, // pk.Boolean(false)
		// empty
	}}
	if err := p2.Scan(pk.Optional[pk.String, *pk.String, *pk.Boolean]{Value: data2}); err != nil {
		panic(err)
	}

	// Tnze
	// WILL NOT BE READ, WILL NOT BE COVERED
}
Output:

Example (Func)
package main

import (
	"fmt"

	pk "github.com/Edouard127/go-mc/net/packet"
)

func main() {
	// As an example, we define this packet as this:
	// +------+-----------------+----------------------------------+
	// | Name | Type            | Notes                            |
	// +------+-----------------+----------------------------------+
	// | Flag | Unsigned Byte   | Odd if the following is present. |
	// +------+-----------------+----------------------------------+
	// | User | Optional String | The player's name.               |
	// +------+-----------------+----------------------------------+
	// So we need a function to decide if the User field is present.
	var data pk.String
	p := pk.Packet{Data: []byte{
		0b_0010_0011,          // pk.Byte(flag)
		4, 'T', 'n', 'z', 'e', // pk.String
	}}
	if err := p.Scan(pk.Optional[pk.String, *pk.String, *pk.Byte]{
		Value: data,
		Comp: func(p *pk.Byte) pk.Boolean {
			return *p&1 == 1
		},
	}); err != nil {
		panic(err)
	}
	fmt.Println(data)

}
Output:

Tnze

func (Optional[T, P]) WriteTo

func (o Optional[T, P]) WriteTo(w io.Writer) (n int64, err error)

type Packet

type Packet struct {
	ID   int32
	Data []byte
}

Packet define a net data package

func Marshal

func Marshal(id int32, fields ...FieldEncoder) (pk Packet)

Marshal generate Packet with the ID and Fields

Example (SetSlot)
package main

import (
	"fmt"

	_ "embed"
	pk "github.com/Edouard127/go-mc/net/packet"
)

func main() {
	for _, pf := range []struct {
		WindowID  byte
		Slot      int16
		Present   bool
		ItemID    int
		ItemCount byte
		NBT       interface{}
	}{
		{WindowID: 0, Slot: 5, Present: false},
		{WindowID: 0, Slot: 5, Present: true, ItemID: 0x01, ItemCount: 1, NBT: pk.Byte(0)},
		{WindowID: 0, Slot: 5, Present: true, ItemID: 0x01, ItemCount: 1, NBT: pk.NBT(int32(0x12345678))},
	} {
		p := pk.Marshal(0x15,
			pk.Byte(pf.WindowID),
			pk.Short(pf.Slot),
			pk.Boolean(pf.Present),
			pk.Optional[pk.Tuple, *pk.Tuple, *pk.Boolean]{
				Has: (*pk.Boolean)(&pf.Present),
				Value: pk.Tuple{
					pk.VarInt(pf.ItemID),
					pk.Byte(pf.ItemCount),
					pf.NBT,
				}},
		)
		fmt.Printf("%02X % 02X\n", p.ID, p.Data)
	}
}
Output:

15 00 00 05 00
15 00 00 05 01 01 01 00
15 00 00 05 01 01 01 03 00 00 12 34 56 78

func (*Packet) Pack

func (p *Packet) Pack(w io.Writer, threshold int) error

Pack 打包一个数据包

func (Packet) Scan

func (p Packet) Scan(fields ...FieldDecoder) error

Scan decode the packet and fill data into fields

Example (JoinGame)
package main

import (
	"fmt"

	_ "embed"
	pk "github.com/Edouard127/go-mc/net/packet"
)

//go:embed joingame_test.bin
var testJoinGameData []byte

func main() {
	p := pk.Packet{ID: 0x23, Data: testJoinGameData}
	var (
		EID            pk.Int
		Hardcore       pk.Boolean
		Gamemode       pk.UnsignedByte
		PreGamemode    pk.Byte
		WorldNames     = make([]pk.Identifier, 0) // This cannot replace with "var DimensionNames []pk.Identifier" because "nil" has no type information
		DimensionCodec struct {
			DimensionType interface{} `nbt:"minecraft:dimension_type"`
			WorldgenBiome interface{} `nbt:"minecraft:worldgen/biome"`
		}
		Dimension                 interface{}
		WorldName                 pk.Identifier
		HashedSeed                pk.Long
		MaxPlayers                pk.VarInt
		ViewDistance              pk.VarInt
		RDI, ERS, IsDebug, IsFlat pk.Boolean
	)
	err := p.Scan(
		&EID,
		&Hardcore,
		&Gamemode,
		&PreGamemode,
		pk.Array(&WorldNames),
		pk.NBT(&DimensionCodec),
		pk.NBT(&Dimension),
		&WorldName,
		&HashedSeed,
		&MaxPlayers,
		&ViewDistance,
		&RDI, &ERS, &IsDebug, &IsFlat,
	)
	fmt.Println(err)
}
Output:

<nil>

func (*Packet) UnPack

func (p *Packet) UnPack(r io.Reader, threshold int) error

UnPack in-place decompression a packet

type PluginMessageData

type PluginMessageData []byte

PluginMessageData is only used in LoginPlugin,and it will read all left bytes

func (*PluginMessageData) ReadFrom

func (p *PluginMessageData) ReadFrom(r io.Reader) (n int64, err error)

func (PluginMessageData) WriteTo

func (p PluginMessageData) WriteTo(w io.Writer) (n int64, err error)

type Position

type Position maths.Vec3d

Position x as a 26-bit integer, followed by y as a 12-bit integer, followed by z as a 26-bit integer (all signed, two's complement)

func (*Position) ReadFrom

func (p *Position) ReadFrom(r io.Reader) (n int64, err error)

func (Position) WriteTo

func (p Position) WriteTo(w io.Writer) (n int64, err error)

type Property

type Property struct {
	Name, Value, Signature string
}

func (*Property) ReadFrom

func (p *Property) ReadFrom(r io.Reader) (n int64, err error)

func (Property) WriteTo

func (p Property) WriteTo(w io.Writer) (n int64, err error)

type Short

type Short int16

Short is signed 16-bit integer, two's complement

func (*Short) ReadFrom

func (s *Short) ReadFrom(r io.Reader) (n int64, err error)

func (Short) WriteTo

func (s Short) WriteTo(w io.Writer) (int64, error)

type String

type String string

String is sequence of Unicode scalar values

func (*String) ReadFrom

func (s *String) ReadFrom(r io.Reader) (n int64, err error)

func (String) WriteTo

func (s String) WriteTo(w io.Writer) (int64, error)

type Tuple

type Tuple []any // FieldEncoder, FieldDecoder or both (Field)

func (Tuple) ReadFrom

func (t Tuple) ReadFrom(r io.Reader) (n int64, err error)

ReadFrom read Tuple from io.Reader, panic when any of field don't implement FieldDecoder

func (Tuple) WriteTo

func (t Tuple) WriteTo(w io.Writer) (n int64, err error)

WriteTo write Tuple to io.Writer, panic when any of filed don't implement FieldEncoder

type UUID

type UUID uuid.UUID

UUID encoded as an unsigned 128-bit integer

func (*UUID) ReadFrom

func (u *UUID) ReadFrom(r io.Reader) (n int64, err error)

func (UUID) String

func (u UUID) String() string

func (UUID) WriteTo

func (u UUID) WriteTo(w io.Writer) (n int64, err error)

type UnsignedByte

type UnsignedByte uint8

UnsignedByte is unsigned 8-bit integer

func (*UnsignedByte) ReadFrom

func (u *UnsignedByte) ReadFrom(r io.Reader) (n int64, err error)

func (UnsignedByte) WriteTo

func (u UnsignedByte) WriteTo(w io.Writer) (n int64, err error)

type UnsignedShort

type UnsignedShort uint16

UnsignedShort is unsigned 16-bit integer

func (*UnsignedShort) ReadFrom

func (us *UnsignedShort) ReadFrom(r io.Reader) (n int64, err error)

func (UnsignedShort) WriteTo

func (us UnsignedShort) WriteTo(w io.Writer) (int64, error)

type VarInt

type VarInt int32

VarInt is variable-length data encoding a two's complement signed 32-bit integer

func (*VarInt) ReadFrom

func (v *VarInt) ReadFrom(r io.Reader) (n int64, err error)

func (VarInt) WriteTo

func (v VarInt) WriteTo(w io.Writer) (n int64, err error)

type VarLong

type VarLong int64

VarLong is variable-length data encoding a two's complement signed 64-bit integer

func (*VarLong) ReadFrom

func (v *VarLong) ReadFrom(r io.Reader) (n int64, err error)

func (VarLong) WriteTo

func (v VarLong) WriteTo(w io.Writer) (n int64, err error)

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL