craftbook-nbt 1.0.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 9cad7e3254d4754df3cf22a84b955032ddfb3b6fab4d068f63e43cfcd1ea5ba5
4
+ data.tar.gz: 9d2cd43e217df4de8d71930b1801812c54fe3751494e6d8c36e96860a02ccf3a
5
+ SHA512:
6
+ metadata.gz: 42f0580120ce0a95c12e7d6017aed4854635b21cf7016c51bf527850c4e51d8853dbb5b16de1468629a3ccde4168019aba59c7164600d37aa2c6d757c53980dd
7
+ data.tar.gz: f9dd45cb4b60e3ed3f4f67b40c8286a9e5231967c31a0bec6f35405465d4980ff6aae2fd47e2d835877410f7ba940dfd66d72d4ddd2da9bf33f155cd98611365
data/.gitignore ADDED
@@ -0,0 +1,9 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /_yardoc/
4
+ /coverage/
5
+ /doc/
6
+ /pkg/
7
+ /spec/reports/
8
+ /tmp/
9
+ /.idea/
data/.yardopts ADDED
@@ -0,0 +1,5 @@
1
+ --readme README.md
2
+ --title 'CraftBook NBT'
3
+ --charset utf-8
4
+ --markup markdown
5
+ lib/**/*.rb
data/CHANGELOG.md ADDED
@@ -0,0 +1,5 @@
1
+ ## [Unreleased]
2
+
3
+ ## [0.1.0] - 2021-08-27
4
+
5
+ - Initial release
@@ -0,0 +1,84 @@
1
+ # Contributor Covenant Code of Conduct
2
+
3
+ ## Our Pledge
4
+
5
+ We as members, contributors, and leaders pledge to make participation in our community a harassment-free experience for everyone, regardless of age, body size, visible or invisible disability, ethnicity, sex characteristics, gender identity and expression, level of experience, education, socio-economic status, nationality, personal appearance, race, religion, or sexual identity and orientation.
6
+
7
+ We pledge to act and interact in ways that contribute to an open, welcoming, diverse, inclusive, and healthy community.
8
+
9
+ ## Our Standards
10
+
11
+ Examples of behavior that contributes to a positive environment for our community include:
12
+
13
+ * Demonstrating empathy and kindness toward other people
14
+ * Being respectful of differing opinions, viewpoints, and experiences
15
+ * Giving and gracefully accepting constructive feedback
16
+ * Accepting responsibility and apologizing to those affected by our mistakes, and learning from the experience
17
+ * Focusing on what is best not just for us as individuals, but for the overall community
18
+
19
+ Examples of unacceptable behavior include:
20
+
21
+ * The use of sexualized language or imagery, and sexual attention or
22
+ advances of any kind
23
+ * Trolling, insulting or derogatory comments, and personal or political attacks
24
+ * Public or private harassment
25
+ * Publishing others' private information, such as a physical or email
26
+ address, without their explicit permission
27
+ * Other conduct which could reasonably be considered inappropriate in a
28
+ professional setting
29
+
30
+ ## Enforcement Responsibilities
31
+
32
+ Community leaders are responsible for clarifying and enforcing our standards of acceptable behavior and will take appropriate and fair corrective action in response to any behavior that they deem inappropriate, threatening, offensive, or harmful.
33
+
34
+ Community leaders have the right and responsibility to remove, edit, or reject comments, commits, code, wiki edits, issues, and other contributions that are not aligned to this Code of Conduct, and will communicate reasons for moderation decisions when appropriate.
35
+
36
+ ## Scope
37
+
38
+ This Code of Conduct applies within all community spaces, and also applies when an individual is officially representing the community in public spaces. Examples of representing our community include using an official e-mail address, posting via an official social media account, or acting as an appointed representative at an online or offline event.
39
+
40
+ ## Enforcement
41
+
42
+ Instances of abusive, harassing, or otherwise unacceptable behavior may be reported to the community leaders responsible for enforcement at Zvezda2013!Svitlana. All complaints will be reviewed and investigated promptly and fairly.
43
+
44
+ All community leaders are obligated to respect the privacy and security of the reporter of any incident.
45
+
46
+ ## Enforcement Guidelines
47
+
48
+ Community leaders will follow these Community Impact Guidelines in determining the consequences for any action they deem in violation of this Code of Conduct:
49
+
50
+ ### 1. Correction
51
+
52
+ **Community Impact**: Use of inappropriate language or other behavior deemed unprofessional or unwelcome in the community.
53
+
54
+ **Consequence**: A private, written warning from community leaders, providing clarity around the nature of the violation and an explanation of why the behavior was inappropriate. A public apology may be requested.
55
+
56
+ ### 2. Warning
57
+
58
+ **Community Impact**: A violation through a single incident or series of actions.
59
+
60
+ **Consequence**: A warning with consequences for continued behavior. No interaction with the people involved, including unsolicited interaction with those enforcing the Code of Conduct, for a specified period of time. This includes avoiding interactions in community spaces as well as external channels like social media. Violating these terms may lead to a temporary or permanent ban.
61
+
62
+ ### 3. Temporary Ban
63
+
64
+ **Community Impact**: A serious violation of community standards, including sustained inappropriate behavior.
65
+
66
+ **Consequence**: A temporary ban from any sort of interaction or public communication with the community for a specified period of time. No public or private interaction with the people involved, including unsolicited interaction with those enforcing the Code of Conduct, is allowed during this period. Violating these terms may lead to a permanent ban.
67
+
68
+ ### 4. Permanent Ban
69
+
70
+ **Community Impact**: Demonstrating a pattern of violation of community standards, including sustained inappropriate behavior, harassment of an individual, or aggression toward or disparagement of classes of individuals.
71
+
72
+ **Consequence**: A permanent ban from any sort of public interaction within the community.
73
+
74
+ ## Attribution
75
+
76
+ This Code of Conduct is adapted from the [Contributor Covenant][homepage], version 2.0,
77
+ available at https://www.contributor-covenant.org/version/2/0/code_of_conduct.html.
78
+
79
+ Community Impact Guidelines were inspired by [Mozilla's code of conduct enforcement ladder](https://github.com/mozilla/diversity).
80
+
81
+ [homepage]: https://www.contributor-covenant.org
82
+
83
+ For answers to common questions about this code of conduct, see the FAQ at
84
+ https://www.contributor-covenant.org/faq. Translations are available at https://www.contributor-covenant.org/translations.
data/Gemfile ADDED
@@ -0,0 +1,6 @@
1
+ # frozen_string_literal: true
2
+
3
+ source "https://rubygems.org"
4
+
5
+ # Specify your gem's dependencies in craftbook-nbt.gemspec
6
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2021 ForeverZer0
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,420 @@
1
+ # CraftBook::NBT
2
+
3
+ A feature-rich and complete Ruby implementation of the Named Binary Tag (NBT) format. While it is an integral part of
4
+ the broader CraftBook API, it is an independent module with no dependencies, and can be used for any purpose where
5
+ reading/writing/converting the NBT format is required.
6
+
7
+ # Features
8
+
9
+ * Intuitive and simple to use, with a user-friendly API surface
10
+ * Reads from any IO-like object
11
+ * `TagBuilder` class for easily building complete NBT documents from scratch (see example below)
12
+ * Conversion to and from JSON
13
+ * Conversion to and from SNBT (aka _stringified_ NBT), performed properly with a grammar and a lexical parser with `racc` (standard library)
14
+ * Custom formatted output in a tree structure for simple viewing NBT, or debugging for correctness
15
+ * Automatic compression detection
16
+ * Well-structured and logical inheritance tree
17
+
18
+ ## Installation
19
+
20
+ Add this line to your application's Gemfile:
21
+
22
+ ```ruby
23
+ gem 'craftbook-nbt'
24
+ ```
25
+
26
+ And then execute:
27
+
28
+ $ bundle install
29
+
30
+ Or install it yourself as:
31
+
32
+ $ gem install craftbook-nbt
33
+
34
+ ## Usage
35
+
36
+ ### Require
37
+
38
+ After installing the gem, everything can be made available by requiring one file:
39
+
40
+ ```ruby
41
+ require 'craftbook/nbt'
42
+ ```
43
+
44
+ ### Reading
45
+
46
+ For the purpose of example, we will use the de-facto [bigtest.nbt](https://raw.github.com/Dav1dde/nbd/master/test/bigtest.nbt)
47
+ for NBT as recommended by https://wiki.vg/NBT where the specification is outlined.
48
+
49
+ To load a file (GZip and ZLib compressed files will be detected and handled automatically.):
50
+ ```ruby
51
+ tag = CraftBook::NBT.read_file('/path/to/bigtest.nbt')
52
+ ```
53
+
54
+ The resulting object is a `CompoundTag` instance, which is the implicit top-level container of all files. This tag
55
+ (among others) implements the `Enumerable` mixin, and exhibits typical array-like behavior, including accessing by
56
+ index for inserting/removing/fetching child elements.
57
+
58
+ If you need to read from an existing IO-like object other than a file, use the following:
59
+ ```ruby
60
+ tag = CraftBook::NBT.read(io)
61
+ ```
62
+
63
+ Unlike a file, compression cannot be detected from a stream, as not all streams support seeking (i.e. network).
64
+ Furthermore, compression algorithms typically cannot start from an unknown position in a stream, and it is unsafe to
65
+ assume the position for each use. For this reason, it is up to users to apply any needed decompression wrappers over the
66
+ IO object before passing to this method. Any object that responds to `#read` and returns a String is a viable
67
+ parameter that can be used.
68
+
69
+ ### Writing
70
+
71
+ Writing is just as simple:
72
+ ```ruby
73
+ CraftBook::NBT.write_file('/path/to/file.nbt', compression: :gzip, level: :optimal)
74
+ ```
75
+
76
+ ...or if writing directly to an `IO` object or one that implements `#write`:
77
+ ```ruby
78
+ compound_tag = CompoundTag.new("My First NBT Tag!")
79
+ compound_tag.push(StringTag.new("Hello", "World"))
80
+
81
+ bytes_written = CraftBook::NBT.write(STDOUT, compound_tag, compression: :zlib, level: :fastest)
82
+ ```
83
+
84
+ Compression is optional, but defaults to GZip with "default" level when not specified.
85
+
86
+ ### Creating Tags
87
+
88
+ Tag creation can be done manually by creating individual tags and building the document manually, or you can use the
89
+ included `TagBuilder` class to ease in their creation.
90
+
91
+ For a complete example, we will re-create the "bigtest.nbt" mentioned above from scratch, which uses all tag types:
92
+
93
+ ```ruby
94
+ include CraftBook::NBT
95
+
96
+ tb = TagBuilder.new("Level")
97
+ tb.compound('nested compound test') do
98
+
99
+ tb.compound('egg') do
100
+ tb.string('name', 'Eggburt')
101
+ tb.float('value', 0.5)
102
+ end
103
+
104
+ tb.compound("ham") do
105
+ tb.string('name', 'Hampus')
106
+ tb.float('value', 0.75)
107
+ end
108
+
109
+ tb.int('intTest', 2147483647)
110
+ tb.byte('byteTest', 127)
111
+ tb.string('stringTest', "HELLO WORLD THIS IS A TEST STRING \u{c5}\u{c4}\u{d6}!")
112
+
113
+ tb.list('listTest (long)', Tag::TYPE_LONG) do
114
+ tb.long(nil, 11)
115
+ tb.long(nil, 12)
116
+ tb.long(nil, 13)
117
+ tb.long(nil, 14)
118
+ tb.long(nil, 15)
119
+ end
120
+
121
+ tb.double('doubleTest', 0.49312871321823148)
122
+ tb.float('floatTest', 0.49823147058486938)
123
+ tb.long('longTest', 9223372036854775807)
124
+
125
+ tb.list('listTest (compound', Tag::TYPE_COMPOUND) do
126
+ tb.compound(nil) do
127
+ tb.long('created-on', 1264099775885)
128
+ tb.string('name', 'Compound tag #0')
129
+ end
130
+ tb.compound(nil) do
131
+ tb.long('created-on', 1264099775885)
132
+ tb.string('name', 'Compound tag #1')
133
+ end
134
+ end
135
+
136
+ name = 'byteArrayTest (the first 1000 values of (n*n*255+n*7)%100, starting with n=0 (0, 62, 34, 16, 8, ...))'
137
+ array = (0...1000).map { |n| (n * n * 255 + n * 7) % 100 }
138
+ tb.byte_array(name, *array)
139
+ tb.short('shortTest', 32767)
140
+ end
141
+
142
+ tag = tb.root
143
+ ```
144
+
145
+ We can then compare the output:
146
+
147
+ ```ruby
148
+ tag.pretty_print(STDOUT)
149
+ ```
150
+
151
+ <details>
152
+ <summary>Click to Expand Output</summary>
153
+
154
+ ```
155
+ TAG_Compound("Level"): 1 child
156
+ {
157
+ TAG_Compound("nested compound test"): 12 children
158
+ {
159
+ TAG_Compound("egg"): 2 children
160
+ {
161
+ TAG_String("name"): "Eggburt"
162
+ TAG_Float("value"): 0.5
163
+ }
164
+ TAG_Compound("ham"): 2 children
165
+ {
166
+ TAG_String("name"): "Hampus"
167
+ TAG_Float("value"): 0.75
168
+ }
169
+ TAG_Int("intTest"): 2147483647
170
+ TAG_Byte("byteTest"): 127
171
+ TAG_String("stringTest"): "HELLO WORLD THIS IS A TEST STRING ÅÄÖ!"
172
+ TAG_List("listTest (long)"): 5 children
173
+ {
174
+ TAG_Long(None): 11
175
+ TAG_Long(None): 12
176
+ TAG_Long(None): 13
177
+ TAG_Long(None): 14
178
+ TAG_Long(None): 15
179
+ }
180
+ TAG_Double("doubleTest"): 0.4931287132182315
181
+ TAG_Float("floatTest"): 0.4982314705848694
182
+ TAG_Long("longTest"): 9223372036854775807
183
+ TAG_List("listTest (compound"): 2 children
184
+ {
185
+ TAG_Compound(None): 2 children
186
+ {
187
+ TAG_Long("created-on"): 1264099775885
188
+ TAG_String("name"): "Compound tag #0"
189
+ }
190
+ TAG_Compound(None): 2 children
191
+ {
192
+ TAG_Long("created-on"): 1264099775885
193
+ TAG_String("name"): "Compound tag #1"
194
+ }
195
+ }
196
+ TAG_Byte_Array("byteArrayTest (the first 1000 values of (n*n*255+n*7)%100, starting with n=0 (0, 62, 34, 16, 8, ...))"): 1 item
197
+ TAG_Short("shortTest"): 32767
198
+ }
199
+ }
200
+ ```
201
+
202
+ </details>
203
+
204
+ Or if you prefer JSON...
205
+
206
+ ```ruby
207
+ pretty = true
208
+ tag.to_json(pretty, indent: ' ')
209
+ ```
210
+
211
+ <details>
212
+ <summary>Click to expand JSON output</summary>
213
+
214
+ ```json
215
+
216
+ {
217
+ "name": "Level",
218
+ "type": 10,
219
+ "values": [
220
+ {
221
+ "name": "nested compound test",
222
+ "type": 10,
223
+ "values": [
224
+ {
225
+ "name": "egg",
226
+ "type": 10,
227
+ "values": [
228
+ {
229
+ "name": "name",
230
+ "type": 8,
231
+ "value": "Eggburt"
232
+ },
233
+ {
234
+ "name": "value",
235
+ "type": 5,
236
+ "value": 0.5
237
+ }
238
+ ]
239
+ },
240
+ {
241
+ "name": "ham",
242
+ "type": 10,
243
+ "values": [
244
+ {
245
+ "name": "name",
246
+ "type": 8,
247
+ "value": "Hampus"
248
+ },
249
+ {
250
+ "name": "value",
251
+ "type": 5,
252
+ "value": 0.75
253
+ }
254
+ ]
255
+ },
256
+ {
257
+ "name": "intTest",
258
+ "type": 3,
259
+ "value": 2147483647
260
+ },
261
+ {
262
+ "name": "byteTest",
263
+ "type": 1,
264
+ "value": 127
265
+ },
266
+ {
267
+ "name": "stringTest",
268
+ "type": 8,
269
+ "value": "HELLO WORLD THIS IS A TEST STRING ÅÄÖ!"
270
+ },
271
+ {
272
+ "name": "listTest (long)",
273
+ "type": 9,
274
+ "child_type": 4,
275
+ "values": [
276
+ {
277
+ "value": 11
278
+ },
279
+ {
280
+ "value": 12
281
+ },
282
+ {
283
+ "value": 13
284
+ },
285
+ {
286
+ "value": 14
287
+ },
288
+ {
289
+ "value": 15
290
+ }
291
+ ]
292
+ },
293
+ {
294
+ "name": "doubleTest",
295
+ "type": 6,
296
+ "value": 0.4931287132182315
297
+ },
298
+ {
299
+ "name": "floatTest",
300
+ "type": 5,
301
+ "value": 0.4982314705848694
302
+ },
303
+ {
304
+ "name": "longTest",
305
+ "type": 4,
306
+ "value": 9223372036854775807
307
+ },
308
+ {
309
+ "name": "listTest (compound",
310
+ "type": 9,
311
+ "child_type": 10,
312
+ "values": [
313
+ {
314
+ "values": [
315
+ {
316
+ "name": "created-on",
317
+ "type": 4,
318
+ "value": 1264099775885
319
+ },
320
+ {
321
+ "name": "name",
322
+ "type": 8,
323
+ "value": "Compound tag #0"
324
+ }
325
+ ]
326
+ },
327
+ {
328
+ "values": [
329
+ {
330
+ "name": "created-on",
331
+ "type": 4,
332
+ "value": 1264099775885
333
+ },
334
+ {
335
+ "name": "name",
336
+ "type": 8,
337
+ "value": "Compound tag #1"
338
+ }
339
+ ]
340
+ }
341
+ ]
342
+ },
343
+ {
344
+ "name": "byteArrayTest (the first 1000 values of (n*n*255+n*7)%100, starting with n=0 (0, 62, 34, 16, 8, ...))",
345
+ "type": 7,
346
+ "values": [
347
+ [
348
+ 0,
349
+ 62,
350
+ 34,
351
+ "Removed for the sake of brevity..."
352
+ ]
353
+ ]
354
+ },
355
+ {
356
+ "name": "shortTest",
357
+ "type": 2,
358
+ "value": 32767
359
+ }
360
+ ]
361
+ }
362
+ ]
363
+ }
364
+ ```
365
+
366
+ </details>
367
+
368
+ ...or perhaps you need to stringify it into SNBT format...
369
+
370
+ ```ruby
371
+ tag.stringify
372
+ ```
373
+
374
+ ```
375
+ {Level:{{nested compound test:{{egg:{name:"Eggburt",value:0.5F},{ham:{name:"Hampus",value:0.75F},intTest:2147483647,byteTest:127B,stringTest:"HELLO WORLD THIS IS A TEST STRING ÅÄÖ!",listTest (long):[11L,12L,13L,14L,15L],doubleTest:0.4931287132182315,floatTest:0.4982314705848694F,longTest:9223372036854775807L,listTest (compound:[{{created-on:1264099775885L,name:"Compound tag #0"},{{created-on:1264099775885L,name:"Compound tag #1"}],byteArrayTest (the first 1000 values of (n*n*255+n*7)%100, starting with n=0 (0, 62, 34, 16, 8, ...)):[B;0,62,...,74,6,48],shortTest:32767S}}
376
+ ```
377
+
378
+ ### Parsing Stringified NBT (SNBT)
379
+
380
+ For parsing SNBT, this library uses a proper lexer with a grammar file approach, taking advantage of the Racc gem, which
381
+ is part of Ruby's standard library. This allows scanning over input and tokenizing it into logical pieces to parse,
382
+ opposed to using monstrous and difficult-to-debug regular expressions.
383
+
384
+ There is only a single method call involved with parsing an arbitrary string of SNBT code: `NBT.parse_snbt`.
385
+
386
+ ```ruby
387
+ snbt_string = '{name1:123,name2:"sometext1",name3:{subname1:456,subname2:"sometext2"}}'
388
+ compound = NBT.parse_snbt(snbt_string)
389
+ compound.pretty_print
390
+ ```
391
+
392
+ **Output:**
393
+ ```
394
+ TAG_Compound(None): 3 children
395
+ {
396
+ TAG_Int("name1"): 123
397
+ TAG_String("name2"): "sometext1"
398
+ TAG_Compound("name3"): 2 children
399
+ {
400
+ TAG_Int("subname1"): 456
401
+ TAG_String("subname2"): "sometext2"
402
+ }
403
+ }
404
+ ```
405
+ ## Documentation
406
+
407
+ Code is fully documented using [YARD](https://yardoc.org/), which is supported by modern linters for inline documentation
408
+ in your editor, and is always available [in full at RubyDoc.info](https://www.rubydoc.info/gems/craftbook-nbt).
409
+
410
+ ## Contributing
411
+
412
+ Bug reports and pull requests are welcome on GitHub at https://github.com/ForeverZer0/craftbook-nbt. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [code of conduct](https://github.com/ForeverZer0/craftbook-nbt/blob/master/CODE_OF_CONDUCT.md).
413
+
414
+ ## License
415
+
416
+ The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
417
+
418
+ ## Code of Conduct
419
+
420
+ Everyone interacting in the Craftbook::Nbt project's codebases, issue trackers, chat rooms and mailing lists is expected to follow the [code of conduct](https://github.com/ForeverZer0/craftbook-nbt/blob/master/CODE_OF_CONDUCT.md).