Debian Codegraph @

platform/web/export/export_plugin.cpp

raw symbols SCIP
1 /**************************************************************************/
2 /* export_plugin.cpp */
3 /**************************************************************************/
4 /* This file is part of: */
5 /* GODOT ENGINE */
6 /* https://godotengine.org */
7 /**************************************************************************/
8 /* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
9 /* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
10 /* */
11 /* Permission is hereby granted, free of charge, to any person obtaining */
12 /* a copy of this software and associated documentation files (the */
13 /* "Software"), to deal in the Software without restriction, including */
14 /* without limitation the rights to use, copy, modify, merge, publish, */
15 /* distribute, sublicense, and/or sell copies of the Software, and to */
16 /* permit persons to whom the Software is furnished to do so, subject to */
17 /* the following conditions: */
18 /* */
19 /* The above copyright notice and this permission notice shall be */
20 /* included in all copies or substantial portions of the Software. */
21 /* */
22 /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
23 /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
24 /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */
25 /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
26 /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
27 /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
28 /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
29 /**************************************************************************/
30
31 #include "export_plugin.h"
32
33 #include "logo_svg.gen.h"
34 #include "run_icon_svg.gen.h"
35
36 #include "core/config/project_settings.h"
37 #include "core/io/dir_access.h"
38 #include "editor/editor_string_names.h"
39 #include "editor/export/editor_export.h"
40 #include "editor/import/resource_importer_texture_settings.h"
41 #include "editor/settings/editor_settings.h"
42 #include "editor/themes/editor_scale.h"
43 #include "scene/resources/image_texture.h"
44
45 #include "modules/modules_enabled.gen.h" // For mono.
46 #include "modules/svg/image_loader_svg.h"
47
48 Error EditorExportPlatformWeb::_extract_template(const String &p_template, const String &p_dir, const String &p_name, bool pwa) {
49 Ref<FileAccess> io_fa;
50 zlib_filefunc_def io = zipio_create_io(&io_fa);
51 unzFile pkg = unzOpen2(p_template.utf8().get_data(), &io);
52
53 if (!pkg) {
54 add_message(EXPORT_MESSAGE_ERROR, TTR("Prepare Templates"), vformat(TTR("Could not open template for export: \"%s\"."), p_template));
55 return ERR_FILE_NOT_FOUND;
56 }
57
58 if (unzGoToFirstFile(pkg) != UNZ_OK) {
59 add_message(EXPORT_MESSAGE_ERROR, TTR("Prepare Templates"), vformat(TTR("Invalid export template: \"%s\"."), p_template));
60 unzClose(pkg);
61 return ERR_FILE_CORRUPT;
62 }
63
64 do {
65 //get filename
66 unz_file_info info;
67 char fname[16384];
68 unzGetCurrentFileInfo(pkg, &info, fname, 16384, nullptr, 0, nullptr, 0);
69
70 String file = String::utf8(fname);
71
72 // Skip folders.
73 if (file.ends_with("/")) {
74 continue;
75 }
76
77 // Skip service worker and offline page if not exporting pwa.
78 if (!pwa && (file == "godot.service.worker.js" || file == "godot.offline.html")) {
79 continue;
80 }
81 Vector<uint8_t> data;
82 data.resize(info.uncompressed_size);
83
84 //read
85 unzOpenCurrentFile(pkg);
86 unzReadCurrentFile(pkg, data.ptrw(), data.size());
87 unzCloseCurrentFile(pkg);
88
89 //write
90 String dst = p_dir.path_join(file.replace("godot", p_name));
91 Ref<FileAccess> f = FileAccess::open(dst, FileAccess::WRITE);
92 if (f.is_null()) {
93 add_message(EXPORT_MESSAGE_ERROR, TTR("Prepare Templates"), vformat(TTR("Could not write file: \"%s\"."), dst));
94 unzClose(pkg);
95 return ERR_FILE_CANT_WRITE;
96 }
97 f->store_buffer(data.ptr(), data.size());
98
99 } while (unzGoToNextFile(pkg) == UNZ_OK);
100 unzClose(pkg);
101 return OK;
102 }
103
104 Error EditorExportPlatformWeb::_write_or_error(const uint8_t *p_content, int p_size, String p_path) {
105 Ref<FileAccess> f = FileAccess::open(p_path, FileAccess::WRITE);
106 if (f.is_null()) {
107 add_message(EXPORT_MESSAGE_ERROR, TTR("Export"), vformat(TTR("Could not write file: \"%s\"."), p_path));
108 return ERR_FILE_CANT_WRITE;
109 }
110 f->store_buffer(p_content, p_size);
111 return OK;
112 }
113
114 void EditorExportPlatformWeb::_replace_strings(const HashMap<String, String> &p_replaces, Vector<uint8_t> &r_template) {
115 String str_template = String::utf8(reinterpret_cast<const char *>(r_template.ptr()), r_template.size());
116 String out;
117 Vector<String> lines = str_template.split("\n");
118 for (int i = 0; i < lines.size(); i++) {
119 String current_line = lines[i];
120 for (const KeyValue<String, String> &E : p_replaces) {
121 current_line = current_line.replace(E.key, E.value);
122 }
123 out += current_line + "\n";
124 }
125 CharString cs = out.utf8();
126 r_template.resize(cs.length());
127 for (int i = 0; i < cs.length(); i++) {
128 r_template.write[i] = cs[i];
129 }
130 }
131
132 void EditorExportPlatformWeb::_fix_html(Vector<uint8_t> &p_html, const Ref<EditorExportPreset> &p_preset, const String &p_name, bool p_debug, BitField<EditorExportPlatform::DebugFlags> p_flags, const Vector<SharedObject> p_shared_objects, const Dictionary &p_file_sizes) {
133 // Engine.js config
134 Dictionary config;
135 Array libs;
136 for (int i = 0; i < p_shared_objects.size(); i++) {
137 libs.push_back(p_shared_objects[i].path.get_file());
138 }
139 Vector<String> flags = gen_export_flags(p_flags & (~DEBUG_FLAG_DUMB_CLIENT));
140 Array args;
141 for (int i = 0; i < flags.size(); i++) {
142 args.push_back(flags[i]);
143 }
144 config["canvasResizePolicy"] = p_preset->get("html/canvas_resize_policy");
145 config["experimentalVK"] = p_preset->get("html/experimental_virtual_keyboard");
146 config["focusCanvas"] = p_preset->get("html/focus_canvas_on_start");
147 config["gdextensionLibs"] = libs;
148 config["executable"] = p_name;
149 config["args"] = args;
150 config["fileSizes"] = p_file_sizes;
151 config["ensureCrossOriginIsolationHeaders"] = (bool)p_preset->get("progressive_web_app/ensure_cross_origin_isolation_headers");
152
153 config["godotPoolSize"] = p_preset->get("threads/godot_pool_size");
154 config["emscriptenPoolSize"] = p_preset->get("threads/emscripten_pool_size");
155
156 String head_include;
157 if (p_preset->get("html/export_icon")) {
158 head_include += "<link id=\"-gd-engine-icon\" rel=\"icon\" type=\"image/png\" href=\"" + p_name + ".icon.png\" />\n";
159 head_include += "<link rel=\"apple-touch-icon\" href=\"" + p_name + ".apple-touch-icon.png\"/>\n";
160 }
161 if (p_preset->get("progressive_web_app/enabled")) {
162 head_include += "<link rel=\"manifest\" href=\"" + p_name + ".manifest.json\">\n";
163 config["serviceWorker"] = p_name + ".service.worker.js";
164 }
165
166 // Replaces HTML string
167 const String str_config = Variant(config).to_json_string();
168 const String custom_head_include = p_preset->get("html/head_include");
169 HashMap<String, String> replaces;
170 replaces["$GODOT_URL"] = p_name + ".js";
171 replaces["$GODOT_PROJECT_NAME"] = get_project_setting(p_preset, "application/config/name");
172 replaces["$GODOT_HEAD_INCLUDE"] = head_include + custom_head_include;
173 replaces["$GODOT_CONFIG"] = str_config;
174 replaces["$GODOT_SPLASH_COLOR"] = "#" + Color(get_project_setting(p_preset, "application/boot_splash/bg_color")).to_html(false);
175
176 Vector<String> godot_splash_classes;
177 godot_splash_classes.push_back("show-image--" + String(get_project_setting(p_preset, "application/boot_splash/show_image")));
178 RenderingServer::SplashStretchMode boot_splash_stretch_mode = get_project_setting(p_preset, "application/boot_splash/stretch_mode");
179 godot_splash_classes.push_back("fullsize--" + String(((boot_splash_stretch_mode != RenderingServer::SplashStretchMode::SPLASH_STRETCH_MODE_DISABLED) ? "true" : "false")));
180 godot_splash_classes.push_back("use-filter--" + String(get_project_setting(p_preset, "application/boot_splash/use_filter")));
181 replaces["$GODOT_SPLASH_CLASSES"] = String(" ").join(godot_splash_classes);
182 replaces["$GODOT_SPLASH"] = p_name + ".png";
183
184 if (p_preset->get("variant/thread_support")) {
185 replaces["$GODOT_THREADS_ENABLED"] = "true";
186 } else {
187 replaces["$GODOT_THREADS_ENABLED"] = "false";
188 }
189
190 _replace_strings(replaces, p_html);
191 }
192
193 Error EditorExportPlatformWeb::_add_manifest_icon(const Ref<EditorExportPreset> &p_preset, const String &p_path, const String &p_icon, int p_size, Array &r_arr) {
194 const String name = p_path.get_file().get_basename();
195 const String icon_name = vformat("%s.%dx%d.png", name, p_size, p_size);
196 const String icon_dest = p_path.get_base_dir().path_join(icon_name);
197
198 Ref<Image> icon;
199 if (!p_icon.is_empty()) {
200 Error err = OK;
201 icon = _load_icon_or_splash_image(p_icon, &err);
202 if (err != OK || icon.is_null() || icon->is_empty()) {
203 add_message(EXPORT_MESSAGE_ERROR, TTR("Icon Creation"), vformat(TTR("Could not read file: \"%s\"."), p_icon));
204 return err;
205 }
206 if (icon->get_width() != p_size || icon->get_height() != p_size) {
207 icon->resize(p_size, p_size);
208 }
209 } else {
210 icon = _get_project_icon(p_preset);
211 icon->resize(p_size, p_size);
212 }
213 const Error err = icon->save_png(icon_dest);
214 if (err != OK) {
215 add_message(EXPORT_MESSAGE_ERROR, TTR("Icon Creation"), vformat(TTR("Could not write file: \"%s\"."), icon_dest));
216 return err;
217 }
218 Dictionary icon_dict;
219 icon_dict["sizes"] = vformat("%dx%d", p_size, p_size);
220 icon_dict["type"] = "image/png";
221 icon_dict["src"] = icon_name;
222 r_arr.push_back(icon_dict);
223 return err;
224 }
225
226 Error EditorExportPlatformWeb::_build_pwa(const Ref<EditorExportPreset> &p_preset, const String p_path, const Vector<SharedObject> &p_shared_objects) {
227 String proj_name = get_project_setting(p_preset, "application/config/name");
228 if (proj_name.is_empty()) {
229 proj_name = "Godot Game";
230 }
231
232 // Service worker
233 const String dir = p_path.get_base_dir();
234 const String name = p_path.get_file().get_basename();
235 bool extensions = (bool)p_preset->get("variant/extensions_support");
236 bool ensure_crossorigin_isolation_headers = (bool)p_preset->get("progressive_web_app/ensure_cross_origin_isolation_headers");
237 HashMap<String, String> replaces;
238 replaces["___GODOT_VERSION___"] = String::num_int64(OS::get_singleton()->get_unix_time()) + "|" + String::num_int64(OS::get_singleton()->get_ticks_usec());
239 replaces["___GODOT_NAME___"] = proj_name.substr(0, 16);
240 replaces["___GODOT_OFFLINE_PAGE___"] = name + ".offline.html";
241 replaces["___GODOT_ENSURE_CROSSORIGIN_ISOLATION_HEADERS___"] = ensure_crossorigin_isolation_headers ? "true" : "false";
242
243 // Files cached during worker install.
244 Array cache_files = {
245 name + ".html",
246 name + ".js",
247 name + ".offline.html"
248 };
249 if (p_preset->get("html/export_icon")) {
250 cache_files.push_back(name + ".icon.png");
251 cache_files.push_back(name + ".apple-touch-icon.png");
252 }
253
254 cache_files.push_back(name + ".audio.worklet.js");
255 cache_files.push_back(name + ".audio.position.worklet.js");
256 replaces["___GODOT_CACHE___"] = Variant(cache_files).to_json_string();
257
258 // Heavy files that are cached on demand.
259 Array opt_cache_files = {
260 name + ".wasm",
261 name + ".pck"
262 };
263 if (extensions) {
264 opt_cache_files.push_back(name + ".side.wasm");
265 for (int i = 0; i < p_shared_objects.size(); i++) {
266 opt_cache_files.push_back(p_shared_objects[i].path.get_file());
267 }
268 }
269 replaces["___GODOT_OPT_CACHE___"] = Variant(opt_cache_files).to_json_string();
270
271 const String sw_path = dir.path_join(name + ".service.worker.js");
272 Vector<uint8_t> sw;
273 {
274 Ref<FileAccess> f = FileAccess::open(sw_path, FileAccess::READ);
275 if (f.is_null()) {
276 add_message(EXPORT_MESSAGE_ERROR, TTR("PWA"), vformat(TTR("Could not read file: \"%s\"."), sw_path));
277 return ERR_FILE_CANT_READ;
278 }
279 sw.resize(f->get_length());
280 f->get_buffer(sw.ptrw(), sw.size());
281 }
282 _replace_strings(replaces, sw);
283 Error err = _write_or_error(sw.ptr(), sw.size(), dir.path_join(name + ".service.worker.js"));
284 if (err != OK) {
285 // Message is supplied by the subroutine method.
286 return err;
287 }
288
289 // Custom offline page
290 const String offline_page = p_preset->get("progressive_web_app/offline_page");
291 if (!offline_page.is_empty()) {
292 Ref<DirAccess> da = DirAccess::create(DirAccess::ACCESS_FILESYSTEM);
293 const String offline_dest = dir.path_join(name + ".offline.html");
294 err = da->copy(ProjectSettings::get_singleton()->globalize_path(offline_page), offline_dest);
295 if (err != OK) {
296 add_message(EXPORT_MESSAGE_ERROR, TTR("PWA"), vformat(TTR("Could not read file: \"%s\"."), offline_dest));
297 return err;
298 }
299 }
300
301 // Manifest
302 const char *modes[4] = { "fullscreen", "standalone", "minimal-ui", "browser" };
303 const char *orientations[3] = { "any", "landscape", "portrait" };
304 const int display = CLAMP(int(p_preset->get("progressive_web_app/display")), 0, 4);
305 const int orientation = CLAMP(int(p_preset->get("progressive_web_app/orientation")), 0, 3);
306
307 Dictionary manifest;
308 manifest["name"] = proj_name;
309 manifest["start_url"] = "./" + name + ".html";
310 manifest["display"] = String::utf8(modes[display]);
311 manifest["orientation"] = String::utf8(orientations[orientation]);
312 manifest["background_color"] = "#" + p_preset->get("progressive_web_app/background_color").operator Color().to_html(false);
313
314 Array icons_arr;
315 const String icon144_path = p_preset->get("progressive_web_app/icon_144x144");
316 err = _add_manifest_icon(p_preset, p_path, icon144_path, 144, icons_arr);
317 if (err != OK) {
318 // Message is supplied by the subroutine method.
319 return err;
320 }
321 const String icon180_path = p_preset->get("progressive_web_app/icon_180x180");
322 err = _add_manifest_icon(p_preset, p_path, icon180_path, 180, icons_arr);
323 if (err != OK) {
324 // Message is supplied by the subroutine method.
325 return err;
326 }
327 const String icon512_path = p_preset->get("progressive_web_app/icon_512x512");
328 err = _add_manifest_icon(p_preset, p_path, icon512_path, 512, icons_arr);
329 if (err != OK) {
330 // Message is supplied by the subroutine method.
331 return err;
332 }
333 manifest["icons"] = icons_arr;
334
335 CharString cs = Variant(manifest).to_json_string().utf8();
336 err = _write_or_error((const uint8_t *)cs.get_data(), cs.length(), dir.path_join(name + ".manifest.json"));
337 if (err != OK) {
338 // Message is supplied by the subroutine method.
339 return err;
340 }
341
342 return OK;
343 }
344
345 void EditorExportPlatformWeb::get_preset_features(const Ref<EditorExportPreset> &p_preset, List<String> *r_features) const {
346 if (p_preset->get("vram_texture_compression/for_desktop")) {
347 r_features->push_back("s3tc");
348 r_features->push_back("bptc");
349 }
350 if (p_preset->get("vram_texture_compression/for_mobile")) {
351 r_features->push_back("etc2");
352 r_features->push_back("astc");
353 }
354 if (p_preset->get("variant/thread_support").operator bool()) {
355 r_features->push_back("threads");
356 } else {
357 r_features->push_back("nothreads");
358 }
359 if (p_preset->get("variant/extensions_support").operator bool()) {
360 r_features->push_back("web_extensions");
361 } else {
362 r_features->push_back("web_noextensions");
363 }
364 r_features->push_back("wasm32");
365 }
366
367 void EditorExportPlatformWeb::get_export_options(List<ExportOption> *r_options) const {
368 r_options->push_back(ExportOption(PropertyInfo(Variant::STRING, "custom_template/debug", PROPERTY_HINT_GLOBAL_FILE, "*.zip"), ""));
369 r_options->push_back(ExportOption(PropertyInfo(Variant::STRING, "custom_template/release", PROPERTY_HINT_GLOBAL_FILE, "*.zip"), ""));
370
371 r_options->push_back(ExportOption(PropertyInfo(Variant::BOOL, "variant/extensions_support"), false)); // GDExtension support.
372 r_options->push_back(ExportOption(PropertyInfo(Variant::BOOL, "variant/thread_support"), false, true)); // Thread support (i.e. run with or without COEP/COOP headers).
373 r_options->push_back(ExportOption(PropertyInfo(Variant::BOOL, "vram_texture_compression/for_desktop"), true)); // S3TC
374 r_options->push_back(ExportOption(PropertyInfo(Variant::BOOL, "vram_texture_compression/for_mobile"), false)); // ETC or ETC2, depending on renderer
375
376 r_options->push_back(ExportOption(PropertyInfo(Variant::BOOL, "html/export_icon"), true));
377 r_options->push_back(ExportOption(PropertyInfo(Variant::STRING, "html/custom_html_shell", PROPERTY_HINT_FILE, "*.html"), ""));
378 r_options->push_back(ExportOption(PropertyInfo(Variant::STRING, "html/head_include", PROPERTY_HINT_MULTILINE_TEXT, "monospace,no_wrap"), ""));
379 r_options->push_back(ExportOption(PropertyInfo(Variant::INT, "html/canvas_resize_policy", PROPERTY_HINT_ENUM, "None,Project,Adaptive"), 2));
380 r_options->push_back(ExportOption(PropertyInfo(Variant::BOOL, "html/focus_canvas_on_start"), true));
381 r_options->push_back(ExportOption(PropertyInfo(Variant::BOOL, "html/experimental_virtual_keyboard"), false));
382 r_options->push_back(ExportOption(PropertyInfo(Variant::BOOL, "progressive_web_app/enabled"), false));
383 r_options->push_back(ExportOption(PropertyInfo(Variant::BOOL, "progressive_web_app/ensure_cross_origin_isolation_headers"), true));
384 r_options->push_back(ExportOption(PropertyInfo(Variant::STRING, "progressive_web_app/offline_page", PROPERTY_HINT_FILE, "*.html"), ""));
385 r_options->push_back(ExportOption(PropertyInfo(Variant::INT, "progressive_web_app/display", PROPERTY_HINT_ENUM, "Fullscreen,Standalone,Minimal UI,Browser"), 1));
386 r_options->push_back(ExportOption(PropertyInfo(Variant::INT, "progressive_web_app/orientation", PROPERTY_HINT_ENUM, "Any,Landscape,Portrait"), 0));
387 r_options->push_back(ExportOption(PropertyInfo(Variant::STRING, "progressive_web_app/icon_144x144", PROPERTY_HINT_FILE, "*.png,*.webp,*.svg"), ""));
388 r_options->push_back(ExportOption(PropertyInfo(Variant::STRING, "progressive_web_app/icon_180x180", PROPERTY_HINT_FILE, "*.png,*.webp,*.svg"), ""));
389 r_options->push_back(ExportOption(PropertyInfo(Variant::STRING, "progressive_web_app/icon_512x512", PROPERTY_HINT_FILE, "*.png,*.webp,*.svg"), ""));
390 r_options->push_back(ExportOption(PropertyInfo(Variant::COLOR, "progressive_web_app/background_color", PROPERTY_HINT_COLOR_NO_ALPHA), Color()));
391
392 r_options->push_back(ExportOption(PropertyInfo(Variant::INT, "threads/emscripten_pool_size"), 8));
393 r_options->push_back(ExportOption(PropertyInfo(Variant::INT, "threads/godot_pool_size"), 4));
394 }
395
396 bool EditorExportPlatformWeb::get_export_option_visibility(const EditorExportPreset *p_preset, const String &p_option) const {
397 bool advanced_options_enabled = p_preset->are_advanced_options_enabled();
398 if (p_option == "custom_template/debug" || p_option == "custom_template/release") {
399 return advanced_options_enabled;
400 }
401
402 if (p_option == "threads/godot_pool_size" || p_option == "threads/emscripten_pool_size") {
403 return p_preset->get("variant/thread_support").operator bool();
404 }
405
406 return true;
407 }
408
409 String EditorExportPlatformWeb::get_name() const {
410 return "Web";
411 }
412
413 String EditorExportPlatformWeb::get_os_name() const {
414 return "Web";
415 }
416
417 Ref<Texture2D> EditorExportPlatformWeb::get_logo() const {
418 return logo;
419 }
420
421 bool EditorExportPlatformWeb::has_valid_export_configuration(const Ref<EditorExportPreset> &p_preset, String &r_error, bool &r_missing_templates, bool p_debug) const {
422 #ifdef MODULE_MONO_ENABLED
423 // Don't check for additional errors, as this particular error cannot be resolved.
424 r_error += TTR("Exporting to Web is currently not supported in Godot 4 when using C#/.NET. Use Godot 3 to target Web with C#/Mono instead.") + "\n";
425 r_error += TTR("If this project does not use C#, use a non-C# editor build to export the project.") + "\n";
426 return false;
427 #else
428
429 String err;
430 bool valid = false;
431 bool extensions = (bool)p_preset->get("variant/extensions_support");
432 bool thread_support = (bool)p_preset->get("variant/thread_support");
433
434 // Look for export templates (first official, and if defined custom templates).
435 bool dvalid = exists_export_template(_get_template_name(extensions, thread_support, true), &err);
436 bool rvalid = exists_export_template(_get_template_name(extensions, thread_support, false), &err);
437
438 if (p_preset->get("custom_template/debug") != "") {
439 dvalid = FileAccess::exists(p_preset->get("custom_template/debug"));
440 if (!dvalid) {
441 err += TTR("Custom debug template not found.") + "\n";
442 }
443 }
444 if (p_preset->get("custom_template/release") != "") {
445 rvalid = FileAccess::exists(p_preset->get("custom_template/release"));
446 if (!rvalid) {
447 err += TTR("Custom release template not found.") + "\n";
448 }
449 }
450
451 valid = dvalid || rvalid;
452 r_missing_templates = !valid;
453
454 if (!err.is_empty()) {
455 r_error = err;
456 }
457
458 return valid;
459 #endif // !MODULE_MONO_ENABLED
460 }
461
462 bool EditorExportPlatformWeb::has_valid_project_configuration(const Ref<EditorExportPreset> &p_preset, String &r_error) const {
463 String err;
464 bool valid = true;
465
466 // Validate the project configuration.
467
468 if (p_preset->get("vram_texture_compression/for_mobile")) {
469 if (!ResourceImporterTextureSettings::should_import_etc2_astc()) {
470 valid = false;
471 }
472 }
473
474 if (!err.is_empty()) {
475 r_error = err;
476 }
477
478 return valid;
479 }
480
481 List<String> EditorExportPlatformWeb::get_binary_extensions(const Ref<EditorExportPreset> &p_preset) const {
482 List<String> list;
483 list.push_back("html");
484 return list;
485 }
486
487 Error EditorExportPlatformWeb::export_project(const Ref<EditorExportPreset> &p_preset, bool p_debug, const String &p_path, BitField<EditorExportPlatform::DebugFlags> p_flags) {
488 ExportNotifier notifier(*this, p_preset, p_debug, p_path, p_flags);
489
490 const String custom_debug = p_preset->get("custom_template/debug");
491 const String custom_release = p_preset->get("custom_template/release");
492 const String custom_html = p_preset->get("html/custom_html_shell");
493 const bool export_icon = p_preset->get("html/export_icon");
494 const bool pwa = p_preset->get("progressive_web_app/enabled");
495
496 const String base_dir = p_path.get_base_dir();
497 const String base_path = p_path.get_basename();
498 const String base_name = p_path.get_file().get_basename();
499
500 if (!DirAccess::exists(base_dir)) {
501 add_message(EXPORT_MESSAGE_ERROR, TTR("Export"), vformat(TTR("Target folder does not exist or is inaccessible: \"%s\""), base_dir));
502 return ERR_FILE_BAD_PATH;
503 }
504
505 // Find the correct template
506 String template_path = p_debug ? custom_debug : custom_release;
507 template_path = template_path.strip_edges();
508 if (template_path.is_empty()) {
509 bool extensions = (bool)p_preset->get("variant/extensions_support");
510 bool thread_support = (bool)p_preset->get("variant/thread_support");
511 template_path = find_export_template(_get_template_name(extensions, thread_support, p_debug));
512 }
513
514 if (!template_path.is_empty() && !FileAccess::exists(template_path)) {
515 add_message(EXPORT_MESSAGE_ERROR, TTR("Prepare Templates"), vformat(TTR("Template file not found: \"%s\"."), template_path));
516 return ERR_FILE_NOT_FOUND;
517 }
518
519 // Export pck and shared objects
520 Vector<SharedObject> shared_objects;
521 String pck_path = base_path + ".pck";
522 Error error = save_pack(p_preset, p_debug, pck_path, &shared_objects);
523 if (error != OK) {
524 add_message(EXPORT_MESSAGE_ERROR, TTR("Export"), vformat(TTR("Could not write file: \"%s\"."), pck_path));
525 return error;
526 }
527
528 {
529 Ref<DirAccess> da = DirAccess::create(DirAccess::ACCESS_FILESYSTEM);
530 for (int i = 0; i < shared_objects.size(); i++) {
531 String dst = base_dir.path_join(shared_objects[i].path.get_file());
532 error = da->copy(shared_objects[i].path, dst);
533 if (error != OK) {
534 add_message(EXPORT_MESSAGE_ERROR, TTR("Export"), vformat(TTR("Could not write file: \"%s\"."), shared_objects[i].path.get_file()));
535 return error;
536 }
537 }
538 }
539
540 // Extract templates.
541 error = _extract_template(template_path, base_dir, base_name, pwa);
542 if (error) {
543 // Message is supplied by the subroutine method.
544 return error;
545 }
546
547 // Parse generated file sizes (pck and wasm, to help show a meaningful loading bar).
548 Dictionary file_sizes;
549 Ref<FileAccess> f = FileAccess::open(pck_path, FileAccess::READ);
550 if (f.is_valid()) {
551 file_sizes[pck_path.get_file()] = (uint64_t)f->get_length();
552 }
553 f = FileAccess::open(base_path + ".wasm", FileAccess::READ);
554 if (f.is_valid()) {
555 file_sizes[base_name + ".wasm"] = (uint64_t)f->get_length();
556 }
557
558 // Read the HTML shell file (custom or from template).
559 const String html_path = custom_html.is_empty() ? base_path + ".html" : custom_html;
560 Vector<uint8_t> html;
561 f = FileAccess::open(html_path, FileAccess::READ);
562 if (f.is_null()) {
563 add_message(EXPORT_MESSAGE_ERROR, TTR("Export"), vformat(TTR("Could not read HTML shell: \"%s\"."), html_path));
564 return ERR_FILE_CANT_READ;
565 }
566 html.resize(f->get_length());
567 f->get_buffer(html.ptrw(), html.size());
568 f.unref(); // close file.
569
570 // Generate HTML file with replaced strings.
571 _fix_html(html, p_preset, base_name, p_debug, p_flags, shared_objects, file_sizes);
572 Error err = _write_or_error(html.ptr(), html.size(), p_path);
573 if (err != OK) {
574 // Message is supplied by the subroutine method.
575 return err;
576 }
577 html.resize(0);
578
579 // Export splash (why?)
580 Ref<Image> splash = _get_project_splash(p_preset);
581 const String splash_png_path = base_path + ".png";
582 if (splash->save_png(splash_png_path) != OK) {
583 add_message(EXPORT_MESSAGE_ERROR, TTR("Export"), vformat(TTR("Could not write file: \"%s\"."), splash_png_path));
584 return ERR_FILE_CANT_WRITE;
585 }
586
587 // Save a favicon that can be accessed without waiting for the project to finish loading.
588 // This way, the favicon can be displayed immediately when loading the page.
589 if (export_icon) {
590 Ref<Image> favicon = _get_project_icon(p_preset);
591 const String favicon_png_path = base_path + ".icon.png";
592 if (favicon->save_png(favicon_png_path) != OK) {
593 add_message(EXPORT_MESSAGE_ERROR, TTR("Export"), vformat(TTR("Could not write file: \"%s\"."), favicon_png_path));
594 return ERR_FILE_CANT_WRITE;
595 }
596 favicon->resize(180, 180);
597 const String apple_icon_png_path = base_path + ".apple-touch-icon.png";
598 if (favicon->save_png(apple_icon_png_path) != OK) {
599 add_message(EXPORT_MESSAGE_ERROR, TTR("Export"), vformat(TTR("Could not write file: \"%s\"."), apple_icon_png_path));
600 return ERR_FILE_CANT_WRITE;
601 }
602 }
603
604 // Generate the PWA worker and manifest
605 if (pwa) {
606 err = _build_pwa(p_preset, p_path, shared_objects);
607 if (err != OK) {
608 // Message is supplied by the subroutine method.
609 return err;
610 }
611 }
612
613 return OK;
614 }
615
616 bool EditorExportPlatformWeb::poll_export() {
617 Ref<EditorExportPreset> preset;
618
619 for (int i = 0; i < EditorExport::get_singleton()->get_export_preset_count(); i++) {
620 Ref<EditorExportPreset> ep = EditorExport::get_singleton()->get_export_preset(i);
621 if (ep->is_runnable() && ep->get_platform() == this) {
622 preset = ep;
623 break;
624 }
625 }
626
627 RemoteDebugState prev_remote_debug_state = remote_debug_state;
628 remote_debug_state = REMOTE_DEBUG_STATE_UNAVAILABLE;
629
630 if (preset.is_valid()) {
631 const bool debug = true;
632 // Throwaway variables to pass to `can_export`.
633 String err;
634 bool missing_templates;
635
636 if (can_export(preset, err, missing_templates, debug)) {
637 if (server->is_listening()) {
638 remote_debug_state = REMOTE_DEBUG_STATE_SERVING;
639 } else {
640 remote_debug_state = REMOTE_DEBUG_STATE_AVAILABLE;
641 }
642 }
643 }
644
645 if (remote_debug_state != REMOTE_DEBUG_STATE_SERVING && server->is_listening()) {
646 server->stop();
647 }
648
649 return remote_debug_state != prev_remote_debug_state;
650 }
651
652 Ref<Texture2D> EditorExportPlatformWeb::get_option_icon(int p_index) const {
653 Ref<Texture2D> play_icon = EditorExportPlatform::get_option_icon(p_index);
654
655 switch (remote_debug_state) {
656 case REMOTE_DEBUG_STATE_UNAVAILABLE: {
657 return nullptr;
658 } break;
659
660 case REMOTE_DEBUG_STATE_AVAILABLE: {
661 switch (p_index) {
662 case 0:
663 case 1:
664 return play_icon;
665 default:
666 ERR_FAIL_V(nullptr);
667 }
668 } break;
669
670 case REMOTE_DEBUG_STATE_SERVING: {
671 switch (p_index) {
672 case 0:
673 return play_icon;
674 case 1:
675 return restart_icon;
676 case 2:
677 return stop_icon;
678 default:
679 ERR_FAIL_V(nullptr);
680 }
681 } break;
682 }
683
684 return nullptr;
685 }
686
687 int EditorExportPlatformWeb::get_options_count() const {
688 switch (remote_debug_state) {
689 case REMOTE_DEBUG_STATE_UNAVAILABLE: {
690 return 0;
691 } break;
692
693 case REMOTE_DEBUG_STATE_AVAILABLE: {
694 return 2;
695 } break;
696
697 case REMOTE_DEBUG_STATE_SERVING: {
698 return 3;
699 } break;
700 }
701
702 return 0;
703 }
704
705 String EditorExportPlatformWeb::get_option_label(int p_index) const {
706 String run_in_browser = TTR("Run in Browser");
707 String start_http_server = TTR("Start HTTP Server");
708 String reexport_project = TTR("Re-export Project");
709 String stop_http_server = TTR("Stop HTTP Server");
710
711 switch (remote_debug_state) {
712 case REMOTE_DEBUG_STATE_UNAVAILABLE:
713 return "";
714
715 case REMOTE_DEBUG_STATE_AVAILABLE: {
716 switch (p_index) {
717 case 0:
718 return run_in_browser;
719 case 1:
720 return start_http_server;
721 default:
722 ERR_FAIL_V("");
723 }
724 } break;
725
726 case REMOTE_DEBUG_STATE_SERVING: {
727 switch (p_index) {
728 case 0:
729 return run_in_browser;
730 case 1:
731 return reexport_project;
732 case 2:
733 return stop_http_server;
734 default:
735 ERR_FAIL_V("");
736 }
737 } break;
738 }
739
740 return "";
741 }
742
743 String EditorExportPlatformWeb::get_option_tooltip(int p_index) const {
744 String run_in_browser = TTR("Run exported HTML in the system's default browser.");
745 String start_http_server = TTR("Start the HTTP server.");
746 String reexport_project = TTR("Export project again to account for updates.");
747 String stop_http_server = TTR("Stop the HTTP server.");
748
749 switch (remote_debug_state) {
750 case REMOTE_DEBUG_STATE_UNAVAILABLE:
751 return "";
752
753 case REMOTE_DEBUG_STATE_AVAILABLE: {
754 switch (p_index) {
755 case 0:
756 return run_in_browser;
757 case 1:
758 return start_http_server;
759 default:
760 ERR_FAIL_V("");
761 }
762 } break;
763
764 case REMOTE_DEBUG_STATE_SERVING: {
765 switch (p_index) {
766 case 0:
767 return run_in_browser;
768 case 1:
769 return reexport_project;
770 case 2:
771 return stop_http_server;
772 default:
773 ERR_FAIL_V("");
774 }
775 } break;
776 }
777
778 return "";
779 }
780
781 Error EditorExportPlatformWeb::run(const Ref<EditorExportPreset> &p_preset, int p_option, BitField<EditorExportPlatform::DebugFlags> p_debug_flags) {
782 const uint16_t bind_port = EDITOR_GET("export/web/http_port");
783 // Resolve host if needed.
784 const String bind_host = EDITOR_GET("export/web/http_host");
785 const bool use_tls = EDITOR_GET("export/web/use_tls");
786
787 switch (remote_debug_state) {
788 case REMOTE_DEBUG_STATE_UNAVAILABLE: {
789 return FAILED;
790 } break;
791
792 case REMOTE_DEBUG_STATE_AVAILABLE: {
793 switch (p_option) {
794 // Run in Browser.
795 case 0: {
796 Error err = _export_project(p_preset, p_debug_flags);
797 if (err != OK) {
798 return err;
799 }
800 err = _start_server(bind_host, bind_port, use_tls);
801 if (err != OK) {
802 return err;
803 }
804 return _launch_browser(bind_host, bind_port, use_tls);
805 } break;
806
807 // Start HTTP Server.
808 case 1: {
809 Error err = _export_project(p_preset, p_debug_flags);
810 if (err != OK) {
811 return err;
812 }
813 return _start_server(bind_host, bind_port, use_tls);
814 } break;
815
816 default: {
817 ERR_FAIL_V_MSG(FAILED, vformat(R"(Invalid option "%s" for the current state.)", p_option));
818 }
819 }
820 } break;
821
822 case REMOTE_DEBUG_STATE_SERVING: {
823 switch (p_option) {
824 // Run in Browser.
825 case 0: {
826 Error err = _export_project(p_preset, p_debug_flags);
827 if (err != OK) {
828 return err;
829 }
830 return _launch_browser(bind_host, bind_port, use_tls);
831 } break;
832
833 // Re-export Project.
834 case 1: {
835 return _export_project(p_preset, p_debug_flags);
836 } break;
837
838 // Stop HTTP Server.
839 case 2: {
840 return _stop_server();
841 } break;
842
843 default: {
844 ERR_FAIL_V_MSG(FAILED, vformat(R"(Invalid option "%s" for the current state.)", p_option));
845 }
846 }
847 } break;
848 }
849
850 return FAILED;
851 }
852
853 Error EditorExportPlatformWeb::_export_project(const Ref<EditorExportPreset> &p_preset, int p_debug_flags) {
854 const String dest = EditorPaths::get_singleton()->get_temp_dir().path_join("web");
855 Ref<DirAccess> da = DirAccess::create(DirAccess::ACCESS_FILESYSTEM);
856 if (!da->dir_exists(dest)) {
857 Error err = da->make_dir_recursive(dest);
858 if (err != OK) {
859 add_message(EXPORT_MESSAGE_ERROR, TTR("Run"), vformat(TTR("Could not create HTTP server directory: %s."), dest));
860 return err;
861 }
862 }
863
864 const String basepath = dest.path_join("tmp_js_export");
865 Error err = export_project(p_preset, true, basepath + ".html", p_debug_flags);
866 if (err != OK) {
867 // Export generates several files, clean them up on failure.
868 DirAccess::remove_file_or_error(basepath + ".html");
869 DirAccess::remove_file_or_error(basepath + ".offline.html");
870 DirAccess::remove_file_or_error(basepath + ".js");
871 DirAccess::remove_file_or_error(basepath + ".audio.worklet.js");
872 DirAccess::remove_file_or_error(basepath + ".audio.position.worklet.js");
873 DirAccess::remove_file_or_error(basepath + ".service.worker.js");
874 DirAccess::remove_file_or_error(basepath + ".pck");
875 DirAccess::remove_file_or_error(basepath + ".png");
876 DirAccess::remove_file_or_error(basepath + ".side.wasm");
877 DirAccess::remove_file_or_error(basepath + ".wasm");
878 DirAccess::remove_file_or_error(basepath + ".icon.png");
879 DirAccess::remove_file_or_error(basepath + ".apple-touch-icon.png");
880 }
881 return err;
882 }
883
884 Error EditorExportPlatformWeb::_launch_browser(const String &p_bind_host, const uint16_t p_bind_port, const bool p_use_tls) {
885 OS::get_singleton()->shell_open(String((p_use_tls ? "https://" : "http://") + p_bind_host + ":" + itos(p_bind_port) + "/tmp_js_export.html"));
886 // FIXME: Find out how to clean up export files after running the successfully
887 // exported game. Might not be trivial.
888 return OK;
889 }
890
891 Error EditorExportPlatformWeb::_start_server(const String &p_bind_host, const uint16_t p_bind_port, const bool p_use_tls) {
892 IPAddress bind_ip;
893 if (p_bind_host.is_valid_ip_address()) {
894 bind_ip = p_bind_host;
895 } else {
896 bind_ip = IP::get_singleton()->resolve_hostname(p_bind_host);
897 }
898 ERR_FAIL_COND_V_MSG(!bind_ip.is_valid(), ERR_INVALID_PARAMETER, "Invalid editor setting 'export/web/http_host': '" + p_bind_host + "'. Try using '127.0.0.1'.");
899
900 const String tls_key = EDITOR_GET("export/web/tls_key");
901 const String tls_cert = EDITOR_GET("export/web/tls_certificate");
902
903 // Restart server.
904 server->stop();
905 Error err = server->listen(p_bind_port, bind_ip, p_use_tls, tls_key, tls_cert);
906 if (err != OK) {
907 add_message(EXPORT_MESSAGE_ERROR, TTR("Run"), vformat(TTR("Error starting HTTP server: %d."), err));
908 }
909 return err;
910 }
911
912 Error EditorExportPlatformWeb::_stop_server() {
913 server->stop();
914 return OK;
915 }
916
917 Ref<Texture2D> EditorExportPlatformWeb::get_run_icon() const {
918 return run_icon;
919 }
920
921 void EditorExportPlatformWeb::initialize() {
922 if (EditorNode::get_singleton()) {
923 server.instantiate();
924
925 Ref<Image> img = memnew(Image);
926 const bool upsample = !Math::is_equal_approx(Math::round(EDSCALE), EDSCALE);
927
928 ImageLoaderSVG::create_image_from_string(img, _web_logo_svg, EDSCALE, upsample, false);
929 logo = ImageTexture::create_from_image(img);
930
931 ImageLoaderSVG::create_image_from_string(img, _web_run_icon_svg, EDSCALE, upsample, false);
932 run_icon = ImageTexture::create_from_image(img);
933
934 Ref<Theme> theme = EditorNode::get_singleton()->get_editor_theme();
935 if (theme.is_valid()) {
936 stop_icon = theme->get_icon(SNAME("Stop"), EditorStringName(EditorIcons));
937 restart_icon = theme->get_icon(SNAME("Reload"), EditorStringName(EditorIcons));
938 } else {
939 stop_icon.instantiate();
940 restart_icon.instantiate();
941 }
942 }
943 }
944
945 EditorExportPlatformWeb::~EditorExportPlatformWeb() {
946 }