Class: NetObj::TuneParams

Inherits:
Object
  • Object
show all
Defined in:
lib/snapshot/items/tune_params.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(hash_or_raw) ⇒ TuneParams

Returns a new instance of TuneParams.



10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
# File 'lib/snapshot/items/tune_params.rb', line 10

def initialize(hash_or_raw)
  @field_names = [:tune_params]
  @fields = @field_names.map do |_|
    0
  end
  @size = @fields.count
  @name = self.class.name
  @notes = [] # hexdump annotation notes
  if hash_or_raw.instance_of?(Hash)
    init_hash(hash_or_raw)
  elsif hash_or_raw.instance_of?(Unpacker)
    init_unpacker(hash_or_raw)
  else
    init_raw(hash_or_raw)
  end
end

Instance Attribute Details

#idObject (readonly)

Returns the value of attribute id.



8
9
10
# File 'lib/snapshot/items/tune_params.rb', line 8

def id
  @id
end

#nameObject (readonly)

Returns the value of attribute name.



8
9
10
# File 'lib/snapshot/items/tune_params.rb', line 8

def name
  @name
end

#notesObject (readonly)

Returns the value of attribute notes.



8
9
10
# File 'lib/snapshot/items/tune_params.rb', line 8

def notes
  @notes
end

#tune_paramsObject

Returns the value of attribute tune_params.



7
8
9
# File 'lib/snapshot/items/tune_params.rb', line 7

def tune_params
  @tune_params
end

Instance Method Details

#init_hash(attr) ⇒ Object



61
62
63
64
65
# File 'lib/snapshot/items/tune_params.rb', line 61

def init_hash(attr)
  @fields_names.each do |name|
    instance_variable_set("@#{name}", attr[name] || 0)
  end
end

#init_raw(data) ⇒ Object



56
57
58
59
# File 'lib/snapshot/items/tune_params.rb', line 56

def init_raw(data)
  u = Unpacker.new(data)
  init_unpacker(u)
end

#init_unpacker(u) ⇒ Object



31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/snapshot/items/tune_params.rb', line 31

def init_unpacker(u)
  @id = u.get_int
  p = u.parsed.last
  @notes.push([:cyan, p[:pos], p[:len], "id=#{@id}"])
  i = 0
  @fields.map! do |_|
    # TODO: as of right now it can get nil values here
    #       the fix would be "u.get_int || 0"
    #       but fixing it would probably make it harder
    #       to debug invalid data
    #
    #       but do rethink this in a later point please :)
    #       for now call .validate() everywhere
    val = u.get_int

    p = u.parsed.last
    color = (i % 2).zero? ? :yellow : :pink
    desc = @field_names[i]
    @notes.push([color, p[:pos], p[:len], "#{desc}=#{val}"])
    i += 1

    val
  end
end

#to_aObject

basically to_network int array the server sends to the client



78
79
80
81
82
83
84
# File 'lib/snapshot/items/tune_params.rb', line 78

def to_a
  arr = []
  @fields.each do |value|
    arr += Packer.pack_int(value)
  end
  arr
end

#to_hObject



67
68
69
70
71
72
73
74
# File 'lib/snapshot/items/tune_params.rb', line 67

def to_h
  hash = {}
  hash[:id] = @id
  @field_names.each_with_index do |name, index|
    hash[name] = @fields[index]
  end
  hash
end

#to_sObject



86
87
88
# File 'lib/snapshot/items/tune_params.rb', line 86

def to_s
  to_h
end

#validateObject



27
28
29
# File 'lib/snapshot/items/tune_params.rb', line 27

def validate
  @fields.select(&:nil?).empty?
end