module Sabrina::Bytestream::ByteOutput

Methods that allow {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.

Public Instance Methods

generate_bytes() click to toggle source

Subclasses should override this to return a string of bytes generated from the representation.

# File lib/sabrina/bytestream/byte_output.rb, line 42
def generate_bytes
  @bytes_cache = present
end
present() click to toggle source

Subclasses should override this to update the representation from byte data.

# File lib/sabrina/bytestream/byte_output.rb, line 48
def present
  @representation ||= to_bytes
end
to_b() click to toggle source

Same as {#to_bytes}.

# File lib/sabrina/bytestream/byte_output.rb, line 53
def to_b
  to_bytes
end
to_bytes() click to toggle source

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.

@return [String]

# 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) click to toggle source

Returns a hexadecimal representation of the byte data, optionally grouping it into quartets if passed true as a parameter.

@return [String]

# 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() click to toggle source

Same as {#to_hex}, but reverses the bytes before conversion.

@return [String]

# 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() click to toggle source

Returns the byte data converted to a base-16 integer. @return [Integer]

# File lib/sabrina/bytestream/byte_output.rb, line 77
def to_i
  to_hex.hex
end
to_lz77() click to toggle source

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. @return [String]

# 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() click to toggle source

Returns the output of {#to_hex}.

Subclasses should override this to provide a concise textual representation of the internal data.

@return [String]

# File lib/sabrina/bytestream/byte_output.rb, line 107
def to_s
  "#{to_hex(true)}"
end