Skip to content
Browse files

add tag name class to encapsulate the reading and writing of the bina…

…ry portions.
  • Loading branch information...
1 parent 8caf25d commit 67328dea2809c390ac852659534cd27822ede9bf mpd committed Sep 22, 2010
Showing with 12 additions and 9 deletions.
  1. +1 −0 lib/nbt.rb
  2. +1 −5 lib/nbt/tag.rb
  3. +1 −2 lib/nbt/tag/compound.rb
  4. +1 −1 lib/nbt/tag/end.rb
  5. +0 −1 lib/nbt/tag/list.rb
  6. +8 −0 lib/nbt/tag_name.rb
View
1 lib/nbt.rb
@@ -1,5 +1,6 @@
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/end', File.dirname(__FILE__))
require File.expand_path('nbt/tag/byte', File.dirname(__FILE__))
View
6 lib/nbt/tag.rb
@@ -8,11 +8,7 @@ def type
end
def read_name(io)
- # If the length is a real TAG_Short, it's actually supposed to be
- # signed, but this is a length measurement and it better not be negative.
- l = io.read(2).unpack('n').first
-
- @name = io.read(l)
+ @name = NBT::TagName.new.read(io).data
end
def tag_type_to_class(tag_type)
View
3 lib/nbt/tag/compound.rb
@@ -10,8 +10,7 @@ def initialize(io, named = true)
until (last_byte = io.read(1).bytes.first) == NBT::Tag::End.type_id
klass = tag_type_to_class(last_byte)
- i = klass.new(io, true)
- add_tag(i)
+ add_tag klass.new(io, true)
end
end
View
2 lib/nbt/tag/end.rb
@@ -15,7 +15,7 @@ def to_s(indent = 0)
''
end
- def to_nbt_string(named = true)
+ def to_nbt_string(named = false)
end
end
View
1 lib/nbt/tag/list.rb
@@ -20,7 +20,6 @@ def self.type_id
end
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) + "{\n"
@payload.each do |load|
View
8 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.
Something went wrong with that request. Please try again.