byteinterpreter 1.1.0 → 1.1.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +4 -0
- data/example/SPELL.BIN +0 -0
- data/example/example_reader.rb +49 -0
- data/example/example_writer.rb +52 -0
- data/example/spell_instructions.json +17 -0
- metadata +6 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 8abca2d5036f76828d07996dc5572e75a3f35a57a401e3f1068153555383024d
|
4
|
+
data.tar.gz: 584df787581701fcfebe5495cca4c860b87a19288bbc2564f2faf4cef9cfc628
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 53aec752d2ab274b483a9f712184c2344854f5e99becb5223ad8b29b80f650d95845cd1913fb9c7038b02df39326687a1eb943ea510814817bb12bf8dd07ab27
|
7
|
+
data.tar.gz: ff9391ff14f919734632c1a5a250fcec1dd691358e4be1db492916f1d49ef651881f8a5e3f29d46dca43b077ea41127208c6778f80caf5dc5466172c417185e0
|
data/README.md
CHANGED
@@ -1,3 +1,7 @@
|
|
1
|
+
[![Gem Version](https://badge.fury.io/rb/byteinterpreter.svg)](https://badge.fury.io/rb/byteinterpreter)
|
2
|
+
[![Maintainability](https://api.codeclimate.com/v1/badges/f071bb580f1d24bb78db/maintainability)](https://codeclimate.com/github/mkgremillion/ByteInterpreter/maintainability)
|
3
|
+
[![Inline docs](http://inch-ci.org/github/mkgremillion/byteinterpreter.svg?branch=master)](http://inch-ci.org/github/mkgremillion/byteinterpreter)
|
4
|
+
|
1
5
|
ByteInterpreter is a tool to interpret binary data in a fixed-length data
|
2
6
|
structure into another format, or to encode data from another format into that
|
3
7
|
same fixed-length data structure.
|
data/example/SPELL.BIN
ADDED
Binary file
|
@@ -0,0 +1,49 @@
|
|
1
|
+
require_relative "../lib/byteinterpreter.rb"
|
2
|
+
|
3
|
+
# Open the file. Mode "rb" means we're only opening it for reading, and we're
|
4
|
+
# opening it for binary reading.
|
5
|
+
bin_file = File.open("SPELL.BIN", "rb")
|
6
|
+
|
7
|
+
# We create our interpreter. SPELL.BIN is in little endian.
|
8
|
+
bi = ByteInterpreter.new(endian: :little, stream: bin_file)
|
9
|
+
|
10
|
+
# Let's read the first byte, which should be the element of the first spell.
|
11
|
+
# Remember, ByteInterpreter will read from whatever position the iostream is
|
12
|
+
# at, and will move the position by however many bytes are read.
|
13
|
+
puts "Reading the first byte: #{bi.interpret_bytes(size: 1)}"
|
14
|
+
|
15
|
+
# The next set of bytes should be the name of the spell, a string.
|
16
|
+
puts "Reading the spell name: #{bi.interpret_string(size: 20)}"
|
17
|
+
|
18
|
+
# Let's read the rest of the spell entry.
|
19
|
+
puts "Reading the spell power: #{bi.interpret_bytes(size: 2)}"
|
20
|
+
puts "Reading the spell description: #{bi.interpret_string(size: 50)}"
|
21
|
+
puts "Reading the spell speed: #{bi.interpret_bytes(size: 1, signed: true)}"
|
22
|
+
|
23
|
+
# Now that we've used the individual reading methods, let's dive into using
|
24
|
+
# instructions. We'll load the instructions from our JSON file.
|
25
|
+
bi.load_instructions(type: :json, filename: "./spell_instructions.json")
|
26
|
+
|
27
|
+
# Let's read one whole spell from our binary file. This will be the second
|
28
|
+
# spell entry.
|
29
|
+
puts "\nReading spell entry 2: "
|
30
|
+
puts "==========================================="
|
31
|
+
bi.interpret_from_instructions do |key, value|
|
32
|
+
puts "Field name: #{key}"
|
33
|
+
puts "Field value: #{value}"
|
34
|
+
puts "-------------------------------"
|
35
|
+
end
|
36
|
+
|
37
|
+
# And of course, if you want to read multiple entries, it's not too difficult.
|
38
|
+
2.times do |i|
|
39
|
+
puts "\nReading spell entry #{i + 3}: "
|
40
|
+
puts "==========================================="
|
41
|
+
bi.interpret_from_instructions do |key, value|
|
42
|
+
puts "Field name: #{key}"
|
43
|
+
puts "Field value: #{value}"
|
44
|
+
puts "-------------------------------"
|
45
|
+
end
|
46
|
+
end
|
47
|
+
|
48
|
+
# Not specific to ByteInterpreter, but don't forget to close your file! =)
|
49
|
+
bin_file.close
|
@@ -0,0 +1,52 @@
|
|
1
|
+
require_relative "../lib/byteinterpreter.rb"
|
2
|
+
|
3
|
+
# Open the file. Mode "wb" means we're only opening it for writing, and we're
|
4
|
+
# opening it for binary writing specifically.
|
5
|
+
bin_file = File.open("SPELL.BIN", "wb")
|
6
|
+
|
7
|
+
# We create our interpreter. SPELL.BIN is in little endian.
|
8
|
+
bi = ByteInterpreter.new(endian: :little, stream: bin_file)
|
9
|
+
|
10
|
+
# Let's write our first byte, which is the element of the first spell.
|
11
|
+
# Remember, ByteInterpreter will write to whatever position the iostream is at,
|
12
|
+
# and will move the position by however many bytes are written.
|
13
|
+
bi.encode_bytes(value: 1, size: 1, signed: false)
|
14
|
+
|
15
|
+
# The next field is the name of the spell, a string.
|
16
|
+
# Even though the string is shorter than the size given, ByteInterpreter will
|
17
|
+
# make an attempt to have the string fit. In this case, it'll pad the name with
|
18
|
+
# spaces until it reaches the appropriate size.
|
19
|
+
bi.encode_string(value: "Fireball", size: 20)
|
20
|
+
|
21
|
+
# Let's finish writing the spell.
|
22
|
+
bi.encode_bytes(value: 20, size: 2, signed: false)
|
23
|
+
bi.encode_string(value: "Tosses a fireball at your foes", size: 50)
|
24
|
+
bi.encode_bytes(value: -10, size: 1, signed: true)
|
25
|
+
|
26
|
+
# Now that we've used the individual writing methods, let's dive into using
|
27
|
+
# instructions. We'll load the instructions from our JSON file.
|
28
|
+
bi.load_instructions(type: :json, filename: "./spell_instructions.json")
|
29
|
+
|
30
|
+
# Here's a hash that represents one of our spells. When writing with
|
31
|
+
# instructions, it is *absolutely vital* that your hash contains every
|
32
|
+
# expected field name.
|
33
|
+
new_spell = {element: 2, power: 15, speed: 20, name: "Thunderclap", description: "A quick strike of lightning"}
|
34
|
+
|
35
|
+
# Now we write the hash to our file.
|
36
|
+
bi.encode_from_instructions(values: new_spell)
|
37
|
+
|
38
|
+
# And if we want to write multiple spells at once, it's not too difficult.
|
39
|
+
spells = [
|
40
|
+
{element: 3, power: 40, speed: -20, name: "Zero K", description: "The ultimate ice spell"},
|
41
|
+
{element: 1, power: 50, speed: 0, name: "Melt", description: "For when you need something gone, now."}
|
42
|
+
]
|
43
|
+
|
44
|
+
spells.each do |spell|
|
45
|
+
bi.encode_from_instructions(values: spell)
|
46
|
+
end
|
47
|
+
|
48
|
+
# Not specific to ByteInterpreter, but don't forget to close your file! =)
|
49
|
+
bin_file.close
|
50
|
+
|
51
|
+
# If you want to see the fruits of your labor, you can use example_reader.rb to
|
52
|
+
# read your SPELL.BIN file, or open it with a hex editor.
|
@@ -0,0 +1,17 @@
|
|
1
|
+
[
|
2
|
+
{
|
3
|
+
"key": "element", "type": "bin", "size": 1, "signed": false
|
4
|
+
},
|
5
|
+
{
|
6
|
+
"key": "name", "type": "str", "size": 20, "signed": false
|
7
|
+
},
|
8
|
+
{
|
9
|
+
"key": "power", "type": "bin", "size": 2, "signed": false
|
10
|
+
},
|
11
|
+
{
|
12
|
+
"key": "description", "type": "str", "size": 50, "signed": false
|
13
|
+
},
|
14
|
+
{
|
15
|
+
"key": "speed", "type": "bin", "size": 1, "signed": true
|
16
|
+
}
|
17
|
+
]
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: byteinterpreter
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 1.1.
|
4
|
+
version: 1.1.1
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Michael K Gremillion
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2018-09-
|
11
|
+
date: 2018-09-22 00:00:00.000000000 Z
|
12
12
|
dependencies: []
|
13
13
|
description: |
|
14
14
|
The ByteInterpreter is a tool to interpret bytes from and encode bytes to
|
@@ -21,6 +21,10 @@ extra_rdoc_files: []
|
|
21
21
|
files:
|
22
22
|
- LICENSE.md
|
23
23
|
- README.md
|
24
|
+
- example/SPELL.BIN
|
25
|
+
- example/example_reader.rb
|
26
|
+
- example/example_writer.rb
|
27
|
+
- example/spell_instructions.json
|
24
28
|
- lib/byteinterpreter.rb
|
25
29
|
- lib/byteinterpreter/instructions.rb
|
26
30
|
homepage: https://github.com/mkgremillion/ByteInterpreter
|