Class: NWN::Key::Bif

Inherits:
Object
  • Object
show all
Defined in:
lib/nwn/key.rb

Overview

A Bif object encapsulates an open file handle pointing to a .bif file. It’s contents are indexed on first access, not on creation by NWN::Key::Key (to speed up things).

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(key, io) ⇒ Bif

Returns a new instance of Bif.



19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/nwn/key.rb', line 19

def initialize key, io
  @key = key
  @io = io

  @contained = {}

  @file_type, @file_version,
    @var_res_count, @fix_res_count,
    @var_table_offset =
    io.e_read(4 + 4 + 3 * 4, "header").unpack("a4 a4 V V V")

  @io.seek(@var_table_offset)
  data = @io.e_read(@var_res_count * 16, "var res table")
  i = 0
  while (x = data[i, 16]) && x.size == 16
    i += 16
    id, offset, size, restype = x.unpack("V V V V")
    id &= 0xfffff
    @contained[id] = [offset, size, restype]
  end
end

Instance Attribute Details

#containedObject (readonly)

A hash containing the resources contained. Usually not needed, accessed by the encapsulating Key object.



17
18
19
# File 'lib/nwn/key.rb', line 17

def contained
  @contained
end

#ioObject (readonly)

The IO object pointing to the .bif file.



13
14
15
# File 'lib/nwn/key.rb', line 13

def io
  @io
end

#keyObject (readonly)

The Key object this Bif belongs to.



10
11
12
# File 'lib/nwn/key.rb', line 10

def key
  @key
end

Instance Method Details

#get_res_id(id) ⇒ Object



45
46
47
48
49
# File 'lib/nwn/key.rb', line 45

def get_res_id id
  offset, size, restype = @contained[id]
  @io.seek(offset)
  @io.e_read(size, "resource #{id} of type #{restype}")
end

#has_res_id?(id) ⇒ Boolean

Returns:

  • (Boolean)


41
42
43
# File 'lib/nwn/key.rb', line 41

def has_res_id? id
  @contained[id] != nil
end