Class: Sc2::Reader

Inherits:
Object
  • Object
show all
Defined in:
lib/sc2/magick/reader.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(rep_file) ⇒ Reader

Returns a new instance of Reader.



4
5
6
7
8
# File 'lib/sc2/magick/reader.rb', line 4

def initialize rep_file
  @rep_file = rep_file
  set_hdrs
  set_tbls
end

Instance Attribute Details

#user_hdrObject (readonly)

Returns the value of attribute user_hdr.



3
4
5
# File 'lib/sc2/magick/reader.rb', line 3

def user_hdr
  @user_hdr
end

Instance Method Details

#read_file(file) ⇒ Object



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
# File 'lib/sc2/magick/reader.rb', line 10

def read_file file
  hasher = Hasher.new
  hash_a = hasher.hash_for :hash_a, file
  hash_b = hasher.hash_for :hash_b, file
  hash_entry = @hash_tbl.find { |h| [h.hash_a, h.hash_b] == [hash_a, hash_b] }
  block_entry = @block_tbl[hash_entry.block_index]
  @rep_file.seek @user_hdr.archive_header_offset + block_entry.block_offset
  file_data = @rep_file.read block_entry.archived_size
  if block_entry.single_unit?
    file_data = Bzip2.uncompress file_data[1, file_data.length] if block_entry.compressed? && file_data.bytes.next == 16
    return file_data
  end
  sector_size = 512 << @archive_hdr.sector_size_shift
  sectors = block_entry.size / sector_size + 1
  sectors += 1 if block_entry.has_checksums
  positions = file_data[0, 4 * (sectors + 1)].unpack "V#{sectors + 1}"
  sectors = []
  positions.each_with_index do |pos, i|
    break if i + 1 == positions.length
    sector = file_data[pos, positions[i + 1] - pos]
    sector = Bzip2.uncompress sector if block_entry.compressed? && block_entry.size > block_entry.archived_size && sector.bytes.next == 16
    sectors << sector
  end
  sectors.join ''
end