Class: TTFunk::ResourceFile

Inherits:
Object
  • Object
show all
Defined in:
lib/ttfunk/resource_file.rb

Overview

Data-fork suitcases resource file

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(io) ⇒ ResourceFile

Returns a new instance of ResourceFile.

Parameters:

  • io (IO)
Source Code
lib/ttfunk/resource_file.rb, line 24
24
def initialize(io)
25
  @io = io
26
27
  data_offset, map_offset, map_length = @io.read(16).unpack('NNx4N')
28
29
  @map = {}
30
  # skip header copy, next map handle, file reference, and attrs
31
  @io.pos = map_offset + 24
32
  type_list_offset, name_list_offset = @io.read(4).unpack('n*')
33
34
  type_list_offset += map_offset
35
  name_list_offset += map_offset
36
37
  @io.pos = type_list_offset
38
  max_index = @io.read(2).unpack1('n')
39
  0.upto(max_index) do
40
    type, max_type_index, ref_list_offset = @io.read(8).unpack('A4nn')
41
    @map[type] = { list: [], named: {} }
42
43
    parse_from(type_list_offset + ref_list_offset) do
44
      0.upto(max_type_index) do
45
        id, name_ofs, attr = @io.read(5).unpack('nnC')
46
        data_ofs = @io.read(3)
47
        data_ofs = data_offset + [0, data_ofs].pack('CA*').unpack1('N')
48
        handle = @io.read(4).unpack1('N')
49
50
        entry = {
51
          id: id,
52
          attributes: attr,
53
          offset: data_ofs,
54
          handle: handle,
55
        }
56
57
        if name_list_offset + name_ofs < map_offset + map_length
58
          parse_from(name_ofs + name_list_offset) do
59
            len = @io.read(1).unpack1('C')
60
            entry[:name] = @io.read(len)
61
          end
62
        end
63
64
        @map[type][:list] << entry
65
        @map[type][:named][entry[:name]] = entry if entry[:name]
66
      end
67
    end
68
  end
69
end

Instance Attribute Details

#mapHash (readonly)

Resource map

Returns:

  • (Hash)
Source Code
lib/ttfunk/resource_file.rb, line 9
9
def map
10
  @map
11
end

Class Method Details

.open(path) {|resource_file| ... } ⇒ any

Open a resource file

Parameters:

  • path (String, Pathname)

Yield Parameters:

Returns:

  • (any)

    result of the block

Source Code
lib/ttfunk/resource_file.rb, line 16
16
def self.open(path)
17
  ::File.open(path, 'rb') { |io|
18
    file = new(io)
19
    yield(file)
20
  }
21
end

Instance Method Details

#[](type, index = 0) ⇒ String #[](type, name) ⇒ String

Get resource

Overloads:

  • #[](type, index = 0) ⇒ String

    Parameters:

    • type (String)
    • inxed (Integer)

    Returns:

    • (String)
  • #[](type, name) ⇒ String

    Parameters:

    • type (String)
    • name (String)

    Returns:

    • (String)
Source Code
lib/ttfunk/resource_file.rb, line 81
81
def [](type, index = 0)
82
  if @map[type]
83
    collection = index.is_a?(Integer) ? :list : :named
84
    if @map[type][collection][index]
85
      parse_from(@map[type][collection][index][:offset]) do
86
        length = @io.read(4).unpack1('N')
87
        return @io.read(length)
88
      end
89
    end
90
  end
91
end

#resources_for(type) ⇒ Array<String>

Get resource names

Parameters:

  • type (String)

Returns:

  • (Array<String>)
Source Code
lib/ttfunk/resource_file.rb, line 97
97
def resources_for(type)
98
  ((@map[type] && @map[type][:named]) || {}).keys
99
end