summaryrefslogtreecommitdiff
path: root/platform/javascript/js/engine/engine.js
blob: 4b8a7dde69d6b23c3ee9499c19dc3d49ccacebd2 (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
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
const Engine = (function () {
	const preloader = new Preloader();

	let wasmExt = '.wasm';
	let unloadAfterInit = true;
	let loadPath = '';
	let loadPromise = null;
	let initPromise = null;
	let stderr = null;
	let stdout = null;
	let progressFunc = null;

	function load(basePath) {
		if (loadPromise == null) {
			loadPath = basePath;
			loadPromise = preloader.loadPromise(basePath + wasmExt);
			preloader.setProgressFunc(progressFunc);
			requestAnimationFrame(preloader.animateProgress);
		}
		return loadPromise;
	}

	function unload() {
		loadPromise = null;
	}

	/** @constructor */
	function Engine() { // eslint-disable-line no-shadow
		this.canvas = null;
		this.executableName = '';
		this.rtenv = null;
		this.customLocale = null;
		this.resizeCanvasOnStart = false;
		this.onExecute = null;
		this.onExit = null;
		this.persistentPaths = ['/userfs'];
		this.gdnativeLibs = [];
	}

	Engine.prototype.init = /** @param {string=} basePath */ function (basePath) {
		if (initPromise) {
			return initPromise;
		}
		if (loadPromise == null) {
			if (!basePath) {
				initPromise = Promise.reject(new Error('A base path must be provided when calling `init` and the engine is not loaded.'));
				return initPromise;
			}
			load(basePath);
		}
		let config = {};
		if (typeof stdout === 'function') {
			config.print = stdout;
		}
		if (typeof stderr === 'function') {
			config.printErr = stderr;
		}
		const me = this;
		initPromise = new Promise(function (resolve, reject) {
			config['locateFile'] = Utils.createLocateRewrite(loadPath);
			config['instantiateWasm'] = Utils.createInstantiatePromise(loadPromise);
			// Emscripten configuration.
			config['thisProgram'] = me.executableName;
			config['noExitRuntime'] = true;
			config['dynamicLibraries'] = me.gdnativeLibs;
			Godot(config).then(function (module) {
				module['initFS'](me.persistentPaths).then(function (fs_err) {
					me.rtenv = module;
					if (unloadAfterInit) {
						unload();
					}
					resolve();
					config = null;
				});
			});
		});
		return initPromise;
	};

	/** @type {function(string, string):Object} */
	Engine.prototype.preloadFile = function (file, path) {
		return preloader.preload(file, path);
	};

	/** @type {function(...string):Object} */
	Engine.prototype.start = function () {
		// Start from arguments.
		const args = [];
		for (let i = 0; i < arguments.length; i++) {
			args.push(arguments[i]);
		}
		const me = this;
		return me.init().then(function () {
			if (!me.rtenv) {
				return Promise.reject(new Error('The engine must be initialized before it can be started'));
			}

			if (!(me.canvas instanceof HTMLCanvasElement)) {
				me.canvas = Utils.findCanvas();
				if (!me.canvas) {
					return Promise.reject(new Error('No canvas found in page'));
				}
			}

			// Canvas can grab focus on click, or key events won't work.
			if (me.canvas.tabIndex < 0) {
				me.canvas.tabIndex = 0;
			}

			// Disable right-click context menu.
			me.canvas.addEventListener('contextmenu', function (ev) {
				ev.preventDefault();
			}, false);

			// Until context restoration is implemented warn the user of context loss.
			me.canvas.addEventListener('webglcontextlost', function (ev) {
				alert('WebGL context lost, please reload the page'); // eslint-disable-line no-alert
				ev.preventDefault();
			}, false);

			// Browser locale, or custom one if defined.
			let locale = me.customLocale;
			if (!locale) {
				locale = navigator.languages ? navigator.languages[0] : navigator.language;
				locale = locale.split('.')[0];
			}
			// Godot configuration.
			me.rtenv['initConfig']({
				'resizeCanvasOnStart': me.resizeCanvasOnStart,
				'canvas': me.canvas,
				'locale': locale,
				'onExecute': function (p_args) {
					if (me.onExecute) {
						me.onExecute(p_args);
						return 0;
					}
					return 1;
				},
				'onExit': function (p_code) {
					me.rtenv['deinitFS']();
					if (me.onExit) {
						me.onExit(p_code);
					}
					me.rtenv = null;
				},
			});

			return new Promise(function (resolve, reject) {
				preloader.preloadedFiles.forEach(function (file) {
					me.rtenv['copyToFS'](file.path, file.buffer);
				});
				preloader.preloadedFiles.length = 0; // Clear memory
				me.rtenv['callMain'](args);
				initPromise = null;
				resolve();
			});
		});
	};

	Engine.prototype.startGame = function (execName, mainPack, extraArgs) {
		// Start and init with execName as loadPath if not inited.
		this.executableName = execName;
		const me = this;
		return Promise.all([
			this.init(execName),
			this.preloadFile(mainPack, mainPack),
		]).then(function () {
			let args = ['--main-pack', mainPack];
			if (extraArgs) {
				args = args.concat(extraArgs);
			}
			return me.start.apply(me, args);
		});
	};

	Engine.prototype.setWebAssemblyFilenameExtension = function (override) {
		if (String(override).length === 0) {
			throw new Error('Invalid WebAssembly filename extension override');
		}
		wasmExt = String(override);
	};

	Engine.prototype.setUnloadAfterInit = function (enabled) {
		unloadAfterInit = enabled;
	};

	Engine.prototype.setCanvas = function (canvasElem) {
		this.canvas = canvasElem;
	};

	Engine.prototype.setCanvasResizedOnStart = function (enabled) {
		this.resizeCanvasOnStart = enabled;
	};

	Engine.prototype.setLocale = function (locale) {
		this.customLocale = locale;
	};

	Engine.prototype.setExecutableName = function (newName) {
		this.executableName = newName;
	};

	Engine.prototype.setProgressFunc = function (func) {
		progressFunc = func;
	};

	Engine.prototype.setStdoutFunc = function (func) {
		const print = function (text) {
			let msg = text;
			if (arguments.length > 1) {
				msg = Array.prototype.slice.call(arguments).join(' ');
			}
			func(msg);
		};
		if (this.rtenv) {
			this.rtenv.print = print;
		}
		stdout = print;
	};

	Engine.prototype.setStderrFunc = function (func) {
		const printErr = function (text) {
			let msg = text;
			if (arguments.length > 1) {
				msg = Array.prototype.slice.call(arguments).join(' ');
			}
			func(msg);
		};
		if (this.rtenv) {
			this.rtenv.printErr = printErr;
		}
		stderr = printErr;
	};

	Engine.prototype.setOnExecute = function (onExecute) {
		this.onExecute = onExecute;
	};

	Engine.prototype.setOnExit = function (onExit) {
		this.onExit = onExit;
	};

	Engine.prototype.copyToFS = function (path, buffer) {
		if (this.rtenv == null) {
			throw new Error('Engine must be inited before copying files');
		}
		this.rtenv['copyToFS'](path, buffer);
	};

	Engine.prototype.setPersistentPaths = function (persistentPaths) {
		this.persistentPaths = persistentPaths;
	};

	Engine.prototype.setGDNativeLibraries = function (gdnativeLibs) {
		this.gdnativeLibs = gdnativeLibs;
	};

	Engine.prototype.requestQuit = function () {
		if (this.rtenv) {
			this.rtenv['request_quit']();
		}
	};

	// Closure compiler exported engine methods.
	/** @export */
	Engine['isWebGLAvailable'] = Utils.isWebGLAvailable;
	Engine['load'] = load;
	Engine['unload'] = unload;
	Engine.prototype['init'] = Engine.prototype.init;
	Engine.prototype['preloadFile'] = Engine.prototype.preloadFile;
	Engine.prototype['start'] = Engine.prototype.start;
	Engine.prototype['startGame'] = Engine.prototype.startGame;
	Engine.prototype['setWebAssemblyFilenameExtension'] = Engine.prototype.setWebAssemblyFilenameExtension;
	Engine.prototype['setUnloadAfterInit'] = Engine.prototype.setUnloadAfterInit;
	Engine.prototype['setCanvas'] = Engine.prototype.setCanvas;
	Engine.prototype['setCanvasResizedOnStart'] = Engine.prototype.setCanvasResizedOnStart;
	Engine.prototype['setLocale'] = Engine.prototype.setLocale;
	Engine.prototype['setExecutableName'] = Engine.prototype.setExecutableName;
	Engine.prototype['setProgressFunc'] = Engine.prototype.setProgressFunc;
	Engine.prototype['setStdoutFunc'] = Engine.prototype.setStdoutFunc;
	Engine.prototype['setStderrFunc'] = Engine.prototype.setStderrFunc;
	Engine.prototype['setOnExecute'] = Engine.prototype.setOnExecute;
	Engine.prototype['setOnExit'] = Engine.prototype.setOnExit;
	Engine.prototype['copyToFS'] = Engine.prototype.copyToFS;
	Engine.prototype['setPersistentPaths'] = Engine.prototype.setPersistentPaths;
	Engine.prototype['setGDNativeLibraries'] = Engine.prototype.setGDNativeLibraries;
	Engine.prototype['requestQuit'] = Engine.prototype.requestQuit;
	return Engine;
}());
if (typeof window !== 'undefined') {
	window['Engine'] = Engine;
}