Class: Sidtool::FileReader

Inherits:
Object
  • Object
show all
Defined in:
lib/sidtool/file_reader.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(format:, version:, init_address:, play_address:, songs:, start_song:, name:, author:, released:, data:) ⇒ FileReader

Returns a new instance of FileReader.



43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/sidtool/file_reader.rb', line 43

def initialize(format:, version:, init_address:, play_address:, songs:, start_song:, name:, author:, released:, data:)
  @format = format
  @version = version
  @init_address = init_address
  @play_address = play_address
  @songs = songs
  @start_song = start_song
  @name = name
  @author = author
  @released = released
  @data = data
end

Instance Attribute Details

#authorObject (readonly)

Returns the value of attribute author.



4
5
6
# File 'lib/sidtool/file_reader.rb', line 4

def author
  @author
end

#dataObject (readonly)

Returns the value of attribute data.



5
6
7
# File 'lib/sidtool/file_reader.rb', line 5

def data
  @data
end

#formatObject (readonly)

Returns the value of attribute format.



3
4
5
# File 'lib/sidtool/file_reader.rb', line 3

def format
  @format
end

#init_addressObject (readonly)

Returns the value of attribute init_address.



3
4
5
# File 'lib/sidtool/file_reader.rb', line 3

def init_address
  @init_address
end

#nameObject (readonly)

Returns the value of attribute name.



4
5
6
# File 'lib/sidtool/file_reader.rb', line 4

def name
  @name
end

#play_addressObject (readonly)

Returns the value of attribute play_address.



3
4
5
# File 'lib/sidtool/file_reader.rb', line 3

def play_address
  @play_address
end

#releasedObject (readonly)

Returns the value of attribute released.



4
5
6
# File 'lib/sidtool/file_reader.rb', line 4

def released
  @released
end

#songsObject (readonly)

Returns the value of attribute songs.



3
4
5
# File 'lib/sidtool/file_reader.rb', line 3

def songs
  @songs
end

#start_songObject (readonly)

Returns the value of attribute start_song.



3
4
5
# File 'lib/sidtool/file_reader.rb', line 3

def start_song
  @start_song
end

#versionObject (readonly)

Returns the value of attribute version.



3
4
5
# File 'lib/sidtool/file_reader.rb', line 3

def version
  @version
end

Class Method Details

.read(path) ⇒ Object



7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/sidtool/file_reader.rb', line 7

def self.read(path)
  contents = File.open(path, 'rb', encoding: 'ascii-8bit') { |file| file.read }

  expected_data_offset = 0x7C
  minimum_file_size = expected_data_offset

  raise "File is too small - it should be at least #{minimum_file_size} bytes. The file may be corrupt." unless contents.length >= minimum_file_size

  format = contents[0..3]
  raise "Unknown file format: #{format}. Only PSID is supported." unless format == 'PSID'

  version = read_word(contents[4..5])
  raise "Invalid version number: #{version}. Only versions 2, 3, and 4 are supported." unless version >= 2 && version <= 4

  data_offset = read_word(contents[6..7])
  raise "Invalid data offset: #{data_offset}. This has to be #{expected_data_offset}. The file may be corrupt." unless data_offset == expected_data_offset

  load_address = read_word(contents[8..9])
  raise "Unsupported load address: #{load_address}. Only 0 is supported for now." unless load_address == 0

  init_address = read_word(contents[10..11])
  play_address = read_word(contents[12..13])
  songs = read_word(contents[14..15])
  start_song = read_word(contents[16..17])

  name = read_null_terminated_string(contents[22..53])
  author = read_null_terminated_string(contents[54..85])
  released = read_null_terminated_string(contents[86..117])

  data = read_bytes(contents[data_offset..-1])

  return self.new(format: format, version: version, init_address: init_address, play_address: play_address,
                  songs: songs, start_song: start_song, name: name, author: author, released: released,
                  data: data)
end