Class NWN::Resources::ContentObject

  1. lib/nwn/res.rb
Parent: Object

This is a generic index to a resource.

Methods

public class

  1. new
  2. new_from

public instance

  1. extension
  2. filename
  3. get
  4. size

Attributes

io [RW]
offset [RW]
res_type [RW]
resref [RW]
size_override [RW]

Public class methods

new (resref, res_type, io = nil, offset = nil, size = nil)
[show source]
    # File lib/nwn/res.rb, line 25
25:       def initialize resref, res_type, io = nil, offset = nil, size = nil
26:         @resref, @res_type = resref.downcase, res_type
27:         @io, @offset = io, offset
28:         @size_override = size
29: 
30:         raise ArgumentError, "Invalid object passed: responds_to :read, want @offset, but does not respond_to :seek" if
31:           @io.respond_to?(:read) && @offset && @offset != 0 && !@io.respond_to?(:seek)
32:       end
new_from (filename, io = nil)

Create a new index to filename, optionally specifying io.

[show source]
    # File lib/nwn/res.rb, line 13
13:       def self.new_from filename, io = nil
14:         FileTest.exists?(filename) or raise Errno::ENOENT unless io
15: 
16:         filename = File.expand_path(filename)
17:         base = File.basename(filename).split(".")[0..-2].join(".").downcase
18:         ext = File.extname(filename)[1..-1].downcase rescue ""
19:         res_type = NWN::Resources::Extensions[ext] or raise ArgumentError,
20:           "Not a valid extension: #{ext.inspect} (while packing #{filename})"
21: 
22:         ContentObject.new(base, res_type, io || filename, 0, io ? io.size : File.stat(filename).size)
23:       end

Public instance methods

extension ()

Get the extension of this object.

[show source]
    # File lib/nwn/res.rb, line 57
57:       def extension
58:         NWN::Resources::Extensions.index(@res_type)
59:       end
filename ()

Get the canonical filename of this object.

[show source]
    # File lib/nwn/res.rb, line 52
52:       def filename
53:         @resref + "." + (self.extension || "unknown-#{@res_type}")
54:       end
get ()

Get the contents of this object. This is a costly operation, loading the whole buffer. If you want fine-grained access, use ContentObject#io and do it yourself, observing ContentObject#offset and ContentObject#size.

[show source]
    # File lib/nwn/res.rb, line 42
42:       def get
43:         if @io.respond_to?(:read)
44:           @io.seek(@offset ? @offset : 0)
45:           @io.e_read(self.size, "filename = #{filename}")
46:         else
47:           IO.read(@io)
48:         end
49:       end
size ()

Get the size in bytes of this object.

[show source]
    # File lib/nwn/res.rb, line 35
35:       def size
36:         @size_override || (@io.is_a?(IO) ? @io.stat.size : File.stat(@io).size)
37:       end