diff --git a/messages7/ctrl_connect.go b/messages7/ctrl_connect.go index 23d3db5..f72cfe1 100644 --- a/messages7/ctrl_connect.go +++ b/messages7/ctrl_connect.go @@ -4,6 +4,7 @@ import ( "slices" "github.com/teeworlds-go/teeworlds/network7" + "github.com/teeworlds-go/teeworlds/packer" ) type CtrlConnect struct { @@ -33,3 +34,8 @@ func (msg CtrlConnect) Pack() []byte { []byte{512: 0}, ) } + +// TODO: no idea if this works +func (msg *CtrlConnect) Unpack(u *packer.Unpacker) { + msg.Token = [4]byte(u.Data()) +} diff --git a/messages7/ctrl_keep_alive.go b/messages7/ctrl_keep_alive.go index 2eaa15e..200ab8f 100644 --- a/messages7/ctrl_keep_alive.go +++ b/messages7/ctrl_keep_alive.go @@ -2,6 +2,7 @@ package messages7 import ( "github.com/teeworlds-go/teeworlds/network7" + "github.com/teeworlds-go/teeworlds/packer" ) type CtrlKeepAlive struct { @@ -26,3 +27,6 @@ func (msg CtrlKeepAlive) Vital() bool { func (msg CtrlKeepAlive) Pack() []byte { return []byte{network7.MsgCtrlKeepAlive} } + +func (msg *CtrlKeepAlive) Unpack(u *packer.Unpacker) { +} diff --git a/messages7/ctrl_token.go b/messages7/ctrl_token.go index 724b2ea..570916d 100644 --- a/messages7/ctrl_token.go +++ b/messages7/ctrl_token.go @@ -7,6 +7,7 @@ import ( "slices" "github.com/teeworlds-go/teeworlds/network7" + "github.com/teeworlds-go/teeworlds/packer" ) type CtrlToken struct { @@ -36,3 +37,8 @@ func (msg CtrlToken) Pack() []byte { []byte{512: 0}, ) } + +// TODO: no idea if this works +func (msg *CtrlToken) Unpack(u *packer.Unpacker) { + msg.Token = [4]byte(u.Data()) +} diff --git a/messages7/enter_game.go b/messages7/enter_game.go index 2572098..d9a3240 100644 --- a/messages7/enter_game.go +++ b/messages7/enter_game.go @@ -2,6 +2,7 @@ package messages7 import ( "github.com/teeworlds-go/teeworlds/network7" + "github.com/teeworlds-go/teeworlds/packer" ) type EnterGame struct { @@ -26,3 +27,6 @@ func (msg EnterGame) Vital() bool { func (msg EnterGame) Pack() []byte { return []byte{} } + +func (msg *EnterGame) Unpack(u *packer.Unpacker) { +} diff --git a/messages7/info.go b/messages7/info.go index a60e2e8..9784eb5 100644 --- a/messages7/info.go +++ b/messages7/info.go @@ -2,6 +2,7 @@ package messages7 import ( "github.com/teeworlds-go/teeworlds/network7" + "github.com/teeworlds-go/teeworlds/packer" ) type Info struct { @@ -31,3 +32,8 @@ func (msg Info) Pack() []byte { 0x5F, 0x31, 0x32, 0x33, 0x00, 0x85, 0x1C, 0x00, } } + +func (msg *Info) Unpack(u *packer.Unpacker) { + // TODO: implement + panic("not implemented") +} diff --git a/messages7/net_message.go b/messages7/net_message.go index 3016c74..578ab12 100644 --- a/messages7/net_message.go +++ b/messages7/net_message.go @@ -1,6 +1,9 @@ package messages7 -import "github.com/teeworlds-go/teeworlds/network7" +import ( + "github.com/teeworlds-go/teeworlds/network7" + "github.com/teeworlds-go/teeworlds/packer" +) type NetMessage interface { MsgId() int @@ -8,4 +11,5 @@ type NetMessage interface { System() bool Vital() bool Pack() []byte + Unpack(u *packer.Unpacker) } diff --git a/messages7/ready.go b/messages7/ready.go index e58fff5..1b452aa 100644 --- a/messages7/ready.go +++ b/messages7/ready.go @@ -2,6 +2,7 @@ package messages7 import ( "github.com/teeworlds-go/teeworlds/network7" + "github.com/teeworlds-go/teeworlds/packer" ) type Ready struct { @@ -26,3 +27,6 @@ func (msg Ready) Vital() bool { func (msg Ready) Pack() []byte { return []byte{} } + +func (msg *Ready) Unpack(u *packer.Unpacker) { +} diff --git a/messages7/ready_to_enter.go b/messages7/ready_to_enter.go new file mode 100644 index 0000000..4286e45 --- /dev/null +++ b/messages7/ready_to_enter.go @@ -0,0 +1,32 @@ +package messages7 + +import ( + "github.com/teeworlds-go/teeworlds/network7" + "github.com/teeworlds-go/teeworlds/packer" +) + +type ReadyToEnter struct { +} + +func (msg ReadyToEnter) MsgId() int { + return network7.MsgGameReadyToEnter +} + +func (msg ReadyToEnter) MsgType() network7.MsgType { + return network7.TypeNet +} + +func (msg ReadyToEnter) System() bool { + return false +} + +func (msg ReadyToEnter) Vital() bool { + return true +} + +func (msg ReadyToEnter) Pack() []byte { + return []byte{} +} + +func (msg *ReadyToEnter) Unpack(u *packer.Unpacker) { +} diff --git a/protocol7/connection.go b/protocol7/connection.go index 24a7bea..afc3d1a 100644 --- a/protocol7/connection.go +++ b/protocol7/connection.go @@ -53,7 +53,7 @@ func (connection *Connection) CtrlToken() *Packet { response.Header.Flags.Control = true response.Messages = append( response.Messages, - messages7.CtrlToken{ + &messages7.CtrlToken{ Token: connection.ClientToken, }, ) @@ -61,8 +61,8 @@ func (connection *Connection) CtrlToken() *Packet { return response } -func (client *Connection) MsgStartInfo() messages7.ClStartInfo { - return messages7.ClStartInfo{ +func (client *Connection) MsgStartInfo() *messages7.ClStartInfo { + return &messages7.ClStartInfo{ Name: "gopher", Clan: "", Country: 0, @@ -98,14 +98,14 @@ func byteSliceToString(s []byte) string { func (connection *Connection) OnSystemMsg(msg int, chunk chunk7.Chunk, u *packer.Unpacker, result *PacketResult) { if msg == network7.MsgSysMapChange { fmt.Println("got map change") - result.Response.Messages = append(result.Response.Messages, messages7.Ready{}) + result.Response.Messages = append(result.Response.Messages, &messages7.Ready{}) } else if msg == network7.MsgSysConReady { fmt.Println("got ready") result.Response.Messages = append(result.Response.Messages, connection.MsgStartInfo()) } else if msg == network7.MsgSysSnapSingle { // tick := u.GetInt() // fmt.Printf("got snap single tick=%d\n", tick) - result.Response.Messages = append(result.Response.Messages, messages7.CtrlKeepAlive{}) + result.Response.Messages = append(result.Response.Messages, &messages7.CtrlKeepAlive{}) } else { fmt.Printf("unknown system message id=%d data=%x\n", msg, chunk.Data) } @@ -123,7 +123,8 @@ func (client *Connection) OnMotd(motd string) { func (client *Connection) OnGameMsg(msg int, chunk chunk7.Chunk, u *packer.Unpacker, result *PacketResult) { if msg == network7.MsgGameReadyToEnter { fmt.Println("got ready to enter") - result.Response.Messages = append(result.Response.Messages, messages7.EnterGame{}) + result.Packet.Messages = append(result.Packet.Messages, &messages7.Ready{}) + result.Response.Messages = append(result.Response.Messages, &messages7.EnterGame{}) } else if msg == network7.MsgGameSvMotd { motd := u.GetString() if motd != "" { @@ -203,14 +204,14 @@ func (connection *Connection) OnPacket(data []byte) (*PacketResult, error) { fmt.Printf("got server token %x\n", connection.ServerToken) result.Response.Messages = append( result.Response.Messages, - messages7.CtrlConnect{ + &messages7.CtrlConnect{ Token: connection.ClientToken, }, ) } else if ctrlMsg == network7.MsgCtrlAccept { fmt.Println("got accept") // TODO: don't hardcode info - result.Response.Messages = append(result.Response.Messages, messages7.Info{}) + result.Response.Messages = append(result.Response.Messages, &messages7.Info{}) } else if ctrlMsg == network7.MsgCtrlClose { // TODO: get length from packet header to determine if a reason is set or not // len(data) -> is 1400 (maxPacketLen) diff --git a/teeworlds.go b/teeworlds.go index 8fe42a0..fbb53da 100644 --- a/teeworlds.go +++ b/teeworlds.go @@ -76,7 +76,9 @@ func main() { // example of modifying outgoing traffic for i, msg := range result.Response.Messages { if msg.MsgId() == network7.MsgCtrlConnect { - if connect, ok := result.Response.Messages[0].(messages7.CtrlConnect); ok { + var connect *messages7.CtrlConnect + var ok bool + if connect, ok = result.Response.Messages[0].(*messages7.CtrlConnect); ok { connect.Token = [4]byte{0xaa, 0xaa, 0xaa, 0xaa} result.Response.Messages[i] = connect }