support files in directories in --embed-file and --preload-file
This commit is contained in:
Родитель
38821ffb77
Коммит
36afa3f0eb
30
emcc
30
emcc
|
@ -743,26 +743,38 @@ try:
|
|||
if len(embed_files) + len(preload_files) > 0:
|
||||
if DEBUG: print >> sys.stderr, 'emcc: setting up files'
|
||||
code = ''
|
||||
|
||||
partial_dirs = []
|
||||
for filename in embed_files + preload_files:
|
||||
dirname = os.path.dirname(filename)
|
||||
parts = dirname.split(os.path.sep)
|
||||
if dirname != '' and dirname != os.path.sep:
|
||||
for i in range(len(parts)):
|
||||
partial = os.path.sep.join(parts[:i])
|
||||
if partial not in partial_dirs:
|
||||
code += '''FS.createFolder('/%s', '%s', true, false);\n''' % (os.path.sep.join(parts[:i-1]), parts[-1])
|
||||
partial_dirs.append(partial)
|
||||
|
||||
for filename in embed_files:
|
||||
code += '''FS.createDataFile('/', '%s', %s, true, true);\n''' % (os.path.basename(filename), str(map(ord, open(filename, 'rb').read())))
|
||||
counter = 0
|
||||
for filename in preload_files:
|
||||
name = 'filePreload%d' % counter
|
||||
varname = 'filePreload%d' % counter
|
||||
counter += 1
|
||||
code += '''
|
||||
var %(name)s = new XMLHttpRequest();
|
||||
%(name)s.open("GET", "%(filename)s", true);
|
||||
%(name)s.responseType = 'arraybuffer';
|
||||
var %(varname)s = new XMLHttpRequest();
|
||||
%(varname)s.open("GET", "%(filename)s", true);
|
||||
%(varname)s.responseType = 'arraybuffer';
|
||||
addRunDependency();
|
||||
%(name)s.onload = function (oEvent) {
|
||||
var arrayBuffer = %(name)s.response; // Note: not X.responseText
|
||||
%(varname)s.onload = function() {
|
||||
var arrayBuffer = %(varname)s.response; // Note: not X.responseText
|
||||
assert(arrayBuffer, 'Loading file %(filename)s failed.');
|
||||
var byteArray = new Uint8Array(arrayBuffer);
|
||||
FS.createDataFile('/', '%(filename)s', byteArray, true, true);
|
||||
FS.createDataFile('/%(dirname)s', '%(basename)s', byteArray, true, true);
|
||||
removeRunDependency();
|
||||
};
|
||||
%(name)s.send(null);
|
||||
''' % { 'name': name, 'filename': filename }
|
||||
%(varname)s.send(null);
|
||||
''' % { 'varname': varname, 'filename': filename, 'dirname': os.path.dirname(filename), 'basename': os.path.basename(filename) }
|
||||
|
||||
src = open(final).read().replace('// {{PRE_RUN_ADDITIONS}}', code)
|
||||
final += '.files.js'
|
||||
|
|
|
@ -6243,6 +6243,44 @@ f.close()
|
|||
Popen([EMCC, os.path.join(self.get_dir(), 'main.cpp'), '--preload-file', 'somefile.txt', '-o', 'page.html']).communicate()
|
||||
self.run_browser('page.html', 'You should see |load me right before|.', '/report_result?1')
|
||||
|
||||
def test_emcc_multifile(self):
|
||||
# a few files inside a directory
|
||||
if not os.path.exists(os.path.join(self.get_dir(), 'subdirr')):
|
||||
os.makedirs(os.path.join(self.get_dir(), 'subdirr'));
|
||||
open(os.path.join(self.get_dir(), 'subdirr', 'data1.txt'), 'w').write('''1214141516171819''')
|
||||
open(os.path.join(self.get_dir(), 'subdirr', 'data2.txt'), 'w').write('''3.14159265358979''')
|
||||
open(os.path.join(self.get_dir(), 'main.cpp'), 'w').write(self.with_report_result(r'''
|
||||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
#include <emscripten.h>
|
||||
int main() {
|
||||
char buf[17];
|
||||
|
||||
FILE *f = fopen("subdirr/data1.txt", "r");
|
||||
fread(buf, 1, 16, f);
|
||||
buf[16] = 0;
|
||||
fclose(f);
|
||||
printf("|%s|\n", buf);
|
||||
int result = !strcmp("1214141516171819", buf);
|
||||
|
||||
FILE *f2 = fopen("subdirr/data2.txt", "r");
|
||||
fread(buf, 1, 16, f2);
|
||||
buf[16] = 0;
|
||||
fclose(f2);
|
||||
printf("|%s|\n", buf);
|
||||
result = result && !strcmp("3.14159265358979", buf);
|
||||
|
||||
REPORT_RESULT();
|
||||
return 0;
|
||||
}
|
||||
'''))
|
||||
|
||||
Popen([EMCC, os.path.join(self.get_dir(), 'main.cpp'), '--preload-file', 'subdirr/data1.txt', '--preload-file', 'subdirr/data2.txt', '-o', 'page.html']).communicate()
|
||||
self.run_browser('page.html', 'You should see two cool numbers', '/report_result?1')
|
||||
|
||||
def test_emcc_sdl_image(self):
|
||||
pass # load an image file, say jpg, get pixel data
|
||||
|
||||
def test_emcc_worker(self):
|
||||
# Test running in a web worker
|
||||
output = Popen([EMCC, path_from_root('tests', 'hello_world_worker.cpp'), '-o', 'worker.js'], stdout=PIPE, stderr=PIPE).communicate()
|
||||
|
|
Загрузка…
Ссылка в новой задаче