Skip to content

Commit

Permalink
add tag name class to encapsulate the reading and writing of the bina…
Browse files Browse the repository at this point in the history
…ry portions.
  • Loading branch information
mpd committed Sep 23, 2010
1 parent 8caf25d commit 67328de
Show file tree
Hide file tree
Showing 6 changed files with 12 additions and 9 deletions.
1 change: 1 addition & 0 deletions lib/nbt.rb
@@ -1,5 +1,6 @@
require File.expand_path('nbt/tag/exceptions', File.dirname(__FILE__)) require File.expand_path('nbt/tag/exceptions', File.dirname(__FILE__))


require File.expand_path('nbt/tag_name', File.dirname(__FILE__))
require File.expand_path('nbt/tag', File.dirname(__FILE__)) require File.expand_path('nbt/tag', File.dirname(__FILE__))
require File.expand_path('nbt/tag/end', File.dirname(__FILE__)) require File.expand_path('nbt/tag/end', File.dirname(__FILE__))
require File.expand_path('nbt/tag/byte', File.dirname(__FILE__)) require File.expand_path('nbt/tag/byte', File.dirname(__FILE__))
Expand Down
6 changes: 1 addition & 5 deletions lib/nbt/tag.rb
Expand Up @@ -8,11 +8,7 @@ def type
end end


def read_name(io) def read_name(io)
# If the length is a real TAG_Short, it's actually supposed to be @name = NBT::TagName.new.read(io).data
# signed, but this is a length measurement and it better not be negative.
l = io.read(2).unpack('n').first

@name = io.read(l)
end end


def tag_type_to_class(tag_type) def tag_type_to_class(tag_type)
Expand Down
3 changes: 1 addition & 2 deletions lib/nbt/tag/compound.rb
Expand Up @@ -10,8 +10,7 @@ def initialize(io, named = true)


until (last_byte = io.read(1).bytes.first) == NBT::Tag::End.type_id until (last_byte = io.read(1).bytes.first) == NBT::Tag::End.type_id
klass = tag_type_to_class(last_byte) klass = tag_type_to_class(last_byte)
i = klass.new(io, true) add_tag klass.new(io, true)
add_tag(i)
end end
end end


Expand Down
2 changes: 1 addition & 1 deletion lib/nbt/tag/end.rb
Expand Up @@ -15,7 +15,7 @@ def to_s(indent = 0)
'' ''
end end


def to_nbt_string(named = true) def to_nbt_string(named = false)


end end
end end
Expand Down
1 change: 0 additions & 1 deletion lib/nbt/tag/list.rb
Expand Up @@ -20,7 +20,6 @@ def self.type_id
end end


def to_s(indent = 0) def to_s(indent = 0)

ret = (' ' * indent) + "TAG_List#{@name ? "(\"#{@name}\")" : ''}: #{@payload.length} entries of type TAG_#{@tag_type.to_s.split('::').last}\n" ret = (' ' * indent) + "TAG_List#{@name ? "(\"#{@name}\")" : ''}: #{@payload.length} entries of type TAG_#{@tag_type.to_s.split('::').last}\n"
ret += (' ' * indent) + "{\n" ret += (' ' * indent) + "{\n"
@payload.each do |load| @payload.each do |load|
Expand Down
8 changes: 8 additions & 0 deletions lib/nbt/tag_name.rb
@@ -0,0 +1,8 @@
module NBT
class TagName < BinData::Record
# Spec says this is a TAG_Short, which is signed. Staying strict
# though this may really be unsigned in practice.
int16be :len, :value => lambda { data.length }
string :data, :read_length => :len
end
end

0 comments on commit 67328de

Please sign in to comment.