Top Level Namespace

Defined Under Namespace

Classes: Array, BigChungusTheChunkGetter, ChatMesage, ClEmoticon, ClSay, Client, ClientInfo, Config, Context, DDNetSnapItem, GameClient, GameInfo, GameServer, InputTiming, Map, MaplistEntryAdd, MaplistEntryRem, NetAddr, NetBase, NetChunk, NetEvent, NetObj, Packer, Packet, PacketFlags, Player, RconCmdAdd, RconCmdRem, RconLine, SecurityToken, ServerInfo, ServerSettings, SnapEventBase, SnapItemBase, Snapshot, SnapshotUnpacker, StartInfo, String, SvClientDrop, TeeworldsClient, TeeworldsServer, Unpacker

Constant Summary collapse

COL_LEN =
9
MAX_NUM_CHUNKS =
1024
SANITIZE =
1
SANITIZE_CC =
2
SKIP_START_WHITESPACES =
4
AVAILABLE_COLORS =
i[
  red green yellow pink magenta blue cyan white
  bg_red bg_green bg_yellow bg_pink bg_magenta bg_blue bg_cyan bg_white
].freeze
GAME_VERSION =
'0.7.5'
GAME_NETVERSION_HASH_FORCED =
'802f1be60a05665f'
GAME_NETVERSION =
"0.7 #{GAME_NETVERSION_HASH_FORCED}".freeze
CLIENT_VERSION =
0x0705
NETMSG_NULL =
0
NETMSG_INFO =
1
NETMSG_MAP_CHANGE =

sent when client should switch map

2
NETMSG_MAP_DATA =

map transfer, contains a chunk of the map file

3
NETMSG_SERVERINFO =
4
NETMSG_CON_READY =

connection is ready, client should send start info

5
NETMSG_SNAP =

normal snapshot, multiple parts

6
NETMSG_SNAPEMPTY =

empty snapshot

7
NETMSG_SNAPSINGLE =

?

8
NETMSG_SNAPSMALL =
9
NETMSG_INPUTTIMING =

reports how off the input was

10
NETMSG_RCON_AUTH_ON =

rcon authentication enabled

11
NETMSG_RCON_AUTH_OFF =

rcon authentication disabled

12
NETMSG_RCON_LINE =

line that should be printed to the remote console

13
NETMSG_RCON_CMD_ADD =
14
NETMSG_RCON_CMD_REM =
15
NETMSG_MAPLIST_ENTRY_ADD =

TODO: 0.8: move up

29
NETMSG_MAPLIST_ENTRY_REM =
30
NETMSG_READY =

sent by client

18
NETMSG_ENTERGAME =
19
NETMSG_INPUT =

contains the inputdata from the client

20
NETMSG_RCON_CMD =
21
NETMSG_RCON_AUTH =
22
NETMSG_REQUEST_MAP_DATA =
23
NETMSG_INVALID =
0
NETMSGTYPE_SV_MOTD =
1
NETMSGTYPE_SV_BROADCAST =
2
NETMSGTYPE_SV_CHAT =
3
NETMSGTYPE_SV_TEAM =
4
NETMSGTYPE_SV_KILLMSG =
5
NETMSGTYPE_SV_TUNEPARAMS =
6
NETMSGTYPE_SV_EXTRAPROJECTILE =
7
NETMSGTYPE_SV_READYTOENTER =
8
NETMSGTYPE_SV_WEAPONPICKUP =
19
NETMSGTYPE_SV_EMOTICON =
10
NETMSGTYPE_SV_VOTECLEAROPTIONS =
11
NETMSGTYPE_SV_VOTEOPTIONLISTADD =
12
NETMSGTYPE_SV_VOTEOPTIONADD =
13
NETMSGTYPE_SV_VOTEOPTIONREMOVE =
14
NETMSGTYPE_SV_VOTESET =
15
NETMSGTYPE_SV_VOTESTATUS =
16
NETMSGTYPE_SV_SERVERSETTINGS =
17
NETMSGTYPE_SV_CLIENTINFO =
18
NETMSGTYPE_SV_GAMEINFO =
19
NETMSGTYPE_SV_CLIENTDROP =
20
NETMSGTYPE_SV_GAMEMSG =
21
NETMSGTYPE_DE_CLIENTENTER =
22
NETMSGTYPE_DE_CLIENTLEAVE =
23
NETMSGTYPE_CL_SAY =
24
NETMSGTYPE_CL_SETTEAM =
25
NETMSGTYPE_CL_SETSPECTATORMODE =
26
NETMSGTYPE_CL_STARTINFO =
27
NETMSGTYPE_CL_KILL =
28
NETMSGTYPE_CL_READYCHANGE =
29
NETMSGTYPE_CL_EMOTICON =
30
NETMSGTYPE_CL_VOTE =
31
NETMSGTYPE_CL_CALLVOTE =
32
NETMSGTYPE_SV_SKINCHANGE =
33
NETMSGTYPE_CL_SKINCHANGE =
34
NETMSGTYPE_SV_RACEFINISH =
35
NETMSGTYPE_SV_CHECKPOINT =
36
NETMSGTYPE_SV_COMMANDINFO =
37
NETMSGTYPE_SV_COMMANDINFOREMOVE =
38
NETMSGTYPE_CL_COMMAND =
39
NUM_NETMSGTYPES =
40
NET_CTRLMSG_KEEPALIVE =
0
NET_CTRLMSG_CONNECT =
1
NET_CTRLMSG_ACCEPT =
2
NET_CTRLMSG_CLOSE =
4
NET_CTRLMSG_TOKEN =
5
NET_MAX_SEQUENCE =
1 << 10
NET_CONNSTATE_OFFLINE =
0
NET_CONNSTATE_TOKEN =
1
NET_CONNSTATE_CONNECT =
2
NET_CONNSTATE_PENDING =
3
NET_CONNSTATE_ONLINE =
4
NET_CONNSTATE_ERROR =
5
NET_MAX_CHUNKHEADERSIZE =
3
NET_PACKETHEADERSIZE =
7
NET_PACKETHEADERSIZE_CONNLESS =
NET_PACKETHEADERSIZE + 2
NET_MAX_PACKETHEADERSIZE =
NET_PACKETHEADERSIZE_CONNLESS
NET_MAX_PACKETSIZE =
1400
NET_MAX_PAYLOAD =
NET_MAX_PACKETSIZE - NET_MAX_PACKETHEADERSIZE
CHAT_NONE =
0
CHAT_ALL =
1
CHAT_TEAM =
2
CHAT_WHISPER =
3
NUM_CHATS =
4
TARGET_SERVER =
-1
PACKET_HEADER_SIZE =
7
VITAL_CHUNK_HEADER_SIZE =
3
NON_VITAL_CHUNK_HEADER_SIZE =
2
MAX_CLIENTS =
64
MAX_PLAYERS =
16
MAP_CHUNK_SIZE =

msg type

NET_MAX_PAYLOAD - NET_MAX_CHUNKHEADERSIZE - 4
SERVER_TICK_SPEED =
50
NETOBJ_INVALID =

should this be moved to some snap related file?

0
NETOBJTYPE_PLAYERINPUT =
1
NETOBJTYPE_PROJECTILE =
2
NETOBJTYPE_LASER =
3
NETOBJTYPE_PICKUP =
4
NETOBJTYPE_FLAG =
5
NETOBJTYPE_GAMEDATA =
6
NETOBJTYPE_GAMEDATATEAM =
7
NETOBJTYPE_GAMEDATAFLAG =
8
NETOBJTYPE_CHARACTERCORE =
9
NETOBJTYPE_CHARACTER =
10
NETOBJTYPE_PLAYERINFO =
11
NETOBJTYPE_SPECTATORINFO =
12
NETOBJTYPE_DE_CLIENTINFO =
13
NETOBJTYPE_DE_GAMEINFO =
14
NETOBJTYPE_DE_TUNEPARAMS =
15
NETEVENTTYPE_COMMON =
16
NETEVENTTYPE_EXPLOSION =
17
NETEVENTTYPE_SPAWN =
18
NETEVENTTYPE_HAMMERHIT =
19
NETEVENTTYPE_DEATH =
20
NETEVENTTYPE_SOUNDWORLD =
21
NETEVENTTYPE_DAMAGE =
22
NETOBJTYPE_PLAYERINFORACE =
23
NETOBJTYPE_GAMEDATARACE =
24
NUM_NETOBJTYPE =
25
TEEWORLDS_NETWORK_VERSION =
'v0.0.3'

Instance Method Summary collapse

Instance Method Details

#bytes_to_str(data) ⇒ Object



108
109
110
# File 'lib/bytes.rb', line 108

def bytes_to_str(data)
  data.unpack('H*').join
end

#data_to_ascii(data) ⇒ Object



11
12
13
14
15
16
17
# File 'lib/bytes.rb', line 11

def data_to_ascii(data)
  ascii = ''
  data.unpack('C*').each do |c|
    ascii += c < 32 || c > 126 ? '.' : c.chr
  end
  ascii
end

#get_byte(data, start = 0, num = 1) ⇒ Object

TODO: remove?



113
114
115
# File 'lib/bytes.rb', line 113

def get_byte(data, start = 0, num = 1)
  data[start...(start + num)].unpack('H*').join.upcase
end

#hexdump_lines(data, width = 2, notes = [], opts = { legend: :long }) ⇒ Object

TODO: make this a gem?!

opts legend: :long legend: :short legend: :inline



27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
# File 'lib/bytes.rb', line 27

def hexdump_lines(data, width = 2, notes = [], opts = { legend: :long })
  byte_groups = data.unpack1('H*').scan(/../).groups_of(4)
  lines = []
  hex = ''
  ascii = ''
  w = 0
  byte = 0
  legend = []
  notes.each do |info|
    color = info.first
    raise "Invalid color '#{color}' valid ones: #{AVAILABLE_COLORS}" unless AVAILABLE_COLORS.include? color

    legend.push([color, info.last.send(color)])
  end
  unless legend.empty?
    case opts[:legend]
    when :long
      legend.each do |leg|
        lines.push("#{leg.first}: #{leg.last}".send(leg.first))
      end
    when :short
      lines.push(legend.map(&:last).join(' '))
    end
  end
  byte_groups.each do |byte_group|
    hex += '  ' unless hex.empty?
    ascii += data_to_ascii(str_bytes(byte_group.join).pack('C*'))
    w += 1
    note = ''
    colors = 0
    notes.each do |info|
      color = info.first
      # p color
      # p info
      from = info[1]
      to = info[1] + (info[2] - 1)

      if from > byte + 3
        # puts "a"
        next
      end
      if to < byte
        # puts "to: #{to} < byte: #{byte}"
        next
      end

      note += " #{info[3]}".send(color) if opts[:legend] == :inline

      from -= byte
      to -= byte
      from = 0 if from.negative?
      to = 3 if to > 3

      # puts "from: #{from} to: #{to}"
      (from..to).each do |i|
        next if byte_group[i].nil?

        byte_group[i] = byte_group[i].send(color)
        colors += 1
      end
    end
    byte += 4
    hex += byte_group.join(' ')
    next unless w >= width

    w = 0
    hex_pad = hex.ljust((width * 4 * 3) + (colors * COL_LEN), ' ')
    ascii_pad = ascii.ljust(width * 4)
    lines.push("#{hex_pad}     #{ascii_pad}#{note}")
    hex = ''
    ascii = ''
  end
  lines.push("#{hex_pad}     #{ascii_pad}#{note}") unless hex.empty?
  lines
end

#pack_msg_id(msg_id, options = { system: false }) ⇒ Object

Turns int into network byte

Takes a NETMSGTYPE_CL_* integer and returns a byte that can be send over the network



11
12
13
# File 'lib/net_base.rb', line 11

def pack_msg_id(msg_id, options = { system: false })
  (msg_id << 1) | (options[:system] ? 1 : 0)
end

#str_bytes(str) ⇒ Object

turn hex string to byte array



104
105
106
# File 'lib/bytes.rb', line 104

def str_bytes(str)
  str.scan(/../).map { |b| b.to_i(16) }
end

#str_hex(data) ⇒ Object

turn byte array into hex string



7
8
9
# File 'lib/bytes.rb', line 7

def str_hex(data)
  data.unpack1('H*').scan(/../).join(' ').upcase
end

#todo_make_this_a_unit_testObject



117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
# File 'lib/bytes.rb', line 117

def todo_make_this_a_unit_test
  notes = [
    [:red, 0, 1, 'foo'],
    [:green, 1, 1, 'bar'],
    [:yellow, 2, 1, 'baz'],
    [:pink, 3, 1, 'bang'],
    [:green, 4, 1, 'bär'],
    [:yellow, 6, 6, 'yee']
  ]

  hexdump_lines("\x01\x41\x02\x03\x03\x03\x03\x03\x03\x03\x03\x03\x03\xef", 40, notes).each do |l|
    puts l
  end

  hexdump_lines("\x01\x41\x02\x03\x03\x03\x03\x03\x03\x03\x03\x03\x03\xef", 40, notes, legend: long).each do |l|
    puts l
  end

  # should not crash when annotating bytes out of range
  hexdump_lines("\x01\x41", 40, notes, legend: :long).each do |l|
    puts l
  end
end