Class: RBStarbound::ExportCommand

Inherits:
Clamp::Command
  • Object
show all
Defined in:
lib/rbstarbound/commands/export.rb

Instance Method Summary collapse

Instance Method Details

#executeObject



13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/rbstarbound/commands/export.rb', line 13

def execute
  start = Time.now.to_i
  extracted = 0
  begin
    File.open(package.to_s, 'rb') do |file|
      RBStarbound::SBAsset6.verbose = true if verbose?
      RBStarbound::SBAsset6.package(file)
      if verbose?
        puts "Extracting #{RBStarbound::SBAsset6.file_count} files..."
      end
      percentage = [RBStarbound::SBAsset6.file_count / 100, 1].max
      RBStarbound::SBAsset6.index.each_key do |path|
        dest_path = dir + path
        dest_dir, = File.split(dest_path)
        FileUtils.mkdir_p(dest_dir) unless Dir.exist?(dest_dir)
        data = RBStarbound::SBAsset6.get(path)
        File.open(dest_path, 'wb') { |f| f.write(data) }
        extracted += 1
        next unless verbose?
        putc '.' if (extracted % percentage).zero?
      end
    end
    return RBStarbound::EX_OK unless verbose?
    puts
    puts "Extracted #{extracted} files in #{Time.now.to_i - start} seconds."
    return RBStarbound::EX_OK
  rescue StandardError => e
    RBStarbound.print_error(e)
    return RBStarbound::EX_ERR
  end
end