Skip to content

Commit

Permalink
work on writing nbt files. we can now read and rewrite the simple tes…
Browse files Browse the repository at this point in the history
…t nbt file perfectly
  • Loading branch information
mpd committed Sep 23, 2010
1 parent 67328de commit 88ab8a8
Show file tree
Hide file tree
Showing 7 changed files with 39 additions and 10 deletions.
15 changes: 13 additions & 2 deletions lib/nbt/tag.rb
Expand Up @@ -3,14 +3,25 @@ module Tag
attr_reader :name
attr_reader :payload

def type
self.class.type
def type_id
self.class.type_id
end

def binary_type_id
# I hope i'm doing this wrong.
::BinData::Int8be.new.read(type_id.chr).to_binary_s
end

def read_name(io)
@name = NBT::TagName.new.read(io).data
end

def name_to_nbt_string
nm = NBT::TagName.new
nm.data = @name
nm.to_binary_s
end

def tag_type_to_class(tag_type)
NBT::Tag.tag_type_to_class(tag_type)
end
Expand Down
4 changes: 3 additions & 1 deletion lib/nbt/tag/byte.rb
Expand Up @@ -18,7 +18,9 @@ def to_s(indent = 0)
end

def to_nbt_string(named = true)

result = binary_type_id
result += name_to_nbt_string if named
result + ::BinData::Int8be.new.read(@payload).to_binary_s
end
end
end
Expand Down
7 changes: 7 additions & 0 deletions lib/nbt/tag/compound.rb
Expand Up @@ -30,7 +30,14 @@ def to_s(indent = 0)
end

def to_nbt_string(named = true)
result = binary_type_id
result += name_to_nbt_string if named

result = @payload.inject(result) do |r, load|
r + load.to_nbt_string(true)
end

result + NBT::Tag::End.new(nil).to_nbt_string
end

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

def to_nbt_string(named = false)

binary_type_id
end
end
end
Expand Down
11 changes: 7 additions & 4 deletions lib/nbt/tag/string.rb
Expand Up @@ -6,9 +6,8 @@ class String
def initialize(io, named = true)
read_name(io) if named

l = ::BinData::Int16be.new.read(io).value.to_i

@payload = io.read(l)
# Tag names are actually TAG_Strings, so yay.
@payload = NBT::TagName.new.read(io).data
end

def self.type_id
Expand All @@ -20,7 +19,11 @@ def to_s(indent = 0)
end

def to_nbt_string(named = true)

result = binary_type_id
result += name_to_nbt_string if named
tag = NBT::TagName.new
tag.data = @payload
result + tag.to_binary_s
end
end
end
Expand Down
2 changes: 1 addition & 1 deletion lib/nbt/tag_name.rb
Expand Up @@ -2,7 +2,7 @@ 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 }
int16be :len, :value => Proc.new { data.length }
string :data, :read_length => :len
end
end
8 changes: 7 additions & 1 deletion script.rb
@@ -1,3 +1,5 @@
# encoding: UTF-8

require 'bundler'
Bundler.require :default

Expand All @@ -18,7 +20,11 @@
end

puts @compound.to_s
puts

puts @compound.to_nbt_string
#Zlib::GzipWriter.open('lolwut.nbt') do |gz|
# gz.write @compound.to_nbt_string
#end

Zlib::GzipReader.open('doc/bigtest.nbt') do |f|
# ostensibly this will always be a single TAG_Compound, per the spec
Expand Down

0 comments on commit 88ab8a8

Please sign in to comment.