From 1cf0184d605bdd44b810d51c6a625761842f0a12 Mon Sep 17 00:00:00 2001 From: ChillerDragon Date: Sat, 22 Jun 2024 13:15:25 +0800 Subject: [PATCH] Add input timing and dont error on snap empty --- messages7/input_timing.go | 52 +++++++++++++++++++++++++++++++++++++++ network7/network7.go | 15 +++++------ protocol7/connection.go | 4 +++ protocol7/packet.go | 4 +++ 4 files changed, 68 insertions(+), 7 deletions(-) create mode 100644 messages7/input_timing.go diff --git a/messages7/input_timing.go b/messages7/input_timing.go new file mode 100644 index 0000000..6f7a431 --- /dev/null +++ b/messages7/input_timing.go @@ -0,0 +1,52 @@ +package messages7 + +import ( + "slices" + + "github.com/teeworlds-go/teeworlds/chunk7" + "github.com/teeworlds-go/teeworlds/network7" + "github.com/teeworlds-go/teeworlds/packer" +) + +type InputTiming struct { + ChunkHeader *chunk7.ChunkHeader + + IntendedPredTick int + TimeLeft int +} + +func (msg InputTiming) MsgId() int { + return network7.MsgSysInputTiming +} + +func (msg InputTiming) MsgType() network7.MsgType { + return network7.TypeNet +} + +func (msg InputTiming) System() bool { + return true +} + +func (msg InputTiming) Vital() bool { + return false +} + +func (msg InputTiming) Pack() []byte { + return slices.Concat( + packer.PackInt(msg.IntendedPredTick), + packer.PackInt(msg.TimeLeft), + ) +} + +func (msg *InputTiming) Unpack(u *packer.Unpacker) { + msg.IntendedPredTick = u.GetInt() + msg.TimeLeft = u.GetInt() +} + +func (msg *InputTiming) Header() *chunk7.ChunkHeader { + return msg.ChunkHeader +} + +func (msg *InputTiming) SetHeader(header *chunk7.ChunkHeader) { + msg.ChunkHeader = header +} diff --git a/network7/network7.go b/network7/network7.go index 58ab8d3..af6b1e5 100644 --- a/network7/network7.go +++ b/network7/network7.go @@ -9,13 +9,14 @@ const ( MsgCtrlToken = 0x05 MsgCtrlClose = 0x04 - MsgSysInfo = 1 - MsgSysMapChange = 2 - MsgSysConReady = 5 - MsgSysSnapEmpty = 7 - MsgSysSnapSingle = 8 - MsgSysReady = 18 - MsgSysEnterGame = 19 + MsgSysInfo = 1 + MsgSysMapChange = 2 + MsgSysConReady = 5 + MsgSysSnapEmpty = 7 + MsgSysSnapSingle = 8 + MsgSysInputTiming = 10 + MsgSysReady = 18 + MsgSysEnterGame = 19 MsgGameSvMotd = 1 MsgGameSvChat = 3 diff --git a/protocol7/connection.go b/protocol7/connection.go index 61dcb5f..a2475cb 100644 --- a/protocol7/connection.go +++ b/protocol7/connection.go @@ -104,6 +104,10 @@ func (connection *Connection) OnSystemMsg(msg messages7.NetMessage, response *Pa case *messages7.SnapSingle: // fmt.Printf("got snap single tick=%d\n", msg.GameTick) response.Messages = append(response.Messages, &messages7.CtrlKeepAlive{}) + case *messages7.SnapEmpty: + // fmt.Printf("got snap empty tick=%d\n", msg.GameTick) + case *messages7.InputTiming: + // fmt.Printf("timing time left=%d\n", msg.TimeLeft) default: fmt.Printf("unknown system message id=%d payload=%x\n", msg.MsgId(), msg.Pack()) return false diff --git a/protocol7/packet.go b/protocol7/packet.go index 4a06c39..555d476 100644 --- a/protocol7/packet.go +++ b/protocol7/packet.go @@ -97,6 +97,10 @@ func (packet *Packet) unpackSystem(msgId int, chunk chunk7.Chunk, u *packer.Unpa msg := &messages7.SnapEmpty{ChunkHeader: &chunk.Header} msg.Unpack(u) packet.Messages = append(packet.Messages, msg) + } else if msgId == network7.MsgSysInputTiming { + msg := &messages7.InputTiming{ChunkHeader: &chunk.Header} + msg.Unpack(u) + packet.Messages = append(packet.Messages, msg) } else { return false }