summaryrefslogtreecommitdiff
path: root/platform/javascript/engine/loader.js
blob: d27fbf612ee1de0fc4a48b65bcc69fa5e14b6870 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
var Loader = /** @constructor */ function() {

	this.env = null;

	this.init = function(loadPromise, basePath, config) {
		var me = this;
		return new Promise(function(resolve, reject) {
			var cfg = config || {};
			cfg['locateFile'] = Utils.createLocateRewrite(basePath);
			cfg['instantiateWasm'] = Utils.createInstantiatePromise(loadPromise);
			loadPromise = null;
			Godot(cfg).then(function(module) {
				me.env = module;
				resolve();
			});
		});
	}

	this.start = function(preloadedFiles, args) {
		var me = this;
		return new Promise(function(resolve, reject) {
			if (!me.env) {
				reject(new Error('The engine must be initialized before it can be started'));
			}
			preloadedFiles.forEach(function(file) {
				Utils.copyToFS(me.env['FS'], file.path, file.buffer);
			});
			preloadedFiles.length = 0; // Clear memory
			me.env['callMain'](args);
			resolve();
		});
	}
};