initial commit, 4.5 stable
Some checks failed
🔗 GHA / 📊 Static checks (push) Has been cancelled
🔗 GHA / 🤖 Android (push) Has been cancelled
🔗 GHA / 🍏 iOS (push) Has been cancelled
🔗 GHA / 🐧 Linux (push) Has been cancelled
🔗 GHA / 🍎 macOS (push) Has been cancelled
🔗 GHA / 🏁 Windows (push) Has been cancelled
🔗 GHA / 🌐 Web (push) Has been cancelled
Some checks failed
🔗 GHA / 📊 Static checks (push) Has been cancelled
🔗 GHA / 🤖 Android (push) Has been cancelled
🔗 GHA / 🍏 iOS (push) Has been cancelled
🔗 GHA / 🐧 Linux (push) Has been cancelled
🔗 GHA / 🍎 macOS (push) Has been cancelled
🔗 GHA / 🏁 Windows (push) Has been cancelled
🔗 GHA / 🌐 Web (push) Has been cancelled
This commit is contained in:
255
platform/web/export/editor_http_server.cpp
Normal file
255
platform/web/export/editor_http_server.cpp
Normal file
@@ -0,0 +1,255 @@
|
||||
/**************************************************************************/
|
||||
/* editor_http_server.cpp */
|
||||
/**************************************************************************/
|
||||
/* This file is part of: */
|
||||
/* GODOT ENGINE */
|
||||
/* https://godotengine.org */
|
||||
/**************************************************************************/
|
||||
/* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
|
||||
/* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
|
||||
/* */
|
||||
/* 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_http_server.h"
|
||||
|
||||
void EditorHTTPServer::_server_thread_poll(void *data) {
|
||||
EditorHTTPServer *web_server = static_cast<EditorHTTPServer *>(data);
|
||||
while (!web_server->server_quit.is_set()) {
|
||||
OS::get_singleton()->delay_usec(6900);
|
||||
{
|
||||
MutexLock lock(web_server->server_lock);
|
||||
web_server->_poll();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void EditorHTTPServer::_clear_client() {
|
||||
peer = Ref<StreamPeer>();
|
||||
tls = Ref<StreamPeerTLS>();
|
||||
tcp = Ref<StreamPeerTCP>();
|
||||
memset(req_buf, 0, sizeof(req_buf));
|
||||
time = 0;
|
||||
req_pos = 0;
|
||||
}
|
||||
|
||||
void EditorHTTPServer::_set_internal_certs(Ref<Crypto> p_crypto) {
|
||||
const String cache_path = EditorPaths::get_singleton()->get_cache_dir();
|
||||
const String key_path = cache_path.path_join("html5_server.key");
|
||||
const String crt_path = cache_path.path_join("html5_server.crt");
|
||||
bool regen = !FileAccess::exists(key_path) || !FileAccess::exists(crt_path);
|
||||
if (!regen) {
|
||||
key = Ref<CryptoKey>(CryptoKey::create());
|
||||
cert = Ref<X509Certificate>(X509Certificate::create());
|
||||
if (key->load(key_path) != OK || cert->load(crt_path) != OK) {
|
||||
regen = true;
|
||||
}
|
||||
}
|
||||
if (regen) {
|
||||
key = p_crypto->generate_rsa(2048);
|
||||
key->save(key_path);
|
||||
cert = p_crypto->generate_self_signed_certificate(key, "CN=godot-debug.local,O=A Game Dev,C=XXA", "20140101000000", "20340101000000");
|
||||
cert->save(crt_path);
|
||||
}
|
||||
}
|
||||
|
||||
void EditorHTTPServer::_send_response() {
|
||||
Vector<String> psa = String((char *)req_buf).split("\r\n");
|
||||
int len = psa.size();
|
||||
ERR_FAIL_COND_MSG(len < 4, "Not enough response headers, got: " + itos(len) + ", expected >= 4.");
|
||||
|
||||
Vector<String> req = psa[0].split(" ", false);
|
||||
ERR_FAIL_COND_MSG(req.size() < 2, "Invalid protocol or status code.");
|
||||
|
||||
// Wrong protocol
|
||||
ERR_FAIL_COND_MSG(req[0] != "GET" || req[2] != "HTTP/1.1", "Invalid method or HTTP version.");
|
||||
|
||||
const int query_index = req[1].find_char('?');
|
||||
const String path = (query_index == -1) ? req[1] : req[1].substr(0, query_index);
|
||||
|
||||
const String req_file = path.get_file();
|
||||
const String req_ext = path.get_extension();
|
||||
const String cache_path = EditorPaths::get_singleton()->get_temp_dir().path_join("web");
|
||||
const String filepath = cache_path.path_join(req_file);
|
||||
|
||||
if (!mimes.has(req_ext) || !FileAccess::exists(filepath)) {
|
||||
String s = "HTTP/1.1 404 Not Found\r\n";
|
||||
s += "Connection: Close\r\n";
|
||||
s += "\r\n";
|
||||
CharString cs = s.utf8();
|
||||
peer->put_data((const uint8_t *)cs.get_data(), cs.size() - 1);
|
||||
return;
|
||||
}
|
||||
const String ctype = mimes[req_ext];
|
||||
|
||||
Ref<FileAccess> f = FileAccess::open(filepath, FileAccess::READ);
|
||||
ERR_FAIL_COND(f.is_null());
|
||||
String s = "HTTP/1.1 200 OK\r\n";
|
||||
s += "Connection: Close\r\n";
|
||||
s += "Content-Type: " + ctype + "\r\n";
|
||||
s += "Access-Control-Allow-Origin: *\r\n";
|
||||
s += "Cross-Origin-Opener-Policy: same-origin\r\n";
|
||||
s += "Cross-Origin-Embedder-Policy: require-corp\r\n";
|
||||
s += "Cache-Control: no-store, max-age=0\r\n";
|
||||
s += "\r\n";
|
||||
CharString cs = s.utf8();
|
||||
Error err = peer->put_data((const uint8_t *)cs.get_data(), cs.size() - 1);
|
||||
if (err != OK) {
|
||||
ERR_FAIL();
|
||||
}
|
||||
|
||||
while (true) {
|
||||
uint8_t bytes[4096];
|
||||
uint64_t read = f->get_buffer(bytes, 4096);
|
||||
if (read == 0) {
|
||||
break;
|
||||
}
|
||||
err = peer->put_data(bytes, read);
|
||||
if (err != OK) {
|
||||
ERR_FAIL();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void EditorHTTPServer::_poll() {
|
||||
if (!server->is_listening()) {
|
||||
return;
|
||||
}
|
||||
if (tcp.is_null()) {
|
||||
if (!server->is_connection_available()) {
|
||||
return;
|
||||
}
|
||||
tcp = server->take_connection();
|
||||
peer = tcp;
|
||||
time = OS::get_singleton()->get_ticks_usec();
|
||||
}
|
||||
if (OS::get_singleton()->get_ticks_usec() - time > 1000000) {
|
||||
_clear_client();
|
||||
return;
|
||||
}
|
||||
if (tcp->get_status() != StreamPeerTCP::STATUS_CONNECTED) {
|
||||
return;
|
||||
}
|
||||
|
||||
if (use_tls) {
|
||||
if (tls.is_null()) {
|
||||
tls = Ref<StreamPeerTLS>(StreamPeerTLS::create());
|
||||
peer = tls;
|
||||
if (tls->accept_stream(tcp, TLSOptions::server(key, cert)) != OK) {
|
||||
_clear_client();
|
||||
return;
|
||||
}
|
||||
}
|
||||
tls->poll();
|
||||
if (tls->get_status() == StreamPeerTLS::STATUS_HANDSHAKING) {
|
||||
// Still handshaking, keep waiting.
|
||||
return;
|
||||
}
|
||||
if (tls->get_status() != StreamPeerTLS::STATUS_CONNECTED) {
|
||||
_clear_client();
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
while (true) {
|
||||
char *r = (char *)req_buf;
|
||||
int l = req_pos - 1;
|
||||
if (l > 3 && r[l] == '\n' && r[l - 1] == '\r' && r[l - 2] == '\n' && r[l - 3] == '\r') {
|
||||
_send_response();
|
||||
_clear_client();
|
||||
return;
|
||||
}
|
||||
|
||||
int read = 0;
|
||||
ERR_FAIL_COND(req_pos >= 4096);
|
||||
Error err = peer->get_partial_data(&req_buf[req_pos], 1, read);
|
||||
if (err != OK) {
|
||||
// Got an error
|
||||
_clear_client();
|
||||
return;
|
||||
} else if (read != 1) {
|
||||
// Busy, wait next poll
|
||||
return;
|
||||
}
|
||||
req_pos += read;
|
||||
}
|
||||
}
|
||||
|
||||
void EditorHTTPServer::stop() {
|
||||
server_quit.set();
|
||||
if (server_thread.is_started()) {
|
||||
server_thread.wait_to_finish();
|
||||
}
|
||||
if (server.is_valid()) {
|
||||
server->stop();
|
||||
}
|
||||
_clear_client();
|
||||
}
|
||||
|
||||
Error EditorHTTPServer::listen(int p_port, IPAddress p_address, bool p_use_tls, String p_tls_key, String p_tls_cert) {
|
||||
MutexLock lock(server_lock);
|
||||
if (server->is_listening()) {
|
||||
return ERR_ALREADY_IN_USE;
|
||||
}
|
||||
use_tls = p_use_tls;
|
||||
if (use_tls) {
|
||||
Ref<Crypto> crypto = Crypto::create();
|
||||
if (crypto.is_null()) {
|
||||
return ERR_UNAVAILABLE;
|
||||
}
|
||||
if (!p_tls_key.is_empty() && !p_tls_cert.is_empty()) {
|
||||
key = Ref<CryptoKey>(CryptoKey::create());
|
||||
Error err = key->load(p_tls_key);
|
||||
ERR_FAIL_COND_V(err != OK, err);
|
||||
cert = Ref<X509Certificate>(X509Certificate::create());
|
||||
err = cert->load(p_tls_cert);
|
||||
ERR_FAIL_COND_V(err != OK, err);
|
||||
} else {
|
||||
_set_internal_certs(crypto);
|
||||
}
|
||||
}
|
||||
Error err = server->listen(p_port, p_address);
|
||||
if (err == OK) {
|
||||
server_quit.clear();
|
||||
server_thread.start(_server_thread_poll, this);
|
||||
}
|
||||
return err;
|
||||
}
|
||||
|
||||
bool EditorHTTPServer::is_listening() const {
|
||||
MutexLock lock(server_lock);
|
||||
return server->is_listening();
|
||||
}
|
||||
|
||||
EditorHTTPServer::EditorHTTPServer() {
|
||||
mimes["html"] = "text/html";
|
||||
mimes["js"] = "application/javascript";
|
||||
mimes["json"] = "application/json";
|
||||
mimes["pck"] = "application/octet-stream";
|
||||
mimes["png"] = "image/png";
|
||||
mimes["svg"] = "image/svg";
|
||||
mimes["wasm"] = "application/wasm";
|
||||
server.instantiate();
|
||||
stop();
|
||||
}
|
||||
|
||||
EditorHTTPServer::~EditorHTTPServer() {
|
||||
stop();
|
||||
}
|
71
platform/web/export/editor_http_server.h
Normal file
71
platform/web/export/editor_http_server.h
Normal file
@@ -0,0 +1,71 @@
|
||||
/**************************************************************************/
|
||||
/* editor_http_server.h */
|
||||
/**************************************************************************/
|
||||
/* This file is part of: */
|
||||
/* GODOT ENGINE */
|
||||
/* https://godotengine.org */
|
||||
/**************************************************************************/
|
||||
/* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
|
||||
/* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
|
||||
/* */
|
||||
/* 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. */
|
||||
/**************************************************************************/
|
||||
|
||||
#pragma once
|
||||
|
||||
#include "core/io/image_loader.h"
|
||||
#include "core/io/stream_peer_tls.h"
|
||||
#include "core/io/tcp_server.h"
|
||||
#include "core/io/zip_io.h"
|
||||
#include "editor/file_system/editor_paths.h"
|
||||
|
||||
class EditorHTTPServer : public RefCounted {
|
||||
private:
|
||||
Ref<TCPServer> server;
|
||||
HashMap<String, String> mimes;
|
||||
Ref<StreamPeerTCP> tcp;
|
||||
Ref<StreamPeerTLS> tls;
|
||||
Ref<StreamPeer> peer;
|
||||
Ref<CryptoKey> key;
|
||||
Ref<X509Certificate> cert;
|
||||
bool use_tls = false;
|
||||
uint64_t time = 0;
|
||||
uint8_t req_buf[4096];
|
||||
int req_pos = 0;
|
||||
|
||||
SafeFlag server_quit;
|
||||
Mutex server_lock;
|
||||
Thread server_thread;
|
||||
|
||||
void _clear_client();
|
||||
void _set_internal_certs(Ref<Crypto> p_crypto);
|
||||
void _send_response();
|
||||
void _poll();
|
||||
|
||||
static void _server_thread_poll(void *data);
|
||||
|
||||
public:
|
||||
EditorHTTPServer();
|
||||
~EditorHTTPServer();
|
||||
|
||||
void stop();
|
||||
Error listen(int p_port, IPAddress p_address, bool p_use_tls, String p_tls_key, String p_tls_cert);
|
||||
bool is_listening() const;
|
||||
};
|
56
platform/web/export/export.cpp
Normal file
56
platform/web/export/export.cpp
Normal file
@@ -0,0 +1,56 @@
|
||||
/**************************************************************************/
|
||||
/* export.cpp */
|
||||
/**************************************************************************/
|
||||
/* This file is part of: */
|
||||
/* GODOT ENGINE */
|
||||
/* https://godotengine.org */
|
||||
/**************************************************************************/
|
||||
/* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
|
||||
/* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
|
||||
/* */
|
||||
/* 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 "export.h"
|
||||
|
||||
#include "export_plugin.h"
|
||||
|
||||
#include "editor/export/editor_export.h"
|
||||
#include "editor/settings/editor_settings.h"
|
||||
|
||||
void register_web_exporter_types() {
|
||||
GDREGISTER_VIRTUAL_CLASS(EditorExportPlatformWeb);
|
||||
}
|
||||
|
||||
void register_web_exporter() {
|
||||
// TODO: Move to editor_settings.cpp
|
||||
EDITOR_DEF("export/web/http_host", "localhost");
|
||||
EDITOR_DEF("export/web/http_port", 8060);
|
||||
EDITOR_DEF("export/web/use_tls", false);
|
||||
EDITOR_DEF("export/web/tls_key", "");
|
||||
EDITOR_DEF("export/web/tls_certificate", "");
|
||||
EditorSettings::get_singleton()->add_property_hint(PropertyInfo(Variant::INT, "export/web/http_port", PROPERTY_HINT_RANGE, "1,65535,1"));
|
||||
EditorSettings::get_singleton()->add_property_hint(PropertyInfo(Variant::STRING, "export/web/tls_key", PROPERTY_HINT_GLOBAL_FILE, "*.key"));
|
||||
EditorSettings::get_singleton()->add_property_hint(PropertyInfo(Variant::STRING, "export/web/tls_certificate", PROPERTY_HINT_GLOBAL_FILE, "*.crt,*.pem"));
|
||||
|
||||
Ref<EditorExportPlatformWeb> platform;
|
||||
platform.instantiate();
|
||||
EditorExport::get_singleton()->add_export_platform(platform);
|
||||
}
|
34
platform/web/export/export.h
Normal file
34
platform/web/export/export.h
Normal file
@@ -0,0 +1,34 @@
|
||||
/**************************************************************************/
|
||||
/* export.h */
|
||||
/**************************************************************************/
|
||||
/* This file is part of: */
|
||||
/* GODOT ENGINE */
|
||||
/* https://godotengine.org */
|
||||
/**************************************************************************/
|
||||
/* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
|
||||
/* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
|
||||
/* */
|
||||
/* 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. */
|
||||
/**************************************************************************/
|
||||
|
||||
#pragma once
|
||||
|
||||
void register_web_exporter_types();
|
||||
void register_web_exporter();
|
944
platform/web/export/export_plugin.cpp
Normal file
944
platform/web/export/export_plugin.cpp
Normal file
@@ -0,0 +1,944 @@
|
||||
/**************************************************************************/
|
||||
/* export_plugin.cpp */
|
||||
/**************************************************************************/
|
||||
/* This file is part of: */
|
||||
/* GODOT ENGINE */
|
||||
/* https://godotengine.org */
|
||||
/**************************************************************************/
|
||||
/* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
|
||||
/* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
|
||||
/* */
|
||||
/* 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 "export_plugin.h"
|
||||
|
||||
#include "logo_svg.gen.h"
|
||||
#include "run_icon_svg.gen.h"
|
||||
|
||||
#include "core/config/project_settings.h"
|
||||
#include "editor/editor_string_names.h"
|
||||
#include "editor/export/editor_export.h"
|
||||
#include "editor/import/resource_importer_texture_settings.h"
|
||||
#include "editor/settings/editor_settings.h"
|
||||
#include "editor/themes/editor_scale.h"
|
||||
#include "scene/resources/image_texture.h"
|
||||
|
||||
#include "modules/modules_enabled.gen.h" // For mono.
|
||||
#include "modules/svg/image_loader_svg.h"
|
||||
|
||||
Error EditorExportPlatformWeb::_extract_template(const String &p_template, const String &p_dir, const String &p_name, bool pwa) {
|
||||
Ref<FileAccess> io_fa;
|
||||
zlib_filefunc_def io = zipio_create_io(&io_fa);
|
||||
unzFile pkg = unzOpen2(p_template.utf8().get_data(), &io);
|
||||
|
||||
if (!pkg) {
|
||||
add_message(EXPORT_MESSAGE_ERROR, TTR("Prepare Templates"), vformat(TTR("Could not open template for export: \"%s\"."), p_template));
|
||||
return ERR_FILE_NOT_FOUND;
|
||||
}
|
||||
|
||||
if (unzGoToFirstFile(pkg) != UNZ_OK) {
|
||||
add_message(EXPORT_MESSAGE_ERROR, TTR("Prepare Templates"), vformat(TTR("Invalid export template: \"%s\"."), p_template));
|
||||
unzClose(pkg);
|
||||
return ERR_FILE_CORRUPT;
|
||||
}
|
||||
|
||||
do {
|
||||
//get filename
|
||||
unz_file_info info;
|
||||
char fname[16384];
|
||||
unzGetCurrentFileInfo(pkg, &info, fname, 16384, nullptr, 0, nullptr, 0);
|
||||
|
||||
String file = String::utf8(fname);
|
||||
|
||||
// Skip folders.
|
||||
if (file.ends_with("/")) {
|
||||
continue;
|
||||
}
|
||||
|
||||
// Skip service worker and offline page if not exporting pwa.
|
||||
if (!pwa && (file == "godot.service.worker.js" || file == "godot.offline.html")) {
|
||||
continue;
|
||||
}
|
||||
Vector<uint8_t> data;
|
||||
data.resize(info.uncompressed_size);
|
||||
|
||||
//read
|
||||
unzOpenCurrentFile(pkg);
|
||||
unzReadCurrentFile(pkg, data.ptrw(), data.size());
|
||||
unzCloseCurrentFile(pkg);
|
||||
|
||||
//write
|
||||
String dst = p_dir.path_join(file.replace("godot", p_name));
|
||||
Ref<FileAccess> f = FileAccess::open(dst, FileAccess::WRITE);
|
||||
if (f.is_null()) {
|
||||
add_message(EXPORT_MESSAGE_ERROR, TTR("Prepare Templates"), vformat(TTR("Could not write file: \"%s\"."), dst));
|
||||
unzClose(pkg);
|
||||
return ERR_FILE_CANT_WRITE;
|
||||
}
|
||||
f->store_buffer(data.ptr(), data.size());
|
||||
|
||||
} while (unzGoToNextFile(pkg) == UNZ_OK);
|
||||
unzClose(pkg);
|
||||
return OK;
|
||||
}
|
||||
|
||||
Error EditorExportPlatformWeb::_write_or_error(const uint8_t *p_content, int p_size, String p_path) {
|
||||
Ref<FileAccess> f = FileAccess::open(p_path, FileAccess::WRITE);
|
||||
if (f.is_null()) {
|
||||
add_message(EXPORT_MESSAGE_ERROR, TTR("Export"), vformat(TTR("Could not write file: \"%s\"."), p_path));
|
||||
return ERR_FILE_CANT_WRITE;
|
||||
}
|
||||
f->store_buffer(p_content, p_size);
|
||||
return OK;
|
||||
}
|
||||
|
||||
void EditorExportPlatformWeb::_replace_strings(const HashMap<String, String> &p_replaces, Vector<uint8_t> &r_template) {
|
||||
String str_template = String::utf8(reinterpret_cast<const char *>(r_template.ptr()), r_template.size());
|
||||
String out;
|
||||
Vector<String> lines = str_template.split("\n");
|
||||
for (int i = 0; i < lines.size(); i++) {
|
||||
String current_line = lines[i];
|
||||
for (const KeyValue<String, String> &E : p_replaces) {
|
||||
current_line = current_line.replace(E.key, E.value);
|
||||
}
|
||||
out += current_line + "\n";
|
||||
}
|
||||
CharString cs = out.utf8();
|
||||
r_template.resize(cs.length());
|
||||
for (int i = 0; i < cs.length(); i++) {
|
||||
r_template.write[i] = cs[i];
|
||||
}
|
||||
}
|
||||
|
||||
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) {
|
||||
// Engine.js config
|
||||
Dictionary config;
|
||||
Array libs;
|
||||
for (int i = 0; i < p_shared_objects.size(); i++) {
|
||||
libs.push_back(p_shared_objects[i].path.get_file());
|
||||
}
|
||||
Vector<String> flags = gen_export_flags(p_flags & (~DEBUG_FLAG_DUMB_CLIENT));
|
||||
Array args;
|
||||
for (int i = 0; i < flags.size(); i++) {
|
||||
args.push_back(flags[i]);
|
||||
}
|
||||
config["canvasResizePolicy"] = p_preset->get("html/canvas_resize_policy");
|
||||
config["experimentalVK"] = p_preset->get("html/experimental_virtual_keyboard");
|
||||
config["focusCanvas"] = p_preset->get("html/focus_canvas_on_start");
|
||||
config["gdextensionLibs"] = libs;
|
||||
config["executable"] = p_name;
|
||||
config["args"] = args;
|
||||
config["fileSizes"] = p_file_sizes;
|
||||
config["ensureCrossOriginIsolationHeaders"] = (bool)p_preset->get("progressive_web_app/ensure_cross_origin_isolation_headers");
|
||||
|
||||
config["godotPoolSize"] = p_preset->get("threads/godot_pool_size");
|
||||
config["emscriptenPoolSize"] = p_preset->get("threads/emscripten_pool_size");
|
||||
|
||||
String head_include;
|
||||
if (p_preset->get("html/export_icon")) {
|
||||
head_include += "<link id=\"-gd-engine-icon\" rel=\"icon\" type=\"image/png\" href=\"" + p_name + ".icon.png\" />\n";
|
||||
head_include += "<link rel=\"apple-touch-icon\" href=\"" + p_name + ".apple-touch-icon.png\"/>\n";
|
||||
}
|
||||
if (p_preset->get("progressive_web_app/enabled")) {
|
||||
head_include += "<link rel=\"manifest\" href=\"" + p_name + ".manifest.json\">\n";
|
||||
config["serviceWorker"] = p_name + ".service.worker.js";
|
||||
}
|
||||
|
||||
// Replaces HTML string
|
||||
const String str_config = Variant(config).to_json_string();
|
||||
const String custom_head_include = p_preset->get("html/head_include");
|
||||
HashMap<String, String> replaces;
|
||||
replaces["$GODOT_URL"] = p_name + ".js";
|
||||
replaces["$GODOT_PROJECT_NAME"] = get_project_setting(p_preset, "application/config/name");
|
||||
replaces["$GODOT_HEAD_INCLUDE"] = head_include + custom_head_include;
|
||||
replaces["$GODOT_CONFIG"] = str_config;
|
||||
replaces["$GODOT_SPLASH_COLOR"] = "#" + Color(get_project_setting(p_preset, "application/boot_splash/bg_color")).to_html(false);
|
||||
|
||||
LocalVector<String> godot_splash_classes;
|
||||
godot_splash_classes.push_back("show-image--" + String(get_project_setting(p_preset, "application/boot_splash/show_image")));
|
||||
godot_splash_classes.push_back("fullsize--" + String(get_project_setting(p_preset, "application/boot_splash/fullsize")));
|
||||
godot_splash_classes.push_back("use-filter--" + String(get_project_setting(p_preset, "application/boot_splash/use_filter")));
|
||||
replaces["$GODOT_SPLASH_CLASSES"] = String(" ").join(godot_splash_classes);
|
||||
replaces["$GODOT_SPLASH"] = p_name + ".png";
|
||||
|
||||
if (p_preset->get("variant/thread_support")) {
|
||||
replaces["$GODOT_THREADS_ENABLED"] = "true";
|
||||
} else {
|
||||
replaces["$GODOT_THREADS_ENABLED"] = "false";
|
||||
}
|
||||
|
||||
_replace_strings(replaces, p_html);
|
||||
}
|
||||
|
||||
Error EditorExportPlatformWeb::_add_manifest_icon(const Ref<EditorExportPreset> &p_preset, const String &p_path, const String &p_icon, int p_size, Array &r_arr) {
|
||||
const String name = p_path.get_file().get_basename();
|
||||
const String icon_name = vformat("%s.%dx%d.png", name, p_size, p_size);
|
||||
const String icon_dest = p_path.get_base_dir().path_join(icon_name);
|
||||
|
||||
Ref<Image> icon;
|
||||
if (!p_icon.is_empty()) {
|
||||
Error err = OK;
|
||||
icon = _load_icon_or_splash_image(p_icon, &err);
|
||||
if (err != OK || icon.is_null() || icon->is_empty()) {
|
||||
add_message(EXPORT_MESSAGE_ERROR, TTR("Icon Creation"), vformat(TTR("Could not read file: \"%s\"."), p_icon));
|
||||
return err;
|
||||
}
|
||||
if (icon->get_width() != p_size || icon->get_height() != p_size) {
|
||||
icon->resize(p_size, p_size);
|
||||
}
|
||||
} else {
|
||||
icon = _get_project_icon(p_preset);
|
||||
icon->resize(p_size, p_size);
|
||||
}
|
||||
const Error err = icon->save_png(icon_dest);
|
||||
if (err != OK) {
|
||||
add_message(EXPORT_MESSAGE_ERROR, TTR("Icon Creation"), vformat(TTR("Could not write file: \"%s\"."), icon_dest));
|
||||
return err;
|
||||
}
|
||||
Dictionary icon_dict;
|
||||
icon_dict["sizes"] = vformat("%dx%d", p_size, p_size);
|
||||
icon_dict["type"] = "image/png";
|
||||
icon_dict["src"] = icon_name;
|
||||
r_arr.push_back(icon_dict);
|
||||
return err;
|
||||
}
|
||||
|
||||
Error EditorExportPlatformWeb::_build_pwa(const Ref<EditorExportPreset> &p_preset, const String p_path, const Vector<SharedObject> &p_shared_objects) {
|
||||
String proj_name = get_project_setting(p_preset, "application/config/name");
|
||||
if (proj_name.is_empty()) {
|
||||
proj_name = "Godot Game";
|
||||
}
|
||||
|
||||
// Service worker
|
||||
const String dir = p_path.get_base_dir();
|
||||
const String name = p_path.get_file().get_basename();
|
||||
bool extensions = (bool)p_preset->get("variant/extensions_support");
|
||||
bool ensure_crossorigin_isolation_headers = (bool)p_preset->get("progressive_web_app/ensure_cross_origin_isolation_headers");
|
||||
HashMap<String, String> replaces;
|
||||
replaces["___GODOT_VERSION___"] = String::num_int64(OS::get_singleton()->get_unix_time()) + "|" + String::num_int64(OS::get_singleton()->get_ticks_usec());
|
||||
replaces["___GODOT_NAME___"] = proj_name.substr(0, 16);
|
||||
replaces["___GODOT_OFFLINE_PAGE___"] = name + ".offline.html";
|
||||
replaces["___GODOT_ENSURE_CROSSORIGIN_ISOLATION_HEADERS___"] = ensure_crossorigin_isolation_headers ? "true" : "false";
|
||||
|
||||
// Files cached during worker install.
|
||||
Array cache_files = {
|
||||
name + ".html",
|
||||
name + ".js",
|
||||
name + ".offline.html"
|
||||
};
|
||||
if (p_preset->get("html/export_icon")) {
|
||||
cache_files.push_back(name + ".icon.png");
|
||||
cache_files.push_back(name + ".apple-touch-icon.png");
|
||||
}
|
||||
|
||||
cache_files.push_back(name + ".audio.worklet.js");
|
||||
cache_files.push_back(name + ".audio.position.worklet.js");
|
||||
replaces["___GODOT_CACHE___"] = Variant(cache_files).to_json_string();
|
||||
|
||||
// Heavy files that are cached on demand.
|
||||
Array opt_cache_files = {
|
||||
name + ".wasm",
|
||||
name + ".pck"
|
||||
};
|
||||
if (extensions) {
|
||||
opt_cache_files.push_back(name + ".side.wasm");
|
||||
for (int i = 0; i < p_shared_objects.size(); i++) {
|
||||
opt_cache_files.push_back(p_shared_objects[i].path.get_file());
|
||||
}
|
||||
}
|
||||
replaces["___GODOT_OPT_CACHE___"] = Variant(opt_cache_files).to_json_string();
|
||||
|
||||
const String sw_path = dir.path_join(name + ".service.worker.js");
|
||||
Vector<uint8_t> sw;
|
||||
{
|
||||
Ref<FileAccess> f = FileAccess::open(sw_path, FileAccess::READ);
|
||||
if (f.is_null()) {
|
||||
add_message(EXPORT_MESSAGE_ERROR, TTR("PWA"), vformat(TTR("Could not read file: \"%s\"."), sw_path));
|
||||
return ERR_FILE_CANT_READ;
|
||||
}
|
||||
sw.resize(f->get_length());
|
||||
f->get_buffer(sw.ptrw(), sw.size());
|
||||
}
|
||||
_replace_strings(replaces, sw);
|
||||
Error err = _write_or_error(sw.ptr(), sw.size(), dir.path_join(name + ".service.worker.js"));
|
||||
if (err != OK) {
|
||||
// Message is supplied by the subroutine method.
|
||||
return err;
|
||||
}
|
||||
|
||||
// Custom offline page
|
||||
const String offline_page = p_preset->get("progressive_web_app/offline_page");
|
||||
if (!offline_page.is_empty()) {
|
||||
Ref<DirAccess> da = DirAccess::create(DirAccess::ACCESS_FILESYSTEM);
|
||||
const String offline_dest = dir.path_join(name + ".offline.html");
|
||||
err = da->copy(ProjectSettings::get_singleton()->globalize_path(offline_page), offline_dest);
|
||||
if (err != OK) {
|
||||
add_message(EXPORT_MESSAGE_ERROR, TTR("PWA"), vformat(TTR("Could not read file: \"%s\"."), offline_dest));
|
||||
return err;
|
||||
}
|
||||
}
|
||||
|
||||
// Manifest
|
||||
const char *modes[4] = { "fullscreen", "standalone", "minimal-ui", "browser" };
|
||||
const char *orientations[3] = { "any", "landscape", "portrait" };
|
||||
const int display = CLAMP(int(p_preset->get("progressive_web_app/display")), 0, 4);
|
||||
const int orientation = CLAMP(int(p_preset->get("progressive_web_app/orientation")), 0, 3);
|
||||
|
||||
Dictionary manifest;
|
||||
manifest["name"] = proj_name;
|
||||
manifest["start_url"] = "./" + name + ".html";
|
||||
manifest["display"] = String::utf8(modes[display]);
|
||||
manifest["orientation"] = String::utf8(orientations[orientation]);
|
||||
manifest["background_color"] = "#" + p_preset->get("progressive_web_app/background_color").operator Color().to_html(false);
|
||||
|
||||
Array icons_arr;
|
||||
const String icon144_path = p_preset->get("progressive_web_app/icon_144x144");
|
||||
err = _add_manifest_icon(p_preset, p_path, icon144_path, 144, icons_arr);
|
||||
if (err != OK) {
|
||||
// Message is supplied by the subroutine method.
|
||||
return err;
|
||||
}
|
||||
const String icon180_path = p_preset->get("progressive_web_app/icon_180x180");
|
||||
err = _add_manifest_icon(p_preset, p_path, icon180_path, 180, icons_arr);
|
||||
if (err != OK) {
|
||||
// Message is supplied by the subroutine method.
|
||||
return err;
|
||||
}
|
||||
const String icon512_path = p_preset->get("progressive_web_app/icon_512x512");
|
||||
err = _add_manifest_icon(p_preset, p_path, icon512_path, 512, icons_arr);
|
||||
if (err != OK) {
|
||||
// Message is supplied by the subroutine method.
|
||||
return err;
|
||||
}
|
||||
manifest["icons"] = icons_arr;
|
||||
|
||||
CharString cs = Variant(manifest).to_json_string().utf8();
|
||||
err = _write_or_error((const uint8_t *)cs.get_data(), cs.length(), dir.path_join(name + ".manifest.json"));
|
||||
if (err != OK) {
|
||||
// Message is supplied by the subroutine method.
|
||||
return err;
|
||||
}
|
||||
|
||||
return OK;
|
||||
}
|
||||
|
||||
void EditorExportPlatformWeb::get_preset_features(const Ref<EditorExportPreset> &p_preset, List<String> *r_features) const {
|
||||
if (p_preset->get("vram_texture_compression/for_desktop")) {
|
||||
r_features->push_back("s3tc");
|
||||
r_features->push_back("bptc");
|
||||
}
|
||||
if (p_preset->get("vram_texture_compression/for_mobile")) {
|
||||
r_features->push_back("etc2");
|
||||
r_features->push_back("astc");
|
||||
}
|
||||
if (p_preset->get("variant/thread_support").operator bool()) {
|
||||
r_features->push_back("threads");
|
||||
} else {
|
||||
r_features->push_back("nothreads");
|
||||
}
|
||||
if (p_preset->get("variant/extensions_support").operator bool()) {
|
||||
r_features->push_back("web_extensions");
|
||||
} else {
|
||||
r_features->push_back("web_noextensions");
|
||||
}
|
||||
r_features->push_back("wasm32");
|
||||
}
|
||||
|
||||
void EditorExportPlatformWeb::get_export_options(List<ExportOption> *r_options) const {
|
||||
r_options->push_back(ExportOption(PropertyInfo(Variant::STRING, "custom_template/debug", PROPERTY_HINT_GLOBAL_FILE, "*.zip"), ""));
|
||||
r_options->push_back(ExportOption(PropertyInfo(Variant::STRING, "custom_template/release", PROPERTY_HINT_GLOBAL_FILE, "*.zip"), ""));
|
||||
|
||||
r_options->push_back(ExportOption(PropertyInfo(Variant::BOOL, "variant/extensions_support"), false)); // GDExtension support.
|
||||
r_options->push_back(ExportOption(PropertyInfo(Variant::BOOL, "variant/thread_support"), false, true)); // Thread support (i.e. run with or without COEP/COOP headers).
|
||||
r_options->push_back(ExportOption(PropertyInfo(Variant::BOOL, "vram_texture_compression/for_desktop"), true)); // S3TC
|
||||
r_options->push_back(ExportOption(PropertyInfo(Variant::BOOL, "vram_texture_compression/for_mobile"), false)); // ETC or ETC2, depending on renderer
|
||||
|
||||
r_options->push_back(ExportOption(PropertyInfo(Variant::BOOL, "html/export_icon"), true));
|
||||
r_options->push_back(ExportOption(PropertyInfo(Variant::STRING, "html/custom_html_shell", PROPERTY_HINT_FILE, "*.html"), ""));
|
||||
r_options->push_back(ExportOption(PropertyInfo(Variant::STRING, "html/head_include", PROPERTY_HINT_MULTILINE_TEXT), ""));
|
||||
r_options->push_back(ExportOption(PropertyInfo(Variant::INT, "html/canvas_resize_policy", PROPERTY_HINT_ENUM, "None,Project,Adaptive"), 2));
|
||||
r_options->push_back(ExportOption(PropertyInfo(Variant::BOOL, "html/focus_canvas_on_start"), true));
|
||||
r_options->push_back(ExportOption(PropertyInfo(Variant::BOOL, "html/experimental_virtual_keyboard"), false));
|
||||
r_options->push_back(ExportOption(PropertyInfo(Variant::BOOL, "progressive_web_app/enabled"), false));
|
||||
r_options->push_back(ExportOption(PropertyInfo(Variant::BOOL, "progressive_web_app/ensure_cross_origin_isolation_headers"), true));
|
||||
r_options->push_back(ExportOption(PropertyInfo(Variant::STRING, "progressive_web_app/offline_page", PROPERTY_HINT_FILE, "*.html"), ""));
|
||||
r_options->push_back(ExportOption(PropertyInfo(Variant::INT, "progressive_web_app/display", PROPERTY_HINT_ENUM, "Fullscreen,Standalone,Minimal UI,Browser"), 1));
|
||||
r_options->push_back(ExportOption(PropertyInfo(Variant::INT, "progressive_web_app/orientation", PROPERTY_HINT_ENUM, "Any,Landscape,Portrait"), 0));
|
||||
r_options->push_back(ExportOption(PropertyInfo(Variant::STRING, "progressive_web_app/icon_144x144", PROPERTY_HINT_FILE, "*.png,*.webp,*.svg"), ""));
|
||||
r_options->push_back(ExportOption(PropertyInfo(Variant::STRING, "progressive_web_app/icon_180x180", PROPERTY_HINT_FILE, "*.png,*.webp,*.svg"), ""));
|
||||
r_options->push_back(ExportOption(PropertyInfo(Variant::STRING, "progressive_web_app/icon_512x512", PROPERTY_HINT_FILE, "*.png,*.webp,*.svg"), ""));
|
||||
r_options->push_back(ExportOption(PropertyInfo(Variant::COLOR, "progressive_web_app/background_color", PROPERTY_HINT_COLOR_NO_ALPHA), Color()));
|
||||
|
||||
r_options->push_back(ExportOption(PropertyInfo(Variant::INT, "threads/emscripten_pool_size"), 8));
|
||||
r_options->push_back(ExportOption(PropertyInfo(Variant::INT, "threads/godot_pool_size"), 4));
|
||||
}
|
||||
|
||||
bool EditorExportPlatformWeb::get_export_option_visibility(const EditorExportPreset *p_preset, const String &p_option) const {
|
||||
bool advanced_options_enabled = p_preset->are_advanced_options_enabled();
|
||||
if (p_option == "custom_template/debug" || p_option == "custom_template/release") {
|
||||
return advanced_options_enabled;
|
||||
}
|
||||
|
||||
if (p_option == "threads/godot_pool_size" || p_option == "threads/emscripten_pool_size") {
|
||||
return p_preset->get("variant/thread_support").operator bool();
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
String EditorExportPlatformWeb::get_name() const {
|
||||
return "Web";
|
||||
}
|
||||
|
||||
String EditorExportPlatformWeb::get_os_name() const {
|
||||
return "Web";
|
||||
}
|
||||
|
||||
Ref<Texture2D> EditorExportPlatformWeb::get_logo() const {
|
||||
return logo;
|
||||
}
|
||||
|
||||
bool EditorExportPlatformWeb::has_valid_export_configuration(const Ref<EditorExportPreset> &p_preset, String &r_error, bool &r_missing_templates, bool p_debug) const {
|
||||
#ifdef MODULE_MONO_ENABLED
|
||||
// Don't check for additional errors, as this particular error cannot be resolved.
|
||||
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";
|
||||
r_error += TTR("If this project does not use C#, use a non-C# editor build to export the project.") + "\n";
|
||||
return false;
|
||||
#else
|
||||
|
||||
String err;
|
||||
bool valid = false;
|
||||
bool extensions = (bool)p_preset->get("variant/extensions_support");
|
||||
bool thread_support = (bool)p_preset->get("variant/thread_support");
|
||||
|
||||
// Look for export templates (first official, and if defined custom templates).
|
||||
bool dvalid = exists_export_template(_get_template_name(extensions, thread_support, true), &err);
|
||||
bool rvalid = exists_export_template(_get_template_name(extensions, thread_support, false), &err);
|
||||
|
||||
if (p_preset->get("custom_template/debug") != "") {
|
||||
dvalid = FileAccess::exists(p_preset->get("custom_template/debug"));
|
||||
if (!dvalid) {
|
||||
err += TTR("Custom debug template not found.") + "\n";
|
||||
}
|
||||
}
|
||||
if (p_preset->get("custom_template/release") != "") {
|
||||
rvalid = FileAccess::exists(p_preset->get("custom_template/release"));
|
||||
if (!rvalid) {
|
||||
err += TTR("Custom release template not found.") + "\n";
|
||||
}
|
||||
}
|
||||
|
||||
valid = dvalid || rvalid;
|
||||
r_missing_templates = !valid;
|
||||
|
||||
if (!err.is_empty()) {
|
||||
r_error = err;
|
||||
}
|
||||
|
||||
return valid;
|
||||
#endif // !MODULE_MONO_ENABLED
|
||||
}
|
||||
|
||||
bool EditorExportPlatformWeb::has_valid_project_configuration(const Ref<EditorExportPreset> &p_preset, String &r_error) const {
|
||||
String err;
|
||||
bool valid = true;
|
||||
|
||||
// Validate the project configuration.
|
||||
|
||||
if (p_preset->get("vram_texture_compression/for_mobile")) {
|
||||
if (!ResourceImporterTextureSettings::should_import_etc2_astc()) {
|
||||
valid = false;
|
||||
}
|
||||
}
|
||||
|
||||
if (!err.is_empty()) {
|
||||
r_error = err;
|
||||
}
|
||||
|
||||
return valid;
|
||||
}
|
||||
|
||||
List<String> EditorExportPlatformWeb::get_binary_extensions(const Ref<EditorExportPreset> &p_preset) const {
|
||||
List<String> list;
|
||||
list.push_back("html");
|
||||
return list;
|
||||
}
|
||||
|
||||
Error EditorExportPlatformWeb::export_project(const Ref<EditorExportPreset> &p_preset, bool p_debug, const String &p_path, BitField<EditorExportPlatform::DebugFlags> p_flags) {
|
||||
ExportNotifier notifier(*this, p_preset, p_debug, p_path, p_flags);
|
||||
|
||||
const String custom_debug = p_preset->get("custom_template/debug");
|
||||
const String custom_release = p_preset->get("custom_template/release");
|
||||
const String custom_html = p_preset->get("html/custom_html_shell");
|
||||
const bool export_icon = p_preset->get("html/export_icon");
|
||||
const bool pwa = p_preset->get("progressive_web_app/enabled");
|
||||
|
||||
const String base_dir = p_path.get_base_dir();
|
||||
const String base_path = p_path.get_basename();
|
||||
const String base_name = p_path.get_file().get_basename();
|
||||
|
||||
if (!DirAccess::exists(base_dir)) {
|
||||
add_message(EXPORT_MESSAGE_ERROR, TTR("Export"), vformat(TTR("Target folder does not exist or is inaccessible: \"%s\""), base_dir));
|
||||
return ERR_FILE_BAD_PATH;
|
||||
}
|
||||
|
||||
// Find the correct template
|
||||
String template_path = p_debug ? custom_debug : custom_release;
|
||||
template_path = template_path.strip_edges();
|
||||
if (template_path.is_empty()) {
|
||||
bool extensions = (bool)p_preset->get("variant/extensions_support");
|
||||
bool thread_support = (bool)p_preset->get("variant/thread_support");
|
||||
template_path = find_export_template(_get_template_name(extensions, thread_support, p_debug));
|
||||
}
|
||||
|
||||
if (!template_path.is_empty() && !FileAccess::exists(template_path)) {
|
||||
add_message(EXPORT_MESSAGE_ERROR, TTR("Prepare Templates"), vformat(TTR("Template file not found: \"%s\"."), template_path));
|
||||
return ERR_FILE_NOT_FOUND;
|
||||
}
|
||||
|
||||
// Export pck and shared objects
|
||||
Vector<SharedObject> shared_objects;
|
||||
String pck_path = base_path + ".pck";
|
||||
Error error = save_pack(p_preset, p_debug, pck_path, &shared_objects);
|
||||
if (error != OK) {
|
||||
add_message(EXPORT_MESSAGE_ERROR, TTR("Export"), vformat(TTR("Could not write file: \"%s\"."), pck_path));
|
||||
return error;
|
||||
}
|
||||
|
||||
{
|
||||
Ref<DirAccess> da = DirAccess::create(DirAccess::ACCESS_FILESYSTEM);
|
||||
for (int i = 0; i < shared_objects.size(); i++) {
|
||||
String dst = base_dir.path_join(shared_objects[i].path.get_file());
|
||||
error = da->copy(shared_objects[i].path, dst);
|
||||
if (error != OK) {
|
||||
add_message(EXPORT_MESSAGE_ERROR, TTR("Export"), vformat(TTR("Could not write file: \"%s\"."), shared_objects[i].path.get_file()));
|
||||
return error;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Extract templates.
|
||||
error = _extract_template(template_path, base_dir, base_name, pwa);
|
||||
if (error) {
|
||||
// Message is supplied by the subroutine method.
|
||||
return error;
|
||||
}
|
||||
|
||||
// Parse generated file sizes (pck and wasm, to help show a meaningful loading bar).
|
||||
Dictionary file_sizes;
|
||||
Ref<FileAccess> f = FileAccess::open(pck_path, FileAccess::READ);
|
||||
if (f.is_valid()) {
|
||||
file_sizes[pck_path.get_file()] = (uint64_t)f->get_length();
|
||||
}
|
||||
f = FileAccess::open(base_path + ".wasm", FileAccess::READ);
|
||||
if (f.is_valid()) {
|
||||
file_sizes[base_name + ".wasm"] = (uint64_t)f->get_length();
|
||||
}
|
||||
|
||||
// Read the HTML shell file (custom or from template).
|
||||
const String html_path = custom_html.is_empty() ? base_path + ".html" : custom_html;
|
||||
Vector<uint8_t> html;
|
||||
f = FileAccess::open(html_path, FileAccess::READ);
|
||||
if (f.is_null()) {
|
||||
add_message(EXPORT_MESSAGE_ERROR, TTR("Export"), vformat(TTR("Could not read HTML shell: \"%s\"."), html_path));
|
||||
return ERR_FILE_CANT_READ;
|
||||
}
|
||||
html.resize(f->get_length());
|
||||
f->get_buffer(html.ptrw(), html.size());
|
||||
f.unref(); // close file.
|
||||
|
||||
// Generate HTML file with replaced strings.
|
||||
_fix_html(html, p_preset, base_name, p_debug, p_flags, shared_objects, file_sizes);
|
||||
Error err = _write_or_error(html.ptr(), html.size(), p_path);
|
||||
if (err != OK) {
|
||||
// Message is supplied by the subroutine method.
|
||||
return err;
|
||||
}
|
||||
html.resize(0);
|
||||
|
||||
// Export splash (why?)
|
||||
Ref<Image> splash = _get_project_splash(p_preset);
|
||||
const String splash_png_path = base_path + ".png";
|
||||
if (splash->save_png(splash_png_path) != OK) {
|
||||
add_message(EXPORT_MESSAGE_ERROR, TTR("Export"), vformat(TTR("Could not write file: \"%s\"."), 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.
|
||||
if (export_icon) {
|
||||
Ref<Image> favicon = _get_project_icon(p_preset);
|
||||
const String favicon_png_path = base_path + ".icon.png";
|
||||
if (favicon->save_png(favicon_png_path) != OK) {
|
||||
add_message(EXPORT_MESSAGE_ERROR, TTR("Export"), vformat(TTR("Could not write file: \"%s\"."), favicon_png_path));
|
||||
return ERR_FILE_CANT_WRITE;
|
||||
}
|
||||
favicon->resize(180, 180);
|
||||
const String apple_icon_png_path = base_path + ".apple-touch-icon.png";
|
||||
if (favicon->save_png(apple_icon_png_path) != OK) {
|
||||
add_message(EXPORT_MESSAGE_ERROR, TTR("Export"), vformat(TTR("Could not write file: \"%s\"."), apple_icon_png_path));
|
||||
return ERR_FILE_CANT_WRITE;
|
||||
}
|
||||
}
|
||||
|
||||
// Generate the PWA worker and manifest
|
||||
if (pwa) {
|
||||
err = _build_pwa(p_preset, p_path, shared_objects);
|
||||
if (err != OK) {
|
||||
// Message is supplied by the subroutine method.
|
||||
return err;
|
||||
}
|
||||
}
|
||||
|
||||
return OK;
|
||||
}
|
||||
|
||||
bool EditorExportPlatformWeb::poll_export() {
|
||||
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;
|
||||
}
|
||||
}
|
||||
|
||||
RemoteDebugState prev_remote_debug_state = remote_debug_state;
|
||||
remote_debug_state = REMOTE_DEBUG_STATE_UNAVAILABLE;
|
||||
|
||||
if (preset.is_valid()) {
|
||||
const bool debug = true;
|
||||
// Throwaway variables to pass to `can_export`.
|
||||
String err;
|
||||
bool missing_templates;
|
||||
|
||||
if (can_export(preset, err, missing_templates, debug)) {
|
||||
if (server->is_listening()) {
|
||||
remote_debug_state = REMOTE_DEBUG_STATE_SERVING;
|
||||
} else {
|
||||
remote_debug_state = REMOTE_DEBUG_STATE_AVAILABLE;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (remote_debug_state != REMOTE_DEBUG_STATE_SERVING && server->is_listening()) {
|
||||
server->stop();
|
||||
}
|
||||
|
||||
return remote_debug_state != prev_remote_debug_state;
|
||||
}
|
||||
|
||||
Ref<Texture2D> EditorExportPlatformWeb::get_option_icon(int p_index) const {
|
||||
Ref<Texture2D> play_icon = EditorExportPlatform::get_option_icon(p_index);
|
||||
|
||||
switch (remote_debug_state) {
|
||||
case REMOTE_DEBUG_STATE_UNAVAILABLE: {
|
||||
return nullptr;
|
||||
} break;
|
||||
|
||||
case REMOTE_DEBUG_STATE_AVAILABLE: {
|
||||
switch (p_index) {
|
||||
case 0:
|
||||
case 1:
|
||||
return play_icon;
|
||||
default:
|
||||
ERR_FAIL_V(nullptr);
|
||||
}
|
||||
} break;
|
||||
|
||||
case REMOTE_DEBUG_STATE_SERVING: {
|
||||
switch (p_index) {
|
||||
case 0:
|
||||
return play_icon;
|
||||
case 1:
|
||||
return restart_icon;
|
||||
case 2:
|
||||
return stop_icon;
|
||||
default:
|
||||
ERR_FAIL_V(nullptr);
|
||||
}
|
||||
} break;
|
||||
}
|
||||
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
int EditorExportPlatformWeb::get_options_count() const {
|
||||
switch (remote_debug_state) {
|
||||
case REMOTE_DEBUG_STATE_UNAVAILABLE: {
|
||||
return 0;
|
||||
} break;
|
||||
|
||||
case REMOTE_DEBUG_STATE_AVAILABLE: {
|
||||
return 2;
|
||||
} break;
|
||||
|
||||
case REMOTE_DEBUG_STATE_SERVING: {
|
||||
return 3;
|
||||
} break;
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
String EditorExportPlatformWeb::get_option_label(int p_index) const {
|
||||
String run_in_browser = TTR("Run in Browser");
|
||||
String start_http_server = TTR("Start HTTP Server");
|
||||
String reexport_project = TTR("Re-export Project");
|
||||
String stop_http_server = TTR("Stop HTTP Server");
|
||||
|
||||
switch (remote_debug_state) {
|
||||
case REMOTE_DEBUG_STATE_UNAVAILABLE:
|
||||
return "";
|
||||
|
||||
case REMOTE_DEBUG_STATE_AVAILABLE: {
|
||||
switch (p_index) {
|
||||
case 0:
|
||||
return run_in_browser;
|
||||
case 1:
|
||||
return start_http_server;
|
||||
default:
|
||||
ERR_FAIL_V("");
|
||||
}
|
||||
} break;
|
||||
|
||||
case REMOTE_DEBUG_STATE_SERVING: {
|
||||
switch (p_index) {
|
||||
case 0:
|
||||
return run_in_browser;
|
||||
case 1:
|
||||
return reexport_project;
|
||||
case 2:
|
||||
return stop_http_server;
|
||||
default:
|
||||
ERR_FAIL_V("");
|
||||
}
|
||||
} break;
|
||||
}
|
||||
|
||||
return "";
|
||||
}
|
||||
|
||||
String EditorExportPlatformWeb::get_option_tooltip(int p_index) const {
|
||||
String run_in_browser = TTR("Run exported HTML in the system's default browser.");
|
||||
String start_http_server = TTR("Start the HTTP server.");
|
||||
String reexport_project = TTR("Export project again to account for updates.");
|
||||
String stop_http_server = TTR("Stop the HTTP server.");
|
||||
|
||||
switch (remote_debug_state) {
|
||||
case REMOTE_DEBUG_STATE_UNAVAILABLE:
|
||||
return "";
|
||||
|
||||
case REMOTE_DEBUG_STATE_AVAILABLE: {
|
||||
switch (p_index) {
|
||||
case 0:
|
||||
return run_in_browser;
|
||||
case 1:
|
||||
return start_http_server;
|
||||
default:
|
||||
ERR_FAIL_V("");
|
||||
}
|
||||
} break;
|
||||
|
||||
case REMOTE_DEBUG_STATE_SERVING: {
|
||||
switch (p_index) {
|
||||
case 0:
|
||||
return run_in_browser;
|
||||
case 1:
|
||||
return reexport_project;
|
||||
case 2:
|
||||
return stop_http_server;
|
||||
default:
|
||||
ERR_FAIL_V("");
|
||||
}
|
||||
} break;
|
||||
}
|
||||
|
||||
return "";
|
||||
}
|
||||
|
||||
Error EditorExportPlatformWeb::run(const Ref<EditorExportPreset> &p_preset, int p_option, BitField<EditorExportPlatform::DebugFlags> p_debug_flags) {
|
||||
const uint16_t bind_port = EDITOR_GET("export/web/http_port");
|
||||
// Resolve host if needed.
|
||||
const String bind_host = EDITOR_GET("export/web/http_host");
|
||||
const bool use_tls = EDITOR_GET("export/web/use_tls");
|
||||
|
||||
switch (remote_debug_state) {
|
||||
case REMOTE_DEBUG_STATE_UNAVAILABLE: {
|
||||
return FAILED;
|
||||
} break;
|
||||
|
||||
case REMOTE_DEBUG_STATE_AVAILABLE: {
|
||||
switch (p_option) {
|
||||
// Run in Browser.
|
||||
case 0: {
|
||||
Error err = _export_project(p_preset, p_debug_flags);
|
||||
if (err != OK) {
|
||||
return err;
|
||||
}
|
||||
err = _start_server(bind_host, bind_port, use_tls);
|
||||
if (err != OK) {
|
||||
return err;
|
||||
}
|
||||
return _launch_browser(bind_host, bind_port, use_tls);
|
||||
} break;
|
||||
|
||||
// Start HTTP Server.
|
||||
case 1: {
|
||||
Error err = _export_project(p_preset, p_debug_flags);
|
||||
if (err != OK) {
|
||||
return err;
|
||||
}
|
||||
return _start_server(bind_host, bind_port, use_tls);
|
||||
} break;
|
||||
|
||||
default: {
|
||||
ERR_FAIL_V_MSG(FAILED, vformat(R"(Invalid option "%s" for the current state.)", p_option));
|
||||
}
|
||||
}
|
||||
} break;
|
||||
|
||||
case REMOTE_DEBUG_STATE_SERVING: {
|
||||
switch (p_option) {
|
||||
// Run in Browser.
|
||||
case 0: {
|
||||
Error err = _export_project(p_preset, p_debug_flags);
|
||||
if (err != OK) {
|
||||
return err;
|
||||
}
|
||||
return _launch_browser(bind_host, bind_port, use_tls);
|
||||
} break;
|
||||
|
||||
// Re-export Project.
|
||||
case 1: {
|
||||
return _export_project(p_preset, p_debug_flags);
|
||||
} break;
|
||||
|
||||
// Stop HTTP Server.
|
||||
case 2: {
|
||||
return _stop_server();
|
||||
} break;
|
||||
|
||||
default: {
|
||||
ERR_FAIL_V_MSG(FAILED, vformat(R"(Invalid option "%s" for the current state.)", p_option));
|
||||
}
|
||||
}
|
||||
} break;
|
||||
}
|
||||
|
||||
return FAILED;
|
||||
}
|
||||
|
||||
Error EditorExportPlatformWeb::_export_project(const Ref<EditorExportPreset> &p_preset, int p_debug_flags) {
|
||||
const String dest = EditorPaths::get_singleton()->get_temp_dir().path_join("web");
|
||||
Ref<DirAccess> da = DirAccess::create(DirAccess::ACCESS_FILESYSTEM);
|
||||
if (!da->dir_exists(dest)) {
|
||||
Error err = da->make_dir_recursive(dest);
|
||||
if (err != OK) {
|
||||
add_message(EXPORT_MESSAGE_ERROR, TTR("Run"), vformat(TTR("Could not create HTTP server directory: %s."), dest));
|
||||
return err;
|
||||
}
|
||||
}
|
||||
|
||||
const String basepath = dest.path_join("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");
|
||||
DirAccess::remove_file_or_error(basepath + ".offline.html");
|
||||
DirAccess::remove_file_or_error(basepath + ".js");
|
||||
DirAccess::remove_file_or_error(basepath + ".audio.worklet.js");
|
||||
DirAccess::remove_file_or_error(basepath + ".audio.position.worklet.js");
|
||||
DirAccess::remove_file_or_error(basepath + ".service.worker.js");
|
||||
DirAccess::remove_file_or_error(basepath + ".pck");
|
||||
DirAccess::remove_file_or_error(basepath + ".png");
|
||||
DirAccess::remove_file_or_error(basepath + ".side.wasm");
|
||||
DirAccess::remove_file_or_error(basepath + ".wasm");
|
||||
DirAccess::remove_file_or_error(basepath + ".icon.png");
|
||||
DirAccess::remove_file_or_error(basepath + ".apple-touch-icon.png");
|
||||
}
|
||||
return err;
|
||||
}
|
||||
|
||||
Error EditorExportPlatformWeb::_launch_browser(const String &p_bind_host, const uint16_t p_bind_port, const bool p_use_tls) {
|
||||
OS::get_singleton()->shell_open(String((p_use_tls ? "https://" : "http://") + p_bind_host + ":" + itos(p_bind_port) + "/tmp_js_export.html"));
|
||||
// FIXME: Find out how to clean up export files after running the successfully
|
||||
// exported game. Might not be trivial.
|
||||
return OK;
|
||||
}
|
||||
|
||||
Error EditorExportPlatformWeb::_start_server(const String &p_bind_host, const uint16_t p_bind_port, const bool p_use_tls) {
|
||||
IPAddress bind_ip;
|
||||
if (p_bind_host.is_valid_ip_address()) {
|
||||
bind_ip = p_bind_host;
|
||||
} else {
|
||||
bind_ip = IP::get_singleton()->resolve_hostname(p_bind_host);
|
||||
}
|
||||
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'.");
|
||||
|
||||
const String tls_key = EDITOR_GET("export/web/tls_key");
|
||||
const String tls_cert = EDITOR_GET("export/web/tls_certificate");
|
||||
|
||||
// Restart server.
|
||||
server->stop();
|
||||
Error err = server->listen(p_bind_port, bind_ip, p_use_tls, tls_key, tls_cert);
|
||||
if (err != OK) {
|
||||
add_message(EXPORT_MESSAGE_ERROR, TTR("Run"), vformat(TTR("Error starting HTTP server: %d."), err));
|
||||
}
|
||||
return err;
|
||||
}
|
||||
|
||||
Error EditorExportPlatformWeb::_stop_server() {
|
||||
server->stop();
|
||||
return OK;
|
||||
}
|
||||
|
||||
Ref<Texture2D> EditorExportPlatformWeb::get_run_icon() const {
|
||||
return run_icon;
|
||||
}
|
||||
|
||||
EditorExportPlatformWeb::EditorExportPlatformWeb() {
|
||||
if (EditorNode::get_singleton()) {
|
||||
server.instantiate();
|
||||
|
||||
Ref<Image> img = memnew(Image);
|
||||
const bool upsample = !Math::is_equal_approx(Math::round(EDSCALE), EDSCALE);
|
||||
|
||||
ImageLoaderSVG::create_image_from_string(img, _web_logo_svg, EDSCALE, upsample, false);
|
||||
logo = ImageTexture::create_from_image(img);
|
||||
|
||||
ImageLoaderSVG::create_image_from_string(img, _web_run_icon_svg, EDSCALE, upsample, false);
|
||||
run_icon = ImageTexture::create_from_image(img);
|
||||
|
||||
Ref<Theme> theme = EditorNode::get_singleton()->get_editor_theme();
|
||||
if (theme.is_valid()) {
|
||||
stop_icon = theme->get_icon(SNAME("Stop"), EditorStringName(EditorIcons));
|
||||
restart_icon = theme->get_icon(SNAME("Reload"), EditorStringName(EditorIcons));
|
||||
} else {
|
||||
stop_icon.instantiate();
|
||||
restart_icon.instantiate();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
EditorExportPlatformWeb::~EditorExportPlatformWeb() {
|
||||
}
|
153
platform/web/export/export_plugin.h
Normal file
153
platform/web/export/export_plugin.h
Normal file
@@ -0,0 +1,153 @@
|
||||
/**************************************************************************/
|
||||
/* export_plugin.h */
|
||||
/**************************************************************************/
|
||||
/* This file is part of: */
|
||||
/* GODOT ENGINE */
|
||||
/* https://godotengine.org */
|
||||
/**************************************************************************/
|
||||
/* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
|
||||
/* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
|
||||
/* */
|
||||
/* 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. */
|
||||
/**************************************************************************/
|
||||
|
||||
#pragma once
|
||||
|
||||
#include "editor_http_server.h"
|
||||
|
||||
#include "core/config/project_settings.h"
|
||||
#include "core/io/image_loader.h"
|
||||
#include "core/io/stream_peer_tls.h"
|
||||
#include "core/io/tcp_server.h"
|
||||
#include "core/io/zip_io.h"
|
||||
#include "editor/editor_node.h"
|
||||
#include "editor/editor_string_names.h"
|
||||
#include "editor/export/editor_export_platform.h"
|
||||
#include "main/splash.gen.h"
|
||||
|
||||
class EditorExportPlatformWeb : public EditorExportPlatform {
|
||||
GDCLASS(EditorExportPlatformWeb, EditorExportPlatform);
|
||||
|
||||
enum RemoteDebugState {
|
||||
REMOTE_DEBUG_STATE_UNAVAILABLE,
|
||||
REMOTE_DEBUG_STATE_AVAILABLE,
|
||||
REMOTE_DEBUG_STATE_SERVING,
|
||||
};
|
||||
|
||||
Ref<ImageTexture> logo;
|
||||
Ref<ImageTexture> run_icon;
|
||||
Ref<ImageTexture> stop_icon;
|
||||
Ref<ImageTexture> restart_icon;
|
||||
RemoteDebugState remote_debug_state = REMOTE_DEBUG_STATE_UNAVAILABLE;
|
||||
|
||||
Ref<EditorHTTPServer> server;
|
||||
|
||||
String _get_template_name(bool p_extension, bool p_thread_support, bool p_debug) const {
|
||||
String name = "web";
|
||||
if (p_extension) {
|
||||
name += "_dlink";
|
||||
}
|
||||
if (!p_thread_support) {
|
||||
name += "_nothreads";
|
||||
}
|
||||
if (p_debug) {
|
||||
name += "_debug.zip";
|
||||
} else {
|
||||
name += "_release.zip";
|
||||
}
|
||||
return name;
|
||||
}
|
||||
|
||||
Ref<Image> _get_project_icon(const Ref<EditorExportPreset> &p_preset) const {
|
||||
Error err = OK;
|
||||
Ref<Image> icon;
|
||||
icon.instantiate();
|
||||
const String icon_path = String(get_project_setting(p_preset, "application/config/icon")).strip_edges();
|
||||
if (!icon_path.is_empty()) {
|
||||
icon = _load_icon_or_splash_image(icon_path, &err);
|
||||
}
|
||||
if (icon_path.is_empty() || err != OK || icon.is_null() || icon->is_empty()) {
|
||||
return EditorNode::get_singleton()->get_editor_theme()->get_icon(SNAME("DefaultProjectIcon"), EditorStringName(EditorIcons))->get_image();
|
||||
}
|
||||
return icon;
|
||||
}
|
||||
|
||||
Ref<Image> _get_project_splash(const Ref<EditorExportPreset> &p_preset) const {
|
||||
Error err = OK;
|
||||
Ref<Image> splash;
|
||||
splash.instantiate();
|
||||
const String splash_path = String(get_project_setting(p_preset, "application/boot_splash/image")).strip_edges();
|
||||
if (!splash_path.is_empty()) {
|
||||
splash = _load_icon_or_splash_image(splash_path, &err);
|
||||
}
|
||||
if (splash_path.is_empty() || err != OK || splash.is_null() || splash->is_empty()) {
|
||||
return Ref<Image>(memnew(Image(boot_splash_png)));
|
||||
}
|
||||
return splash;
|
||||
}
|
||||
|
||||
Error _extract_template(const String &p_template, const String &p_dir, const String &p_name, bool pwa);
|
||||
void _replace_strings(const HashMap<String, String> &p_replaces, Vector<uint8_t> &r_template);
|
||||
void _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);
|
||||
Error _add_manifest_icon(const Ref<EditorExportPreset> &p_preset, const String &p_path, const String &p_icon, int p_size, Array &r_arr);
|
||||
Error _build_pwa(const Ref<EditorExportPreset> &p_preset, const String p_path, const Vector<SharedObject> &p_shared_objects);
|
||||
Error _write_or_error(const uint8_t *p_content, int p_len, String p_path);
|
||||
|
||||
Error _export_project(const Ref<EditorExportPreset> &p_preset, int p_debug_flags);
|
||||
Error _launch_browser(const String &p_bind_host, uint16_t p_bind_port, bool p_use_tls);
|
||||
Error _start_server(const String &p_bind_host, uint16_t p_bind_port, bool p_use_tls);
|
||||
Error _stop_server();
|
||||
|
||||
public:
|
||||
virtual void get_preset_features(const Ref<EditorExportPreset> &p_preset, List<String> *r_features) const override;
|
||||
|
||||
virtual void get_export_options(List<ExportOption> *r_options) const override;
|
||||
virtual bool get_export_option_visibility(const EditorExportPreset *p_preset, const String &p_option) const override;
|
||||
|
||||
virtual String get_name() const override;
|
||||
virtual String get_os_name() const override;
|
||||
virtual Ref<Texture2D> get_logo() const override;
|
||||
|
||||
virtual bool has_valid_export_configuration(const Ref<EditorExportPreset> &p_preset, String &r_error, bool &r_missing_templates, bool p_debug = false) const override;
|
||||
virtual bool has_valid_project_configuration(const Ref<EditorExportPreset> &p_preset, String &r_error) const override;
|
||||
virtual List<String> get_binary_extensions(const Ref<EditorExportPreset> &p_preset) const override;
|
||||
virtual Error export_project(const Ref<EditorExportPreset> &p_preset, bool p_debug, const String &p_path, BitField<EditorExportPlatform::DebugFlags> p_flags = 0) override;
|
||||
|
||||
virtual bool poll_export() override;
|
||||
virtual int get_options_count() const override;
|
||||
virtual String get_option_label(int p_index) const override;
|
||||
virtual String get_option_tooltip(int p_index) const override;
|
||||
virtual Ref<Texture2D> get_option_icon(int p_index) const override;
|
||||
virtual Error run(const Ref<EditorExportPreset> &p_preset, int p_option, BitField<EditorExportPlatform::DebugFlags> p_debug_flags) override;
|
||||
virtual Ref<Texture2D> get_run_icon() const override;
|
||||
|
||||
virtual void get_platform_features(List<String> *r_features) const override {
|
||||
r_features->push_back("web");
|
||||
r_features->push_back(get_os_name().to_lower());
|
||||
}
|
||||
|
||||
virtual void resolve_platform_feature_priorities(const Ref<EditorExportPreset> &p_preset, HashSet<String> &p_features) override {
|
||||
}
|
||||
|
||||
String get_debug_protocol() const override { return "ws://"; }
|
||||
|
||||
EditorExportPlatformWeb();
|
||||
~EditorExportPlatformWeb();
|
||||
};
|
1
platform/web/export/logo.svg
Normal file
1
platform/web/export/logo.svg
Normal file
@@ -0,0 +1 @@
|
||||
<svg xmlns="http://www.w3.org/2000/svg" width="32" height="32"><path fill="#fff" d="M7 5h18v21H7z"/><path fill="#eb6428" d="M3.143 1 5.48 27.504 15.967 31l10.553-3.496L28.857 1zM23.78 9.565H11.473l.275 3.308h11.759l-.911 9.937-6.556 1.808v.02h-.073l-6.61-1.828-.402-5.076h3.195l.234 2.552 3.583.97 3.595-.97.402-4.165H8.788L7.93 6.37h16.145z"/></svg>
|
After Width: | Height: | Size: 351 B |
1
platform/web/export/run_icon.svg
Normal file
1
platform/web/export/run_icon.svg
Normal file
@@ -0,0 +1 @@
|
||||
<svg xmlns="http://www.w3.org/2000/svg" xml:space="preserve" width="16" height="16"><path fill="#e0e0e0" d="m2 1 1.09 12.357 4.9 1.63 4.9-1.63L13.98 1zm9.622 3.994h-5.74l.129 1.541h5.482l-.424 4.634-3.057.843v.01h-.033l-3.082-.853-.187-2.367h1.489l.11 1.19 1.67.452 1.676-.453.187-1.942h-5.21l-.4-4.546h7.527z"/></svg>
|
After Width: | Height: | Size: 319 B |
Reference in New Issue
Block a user