'''
A tool that generates FS API calls to generate a filesystem, and packages the files
to work with that.

This is called by emcc. You can also call it yourself.

You can split your files into "asset bundles", and create each bundle separately
with this tool. Then just include the generated js for each and they will load
the data and prepare it accordingly. This allows you to share assets and reduce
data downloads.

Usage:

  file_packager.py TARGET [--preload A [B..]] [--embed C [D..]] [--exclude E [F..]] [--compress COMPRESSION_DATA] [--crunch[=X]] [--js-output=OUTPUT.js] [--no-force] [--use-preload-cache] [--no-heap-copy]

  --preload  ,
  --embed    See emcc --help for more details on those options.

  --crunch=X Will compress dxt files to crn with quality level X. The crunch commandline tool must be present
             and CRUNCH should be defined in ~/.emscripten that points to it. JS crunch decompressing code will
             be added to convert the crn to dds in the browser.
             crunch-worker.js will be generated in the current directory. You should include that file when
             packaging your site.
             DDS files will not be crunched if the .crn is more recent than the .dds. This prevents a lot of
             unneeded computation.

  --js-output=FILE Writes output in FILE, if not specified, standard output is used.

  --no-force Don't create output if no valid input file is specified.

  --use-prelo
ad-cache Stores package in IndexedDB so that sub
sequent loads don't need to do XHR. Checks package version.

  --no-heap-copy If specified, the preloaded filesystem is not copied inside the Emscripten HEAP, but kept in a separate typed array outside it.
                 The default, if this is not specified, is to embed the VFS inside the HEAP, so that mmap()ing files in it is a no-op.
                 Passing this flag optimizes for fread() usage, omitting it optimizes for mmap() usage.

Notes:

  * The file packager generates unix-style file paths. So if you are on windows and a file is accessed at
    subdir\file, in JS it will be subdir/file. For simplicity we treat the web platform as a *NIX.

TODO:        You can also provide .crn files yourself, pre-crunched. With this option, they will be decompressed
             to dds files in the browser, exactly the same as if this tool compressed them.
'''

import os, sys, shutil, random, uuid, ctypes
import posixpath
import shared
from shared import Compression, execute, suffix, unsuffixed
from subprocess import Popen, PIPE, STDOUT
import fnmatch

if len(sys.argv) == 1:
  print '''Usage: file_packager.py TARGET [--preload A...] [--embed B...] [--exclude C...] [--compress COMPRESSION_DATA] [--crunch[=X]] [--js-output=OUTPUT.js] [--no-force] [--use-preload-cache] [--no-heap-copy]
See the source for more details.'''
  sys.exit(0)

DEBUG = os.environ.get('EMCC_DEBUG')

data_target = sys.argv[1]

IMAGE_SUFFIXES = ('.jpg', '.png', '.bmp')
AUDIO_SUFFIXES = ('.ogg', '.wav', '.mp3')
AUDIO_MIMETYPES = { 'ogg': 'audio/ogg', 'wav': 'audio/wav', 'mp3': 'audio/mpeg' }
CRUNCH_INPUT_SUFFIX = '.dds'
CRUNCH_OUTPUT_SUFFIX = '.crn'

DDS_HEADER_SIZE = 128

AV_WORKAROUND = 0 # Set to 1 to randomize file order and add some padding, to work around silly av false positives

data_files = []
excluded_patterns = []
leading = ''
has_preloaded = False
compress_cnt = 0
crunch = 0
plugins = []
jsoutput = None
force = True
# If set to True, IndexedDB (IDBFS in library_idbfs.js) is used to locally cache VFS XHR so that subsequent 
# page loads can read the data from the offline cache instead.
use_preload_cache = False
# If set to True, the blob received from XHR is moved to the Emscripten HEAP, optimizing for mmap() performance.
# If set to False, the XHR blob is kept intact, and fread()s etc. are performed directly to that data. This optimizes for minimal memory usage and fread() performance.
no_heap_copy = True

for arg in sys.argv[2:]:
  if arg == '--preload':
    has_preloaded = True
    leading = 'preload'
  elif arg == '--embed':
    leading = 'embed'
  elif arg == '--exclude':
    leading = 'exclude'
  elif arg == '--compress':
    compress_cnt = 1
    Compression.on = True
    leading = 'compress'
  elif arg == '--no-force':
    force = False
    leading = ''
  elif arg == '--use-preload-cache':
    use_preload_cache = True
    leading = ''
  elif arg == '--no-heap-copy':
    no_heap_copy = False
    leading = ''
  elif arg.startswith('--js-output'):
    jsoutput = arg.split('=')[1] if '=' in arg else None
    leading = ''
  elif arg.startswith('--crunch'):
    try:
      from shared import CRUNCH
    except Exception, e:
      print >> sys.stderr, 'could not import CRUNCH (make sure it is defined properly in ~/.emscripten)'
      raise e
    crunch = arg.split('=')[1] if '=' in arg else '128'
    leading = ''
  elif arg.startswith('--plugin'):
    plugin = open(arg.split('=')[1], 'r').read()
    eval(plugin) # should append itself to plugins
    leading = ''
  elif leading == 'preload' or leading == 'embed':
    mode = leading
    uses_at_notation = '@' in arg.replace('@@', '') # '@@' in input string means there is an actual @ character, a single '@' means the 'src@dst' notation.
    arg = arg.replace('@@', '@')
    if uses_at_notation:
      srcpath, dstpath = arg.split('@') # User is specifying destination filename explicitly.
    else:
      srcpath = dstpath = arg # Use source path as destination path.
    if os.path.isfile(srcpath) or os.path.isdir(srcpath):
      data_files.append({ 'srcpath': srcpath, 'dstpath': dstpath, 'mode': mode, 'explicit_dst_path': uses_at_notation })
    else:
      print >> sys.stderr, 'Warning: ' + arg + ' does not exist, ignoring.'
  elif leading == 'exclude':
    excluded_patterns.append(arg)
  elif leading == 'compress':
    if compress_cnt == 1:
      Compression.encoder = arg
      compress_cnt = 2
    elif compress_cnt == 2:
      Compression.decoder = arg
      compress_cnt = 3
    elif compress_cnt == 3:
      Compression.js_name = arg
      compress_cnt = 0
  else:
    print >> sys.stderr, 'Unknown parameter:', arg
    sys.exit(1)

if (not force) and len(data_files) == 0:
  has_preloaded = False

ret = '''
var Module;
if (typeof Module === 'undefined') Module = eval('(function() { try { return Module || {} } catch(e) { return {} } })()');
if (!Module.expectedDataFileDownloads) {
  Module.expectedDataFileDownloads = 0;
  Module.finishedDataFileDownloads = 0;
}
Module.expectedDataFileDownloads++;
(function() {
'''

code = '''
function assert(check, msg) {
  if (!check) throw msg + new Error().stack;
}
'''

# Win32 code to test whether the given file has the hidden property set.
def has_hidden_attribute(filepath):
  if sys.platform != 'win32':
    return False
    
  try:
    attrs = ctypes.windll.kernel32.GetFileAttributesW(unicode(filepath))
    assert attrs != -1
    result = bool(attrs & 2)
  except (AttributeError, AssertionError):
    result = False
  return result

# The packager should never preload/embed files if the file is hidden (Win32).
# or it matches any pattern specified in --exclude
def should_ignore(fullname):
  if has_hidden_attribute(fullname):
    return True
    
  for p in excluded_patterns:
    if fnmatch.fnmatch(fullname, p):
      return True
  return False

# Returns the given string with escapes added so that it can safely be placed inside a string in JS code.
def escape_for_js_string(s):
  s = s.replace("'", "\\'").replace('"', '\\"')
  return s

# Expand directories into individual files
def add(arg, dirname, names):
  # rootpathsrc: The path name of the root directory on the local FS we are adding to emscripten virtual FS.
  # rootpathdst: The name we want to make the source path available on the emscripten virtual FS.
  mode, rootpathsrc, rootpathdst = arg
  new_names = []
  for name in names:
    fullname = os.path.join(dirname, name)
    if should_ignore(fullname):
      if DEBUG:
        print >> sys.stderr, 'Skipping file "' + fullname + '" from inclusion in the emscripten virtual file system.'
    else:
      new_names.append(name)
      if not os.path.isdir(fullname):
        dstpath = os.path.j