Class NWN::Erf::Erf

  1. lib/nwn/erf.rb

This reads and writes NWN::Resources::Container as valid ERF binary data.

Methods

public class

  1. new

public instance

  1. add
  2. add_file
  3. write_to

Attributes

day_of_year [RW]
description_str_ref [RW]
file_type [RW]
file_version [RW]
localized_strings [RW]
year [RW]

Public class methods

new (io = nil)

Create a new Erf object, optionally reading a existing file from io.

[show source]
    # File lib/nwn/erf.rb, line 20
20:       def initialize io = nil
21:         super()
22:         @localized_strings = {}
23:         @io = io
24:         @file_type, @file_version = "ERF", "V1.0"
25:         @year = Time.now.year - 1900
26:         @description_str_ref = 0xffffffff
27:         @day_of_year = Time.now.yday
28:         read_from io if io
29:       end

Public instance methods

add (o)
[show source]
    # File lib/nwn/erf.rb, line 38
38:       def add o
39:         fnlen = filename_length @file_version
40:         raise ArgumentError, "Invalid filename: #{o.filename.inspect}" if
41:           o.resref.size == 0 || o.resref.size > fnlen
42:         super(o)
43:       end
add_file (filename, io = nil)
[show source]
    # File lib/nwn/erf.rb, line 31
31:       def add_file filename, io = nil
32:         fnlen = filename_length @file_version
33:         raise ArgumentError, "Invalid filename: #{filename.inspect}" if
34:           filename.size == 0 || filename.size > (fnlen + 4)
35:         super(filename, io)
36:       end
write_to (io)

Writes this Erf to a io stream.

[show source]
     # File lib/nwn/erf.rb, line 127
127:       def write_to io
128:         fnlen = filename_length @file_version
129: 
130:         locstr = @localized_strings.map {|x| [x[0], x[1].size, x[1]].pack("V V a*") }.join("")
131:         keylist = @content.map {|c|
132:           NWN.log_debug "truncating filename #{c.resref}, longer than #{fnlen}" if c.resref.size > fnlen
133:           [c.resref, @content.index(c), c.res_type, 0].pack("a#{fnlen} V v v")
134:         }.join("")
135: 
136:         pre_offset = 160 + locstr.size + keylist.size + 8 * @content.size
137: 
138:         reslist = @content.map {|c|
139:           offset = pre_offset +
140:             @content[0, @content.index(c)].inject(0) {|sum,x| sum + x.size }
141:           [offset, c.size].pack("V V")
142:         }.join("")
143: 
144:         offset_to_locstr = 160
145:         offset_to_keylist = offset_to_locstr + locstr.size
146:         offset_to_resourcelist = offset_to_keylist + keylist.size
147: 
148:         header = [@file_type, @file_version,
149:           @localized_strings.size, locstr.size,
150:           @content.size,
151:           offset_to_locstr, offset_to_keylist,
152:           offset_to_resourcelist,
153:           @year, @day_of_year, @description_str_ref, ""].pack("A4 A4 VV VV VV VV V a116")
154: 
155:         io.write(header)
156:         io.write(locstr)
157:         io.write(keylist)
158:         io.write(reslist)
159: 
160:         @content.each {|c|
161:           io.write(c.get)
162:         }
163:       end