From ac98a931bab92754315774a3ec8db7c4a2c8f7b9 Mon Sep 17 00:00:00 2001 From: Robin Date: Thu, 28 Dec 2023 22:39:45 +0100 Subject: [PATCH] preader/pwriter --- pkg/edition/java/proto/packet/joingame.go | 232 ++++--------------- pkg/edition/java/proto/packet/respawn.go | 5 +- pkg/edition/java/proto/packet/server_data.go | 66 ++---- pkg/edition/java/proto/util/preader.go | 24 ++ pkg/edition/java/proxy/player.go | 6 +- 5 files changed, 87 insertions(+), 246 deletions(-) diff --git a/pkg/edition/java/proto/packet/joingame.go b/pkg/edition/java/proto/packet/joingame.go index fe7bd0fc..e7892f8b 100644 --- a/pkg/edition/java/proto/packet/joingame.go +++ b/pkg/edition/java/proto/packet/joingame.go @@ -45,14 +45,13 @@ type DeathPosition struct { Value int64 } -func (d *DeathPosition) encode(wr io.Writer) error { +func (d *DeathPosition) encode(wr io.Writer) { w := util.PanicWriter(wr) w.Bool(d != nil) if d != nil { w.String(d.Key) w.Int64(d.Value) } - return nil } func decodeDeathPosition(rd io.Reader) (*DeathPosition, error) { @@ -126,10 +125,7 @@ func (j *JoinGame) encode116Up(c *proto.PacketContext, wr io.Writer) error { w.Bool(j.DimensionInfo.DebugType) w.Bool(j.DimensionInfo.Flat) if c.Protocol.GreaterEqual(version.Minecraft_1_19) { - err = j.LastDeathPosition.encode(wr) - if err != nil { - return err - } + j.LastDeathPosition.encode(wr) } if c.Protocol.GreaterEqual(version.Minecraft_1_20) { w.VarInt(j.PortalCooldown) @@ -173,107 +169,34 @@ func (j *JoinGame) encodeLegacy(c *proto.PacketContext, wr io.Writer) error { return nil } func (j *JoinGame) encode1202Up(c *proto.PacketContext, wr io.Writer) error { - err := util.WriteInt(wr, j.EntityID) - if err != nil { - return err - } - err = util.WriteBool(wr, j.Hardcore) - if err != nil { - return err - } - - err = util.WriteStrings(wr, j.LevelNames) - if err != nil { - return err - } - - err = util.WriteVarInt(wr, j.MaxPlayers) - if err != nil { - return err - } - - err = util.WriteVarInt(wr, j.ViewDistance) - if err != nil { - return err - } - err = util.WriteVarInt(wr, j.SimulationDistance) - if err != nil { - return err - } - - err = util.WriteBool(wr, j.ReducedDebugInfo) - if err != nil { - return err - } - err = util.WriteBool(wr, j.ShowRespawnScreen) - if err != nil { - return err - } - err = util.WriteBool(wr, j.DoLimitedCrafting) - if err != nil { - return err - } - - err = util.WriteString(wr, j.DimensionInfo.RegistryIdentifier) - if err != nil { - return err - } + w := util.PanicWriter(wr) + w.Int(j.EntityID) + w.Bool(j.Hardcore) + w.Strings(j.LevelNames) + w.VarInt(j.MaxPlayers) + w.VarInt(j.ViewDistance) + w.VarInt(j.SimulationDistance) + w.Bool(j.ReducedDebugInfo) + w.Bool(j.ShowRespawnScreen) + w.Bool(j.DoLimitedCrafting) + w.String(j.DimensionInfo.RegistryIdentifier) if j.DimensionInfo.LevelName == nil { return errors.New("dimension info level name must not be nil") } - err = util.WriteString(wr, *j.DimensionInfo.LevelName) - if err != nil { - return err - } - err = util.WriteInt64(wr, j.PartialHashedSeed) - if err != nil { - return err - } - - err = util.WriteByte(wr, byte(j.Gamemode)) - if err != nil { - return err - } - err = util.WriteByte(wr, byte(j.PreviousGamemode)) - if err != nil { - return err - } - - err = util.WriteBool(wr, j.DimensionInfo.DebugType) - if err != nil { - return err - } - err = util.WriteBool(wr, j.DimensionInfo.Flat) - if err != nil { - return err - } - - // optional death location + w.String(*j.DimensionInfo.LevelName) + w.Int64(j.PartialHashedSeed) + w.Byte(byte(j.Gamemode)) + w.Byte(byte(j.PreviousGamemode)) + w.Bool(j.DimensionInfo.DebugType) + w.Bool(j.DimensionInfo.Flat) if j.LastDeathPosition != nil { - err = util.WriteBool(wr, true) - if err != nil { - return err - } - err = util.WriteString(wr, j.LastDeathPosition.Key) - if err != nil { - return err - } - err = util.WriteInt64(wr, j.LastDeathPosition.Value) - if err != nil { - return err - } + w.Bool(true) + w.String(j.LastDeathPosition.Key) + w.Int64(j.LastDeathPosition.Value) } else { - err = util.WriteBool(wr, false) - if err != nil { - return err - } - } - - err = util.WriteVarInt(wr, j.PortalCooldown) - if err != nil { - return err + w.Bool(false) } - + w.VarInt(j.PortalCooldown) return nil } @@ -405,83 +328,29 @@ func (j *JoinGame) decode116Up(c *proto.PacketContext, rd io.Reader) (err error) } return nil } - func (j *JoinGame) decode1202Up(c *proto.PacketContext, rd io.Reader) error { - var err error - j.EntityID, err = util.ReadInt(rd) - if err != nil { - return err - } - j.Hardcore, err = util.ReadBool(rd) - if err != nil { - return err - } - - j.LevelNames, err = util.ReadStringArray(rd) - if err != nil { - return err - } - - j.MaxPlayers, err = util.ReadVarInt(rd) - if err != nil { - return err - } - - j.ViewDistance, err = util.ReadVarInt(rd) - if err != nil { - return err - } - j.SimulationDistance, err = util.ReadVarInt(rd) - if err != nil { - return err - } + r := util.PanicReader(rd) - j.ReducedDebugInfo, err = util.ReadBool(rd) - if err != nil { - return err - } - j.ShowRespawnScreen, err = util.ReadBool(rd) - if err != nil { - return err - } - j.DoLimitedCrafting, err = util.ReadBool(rd) - if err != nil { - return err - } + r.Int(&j.EntityID) + r.Bool(&j.Hardcore) + r.Strings(&j.LevelNames) + r.VarInt(&j.MaxPlayers) + r.VarInt(&j.ViewDistance) + r.VarInt(&j.SimulationDistance) + r.Bool(&j.ReducedDebugInfo) + r.Bool(&j.ShowRespawnScreen) + r.Bool(&j.DoLimitedCrafting) - dimensionIdentifier, err := util.ReadString(rd) - if err != nil { - return err - } - levelName, err := util.ReadString(rd) - if err != nil { - return err - } - j.PartialHashedSeed, err = util.ReadInt64(rd) - if err != nil { - return err - } + dimensionIdentifier := util.PReadStringVal(rd) + levelName := util.PReadStringVal(rd) + r.Int64(&j.PartialHashedSeed) - gamemode, err := util.ReadByte(rd) - if err != nil { - return err - } - j.Gamemode = int16(gamemode) + j.Gamemode = int16(util.PReadByteVal(rd)) - previousGamemode, err := util.ReadByte(rd) - if err != nil { - return err - } - j.PreviousGamemode = int16(previousGamemode) + j.PreviousGamemode = int16(util.PReadByteVal(rd)) - isDebug, err := util.ReadBool(rd) - if err != nil { - return err - } - isFlat, err := util.ReadBool(rd) - if err != nil { - return err - } + isDebug := r.Ok() + isFlat := r.Ok() j.DimensionInfo = &DimensionInfo{ RegistryIdentifier: dimensionIdentifier, LevelName: &levelName, @@ -490,24 +359,13 @@ func (j *JoinGame) decode1202Up(c *proto.PacketContext, rd io.Reader) error { } // optional death location - if ok, err := util.ReadBool(rd); err != nil { - return err - } else if ok { - key, err := util.ReadString(rd) - if err != nil { - return err - } - value, err := util.ReadInt64(rd) - if err != nil { - return err - } + if r.Ok() { + key := util.PReadStringVal(rd) + value := util.PReadInt64Val(rd) j.LastDeathPosition = &DeathPosition{Key: key, Value: value} } - j.PortalCooldown, err = util.ReadVarInt(rd) - if err != nil { - return err - } + r.VarInt(&j.PortalCooldown) return nil } diff --git a/pkg/edition/java/proto/packet/respawn.go b/pkg/edition/java/proto/packet/respawn.go index f72a25e6..e33adfbf 100644 --- a/pkg/edition/java/proto/packet/respawn.go +++ b/pkg/edition/java/proto/packet/respawn.go @@ -98,10 +98,7 @@ func (r *Respawn) Encode(c *proto.PacketContext, wr io.Writer) (err error) { // optional death location if c.Protocol.GreaterEqual(version.Minecraft_1_19) { - err = r.LastDeathPosition.encode(wr) - if err != nil { - return err - } + r.LastDeathPosition.encode(wr) } if c.Protocol.GreaterEqual(version.Minecraft_1_20) { err = util.WriteVarInt(wr, r.PortalCooldown) diff --git a/pkg/edition/java/proto/packet/server_data.go b/pkg/edition/java/proto/packet/server_data.go index a4db5c67..f603779a 100644 --- a/pkg/edition/java/proto/packet/server_data.go +++ b/pkg/edition/java/proto/packet/server_data.go @@ -17,12 +17,10 @@ type ServerData struct { } func (s *ServerData) Encode(c *proto.PacketContext, wr io.Writer) error { + w := util.PanicWriter(wr) hasDescription := s.Description != nil if c.Protocol.Lower(version.Minecraft_1_19_4) { - err := util.WriteBool(wr, s.Description != nil) - if err != nil { - return err - } + w.Bool(hasDescription) } if c.Protocol.GreaterEqual(version.Minecraft_1_19_4) || hasDescription { err := util.WriteComponent(wr, c.Protocol, s.Description) @@ -31,86 +29,50 @@ func (s *ServerData) Encode(c *proto.PacketContext, wr io.Writer) error { } } hasFavicon := s.Favicon != "" - err := util.WriteBool(wr, hasFavicon) - if err != nil { - return err - } + w.Bool(hasFavicon) if hasFavicon { if c.Protocol.GreaterEqual(version.Minecraft_1_19_4) { - err = util.WriteBytes(wr, s.Favicon.Bytes()) - if err != nil { - return err - } + w.Bytes(s.Favicon.Bytes()) } else { - err = util.WriteString(wr, string(s.Favicon)) - if err != nil { - return err - } + w.String(string(s.Favicon)) } } if c.Protocol.Lower(version.Minecraft_1_19_3) { - err = util.WriteBool(wr, false) - if err != nil { - return err - } + w.Bool(false) } if c.Protocol.GreaterEqual(version.Minecraft_1_19_1) { - err = util.WriteBool(wr, s.SecureChatEnforced) - if err != nil { - return err - } + w.Bool(s.SecureChatEnforced) } return nil } func (s *ServerData) Decode(c *proto.PacketContext, rd io.Reader) (err error) { + r := util.PanicReader(rd) if c.Protocol.GreaterEqual(version.Minecraft_1_19_4) { s.Description, err = util.ReadComponent(rd, c.Protocol) if err != nil { return err } } else { - ok, err := util.ReadBool(rd) - if err != nil { - return err - } - if ok { + if r.Ok() { s.Description, err = util.ReadComponent(rd, c.Protocol) if err != nil { return err } } } - ok, err := util.ReadBool(rd) - if err != nil { - return err - } - if ok { + if r.Ok() { if c.Protocol.GreaterEqual(version.Minecraft_1_19_4) { - b, err := util.ReadBytes(rd) - if err != nil { - return err - } - s.Favicon = favicon.FromBytes(b) + s.Favicon = favicon.FromBytes(util.PReadBytesVal(rd)) } else { - fi, err := util.ReadString(rd) - if err != nil { - return err - } - s.Favicon = favicon.Favicon(fi) + s.Favicon = favicon.Favicon(util.PReadStringVal(rd)) } } if c.Protocol.Lower(version.Minecraft_1_19_3) { - _, err = util.ReadBool(rd) - if err != nil { - return err - } + _ = r.Ok() } if c.Protocol.GreaterEqual(version.Minecraft_1_19_1) { - s.SecureChatEnforced, err = util.ReadBool(rd) - if err != nil { - return err - } + r.Bool(&s.SecureChatEnforced) } return nil } diff --git a/pkg/edition/java/proto/util/preader.go b/pkg/edition/java/proto/util/preader.go index b3525c99..a6d58743 100644 --- a/pkg/edition/java/proto/util/preader.go +++ b/pkg/edition/java/proto/util/preader.go @@ -114,3 +114,27 @@ func PReadByteVal(rd io.Reader) byte { } return v } + +func PReadStringVal(rd io.Reader) string { + v, err := ReadString(rd) + if err != nil { + panic(err) + } + return v +} + +func PReadInt64Val(rd io.Reader) int64 { + v, err := ReadInt64(rd) + if err != nil { + panic(err) + } + return v +} + +func PReadBytesVal(rd io.Reader) []byte { + v, err := ReadBytes(rd) + if err != nil { + panic(err) + } + return v +} diff --git a/pkg/edition/java/proxy/player.go b/pkg/edition/java/proxy/player.go index f8c889a1..96909d61 100644 --- a/pkg/edition/java/proxy/player.go +++ b/pkg/edition/java/proxy/player.go @@ -55,7 +55,7 @@ type Player interface { // CreateConnectionRequest creates a connection request to begin switching the backend server. CreateConnectionRequest(target RegisteredServer) ConnectionRequest GameProfile() profile.GameProfile // Returns the player's game profile. - PlayerSettings() player.Settings // The player's client settings. Returns player.DefaultSettings if unknown. + Settings() player.Settings // The player's client settings. Returns player.DefaultSettings if unknown. // Disconnect disconnects the player with a reason. // Once called, further interface calls to this player become undefined. Disconnect(reason component.Component) @@ -681,9 +681,9 @@ func (p *connectedPlayer) setClientSettings(settings *packet.ClientSettings) { }) } -// PlayerSettings returns the players client settings. +// Settings returns the players client settings. // If not known already, returns player.DefaultSettings. -func (p *connectedPlayer) PlayerSettings() player.Settings { +func (p *connectedPlayer) Settings() player.Settings { p.mu.RLock() defer p.mu.RUnlock() if p.settings != nil {