Browse Source

Merge pull request #35381 from Calinou/html5-export-favicon

Export and reference the icon as favicon when exporting to HTML5
Fabio Alessandrelli 5 years ago
parent
commit
343b29a651

+ 1 - 0
misc/dist/html/fixed-size.html

@@ -2,6 +2,7 @@
 <html xmlns="http://www.w3.org/1999/xhtml" lang="" xml:lang="">
 <head>
 	<meta charset="utf-8" />
+	<link id='-gd-engine-icon' rel='icon' type='image/png' href='favicon.png' />
 	<title></title>
 	<style type="text/css">
 

+ 1 - 0
misc/dist/html/full-size.html

@@ -3,6 +3,7 @@
 <head>
 	<meta charset='utf-8' />
 	<meta name='viewport' content='width=device-width, user-scalable=no' />
+	<link id='-gd-engine-icon' rel='icon' type='image/png' href='favicon.png' />
 	<title></title>
 	<style type='text/css'>
 

+ 40 - 15
platform/javascript/export/export.cpp

@@ -86,7 +86,7 @@ public:
 		ERR_FAIL_COND_MSG(req[0] != "GET" || req[2] != "HTTP/1.1", "Invalid method or HTTP version.");
 
 		String filepath = EditorSettings::get_singleton()->get_cache_dir().plus_file("tmp_js_export");
-		String basereq = "/tmp_js_export";
+		const String basereq = "/tmp_js_export";
 		String ctype = "";
 		if (req[1] == basereq + ".html") {
 			filepath += ".html";
@@ -97,8 +97,13 @@ public:
 		} else if (req[1] == basereq + ".pck") {
 			filepath += ".pck";
 			ctype = "application/octet-stream";
-		} else if (req[1] == basereq + ".png") {
-			filepath += ".png";
+		} else if (req[1] == basereq + ".png" || req[1] == "/favicon.png") {
+			// Also allow serving the generated favicon for a smoother loading experience.
+			if (req[1] == "/favicon.png") {
+				filepath = EditorSettings::get_singleton()->get_cache_dir().plus_file("favicon.png");
+			} else {
+				filepath += ".png";
+			}
 			ctype = "image/png";
 		} else if (req[1] == basereq + ".wasm") {
 			filepath += ".wasm";
@@ -470,11 +475,10 @@ Error EditorExportPlatformJavaScript::export_project(const Ref<EditorExportPrese
 	}
 
 	Ref<Image> splash;
-	String splash_path = GLOBAL_GET("application/boot_splash/image");
-	splash_path = splash_path.strip_edges();
+	const String splash_path = String(GLOBAL_GET("application/boot_splash/image")).strip_edges();
 	if (!splash_path.empty()) {
 		splash.instance();
-		Error err = splash->load(splash_path);
+		const Error err = splash->load(splash_path);
 		if (err) {
 			EditorNode::get_singleton()->show_warning(TTR("Could not read boot splash image file:") + "\n" + splash_path + "\n" + TTR("Using default boot splash image."));
 			splash.unref();
@@ -483,11 +487,32 @@ Error EditorExportPlatformJavaScript::export_project(const Ref<EditorExportPrese
 	if (splash.is_null()) {
 		splash = Ref<Image>(memnew(Image(boot_splash_png)));
 	}
-	String png_path = p_path.get_base_dir().plus_file(p_path.get_file().get_basename() + ".png");
-	if (splash->save_png(png_path) != OK) {
-		EditorNode::get_singleton()->show_warning(TTR("Could not write file:") + "\n" + png_path);
+	const String splash_png_path = p_path.get_base_dir().plus_file(p_path.get_file().get_basename() + ".png");
+	if (splash->save_png(splash_png_path) != OK) {
+		EditorNode::get_singleton()->show_warning(TTR("Could not write file:") + "\n" + splash_png_path);
 		return ERR_FILE_CANT_WRITE;
 	}
+
+	// Save a favicon that can be accessed without waiting for the project to finish loading.
+	// This way, the favicon can be displayed immediately when loading the page.
+	Ref<Image> favicon;
+	const String favicon_path = String(GLOBAL_GET("application/config/icon")).strip_edges();
+	if (!favicon_path.empty()) {
+		favicon.instance();
+		const Error err = favicon->load(favicon_path);
+		if (err) {
+			favicon.unref();
+		}
+	}
+
+	if (favicon.is_valid()) {
+		const String favicon_png_path = p_path.get_base_dir().plus_file("favicon.png");
+		if (favicon->save_png(favicon_png_path) != OK) {
+			EditorNode::get_singleton()->show_warning(TTR("Could not write file:") + "\n" + favicon_png_path);
+			return ERR_FILE_CANT_WRITE;
+		}
+	}
+
 	return OK;
 }
 
@@ -536,9 +561,8 @@ Error EditorExportPlatformJavaScript::run(const Ref<EditorExportPreset> &p_prese
 		return OK;
 	}
 
-	String basepath = EditorSettings::get_singleton()->get_cache_dir().plus_file("tmp_js_export");
-	String path = basepath + ".html";
-	Error err = export_project(p_preset, true, path, p_debug_flags);
+	const String basepath = EditorSettings::get_singleton()->get_cache_dir().plus_file("tmp_js_export");
+	Error err = export_project(p_preset, true, basepath + ".html", p_debug_flags);
 	if (err != OK) {
 		// Export generates several files, clean them up on failure.
 		DirAccess::remove_file_or_error(basepath + ".html");
@@ -546,13 +570,14 @@ Error EditorExportPlatformJavaScript::run(const Ref<EditorExportPreset> &p_prese
 		DirAccess::remove_file_or_error(basepath + ".pck");
 		DirAccess::remove_file_or_error(basepath + ".png");
 		DirAccess::remove_file_or_error(basepath + ".wasm");
+		DirAccess::remove_file_or_error(EditorSettings::get_singleton()->get_cache_dir().plus_file("favicon.png"));
 		return err;
 	}
 
-	IP_Address bind_ip;
-	uint16_t bind_port = EDITOR_GET("export/web/http_port");
+	const uint16_t bind_port = EDITOR_GET("export/web/http_port");
 	// Resolve host if needed.
-	String bind_host = EDITOR_GET("export/web/http_host");
+	const String bind_host = EDITOR_GET("export/web/http_host");
+	IP_Address bind_ip;
 	if (bind_host.is_valid_ip_address()) {
 		bind_ip = bind_host;
 	} else {