summaryrefslogtreecommitdiff
path: root/platform/javascript/export/export.cpp
blob: b436d5236390e14bc7cf8ded0ec78cff2234f48c (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
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
/*************************************************************************/
/*  export.cpp                                                           */
/*************************************************************************/
/*                       This file is part of:                           */
/*                           GODOT ENGINE                                */
/*                    http://www.godotengine.org                         */
/*************************************************************************/
/* Copyright (c) 2007-2017 Juan Linietsky, Ariel Manzur.                 */
/* Copyright (c) 2014-2017 Godot Engine contributors (cf. AUTHORS.md)    */
/*                                                                       */
/* Permission is hereby granted, free of charge, to any person obtaining */
/* a copy of this software and associated documentation files (the       */
/* "Software"), to deal in the Software without restriction, including   */
/* without limitation the rights to use, copy, modify, merge, publish,   */
/* distribute, sublicense, and/or sell copies of the Software, and to    */
/* permit persons to whom the Software is furnished to do so, subject to */
/* the following conditions:                                             */
/*                                                                       */
/* The above copyright notice and this permission notice shall be        */
/* included in all copies or substantial portions of the Software.       */
/*                                                                       */
/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,       */
/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF    */
/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/
/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY  */
/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,  */
/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE     */
/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.                */
/*************************************************************************/
#include "editor/editor_node.h"
#include "editor_export.h"
#include "io/zip_io.h"
#include "platform/javascript/logo.gen.h"
#include "platform/javascript/run_icon.gen.h"

#define EXPORT_TEMPLATE_WEBASSEMBLY_RELEASE "webassembly_release.zip"
#define EXPORT_TEMPLATE_WEBASSEMBLY_DEBUG "webassembly_debug.zip"
#define EXPORT_TEMPLATE_ASMJS_RELEASE "javascript_release.zip"
#define EXPORT_TEMPLATE_ASMJS_DEBUG "javascript_debug.zip"

class EditorExportPlatformJavaScript : public EditorExportPlatform {

	GDCLASS(EditorExportPlatformJavaScript, EditorExportPlatform)

	Ref<ImageTexture> logo;
	Ref<ImageTexture> run_icon;
	bool runnable_when_last_polled;

	void _fix_html(Vector<uint8_t> &p_html, const Ref<EditorExportPreset> &p_preset, const String &p_name, bool p_debug);
	void _fix_fsloader_js(Vector<uint8_t> &p_js, const String &p_pack_name, uint64_t p_pack_size);

public:
	enum Target {
		TARGET_WEBASSEMBLY,
		TARGET_ASMJS
	};

	virtual void get_preset_features(const Ref<EditorExportPreset> &p_preset, List<String> *r_features);

	virtual void get_export_options(List<ExportOption> *r_options);
	virtual bool get_option_visibility(const String &p_option, const Map<StringName, Variant> &p_options) const;

	virtual String get_name() const;
	virtual String get_os_name() const;
	virtual Ref<Texture> get_logo() const;

	virtual bool can_export(const Ref<EditorExportPreset> &p_preset, String &r_error, bool &r_missing_templates) const;
	virtual String get_binary_extension() const;
	virtual Error export_project(const Ref<EditorExportPreset> &p_preset, bool p_debug, const String &p_path, int p_flags = 0);

	virtual bool poll_devices();
	virtual int get_device_count() const;
	virtual String get_device_name(int p_device) const { return TTR("Run in Browser"); }
	virtual String get_device_info(int p_device) const { return TTR("Run exported HTML in the system's default browser."); }
	virtual Error run(const Ref<EditorExportPreset> &p_preset, int p_device, int p_debug_flags);
	virtual Ref<Texture> get_run_icon() const;

	virtual void get_platform_features(List<String> *r_features) {

		r_features->push_back("web");
		r_features->push_back("JavaScript");
	}

	EditorExportPlatformJavaScript();
};

void EditorExportPlatformJavaScript::_fix_html(Vector<uint8_t> &p_html, const Ref<EditorExportPreset> &p_preset, const String &p_name, bool p_debug) {

	String str_template = String::utf8(reinterpret_cast<const char *>(p_html.ptr()), p_html.size());
	String str_export;
	Vector<String> lines = str_template.split("\n");

	int memory_mb;
	if (p_preset->get("options/target").operator int() != TARGET_ASMJS)
		// WebAssembly allows memory growth, so start with a reasonable default
		memory_mb = 1 << 4;
	else
		memory_mb = 1 << (p_preset->get("options/memory_size").operator int() + 5);

	for (int i = 0; i < lines.size(); i++) {

		String current_line = lines[i];
		current_line = current_line.replace("$GODOT_TMEM", itos(memory_mb * 1024 * 1024));
		current_line = current_line.replace("$GODOT_BASE", p_name);
		current_line = current_line.replace("$GODOT_HEAD_INCLUDE", p_preset->get("html/head_include"));
		current_line = current_line.replace("$GODOT_DEBUG_ENABLED", p_debug ? "true" : "false");
		str_export += current_line + "\n";
	}

	CharString cs = str_export.utf8();
	p_html.resize(cs.length());
	for (int i = 0; i < cs.length(); i++) {
		p_html[i] = cs[i];
	}
}

void EditorExportPlatformJavaScript::_fix_fsloader_js(Vector<uint8_t> &p_js, const String &p_pack_name, uint64_t p_pack_size) {

	String str_template = String::utf8(reinterpret_cast<const char *>(p_js.ptr()), p_js.size());
	String str_export;
	Vector<String> lines = str_template.split("\n");
	for (int i = 0; i < lines.size(); i++) {
		if (lines[i].find("$GODOT_PACK_NAME") != -1) {
			str_export += lines[i].replace("$GODOT_PACK_NAME", p_pack_name);
		} else if (lines[i].find("$GODOT_PACK_SIZE") != -1) {
			str_export += lines[i].replace("$GODOT_PACK_SIZE", itos(p_pack_size));
		} else {
			str_export += lines[i] + "\n";
		}
	}

	CharString cs = str_export.utf8();
	p_js.resize(cs.length());
	for (int i = 0; i < cs.length(); i++) {
		p_js[i] = cs[i];
	}
}

void EditorExportPlatformJavaScript::get_preset_features(const Ref<EditorExportPreset> &p_preset, List<String> *r_features) {

	if (p_preset->get("texture_format/s3tc")) {
		r_features->push_back("s3tc");
	}
	if (p_preset->get("texture_format/etc")) {
		r_features->push_back("etc");
	}
	if (p_preset->get("texture_format/etc2")) {
		r_features->push_back("etc2");
	}
}

void EditorExportPlatformJavaScript::get_export_options(List<ExportOption> *r_options) {

	r_options->push_back(ExportOption(PropertyInfo(Variant::INT, "options/target", PROPERTY_HINT_ENUM, "WebAssembly,asm.js"), TARGET_WEBASSEMBLY));
	r_options->push_back(ExportOption(PropertyInfo(Variant::INT, "options/memory_size", PROPERTY_HINT_ENUM, "32 MB,64 MB,128 MB,256 MB,512 MB,1 GB"), 3));
	r_options->push_back(ExportOption(PropertyInfo(Variant::BOOL, "texture_format/s3tc"), false));
	r_options->push_back(ExportOption(PropertyInfo(Variant::BOOL, "texture_format/etc"), true));
	r_options->push_back(ExportOption(PropertyInfo(Variant::BOOL, "texture_format/etc2"), false));
	r_options->push_back(ExportOption(PropertyInfo(Variant::STRING, "html/head_include", PROPERTY_HINT_MULTILINE_TEXT), ""));
	r_options->push_back(ExportOption(PropertyInfo(Variant::STRING, "custom_template/release", PROPERTY_HINT_GLOBAL_FILE, "zip"), ""));
	r_options->push_back(ExportOption(PropertyInfo(Variant::STRING, "custom_template/debug", PROPERTY_HINT_GLOBAL_FILE, "zip"), ""));
}

bool EditorExportPlatformJavaScript::get_option_visibility(const String &p_option, const Map<StringName, Variant> &p_options) const {

	if (p_option == "options/memory_size") {
		return p_options["options/target"].operator int() == TARGET_ASMJS;
	}
	return true;
}

String EditorExportPlatformJavaScript::get_name() const {

	return "HTML5";
}

String EditorExportPlatformJavaScript::get_os_name() const {

	return "JavaScript";
}

Ref<Texture> EditorExportPlatformJavaScript::get_logo() const {

	return logo;
}

bool EditorExportPlatformJavaScript::can_export(const Ref<EditorExportPreset> &p_preset, String &r_error, bool &r_missing_templates) const {

	r_missing_templates = false;

	if (p_preset->get("options/target").operator int() == TARGET_WEBASSEMBLY) {
		if (find_export_template(EXPORT_TEMPLATE_WEBASSEMBLY_RELEASE) == String())
			r_missing_templates = true;
		else if (find_export_template(EXPORT_TEMPLATE_WEBASSEMBLY_DEBUG) == String())
			r_missing_templates = true;
	} else {
		if (find_export_template(EXPORT_TEMPLATE_ASMJS_RELEASE) == String())
			r_missing_templates = true;
		else if (find_export_template(EXPORT_TEMPLATE_ASMJS_DEBUG) == String())
			r_missing_templates = true;
	}

	return !r_missing_templates;
}

String EditorExportPlatformJavaScript::get_binary_extension() const {

	return "html";
}

Error EditorExportPlatformJavaScript::export_project(const Ref<EditorExportPreset> &p_preset, bool p_debug, const String &p_path, int p_flags) {

	String custom_debug = p_preset->get("custom_template/debug");
	String custom_release = p_preset->get("custom_template/release");

	String template_path = p_debug ? custom_debug : custom_release;

	template_path = template_path.strip_edges();

	if (template_path == String()) {

		if (p_preset->get("options/target").operator int() == TARGET_WEBASSEMBLY) {
			if (p_debug)
				template_path = find_export_template(EXPORT_TEMPLATE_WEBASSEMBLY_DEBUG);
			else
				template_path = find_export_template(EXPORT_TEMPLATE_WEBASSEMBLY_RELEASE);
		} else {
			if (p_debug)
				template_path = find_export_template(EXPORT_TEMPLATE_ASMJS_DEBUG);
			else
				template_path = find_export_template(EXPORT_TEMPLATE_ASMJS_RELEASE);
		}
	}

	if (template_path != String() && !FileAccess::exists(template_path)) {
		EditorNode::get_singleton()->show_warning(TTR("Template file not found:\n") + template_path);
		return ERR_FILE_NOT_FOUND;
	}

	String pck_path = p_path.get_basename() + ".pck";
	Error error = save_pack(p_preset, pck_path);
	if (error != OK) {
		EditorNode::get_singleton()->show_warning(TTR("Could not write file:\n") + pck_path);
		return error;
	}

	FileAccess *f = FileAccess::open(pck_path, FileAccess::READ);
	if (!f) {
		EditorNode::get_singleton()->show_warning(TTR("Could not read file:\n") + pck_path);
		return ERR_FILE_CANT_READ;
	}
	size_t pack_size = f->get_len();
	memdelete(f);

	FileAccess *src_f = NULL;
	zlib_filefunc_def io = zipio_create_io_from_file(&src_f);
	unzFile pkg = unzOpen2(template_path.utf8().get_data(), &io);

	if (!pkg) {

		EditorNode::get_singleton()->show_warning(TTR("Could not open template for export:\n") + template_path);
		return ERR_FILE_NOT_FOUND;
	}

	int ret = unzGoToFirstFile(pkg);
	while (ret == UNZ_OK) {

		//get filename
		unz_file_info info;
		char fname[16384];
		ret = unzGetCurrentFileInfo(pkg, &info, fname, 16384, NULL, 0, NULL, 0);

		String file = fname;

		Vector<uint8_t> data;
		data.resize(info.uncompressed_size);

		//read
		unzOpenCurrentFile(pkg);
		unzReadCurrentFile(pkg, data.ptr(), data.size());
		unzCloseCurrentFile(pkg);

		//write

		if (file == "godot.html") {

			_fix_html(data, p_preset, p_path.get_file().get_basename(), p_debug);
			file = p_path.get_file();
		} else if (file == "godotfs.js") {

			_fix_fsloader_js(data, pck_path.get_file(), pack_size);
			file = p_path.get_file().get_basename() + "fs.js";
		} else if (file == "godot.js") {

			file = p_path.get_file().get_basename() + ".js";
		} else if (file == "godot.wasm") {

			file = p_path.get_file().get_basename() + ".wasm";
		} else if (file == "godot.asm.js") {

			file = p_path.get_file().get_basename() + ".asm.js";
		} else if (file == "godot.mem") {

			file = p_path.get_file().get_basename() + ".mem";
		}

		String dst = p_path.get_base_dir().plus_file(file);
		FileAccess *f = FileAccess::open(dst, FileAccess::WRITE);
		if (!f) {
			EditorNode::get_singleton()->show_warning(TTR("Could not write file:\n") + dst);
			unzClose(pkg);
			return ERR_FILE_CANT_WRITE;
		}
		f->store_buffer(data.ptr(), data.size());
		memdelete(f);

		ret = unzGoToNextFile(pkg);
	}

	return OK;
}

bool EditorExportPlatformJavaScript::poll_devices() {

	Ref<EditorExportPreset> preset;

	for (int i = 0; i < EditorExport::get_singleton()->get_export_preset_count(); i++) {

		Ref<EditorExportPreset> ep = EditorExport::get_singleton()->get_export_preset(i);
		if (ep->is_runnable() && ep->get_platform() == this) {
			preset = ep;
			break;
		}
	}

	bool prev = runnable_when_last_polled;
	runnable_when_last_polled = preset.is_valid();
	return runnable_when_last_polled != prev;
}

int EditorExportPlatformJavaScript::get_device_count() const {

	return runnable_when_last_polled;
}

Error EditorExportPlatformJavaScript::run(const Ref<EditorExportPreset> &p_preset, int p_device, int p_debug_flags) {

	String path = EditorSettings::get_singleton()->get_settings_path() + "/tmp/tmp_export.html";
	Error err = export_project(p_preset, true, path, p_debug_flags);
	if (err) {
		return err;
	}
	OS::get_singleton()->shell_open(path);
	return OK;
}

Ref<Texture> EditorExportPlatformJavaScript::get_run_icon() const {

	return run_icon;
}

EditorExportPlatformJavaScript::EditorExportPlatformJavaScript() {

	Ref<Image> img = memnew(Image(_javascript_logo));
	logo.instance();
	logo->create_from_image(img);

	img = Ref<Image>(memnew(Image(_javascript_run_icon)));
	run_icon.instance();
	run_icon->create_from_image(img);

	runnable_when_last_polled = false;
}

void register_javascript_exporter() {

	Ref<EditorExportPlatformJavaScript> platform;
	platform.instance();
	EditorExport::get_singleton()->add_export_platform(platform);
}