Class: NBTFile::Private::Writer

Inherits:
Object
  • Object
show all
Defined in:
lib/nbtfile.rb

Instance Method Summary collapse

Constructor Details

#initialize(emitter) ⇒ Writer

Returns a new instance of Writer.



696
697
698
# File 'lib/nbtfile.rb', line 696

def initialize(emitter)
  @emitter = emitter
end

Instance Method Details

#type_to_token(type) ⇒ Object



700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
# File 'lib/nbtfile.rb', line 700

def type_to_token(type)
  case
  when type == Types::Byte
    token = Tokens::TAG_Byte
  when type == Types::Short
    token = Tokens::TAG_Short
  when type == Types::Int
    token = Tokens::TAG_Int
  when type == Types::Long
    token = Tokens::TAG_Long
  when type == Types::Float
    token = Tokens::TAG_Float
  when type == Types::Double
    token = Tokens::TAG_Double
  when type == Types::String
    token = Tokens::TAG_String
  when type == Types::ByteArray
    token = Tokens::TAG_Byte_Array
  when type == Types::List
    token = Tokens::TAG_List
  when type == Types::Compound
    token = Tokens::TAG_Compound
  else
    raise TypeError, "Unexpected list type #{type}"
  end
  return token
end

#write_pair(name, value) ⇒ Object



728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
# File 'lib/nbtfile.rb', line 728

def write_pair(name, value)
  case value
  when Types::Byte
    @emitter.emit_token(Tokens::TAG_Byte[name, value.value])
  when Types::Short
    @emitter.emit_token(Tokens::TAG_Short[name, value.value])
  when Types::Int
    @emitter.emit_token(Tokens::TAG_Int[name, value.value])
  when Types::Long
    @emitter.emit_token(Tokens::TAG_Long[name, value.value])
  when Types::Float
    @emitter.emit_token(Tokens::TAG_Float[name, value.value])
  when Types::Double
    @emitter.emit_token(Tokens::TAG_Double[name, value.value])
  when Types::String
    @emitter.emit_token(Tokens::TAG_String[name, value.value])
  when Types::ByteArray
    @emitter.emit_token(Tokens::TAG_Byte_Array[name, value.value])
  when Types::List
    token = type_to_token(value.type)
    @emitter.emit_token(Tokens::TAG_List[name, token])
    for item in value
      write_pair(nil, item)
    end
    @emitter.emit_token(Tokens::TAG_End[nil, nil])
  when Types::Compound
    @emitter.emit_token(Tokens::TAG_Compound[name, nil])
    for k, v in value
      write_pair(k, v)
    end
    @emitter.emit_token(Tokens::TAG_End[nil, nil])
  end
end