Class: NWN::Resources::ContentObject

Inherits:
Object
  • Object
show all
Defined in:
lib/nwn/res.rb

Overview

This is a generic index to a resource.

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(resref, res_type, io = nil, offset = nil, size = nil) ⇒ ContentObject

Returns a new instance of ContentObject.

Raises:

  • (ArgumentError)


25
26
27
28
29
30
31
32
# File 'lib/nwn/res.rb', line 25

def initialize resref, res_type, io = nil, offset = nil, size = nil
  @resref, @res_type = resref.downcase, res_type
  @io, @offset = io, offset
  @size_override = size

  raise ArgumentError, "Invalid object passed: responds_to :read, want @offset, but does not respond_to :seek" if
    @io.respond_to?(:read) && @offset && @offset != 0 && !@io.respond_to?(:seek)
end

Instance Attribute Details

#ioObject

Returns the value of attribute io.



8
9
10
# File 'lib/nwn/res.rb', line 8

def io
  @io
end

#offsetObject

Returns the value of attribute offset.



9
10
11
# File 'lib/nwn/res.rb', line 9

def offset
  @offset
end

#res_typeObject

Returns the value of attribute res_type.



7
8
9
# File 'lib/nwn/res.rb', line 7

def res_type
  @res_type
end

#resrefObject

Returns the value of attribute resref.



6
7
8
# File 'lib/nwn/res.rb', line 6

def resref
  @resref
end

#size_overrideObject

Returns the value of attribute size_override.



10
11
12
# File 'lib/nwn/res.rb', line 10

def size_override
  @size_override
end

Class Method Details

.new_from(filename, io = nil) ⇒ Object

Create a new index to filename, optionally specifying io.



13
14
15
16
17
18
19
20
21
22
23
# File 'lib/nwn/res.rb', line 13

def self.new_from filename, io = nil
  FileTest.exists?(filename) or raise Errno::ENOENT unless io

  filename = File.expand_path(filename)
  base = File.basename(filename).split(".")[0..-2].join(".").downcase
  ext = File.extname(filename)[1..-1].downcase rescue ""
  res_type = NWN::Resources::Extensions[ext] or raise ArgumentError,
    "Not a valid extension: #{ext.inspect} (while packing #{filename})"

  ContentObject.new(base, res_type, io || filename, 0, io ? io.size : File.stat(filename).size)
end

Instance Method Details

#extensionObject

Get the extension of this object.



57
58
59
# File 'lib/nwn/res.rb', line 57

def extension
  NWN::Resources::Extensions.index(@res_type)
end

#filenameObject

Get the canonical filename of this object.



52
53
54
# File 'lib/nwn/res.rb', line 52

def filename
  @resref + "." + (self.extension || "unknown-#{@res_type}")
end

#getObject

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.



42
43
44
45
46
47
48
49
# File 'lib/nwn/res.rb', line 42

def get
  if @io.respond_to?(:read)
    @io.seek(@offset ? @offset : 0)
    @io.e_read(self.size, "filename = #{filename}")
  else
    IO.read(@io)
  end
end

#sizeObject

Get the size in bytes of this object.



35
36
37
# File 'lib/nwn/res.rb', line 35

def size
  @size_override || (@io.is_a?(IO) ? @io.stat.size : File.stat(@io).size)
end