Start snap item refactor (break everything)

This commit is contained in:
ChillerDragon 2022-11-18 10:34:47 +01:00
parent 396b539cca
commit 2da6b01738
4 changed files with 207 additions and 165 deletions

View file

@ -154,7 +154,9 @@ class GameClient
end end
def on_snapshot(chunk) def on_snapshot(chunk)
game_tick = snap_single(chunk) u = SnapshotUnpacker.new
game_tick = u.snap_single(chunk)
return if game_tick.nil?
# ack every snapshot no matter how broken # ack every snapshot no matter how broken
@ack_game_tick = game_tick @ack_game_tick = game_tick

View file

@ -16,14 +16,17 @@ class NetObj
0 0
end end
@size = @fields.count @size = @fields.count
@name = self.class.name
if hash_or_raw.instance_of?(Hash) if hash_or_raw.instance_of?(Hash)
init_hash(hash_or_raw) init_hash(hash_or_raw)
elsif hash_or_raw.instance_of?(Unpacker)
init_unpacker(hash_or_raw)
else else
init_raw(hash_or_raw) init_raw(hash_or_raw)
end end
end end
def match_type?(type) def self.match_type?(type)
type == NETOBJTYPE_GAMEDATA type == NETOBJTYPE_GAMEDATA
end end
@ -31,8 +34,7 @@ class NetObj
@fields.select(&:nil?).empty? @fields.select(&:nil?).empty?
end end
def init_raw(data) def init_unpacker(u)
u = Unpacker.new(data)
@fields.map! do |_| @fields.map! do |_|
# TODO: as of right now it can get nil values here # TODO: as of right now it can get nil values here
# the fix would be "u.get_int || 0" # the fix would be "u.get_int || 0"
@ -45,6 +47,11 @@ class NetObj
end end
end end
def init_raw(data)
u = Unpacker.new(data)
init_unpacker(u)
end
def init_hash(attr) def init_hash(attr)
@fields_names.each do |name| @fields_names.each do |name|
instance_variable_set("@#{name}", attr[name] || 0) instance_variable_set("@#{name}", attr[name] || 0)

View file

@ -1,7 +1,9 @@
# frozen_string_literal: true # frozen_string_literal: true
require_relative 'snap_items/game_data'
require_relative 'packer' require_relative 'packer'
class SnapshotUnpacker
def snap_single(chunk) def snap_single(chunk)
u = Unpacker.new(chunk.data) u = Unpacker.new(chunk.data)
msg_id = u.get_int msg_id = u.get_int
@ -138,34 +140,64 @@ def snap_single(chunk)
notes.push([:red, p[:pos], p[:len], "del[#{i}]=#{deleted}"]) notes.push([:red, p[:pos], p[:len], "del[#{i}]=#{deleted}"])
end end
item_delta = u.get_int item_type = u.get_int
while item_delta id_parsed = u.parsed.last
item_type = item_delta while item_type
item_meta = @snap_items[item_type] obj = nil
item_name = item_meta[:name] if NetObj::GameData.match_type?(item_type)
obj = NetObj::GameData.new(u)
p = u.parsed.last p obj
notes.push([:green, p[:pos], p[:len], "type = #{item_type} #{item_name}"]) else
# puts "no match #{item_type}"
item_id = u.get_int
p = u.parsed.last
notes.push([:cyan, p[:pos], p[:len], "id=#{item_id}"])
size = item_meta[:size]
(0...size).each do |i|
val = u.get_int
p = u.parsed.last
color = (i % 2).zero? ? :yellow : :pink
fields = item_meta[:fields]
desc = ''
desc = fields[i][:name] unless fields.nil? || fields[i].nil?
notes.push([color, p[:pos], p[:len], "data[#{i}]=#{val} #{desc}"])
end end
item_delta = u.get_int if obj
notes.push([
:green,
id_parsed[:pos],
id_parsed[:len],
"type=#{item_type}"
])
else
notes.push([
:bg_red,
id_parsed[:pos],
id_parsed[:len],
"invalid_type=#{item_type}"
])
end end
item_type = u.get_int
id_parsed = u.parsed.last
end
# item_delta = u.get_int
# while item_delta
# item_type = item_delta
# item_meta = @snap_items[item_type]
# item_name = item_meta[:name]
# p = u.parsed.last
# notes.push([:green, p[:pos], p[:len], "type = #{item_type} #{item_name}"])
# item_id = u.get_int
# p = u.parsed.last
# notes.push([:cyan, p[:pos], p[:len], "id=#{item_id}"])
# size = item_meta[:size]
# (0...size).each do |i|
# val = u.get_int
# p = u.parsed.last
# color = (i % 2).zero? ? :yellow : :pink
# fields = item_meta[:fields]
# desc = ''
# desc = fields[i][:name] unless fields.nil? || fields[i].nil?
# notes.push([color, p[:pos], p[:len], "data[#{i}]=#{val} #{desc}"])
# end
# item_delta = u.get_int
# end
hexdump_lines(data.pack('C*'), 1, notes, legend: :inline).each do |hex| hexdump_lines(data.pack('C*'), 1, notes, legend: :inline).each do |hex|
puts " #{hex}" puts " #{hex}"
end end
game_tick game_tick
end end
end

View file

@ -2,6 +2,7 @@
AVAILABLE_COLORS = %i[ AVAILABLE_COLORS = %i[
red green yellow pink magenta blue cyan white 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 ].freeze
# String color # String color