Class: MilkTea::AssetPack

Inherits:
Object
  • Object
show all
Defined in:
lib/milk_tea/tooling/asset_pack.rb

Defined Under Namespace

Classes: Entry, SourceEntry

Constant Summary collapse

MAGIC =
"MTAP".b.freeze
VERSION =
1
HEADER_FLAGS =
0
ENTRY_FLAGS_RAW =
0
HEADER_FORMAT =
"a4vvVQ<Q<".freeze
ENTRY_PREFIX_FORMAT =
"VVQ<Q<Q<".freeze
HEADER_SIZE =
[MAGIC, VERSION, HEADER_FLAGS, 0, 0, 0].pack(HEADER_FORMAT).bytesize
ENTRY_PREFIX_SIZE =
[0, ENTRY_FLAGS_RAW, 0, 0, 0].pack(ENTRY_PREFIX_FORMAT).bytesize

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(output_path, source_paths) ⇒ AssetPack

Returns a new instance of AssetPack.



26
27
28
29
# File 'lib/milk_tea/tooling/asset_pack.rb', line 26

def initialize(output_path, source_paths)
  @output_path = File.expand_path(output_path)
  @source_paths = source_paths.map { |source_path| File.expand_path(source_path) }
end

Class Method Details

.write(output_path, source_paths) ⇒ Object



22
23
24
# File 'lib/milk_tea/tooling/asset_pack.rb', line 22

def self.write(output_path, source_paths)
  new(output_path, source_paths).write
end

Instance Method Details

#writeObject



31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/milk_tea/tooling/asset_pack.rb', line 31

def write
  ensure_output_does_not_overlap_sources!

  source_entries = collect_source_entries
  index_size = source_entries.sum { |entry| ENTRY_PREFIX_SIZE + entry.logical_path.bytesize }
  data_offset = HEADER_SIZE + index_size
  entries = assign_offsets(source_entries, data_offset)

  FileUtils.mkdir_p(File.dirname(@output_path))
  Tempfile.create(["milk-tea-asset-pack", ".bin"], File.dirname(@output_path)) do |file|
    file.binmode
    write_header(file, entries.length, index_size, data_offset)
    write_index(file, entries)
    write_data(file, entries)
    file.flush
    file.close

    FileUtils.rm_f(@output_path)
    FileUtils.mv(file.path, @output_path)
  end

  @output_path
end