Skip to content

Commit

Permalink
Horrible refactor to allow unpacking net messages
Browse files Browse the repository at this point in the history
  • Loading branch information
ChillerDragon committed Jun 20, 2024
1 parent a78bf12 commit 81f6b89
Show file tree
Hide file tree
Showing 10 changed files with 79 additions and 10 deletions.
6 changes: 6 additions & 0 deletions messages7/ctrl_connect.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"slices"

"github.com/teeworlds-go/teeworlds/network7"
"github.com/teeworlds-go/teeworlds/packer"
)

type CtrlConnect struct {
Expand Down Expand Up @@ -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())
}
4 changes: 4 additions & 0 deletions messages7/ctrl_keep_alive.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package messages7

import (
"github.com/teeworlds-go/teeworlds/network7"
"github.com/teeworlds-go/teeworlds/packer"
)

type CtrlKeepAlive struct {
Expand All @@ -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) {
}
6 changes: 6 additions & 0 deletions messages7/ctrl_token.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"slices"

"github.com/teeworlds-go/teeworlds/network7"
"github.com/teeworlds-go/teeworlds/packer"
)

type CtrlToken struct {
Expand Down Expand Up @@ -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())
}
4 changes: 4 additions & 0 deletions messages7/enter_game.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package messages7

import (
"github.com/teeworlds-go/teeworlds/network7"
"github.com/teeworlds-go/teeworlds/packer"
)

type EnterGame struct {
Expand All @@ -26,3 +27,6 @@ func (msg EnterGame) Vital() bool {
func (msg EnterGame) Pack() []byte {
return []byte{}
}

func (msg *EnterGame) Unpack(u *packer.Unpacker) {
}
6 changes: 6 additions & 0 deletions messages7/info.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package messages7

import (
"github.com/teeworlds-go/teeworlds/network7"
"github.com/teeworlds-go/teeworlds/packer"
)

type Info struct {
Expand Down Expand Up @@ -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")
}
6 changes: 5 additions & 1 deletion messages7/net_message.go
Original file line number Diff line number Diff line change
@@ -1,11 +1,15 @@
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
MsgType() network7.MsgType
System() bool
Vital() bool
Pack() []byte
Unpack(u *packer.Unpacker)
}
4 changes: 4 additions & 0 deletions messages7/ready.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package messages7

import (
"github.com/teeworlds-go/teeworlds/network7"
"github.com/teeworlds-go/teeworlds/packer"
)

type Ready struct {
Expand All @@ -26,3 +27,6 @@ func (msg Ready) Vital() bool {
func (msg Ready) Pack() []byte {
return []byte{}
}

func (msg *Ready) Unpack(u *packer.Unpacker) {
}
32 changes: 32 additions & 0 deletions messages7/ready_to_enter.go
Original file line number Diff line number Diff line change
@@ -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) {
}
17 changes: 9 additions & 8 deletions protocol7/connection.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,16 +53,16 @@ func (connection *Connection) CtrlToken() *Packet {
response.Header.Flags.Control = true
response.Messages = append(
response.Messages,
messages7.CtrlToken{
&messages7.CtrlToken{
Token: connection.ClientToken,
},
)

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,
Expand Down Expand Up @@ -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)
}
Expand All @@ -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 != "" {
Expand Down Expand Up @@ -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)
Expand Down
4 changes: 3 additions & 1 deletion teeworlds.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down

0 comments on commit 81f6b89

Please sign in to comment.