summaryrefslogtreecommitdiff
path: root/platform
diff options
context:
space:
mode:
authorRĂ©mi Verschelde <rverschelde@gmail.com>2018-04-04 22:26:14 +0200
committerGitHub <noreply@github.com>2018-04-04 22:26:14 +0200
commit919209907dfcfa1fddaf7cf5f11068f523f8ead5 (patch)
tree6aff3969762e4efa585337700d741d376dd46f90 /platform
parentbba80721dc6c3f8d58322d25d85431cd28925cc2 (diff)
parent3014e48ec5a985b9d143ba27b91e32b933dcfdad (diff)
Merge pull request #17792 from eska014/enginejs-preloadpaths
Handle directories in engine.js preloadFile()
Diffstat (limited to 'platform')
-rw-r--r--platform/javascript/detect.py7
-rw-r--r--platform/javascript/engine.js40
-rw-r--r--platform/javascript/pre.js2
3 files changed, 33 insertions, 16 deletions
diff --git a/platform/javascript/detect.py b/platform/javascript/detect.py
index be1866987a..a48cb872ee 100644
--- a/platform/javascript/detect.py
+++ b/platform/javascript/detect.py
@@ -139,13 +139,6 @@ def configure(env):
# This setting just makes WebGL 2 APIs available, it does NOT disable WebGL 1.
env.Append(LINKFLAGS=['-s', 'USE_WEBGL2=1'])
- # engine.js uses FS but is not currently evaluated by Emscripten, so export FS.
- # TODO: Getting rid of this export is desirable.
- extra_exports = [
- 'FS',
- ]
- env.Append(LINKFLAGS=['-s', 'EXTRA_EXPORTED_RUNTIME_METHODS="%s"' % repr(extra_exports)])
-
env.Append(LINKFLAGS=['-s', 'INVOKE_RUN=0'])
# TODO: Reevaluate usage of this setting now that engine.js manages engine runtime.
diff --git a/platform/javascript/engine.js b/platform/javascript/engine.js
index e6fb48d0d2..e4839af433 100644
--- a/platform/javascript/engine.js
+++ b/platform/javascript/engine.js
@@ -1,3 +1,5 @@
+ exposedLibs['PATH'] = PATH;
+ exposedLibs['FS'] = FS;
return Module;
},
};
@@ -12,6 +14,13 @@
var loadingFiles = {};
+ function getPathLeaf(path) {
+
+ while (path.endsWith('/'))
+ path = path.slice(0, -1);
+ return path.slice(path.lastIndexOf('/') + 1);
+ }
+
function getBasePath(path) {
if (path.endsWith('/'))
@@ -23,14 +32,15 @@
function getBaseName(path) {
- path = getBasePath(path);
- return path.slice(path.lastIndexOf('/') + 1);
+ return getPathLeaf(getBasePath(path));
}
Engine = function Engine() {
this.rtenv = null;
+ var LIBS = {};
+
var initPromise = null;
var unloadAfterInit = true;
@@ -80,11 +90,11 @@
return new Promise(function(resolve, reject) {
rtenvProps.onRuntimeInitialized = resolve;
rtenvProps.onAbort = reject;
- rtenvProps.engine.rtenv = Engine.RuntimeEnvironment(rtenvProps);
+ rtenvProps.engine.rtenv = Engine.RuntimeEnvironment(rtenvProps, LIBS);
});
}
- this.preloadFile = function(pathOrBuffer, bufferFilename) {
+ this.preloadFile = function(pathOrBuffer, destPath) {
if (pathOrBuffer instanceof ArrayBuffer) {
pathOrBuffer = new Uint8Array(pathOrBuffer);
@@ -93,14 +103,14 @@
}
if (pathOrBuffer instanceof Uint8Array) {
preloadedFiles.push({
- name: bufferFilename,
+ path: destPath,
buffer: pathOrBuffer
});
return Promise.resolve();
} else if (typeof pathOrBuffer === 'string') {
return loadPromise(pathOrBuffer, preloadProgressTracker).then(function(xhr) {
preloadedFiles.push({
- name: pathOrBuffer,
+ path: destPath || pathOrBuffer,
buffer: xhr.response
});
});
@@ -119,7 +129,12 @@
this.startGame = function(mainPack) {
executableName = getBaseName(mainPack);
- return Promise.all([this.init(getBasePath(mainPack)), this.preloadFile(mainPack)]).then(
+ return Promise.all([
+ // Load from directory,
+ this.init(getBasePath(mainPack)),
+ // ...but write to root where the engine expects it.
+ this.preloadFile(mainPack, getPathLeaf(mainPack))
+ ]).then(
Function.prototype.apply.bind(synchronousStart, this, [])
);
};
@@ -163,7 +178,16 @@
this.rtenv.thisProgram = executableName || getBaseName(basePath);
preloadedFiles.forEach(function(file) {
- this.rtenv.FS.createDataFile('/', file.name, new Uint8Array(file.buffer), true, true, true);
+ var dir = LIBS.PATH.dirname(file.path);
+ try {
+ LIBS.FS.stat(dir);
+ } catch (e) {
+ if (e.code !== 'ENOENT') {
+ throw e;
+ }
+ LIBS.FS.mkdirTree(dir);
+ }
+ LIBS.FS.createDataFile('/', file.path, new Uint8Array(file.buffer), true, true, true);
}, this);
preloadedFiles = null;
diff --git a/platform/javascript/pre.js b/platform/javascript/pre.js
index 311aa44fda..02194bc75e 100644
--- a/platform/javascript/pre.js
+++ b/platform/javascript/pre.js
@@ -1,2 +1,2 @@
var Engine = {
- RuntimeEnvironment: function(Module) {
+ RuntimeEnvironment: function(Module, exposedLibs) {