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

Export and reference the icon as favicon when exporting to HTML5
This commit is contained in:
Fabio Alessandrelli 2020-02-08 04:45:53 +01:00 committed by GitHub
commit 343b29a651
3 changed files with 42 additions and 15 deletions

View File

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

View File

@ -3,6 +3,7 @@
<head> <head>
<meta charset='utf-8' /> <meta charset='utf-8' />
<meta name='viewport' content='width=device-width, user-scalable=no' /> <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> <title></title>
<style type='text/css'> <style type='text/css'>

View File

@ -86,7 +86,7 @@ public:
ERR_FAIL_COND_MSG(req[0] != "GET" || req[2] != "HTTP/1.1", "Invalid method or HTTP version."); 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 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 = ""; String ctype = "";
if (req[1] == basereq + ".html") { if (req[1] == basereq + ".html") {
filepath += ".html"; filepath += ".html";
@ -97,8 +97,13 @@ public:
} else if (req[1] == basereq + ".pck") { } else if (req[1] == basereq + ".pck") {
filepath += ".pck"; filepath += ".pck";
ctype = "application/octet-stream"; ctype = "application/octet-stream";
} else if (req[1] == basereq + ".png") { } else if (req[1] == basereq + ".png" || req[1] == "/favicon.png") {
filepath += ".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"; ctype = "image/png";
} else if (req[1] == basereq + ".wasm") { } else if (req[1] == basereq + ".wasm") {
filepath += ".wasm"; filepath += ".wasm";
@ -470,11 +475,10 @@ Error EditorExportPlatformJavaScript::export_project(const Ref<EditorExportPrese
} }
Ref<Image> splash; Ref<Image> splash;
String splash_path = GLOBAL_GET("application/boot_splash/image"); const String splash_path = String(GLOBAL_GET("application/boot_splash/image")).strip_edges();
splash_path = splash_path.strip_edges();
if (!splash_path.empty()) { if (!splash_path.empty()) {
splash.instance(); splash.instance();
Error err = splash->load(splash_path); const Error err = splash->load(splash_path);
if (err) { 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.")); 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(); splash.unref();
@ -483,11 +487,32 @@ Error EditorExportPlatformJavaScript::export_project(const Ref<EditorExportPrese
if (splash.is_null()) { if (splash.is_null()) {
splash = Ref<Image>(memnew(Image(boot_splash_png))); 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"); const String splash_png_path = p_path.get_base_dir().plus_file(p_path.get_file().get_basename() + ".png");
if (splash->save_png(png_path) != OK) { if (splash->save_png(splash_png_path) != OK) {
EditorNode::get_singleton()->show_warning(TTR("Could not write file:") + "\n" + png_path); EditorNode::get_singleton()->show_warning(TTR("Could not write file:") + "\n" + splash_png_path);
return ERR_FILE_CANT_WRITE; 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; return OK;
} }
@ -536,9 +561,8 @@ Error EditorExportPlatformJavaScript::run(const Ref<EditorExportPreset> &p_prese
return OK; return OK;
} }
String basepath = EditorSettings::get_singleton()->get_cache_dir().plus_file("tmp_js_export"); const String basepath = EditorSettings::get_singleton()->get_cache_dir().plus_file("tmp_js_export");
String path = basepath + ".html"; Error err = export_project(p_preset, true, basepath + ".html", p_debug_flags);
Error err = export_project(p_preset, true, path, p_debug_flags);
if (err != OK) { if (err != OK) {
// Export generates several files, clean them up on failure. // Export generates several files, clean them up on failure.
DirAccess::remove_file_or_error(basepath + ".html"); 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 + ".pck");
DirAccess::remove_file_or_error(basepath + ".png"); DirAccess::remove_file_or_error(basepath + ".png");
DirAccess::remove_file_or_error(basepath + ".wasm"); DirAccess::remove_file_or_error(basepath + ".wasm");
DirAccess::remove_file_or_error(EditorSettings::get_singleton()->get_cache_dir().plus_file("favicon.png"));
return err; return err;
} }
IP_Address bind_ip; const uint16_t bind_port = EDITOR_GET("export/web/http_port");
uint16_t bind_port = EDITOR_GET("export/web/http_port");
// Resolve host if needed. // 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()) { if (bind_host.is_valid_ip_address()) {
bind_ip = bind_host; bind_ip = bind_host;
} else { } else {