Skip to content

Commit

Permalink
protocol/packet: Prepare for packet encoding/decoding merging.
Browse files Browse the repository at this point in the history
  • Loading branch information
Sandertv committed Mar 22, 2023
1 parent c15b801 commit a2c7d94
Show file tree
Hide file tree
Showing 193 changed files with 1,035 additions and 905 deletions.
10 changes: 7 additions & 3 deletions minecraft/conn.go
Original file line number Diff line number Diff line change
Expand Up @@ -679,7 +679,7 @@ func (conn *Conn) handleRequestNetworkSettings(pk *packet.RequestNetworkSettings
conn.expect(packet.IDLogin)
if err := conn.WritePacket(&packet.NetworkSettings{
CompressionThreshold: 512,
CompressionAlgorithm: conn.compression,
CompressionAlgorithm: conn.compression.EncodeCompression(),
}); err != nil {
return fmt.Errorf("error sending network settings: %v", err)
}
Expand All @@ -691,8 +691,12 @@ func (conn *Conn) handleRequestNetworkSettings(pk *packet.RequestNetworkSettings

// handleNetworkSettings handles an incoming NetworkSettings packet, enabling compression for future packets.
func (conn *Conn) handleNetworkSettings(pk *packet.NetworkSettings) error {
conn.enc.EnableCompression(pk.CompressionAlgorithm)
conn.dec.EnableCompression(pk.CompressionAlgorithm)
alg, ok := packet.CompressionByID(pk.CompressionAlgorithm)
if !ok {
return fmt.Errorf("unknown compression algorithm: %v", pk.CompressionAlgorithm)
}
conn.enc.EnableCompression(alg)
conn.dec.EnableCompression(alg)
conn.readyToLogin = true
return nil
}
Expand Down
4 changes: 2 additions & 2 deletions minecraft/listener.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ type ListenConfig struct {
// be disconnected.
AcceptedProtocols []Protocol
// Compression is the packet.Compression to use for packets sent over this Conn. If set to nil, the compression
// will default to packet.FlateCompression.
// will default to packet.flateCompression.
Compression packet.Compression // TODO: Change this to snappy once Windows crashes are resolved.
// FlushRate is the rate at which packets sent are flushed. Packets are buffered for a duration up to
// FlushRate and are compressed/encrypted together to improve compression ratios. The lower this
Expand Down Expand Up @@ -109,7 +109,7 @@ func (cfg ListenConfig) Listen(network string, address string) (*Listener, error
cfg.StatusProvider = NewStatusProvider("Minecraft Server")
}
if cfg.Compression == nil {
cfg.Compression = packet.FlateCompression{}
cfg.Compression = packet.DefaultCompression
}
if cfg.FlushRate == 0 {
cfg.FlushRate = time.Second / 20
Expand Down
7 changes: 7 additions & 0 deletions minecraft/protocol/command.go
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,13 @@ type CommandEnum struct {
Dynamic bool
}

// Marshal encodes/decodes a CommandEnum as a "soft" (dynamic) enum.
func (x *CommandEnum) Marshal(r IO) {
x.Dynamic = true
r.String(&x.Type)
FuncSlice(r, &x.Options, r.String)
}

const (
CommandOriginPlayer = iota
CommandOriginBlock
Expand Down
2 changes: 2 additions & 0 deletions minecraft/protocol/io.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ type IO interface {
UBlockPos(x *BlockPos)
ChunkPos(x *ChunkPos)
SubChunkPos(x *SubChunkPos)
SoundPos(x *mgl32.Vec3)
ByteFloat(x *float32)
Bytes(p *[]byte)
NBT(m *map[string]any, encoding nbt.Encoding)
Expand All @@ -48,6 +49,7 @@ type IO interface {
ItemDescriptorCount(i *ItemDescriptorCount)
MaterialReducer(x *MaterialReducer)
GameRule(x *GameRule)
AbilityValue(x *any)

ShieldID() int32
UnknownEnumOption(value any, enum string)
Expand Down
8 changes: 5 additions & 3 deletions minecraft/protocol/packet/actor_event.go
Original file line number Diff line number Diff line change
Expand Up @@ -92,13 +92,15 @@ func (*ActorEvent) ID() uint32 {

// Marshal ...
func (pk *ActorEvent) Marshal(w *protocol.Writer) {
w.Varuint64(&pk.EntityRuntimeID)
w.Uint8(&pk.EventType)
w.Varint32(&pk.EventData)
pk.marshal(w)
}

// Unmarshal ...
func (pk *ActorEvent) Unmarshal(r *protocol.Reader) {
pk.marshal(r)
}

func (pk *ActorEvent) marshal(r protocol.IO) {
r.Varuint64(&pk.EntityRuntimeID)
r.Uint8(&pk.EventType)
r.Varint32(&pk.EventData)
Expand Down
8 changes: 5 additions & 3 deletions minecraft/protocol/packet/actor_pick_request.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,13 +24,15 @@ func (*ActorPickRequest) ID() uint32 {

// Marshal ...
func (pk *ActorPickRequest) Marshal(w *protocol.Writer) {
w.Int64(&pk.EntityUniqueID)
w.Uint8(&pk.HotBarSlot)
w.Bool(&pk.WithData)
pk.marshal(w)
}

// Unmarshal ...
func (pk *ActorPickRequest) Unmarshal(r *protocol.Reader) {
pk.marshal(r)
}

func (pk *ActorPickRequest) marshal(r protocol.IO) {
r.Int64(&pk.EntityUniqueID)
r.Uint8(&pk.HotBarSlot)
r.Bool(&pk.WithData)
Expand Down
18 changes: 5 additions & 13 deletions minecraft/protocol/packet/add_actor.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,23 +59,15 @@ func (*AddActor) ID() uint32 {

// Marshal ...
func (pk *AddActor) Marshal(w *protocol.Writer) {
w.Varint64(&pk.EntityUniqueID)
w.Varuint64(&pk.EntityRuntimeID)
w.String(&pk.EntityType)
w.Vec3(&pk.Position)
w.Vec3(&pk.Velocity)
w.Float32(&pk.Pitch)
w.Float32(&pk.Yaw)
w.Float32(&pk.HeadYaw)
w.Float32(&pk.BodyYaw)
protocol.Slice(w, &pk.Attributes)
w.EntityMetadata(&pk.EntityMetadata)
protocol.Single(w, &pk.EntityProperties)
protocol.Slice(w, &pk.EntityLinks)
pk.marshal(w)
}

// Unmarshal ...
func (pk *AddActor) Unmarshal(r *protocol.Reader) {
pk.marshal(r)
}

func (pk *AddActor) marshal(r protocol.IO) {
r.Varint64(&pk.EntityUniqueID)
r.Varuint64(&pk.EntityRuntimeID)
r.String(&pk.EntityType)
Expand Down
6 changes: 5 additions & 1 deletion minecraft/protocol/packet/add_behaviour_tree.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,14 @@ func (*AddBehaviourTree) ID() uint32 {

// Marshal ...
func (pk *AddBehaviourTree) Marshal(w *protocol.Writer) {
w.String(&pk.BehaviourTree)
pk.marshal(w)
}

// Unmarshal ...
func (pk *AddBehaviourTree) Unmarshal(r *protocol.Reader) {
pk.marshal(r)
}

func (pk *AddBehaviourTree) marshal(r protocol.IO) {
r.String(&pk.BehaviourTree)
}
6 changes: 5 additions & 1 deletion minecraft/protocol/packet/add_entity.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,14 @@ func (pk *AddEntity) ID() uint32 {

// Marshal ...
func (pk *AddEntity) Marshal(w *protocol.Writer) {
w.Varuint64(&pk.EntityNetworkID)
pk.marshal(w)
}

// Unmarshal ...
func (pk *AddEntity) Unmarshal(r *protocol.Reader) {
pk.marshal(r)
}

func (pk *AddEntity) marshal(r protocol.IO) {
r.Varuint64(&pk.EntityNetworkID)
}
12 changes: 5 additions & 7 deletions minecraft/protocol/packet/add_item_actor.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,17 +40,15 @@ func (*AddItemActor) ID() uint32 {

// Marshal ...
func (pk *AddItemActor) Marshal(w *protocol.Writer) {
w.Varint64(&pk.EntityUniqueID)
w.Varuint64(&pk.EntityRuntimeID)
w.ItemInstance(&pk.Item)
w.Vec3(&pk.Position)
w.Vec3(&pk.Velocity)
w.EntityMetadata(&pk.EntityMetadata)
w.Bool(&pk.FromFishing)
pk.marshal(w)
}

// Unmarshal ...
func (pk *AddItemActor) Unmarshal(r *protocol.Reader) {
pk.marshal(r)
}

func (pk *AddItemActor) marshal(r protocol.IO) {
r.Varint64(&pk.EntityUniqueID)
r.Varuint64(&pk.EntityRuntimeID)
r.ItemInstance(&pk.Item)
Expand Down
10 changes: 5 additions & 5 deletions minecraft/protocol/packet/add_painting.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,15 +32,15 @@ func (*AddPainting) ID() uint32 {

// Marshal ...
func (pk *AddPainting) Marshal(w *protocol.Writer) {
w.Varint64(&pk.EntityUniqueID)
w.Varuint64(&pk.EntityRuntimeID)
w.Vec3(&pk.Position)
w.Varint32(&pk.Direction)
w.String(&pk.Title)
pk.marshal(w)
}

// Unmarshal ...
func (pk *AddPainting) Unmarshal(r *protocol.Reader) {
pk.marshal(r)
}

func (pk *AddPainting) marshal(r protocol.IO) {
r.Varint64(&pk.EntityUniqueID)
r.Varuint64(&pk.EntityRuntimeID)
r.Vec3(&pk.Position)
Expand Down
22 changes: 5 additions & 17 deletions minecraft/protocol/packet/add_player.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,27 +71,15 @@ func (*AddPlayer) ID() uint32 {

// Marshal ...
func (pk *AddPlayer) Marshal(w *protocol.Writer) {
w.UUID(&pk.UUID)
w.String(&pk.Username)
w.Varuint64(&pk.EntityRuntimeID)
w.String(&pk.PlatformChatID)
w.Vec3(&pk.Position)
w.Vec3(&pk.Velocity)
w.Float32(&pk.Pitch)
w.Float32(&pk.Yaw)
w.Float32(&pk.HeadYaw)
w.ItemInstance(&pk.HeldItem)
w.Varint32(&pk.GameType)
w.EntityMetadata(&pk.EntityMetadata)
protocol.Single(w, &pk.EntityProperties)
protocol.Single(w, &pk.AbilityData)
protocol.Slice(w, &pk.EntityLinks)
w.String(&pk.DeviceID)
w.Int32(&pk.BuildPlatform)
pk.marshal(w)
}

// Unmarshal ...
func (pk *AddPlayer) Unmarshal(r *protocol.Reader) {
pk.marshal(r)
}

func (pk *AddPlayer) marshal(r protocol.IO) {
r.UUID(&pk.UUID)
r.String(&pk.Username)
r.Varuint64(&pk.EntityRuntimeID)
Expand Down
13 changes: 5 additions & 8 deletions minecraft/protocol/packet/add_volume_entity.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,18 +34,15 @@ func (*AddVolumeEntity) ID() uint32 {

// Marshal ...
func (pk *AddVolumeEntity) Marshal(w *protocol.Writer) {
w.Uint64(&pk.EntityRuntimeID)
w.NBT(&pk.EntityMetadata, nbt.NetworkLittleEndian)
w.String(&pk.EncodingIdentifier)
w.String(&pk.InstanceIdentifier)
w.UBlockPos(&pk.Bounds[0])
w.UBlockPos(&pk.Bounds[1])
w.Varint32(&pk.Dimension)
w.String(&pk.EngineVersion)
pk.marshal(w)
}

// Unmarshal ...
func (pk *AddVolumeEntity) Unmarshal(r *protocol.Reader) {
pk.marshal(r)
}

func (pk *AddVolumeEntity) marshal(r protocol.IO) {
r.Uint64(&pk.EntityRuntimeID)
r.NBT(&pk.EntityMetadata, nbt.NetworkLittleEndian)
r.String(&pk.EncodingIdentifier)
Expand Down
11 changes: 5 additions & 6 deletions minecraft/protocol/packet/adventure_settings.go
Original file line number Diff line number Diff line change
Expand Up @@ -80,16 +80,15 @@ func (*AdventureSettings) ID() uint32 {

// Marshal ...
func (pk *AdventureSettings) Marshal(w *protocol.Writer) {
w.Varuint32(&pk.Flags)
w.Varuint32(&pk.CommandPermissionLevel)
w.Varuint32(&pk.ActionPermissions)
w.Varuint32(&pk.PermissionLevel)
w.Varuint32(&pk.CustomStoredPermissions)
w.Int64(&pk.PlayerUniqueID)
pk.marshal(w)
}

// Unmarshal ...
func (pk *AdventureSettings) Unmarshal(r *protocol.Reader) {
pk.marshal(r)
}

func (pk *AdventureSettings) marshal(r protocol.IO) {
r.Varuint32(&pk.Flags)
r.Varuint32(&pk.CommandPermissionLevel)
r.Varuint32(&pk.ActionPermissions)
Expand Down
8 changes: 5 additions & 3 deletions minecraft/protocol/packet/agent_action.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,13 +43,15 @@ func (*AgentAction) ID() uint32 {

// Marshal ...
func (pk *AgentAction) Marshal(w *protocol.Writer) {
w.String(&pk.Identifier)
w.Varint32(&pk.Action)
w.ByteSlice(&pk.Response)
pk.marshal(w)
}

// Unmarshal ...
func (pk *AgentAction) Unmarshal(r *protocol.Reader) {
pk.marshal(r)
}

func (pk *AgentAction) marshal(r protocol.IO) {
r.String(&pk.Identifier)
r.Varint32(&pk.Action)
r.ByteSlice(&pk.Response)
Expand Down
10 changes: 5 additions & 5 deletions minecraft/protocol/packet/animate.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,15 +38,15 @@ func (*Animate) ID() uint32 {

// Marshal ...
func (pk *Animate) Marshal(w *protocol.Writer) {
w.Varint32(&pk.ActionType)
w.Varuint64(&pk.EntityRuntimeID)
if pk.ActionType&0x80 != 0 {
w.Float32(&pk.BoatRowingTime)
}
pk.marshal(w)
}

// Unmarshal ...
func (pk *Animate) Unmarshal(r *protocol.Reader) {
pk.marshal(r)
}

func (pk *Animate) marshal(r protocol.IO) {
r.Varint32(&pk.ActionType)
r.Varuint64(&pk.EntityRuntimeID)
if pk.ActionType&0x80 != 0 {
Expand Down
12 changes: 5 additions & 7 deletions minecraft/protocol/packet/animate_entity.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,17 +36,15 @@ func (*AnimateEntity) ID() uint32 {

// Marshal ...
func (pk *AnimateEntity) Marshal(w *protocol.Writer) {
w.String(&pk.Animation)
w.String(&pk.NextState)
w.String(&pk.StopCondition)
w.Int32(&pk.StopConditionVersion)
w.String(&pk.Controller)
w.Float32(&pk.BlendOutTime)
protocol.FuncSlice(w, &pk.EntityRuntimeIDs, w.Varuint64)
pk.marshal(w)
}

// Unmarshal ...
func (pk *AnimateEntity) Unmarshal(r *protocol.Reader) {
pk.marshal(r)
}

func (pk *AnimateEntity) marshal(r protocol.IO) {
r.String(&pk.Animation)
r.String(&pk.NextState)
r.String(&pk.StopCondition)
Expand Down
7 changes: 5 additions & 2 deletions minecraft/protocol/packet/anvil_damage.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,15 @@ func (*AnvilDamage) ID() uint32 {

// Marshal ...
func (pk *AnvilDamage) Marshal(w *protocol.Writer) {
w.Uint8(&pk.Damage)
w.UBlockPos(&pk.AnvilPosition)
pk.marshal(w)
}

// Unmarshal ..
func (pk *AnvilDamage) Unmarshal(r *protocol.Reader) {
pk.marshal(r)
}

func (pk *AnvilDamage) marshal(r protocol.IO) {
r.Uint8(&pk.Damage)
r.UBlockPos(&pk.AnvilPosition)
}
6 changes: 5 additions & 1 deletion minecraft/protocol/packet/automation_client_connect.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,14 @@ func (*AutomationClientConnect) ID() uint32 {

// Marshal ...
func (pk *AutomationClientConnect) Marshal(w *protocol.Writer) {
w.String(&pk.ServerURI)
pk.marshal(w)
}

// Unmarshal ...
func (pk *AutomationClientConnect) Unmarshal(r *protocol.Reader) {
pk.marshal(r)
}

func (pk *AutomationClientConnect) marshal(r protocol.IO) {
r.String(&pk.ServerURI)
}

0 comments on commit a2c7d94

Please sign in to comment.