Module: SteamSerializable

Overview

Included in each Generated Ruby class. Provides access to methods on the class as well as serialization options.

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#constantsObject (readonly)

Variables in this class



13
14
15
# File 'lib/steamd/generator/ruby/steam_serializable.rb', line 13

def constants
  @constants
end

#variablesObject (readonly)

Variables in this class



10
11
12
# File 'lib/steamd/generator/ruby/steam_serializable.rb', line 10

def variables
  @variables
end

Instance Method Details

#constsArray<SerizableConstant>

Returns a list of SerizableConstant objects. Allowing you to serialize each constant

Returns:



53
54
55
# File 'lib/steamd/generator/ruby/steam_serializable.rb', line 53

def consts
  constants.values.map { |const| SerizableConstant.new(const) }
end

#deserialize(io) ⇒ Object Also known as: decode, decode_from

Deserialize the object using an IO stream

Parameters:

  • io (:read)

    The io stream to deserialize



42
43
44
45
# File 'lib/steamd/generator/ruby/steam_serializable.rb', line 42

def deserialize(io)
  (consts + vars).each { |v| send("#{v.name}=", v.deserialize(io)) }
  self
end

#encode_to(io) ⇒ Object

Encode to a given IO stream

Parameters:

  • io (:read)


24
25
26
# File 'lib/steamd/generator/ruby/steam_serializable.rb', line 24

def encode_to(io)
  io.write(serialize)
end

#flag(vars, var) ⇒ Object

Looks up the variable flag. This is used for when variables are of a defined length

ie:

int header_len;
proto<header_len> ProtoBuf name;

The flag in the case of serialization would be the “name” variable as it would need to write that before it can write header len.

The flag in the case of deserialization would be the “header_len” variable because it defines how many bytes to read so we can read the “name” variable



85
86
87
88
# File 'lib/steamd/generator/ruby/steam_serializable.rb', line 85

def flag(vars, var)
  (vars.select { |v| v.modifier_size == var.name } +
          vars.select { |v| v.name == var.modifier_size }).first
end

#initialize(vars, consts = []) ⇒ Object

Sets the variables and constants of this Steam object



16
17
18
19
# File 'lib/steamd/generator/ruby/steam_serializable.rb', line 16

def initialize(vars, consts = [])
  @variables = vars.each_with_object({})   { |v, m| m[v[:name]] = v }
  @constants = consts.each_with_object({}) { |v, m| m[v[:name]] = v }
end

#serializeString Also known as: encode

Serialize the constants and variables into a stream

Returns:

  • (String)

    the byte representation



31
32
33
34
35
36
# File 'lib/steamd/generator/ruby/steam_serializable.rb', line 31

def serialize
  stream = StringIO.new
  stream.set_encoding('BINARY')
  stream.write((consts + vars).map(&:serialize).join)
  stream.string
end

#varsArray<SerializedVariable>

Returns a list of SerializedVariable objects. Allowing you to serialize each constant

Returns:

  • (Array<SerializedVariable>)

    the constants



61
62
63
64
65
66
67
68
69
70
# File 'lib/steamd/generator/ruby/steam_serializable.rb', line 61

def vars
  vars = variables.values.map do |v|
    SerizableVariable.new(v)
  end

  vars.map do |var|
    var.flag = flag(vars, var)
    var
  end
end