Module: Sabrina::Bytestream::ByteOutput
- Included in:
- Sabrina::Bytestream
- Defined in:
- lib/sabrina/bytestream/byte_output.rb
Overview
Methods that allow Sabrina::Bytestream to output the raw byte data or export it to various formats. Subclasses should override #to_bytes to allow generation from an internal representation if present, and add to the list of output formats if necessary.
Instance Method Summary collapse
-
#generate_bytes ⇒ Object
Subclasses should override this to return a string of bytes generated from the representation.
-
#present ⇒ Object
Subclasses should override this to update the representation from byte data.
-
#to_b ⇒ Object
Same as #to_bytes.
-
#to_bytes ⇒ String
Outputs a raw byte string.
-
#to_hex(pretty = false) ⇒ String
Returns a hexadecimal representation of the byte data, optionally grouping it into quartets if passed
true
as a parameter. -
#to_hex_reverse ⇒ String
Same as #to_hex, but reverses the bytes before conversion.
-
#to_i ⇒ Integer
Returns the byte data converted to a base-16 integer.
-
#to_lz77 ⇒ String
Outputs the byte data as a GBA-compatible Lz77-compressed stream, raising an error when the data is empty.
-
#to_s ⇒ String
Returns the output of #to_hex.
Instance Method Details
#generate_bytes ⇒ Object
Subclasses should override this to return a string of bytes generated from the representation.
42 43 44 |
# File 'lib/sabrina/bytestream/byte_output.rb', line 42 def generate_bytes @bytes_cache = present end |
#present ⇒ Object
Subclasses should override this to update the representation from byte data.
48 49 50 |
# File 'lib/sabrina/bytestream/byte_output.rb', line 48 def present @representation ||= to_bytes end |
#to_b ⇒ Object
Same as #to_bytes.
53 54 55 |
# File 'lib/sabrina/bytestream/byte_output.rb', line 53 def to_b to_bytes end |
#to_bytes ⇒ String
Outputs a raw byte string. ROM and either table and index (recommended) or offset should have been specified, otherwise the return string will be empty. This method is relied on for write and save operations.
This method uses an internal cache. The cache should be wiped automatically on changes to ROM or internal data, otherwise it can be wiped manually with RomOperations#clear_cache.
Subclasses should define #generate_bytes to create the bytestream from the internal representation instead and only read from the ROM if the internal representation is absent.
21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 |
# File 'lib/sabrina/bytestream/byte_output.rb', line 21 def to_bytes return @bytes_cache if @bytes_cache return @bytes_cache = generate_bytes if @representation l_offset = offset if @rom && offset if @lz77 data = @rom.read_lz77(l_offset) @length_cache = data[:original_length] @lz77_cache = data[:original_stream] return @bytes_cache = data[:stream] end return @bytes_cache = @rom.read_string(l_offset) if @is_gba_string return @bytes_cache = @rom.read(l_offset, @length) if @length end return '' end |
#to_hex(pretty = false) ⇒ String
Returns a hexadecimal representation of the byte data, optionally grouping it into quartets if passed true
as a parameter.
61 62 63 64 65 66 |
# File 'lib/sabrina/bytestream/byte_output.rb', line 61 def to_hex(pretty = false) h = to_bytes.each_byte.to_a.map { |x| format('%02x', x) }.join('') return h unless pretty h.scan(/......../).map { |x| x.scan(/../).join(':') }.join(' ') end |
#to_hex_reverse ⇒ String
Same as #to_hex, but reverses the bytes before conversion.
71 72 73 |
# File 'lib/sabrina/bytestream/byte_output.rb', line 71 def to_hex_reverse to_bytes.each_byte.to_a.map { |x| format('%02x', x) }.reverse.join('') end |
#to_i ⇒ Integer
Returns the byte data converted to a base-16 integer.
77 78 79 |
# File 'lib/sabrina/bytestream/byte_output.rb', line 77 def to_i to_hex.hex end |
#to_lz77 ⇒ String
Outputs the byte data as a GBA-compatible Lz77-compressed stream, raising an error when the data is empty. RomOperations#write_to_rom relies on this method when :lz77 is set to true.
This method uses an internal cache. The cache should be wiped automatically on changes to ROM or internal data, otherwise it can be wiped manually with RomOperations#clear_cache.
Subclasses should clear the internal cache by calling RomOperations#clear_cache whenever the internal representation has changed.
94 95 96 97 98 99 |
# File 'lib/sabrina/bytestream/byte_output.rb', line 94 def to_lz77 return @lz77_cache if @lz77_cache b = to_bytes fail 'Cannot compress empty data.' if b.empty? @lz77_cache = Lz77.compress(b) end |
#to_s ⇒ String
Returns the output of #to_hex.
Subclasses should override this to provide a concise textual representation of the internal data.
107 108 109 |
# File 'lib/sabrina/bytestream/byte_output.rb', line 107 def to_s "#{to_hex(true)}" end |