module SteamSerializable

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

Attributes

constants[R]

Variables in this class

variables[R]

Variables in this class

Public Class Methods

new(vars, consts = []) click to toggle source

Sets the variables and constants of this Steam object

# 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

Public Instance Methods

consts() click to toggle source

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

@return [Array<SerizableConstant>] the constants

# File lib/steamd/generator/ruby/steam_serializable.rb, line 53
def consts
  constants.values.map { |const| SerizableConstant.new(const) }
end
decode(io)
Alias for: deserialize
decode_from(io)
Alias for: deserialize
deserialize(io) click to toggle source

Deserialize the object using an IO stream

@param io [:read] The io stream to deserialize

# 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
Also aliased as: decode, decode_from
encode()
Alias for: serialize
encode_to(io) click to toggle source

Encode to a given IO stream

@param io [:read]

# File lib/steamd/generator/ruby/steam_serializable.rb, line 24
def encode_to(io)
  io.write(serialize)
end
flag(vars, var) click to toggle source

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

# 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
serialize() click to toggle source

Serialize the constants and variables into a stream

@return [String] the byte representation

# 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
Also aliased as: encode
vars() click to toggle source

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

@return [Array<SerializedVariable>] the constants

# 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