module SteamSerializable
Included in each Generated Ruby class. Provides access to methods on the class as well as serialization options.
Attributes
Variables in this class
Variables in this class
Public Class Methods
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
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
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
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
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 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
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