Unhardcode more chunk headers

This commit is contained in:
ChillerDragon 2022-11-04 10:12:23 +01:00
parent 68e8cb6dd0
commit 2919feb529
4 changed files with 32 additions and 20 deletions

View file

@ -4,7 +4,7 @@ require_relative 'bytes'
class NetChunk class NetChunk
attr_reader :next, :data, :msg, :sys, :flags attr_reader :next, :data, :msg, :sys, :flags
@@sent_vital_chunks = 3 # BIG TODO: SEND READY AND SHIT WITH PROPER HEADER @@sent_vital_chunks = 1
def initialize(data) def initialize(data)
@next = nil @next = nil
@ -20,6 +20,13 @@ class NetChunk
@next = data[chunk_end..] if data.size > chunk_end @next = data[chunk_end..] if data.size > chunk_end
end end
def to_s
"NetChunk\n" +
" msg=#{msg} sys=#{sys}\n" +
" #{@flags}\n" +
" data: #{str_hex(data)}"
end
## ##
# Create int array ready to be send over the network # Create int array ready to be send over the network
# #

View file

@ -6,6 +6,7 @@ class NetBase
@port = nil @port = nil
@s = nil @s = nil
@ack = 0 @ack = 0
@server_token = [0xFF, 0xFF, 0xFF, 0xFF].map { |b| b.to_s(16) }.join('')
end end
def connect(socket, ip, port) def connect(socket, ip, port)

View file

@ -16,15 +16,10 @@ class TwClient
def initialize(options = {}) def initialize(options = {})
@verbose = options[:verbose] || false @verbose = options[:verbose] || false
@client_token = MY_TOKEN.map { |b| b.to_s(16) }.join('')
puts "client token #{@client_token}"
@state = NET_CONNSTATE_OFFLINE @state = NET_CONNSTATE_OFFLINE
@ip = 'localhost' @ip = 'localhost'
@port = 8303 @port = 8303
@packet_flags = {} @packet_flags = {}
@ticks = 0
@netbase = NetBase.new
@netbase.client_token = @client_token
@hooks = {} @hooks = {}
@thread_running = false @thread_running = false
@signal_disconnect = false @signal_disconnect = false
@ -42,6 +37,11 @@ class TwClient
return return
end end
end end
@ticks = 0
@client_token = MY_TOKEN.map { |b| b.to_s(16) }.join('')
puts "client token #{@client_token}"
@netbase = NetBase.new
@netbase.client_token = @client_token
@ip = ip @ip = ip
@port = port @port = port
puts "connecting to #{@ip}:#{@port} .." puts "connecting to #{@ip}:#{@port} .."
@ -87,14 +87,14 @@ class TwClient
end end
def send_msg_connect() def send_msg_connect()
header = [0x04, 0x00, 0x00] + str_bytes(@token) msg = [NET_CTRLMSG_CONNECT] + str_bytes(@client_token) + Array.new(501, 0x00)
msg = header + [NET_CTRLMSG_CONNECT] + str_bytes(@client_token) + Array.new(501, 0x00) @netbase.send_packet(msg, 0, control: true)
@s.send(msg.pack('C*'), 0, @ip, @port)
end end
def send_ctrl_with_token() def send_ctrl_with_token()
@state = NET_CONNSTATE_TOKEN @state = NET_CONNSTATE_TOKEN
@s.send(MSG_TOKEN.pack('C*'), 0, @ip, @port) msg = [NET_CTRLMSG_TOKEN] + str_bytes(@client_token) + Array.new(512, 0x00)
@netbase.send_packet(msg, 0, control: true)
end end
def send_info() def send_info()
@ -102,21 +102,25 @@ class TwClient
end end
def send_msg_startinfo() def send_msg_startinfo()
header = [0x00, 0x04, 0x01] + str_bytes(@token) # todo: build startinfo chunk here
msg = header + MSG_STARTINFO
@s.send(msg.pack('C*'), 0, @ip, @port) # create unused chunk just to bump
# the sequence number ._.
NetChunk.create_vital_header({vital: true}, 1)
@netbase.send_packet(MSG_STARTINFO)
end end
def send_msg_ready() def send_msg_ready()
header = [0x00, 0x01, 0x01] + str_bytes(@token) @netbase.send_packet(
msg = header + [0x40, 0x01, 0x02, 0x25] NetChunk.create_vital_header({vital: true}, 1) +
@s.send(msg.pack('C*'), 0, @ip, @port) [pack_msg_id(NETMSG_READY, system: true)])
end end
def send_enter_game() def send_enter_game()
@netbase.send_packet( @netbase.send_packet(
NetChunk.create_vital_header({vital: true}, 1) + NetChunk.create_vital_header({vital: true}, 1) +
[pack_msg_id(NETMSG_ENTERGAME, true)]) [pack_msg_id(NETMSG_ENTERGAME, system: true)])
end end
## ##
@ -125,8 +129,8 @@ class TwClient
# Takes a NETMSGTYPE_CL_* integer # Takes a NETMSGTYPE_CL_* integer
# and returns a byte that can be send over # and returns a byte that can be send over
# the network # the network
def pack_msg_id(msg_id, system = false) def pack_msg_id(msg_id, options = {system: false})
(msg_id << 1) | (system ? 1 : 0) (msg_id << 1) | (options[:system] ? 1 : 0)
end end
def send_chat(str) def send_chat(str)

View file

@ -31,7 +31,7 @@ client.hook_chat do |msg|
puts "chat: #{msg}" puts "chat: #{msg}"
end end
client.connect(args[:ip], args[:port], detach: true) client.connect(args[:ip], args[:port], detach: false)
loop do loop do
sleep 2 sleep 2