Class: NBTUtils::Tag::ByteArray

Inherits:
Object
  • Object
show all
Includes:
NBTUtils::Tag
Defined in:
lib/nbt_utils/tag/byte_array.rb

Instance Attribute Summary

Attributes included from NBTUtils::Tag

#name, #payload

Instance Method Summary collapse

Methods included from NBTUtils::Tag

add_tag_type, #binary_type_id, included, #name_to_nbt_string, #read_name, #tag_type_to_class, tag_type_to_class, #type_id

Constructor Details

#initialize(io, named: true) ⇒ ByteArray

Returns a new instance of ByteArray.



10
11
12
13
14
15
16
17
# File 'lib/nbt_utils/tag/byte_array.rb', line 10

def initialize(io, named: true)
  read_name(io) if named

  len = ::BinData::Int32be.new.read(io).value
  # use single string for the payload because an array means each byte is a
  # separate object which is incredibly SLOW
  @payload = ::BinData::String.new(read_length: len).read(io)
end

Instance Method Details

#set_value(new_value, index) ⇒ Object



31
32
33
34
35
# File 'lib/nbt_utils/tag/byte_array.rb', line 31

def set_value(new_value, index)
  b = ::BinData::Uint8.new
  b.value = new_value
  payload[index] = b.to_binary_s
end

#to_nbt_string(named: true) ⇒ Object



23
24
25
26
27
28
29
# File 'lib/nbt_utils/tag/byte_array.rb', line 23

def to_nbt_string(named: true)
  result = named ? binary_type_id + name_to_nbt_string : ''
  len = ::BinData::Int32be.new
  len.value = payload.length
  result << len.to_binary_s
  result + payload.to_binary_s
end

#to_s(indent = 0) ⇒ Object



19
20
21
# File 'lib/nbt_utils/tag/byte_array.rb', line 19

def to_s(indent = 0)
  (' ' * indent) + "TAG_Byte_Array#{name ? "(\"#{name}\")" : ''}: [#{payload.length} bytes]"
end