nbt_utils 0.0.1 → 0.0.2
Sign up to get free protection for your applications and to get access to all the features.
- data/.gitignore +2 -0
- data/Gemfile.lock +1 -2
- data/README.md +5 -3
- data/doc/test.mclevel +0 -0
- data/lib/nbt_utils/file.rb +5 -6
- data/lib/nbt_utils/tag.rb +4 -0
- data/lib/nbt_utils/tag/byte_array.rb +10 -3
- data/lib/nbt_utils/tag/compound.rb +24 -5
- data/lib/nbt_utils/tag/list.rb +16 -6
- data/lib/nbt_utils/tag_name.rb +4 -0
- data/lib/nbt_utils/version.rb +1 -1
- data/script.rb +22 -14
- metadata +5 -22
data/.gitignore
CHANGED
data/Gemfile.lock
CHANGED
data/README.md
CHANGED
@@ -1,9 +1,9 @@
|
|
1
1
|
nbt_utils
|
2
2
|
=========
|
3
3
|
|
4
|
-
Some classes for handling Minecraft .nbt files.
|
4
|
+
Some classes for handling [Minecraft](http://minecraft.net) .nbt files.
|
5
5
|
|
6
|
-
See http://www.minecraft.net/docs/NBT.txt for
|
6
|
+
See [the spec](http://www.minecraft.net/docs/NBT.txt) for more info, also mirrored in the doc directory in a file named `NBT.txt`.
|
7
7
|
|
8
8
|
Installation
|
9
9
|
============
|
@@ -12,6 +12,8 @@ Requires ruby 1.9 minimum. Tested with 1.9.2.
|
|
12
12
|
|
13
13
|
gem install nbt_utils
|
14
14
|
|
15
|
+
No sudo. You *are* using [rvm](http://rvm.beginrescueend.com/), right?
|
16
|
+
|
15
17
|
Use
|
16
18
|
===
|
17
19
|
|
@@ -25,4 +27,4 @@ Copyright
|
|
25
27
|
|
26
28
|
Copyright (c) 2010 Michael Dungan, mpd@jesters-court.net, released under the MIT license.
|
27
29
|
|
28
|
-
The files `NBT.txt`, `test.nbt`, and `bigtest.nbt` in the
|
30
|
+
The files `NBT.txt`, `test.nbt`, and `bigtest.nbt` in the doc directory are mirrored from the Minecraft website and not covered under the above license.
|
data/doc/test.mclevel
ADDED
Binary file
|
data/lib/nbt_utils/file.rb
CHANGED
@@ -6,14 +6,13 @@ module NBTUtils
|
|
6
6
|
|
7
7
|
def read(path = @path)
|
8
8
|
Zlib::GzipReader.open(path) do |f|
|
9
|
-
|
10
|
-
last_byte = f.read(1).bytes.first
|
11
|
-
klass = NBTUtils::Tag.tag_type_to_class(last_byte)
|
12
|
-
|
13
|
-
@tag = klass.new(f, true)
|
9
|
+
@content = StringIO.new(f.read)
|
14
10
|
end
|
15
11
|
|
16
|
-
@
|
12
|
+
last_byte = @content.read(1).bytes.first
|
13
|
+
klass = NBTUtils::Tag.tag_type_to_class(last_byte)
|
14
|
+
|
15
|
+
@tag = klass.new(@content, true)
|
17
16
|
end
|
18
17
|
|
19
18
|
def write(path = @path, tag = @tag)
|
data/lib/nbt_utils/tag.rb
CHANGED
@@ -9,8 +9,9 @@ module NBTUtils
|
|
9
9
|
read_name(io) if named
|
10
10
|
|
11
11
|
len = ::BinData::Int32be.new.read(io).value
|
12
|
-
#
|
13
|
-
|
12
|
+
# use single string for the payload because an array means each byte is a
|
13
|
+
# separate object which is incredibly SLOW
|
14
|
+
@payload = ::BinData::String.new(:read_length => len).read(io)
|
14
15
|
end
|
15
16
|
|
16
17
|
def to_s(indent = 0)
|
@@ -21,9 +22,15 @@ module NBTUtils
|
|
21
22
|
result = named ? binary_type_id + name_to_nbt_string : ''
|
22
23
|
len = ::BinData::Int32be.new
|
23
24
|
len.value = @payload.length
|
24
|
-
result
|
25
|
+
result << len.to_binary_s
|
25
26
|
result + @payload.to_binary_s
|
26
27
|
end
|
28
|
+
|
29
|
+
def set_value(new_value, index)
|
30
|
+
b = ::BinData::Uint8.new
|
31
|
+
b.value = new_value
|
32
|
+
@payload[index] = to_binary_s
|
33
|
+
end
|
27
34
|
end
|
28
35
|
end
|
29
36
|
end
|
@@ -18,11 +18,11 @@ module NBTUtils
|
|
18
18
|
|
19
19
|
def to_s(indent = 0)
|
20
20
|
ret = (' ' * indent) + "TAG_Compound#{@name ? "(\"#{@name}\")" : ''}: #{@payload.length} entries\n"
|
21
|
-
ret
|
21
|
+
ret << (' ' * indent) + "{\n"
|
22
22
|
@payload.each do |load|
|
23
|
-
ret
|
23
|
+
ret << "#{load.to_s(indent + 2)}\n"
|
24
24
|
end
|
25
|
-
ret
|
25
|
+
ret << (' ' * indent) + "}"
|
26
26
|
|
27
27
|
ret
|
28
28
|
end
|
@@ -38,11 +38,15 @@ module NBTUtils
|
|
38
38
|
end
|
39
39
|
|
40
40
|
def find_tag(name)
|
41
|
-
|
41
|
+
if name.kind_of?(Regexp)
|
42
|
+
@payload.detect { |tag| tag.name.to_s =~ /#{name}/ }
|
43
|
+
else
|
44
|
+
@payload.detect { |tag| tag.name.to_s == name }
|
45
|
+
end
|
42
46
|
end
|
43
47
|
|
44
48
|
def find_tags(name)
|
45
|
-
@payload.select { |tag| tag.name =~ /#{name}/ }
|
49
|
+
@payload.select { |tag| tag.name.to_s =~ /#{name}/ }
|
46
50
|
end
|
47
51
|
|
48
52
|
def add_tag(tag)
|
@@ -51,6 +55,21 @@ module NBTUtils
|
|
51
55
|
@tag_names << tag.name
|
52
56
|
@payload << tag
|
53
57
|
end
|
58
|
+
|
59
|
+
# update one of my tags indirectly
|
60
|
+
def update_tag(name, new_value, index = nil)
|
61
|
+
tag = find_tag(name)
|
62
|
+
tag.set_value(new_value, index)
|
63
|
+
end
|
64
|
+
|
65
|
+
# update one of my tags directly. sort of wonky but here to conform to the api.
|
66
|
+
def set_value(new_value, index)
|
67
|
+
update_tag(index, new_value)
|
68
|
+
end
|
69
|
+
|
70
|
+
def remove_tag(name)
|
71
|
+
@payload.delete find_tag(name)
|
72
|
+
end
|
54
73
|
end
|
55
74
|
end
|
56
75
|
end
|
data/lib/nbt_utils/tag/list.rb
CHANGED
@@ -19,26 +19,36 @@ module NBTUtils
|
|
19
19
|
|
20
20
|
def to_s(indent = 0)
|
21
21
|
ret = (' ' * indent) + "TAG_List#{@name ? "(\"#{@name}\")" : ''}: #{@payload.length} entries of type TAG_#{@tag_type.to_s.split('::').last}\n"
|
22
|
-
ret
|
22
|
+
ret << (' ' * indent) + "{\n"
|
23
23
|
@payload.each do |load|
|
24
|
-
ret
|
24
|
+
ret << "#{load.to_s(indent + 2)}\n"
|
25
25
|
end
|
26
|
-
ret
|
26
|
+
ret << (' ' * indent) + "}"
|
27
27
|
ret
|
28
28
|
end
|
29
29
|
|
30
30
|
def to_nbt_string(named = true)
|
31
31
|
result = named ? binary_type_id + name_to_nbt_string : ''
|
32
|
-
type
|
32
|
+
type = ::BinData::Int8be.new
|
33
33
|
type.value = @tag_type.type_id
|
34
|
-
result
|
34
|
+
result << type.to_binary_s
|
35
35
|
len = ::BinData::Int32be.new
|
36
36
|
len.value = @payload.length
|
37
|
-
result
|
37
|
+
result << len.to_binary_s
|
38
38
|
@payload.inject(result) do |r, load|
|
39
39
|
r + load.to_nbt_string(false)
|
40
40
|
end
|
41
41
|
end
|
42
|
+
|
43
|
+
def set_value(new_value, index)
|
44
|
+
unless new_value.kind_of?(NBTUtils::Tag)
|
45
|
+
t = @tag_type.new
|
46
|
+
t.value = new_value
|
47
|
+
new_value = t
|
48
|
+
end
|
49
|
+
|
50
|
+
@payload[index] = new_value
|
51
|
+
end
|
42
52
|
end
|
43
53
|
end
|
44
54
|
end
|
data/lib/nbt_utils/tag_name.rb
CHANGED
data/lib/nbt_utils/version.rb
CHANGED
data/script.rb
CHANGED
@@ -8,24 +8,32 @@ require 'zlib'
|
|
8
8
|
|
9
9
|
@compound = nil
|
10
10
|
|
11
|
-
file = NBTUtils::File.new('doc/test.nbt')
|
12
|
-
|
13
|
-
|
14
|
-
puts @compound.to_s
|
11
|
+
#file = NBTUtils::File.new('doc/test.nbt')
|
12
|
+
#@compound = file.read
|
13
|
+
#
|
14
|
+
#puts @compound.to_s
|
15
|
+
#
|
16
|
+
#@compound.update_tag('name', 'asdf')
|
17
|
+
#puts @compound.to_s
|
18
|
+
#
|
19
|
+
#@compound.remove_tag('name')
|
20
|
+
#puts @compound.to_s
|
15
21
|
|
16
22
|
#puts @compound.to_nbt_string
|
17
|
-
#
|
18
|
-
|
19
|
-
|
23
|
+
#NBTUtils::File.new.write('biglolwut.nbt', @compound)
|
24
|
+
|
25
|
+
#@compound = NBTUtils::File.new.read('doc/bigtest.nbt')
|
26
|
+
#puts @compound.to_s
|
27
|
+
|
20
28
|
|
21
|
-
@compound = NBTUtils::File.new.read('doc/
|
29
|
+
@compound = NBTUtils::File.new.read('doc/test.mclevel')
|
22
30
|
puts @compound.to_s
|
23
31
|
|
24
32
|
#puts @compound.to_nbt_string
|
25
|
-
#
|
26
|
-
# gz.write @compound.to_nbt_string
|
27
|
-
#end
|
33
|
+
#NBTUtils::File.new.write('biglolwut.nbt', @compound)
|
28
34
|
|
29
|
-
|
30
|
-
#
|
31
|
-
#
|
35
|
+
#p @compound.find_tag(/Test/)
|
36
|
+
#p @compound.find_tag('Test')
|
37
|
+
#p @compound.find_tag('intTest')
|
38
|
+
#p @compound.find_tags(/(?:byte|int)Test/)
|
39
|
+
#p @compound.find_tags 'intasdf'
|
metadata
CHANGED
@@ -1,12 +1,8 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: nbt_utils
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
prerelease:
|
5
|
-
|
6
|
-
- 0
|
7
|
-
- 0
|
8
|
-
- 1
|
9
|
-
version: 0.0.1
|
4
|
+
prerelease:
|
5
|
+
version: 0.0.2
|
10
6
|
platform: ruby
|
11
7
|
authors:
|
12
8
|
- Michael Dungan
|
@@ -14,7 +10,7 @@ autorequire:
|
|
14
10
|
bindir: bin
|
15
11
|
cert_chain: []
|
16
12
|
|
17
|
-
date:
|
13
|
+
date: 2011-02-26 00:00:00 -08:00
|
18
14
|
default_executable:
|
19
15
|
dependencies:
|
20
16
|
- !ruby/object:Gem::Dependency
|
@@ -25,9 +21,6 @@ dependencies:
|
|
25
21
|
requirements:
|
26
22
|
- - ~>
|
27
23
|
- !ruby/object:Gem::Version
|
28
|
-
segments:
|
29
|
-
- 1
|
30
|
-
- 2
|
31
24
|
version: "1.2"
|
32
25
|
type: :runtime
|
33
26
|
version_requirements: *id001
|
@@ -39,10 +32,6 @@ dependencies:
|
|
39
32
|
requirements:
|
40
33
|
- - ">="
|
41
34
|
- !ruby/object:Gem::Version
|
42
|
-
segments:
|
43
|
-
- 1
|
44
|
-
- 0
|
45
|
-
- 0
|
46
35
|
version: 1.0.0
|
47
36
|
type: :development
|
48
37
|
version_requirements: *id002
|
@@ -63,6 +52,7 @@ files:
|
|
63
52
|
- Rakefile
|
64
53
|
- doc/NBT.txt
|
65
54
|
- doc/bigtest.nbt
|
55
|
+
- doc/test.mclevel
|
66
56
|
- doc/test.nbt
|
67
57
|
- lib/nbt_utils.rb
|
68
58
|
- lib/nbt_utils/file.rb
|
@@ -97,24 +87,17 @@ required_ruby_version: !ruby/object:Gem::Requirement
|
|
97
87
|
requirements:
|
98
88
|
- - ~>
|
99
89
|
- !ruby/object:Gem::Version
|
100
|
-
segments:
|
101
|
-
- 1
|
102
|
-
- 9
|
103
90
|
version: "1.9"
|
104
91
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
105
92
|
none: false
|
106
93
|
requirements:
|
107
94
|
- - ">="
|
108
95
|
- !ruby/object:Gem::Version
|
109
|
-
segments:
|
110
|
-
- 1
|
111
|
-
- 3
|
112
|
-
- 6
|
113
96
|
version: 1.3.6
|
114
97
|
requirements: []
|
115
98
|
|
116
99
|
rubyforge_project:
|
117
|
-
rubygems_version: 1.
|
100
|
+
rubygems_version: 1.5.0
|
118
101
|
signing_key:
|
119
102
|
specification_version: 3
|
120
103
|
summary: Set of classes to read and write Minecraft .nbt files
|