2022-11-05 16:48:47 +00:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2022-11-13 07:54:46 +00:00
|
|
|
require_relative 'models/player'
|
|
|
|
require_relative 'models/chat_message'
|
2022-11-16 08:37:47 +00:00
|
|
|
require_relative 'messages/input_timing'
|
|
|
|
require_relative 'messages/sv_client_drop'
|
2022-11-16 09:50:54 +00:00
|
|
|
require_relative 'messages/rcon_cmd_add'
|
|
|
|
require_relative 'messages/rcon_cmd_rem'
|
|
|
|
require_relative 'messages/maplist_entry_add'
|
|
|
|
require_relative 'messages/maplist_entry_rem'
|
2022-11-04 15:26:24 +00:00
|
|
|
require_relative 'packer'
|
2022-11-14 09:25:28 +00:00
|
|
|
require_relative 'context'
|
2022-11-05 09:35:40 +00:00
|
|
|
|
2022-11-04 15:26:24 +00:00
|
|
|
class GameClient
|
2022-11-06 19:08:32 +00:00
|
|
|
attr_accessor :players, :pred_game_tick, :ack_game_tick
|
2022-11-04 15:26:24 +00:00
|
|
|
|
|
|
|
def initialize(client)
|
|
|
|
@client = client
|
|
|
|
@players = {}
|
2022-11-06 19:08:32 +00:00
|
|
|
@ack_game_tick = -1
|
|
|
|
@pred_game_tick = 0
|
2022-11-04 15:26:24 +00:00
|
|
|
end
|
|
|
|
|
2022-11-14 09:25:28 +00:00
|
|
|
##
|
|
|
|
# call_hook
|
|
|
|
#
|
|
|
|
# @param: hook_sym [Symbol] name of the symbol to call
|
|
|
|
# @param: context [Context] context object to pass on data
|
|
|
|
# @param: optional [Any] optional 2nd parameter passed to the callback
|
|
|
|
def call_hook(hook_sym, context, optional = nil)
|
|
|
|
@client.hooks[hook_sym].each do |hook|
|
|
|
|
hook.call(context, optional)
|
|
|
|
context.verify
|
2022-11-16 09:50:54 +00:00
|
|
|
return nil if context.canceld?
|
2022-11-14 09:25:28 +00:00
|
|
|
end
|
|
|
|
context
|
|
|
|
end
|
|
|
|
|
2022-11-16 09:50:54 +00:00
|
|
|
def on_auth_on
|
|
|
|
return if call_hook(:auth_on, Context.new(nil)).nil?
|
|
|
|
|
|
|
|
@client.rcon_authed = true
|
|
|
|
puts 'rcon logged in'
|
|
|
|
end
|
|
|
|
|
|
|
|
def on_auth_off
|
|
|
|
return if call_hook(:auth_off, Context.new(nil)).nil?
|
|
|
|
|
|
|
|
@client.rcon_authed = false
|
|
|
|
puts 'rcon logged out'
|
|
|
|
end
|
|
|
|
|
|
|
|
def on_rcon_cmd_add(chunk)
|
|
|
|
todo_rename_this = RconCmdAdd.new(chunk.data[1..])
|
|
|
|
context = Context.new(todo_rename_this)
|
|
|
|
return if call_hook(:rcon_cmd_add, context).nil?
|
|
|
|
|
|
|
|
p context.todo_rename_this
|
|
|
|
end
|
|
|
|
|
|
|
|
def on_rcon_cmd_rem(chunk)
|
|
|
|
todo_rename_this = RconCmdRem.new(chunk.data[1..])
|
|
|
|
context = Context.new(todo_rename_this)
|
|
|
|
return if call_hook(:rcon_cmd_rem, context).nil?
|
|
|
|
|
|
|
|
p context.todo_rename_this
|
|
|
|
end
|
|
|
|
|
|
|
|
def on_maplist_entry_add(chunk)
|
|
|
|
todo_rename_this = MaplistEntryAdd.new(chunk.data[1..])
|
|
|
|
context = Context.new(todo_rename_this)
|
|
|
|
return if call_hook(:maplist_entry_add, context).nil?
|
|
|
|
|
|
|
|
p context.todo_rename_this
|
|
|
|
end
|
|
|
|
|
|
|
|
def on_maplist_entry_rem(chunk)
|
|
|
|
todo_rename_this = MaplistEntryRem.new(chunk.data[1..])
|
|
|
|
context = Context.new(todo_rename_this)
|
|
|
|
return if call_hook(:maplist_entry_rem, context).nil?
|
|
|
|
|
|
|
|
p context.todo_rename_this
|
|
|
|
end
|
|
|
|
|
2022-11-05 09:35:40 +00:00
|
|
|
def on_client_info(chunk)
|
2022-11-04 15:26:24 +00:00
|
|
|
# puts "Got playerinfo flags: #{chunk.flags}"
|
|
|
|
u = Unpacker.new(chunk.data[1..])
|
|
|
|
player = Player.new(
|
2022-11-05 16:19:05 +00:00
|
|
|
id: u.get_int,
|
|
|
|
local: u.get_int,
|
|
|
|
team: u.get_int,
|
|
|
|
name: u.get_string,
|
|
|
|
clan: u.get_string,
|
|
|
|
country: u.get_int
|
|
|
|
)
|
2022-11-04 15:26:24 +00:00
|
|
|
# skinparts and the silent flag
|
|
|
|
# are currently ignored
|
|
|
|
|
2022-11-05 09:35:40 +00:00
|
|
|
context = Context.new(
|
2022-11-15 09:33:05 +00:00
|
|
|
nil,
|
2022-11-05 16:19:05 +00:00
|
|
|
player:,
|
|
|
|
chunk:
|
2022-11-05 09:35:40 +00:00
|
|
|
)
|
2022-11-14 09:25:28 +00:00
|
|
|
return if call_hook(:client_info, context).nil?
|
2022-11-05 09:35:40 +00:00
|
|
|
|
|
|
|
player = context.data[:player]
|
2022-11-04 15:26:24 +00:00
|
|
|
@players[player.id] = player
|
2022-11-05 10:59:36 +00:00
|
|
|
end
|
|
|
|
|
2022-11-15 09:33:05 +00:00
|
|
|
def on_input_timing(chunk)
|
|
|
|
todo_rename_this = InputTiming.new(chunk.data[1..])
|
2022-11-15 11:55:43 +00:00
|
|
|
context = Context.new(todo_rename_this, chunk:)
|
2022-11-15 09:33:05 +00:00
|
|
|
call_hook(:input_timing, context)
|
|
|
|
end
|
|
|
|
|
2022-11-05 10:59:36 +00:00
|
|
|
def on_client_drop(chunk)
|
2022-11-15 11:55:43 +00:00
|
|
|
todo_rename_this = SvClientDrop.new(chunk.data[1..])
|
2022-11-05 10:59:36 +00:00
|
|
|
context = Context.new(
|
2022-11-15 09:33:05 +00:00
|
|
|
nil,
|
2022-11-15 11:55:43 +00:00
|
|
|
player: @players[todo_rename_this.client_id],
|
2022-11-05 16:19:05 +00:00
|
|
|
chunk:,
|
2022-11-15 11:55:43 +00:00
|
|
|
client_id: todo_rename_this.client_id,
|
|
|
|
reason: todo_rename_this.reason,
|
|
|
|
silent: todo_rename_this.silent?
|
2022-11-05 10:59:36 +00:00
|
|
|
)
|
2022-11-14 09:25:28 +00:00
|
|
|
return if call_hook(:client_drop, context).nil?
|
2022-11-05 10:59:36 +00:00
|
|
|
|
|
|
|
@players.delete(context.data[:client_id])
|
2022-11-04 15:26:24 +00:00
|
|
|
end
|
|
|
|
|
2022-11-05 16:19:05 +00:00
|
|
|
def on_ready_to_enter(_chunk)
|
2022-11-05 09:35:40 +00:00
|
|
|
@client.send_enter_game
|
|
|
|
end
|
|
|
|
|
|
|
|
def on_connected
|
2022-11-15 09:33:05 +00:00
|
|
|
context = Context.new(nil)
|
2022-11-14 09:25:28 +00:00
|
|
|
return if call_hook(:connected, context).nil?
|
|
|
|
|
2022-11-13 09:37:46 +00:00
|
|
|
@client.send_msg_start_info
|
2022-11-05 09:35:40 +00:00
|
|
|
end
|
|
|
|
|
2022-11-12 15:47:12 +00:00
|
|
|
def on_disconnect
|
2022-11-15 09:33:05 +00:00
|
|
|
call_hook(:disconnect, Context.new(nil))
|
2022-11-12 15:47:12 +00:00
|
|
|
end
|
|
|
|
|
2022-11-06 17:26:14 +00:00
|
|
|
def on_rcon_line(chunk)
|
|
|
|
u = Unpacker.new(chunk.data[1..])
|
|
|
|
context = Context.new(
|
2022-11-15 09:33:05 +00:00
|
|
|
nil,
|
2022-11-06 17:26:14 +00:00
|
|
|
line: u.get_string
|
|
|
|
)
|
2022-11-14 09:25:28 +00:00
|
|
|
call_hook(:rcon_line, context)
|
2022-11-06 17:26:14 +00:00
|
|
|
end
|
|
|
|
|
2022-11-06 19:08:32 +00:00
|
|
|
def on_snapshot(chunk)
|
|
|
|
u = Unpacker.new(chunk.data)
|
2022-11-16 13:27:14 +00:00
|
|
|
msg_id = u.get_int
|
|
|
|
msg_id >>= 1
|
2022-11-06 19:08:32 +00:00
|
|
|
|
2022-11-16 13:27:14 +00:00
|
|
|
num_parts = 1
|
|
|
|
part = 0
|
2022-11-06 19:08:32 +00:00
|
|
|
game_tick = u.get_int
|
2022-11-16 13:27:14 +00:00
|
|
|
delta_tick = u.get_int
|
|
|
|
part_size = 0
|
|
|
|
crc = 0
|
2022-11-06 19:08:32 +00:00
|
|
|
# complete_size = 0
|
|
|
|
# data = nil
|
|
|
|
|
2022-11-16 16:33:35 +00:00
|
|
|
chunk_num = 0
|
|
|
|
|
2022-11-06 19:08:32 +00:00
|
|
|
# TODO: state check
|
|
|
|
|
2022-11-16 13:27:14 +00:00
|
|
|
if msg_id == NETMSG_SNAP
|
|
|
|
num_parts = u.get_int
|
|
|
|
part = u.get_int
|
|
|
|
end
|
2022-11-06 19:08:32 +00:00
|
|
|
|
2022-11-16 13:27:14 +00:00
|
|
|
unless msg_id == NETMSG_SNAPEMPTY
|
|
|
|
crc = u.get_int
|
|
|
|
part_size = u.get_int
|
2022-11-16 16:33:35 +00:00
|
|
|
chunk_num = u.get_int
|
2022-11-16 13:27:14 +00:00
|
|
|
end
|
2022-11-06 19:08:32 +00:00
|
|
|
|
2022-11-16 14:55:25 +00:00
|
|
|
snap_name = 'SNAP_INVALID'
|
|
|
|
case msg_id
|
|
|
|
when NETMSG_SNAP then snap_name = 'NETMSG_SNAP'
|
|
|
|
when NETMSG_SNAPSINGLE then snap_name = 'NETMSG_SNAPSINGLE'
|
|
|
|
when NETMSG_SNAPEMPTY then snap_name = 'NETMSG_SNAPEMPTY'
|
|
|
|
end
|
|
|
|
|
2022-11-16 17:15:57 +00:00
|
|
|
return unless msg_id == NETMSG_SNAPSINGLE
|
|
|
|
|
2022-11-16 14:55:25 +00:00
|
|
|
puts ">>> snap #{snap_name} (#{msg_id})"
|
|
|
|
puts " id=#{msg_id} game_tick=#{game_tick} delta_tick=#{delta_tick}"
|
2022-11-16 13:27:14 +00:00
|
|
|
puts " num_parts=#{num_parts} part=#{part} crc=#{crc} part_size=#{part_size}"
|
2022-11-16 16:33:35 +00:00
|
|
|
puts " chunk_num=#{chunk_num}"
|
2022-11-16 14:55:25 +00:00
|
|
|
puts "\n header:"
|
2022-11-16 14:45:04 +00:00
|
|
|
|
|
|
|
header = []
|
|
|
|
notes = []
|
|
|
|
u.parsed.each_with_index do |parsed, index|
|
|
|
|
color = (index % 2).zero? ? :green : :pink
|
2022-11-16 14:55:25 +00:00
|
|
|
txt = "#{parsed[:type]} #{parsed[:value]}"
|
|
|
|
txt += " >> 1 = #{parsed[:value] >> 1}" if header.empty?
|
|
|
|
notes.push([color, parsed[:pos], parsed[:len], txt])
|
|
|
|
header += parsed[:raw]
|
2022-11-16 14:45:04 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
hexdump_lines(header.pack('C*'), 1, notes, legend: :inline).each do |hex|
|
2022-11-16 14:55:25 +00:00
|
|
|
puts " #{hex}"
|
2022-11-16 14:45:04 +00:00
|
|
|
end
|
|
|
|
|
2022-11-16 14:55:25 +00:00
|
|
|
puts "\n payload:"
|
2022-11-16 14:45:04 +00:00
|
|
|
data = u.get_raw
|
2022-11-16 17:15:57 +00:00
|
|
|
# [:green, 0, 4, 'who dis?']
|
|
|
|
notes = []
|
|
|
|
# data.groups_of(4).each_with_index do |item, index|
|
|
|
|
# # reverse for little endian
|
|
|
|
# type = item[0...2].reverse.map { |b| b.to_s(2).rjust(8, '0') }.join.to_i(2)
|
|
|
|
# notes.push([:green, index * 4, 2, "type=#{type}"])
|
|
|
|
# next unless item.length == 4
|
|
|
|
|
|
|
|
# # reverse for little endian
|
|
|
|
# id = item[2...4].reverse.map { |b| b.to_s(2).rjust(8, '0') }.join.to_i(2)
|
|
|
|
# notes.push([:yellow, index * 4 + 2, 2, "id=#{id}"])
|
|
|
|
# end
|
|
|
|
|
|
|
|
@sizes = [
|
2022-11-17 07:39:18 +00:00
|
|
|
0,
|
2022-11-16 17:15:57 +00:00
|
|
|
10,
|
|
|
|
6,
|
|
|
|
5,
|
|
|
|
3,
|
|
|
|
3,
|
|
|
|
3,
|
|
|
|
2,
|
|
|
|
4,
|
|
|
|
15,
|
|
|
|
22,
|
|
|
|
3,
|
|
|
|
4,
|
|
|
|
58,
|
|
|
|
5,
|
|
|
|
32,
|
|
|
|
2,
|
|
|
|
2,
|
|
|
|
2,
|
|
|
|
2,
|
|
|
|
3,
|
|
|
|
3,
|
|
|
|
5
|
2022-11-16 11:40:52 +00:00
|
|
|
]
|
2022-11-16 17:15:57 +00:00
|
|
|
|
2022-11-17 07:39:18 +00:00
|
|
|
@snap_items = [
|
|
|
|
{ name: 'placeholder', size: 0 },
|
|
|
|
{ name: 'obj_player_input', size: 10 },
|
|
|
|
{ name: 'obj_projectile', size: 6 },
|
|
|
|
{ name: 'obj_laser', size: 5 },
|
|
|
|
{ name: 'obj_pickup', size: 3 },
|
|
|
|
{ name: 'obj_flag', size: 3 },
|
|
|
|
{ name: 'obj_game_data', size: 3 },
|
|
|
|
{ name: 'obj_game_data_team', size: 2 },
|
|
|
|
{ name: 'obj_game_data_flag', size: 4 },
|
|
|
|
{ name: 'obj_character_core', size: 15 },
|
|
|
|
{ name: 'obj_character', size: 22 },
|
|
|
|
{ name: 'obj_player_info', size: 3 },
|
|
|
|
{ name: 'obj_spectator_info', size: 4 },
|
|
|
|
{ name: 'obj_client_info', size: 58 },
|
|
|
|
{ name: 'obj_game_info', size: 5 },
|
|
|
|
{ name: 'obj_tune_params', size: 32 },
|
|
|
|
{ name: 'event_common', size: 2 },
|
|
|
|
{ name: 'event_explosion', size: 2 },
|
|
|
|
{ name: 'event_spawn', size: 2 },
|
|
|
|
{ name: 'event_hammerhit', size: 2 },
|
|
|
|
{ name: 'event_death', size: 3 },
|
|
|
|
{ name: 'event_sound_world', size: 3 },
|
|
|
|
{ name: 'event_damage', size: 5 }
|
|
|
|
]
|
|
|
|
|
2022-11-16 17:15:57 +00:00
|
|
|
skip = 0
|
|
|
|
(0...data.size).each do |i|
|
|
|
|
skip -= 1
|
|
|
|
next unless skip.negative?
|
|
|
|
|
|
|
|
# reverse for little endian
|
2022-11-17 07:39:18 +00:00
|
|
|
id = data[i...(i + 2)].reverse.map { |b| b.to_s(2).rjust(8, '0') }.join.to_i(2)
|
|
|
|
|
2022-11-17 08:56:10 +00:00
|
|
|
next if data[i + 4].nil? && i > 2
|
2022-11-17 07:39:18 +00:00
|
|
|
|
|
|
|
type = data[(i + 2)...(i + 4)].reverse.map { |b| b.to_s(2).rjust(8, '0') }.join.to_i(2)
|
2022-11-16 17:15:57 +00:00
|
|
|
size = @sizes[type]
|
2022-11-17 07:39:18 +00:00
|
|
|
# p "id=#{id} type=#{type}"
|
2022-11-16 17:15:57 +00:00
|
|
|
|
2022-11-17 08:56:10 +00:00
|
|
|
next if size.nil? && i > 2
|
2022-11-16 17:15:57 +00:00
|
|
|
|
2022-11-17 08:56:10 +00:00
|
|
|
size *= 4
|
2022-11-17 07:39:18 +00:00
|
|
|
|
|
|
|
notes.push([:green, i, 2, "id=#{id}"])
|
|
|
|
notes.push([:pink, i + 2, 2, "type=#{type} (#{@snap_items[type][:name]} size: #{size})"])
|
2022-11-17 08:56:10 +00:00
|
|
|
|
|
|
|
item_payload = data[(i + 4)..]
|
|
|
|
u = Unpacker.new(item_payload)
|
|
|
|
(0...(size / 4)).each do |d|
|
|
|
|
val = u.get_int
|
|
|
|
notes.push([:yellow, i + 4 + (d * 4), 4, "data[#{d}]=#{val}"])
|
|
|
|
end
|
2022-11-16 17:15:57 +00:00
|
|
|
skip += 3 + size
|
|
|
|
|
|
|
|
# next
|
|
|
|
# next unless item.length == 4
|
|
|
|
|
|
|
|
# # reverse for little endian
|
|
|
|
# id = item[2...4].reverse.map { |b| b.to_s(2).rjust(8, '0') }.join.to_i(2)
|
|
|
|
# notes.push([:yellow, (index * 4) + 2, 2, "id=#{id}"])
|
|
|
|
end
|
|
|
|
|
|
|
|
hexdump_lines(data.pack('C*'), 1, notes, legend: :inline).each do |hex|
|
2022-11-16 14:55:25 +00:00
|
|
|
puts " #{hex}"
|
2022-11-16 10:30:13 +00:00
|
|
|
end
|
2022-11-06 19:08:32 +00:00
|
|
|
|
|
|
|
# ack every snapshot no matter how broken
|
|
|
|
@ack_game_tick = game_tick
|
|
|
|
return unless (@pred_game_tick - @ack_game_tick).abs > 10
|
|
|
|
|
|
|
|
@pred_game_tick = @ack_game_tick + 1
|
2022-11-17 08:56:10 +00:00
|
|
|
# exit
|
2022-11-06 19:08:32 +00:00
|
|
|
end
|
|
|
|
|
2022-11-05 16:19:05 +00:00
|
|
|
def on_emoticon(chunk); end
|
2022-11-05 10:34:34 +00:00
|
|
|
|
2022-11-05 09:35:40 +00:00
|
|
|
def on_map_change(chunk)
|
2022-11-15 09:33:05 +00:00
|
|
|
context = Context.new(nil, chunk:)
|
2022-11-14 09:25:28 +00:00
|
|
|
return if call_hook(:map_change, context).nil?
|
|
|
|
|
2022-11-05 09:35:40 +00:00
|
|
|
# ignore mapdownload at all times
|
|
|
|
# and claim to have the map
|
|
|
|
@client.send_msg_ready
|
|
|
|
end
|
|
|
|
|
2022-11-04 15:26:24 +00:00
|
|
|
def on_chat(chunk)
|
|
|
|
u = Unpacker.new(chunk.data[1..])
|
|
|
|
data = {
|
2022-11-05 16:19:05 +00:00
|
|
|
mode: u.get_int,
|
|
|
|
client_id: u.get_int,
|
|
|
|
target_id: u.get_int,
|
|
|
|
message: u.get_string
|
2022-11-04 15:26:24 +00:00
|
|
|
}
|
|
|
|
data[:author] = @players[data[:client_id]]
|
|
|
|
msg = ChatMesage.new(data)
|
|
|
|
|
2022-11-15 09:33:05 +00:00
|
|
|
context = Context.new(nil, chunk:)
|
2022-11-14 09:25:28 +00:00
|
|
|
call_hook(:chat, context, msg)
|
2022-11-04 15:26:24 +00:00
|
|
|
end
|
|
|
|
end
|