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
      def initialize io = nil
        super()
        @localized_strings = {}
        @io = io
        @file_type, @file_version = "ERF", "V1.0"
        @year = Time.now.year - 1900
        @description_str_ref = 0xffffffff
        @day_of_year = Time.now.yday
        read_from io if io
      end

Public instance methods

add (o)
[show source]
# 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)
[show source]
# 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)

Writes this Erf to a io stream.

[show source]
# File lib/nwn/erf.rb, line 127
      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