Class: NWN::Resources::ContentObject
- Inherits:
-
Object
- Object
- NWN::Resources::ContentObject
- Defined in:
- lib/nwn/res.rb
Overview
This is a generic index to a resource.
Instance Attribute Summary collapse
-
#io ⇒ Object
Returns the value of attribute io.
-
#offset ⇒ Object
Returns the value of attribute offset.
-
#res_type ⇒ Object
Returns the value of attribute res_type.
-
#resref ⇒ Object
Returns the value of attribute resref.
-
#size_override ⇒ Object
Returns the value of attribute size_override.
Class Method Summary collapse
-
.new_from(filename, io = nil) ⇒ Object
Create a new index to
filename
, optionally specifyingio
.
Instance Method Summary collapse
-
#extension ⇒ Object
Get the extension of this object.
-
#filename ⇒ Object
Get the canonical filename of this object.
-
#get ⇒ Object
Get the contents of this object.
-
#initialize(resref, res_type, io = nil, offset = nil, size = nil) ⇒ ContentObject
constructor
A new instance of ContentObject.
-
#size ⇒ Object
Get the size in bytes of this object.
Constructor Details
#initialize(resref, res_type, io = nil, offset = nil, size = nil) ⇒ ContentObject
Returns a new instance of ContentObject.
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
#io ⇒ Object
Returns the value of attribute io.
8 9 10 |
# File 'lib/nwn/res.rb', line 8 def io @io end |
#offset ⇒ Object
Returns the value of attribute offset.
9 10 11 |
# File 'lib/nwn/res.rb', line 9 def offset @offset end |
#res_type ⇒ Object
Returns the value of attribute res_type.
7 8 9 |
# File 'lib/nwn/res.rb', line 7 def res_type @res_type end |
#resref ⇒ Object
Returns the value of attribute resref.
6 7 8 |
# File 'lib/nwn/res.rb', line 6 def resref @resref end |
#size_override ⇒ Object
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.(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
#extension ⇒ Object
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 |
#filename ⇒ Object
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 |
#get ⇒ Object
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 |
#size ⇒ Object
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 |