Class NWN::Key::Key

  1. lib/nwn/key.rb

Methods

public class

  1. new

Attributes

bif [R] An array of Bif objects contained in this key index. Not needed to access individual files, use Container#content instead.
day_of_year [R]
file_type [R]
file_version [R]
year [R]

Public class methods

new (io, data_path)

Creates a new Key wrapper. The parameters exepected are an IO object pointing to the .key-file, and the base path in which your data/.bif files can be found. (This is usually your NWN directory, NOT the data/ directory).

[show source]
     # File lib/nwn/key.rb, line 67
 67:       def initialize io, data_path
 68:         super()
 69: 
 70:         @root = data_path
 71:         @bif = []
 72: 
 73:         @file_type, @file_version,
 74:           bif_count, key_count,
 75:           offset_to_file_table, offset_to_key_table,
 76:           @year, @day_of_year, reserved =
 77:           io.e_read(8 + (4 * 6) + 32, "header").unpack("A4 A4 VVVVVV a32")
 78: 
 79:         io.seek(offset_to_file_table)
 80:         data = io.e_read(12 * bif_count, "bif data")
 81: 
 82:         # Contains all bifs linked in this key
 83:         i = 0
 84:         @file_table = []
 85:         while (x = data[i, 12]) && x.size == 12
 86:           i += 12
 87:           size, name_offset, name_size, drives = x.unpack("VVvv")
 88:           io.seek(name_offset)
 89:           name = io.e_read(name_size, "name table").unpack("A*")[0]
 90:           name.gsub!("\\", "/")
 91:           name = File.expand_path(@root + "/" + name)
 92: 
 93:           _io = File.new(name, "r")
 94:           @bif << Bif.new(self, _io)
 95: 
 96:           @file_table << [size, name, drives]
 97:         end
 98: 
 99:         @key_table = {}
100:         io.seek(offset_to_key_table)
101:         data = io.e_read(22 * key_count, "key table")
102:         i = 0
103:         while (x = data[i, 22]) && x.size == 22
104:           i += 22
105:           resref, res_type, res_id = x.unpack("A16 v V")
106:           @key_table[res_id] = [resref, res_type]
107:         end
108: 
109:         @fn_to_co = {}
110:         @key_table.each {|res_id, (resref, res_type)|
111:           bif_index = res_id >> 20
112:           bif = @bif[bif_index]
113:           id = res_id & 0xfffff
114:           bif.contained[id] or fail "#{bif} does not have #{id}"
115:           ofs, sz, _rt = bif.contained[id]
116:           o = NWN::Resources::ContentObject.new(resref, res_type, bif.io, ofs, sz)
117:           if @fn_to_co[o.filename] && @fn_to_co[o.filename][2] < bif_index
118:             oo, biff = @fn_to_co[o.filename]
119:             NWN.log_debug "#{o.filename} in #{biff.io.inspect} shadowed by file of same name in #{bif.io.inspect}"
120:             @content.delete(oo)
121:           end
122:           @fn_to_co[o.filename] = [o, bif, bif_index]
123:           @content << o
124:         }
125:       end