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)


24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
# File 'lib/ttfunk/resource_file.rb', line 24

def initialize(io)
  @io = io

  data_offset, map_offset, map_length = @io.read(16).unpack('NNx4N')

  @map = {}
  # skip header copy, next map handle, file reference, and attrs
  @io.pos = map_offset + 24
  type_list_offset, name_list_offset = @io.read(4).unpack('n*')

  type_list_offset += map_offset
  name_list_offset += map_offset

  @io.pos = type_list_offset
  max_index = @io.read(2).unpack1('n')
  0.upto(max_index) do
    type, max_type_index, ref_list_offset = @io.read(8).unpack('A4nn')
    @map[type] = { list: [], named: {} }

    parse_from(type_list_offset + ref_list_offset) do
      0.upto(max_type_index) do
        id, name_ofs, attr = @io.read(5).unpack('nnC')
        data_ofs = @io.read(3)
        data_ofs = data_offset + [0, data_ofs].pack('CA*').unpack1('N')
        handle = @io.read(4).unpack1('N')

        entry = {
          id: id,
          attributes: attr,
          offset: data_ofs,
          handle: handle,
        }

        if name_list_offset + name_ofs < map_offset + map_length
          parse_from(name_ofs + name_list_offset) do
            len = @io.read(1).unpack1('C')
            entry[:name] = @io.read(len)
          end
        end

        @map[type][:list] << entry
        @map[type][:named][entry[:name]] = entry if entry[:name]
      end
    end
  end
end

Instance Attribute Details

#mapHash (readonly)

Resource map

Returns:

  • (Hash)


9
10
11
# File 'lib/ttfunk/resource_file.rb', line 9

def map
  @map
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



16
17
18
19
20
21
# File 'lib/ttfunk/resource_file.rb', line 16

def self.open(path)
  ::File.open(path, 'rb') { |io|
    file = new(io)
    yield(file)
  }
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)


81
82
83
84
85
86
87
88
89
90
91
# File 'lib/ttfunk/resource_file.rb', line 81

def [](type, index = 0)
  if @map[type]
    collection = index.is_a?(Integer) ? :list : :named
    if @map[type][collection][index]
      parse_from(@map[type][collection][index][:offset]) do
        length = @io.read(4).unpack1('N')
        return @io.read(length)
      end
    end
  end
end

#resources_for(type) ⇒ Array<String>

Get resource names

Parameters:

  • type (String)

Returns:

  • (Array<String>)


97
98
99
# File 'lib/ttfunk/resource_file.rb', line 97

def resources_for(type)
  ((@map[type] && @map[type][:named]) || {}).keys
end