Module: Sabrina::Bytestream::ByteInput

Included in:
Sabrina::Bytestream
Defined in:
lib/sabrina/bytestream/byte_input.rb

Overview

Constructors that allow Sabrina::Bytestream to create byte data from ROMs or other sources. All methods should accept an optional final hash of options.

Subclasses should override or add to these methods as necessary, using the final hash to pass the internal representation and any other necessary parameters and defaults.

Instance Method Summary collapse

Instance Method Details

#from_bytes(b, h = {}) ⇒ Bytestream

Creates a new Sabrina::Bytestream object from bytes.

Parameters:

Returns:



16
17
18
19
20
# File 'lib/sabrina/bytestream/byte_input.rb', line 16

def from_bytes(b, h = {})
  h.merge!(bytes_cache: b)

  new(h)
end

#from_hex(s, h = {}) ⇒ Bytestream

Same as #from_bytes, but takes a hexadecimal string that will be converted to bytes.

Parameters:

Returns:



113
114
115
116
117
118
119
120
121
122
123
# File 'lib/sabrina/bytestream/byte_input.rb', line 113

def from_hex(s, h = {})
  if /[^0-9a-fx]/ =~ s.downcase
    fail "\'#{s}\' does not look like a hex string."
  end

  b = s.rpartition('x').last.scan(/../).map { |x| x.hex.chr }.join('')

  h.merge!(bytes_cache: b)

  new(h)
end

#from_rom(rom, offset, length = nil, h = {}) ⇒ Bytestream

Creates a new Sabrina::Bytestream object by reading length bytes from a ROM offset.

Parameters:

Returns:



79
80
81
82
83
84
85
86
87
# File 'lib/sabrina/bytestream/byte_input.rb', line 79

def from_rom(rom, offset, length = nil, h = {})
  h.merge!(
    rom: rom,
    offset: offset,
    length: length
  )

  new(h)
end

#from_rom_as_lz77(rom, offset, h = {}) ⇒ Bytestream

Creates a new Sabrina::Bytestream object from a ROM offset, attempting to read it as Lz77-compressed data.

Parameters:

Returns:



97
98
99
100
101
102
103
104
105
# File 'lib/sabrina/bytestream/byte_input.rb', line 97

def from_rom_as_lz77(rom, offset, h = {})
  h.merge!(
    rom: rom,
    offset: offset,
    lz77: true
  )

  new(h)
end

#from_table(rom, table, index, index_length, length = index_length, h = {}) ⇒ Bytestream

Creates a new Sabrina::Bytestream object that will read length bytes of data from a ROM table and index, expecting each entry before the index to be index_length bytes.

Parameters:

  • rom (Rom)
  • table (String, Symbol)

    a key specified in rom_data config.

  • index (Integer)

    the index to read from the table. For monsters, it should be the real (not dex) number of the monster you wish to act on.

  • index_length (Integer)

    the expected length of a single entry.

  • length (Integer) (defaults to: index_length)

    how many bytes to read from the calculated offset.

  • h (Hash) (defaults to: {})

Returns:



36
37
38
39
40
41
42
43
44
45
46
# File 'lib/sabrina/bytestream/byte_input.rb', line 36

def from_table(rom, table, index, index_length, length = index_length, h = {})
  h.merge!(
    rom: rom,
    table: table,
    index: index,
    index_length: index_length,
    length: length
  )

  new(h)
end

#from_table_as_pointer(rom, table, index, h = {}) ⇒ Bytestream

Creates a new Sabrina::Bytestream object that will read a pointer from a ROM table and index. This is the recommended method to read monster data from a ROM.

Parameters:

  • rom (Rom)
  • table (String, Symbol)

    a key specified in rom_data config.

  • index (Integer)

    the index to read from the table. For monsters, it should be the real (not dex) number of the monster you wish to act on.

  • h (Hash) (defaults to: {})

Returns:



59
60
61
62
63
64
65
66
67
68
# File 'lib/sabrina/bytestream/byte_input.rb', line 59

def from_table_as_pointer(rom, table, index, h = {})
  h.merge!(
    rom: rom,
    table: table,
    index: index,
    pointer_mode: true
  )

  new(h)
end