Skip to content

Commit

Permalink
noxnet: Move net xfer to a separate package.
Browse files Browse the repository at this point in the history
  • Loading branch information
dennwc committed Jan 28, 2024
1 parent 6818015 commit 629f4ef
Show file tree
Hide file tree
Showing 10 changed files with 406 additions and 293 deletions.
28 changes: 14 additions & 14 deletions noxnet/helpers.go → binenc/string.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package noxnet
package binenc

import (
"bytes"
Expand All @@ -15,30 +15,30 @@ func allZeros(data []byte) bool {
return true
}

func cLen(data []byte) int {
func CLen(data []byte) int {
if i := bytes.IndexByte(data, 0); i >= 0 {
return i
}
return len(data)
}

func cString(data []byte) string {
data = data[:cLen(data)]
func CString(data []byte) string {
data = data[:CLen(data)]
return string(data)
}

func cStringSet0(data []byte, s string) {
func CStringSet0(data []byte, s string) {
zeros(data)
copy(data, s)
data[len(data)-1] = 0
}

func cStringSet(data []byte, s string) int {
func CStringSet(data []byte, s string) int {
zeros(data)
return copy(data, s)
}

func cLen16(data []byte) int {
func CLen16(data []byte) int {
for i := 0; i < len(data)/2; i++ {
v := binary.LittleEndian.Uint16(data[2*i:])
if v == 0 {
Expand All @@ -48,7 +48,7 @@ func cLen16(data []byte) int {
return len(data)
}

func cString16(data []byte) string {
func CString16(data []byte) string {
data16 := make([]uint16, len(data)/2)
for i := range data16 {
v := binary.LittleEndian.Uint16(data[2*i:])
Expand All @@ -61,7 +61,7 @@ func cString16(data []byte) string {
return string(utf16.Decode(data16))
}

func cStringSet16(data []byte, s string) {
func CStringSet16(data []byte, s string) {
zeros(data)
data16 := utf16.Encode([]rune(s))
for i, v := range data16 {
Expand All @@ -75,20 +75,20 @@ func zeros(data []byte) {
}
}

type FixedString struct {
type String struct {
Value string
Junk []byte
}

func (s *FixedString) Encode(data []byte) {
i := cStringSet(data, s.Value)
func (s *String) Encode(data []byte) {
i := CStringSet(data, s.Value)
if len(s.Junk) != 0 {
copy(data[i+1:], s.Junk)
}
}

func (s *FixedString) Decode(data []byte) {
s.Value = cString(data)
func (s *String) Decode(data []byte) {
s.Value = CString(data)
s.Junk = nil
if i := len(s.Value); !allZeros(data[i+1:]) {
s.Junk = make([]byte, len(data)-i-1)
Expand Down
40 changes: 21 additions & 19 deletions noxnet/message_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,9 @@ import (
"testing"

"github.com/shoenig/test/must"

"github.com/noxworld-dev/opennox-lib/binenc"
"github.com/noxworld-dev/opennox-lib/noxnet/xfer"
)

func TestDecodePacket(t *testing.T) {
Expand Down Expand Up @@ -90,7 +93,7 @@ func TestDecodePacket(t *testing.T) {
{
name: "use map",
packet: &MsgUseMap{
MapName: FixedString{
MapName: binenc.String{
Value: "So_Druid.map",
Junk: []byte{0x9, 0x0, 0x80, 0x96, 0x98, 0x0, 0x0, 0x0, 0x0, 0x0, 0x57, 0xd2, 0x30, 0x14, 0x1, 0x0, 0x0, 0x0, 0x13},
},
Expand Down Expand Up @@ -174,7 +177,7 @@ func TestDecodePacket(t *testing.T) {
packet: &MsgMapSendStart{
Unk1: [3]byte{0, 0, 0},
MapSize: 208134,
MapName: FixedString{Value: "_noxtest.map"},
MapName: binenc.String{Value: "_noxtest.map"},
},
},
{
Expand All @@ -196,44 +199,43 @@ func TestDecodePacket(t *testing.T) {
},
{
name: "xfer start motd",
packet: &MsgXfer{&MsgXferStart{
Act: 1,
Unk1: 0,
Size: 376,
Type: FixedString{Value: "MOTD"},
Token: 0,
Unk5: [3]byte{0, 0, 0},
packet: &MsgXfer{&xfer.MsgStart{
Act: 1,
Unk1: 0,
Size: 376,
Type: binenc.String{Value: "MOTD"},
SendID: 0,
Unk5: [3]byte{0, 0, 0},
}},
},
{
name: "xfer accept",
packet: &MsgXfer{&MsgXferState{
Code: XferAccept,
Token: 0,
Stream: 0,
packet: &MsgXfer{&xfer.MsgAccept{
RecvID: 0,
SendID: 0,
}},
},
{
name: "xfer data motd",
packet: &MsgXfer{&MsgXferData{
packet: &MsgXfer{&xfer.MsgData{
Token: 0,
Stream: 0,
RecvID: 0,
Chunk: 1,
Data: []byte("\r\nWelcome to Nox multiplayer!\r\nVisit www.westwood.com for the latest news and updates.\r\n\r\n--------------\r\n\r\nIf you are hosting a game, select a game type and a map \r\nfrom the menu to the right, then click \"GO!\".\r\n\r\n\r\nTo close this message window, click the \"OK\" button.\r\n\r\n\r\n(You can customize this message by editing the file \r\n'motd.txt' found in your Nox game directory)\r\n\x00"),
}},
},
{
name: "xfer ack",
packet: &MsgXfer{&MsgXferAck{
packet: &MsgXfer{&xfer.MsgAck{
Token: 0,
Stream: 0,
RecvID: 0,
Chunk: 1,
}},
},
{
name: "xfer close",
packet: &MsgXfer{&MsgXferClose{
Stream: 0,
packet: &MsgXfer{&xfer.MsgDone{
RecvID: 0,
}},
},
}
Expand Down
10 changes: 6 additions & 4 deletions noxnet/msg_discover.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ package noxnet
import (
"encoding/binary"
"io"

"github.com/noxworld-dev/opennox-lib/binenc"
)

func init() {
Expand Down Expand Up @@ -75,7 +77,7 @@ func (p *MsgServerInfo) Encode(data []byte) (int, error) {
data[0] = p.PlayersCur
data[1] = p.PlayersMax
copy(data[2:7], p.Unk2[:])
cStringSet0(data[7:16], p.MapName)
binenc.CStringSet0(data[7:16], p.MapName)
data[16] = p.Status1
data[17] = p.Status2
copy(data[18:25], p.Unk19[:])
Expand All @@ -86,7 +88,7 @@ func (p *MsgServerInfo) Encode(data []byte) (int, error) {
binary.LittleEndian.PutUint32(data[41:], p.Token)
copy(data[45:65], p.Unk45[:])
copy(data[65:69], p.Unk65[:])
cStringSet0(data[69:], p.ServerName)
binenc.CStringSet0(data[69:], p.ServerName)
return sz, nil
}

Expand All @@ -97,7 +99,7 @@ func (p *MsgServerInfo) Decode(data []byte) (int, error) {
p.PlayersCur = data[0]
p.PlayersMax = data[1]
copy(p.Unk2[:], data[2:7])
p.MapName = cString(data[7:16])
p.MapName = binenc.CString(data[7:16])
p.Status1 = data[16]
p.Status2 = data[17]
copy(p.Unk19[:], data[18:25])
Expand All @@ -108,6 +110,6 @@ func (p *MsgServerInfo) Decode(data []byte) (int, error) {
p.Token = binary.LittleEndian.Uint32(data[41:])
copy(p.Unk45[:], data[45:65])
copy(p.Unk65[:], data[65:69])
p.ServerName = cString(data[69:])
p.ServerName = binenc.CString(data[69:])
return len(data), nil
}
4 changes: 3 additions & 1 deletion noxnet/msg_map_send.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ import (
"errors"
"io"
"math"

"github.com/noxworld-dev/opennox-lib/binenc"
)

func init() {
Expand All @@ -17,7 +19,7 @@ func init() {
type MsgMapSendStart struct {
Unk1 [3]byte
MapSize uint32
MapName FixedString
MapName binenc.String
}

func (*MsgMapSendStart) NetOp() Op {
Expand Down
10 changes: 6 additions & 4 deletions noxnet/msg_server_accept.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ package noxnet
import (
"encoding/binary"
"io"

"github.com/noxworld-dev/opennox-lib/binenc"
)

func init() {
Expand Down Expand Up @@ -71,13 +73,13 @@ func (p *MsgClientAccept) Encode(data []byte) (int, error) {
}
data[0] = p.Unk0
data[1] = p.Unk1
cStringSet16(data[2:68], p.PlayerName)
binenc.CStringSet16(data[2:68], p.PlayerName)
data[68] = p.PlayerClass
data[69] = p.IsFemale
copy(data[70:99], p.Unk70[:])
binary.LittleEndian.PutUint32(data[99:103], p.ScreenWidth)
binary.LittleEndian.PutUint32(data[103:107], p.ScreenHeight)
cStringSet(data[107:129], p.Serial)
binenc.CStringSet(data[107:129], p.Serial)
copy(data[129:155], p.Unk129[:])
return 155, nil
}
Expand All @@ -88,13 +90,13 @@ func (p *MsgClientAccept) Decode(data []byte) (int, error) {
}
p.Unk0 = data[0]
p.Unk1 = data[1]
p.PlayerName = cString16(data[2:68])
p.PlayerName = binenc.CString16(data[2:68])
p.PlayerClass = data[68]
p.IsFemale = data[69]
copy(p.Unk70[:], data[70:99])
p.ScreenWidth = binary.LittleEndian.Uint32(data[99:103])
p.ScreenHeight = binary.LittleEndian.Uint32(data[103:107])
p.Serial = cString(data[107:129])
p.Serial = binenc.CString(data[107:129])
copy(p.Unk129[:], data[129:155])
return 155, nil
}
10 changes: 6 additions & 4 deletions noxnet/msg_server_join.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ package noxnet
import (
"encoding/binary"
"io"

"github.com/noxworld-dev/opennox-lib/binenc"
)

func init() {
Expand Down Expand Up @@ -31,8 +33,8 @@ func (p *MsgServerJoin) Encode(data []byte) (int, error) {
return 0, io.ErrShortBuffer
}
data[0] = p.Unk0
cStringSet16(data[1:53], p.PlayerName)
cStringSet(data[53:75], p.Serial)
binenc.CStringSet16(data[1:53], p.PlayerName)
binenc.CStringSet(data[53:75], p.Serial)
copy(data[75:77], p.Unk75[:])
binary.LittleEndian.PutUint32(data[77:81], p.Version)
copy(data[81:97], p.Unk81[:])
Expand All @@ -44,8 +46,8 @@ func (p *MsgServerJoin) Decode(data []byte) (int, error) {
return 0, io.ErrUnexpectedEOF
}
p.Unk0 = data[0]
p.PlayerName = cString16(data[1:53])
p.Serial = cString(data[53:75])
p.PlayerName = binenc.CString16(data[1:53])
p.Serial = binenc.CString(data[53:75])
copy(p.Unk75[:], data[75:77])
p.Version = binary.LittleEndian.Uint32(data[77:81])
copy(p.Unk81[:], data[81:97])
Expand Down
10 changes: 6 additions & 4 deletions noxnet/msg_text.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ package noxnet
import (
"encoding/binary"
"io"

"github.com/noxworld-dev/opennox-lib/binenc"
)

func init() {
Expand Down Expand Up @@ -46,24 +48,24 @@ func (p *MsgText) Text() string {
if len(data) > sz {
data = data[:sz]
}
return cString(data)
return binenc.CString(data)
}
// UTF-16
data, sz := p.Data, 2*int(p.Size)
if len(data) > sz {
data = data[:sz]
}
return cString16(data)
return binenc.CString16(data)
}

func (p *MsgText) Payload() []byte {
if !p.Flags.Has(TextUTF8) && !p.Flags.Has(TextLocalized) {
i := cLen16(p.Data)
i := binenc.CLen16(p.Data)
if i+2 < len(p.Data) {
return p.Data[i+2:]
}
} else {
i := cLen(p.Data)
i := binenc.CLen(p.Data)
if i+1 < len(p.Data) {
return p.Data[i+1:]
}
Expand Down
4 changes: 3 additions & 1 deletion noxnet/msg_use_map.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,16 @@ package noxnet
import (
"encoding/binary"
"io"

"github.com/noxworld-dev/opennox-lib/binenc"
)

func init() {
RegisterMessage(&MsgUseMap{})
}

type MsgUseMap struct {
MapName FixedString
MapName binenc.String
CRC uint32
T uint32
}
Expand Down
Loading

0 comments on commit 629f4ef

Please sign in to comment.