aboutsummaryrefslogtreecommitdiff
path: root/tools/file_packager.py
diff options
context:
space:
mode:
authorAlon Zakai <alonzakai@gmail.com>2012-06-18 10:52:13 -0700
committerAlon Zakai <alonzakai@gmail.com>2012-06-18 10:52:13 -0700
commit85252a46c2dabc8a41720390d1198b5260d16aa9 (patch)
tree41605923b046585924e3828e10eaede280eded53 /tools/file_packager.py
parent618eac34c8caa918b966deb051ffd0296f3bdcb1 (diff)
de-crunch in a worker
Diffstat (limited to 'tools/file_packager.py')
-rw-r--r--tools/file_packager.py40
1 files changed, 35 insertions, 5 deletions
diff --git a/tools/file_packager.py b/tools/file_packager.py
index 0cef3bcb..0832c1b3 100644
--- a/tools/file_packager.py
+++ b/tools/file_packager.py
@@ -16,6 +16,8 @@ Notes:
--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.
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.
@@ -134,7 +136,22 @@ data_files = filter(lambda file_: not was_seen(file_['name']), data_files)
# Crunch files
if crunch:
- print open(shared.path_from_root('tools', 'crunch.js')).read()
+ shutil.copyfile(shared.path_from_root('tools', 'crunch-worker.js'), 'crunch-worker.js')
+ print '''
+ var decrunchWorker = new Worker('crunch-worker.js');
+ var decrunchCallbacks = [];
+ decrunchWorker.onmessage = function(msg) {
+ decrunchCallbacks[msg.data.callbackID](msg.data.data);
+ decrunchCallbacks[msg.data.callbackID] = null;
+ };
+ function requestDecrunch(data, callback) {
+ decrunchWorker.postMessage({
+ data: data,
+ callbackID: decrunchCallbacks.length
+ });
+ decrunchCallbacks.push(callback);
+ }
+'''
for file_ in data_files:
if file_['name'].endswith(CRUNCH_INPUT_SUFFIX):
@@ -263,12 +280,17 @@ for file_ in data_files:
# decompress crunch format into dds
prepare = '''
var ddsHeader = byteArray.subarray(0, %(dds_header_size)d);
- var ddsData = deCrunch(byteArray.subarray(%(dds_header_size)d));
- byteArray = new Uint8Array(ddsHeader.length + ddsData.length);
- byteArray.set(ddsHeader, 0);
- byteArray.set(ddsData, %(dds_header_size)d);
+ requestDecrunch(byteArray.subarray(%(dds_header_size)d), function(ddsData) {
+ byteArray = new Uint8Array(ddsHeader.length + ddsData.length);
+ byteArray.set(ddsHeader, 0);
+ byteArray.set(ddsData, %(dds_header_size)d);
''' % { 'dds_header_size': DDS_HEADER_SIZE }
+ finish = '''
+ Module['removeRunDependency']();
+ });
+'''
+
code += '''
var %(varname)s = new %(request)s();
%(varname)s.open('GET', '%(filename)s', true);
@@ -343,3 +365,11 @@ print code
if pre_run:
print ' });\n'
+if crunch:
+ print '''
+ if (!Module['postRun']) Module['postRun'] = [];
+ Module["postRun"].push(function() {
+ decrunchWorker.terminate();
+ });
+'''
+