class Blender3d::DnaBlock::Reader

Public Class Methods

new(reader, dna_block = DnaBlock.new) click to toggle source
# File lib/blender-3d/dna_block.rb, line 14
def initialize(reader, dna_block = DnaBlock.new)
  @reader, @dna_block = reader, dna_block
end

Public Instance Methods

read() click to toggle source
# File lib/blender-3d/dna_block.rb, line 18
def read
  @dna_block.code = @reader.read(4)
  @names = read_names
  read_types
  read_structures
  @dna_block
end

Private Instance Methods

read_aligned() click to toggle source
# File lib/blender-3d/dna_block.rb, line 46
def read_aligned
  offset = 4 + (( 4 - (@reader.tell % 4) ) % 4)
  @reader.read(offset)
end
read_field() click to toggle source
# File lib/blender-3d/dna_block.rb, line 66
def read_field
  field_type = @dna_block.types[@reader.read_int16][0]
  field_name = @names[@reader.read_int16]
  factory = FieldParserFactory.create_for(field_type, field_name)
  factory.parse
end
read_names() click to toggle source
# File lib/blender-3d/dna_block.rb, line 28
def read_names
  @reader.read(4) # identifier 'NAME'
  count = @reader.read_int32
  count.times.map { @reader.read_string.chop.freeze }
end
read_structure() click to toggle source
# File lib/blender-3d/dna_block.rb, line 57
def read_structure
  type_index = @reader.read_int16
  num_fields = @reader.read_int16

  fields = num_fields.times.map { read_field }

  StructureDefinition.new(*@dna_block.types[type_index], fields)
end
read_structures() click to toggle source
# File lib/blender-3d/dna_block.rb, line 51
def read_structures
  read_aligned # identifier 'STRC' 4 byte aligned
  count = @reader.read_int32
  @dna_block.structures = count.times.map { read_structure }
end
read_types() click to toggle source
# File lib/blender-3d/dna_block.rb, line 34
def read_types
  read_aligned # identifier 'TYPE' 4 byte aligned

  count = @reader.read_int32
  types = count.times.map { @reader.read_string.chop.freeze }

  read_aligned # identifier 'TLEN' 4 byte aligned
  lengths = count.times.map { @reader.read_int16 }

  @dna_block.types = types.zip(lengths)
end