Class: NWN::Erf::Erf

Inherits:
Resources::Container show all
Defined in:
lib/nwn/erf.rb

Overview

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

Instance Attribute Summary collapse

Attributes inherited from Resources::Container

#content

Instance Method Summary collapse

Methods inherited from Resources::Container

#filenames, #get, #get_content_object, #has?

Constructor Details

#initialize(io = nil) ⇒ Erf

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



20
21
22
23
24
25
26
27
28
29
# File 'lib/nwn/erf.rb', line 20

def initialize io = nil
  super()
  @localized_strings = {}
  @io = io
  @file_type, @file_version = "ERF", "V1.0"
  @year = Time.now.year
  @description_str_ref = 0xffffffff
  @day_of_year = Time.now.yday
  read_from io if io
end

Instance Attribute Details

#day_of_yearObject

Returns the value of attribute day_of_year.



16
17
18
# File 'lib/nwn/erf.rb', line 16

def day_of_year
  @day_of_year
end

#description_str_refObject

Returns the value of attribute description_str_ref.



12
13
14
# File 'lib/nwn/erf.rb', line 12

def description_str_ref
  @description_str_ref
end

#file_typeObject

Returns the value of attribute file_type.



14
15
16
# File 'lib/nwn/erf.rb', line 14

def file_type
  @file_type
end

#file_versionObject

Returns the value of attribute file_version.



15
16
17
# File 'lib/nwn/erf.rb', line 15

def file_version
  @file_version
end

#localized_stringsObject

Returns the value of attribute localized_strings.



11
12
13
# File 'lib/nwn/erf.rb', line 11

def localized_strings
  @localized_strings
end

#yearObject

Returns the value of attribute year.



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

def year
  @year
end

Instance Method Details

#add(o) ⇒ Object

Raises:

  • (ArgumentError)


38
39
40
41
42
43
# File 'lib/nwn/erf.rb', line 38

def add o
  fnlen = filename_length @file_version
  raise ArgumentError, "Invalid filename: #{o.filename.inspect}" if
    o.resref.size == 0 || o.resref.size > fnlen
  super(o)
end

#add_file(filename, io = nil) ⇒ Object

Raises:

  • (ArgumentError)


31
32
33
34
35
36
# File 'lib/nwn/erf.rb', line 31

def add_file filename, io = nil
  fnlen = filename_length @file_version
  raise ArgumentError, "Invalid filename: #{filename.inspect}" if
    filename.size == 0 || filename.size > (fnlen + 4)
  super(filename, io)
end

#write_to(io) ⇒ Object

Writes this Erf to a io stream.



133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
# File 'lib/nwn/erf.rb', line 133

def write_to io
  fnlen = filename_length @file_version

  locstr = @localized_strings.map {|x| [x[0], x[1].size, x[1]].pack("V V a*") }.join("")
  keylist = @content.map {|c|
    NWN.log_debug "truncating filename #{c.resref}, longer than #{fnlen}" if c.resref.size > fnlen
    [c.resref, @content.index(c), c.res_type, 0].pack("a#{fnlen} V v v")
  }.join("")

  pre_offset = 160 + locstr.size + keylist.size + 8 * @content.size

  reslist = @content.map {|c|
    offset = pre_offset +
      @content[0, @content.index(c)].inject(0) {|sum,x| sum + x.size }
    [offset, c.size].pack("V V")
  }.join("")

  offset_to_locstr = 160
  offset_to_keylist = offset_to_locstr + locstr.size
  offset_to_resourcelist = offset_to_keylist + keylist.size

  header = [@file_type, @file_version,
    @localized_strings.size, locstr.size,
    @content.size,
    offset_to_locstr, offset_to_keylist,
    offset_to_resourcelist,
    @year, @day_of_year, @description_str_ref, ""].pack("A4 A4 VV VV VV VV V a116")

  io.write(header)
  io.write(locstr)
  io.write(keylist)
  io.write(reslist)

  @content.each {|c|
    io.write(c.get)
  }
end