class FuturoCube::ResourceFile

Attributes

header[R]
records[R]

Public Class Methods

new(io) click to toggle source
# File lib/futurocube/resource_file.rb, line 28
def initialize(io)
  @io = io
end
open(file, &block) click to toggle source
# File lib/futurocube/resource_file.rb, line 84
def self.open(file, &block)
  if block
    File.open(file, File::RDONLY) do |io|
      rf = ResourceFile.new(io)
      begin
        block.call(rf)
      ensure
        rf.close
      end
    end
  else
    ResourceFile.new(File.open(file, File::RDONLY))
  end
end

Public Instance Methods

close() click to toggle source
# File lib/futurocube/resource_file.rb, line 99
def close
  @io.close
end
compute_checksum(&block) click to toggle source

Computes the checksum for the resource file.

@yield [done] Provides feedback about the progress of the operation.

# File lib/futurocube/resource_file.rb, line 65
def compute_checksum(&block)
  crc = CRC.new
  # Have to read this first because it might change the seek position.
  file_size = header.file_size
  @io.seek(8, IO::SEEK_SET)
  pos = 8
  length = 4096-8
  buf = nil
  while true
    buf = @io.read(length, buf)
    break if !buf
    crc.update(buf)
    pos += buf.size
    block.call(pos) if block
    length = 4096
  end
  crc.crc
end
data(rec) click to toggle source

Reads the data for a given record.

@param rec [ResourceRecord] the record to read the data for.

# File lib/futurocube/resource_file.rb, line 57
def data(rec)
  @io.seek(rec.data_offset, IO::SEEK_SET)
  @io.read(rec.data_length)
end