feat: godot-engine-source-4.3-stable

This commit is contained in:
Jan van der Weide 2025-01-17 16:36:38 +01:00
parent c59a7dcade
commit 7125d019b5
11149 changed files with 5070401 additions and 0 deletions

View file

@ -0,0 +1,22 @@
# Web platform port
This folder contains the C++ and JavaScript code for the Web platform port,
compiled using [Emscripten](https://emscripten.org/).
It also contains a ESLint linting setup (see [`package.json`](package.json)).
See also [`misc/dist/html`](/misc/dist/html) folder for additional files used by
this platform such as the html shell (web page).
## Documentation
- [Compiling for the Web](https://docs.godotengine.org/en/latest/contributing/development/compiling/compiling_for_web.html)
- Instructions on building this platform port from source.
- [Exporting for the Web](https://docs.godotengine.org/en/latest/tutorials/export/exporting_for_web.html)
- Instructions on using the compiled export templates to export a project.
## Artwork license
[`logo.png`](logo.png) and [`run_icon.png`](run_icon.png) are licensed under
[Creative Commons Attribution 3.0 Unported](https://www.w3.org/html/logo/faq.html#how-licenced)
per the HTML5 logo usage guidelines.

112
engine/platform/web/SCsub Normal file
View file

@ -0,0 +1,112 @@
#!/usr/bin/env python
from methods import print_error
Import("env")
# The HTTP server "targets". Run with "scons p=web serve", or "scons p=web run"
if "serve" in COMMAND_LINE_TARGETS or "run" in COMMAND_LINE_TARGETS:
import os
from serve import serve
port = os.environ.get("GODOT_WEB_TEST_PORT", 8060)
try:
port = int(port)
except Exception:
print_error("GODOT_WEB_TEST_PORT must be a valid integer")
sys.exit(255)
serve(env.Dir(env.GetTemplateZipPath()).abspath, port, "run" in COMMAND_LINE_TARGETS)
sys.exit(0)
web_files = [
"audio_driver_web.cpp",
"display_server_web.cpp",
"http_client_web.cpp",
"javascript_bridge_singleton.cpp",
"web_main.cpp",
"os_web.cpp",
"api/web_tools_editor_plugin.cpp",
]
sys_env = env.Clone()
sys_env.AddJSLibraries(
[
"js/libs/library_godot_audio.js",
"js/libs/library_godot_display.js",
"js/libs/library_godot_fetch.js",
"js/libs/library_godot_os.js",
"js/libs/library_godot_runtime.js",
"js/libs/library_godot_input.js",
"js/libs/library_godot_webgl2.js",
]
)
sys_env.AddJSExterns(
[
"js/libs/library_godot_webgl2.externs.js",
]
)
if env["javascript_eval"]:
sys_env.AddJSLibraries(["js/libs/library_godot_javascript_singleton.js"])
for lib in sys_env["JS_LIBS"]:
sys_env.Append(LINKFLAGS=["--js-library", lib.abspath])
for js in sys_env["JS_PRE"]:
sys_env.Append(LINKFLAGS=["--pre-js", js.abspath])
for ext in sys_env["JS_EXTERNS"]:
sys_env["ENV"]["EMCC_CLOSURE_ARGS"] += " --externs " + ext.abspath
build = []
build_targets = ["#bin/godot${PROGSUFFIX}.js", "#bin/godot${PROGSUFFIX}.wasm", "#bin/godot${PROGSUFFIX}.worker.js"]
if env["dlink_enabled"]:
# Reset libraries. The main runtime will only link emscripten libraries, not godot ones.
sys_env["LIBS"] = []
# We use IDBFS. Since Emscripten 1.39.1 it needs to be linked explicitly.
sys_env.Append(LIBS=["idbfs.js"])
# Configure it as a main module (dynamic linking support).
sys_env["CCFLAGS"].remove("-sSIDE_MODULE=2")
sys_env["LINKFLAGS"].remove("-sSIDE_MODULE=2")
sys_env.Append(CCFLAGS=["-s", "MAIN_MODULE=1"])
sys_env.Append(LINKFLAGS=["-s", "MAIN_MODULE=1"])
sys_env.Append(LINKFLAGS=["-s", "EXPORT_ALL=1"])
sys_env.Append(LINKFLAGS=["-s", "WARN_ON_UNDEFINED_SYMBOLS=0"])
sys_env["CCFLAGS"].remove("-fvisibility=hidden")
sys_env["LINKFLAGS"].remove("-fvisibility=hidden")
# The main emscripten runtime, with exported standard libraries.
sys = sys_env.Program(build_targets, ["web_runtime.cpp"])
# The side library, containing all Godot code.
wasm = env.add_program("#bin/godot.side${PROGSUFFIX}.wasm", web_files)
build = sys + [wasm[0]]
else:
# We use IDBFS. Since Emscripten 1.39.1 it needs to be linked explicitly.
sys_env.Append(LIBS=["idbfs.js"])
build = sys_env.Program(build_targets, web_files + ["web_runtime.cpp"])
sys_env.Depends(build[0], sys_env["JS_LIBS"])
sys_env.Depends(build[0], sys_env["JS_PRE"])
sys_env.Depends(build[0], sys_env["JS_EXTERNS"])
engine = [
"js/engine/features.js",
"js/engine/preloader.js",
"js/engine/config.js",
"js/engine/engine.js",
]
externs = [env.File("#platform/web/js/engine/engine.externs.js")]
js_engine = env.CreateEngineFile("#bin/godot${PROGSUFFIX}.engine.js", engine, externs, env["threads"])
env.Depends(js_engine, externs)
wrap_list = [
build[0],
js_engine,
]
js_wrapped = env.Textfile("#bin/godot", [env.File(f) for f in wrap_list], TEXTFILESUFFIX="${PROGSUFFIX}.wrapped.js")
# 0 - unwrapped js file (use wrapped one instead)
# 1 - wasm file
# 2 - worker file
# 3 - wasm side (when dlink is enabled).
env.CreateTemplateZip(js_wrapped, build[1], build[2], build[3] if len(build) > 3 else None)

View file

@ -0,0 +1,129 @@
/**************************************************************************/
/* api.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 "api.h"
#include "javascript_bridge_singleton.h"
#include "web_tools_editor_plugin.h"
#include "core/config/engine.h"
static JavaScriptBridge *javascript_bridge_singleton;
void register_web_api() {
WebToolsEditorPlugin::initialize();
GDREGISTER_ABSTRACT_CLASS(JavaScriptObject);
GDREGISTER_ABSTRACT_CLASS(JavaScriptBridge);
javascript_bridge_singleton = memnew(JavaScriptBridge);
Engine::get_singleton()->add_singleton(Engine::Singleton("JavaScriptBridge", javascript_bridge_singleton));
}
void unregister_web_api() {
memdelete(javascript_bridge_singleton);
}
JavaScriptBridge *JavaScriptBridge::singleton = nullptr;
JavaScriptBridge *JavaScriptBridge::get_singleton() {
return singleton;
}
JavaScriptBridge::JavaScriptBridge() {
ERR_FAIL_COND_MSG(singleton != nullptr, "JavaScriptBridge singleton already exist.");
singleton = this;
}
JavaScriptBridge::~JavaScriptBridge() {}
void JavaScriptBridge::_bind_methods() {
ClassDB::bind_method(D_METHOD("eval", "code", "use_global_execution_context"), &JavaScriptBridge::eval, DEFVAL(false));
ClassDB::bind_method(D_METHOD("get_interface", "interface"), &JavaScriptBridge::get_interface);
ClassDB::bind_method(D_METHOD("create_callback", "callable"), &JavaScriptBridge::create_callback);
{
MethodInfo mi;
mi.name = "create_object";
mi.arguments.push_back(PropertyInfo(Variant::STRING, "object"));
ClassDB::bind_vararg_method(METHOD_FLAGS_DEFAULT, "create_object", &JavaScriptBridge::_create_object_bind, mi);
}
ClassDB::bind_method(D_METHOD("download_buffer", "buffer", "name", "mime"), &JavaScriptBridge::download_buffer, DEFVAL("application/octet-stream"));
ClassDB::bind_method(D_METHOD("pwa_needs_update"), &JavaScriptBridge::pwa_needs_update);
ClassDB::bind_method(D_METHOD("pwa_update"), &JavaScriptBridge::pwa_update);
ClassDB::bind_method(D_METHOD("force_fs_sync"), &JavaScriptBridge::force_fs_sync);
ADD_SIGNAL(MethodInfo("pwa_update_available"));
}
#if !defined(WEB_ENABLED) || !defined(JAVASCRIPT_EVAL_ENABLED)
Variant JavaScriptBridge::eval(const String &p_code, bool p_use_global_exec_context) {
return Variant();
}
Ref<JavaScriptObject> JavaScriptBridge::get_interface(const String &p_interface) {
return Ref<JavaScriptObject>();
}
Ref<JavaScriptObject> JavaScriptBridge::create_callback(const Callable &p_callable) {
return Ref<JavaScriptObject>();
}
Variant JavaScriptBridge::_create_object_bind(const Variant **p_args, int p_argcount, Callable::CallError &r_error) {
if (p_argcount < 1) {
r_error.error = Callable::CallError::CALL_ERROR_TOO_FEW_ARGUMENTS;
r_error.expected = 1;
return Ref<JavaScriptObject>();
}
if (p_args[0]->get_type() != Variant::STRING) {
r_error.error = Callable::CallError::CALL_ERROR_INVALID_ARGUMENT;
r_error.argument = 0;
r_error.expected = Variant::STRING;
return Ref<JavaScriptObject>();
}
return Ref<JavaScriptObject>();
}
#endif
#if !defined(WEB_ENABLED)
bool JavaScriptBridge::pwa_needs_update() const {
return false;
}
Error JavaScriptBridge::pwa_update() {
return ERR_UNAVAILABLE;
}
void JavaScriptBridge::force_fs_sync() {
}
void JavaScriptBridge::download_buffer(Vector<uint8_t> p_arr, const String &p_name, const String &p_mime) {
}
#endif

View file

@ -0,0 +1,37 @@
/**************************************************************************/
/* api.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. */
/**************************************************************************/
#ifndef WEB_API_H
#define WEB_API_H
void register_web_api();
void unregister_web_api();
#endif // WEB_API_H

View file

@ -0,0 +1,71 @@
/**************************************************************************/
/* javascript_bridge_singleton.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. */
/**************************************************************************/
#ifndef JAVASCRIPT_BRIDGE_SINGLETON_H
#define JAVASCRIPT_BRIDGE_SINGLETON_H
#include "core/object/class_db.h"
#include "core/object/ref_counted.h"
class JavaScriptObject : public RefCounted {
private:
GDCLASS(JavaScriptObject, RefCounted);
protected:
virtual bool _set(const StringName &p_name, const Variant &p_value) { return false; }
virtual bool _get(const StringName &p_name, Variant &r_ret) const { return false; }
virtual void _get_property_list(List<PropertyInfo> *p_list) const {}
};
class JavaScriptBridge : public Object {
private:
GDCLASS(JavaScriptBridge, Object);
static JavaScriptBridge *singleton;
protected:
static void _bind_methods();
public:
Variant eval(const String &p_code, bool p_use_global_exec_context = false);
Ref<JavaScriptObject> get_interface(const String &p_interface);
Ref<JavaScriptObject> create_callback(const Callable &p_callable);
Variant _create_object_bind(const Variant **p_args, int p_argcount, Callable::CallError &r_error);
void download_buffer(Vector<uint8_t> p_arr, const String &p_name, const String &p_mime = "application/octet-stream");
bool pwa_needs_update() const;
Error pwa_update();
void force_fs_sync();
static JavaScriptBridge *get_singleton();
JavaScriptBridge();
~JavaScriptBridge();
};
#endif // JAVASCRIPT_BRIDGE_SINGLETON_H

View file

@ -0,0 +1,159 @@
/**************************************************************************/
/* web_tools_editor_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 "web_tools_editor_plugin.h"
#if defined(TOOLS_ENABLED) && defined(WEB_ENABLED)
#include "core/config/engine.h"
#include "core/config/project_settings.h"
#include "core/io/dir_access.h"
#include "core/io/file_access.h"
#include "core/os/time.h"
#include "editor/editor_node.h"
#include <emscripten/emscripten.h>
// Web functions defined in library_godot_editor_tools.js
extern "C" {
extern void godot_js_os_download_buffer(const uint8_t *p_buf, int p_buf_size, const char *p_name, const char *p_mime);
}
static void _web_editor_init_callback() {
EditorNode::get_singleton()->add_editor_plugin(memnew(WebToolsEditorPlugin));
}
void WebToolsEditorPlugin::initialize() {
EditorNode::add_init_callback(_web_editor_init_callback);
}
WebToolsEditorPlugin::WebToolsEditorPlugin() {
add_tool_menu_item("Download Project Source", callable_mp(this, &WebToolsEditorPlugin::_download_zip));
}
void WebToolsEditorPlugin::_download_zip() {
if (!Engine::get_singleton() || !Engine::get_singleton()->is_editor_hint()) {
ERR_PRINT("Downloading the project as a ZIP archive is only available in Editor mode.");
return;
}
String resource_path = ProjectSettings::get_singleton()->get_resource_path();
Ref<FileAccess> io_fa;
zlib_filefunc_def io = zipio_create_io(&io_fa);
// Name the downloaded ZIP file to contain the project name and download date for easier organization.
// Replace characters not allowed (or risky) in Windows file names with safe characters.
// In the project name, all invalid characters become an empty string so that a name
// like "Platformer 2: Godette's Revenge" becomes "platformer_2-_godette-s_revenge".
const String project_name = GLOBAL_GET("application/config/name");
const String project_name_safe = project_name.to_lower().replace(" ", "_");
const String datetime_safe =
Time::get_singleton()->get_datetime_string_from_system(false, true).replace(" ", "_");
const String output_name = OS::get_singleton()->get_safe_dir_name(vformat("%s_%s.zip", project_name_safe, datetime_safe));
const String output_path = String("/tmp").path_join(output_name);
zipFile zip = zipOpen2(output_path.utf8().get_data(), APPEND_STATUS_CREATE, nullptr, &io);
const String base_path = resource_path.substr(0, resource_path.rfind("/")) + "/";
_zip_recursive(resource_path, base_path, zip);
zipClose(zip, nullptr);
{
Ref<FileAccess> f = FileAccess::open(output_path, FileAccess::READ);
ERR_FAIL_COND_MSG(f.is_null(), "Unable to create ZIP file.");
Vector<uint8_t> buf;
buf.resize(f->get_length());
f->get_buffer(buf.ptrw(), buf.size());
godot_js_os_download_buffer(buf.ptr(), buf.size(), output_name.utf8().get_data(), "application/zip");
}
// Remove the temporary file since it was sent to the user's native filesystem as a download.
DirAccess::remove_file_or_error(output_path);
}
void WebToolsEditorPlugin::_zip_file(String p_path, String p_base_path, zipFile p_zip) {
Ref<FileAccess> f = FileAccess::open(p_path, FileAccess::READ);
if (f.is_null()) {
WARN_PRINT("Unable to open file for zipping: " + p_path);
return;
}
Vector<uint8_t> data;
uint64_t len = f->get_length();
data.resize(len);
f->get_buffer(data.ptrw(), len);
String path = p_path.replace_first(p_base_path, "");
zipOpenNewFileInZip(p_zip,
path.utf8().get_data(),
nullptr,
nullptr,
0,
nullptr,
0,
nullptr,
Z_DEFLATED,
Z_DEFAULT_COMPRESSION);
zipWriteInFileInZip(p_zip, data.ptr(), data.size());
zipCloseFileInZip(p_zip);
}
void WebToolsEditorPlugin::_zip_recursive(String p_path, String p_base_path, zipFile p_zip) {
Ref<DirAccess> dir = DirAccess::open(p_path);
if (dir.is_null()) {
WARN_PRINT("Unable to open directory for zipping: " + p_path);
return;
}
dir->list_dir_begin();
String cur = dir->get_next();
String project_data_dir_name = ProjectSettings::get_singleton()->get_project_data_dir_name();
while (!cur.is_empty()) {
String cs = p_path.path_join(cur);
if (cur == "." || cur == ".." || cur == project_data_dir_name) {
// Skip
} else if (dir->current_is_dir()) {
String path = cs.replace_first(p_base_path, "") + "/";
zipOpenNewFileInZip(p_zip,
path.utf8().get_data(),
nullptr,
nullptr,
0,
nullptr,
0,
nullptr,
Z_DEFLATED,
Z_DEFAULT_COMPRESSION);
zipCloseFileInZip(p_zip);
_zip_recursive(cs, p_base_path, p_zip);
} else {
_zip_file(cs, p_base_path, p_zip);
}
cur = dir->get_next();
}
}
#endif // TOOLS_ENABLED && WEB_ENABLED

View file

@ -0,0 +1,62 @@
/**************************************************************************/
/* web_tools_editor_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. */
/**************************************************************************/
#ifndef WEB_TOOLS_EDITOR_PLUGIN_H
#define WEB_TOOLS_EDITOR_PLUGIN_H
#if defined(TOOLS_ENABLED) && defined(WEB_ENABLED)
#include "core/io/zip_io.h"
#include "editor/plugins/editor_plugin.h"
class WebToolsEditorPlugin : public EditorPlugin {
GDCLASS(WebToolsEditorPlugin, EditorPlugin);
private:
void _zip_file(String p_path, String p_base_path, zipFile p_zip);
void _zip_recursive(String p_path, String p_base_path, zipFile p_zip);
void _download_zip();
public:
static void initialize();
WebToolsEditorPlugin();
};
#else
class WebToolsEditorPlugin {
public:
static void initialize() {}
};
#endif // TOOLS_ENABLED && WEB_ENABLED
#endif // WEB_TOOLS_EDITOR_PLUGIN_H

View file

@ -0,0 +1,495 @@
/**************************************************************************/
/* audio_driver_web.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 "audio_driver_web.h"
#include "godot_audio.h"
#include "core/config/project_settings.h"
#include "core/object/object.h"
#include "scene/main/node.h"
#include "servers/audio/audio_stream.h"
#include <emscripten.h>
AudioDriverWeb::AudioContext AudioDriverWeb::audio_context;
bool AudioDriverWeb::is_available() {
return godot_audio_is_available() != 0;
}
void AudioDriverWeb::_state_change_callback(int p_state) {
AudioDriverWeb::audio_context.state = p_state;
}
void AudioDriverWeb::_latency_update_callback(float p_latency) {
AudioDriverWeb::audio_context.output_latency = p_latency;
}
void AudioDriverWeb::_sample_playback_finished_callback(const char *p_playback_object_id) {
const ObjectID playback_id = ObjectID(String::to_int(p_playback_object_id));
Object *playback_object = ObjectDB::get_instance(playback_id);
if (playback_object == nullptr) {
return;
}
Ref<AudioSamplePlayback> playback = Object::cast_to<AudioSamplePlayback>(playback_object);
if (playback.is_null()) {
return;
}
AudioServer::get_singleton()->stop_sample_playback(playback);
}
void AudioDriverWeb::_audio_driver_process(int p_from, int p_samples) {
int32_t *stream_buffer = reinterpret_cast<int32_t *>(output_rb);
const int max_samples = memarr_len(output_rb);
int write_pos = p_from;
int to_write = p_samples;
if (to_write == 0) {
to_write = max_samples;
}
// High part
if (write_pos + to_write > max_samples) {
const int samples_high = max_samples - write_pos;
audio_server_process(samples_high / channel_count, &stream_buffer[write_pos]);
for (int i = write_pos; i < max_samples; i++) {
output_rb[i] = float(stream_buffer[i] >> 16) / 32768.f;
}
to_write -= samples_high;
write_pos = 0;
}
// Leftover
audio_server_process(to_write / channel_count, &stream_buffer[write_pos]);
for (int i = write_pos; i < write_pos + to_write; i++) {
output_rb[i] = float(stream_buffer[i] >> 16) / 32768.f;
}
}
void AudioDriverWeb::_audio_driver_capture(int p_from, int p_samples) {
if (get_input_buffer().size() == 0) {
return; // Input capture stopped.
}
const int max_samples = memarr_len(input_rb);
int read_pos = p_from;
int to_read = p_samples;
if (to_read == 0) {
to_read = max_samples;
}
// High part
if (read_pos + to_read > max_samples) {
const int samples_high = max_samples - read_pos;
for (int i = read_pos; i < max_samples; i++) {
input_buffer_write(int32_t(input_rb[i] * 32768.f) * (1U << 16));
}
to_read -= samples_high;
read_pos = 0;
}
// Leftover
for (int i = read_pos; i < read_pos + to_read; i++) {
input_buffer_write(int32_t(input_rb[i] * 32768.f) * (1U << 16));
}
}
Error AudioDriverWeb::init() {
int latency = Engine::get_singleton()->get_audio_output_latency();
if (!audio_context.inited) {
audio_context.mix_rate = _get_configured_mix_rate();
audio_context.channel_count = godot_audio_init(&audio_context.mix_rate, latency, &_state_change_callback, &_latency_update_callback);
audio_context.inited = true;
}
mix_rate = audio_context.mix_rate;
channel_count = audio_context.channel_count;
buffer_length = closest_power_of_2((latency * mix_rate / 1000));
Error err = create(buffer_length, channel_count);
if (err != OK) {
return err;
}
if (output_rb) {
memdelete_arr(output_rb);
}
const size_t array_size = buffer_length * (size_t)channel_count;
output_rb = memnew_arr(float, array_size);
if (!output_rb) {
return ERR_OUT_OF_MEMORY;
}
if (input_rb) {
memdelete_arr(input_rb);
}
input_rb = memnew_arr(float, array_size);
if (!input_rb) {
return ERR_OUT_OF_MEMORY;
}
godot_audio_sample_set_finished_callback(&_sample_playback_finished_callback);
return OK;
}
void AudioDriverWeb::start() {
start(output_rb, memarr_len(output_rb), input_rb, memarr_len(input_rb));
}
void AudioDriverWeb::resume() {
if (audio_context.state == 0) { // 'suspended'
godot_audio_resume();
}
}
float AudioDriverWeb::get_latency() {
return audio_context.output_latency + (float(buffer_length) / mix_rate);
}
int AudioDriverWeb::get_mix_rate() const {
return mix_rate;
}
AudioDriver::SpeakerMode AudioDriverWeb::get_speaker_mode() const {
return get_speaker_mode_by_total_channels(channel_count);
}
void AudioDriverWeb::finish() {
finish_driver();
if (output_rb) {
memdelete_arr(output_rb);
output_rb = nullptr;
}
if (input_rb) {
memdelete_arr(input_rb);
input_rb = nullptr;
}
}
Error AudioDriverWeb::input_start() {
lock();
input_buffer_init(buffer_length);
unlock();
if (godot_audio_input_start()) {
return FAILED;
}
return OK;
}
Error AudioDriverWeb::input_stop() {
godot_audio_input_stop();
lock();
input_buffer.clear();
unlock();
return OK;
}
bool AudioDriverWeb::is_stream_registered_as_sample(const Ref<AudioStream> &p_stream) const {
ERR_FAIL_COND_V_MSG(p_stream.is_null(), false, "Parameter p_stream is null.");
return godot_audio_sample_stream_is_registered(itos(p_stream->get_instance_id()).utf8().get_data()) != 0;
}
void AudioDriverWeb::register_sample(const Ref<AudioSample> &p_sample) {
ERR_FAIL_COND_MSG(p_sample.is_null(), "Parameter p_sample is null.");
ERR_FAIL_COND_MSG(p_sample->stream.is_null(), "Parameter p_sample->stream is null.");
String loop_mode;
switch (p_sample->loop_mode) {
case AudioSample::LoopMode::LOOP_DISABLED: {
loop_mode = "disabled";
} break;
case AudioSample::LoopMode::LOOP_FORWARD: {
loop_mode = "forward";
} break;
case AudioSample::LoopMode::LOOP_PINGPONG: {
loop_mode = "pingpong";
} break;
case AudioSample::LoopMode::LOOP_BACKWARD: {
loop_mode = "backward";
} break;
}
double length = p_sample->stream->get_length();
Vector<AudioFrame> frames;
int frames_total = mix_rate * length;
{
Ref<AudioStreamPlayback> stream_playback = p_sample->stream->instantiate_playback();
frames.resize(frames_total);
AudioFrame *frames_ptr = frames.ptrw();
stream_playback->start();
stream_playback->mix(frames_ptr, 1.0f, frames_total);
}
PackedFloat32Array data;
data.resize(frames_total * 2);
float *data_ptrw = data.ptrw();
for (int i = 0; i < frames_total; i++) {
data_ptrw[i] = frames[i].left;
data_ptrw[i + frames_total] = frames[i].right;
}
godot_audio_sample_register_stream(
itos(p_sample->stream->get_instance_id()).utf8().get_data(),
data_ptrw,
frames_total,
loop_mode.utf8().get_data(),
p_sample->loop_begin,
p_sample->loop_end);
}
void AudioDriverWeb::unregister_sample(const Ref<AudioSample> &p_sample) {
ERR_FAIL_COND_MSG(p_sample.is_null(), "Parameter p_sample is null.");
ERR_FAIL_COND_MSG(p_sample->stream.is_null(), "Parameter p_sample->stream is null.");
godot_audio_sample_unregister_stream(itos(p_sample->stream->get_instance_id()).utf8().get_data());
}
void AudioDriverWeb::start_sample_playback(const Ref<AudioSamplePlayback> &p_playback) {
ERR_FAIL_COND_MSG(p_playback.is_null(), "Parameter p_playback is null.");
ERR_FAIL_COND_MSG(p_playback->stream.is_null(), "Parameter p_playback->stream is null.");
constexpr int real_max_channels = AudioServer::MAX_CHANNELS_PER_BUS * 2;
PackedFloat32Array volume;
volume.resize(real_max_channels);
float *volume_ptrw = volume.ptrw();
for (int i = 0; i < real_max_channels; i += 2) {
if (p_playback->volume_vector.is_empty()) {
volume_ptrw[i] = 0;
volume_ptrw[i + 1] = 0;
} else {
const AudioFrame &frame = p_playback->volume_vector[i / 2];
volume_ptrw[i] = frame.left;
volume_ptrw[i + 1] = frame.right;
}
}
godot_audio_sample_start(
itos(p_playback->get_instance_id()).utf8().get_data(),
itos(p_playback->stream->get_instance_id()).utf8().get_data(),
AudioServer::get_singleton()->get_bus_index(p_playback->bus),
p_playback->offset,
volume_ptrw);
}
void AudioDriverWeb::stop_sample_playback(const Ref<AudioSamplePlayback> &p_playback) {
ERR_FAIL_COND_MSG(p_playback.is_null(), "Parameter p_playback is null.");
godot_audio_sample_stop(itos(p_playback->get_instance_id()).utf8().get_data());
}
void AudioDriverWeb::set_sample_playback_pause(const Ref<AudioSamplePlayback> &p_playback, bool p_paused) {
ERR_FAIL_COND_MSG(p_playback.is_null(), "Parameter p_playback is null.");
godot_audio_sample_set_pause(itos(p_playback->get_instance_id()).utf8().get_data(), p_paused);
}
bool AudioDriverWeb::is_sample_playback_active(const Ref<AudioSamplePlayback> &p_playback) {
ERR_FAIL_COND_V_MSG(p_playback.is_null(), false, "Parameter p_playback is null.");
return godot_audio_sample_is_active(itos(p_playback->get_instance_id()).utf8().get_data()) != 0;
}
void AudioDriverWeb::update_sample_playback_pitch_scale(const Ref<AudioSamplePlayback> &p_playback, float p_pitch_scale) {
ERR_FAIL_COND_MSG(p_playback.is_null(), "Parameter p_playback is null.");
godot_audio_sample_update_pitch_scale(
itos(p_playback->get_instance_id()).utf8().get_data(),
p_pitch_scale);
}
void AudioDriverWeb::set_sample_playback_bus_volumes_linear(const Ref<AudioSamplePlayback> &p_playback, const HashMap<StringName, Vector<AudioFrame>> &p_bus_volumes) {
ERR_FAIL_COND_MSG(p_playback.is_null(), "Parameter p_playback is null.");
constexpr int real_max_channels = AudioServer::MAX_CHANNELS_PER_BUS * 2;
PackedInt32Array buses;
buses.resize(p_bus_volumes.size());
int32_t *buses_ptrw = buses.ptrw();
PackedFloat32Array values;
values.resize(p_bus_volumes.size() * AudioServer::MAX_CHANNELS_PER_BUS * 2);
float *values_ptrw = values.ptrw();
int idx = 0;
for (KeyValue<StringName, Vector<AudioFrame>> pair : p_bus_volumes) {
int bus_index = AudioServer::get_singleton()->get_bus_index(pair.key);
buses_ptrw[idx] = bus_index;
ERR_FAIL_COND(pair.value.size() != AudioServer::MAX_CHANNELS_PER_BUS);
for (int i = 0; i < real_max_channels; i += 2) {
const AudioFrame &frame = pair.value[i / 2];
values_ptrw[(idx * real_max_channels) + i] = frame.left;
values_ptrw[(idx * real_max_channels) + i + 1] = frame.right;
}
idx++;
}
godot_audio_sample_set_volumes_linear(
itos(p_playback->get_instance_id()).utf8().get_data(),
buses_ptrw,
buses.size(),
values_ptrw,
values.size());
}
void AudioDriverWeb::set_sample_bus_count(int p_count) {
godot_audio_sample_bus_set_count(p_count);
}
void AudioDriverWeb::remove_sample_bus(int p_index) {
godot_audio_sample_bus_remove(p_index);
}
void AudioDriverWeb::add_sample_bus(int p_at_pos) {
godot_audio_sample_bus_add(p_at_pos);
}
void AudioDriverWeb::move_sample_bus(int p_bus, int p_to_pos) {
godot_audio_sample_bus_move(p_bus, p_to_pos);
}
void AudioDriverWeb::set_sample_bus_send(int p_bus, const StringName &p_send) {
godot_audio_sample_bus_set_send(p_bus, AudioServer::get_singleton()->get_bus_index(p_send));
}
void AudioDriverWeb::set_sample_bus_volume_db(int p_bus, float p_volume_db) {
godot_audio_sample_bus_set_volume_db(p_bus, p_volume_db);
}
void AudioDriverWeb::set_sample_bus_solo(int p_bus, bool p_enable) {
godot_audio_sample_bus_set_solo(p_bus, p_enable);
}
void AudioDriverWeb::set_sample_bus_mute(int p_bus, bool p_enable) {
godot_audio_sample_bus_set_mute(p_bus, p_enable);
}
#ifdef THREADS_ENABLED
/// AudioWorkletNode implementation (threads)
void AudioDriverWorklet::_audio_thread_func(void *p_data) {
AudioDriverWorklet *driver = static_cast<AudioDriverWorklet *>(p_data);
const int out_samples = memarr_len(driver->get_output_rb());
const int in_samples = memarr_len(driver->get_input_rb());
int wpos = 0;
int to_write = out_samples;
int rpos = 0;
int to_read = 0;
int32_t step = 0;
while (!driver->quit) {
if (to_read) {
driver->lock();
driver->_audio_driver_capture(rpos, to_read);
godot_audio_worklet_state_add(driver->state, STATE_SAMPLES_IN, -to_read);
driver->unlock();
rpos += to_read;
if (rpos >= in_samples) {
rpos -= in_samples;
}
}
if (to_write) {
driver->lock();
driver->_audio_driver_process(wpos, to_write);
godot_audio_worklet_state_add(driver->state, STATE_SAMPLES_OUT, to_write);
driver->unlock();
wpos += to_write;
if (wpos >= out_samples) {
wpos -= out_samples;
}
}
step = godot_audio_worklet_state_wait(driver->state, STATE_PROCESS, step, 1);
to_write = out_samples - godot_audio_worklet_state_get(driver->state, STATE_SAMPLES_OUT);
to_read = godot_audio_worklet_state_get(driver->state, STATE_SAMPLES_IN);
}
}
Error AudioDriverWorklet::create(int &p_buffer_size, int p_channels) {
if (!godot_audio_has_worklet()) {
return ERR_UNAVAILABLE;
}
return (Error)godot_audio_worklet_create(p_channels);
}
void AudioDriverWorklet::start(float *p_out_buf, int p_out_buf_size, float *p_in_buf, int p_in_buf_size) {
godot_audio_worklet_start(p_in_buf, p_in_buf_size, p_out_buf, p_out_buf_size, state);
thread.start(_audio_thread_func, this);
}
void AudioDriverWorklet::lock() {
mutex.lock();
}
void AudioDriverWorklet::unlock() {
mutex.unlock();
}
void AudioDriverWorklet::finish_driver() {
quit = true; // Ask thread to quit.
thread.wait_to_finish();
}
#else // No threads.
/// AudioWorkletNode implementation (no threads)
AudioDriverWorklet *AudioDriverWorklet::singleton = nullptr;
Error AudioDriverWorklet::create(int &p_buffer_size, int p_channels) {
if (!godot_audio_has_worklet()) {
return ERR_UNAVAILABLE;
}
return (Error)godot_audio_worklet_create(p_channels);
}
void AudioDriverWorklet::start(float *p_out_buf, int p_out_buf_size, float *p_in_buf, int p_in_buf_size) {
_audio_driver_process();
godot_audio_worklet_start_no_threads(p_out_buf, p_out_buf_size, &_process_callback, p_in_buf, p_in_buf_size, &_capture_callback);
}
void AudioDriverWorklet::_process_callback(int p_pos, int p_samples) {
AudioDriverWorklet *driver = AudioDriverWorklet::get_singleton();
driver->_audio_driver_process(p_pos, p_samples);
}
void AudioDriverWorklet::_capture_callback(int p_pos, int p_samples) {
AudioDriverWorklet *driver = AudioDriverWorklet::get_singleton();
driver->_audio_driver_capture(p_pos, p_samples);
}
/// ScriptProcessorNode implementation
AudioDriverScriptProcessor *AudioDriverScriptProcessor::singleton = nullptr;
void AudioDriverScriptProcessor::_process_callback() {
AudioDriverScriptProcessor::get_singleton()->_audio_driver_capture();
AudioDriverScriptProcessor::get_singleton()->_audio_driver_process();
}
Error AudioDriverScriptProcessor::create(int &p_buffer_samples, int p_channels) {
if (!godot_audio_has_script_processor()) {
return ERR_UNAVAILABLE;
}
return (Error)godot_audio_script_create(&p_buffer_samples, p_channels);
}
void AudioDriverScriptProcessor::start(float *p_out_buf, int p_out_buf_size, float *p_in_buf, int p_in_buf_size) {
godot_audio_script_start(p_in_buf, p_in_buf_size, p_out_buf, p_out_buf_size, &_process_callback);
}
#endif // THREADS_ENABLED

View file

@ -0,0 +1,195 @@
/**************************************************************************/
/* audio_driver_web.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. */
/**************************************************************************/
#ifndef AUDIO_DRIVER_WEB_H
#define AUDIO_DRIVER_WEB_H
#include "godot_audio.h"
#include "godot_js.h"
#include "core/os/mutex.h"
#include "core/os/thread.h"
#include "servers/audio_server.h"
class AudioDriverWeb : public AudioDriver {
private:
struct AudioContext {
bool inited = false;
float output_latency = 0.0;
int state = -1;
int channel_count = 0;
int mix_rate = 0;
};
static AudioContext audio_context;
float *output_rb = nullptr;
float *input_rb = nullptr;
int buffer_length = 0;
int mix_rate = 0;
int channel_count = 0;
WASM_EXPORT static void _state_change_callback(int p_state);
WASM_EXPORT static void _latency_update_callback(float p_latency);
WASM_EXPORT static void _sample_playback_finished_callback(const char *p_playback_object_id);
static AudioDriverWeb *singleton;
protected:
void _audio_driver_process(int p_from = 0, int p_samples = 0);
void _audio_driver_capture(int p_from = 0, int p_samples = 0);
float *get_output_rb() const { return output_rb; }
float *get_input_rb() const { return input_rb; }
virtual Error create(int &p_buffer_samples, int p_channels) = 0;
virtual void start(float *p_out_buf, int p_out_buf_size, float *p_in_buf, int p_in_buf_size) = 0;
virtual void finish_driver() {}
public:
static bool is_available();
virtual Error init() final;
virtual void start() final;
virtual void finish() final;
virtual int get_mix_rate() const override;
virtual SpeakerMode get_speaker_mode() const override;
virtual float get_latency() override;
virtual Error input_start() override;
virtual Error input_stop() override;
static void resume();
// Samples.
virtual bool is_stream_registered_as_sample(const Ref<AudioStream> &p_stream) const override;
virtual void register_sample(const Ref<AudioSample> &p_sample) override;
virtual void unregister_sample(const Ref<AudioSample> &p_sample) override;
virtual void start_sample_playback(const Ref<AudioSamplePlayback> &p_playback) override;
virtual void stop_sample_playback(const Ref<AudioSamplePlayback> &p_playback) override;
virtual void set_sample_playback_pause(const Ref<AudioSamplePlayback> &p_playback, bool p_paused) override;
virtual bool is_sample_playback_active(const Ref<AudioSamplePlayback> &p_playback) override;
virtual void update_sample_playback_pitch_scale(const Ref<AudioSamplePlayback> &p_playback, float p_pitch_scale = 0.0f) override;
virtual void set_sample_playback_bus_volumes_linear(const Ref<AudioSamplePlayback> &p_playback, const HashMap<StringName, Vector<AudioFrame>> &p_bus_volumes) override;
virtual void set_sample_bus_count(int p_count) override;
virtual void remove_sample_bus(int p_index) override;
virtual void add_sample_bus(int p_at_pos = -1) override;
virtual void move_sample_bus(int p_bus, int p_to_pos) override;
virtual void set_sample_bus_send(int p_bus, const StringName &p_send) override;
virtual void set_sample_bus_volume_db(int p_bus, float p_volume_db) override;
virtual void set_sample_bus_solo(int p_bus, bool p_enable) override;
virtual void set_sample_bus_mute(int p_bus, bool p_enable) override;
AudioDriverWeb() {}
};
#ifdef THREADS_ENABLED
class AudioDriverWorklet : public AudioDriverWeb {
private:
enum {
STATE_LOCK,
STATE_PROCESS,
STATE_SAMPLES_IN,
STATE_SAMPLES_OUT,
STATE_MAX,
};
Mutex mutex;
Thread thread;
bool quit = false;
int32_t state[STATE_MAX] = { 0 };
static void _audio_thread_func(void *p_data);
protected:
virtual Error create(int &p_buffer_size, int p_output_channels) override;
virtual void start(float *p_out_buf, int p_out_buf_size, float *p_in_buf, int p_in_buf_size) override;
virtual void finish_driver() override;
public:
virtual const char *get_name() const override {
return "AudioWorklet";
}
virtual void lock() override;
virtual void unlock() override;
};
#else
class AudioDriverWorklet : public AudioDriverWeb {
private:
static void _process_callback(int p_pos, int p_samples);
static void _capture_callback(int p_pos, int p_samples);
static AudioDriverWorklet *singleton;
protected:
virtual Error create(int &p_buffer_size, int p_output_channels) override;
virtual void start(float *p_out_buf, int p_out_buf_size, float *p_in_buf, int p_in_buf_size) override;
public:
virtual const char *get_name() const override {
return "AudioWorklet";
}
virtual void lock() override {}
virtual void unlock() override {}
static AudioDriverWorklet *get_singleton() { return singleton; }
AudioDriverWorklet() { singleton = this; }
};
class AudioDriverScriptProcessor : public AudioDriverWeb {
private:
static void _process_callback();
static AudioDriverScriptProcessor *singleton;
protected:
virtual Error create(int &p_buffer_size, int p_output_channels) override;
virtual void start(float *p_out_buf, int p_out_buf_size, float *p_in_buf, int p_in_buf_size) override;
virtual void finish_driver() override;
public:
virtual const char *get_name() const override { return "ScriptProcessor"; }
virtual void lock() override {}
virtual void unlock() override {}
static AudioDriverScriptProcessor *get_singleton() { return singleton; }
AudioDriverScriptProcessor() { singleton = this; }
};
#endif // THREADS_ENABLED
#endif // AUDIO_DRIVER_WEB_H

View file

@ -0,0 +1,304 @@
import os
import sys
from typing import TYPE_CHECKING
from emscripten_helpers import (
add_js_externs,
add_js_libraries,
add_js_pre,
create_engine_file,
create_template_zip,
get_template_zip_path,
run_closure_compiler,
)
from SCons.Util import WhereIs
from methods import get_compiler_version, print_error, print_warning
if TYPE_CHECKING:
from SCons.Script.SConscript import SConsEnvironment
def get_name():
return "Web"
def can_build():
return WhereIs("emcc") is not None
def get_opts():
from SCons.Variables import BoolVariable
return [
("initial_memory", "Initial WASM memory (in MiB)", 32),
# Matches default values from before Emscripten 3.1.27. New defaults are too low for Godot.
("stack_size", "WASM stack size (in KiB)", 5120),
("default_pthread_stack_size", "WASM pthread default stack size (in KiB)", 2048),
BoolVariable("use_assertions", "Use Emscripten runtime assertions", False),
BoolVariable("use_ubsan", "Use Emscripten undefined behavior sanitizer (UBSAN)", False),
BoolVariable("use_asan", "Use Emscripten address sanitizer (ASAN)", False),
BoolVariable("use_lsan", "Use Emscripten leak sanitizer (LSAN)", False),
BoolVariable("use_safe_heap", "Use Emscripten SAFE_HEAP sanitizer", False),
# eval() can be a security concern, so it can be disabled.
BoolVariable("javascript_eval", "Enable JavaScript eval interface", True),
BoolVariable(
"dlink_enabled", "Enable WebAssembly dynamic linking (GDExtension support). Produces bigger binaries", False
),
BoolVariable("use_closure_compiler", "Use closure compiler to minimize JavaScript code", False),
BoolVariable(
"proxy_to_pthread",
"Use Emscripten PROXY_TO_PTHREAD option to run the main application code to a separate thread",
False,
),
]
def get_doc_classes():
return [
"EditorExportPlatformWeb",
]
def get_doc_path():
return "doc_classes"
def get_flags():
return {
"arch": "wasm32",
"target": "template_debug",
"builtin_pcre2_with_jit": False,
"vulkan": False,
# Embree is heavy and requires too much memory (GH-70621).
"module_raycast_enabled": False,
# Use -Os to prioritize optimizing for reduced file size. This is
# particularly valuable for the web platform because it directly
# decreases download time.
# -Os reduces file size by around 5 MiB over -O3. -Oz only saves about
# 100 KiB over -Os, which does not justify the negative impact on
# run-time performance.
# Note that this overrides the "auto" behavior for target/dev_build.
"optimize": "size",
}
def configure(env: "SConsEnvironment"):
# Validate arch.
supported_arches = ["wasm32"]
if env["arch"] not in supported_arches:
print_error(
'Unsupported CPU architecture "%s" for Web. Supported architectures are: %s.'
% (env["arch"], ", ".join(supported_arches))
)
sys.exit(255)
try:
env["initial_memory"] = int(env["initial_memory"])
except Exception:
print_error("Initial memory must be a valid integer")
sys.exit(255)
## Build type
if env.debug_features:
# Retain function names for backtraces at the cost of file size.
env.Append(LINKFLAGS=["--profiling-funcs"])
else:
env["use_assertions"] = True
if env["use_assertions"]:
env.Append(LINKFLAGS=["-sASSERTIONS=1"])
if env.editor_build and env["initial_memory"] < 64:
print("Note: Forcing `initial_memory=64` as it is required for the web editor.")
env["initial_memory"] = 64
env.Append(LINKFLAGS=["-sINITIAL_MEMORY=%sMB" % env["initial_memory"]])
## Copy env variables.
env["ENV"] = os.environ
# LTO
if env["lto"] == "auto": # Full LTO for production.
env["lto"] = "full"
if env["lto"] != "none":
if env["lto"] == "thin":
env.Append(CCFLAGS=["-flto=thin"])
env.Append(LINKFLAGS=["-flto=thin"])
else:
env.Append(CCFLAGS=["-flto"])
env.Append(LINKFLAGS=["-flto"])
# Sanitizers
if env["use_ubsan"]:
env.Append(CCFLAGS=["-fsanitize=undefined"])
env.Append(LINKFLAGS=["-fsanitize=undefined"])
if env["use_asan"]:
env.Append(CCFLAGS=["-fsanitize=address"])
env.Append(LINKFLAGS=["-fsanitize=address"])
if env["use_lsan"]:
env.Append(CCFLAGS=["-fsanitize=leak"])
env.Append(LINKFLAGS=["-fsanitize=leak"])
if env["use_safe_heap"]:
env.Append(LINKFLAGS=["-sSAFE_HEAP=1"])
# Closure compiler
if env["use_closure_compiler"]:
# For emscripten support code.
env.Append(LINKFLAGS=["--closure", "1"])
# Register builder for our Engine files
jscc = env.Builder(generator=run_closure_compiler, suffix=".cc.js", src_suffix=".js")
env.Append(BUILDERS={"BuildJS": jscc})
# Add helper method for adding libraries, externs, pre-js.
env["JS_LIBS"] = []
env["JS_PRE"] = []
env["JS_EXTERNS"] = []
env.AddMethod(add_js_libraries, "AddJSLibraries")
env.AddMethod(add_js_pre, "AddJSPre")
env.AddMethod(add_js_externs, "AddJSExterns")
# Add method that joins/compiles our Engine files.
env.AddMethod(create_engine_file, "CreateEngineFile")
# Add method for getting the final zip path
env.AddMethod(get_template_zip_path, "GetTemplateZipPath")
# Add method for creating the final zip file
env.AddMethod(create_template_zip, "CreateTemplateZip")
# Closure compiler extern and support for ecmascript specs (const, let, etc).
env["ENV"]["EMCC_CLOSURE_ARGS"] = "--language_in ECMASCRIPT_2021"
env["CC"] = "emcc"
env["CXX"] = "em++"
env["AR"] = "emar"
env["RANLIB"] = "emranlib"
# Use TempFileMunge since some AR invocations are too long for cmd.exe.
# Use POSIX-style paths, required with TempFileMunge.
env["ARCOM_POSIX"] = env["ARCOM"].replace("$TARGET", "$TARGET.posix").replace("$SOURCES", "$SOURCES.posix")
env["ARCOM"] = "${TEMPFILE('$ARCOM_POSIX','$ARCOMSTR')}"
# All intermediate files are just object files.
env["OBJPREFIX"] = ""
env["OBJSUFFIX"] = ".o"
env["PROGPREFIX"] = ""
# Program() output consists of multiple files, so specify suffixes manually at builder.
env["PROGSUFFIX"] = ""
env["LIBPREFIX"] = "lib"
env["LIBSUFFIX"] = ".a"
env["LIBPREFIXES"] = ["$LIBPREFIX"]
env["LIBSUFFIXES"] = ["$LIBSUFFIX"]
# Get version info for checks below.
cc_version = get_compiler_version(env)
cc_semver = (cc_version["major"], cc_version["minor"], cc_version["patch"])
env.Prepend(CPPPATH=["#platform/web"])
env.Append(CPPDEFINES=["WEB_ENABLED", "UNIX_ENABLED"])
if env["opengl3"]:
env.AppendUnique(CPPDEFINES=["GLES3_ENABLED"])
# This setting just makes WebGL 2 APIs available, it does NOT disable WebGL 1.
env.Append(LINKFLAGS=["-sMAX_WEBGL_VERSION=2"])
# Allow use to take control of swapping WebGL buffers.
env.Append(LINKFLAGS=["-sOFFSCREEN_FRAMEBUFFER=1"])
# Disables the use of *glGetProcAddress() which is inefficient.
# See https://emscripten.org/docs/tools_reference/settings_reference.html#gl-enable-get-proc-address
if cc_semver >= (3, 1, 51):
env.Append(LINKFLAGS=["-sGL_ENABLE_GET_PROC_ADDRESS=0"])
if env["javascript_eval"]:
env.Append(CPPDEFINES=["JAVASCRIPT_EVAL_ENABLED"])
stack_size_opt = "STACK_SIZE" if cc_semver >= (3, 1, 25) else "TOTAL_STACK"
env.Append(LINKFLAGS=["-s%s=%sKB" % (stack_size_opt, env["stack_size"])])
if env["threads"]:
# Thread support (via SharedArrayBuffer).
env.Append(CPPDEFINES=["PTHREAD_NO_RENAME"])
env.Append(CCFLAGS=["-sUSE_PTHREADS=1"])
env.Append(LINKFLAGS=["-sUSE_PTHREADS=1"])
env.Append(LINKFLAGS=["-sDEFAULT_PTHREAD_STACK_SIZE=%sKB" % env["default_pthread_stack_size"]])
env.Append(LINKFLAGS=["-sPTHREAD_POOL_SIZE=8"])
env.Append(LINKFLAGS=["-sWASM_MEM_MAX=2048MB"])
if not env["dlink_enabled"]:
# Workaround https://github.com/emscripten-core/emscripten/issues/21844#issuecomment-2116936414.
# Not needed (and potentially dangerous) when dlink_enabled=yes, since we set EXPORT_ALL=1 in that case.
env.Append(LINKFLAGS=["-sEXPORTED_FUNCTIONS=['__emscripten_thread_crashed','_main']"])
elif env["proxy_to_pthread"]:
print_warning('"threads=no" support requires "proxy_to_pthread=no", disabling proxy to pthread.')
env["proxy_to_pthread"] = False
if env["lto"] != "none":
# Workaround https://github.com/emscripten-core/emscripten/issues/19781.
if cc_semver >= (3, 1, 42) and cc_semver < (3, 1, 46):
env.Append(LINKFLAGS=["-Wl,-u,scalbnf"])
# Workaround https://github.com/emscripten-core/emscripten/issues/16836.
if cc_semver >= (3, 1, 47):
env.Append(LINKFLAGS=["-Wl,-u,_emscripten_run_callback_on_thread"])
if env["dlink_enabled"]:
if env["proxy_to_pthread"]:
print_warning("GDExtension support requires proxy_to_pthread=no, disabling proxy to pthread.")
env["proxy_to_pthread"] = False
if cc_semver < (3, 1, 14):
print_error("GDExtension support requires emscripten >= 3.1.14, detected: %s.%s.%s" % cc_semver)
sys.exit(255)
env.Append(CCFLAGS=["-sSIDE_MODULE=2"])
env.Append(LINKFLAGS=["-sSIDE_MODULE=2"])
env.Append(CCFLAGS=["-fvisibility=hidden"])
env.Append(LINKFLAGS=["-fvisibility=hidden"])
env.extra_suffix = ".dlink" + env.extra_suffix
# WASM_BIGINT is needed since emscripten ≥ 3.1.41
needs_wasm_bigint = cc_semver >= (3, 1, 41)
# Run the main application in a web worker
if env["proxy_to_pthread"]:
env.Append(LINKFLAGS=["-sPROXY_TO_PTHREAD=1"])
env.Append(CPPDEFINES=["PROXY_TO_PTHREAD_ENABLED"])
env.Append(LINKFLAGS=["-sEXPORTED_RUNTIME_METHODS=['_emscripten_proxy_main']"])
# https://github.com/emscripten-core/emscripten/issues/18034#issuecomment-1277561925
env.Append(LINKFLAGS=["-sTEXTDECODER=0"])
# BigInt support to pass object pointers between contexts
needs_wasm_bigint = True
if needs_wasm_bigint:
env.Append(LINKFLAGS=["-sWASM_BIGINT"])
# Reduce code size by generating less support code (e.g. skip NodeJS support).
env.Append(LINKFLAGS=["-sENVIRONMENT=web,worker"])
# Wrap the JavaScript support code around a closure named Godot.
env.Append(LINKFLAGS=["-sMODULARIZE=1", "-sEXPORT_NAME='Godot'"])
# Force long jump mode to 'wasm'
env.Append(CCFLAGS=["-sSUPPORT_LONGJMP='wasm'"])
env.Append(LINKFLAGS=["-sSUPPORT_LONGJMP='wasm'"])
# Allow increasing memory buffer size during runtime. This is efficient
# when using WebAssembly (in comparison to asm.js) and works well for
# us since we don't know requirements at compile-time.
env.Append(LINKFLAGS=["-sALLOW_MEMORY_GROWTH=1"])
# Do not call main immediately when the support code is ready.
env.Append(LINKFLAGS=["-sINVOKE_RUN=0"])
# callMain for manual start, cwrap for the mono version.
env.Append(LINKFLAGS=["-sEXPORTED_RUNTIME_METHODS=['callMain','cwrap']"])
# Add code that allow exiting runtime.
env.Append(LINKFLAGS=["-sEXIT_RUNTIME=1"])
# This workaround creates a closure that prevents the garbage collector from freeing the WebGL context.
# We also only use WebGL2, and changing context version is not widely supported anyway.
env.Append(LINKFLAGS=["-sGL_WORKAROUND_SAFARI_GETCONTEXT_BUG=0"])

File diff suppressed because it is too large Load diff

View file

@ -0,0 +1,285 @@
/**************************************************************************/
/* display_server_web.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. */
/**************************************************************************/
#ifndef DISPLAY_SERVER_WEB_H
#define DISPLAY_SERVER_WEB_H
#include "servers/display_server.h"
#include "godot_js.h"
#include <emscripten.h>
#include <emscripten/html5.h>
class DisplayServerWeb : public DisplayServer {
// No need to register with GDCLASS, it's platform-specific and nothing is added.
private:
struct JSTouchEvent {
uint32_t identifier[32] = { 0 };
double coords[64] = { 0 };
};
JSTouchEvent touch_event;
struct JSKeyEvent {
char code[32] = { 0 };
char key[32] = { 0 };
uint8_t modifiers[4] = { 0 };
};
JSKeyEvent key_event;
#ifdef GLES3_ENABLED
EMSCRIPTEN_WEBGL_CONTEXT_HANDLE webgl_ctx = 0;
#endif
HashMap<int, CharString> utterance_ids;
WindowMode window_mode = WINDOW_MODE_WINDOWED;
ObjectID window_attached_instance_id = {};
Callable rect_changed_callback;
Callable window_event_callback;
Callable input_event_callback;
Callable input_text_callback;
Callable drop_files_callback;
String clipboard;
Point2 touches[32];
Array voices;
char canvas_id[256] = { 0 };
bool cursor_inside_canvas = true;
CursorShape cursor_shape = CURSOR_ARROW;
Point2i last_click_pos = Point2(-100, -100); // TODO check this again.
uint64_t last_click_ms = 0;
MouseButton last_click_button_index = MouseButton::NONE;
bool ime_active = false;
bool ime_started = false;
String ime_text;
Vector2i ime_selection;
struct KeyEvent {
bool pressed = false;
bool echo = false;
bool raw = false;
Key keycode = Key::NONE;
Key physical_keycode = Key::NONE;
Key key_label = Key::NONE;
uint32_t unicode = 0;
KeyLocation location = KeyLocation::UNSPECIFIED;
int mod = 0;
};
Vector<KeyEvent> key_event_buffer;
int key_event_pos = 0;
bool swap_cancel_ok = false;
bool tts = false;
NativeMenu *native_menu = nullptr;
// utilities
static void dom2godot_mod(Ref<InputEventWithModifiers> ev, int p_mod, Key p_keycode);
static const char *godot2dom_cursor(DisplayServer::CursorShape p_shape);
// events
WASM_EXPORT static void fullscreen_change_callback(int p_fullscreen);
static void _fullscreen_change_callback(int p_fullscreen);
WASM_EXPORT static int mouse_button_callback(int p_pressed, int p_button, double p_x, double p_y, int p_modifiers);
static int _mouse_button_callback(int p_pressed, int p_button, double p_x, double p_y, int p_modifiers);
WASM_EXPORT static void mouse_move_callback(double p_x, double p_y, double p_rel_x, double p_rel_y, int p_modifiers);
static void _mouse_move_callback(double p_x, double p_y, double p_rel_x, double p_rel_y, int p_modifiers);
WASM_EXPORT static int mouse_wheel_callback(double p_delta_x, double p_delta_y);
static int _mouse_wheel_callback(double p_delta_x, double p_delta_y);
WASM_EXPORT static void touch_callback(int p_type, int p_count);
static void _touch_callback(int p_type, int p_count);
WASM_EXPORT static void key_callback(int p_pressed, int p_repeat, int p_modifiers);
static void _key_callback(const String &p_key_event_code, const String &p_key_event_key, int p_pressed, int p_repeat, int p_modifiers);
WASM_EXPORT static void vk_input_text_callback(const char *p_text, int p_cursor);
static void _vk_input_text_callback(const String &p_text, int p_cursor);
WASM_EXPORT static void gamepad_callback(int p_index, int p_connected, const char *p_id, const char *p_guid);
static void _gamepad_callback(int p_index, int p_connected, const String &p_id, const String &p_guid);
WASM_EXPORT static void js_utterance_callback(int p_event, int p_id, int p_pos);
static void _js_utterance_callback(int p_event, int p_id, int p_pos);
WASM_EXPORT static void ime_callback(int p_type, const char *p_text);
static void _ime_callback(int p_type, const String &p_text);
WASM_EXPORT static void request_quit_callback();
static void _request_quit_callback();
WASM_EXPORT static void window_blur_callback();
static void _window_blur_callback();
WASM_EXPORT static void update_voices_callback(int p_size, const char **p_voice);
static void _update_voices_callback(const Vector<String> &p_voices);
WASM_EXPORT static void update_clipboard_callback(const char *p_text);
static void _update_clipboard_callback(const String &p_text);
WASM_EXPORT static void send_window_event_callback(int p_notification);
static void _send_window_event_callback(int p_notification);
WASM_EXPORT static void drop_files_js_callback(const char **p_filev, int p_filec);
static void _drop_files_js_callback(const Vector<String> &p_files);
void process_joypads();
void process_keys();
static Vector<String> get_rendering_drivers_func();
static DisplayServer *create_func(const String &p_rendering_driver, WindowMode p_window_mode, VSyncMode p_vsync_mode, uint32_t p_flags, const Vector2i *p_position, const Vector2i &p_resolution, int p_screen, Context p_context, Error &r_error);
static void _dispatch_input_event(const Ref<InputEvent> &p_event);
protected:
int get_current_video_driver() const;
public:
// Override return type to make writing static callbacks less tedious.
static DisplayServerWeb *get_singleton();
// utilities
bool check_size_force_redraw();
// from DisplayServer
virtual bool has_feature(Feature p_feature) const override;
virtual String get_name() const override;
// tts
virtual bool tts_is_speaking() const override;
virtual bool tts_is_paused() const override;
virtual TypedArray<Dictionary> tts_get_voices() const override;
virtual void tts_speak(const String &p_text, const String &p_voice, int p_volume = 50, float p_pitch = 1.f, float p_rate = 1.f, int p_utterance_id = 0, bool p_interrupt = false) override;
virtual void tts_pause() override;
virtual void tts_resume() override;
virtual void tts_stop() override;
// cursor
virtual void cursor_set_shape(CursorShape p_shape) override;
virtual CursorShape cursor_get_shape() const override;
virtual void cursor_set_custom_image(const Ref<Resource> &p_cursor, CursorShape p_shape = CURSOR_ARROW, const Vector2 &p_hotspot = Vector2()) override;
// mouse
virtual void mouse_set_mode(MouseMode p_mode) override;
virtual MouseMode mouse_get_mode() const override;
virtual Point2i mouse_get_position() const override;
// ime
virtual void window_set_ime_active(const bool p_active, WindowID p_window = MAIN_WINDOW_ID) override;
virtual void window_set_ime_position(const Point2i &p_pos, WindowID p_window = MAIN_WINDOW_ID) override;
virtual Point2i ime_get_selection() const override;
virtual String ime_get_text() const override;
// touch
virtual bool is_touchscreen_available() const override;
// clipboard
virtual void clipboard_set(const String &p_text) override;
virtual String clipboard_get() const override;
// screen
virtual int get_screen_count() const override;
virtual int get_primary_screen() const override;
virtual Point2i screen_get_position(int p_screen = SCREEN_OF_MAIN_WINDOW) const override;
virtual Size2i screen_get_size(int p_screen = SCREEN_OF_MAIN_WINDOW) const override;
virtual Rect2i screen_get_usable_rect(int p_screen = SCREEN_OF_MAIN_WINDOW) const override;
virtual int screen_get_dpi(int p_screen = SCREEN_OF_MAIN_WINDOW) const override;
virtual float screen_get_scale(int p_screen = SCREEN_OF_MAIN_WINDOW) const override;
virtual float screen_get_refresh_rate(int p_screen = SCREEN_OF_MAIN_WINDOW) const override;
virtual void virtual_keyboard_show(const String &p_existing_text, const Rect2 &p_screen_rect = Rect2(), VirtualKeyboardType p_type = KEYBOARD_TYPE_DEFAULT, int p_max_input_length = -1, int p_cursor_start = -1, int p_cursor_end = -1) override;
virtual void virtual_keyboard_hide() override;
// windows
virtual Vector<DisplayServer::WindowID> get_window_list() const override;
virtual WindowID get_window_at_screen_position(const Point2i &p_position) const override;
virtual void window_attach_instance_id(ObjectID p_instance, WindowID p_window = MAIN_WINDOW_ID) override;
virtual ObjectID window_get_attached_instance_id(WindowID p_window = MAIN_WINDOW_ID) const override;
virtual void window_set_rect_changed_callback(const Callable &p_callable, WindowID p_window = MAIN_WINDOW_ID) override;
virtual void window_set_window_event_callback(const Callable &p_callable, WindowID p_window = MAIN_WINDOW_ID) override;
virtual void window_set_input_event_callback(const Callable &p_callable, WindowID p_window = MAIN_WINDOW_ID) override;
virtual void window_set_input_text_callback(const Callable &p_callable, WindowID p_window = MAIN_WINDOW_ID) override;
virtual void window_set_drop_files_callback(const Callable &p_callable, WindowID p_window = MAIN_WINDOW_ID) override;
virtual void window_set_title(const String &p_title, WindowID p_window = MAIN_WINDOW_ID) override;
virtual int window_get_current_screen(WindowID p_window = MAIN_WINDOW_ID) const override;
virtual void window_set_current_screen(int p_screen, WindowID p_window = MAIN_WINDOW_ID) override;
virtual Point2i window_get_position(WindowID p_window = MAIN_WINDOW_ID) const override;
virtual Point2i window_get_position_with_decorations(WindowID p_window = MAIN_WINDOW_ID) const override;
virtual void window_set_position(const Point2i &p_position, WindowID p_window = MAIN_WINDOW_ID) override;
virtual void window_set_transient(WindowID p_window, WindowID p_parent) override;
virtual void window_set_max_size(const Size2i p_size, WindowID p_window = MAIN_WINDOW_ID) override;
virtual Size2i window_get_max_size(WindowID p_window = MAIN_WINDOW_ID) const override;
virtual void window_set_min_size(const Size2i p_size, WindowID p_window = MAIN_WINDOW_ID) override;
virtual Size2i window_get_min_size(WindowID p_window = MAIN_WINDOW_ID) const override;
virtual void window_set_size(const Size2i p_size, WindowID p_window = MAIN_WINDOW_ID) override;
virtual Size2i window_get_size(WindowID p_window = MAIN_WINDOW_ID) const override;
virtual Size2i window_get_size_with_decorations(WindowID p_window = MAIN_WINDOW_ID) const override;
virtual void window_set_mode(WindowMode p_mode, WindowID p_window = MAIN_WINDOW_ID) override;
virtual WindowMode window_get_mode(WindowID p_window = MAIN_WINDOW_ID) const override;
virtual bool window_is_maximize_allowed(WindowID p_window = MAIN_WINDOW_ID) const override;
virtual void window_set_flag(WindowFlags p_flag, bool p_enabled, WindowID p_window = MAIN_WINDOW_ID) override;
virtual bool window_get_flag(WindowFlags p_flag, WindowID p_window = MAIN_WINDOW_ID) const override;
virtual void window_request_attention(WindowID p_window = MAIN_WINDOW_ID) override;
virtual void window_move_to_foreground(WindowID p_window = MAIN_WINDOW_ID) override;
virtual bool window_is_focused(WindowID p_window = MAIN_WINDOW_ID) const override;
virtual bool window_can_draw(WindowID p_window = MAIN_WINDOW_ID) const override;
virtual bool can_any_window_draw() const override;
virtual DisplayServer::VSyncMode window_get_vsync_mode(WindowID p_vsync_mode) const override;
// events
virtual void process_events() override;
// icon
virtual void set_icon(const Ref<Image> &p_icon) override;
// others
virtual bool get_swap_cancel_ok() override;
virtual void swap_buffers() override;
static void register_web_driver();
DisplayServerWeb(const String &p_rendering_driver, WindowMode p_window_mode, VSyncMode p_vsync_mode, uint32_t p_flags, const Point2i *p_position, const Size2i &p_resolution, int p_screen, Context p_context, Error &r_error);
~DisplayServerWeb();
};
#endif // DISPLAY_SERVER_WEB_H

View file

@ -0,0 +1,96 @@
<?xml version="1.0" encoding="UTF-8" ?>
<class name="EditorExportPlatformWeb" inherits="EditorExportPlatform" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="../class.xsd">
<brief_description>
Exporter for the Web.
</brief_description>
<description>
The Web exporter customizes how a web build is handled. In the editor's "Export" window, it is created when adding a new "Web" preset.
[b]Note:[/b] Godot on Web is rendered inside a [code]&lt;canvas&gt;[/code] tag. Normally, the canvas cannot be positioned or resized manually, but otherwise acts as the main [Window] of the application.
</description>
<tutorials>
<link title="Exporting for the Web">$DOCS_URL/tutorials/export/exporting_for_web.html</link>
<link title="Web documentation index">$DOCS_URL/tutorials/platform/web/index.html</link>
</tutorials>
<members>
<member name="custom_template/debug" type="String" setter="" getter="">
File path to the custom export template used for debug builds. If left empty, the default template is used.
</member>
<member name="custom_template/release" type="String" setter="" getter="">
File path to the custom export template used for release builds. If left empty, the default template is used.
</member>
<member name="html/canvas_resize_policy" type="int" setter="" getter="">
Determines how the canvas should be resized by Godot.
- [b]None:[/b] The canvas is not automatically resized.
- [b]Project:[/b] The size of the canvas is dependent on the [ProjectSettings].
- [b]Adaptive:[/b] The canvas is automatically resized to fit as much of the web page as possible.
</member>
<member name="html/custom_html_shell" type="String" setter="" getter="">
The custom HTML page that wraps the exported web build. If left empty, the default HTML shell is used.
For more information, see the [url=$DOCS_URL/tutorials/platform/web/customizing_html5_shell.html]Customizing HTML5 Shell[/url] tutorial.
</member>
<member name="html/experimental_virtual_keyboard" type="bool" setter="" getter="" experimental="">
If [code]true[/code], embeds support for a virtual keyboard into the web page, which is shown when necessary on touchscreen devices.
</member>
<member name="html/export_icon" type="bool" setter="" getter="">
If [code]true[/code], the project icon will be used as the favicon for this application's web page.
</member>
<member name="html/focus_canvas_on_start" type="bool" setter="" getter="">
If [code]true[/code], the canvas will be focused as soon as the application is loaded, if the browser window is already in focus.
</member>
<member name="html/head_include" type="String" setter="" getter="">
Additional HTML tags to include inside the [code]&lt;head&gt;[/code], such as [code]&lt;meta&gt;[/code] tags.
[b]Note:[/b] You do not need to add a [code]&lt;title&gt;[/code] tag, as it is automatically included based on the project's name.
</member>
<member name="progressive_web_app/background_color" type="Color" setter="" getter="">
The background color used behind the web application.
</member>
<member name="progressive_web_app/display" type="int" setter="" getter="">
The [url=https://developer.mozilla.org/en-US/docs/Web/Manifest/display/]display mode[/url] to use for this progressive web application. Different browsers and platforms may not behave the same.
- [b]Fullscreen:[/b] Displays the app in fullscreen and hides all of the browser's UI elements.
- [b]Standalone:[/b] Displays the app in a separate window and hides all of the browser's UI elements.
- [b]Minimal UI:[/b] Displays the app in a separate window and only shows the browser's UI elements for navigation.
- [b]Browser:[/b] Displays the app as a normal web page.
</member>
<member name="progressive_web_app/enabled" type="bool" setter="" getter="">
If [code]true[/code], turns this web build into a [url=https://en.wikipedia.org/wiki/Progressive_web_app]progressive web application[/url] (PWA).
</member>
<member name="progressive_web_app/ensure_cross_origin_isolation_headers" type="bool" setter="" getter="">
When enabled, the progressive web app will make sure that each request has cross-origin isolation headers (COEP/COOP).
This can simplify the setup to serve the exported game.
</member>
<member name="progressive_web_app/icon_144x144" type="String" setter="" getter="">
File path to the smallest icon for this web application. If not defined, defaults to the project icon.
[b]Note:[/b] If the icon is not 144x144, it will be automatically resized for the final build.
</member>
<member name="progressive_web_app/icon_180x180" type="String" setter="" getter="">
File path to the small icon for this web application. If not defined, defaults to the project icon.
[b]Note:[/b] If the icon is not 180x180, it will be automatically resized for the final build.
</member>
<member name="progressive_web_app/icon_512x512" type="String" setter="" getter="">
File path to the smallest icon for this web application. If not defined, defaults to the project icon.
[b]Note:[/b] If the icon is not 512x512, it will be automatically resized for the final build.
</member>
<member name="progressive_web_app/offline_page" type="String" setter="" getter="">
The page to display, should the server hosting the page not be available. This page is saved in the client's machine.
</member>
<member name="progressive_web_app/orientation" type="int" setter="" getter="">
The orientation to use when the web application is run through a mobile device.
- [b]Any:[/b] No orientation is forced.
- [b]Landscape:[/b] Forces a horizontal layout (wider than it is taller).
- [b]Portrait:[/b] Forces a vertical layout (taller than it is wider).
</member>
<member name="variant/extensions_support" type="bool" setter="" getter="">
If [code]true[/code] enables [GDExtension] support for this web build.
</member>
<member name="variant/thread_support" type="bool" setter="" getter="">
If [code]true[/code], the exported game will support threads. It requires [url=https://web.dev/articles/coop-coep]a "cross-origin isolated" website[/url], which may be difficult to set up and is limited for security reasons (such as not being able to communicate with third-party websites).
If [code]false[/code], the exported game will not support threads. As a result, it is more prone to performance and audio issues, but will only require to be run on an HTTPS website.
</member>
<member name="vram_texture_compression/for_desktop" type="bool" setter="" getter="">
If [code]true[/code], allows textures to be optimized for desktop through the S3TC algorithm.
</member>
<member name="vram_texture_compression/for_mobile" type="bool" setter="" getter="">
If [code]true[/code] allows textures to be optimized for mobile through the ETC2 algorithm.
</member>
</members>
</class>

View file

@ -0,0 +1,246 @@
/**************************************************************************/
/* dom_keys.inc */
/**************************************************************************/
/* 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 "core/os/keyboard.h"
// See https://w3c.github.io/uievents-code/#code-value-tables
Key dom_code2godot_scancode(EM_UTF8 const p_code[32], EM_UTF8 const p_key[32], bool p_physical) {
#define DOM2GODOT(p_str, p_godot_code) \
if (memcmp((const void *)p_str, (void *)(p_physical ? p_code : p_key), strlen(p_str) + 1) == 0) { \
return Key::p_godot_code; \
}
// Numpad section.
DOM2GODOT("NumLock", NUMLOCK);
DOM2GODOT("Numpad0", KP_0);
DOM2GODOT("Numpad1", KP_1);
DOM2GODOT("Numpad2", KP_2);
DOM2GODOT("Numpad3", KP_3);
DOM2GODOT("Numpad4", KP_4);
DOM2GODOT("Numpad5", KP_5);
DOM2GODOT("Numpad6", KP_6);
DOM2GODOT("Numpad7", KP_7);
DOM2GODOT("Numpad8", KP_8);
DOM2GODOT("Numpad9", KP_9);
DOM2GODOT("NumpadAdd", KP_ADD);
DOM2GODOT("NumpadBackspace", BACKSPACE);
DOM2GODOT("Clear", CLEAR); // NumLock on macOS.
DOM2GODOT("NumpadClear", CLEAR);
DOM2GODOT("NumpadClearEntry", CLEAR);
//DOM2GODOT("NumpadComma", UNKNOWN);
DOM2GODOT("NumpadDecimal", KP_PERIOD);
DOM2GODOT("NumpadDivide", KP_DIVIDE);
DOM2GODOT("NumpadEnter", KP_ENTER);
DOM2GODOT("NumpadEqual", EQUAL);
//DOM2GODOT("NumpadHash", UNKNOWN);
//DOM2GODOT("NumpadMemoryAdd", UNKNOWN);
//DOM2GODOT("NumpadMemoryClear", UNKNOWN);
//DOM2GODOT("NumpadMemoryRecall", UNKNOWN);
//DOM2GODOT("NumpadMemoryStore", UNKNOWN);
//DOM2GODOT("NumpadMemorySubtract", UNKNOWN);
DOM2GODOT("NumpadMultiply", KP_MULTIPLY);
DOM2GODOT("NumpadParenLeft", PARENLEFT);
DOM2GODOT("NumpadParenRight", PARENRIGHT);
DOM2GODOT("NumpadStar", KP_MULTIPLY); // or ASTERISK ?
DOM2GODOT("NumpadSubtract", KP_SUBTRACT);
// Alphanumeric section.
DOM2GODOT("Backquote", QUOTELEFT);
DOM2GODOT("Backslash", BACKSLASH);
DOM2GODOT("BracketLeft", BRACKETLEFT);
DOM2GODOT("BracketRight", BRACKETRIGHT);
DOM2GODOT("Comma", COMMA);
DOM2GODOT("Digit0", KEY_0);
DOM2GODOT("Digit1", KEY_1);
DOM2GODOT("Digit2", KEY_2);
DOM2GODOT("Digit3", KEY_3);
DOM2GODOT("Digit4", KEY_4);
DOM2GODOT("Digit5", KEY_5);
DOM2GODOT("Digit6", KEY_6);
DOM2GODOT("Digit7", KEY_7);
DOM2GODOT("Digit8", KEY_8);
DOM2GODOT("Digit9", KEY_9);
DOM2GODOT("Equal", EQUAL);
DOM2GODOT("IntlBackslash", BACKSLASH);
//DOM2GODOT("IntlRo", UNKNOWN);
DOM2GODOT("IntlYen", YEN);
DOM2GODOT("KeyA", A);
DOM2GODOT("KeyB", B);
DOM2GODOT("KeyC", C);
DOM2GODOT("KeyD", D);
DOM2GODOT("KeyE", E);
DOM2GODOT("KeyF", F);
DOM2GODOT("KeyG", G);
DOM2GODOT("KeyH", H);
DOM2GODOT("KeyI", I);
DOM2GODOT("KeyJ", J);
DOM2GODOT("KeyK", K);
DOM2GODOT("KeyL", L);
DOM2GODOT("KeyM", M);
DOM2GODOT("KeyN", N);
DOM2GODOT("KeyO", O);
DOM2GODOT("KeyP", P);
DOM2GODOT("KeyQ", Q);
DOM2GODOT("KeyR", R);
DOM2GODOT("KeyS", S);
DOM2GODOT("KeyT", T);
DOM2GODOT("KeyU", U);
DOM2GODOT("KeyV", V);
DOM2GODOT("KeyW", W);
DOM2GODOT("KeyX", X);
DOM2GODOT("KeyY", Y);
DOM2GODOT("KeyZ", Z);
DOM2GODOT("Minus", MINUS);
DOM2GODOT("Period", PERIOD);
DOM2GODOT("Quote", APOSTROPHE);
DOM2GODOT("Semicolon", SEMICOLON);
DOM2GODOT("Slash", SLASH);
// Functional keys in the Alphanumeric section.
DOM2GODOT("Alt", ALT);
DOM2GODOT("AltLeft", ALT);
DOM2GODOT("AltRight", ALT);
DOM2GODOT("Backspace", BACKSPACE);
DOM2GODOT("CapsLock", CAPSLOCK);
DOM2GODOT("ContextMenu", MENU);
DOM2GODOT("Control", CTRL);
DOM2GODOT("ControlLeft", CTRL);
DOM2GODOT("ControlRight", CTRL);
DOM2GODOT("Enter", ENTER);
DOM2GODOT("Meta", META);
DOM2GODOT("MetaLeft", META);
DOM2GODOT("MetaRight", META);
DOM2GODOT("OSLeft", META); // Command on macOS.
DOM2GODOT("OSRight", META); // Command on macOS.
DOM2GODOT("Shift", SHIFT);
DOM2GODOT("ShiftLeft", SHIFT);
DOM2GODOT("ShiftRight", SHIFT);
DOM2GODOT("Space", SPACE);
DOM2GODOT("Tab", TAB);
// ControlPad section.
DOM2GODOT("Delete", KEY_DELETE);
DOM2GODOT("End", END);
DOM2GODOT("Help", HELP);
DOM2GODOT("Home", HOME);
DOM2GODOT("Insert", INSERT);
DOM2GODOT("PageDown", PAGEDOWN);
DOM2GODOT("PageUp", PAGEUP);
// ArrowPad section.
DOM2GODOT("ArrowDown", DOWN);
DOM2GODOT("ArrowLeft", LEFT);
DOM2GODOT("ArrowRight", RIGHT);
DOM2GODOT("ArrowUp", UP);
// Function section.
DOM2GODOT("Escape", ESCAPE);
DOM2GODOT("F1", F1);
DOM2GODOT("F2", F2);
DOM2GODOT("F3", F3);
DOM2GODOT("F4", F4);
DOM2GODOT("F5", F5);
DOM2GODOT("F6", F6);
DOM2GODOT("F7", F7);
DOM2GODOT("F8", F8);
DOM2GODOT("F9", F9);
DOM2GODOT("F10", F10);
DOM2GODOT("F11", F11);
DOM2GODOT("F12", F12);
//DOM2GODOT("Fn", UNKNOWN); // never actually fired, but included in the standard draft.
//DOM2GODOT("FnLock", UNKNOWN);
DOM2GODOT("PrintScreen", PRINT);
DOM2GODOT("ScrollLock", SCROLLLOCK);
DOM2GODOT("Pause", PAUSE);
// Media keys section.
DOM2GODOT("BrowserBack", BACK);
DOM2GODOT("BrowserFavorites", FAVORITES);
DOM2GODOT("BrowserForward", FORWARD);
DOM2GODOT("BrowserHome", OPENURL);
DOM2GODOT("BrowserRefresh", REFRESH);
DOM2GODOT("BrowserSearch", SEARCH);
DOM2GODOT("BrowserStop", STOP);
//DOM2GODOT("Eject", UNKNOWN);
DOM2GODOT("LaunchApp1", LAUNCH0);
DOM2GODOT("LaunchApp2", LAUNCH1);
DOM2GODOT("LaunchMail", LAUNCHMAIL);
DOM2GODOT("MediaPlayPause", MEDIAPLAY);
DOM2GODOT("MediaSelect", LAUNCHMEDIA);
DOM2GODOT("MediaStop", MEDIASTOP);
DOM2GODOT("MediaTrackNext", MEDIANEXT);
DOM2GODOT("MediaTrackPrevious", MEDIAPREVIOUS);
//DOM2GODOT("Power", UNKNOWN);
//DOM2GODOT("Sleep", UNKNOWN);
DOM2GODOT("AudioVolumeDown", VOLUMEDOWN);
DOM2GODOT("AudioVolumeMute", VOLUMEMUTE);
DOM2GODOT("AudioVolumeUp", VOLUMEUP);
//DOM2GODOT("WakeUp", UNKNOWN);
// Printable ASCII.
uint8_t b0 = (uint8_t)p_key[0];
uint8_t b1 = (uint8_t)p_key[1];
if (b0 >= 0x20 && b0 < 0x7F) { // ASCII.
if (b0 > 0x60 && b0 < 0x7B) { // Lowercase ASCII.
b0 -= 32;
}
return (Key)b0;
} else if (b0 == 0xC2 && b1 == 0xA5) {
return Key::YEN;
} else if (b0 == 0xC2 && b1 == 0xA7) {
return Key::SECTION;
}
return Key::NONE;
#undef DOM2GODOT
}
KeyLocation dom_code2godot_key_location(EM_UTF8 const p_code[32]) {
#define DOM2GODOT(m_str, m_godot_code) \
if (memcmp((const void *)m_str, (void *)p_code, strlen(m_str) + 1) == 0) { \
return KeyLocation::m_godot_code; \
}
DOM2GODOT("AltLeft", LEFT);
DOM2GODOT("AltRight", RIGHT);
DOM2GODOT("ControlLeft", LEFT);
DOM2GODOT("ControlRight", RIGHT);
DOM2GODOT("MetaLeft", LEFT);
DOM2GODOT("MetaRight", RIGHT);
DOM2GODOT("OSLeft", LEFT);
DOM2GODOT("OSRight", RIGHT);
DOM2GODOT("ShiftLeft", LEFT);
DOM2GODOT("ShiftRight", RIGHT);
return KeyLocation::UNSPECIFIED;
#undef DOM2GODOT
}

View file

@ -0,0 +1,142 @@
import json
import os
from SCons.Util import WhereIs
def run_closure_compiler(target, source, env, for_signature):
closure_bin = os.path.join(
os.path.dirname(WhereIs("emcc")),
"node_modules",
".bin",
"google-closure-compiler",
)
cmd = [WhereIs("node"), closure_bin]
cmd.extend(["--compilation_level", "ADVANCED_OPTIMIZATIONS"])
for f in env["JSEXTERNS"]:
cmd.extend(["--externs", f.get_abspath()])
for f in source:
cmd.extend(["--js", f.get_abspath()])
cmd.extend(["--js_output_file", target[0].get_abspath()])
return " ".join(cmd)
def get_build_version():
import version
name = "custom_build"
if os.getenv("BUILD_NAME") is not None:
name = os.getenv("BUILD_NAME")
v = "%d.%d" % (version.major, version.minor)
if version.patch > 0:
v += ".%d" % version.patch
status = version.status
if os.getenv("GODOT_VERSION_STATUS") is not None:
status = str(os.getenv("GODOT_VERSION_STATUS"))
v += ".%s.%s" % (status, name)
return v
def create_engine_file(env, target, source, externs, threads_enabled):
if env["use_closure_compiler"]:
return env.BuildJS(target, source, JSEXTERNS=externs)
subst_dict = {"___GODOT_THREADS_ENABLED": "true" if threads_enabled else "false"}
return env.Substfile(target=target, source=[env.File(s) for s in source], SUBST_DICT=subst_dict)
def create_template_zip(env, js, wasm, worker, side):
binary_name = "godot.editor" if env.editor_build else "godot"
zip_dir = env.Dir(env.GetTemplateZipPath())
in_files = [
js,
wasm,
"#platform/web/js/libs/audio.worklet.js",
]
out_files = [
zip_dir.File(binary_name + ".js"),
zip_dir.File(binary_name + ".wasm"),
zip_dir.File(binary_name + ".audio.worklet.js"),
]
if env["threads"]:
in_files.append(worker)
out_files.append(zip_dir.File(binary_name + ".worker.js"))
# Dynamic linking (extensions) specific.
if env["dlink_enabled"]:
in_files.append(side) # Side wasm (contains the actual Godot code).
out_files.append(zip_dir.File(binary_name + ".side.wasm"))
service_worker = "#misc/dist/html/service-worker.js"
if env.editor_build:
# HTML
html = "#misc/dist/html/editor.html"
cache = [
"godot.editor.html",
"offline.html",
"godot.editor.js",
"godot.editor.audio.worklet.js",
"logo.svg",
"favicon.png",
]
if env["threads"]:
cache.append("godot.editor.worker.js")
opt_cache = ["godot.editor.wasm"]
subst_dict = {
"___GODOT_VERSION___": get_build_version(),
"___GODOT_NAME___": "GodotEngine",
"___GODOT_CACHE___": json.dumps(cache),
"___GODOT_OPT_CACHE___": json.dumps(opt_cache),
"___GODOT_OFFLINE_PAGE___": "offline.html",
"___GODOT_THREADS_ENABLED___": "true" if env["threads"] else "false",
}
html = env.Substfile(target="#bin/godot${PROGSUFFIX}.html", source=html, SUBST_DICT=subst_dict)
in_files.append(html)
out_files.append(zip_dir.File(binary_name + ".html"))
# And logo/favicon
in_files.append("#misc/dist/html/logo.svg")
out_files.append(zip_dir.File("logo.svg"))
in_files.append("#icon.png")
out_files.append(zip_dir.File("favicon.png"))
# PWA
service_worker = env.Substfile(
target="#bin/godot${PROGSUFFIX}.service.worker.js",
source=service_worker,
SUBST_DICT=subst_dict,
)
in_files.append(service_worker)
out_files.append(zip_dir.File("service.worker.js"))
in_files.append("#misc/dist/html/manifest.json")
out_files.append(zip_dir.File("manifest.json"))
in_files.append("#misc/dist/html/offline.html")
out_files.append(zip_dir.File("offline.html"))
else:
# HTML
in_files.append("#misc/dist/html/full-size.html")
out_files.append(zip_dir.File(binary_name + ".html"))
in_files.append(service_worker)
out_files.append(zip_dir.File(binary_name + ".service.worker.js"))
in_files.append("#misc/dist/html/offline-export.html")
out_files.append(zip_dir.File("godot.offline.html"))
zip_files = env.InstallAs(out_files, in_files)
env.Zip(
"#bin/godot",
zip_files,
ZIPROOT=zip_dir,
ZIPSUFFIX="${PROGSUFFIX}${ZIPSUFFIX}",
)
def get_template_zip_path(env):
return "#bin/.web_zip"
def add_js_libraries(env, libraries):
env.Append(JS_LIBS=env.File(libraries))
def add_js_pre(env, js_pre):
env.Append(JS_PRE=env.File(js_pre))
def add_js_externs(env, externs):
env.Append(JS_EXTERNS=env.File(externs))

View file

@ -0,0 +1,205 @@
const fs = require('fs');
const globals = require('globals');
const htmlParser = require('@html-eslint/parser');
const htmlPlugin = require('@html-eslint/eslint-plugin');
const pluginJs = require('@eslint/js');
const pluginReference = require('eslint-plugin-html');
const stylistic = require('@stylistic/eslint-plugin');
if (process && process.env && process.env.npm_command && !fs.existsSync('./platform/web/eslint.config.cjs')) {
throw Error('eslint must be run from the Godot project root folder');
}
const emscriptenGlobals = {
'Browser': true,
'ERRNO_CODES': true,
'FS': true,
'GL': true,
'HEAP32': true,
'HEAP8': true,
'HEAPF32': true,
'HEAPU8': true,
'IDBFS': true,
'LibraryManager': true,
'Module': true,
'UTF8ToString': true,
'_emscripten_webgl_get_current_context': true,
'_free': true,
'_malloc': true,
'autoAddDeps': true,
'getValue': true,
'lengthBytesUTF8': true,
'mergeInto': true,
'runtimeKeepalivePop': true,
'runtimeKeepalivePush': true,
'setValue': true,
'stringToUTF8': true,
'stringToUTF8Array': true,
'wasmTable': true,
};
module.exports = [
pluginJs.configs.all,
stylistic.configs.customize({ jsx: false }),
{
rules: {
'consistent-this': ['error', 'me'], // enforce consistent naming when capturing the current execution context
'curly': ['error', 'all'], // enforce consistent brace style for all control statements
'no-else-return': ['error', { 'allowElseIf': true }], // disallow else blocks after return statements in if statements
'no-param-reassign': ['error', { 'props': false }], // disallow reassigning function parameters
'no-unused-vars': ['error', { 'args': 'none', 'caughtErrors': 'none' }], // disallow unused variables
'@stylistic/arrow-parens': ['error', 'always'], // enforces the consistent use of parentheses in arrow functions
'@stylistic/brace-style': ['error', '1tbs', { 'allowSingleLine': false }], // describes the placement of braces relative to their control statement and body
'@stylistic/comma-dangle': ['error', {
'arrays': 'always-multiline',
'objects': 'always-multiline',
'imports': 'always-multiline',
'exports': 'always-multiline',
'functions': 'never',
}], // enforces consistent use of trailing commas in object and array literals
'@stylistic/indent': ['error', 'tab', { 'SwitchCase': 0 }], // enforces a consistent indentation style
'@stylistic/indent-binary-ops': ['error', 'tab'], // indentation for binary operators in multiline expressions
'@stylistic/multiline-ternary': ['error', 'always-multiline'], // enforces or disallows newlines between operands of a ternary expression
'@stylistic/no-tabs': ['error', { 'allowIndentationTabs': true }], // looks for tabs anywhere inside a file: code, comments or anything else
'@stylistic/quote-props': ['error', 'consistent'], // requires quotes around object literal property names
'@stylistic/quotes': ['error', 'single'], // enforces the consistent use of either backticks, double, or single quotes
'@stylistic/semi': ['error', 'always'], // enforces consistent use of semicolons
'@stylistic/spaced-comment': ['error', 'always', { 'block': { 'exceptions': ['*'] } }], // enforce consistency of spacing after the start of a comment
'camelcase': 'off', // disable: camelcase naming convention
'capitalized-comments': 'off', // disable: enforce or disallow capitalization of the first letter of a comment
'complexity': 'off', // disable: enforce a maximum cyclomatic complexity allowed in a program
'dot-notation': 'off', // disable: enforce dot notation whenever possible
'eqeqeq': 'off', // disable: require the use of === and !==
'func-name-matching': 'off', // disable: require function names to match the name of the variable or property to which they are assigned
'func-names': 'off', // disable: checking named function expressions
'func-style': 'off', // disable: consistent use of either function declarations or expressions
'id-length': 'off', // disable: enforce minimum and maximum identifier lengths
'init-declarations': 'off', // disable: require or disallow initialization in variable declarations
'line-comment-position': 'off', // disable: enforce position of line comments
'max-classes-per-file': 'off', // disable: maximum number of classes per file
'max-lines': 'off', // disable: maximum number of lines per file
'max-lines-per-function': 'off', // disable: maximum number of lines of code in a function
'max-params': 'off', // disable: enforce a maximum number of parameters in function definitions
'max-statements': 'off', // disable: maximum number of statements allowed in function blocks
'multiline-comment-style': 'off', // disable: enforce a particular style for multiline comments
'new-cap': 'off', // disable: require constructor names to begin with a capital letter
'no-bitwise': 'off', // disable: disallow bitwise operators
'no-continue': 'off', // disable: disallow continue statements
'no-empty-function': 'off', // disable: disallow empty functions
'no-eq-null': 'off', // disable: disallow null comparisons without type-checking operators
'no-implicit-coercion': 'off', // disable: disallow shorthand type conversions
'no-inline-comments': 'off', // disable: disallow inline comments after code
'no-magic-numbers': 'off', // disable: disallow magic numbers
'no-negated-condition': 'off', // disable: disallow negated conditions
'no-plusplus': 'off', // disable: disallow the unary operators ++ and --
'no-self-assign': 'off', // disable: disallow assignments where both sides are exactly the same
'no-ternary': 'off', // disable: disallow ternary operators
'no-undefined': 'off', // disable: disallow the use of undefined as an identifier
'no-underscore-dangle': 'off', // disable: disallow dangling underscores in identifiers
'no-useless-assignment': 'off', // disable: disallow variable assignments when the value is not used
'no-warning-comments': 'off', // disable: disallow specified warning terms in comments
'object-shorthand': 'off', // disable: require or disallow method and property shorthand syntax for object literals
'one-var': 'off', // disable: enforce variables to be declared either together or separately in functions
'prefer-arrow-callback': 'off', // disable: require using arrow functions for callbacks
'prefer-destructuring': 'off', // disable: require destructuring from arrays and/or objects
'prefer-named-capture-group': 'off', // disable: enforce using named capture group in regular expression
'prefer-promise-reject-errors': 'off', // disable: require using Error objects as Promise rejection reasons
'prefer-rest-params': 'off', // disable: require rest parameters instead of arguments
'prefer-spread': 'off', // disable: require spread operators instead of .apply()
'require-unicode-regexp': 'off', // disable: enforce the use of u or v flag on RegExp
'sort-keys': 'off', // disable: require object keys to be sorted
},
},
// jsdoc2rst (node)
{
files: ['js/jsdoc2rst/**/*.js', 'platform/web/js/jsdoc2rst/**/*.js'],
languageOptions: {
globals: globals.node,
},
},
// engine files (browser)
{
files: ['js/engine/**/*.js', 'platform/web/js/engine/**/*.js'],
languageOptions: {
globals: {
...globals.browser,
'Features': true,
'Godot': true,
'InternalConfig': true,
'Preloader': true,
},
},
},
// libraries and modules (browser)
{
files: ['js/libs/**/*.js', 'platform/web/js/libs/**/*.js', 'modules/**/*.js'],
languageOptions: {
globals: {
...globals.browser,
...emscriptenGlobals,
'GodotConfig': true,
'GodotEventListeners': true,
'GodotFS': true,
'GodotOS': true,
'GodotAudio': true,
'GodotRuntime': true,
'IDHandler': true,
'XRWebGLLayer': true,
},
},
},
// javascript templates (service workers)
{
files: ['misc/dist/html/**/*.js'],
languageOptions: {
globals: {
...globals.browser,
'___GODOT_CACHE___': true,
'___GODOT_ENSURE_CROSSORIGIN_ISOLATION_HEADERS___': true,
'___GODOT_OPT_CACHE___': true,
},
},
},
// html templates
{
files: ['misc/dist/html/**/*.html'],
plugins: {
'@html-eslint': htmlPlugin,
'eslint-plugin-html': pluginReference,
},
languageOptions: {
parser: htmlParser,
globals: {
...globals.browser,
'Engine': true,
'$GODOT_CONFIG': true,
'$GODOT_PROJECT_NAME': true,
'$GODOT_THREADS_ENABLED': true,
'___GODOT_THREADS_ENABLED___': true,
},
},
rules: {
...htmlPlugin.configs.recommended.rules,
'@html-eslint/indent': ['error', 'tab'],
'@html-eslint/require-closing-tags': ['error', { 'selfClosing': 'never' }],
'no-alert': 'off',
'no-console': 'off',
},
},
{
ignores: [
'**/eslint.config.cjs',
'**/.eslintrc*.js',
'**/*.externs.js',
],
},
];

View 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_cache_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();
}

View file

@ -0,0 +1,74 @@
/**************************************************************************/
/* 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. */
/**************************************************************************/
#ifndef WEB_EDITOR_HTTP_SERVER_H
#define WEB_EDITOR_HTTP_SERVER_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_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;
};
#endif // WEB_EDITOR_HTTP_SERVER_H

View file

@ -0,0 +1,57 @@
/**************************************************************************/
/* 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/editor_settings.h"
#include "editor/export/editor_export.h"
void register_web_exporter_types() {
GDREGISTER_VIRTUAL_CLASS(EditorExportPlatformWeb);
}
void register_web_exporter() {
#ifndef ANDROID_ENABLED
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"));
#endif
Ref<EditorExportPlatformWeb> platform;
platform.instantiate();
EditorExport::get_singleton()->add_export_platform(platform);
}

View file

@ -0,0 +1,37 @@
/**************************************************************************/
/* 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. */
/**************************************************************************/
#ifndef WEB_EXPORT_H
#define WEB_EXPORT_H
void register_web_exporter_types();
void register_web_exporter();
#endif // WEB_EXPORT_H

View file

@ -0,0 +1,913 @@
/**************************************************************************/
/* 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_settings.h"
#include "editor/editor_string_names.h"
#include "editor/export/editor_export.h"
#include "editor/import/resource_importer_texture_settings.h"
#include "editor/themes/editor_scale.h"
#include "scene/resources/image_texture.h"
#include "modules/modules_enabled.gen.h" // For mono and svg.
#ifdef MODULE_SVG_ENABLED
#include "modules/svg/image_loader_svg.h"
#endif
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, int 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(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");
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"] = GLOBAL_GET("application/config/name");
replaces["$GODOT_HEAD_INCLUDE"] = head_include + custom_head_include;
replaces["$GODOT_CONFIG"] = str_config;
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 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()) {
icon.instantiate();
const Error err = ImageLoader::load_image(p_icon, icon);
if (err != OK) {
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();
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 = GLOBAL_GET("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;
cache_files.push_back(name + ".html");
cache_files.push_back(name + ".js");
cache_files.push_back(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 + ".worker.js");
cache_files.push_back(name + ".audio.worklet.js");
replaces["___GODOT_CACHE___"] = Variant(cache_files).to_json_string();
// Heavy files that are cached on demand.
Array opt_cache_files;
opt_cache_files.push_back(name + ".wasm");
opt_cache_files.push_back(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_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_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_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");
}
if (p_preset->get("vram_texture_compression/for_mobile")) {
r_features->push_back("etc2");
}
if (p_preset->get("variant/thread_support").operator bool()) {
r_features->push_back("threads");
} else {
r_features->push_back("nothreads");
}
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)); // 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()));
}
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, int 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();
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();
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<ImageTexture> EditorExportPlatformWeb::get_option_icon(int p_index) const {
Ref<ImageTexture> 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, int 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_cache_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 + ".worker.js");
DirAccess::remove_file_or_error(basepath + ".audio.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();
#ifdef MODULE_SVG_ENABLED
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);
#endif
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() {
}

View file

@ -0,0 +1,147 @@
/**************************************************************************/
/* 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. */
/**************************************************************************/
#ifndef WEB_EXPORT_PLUGIN_H
#define WEB_EXPORT_PLUGIN_H
#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<Image> icon;
icon.instantiate();
const String icon_path = String(GLOBAL_GET("application/config/icon")).strip_edges();
if (icon_path.is_empty() || ImageLoader::load_image(icon_path, icon) != OK) {
return EditorNode::get_singleton()->get_editor_theme()->get_icon(SNAME("DefaultProjectIcon"), EditorStringName(EditorIcons))->get_image();
}
return icon;
}
Ref<Image> _get_project_splash() const {
Ref<Image> splash;
splash.instantiate();
const String splash_path = String(GLOBAL_GET("application/boot_splash/image")).strip_edges();
if (splash_path.is_empty() || ImageLoader::load_image(splash_path, splash) != OK) {
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, int p_flags, const Vector<SharedObject> p_shared_objects, const Dictionary &p_file_sizes);
Error _add_manifest_icon(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 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, int 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<ImageTexture> get_option_icon(int p_index) const override;
virtual Error run(const Ref<EditorExportPreset> &p_preset, int p_option, int 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();
};
#endif // WEB_EXPORT_PLUGIN_H

View 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: 350 B

View 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: 318 B

View file

@ -0,0 +1,88 @@
/**************************************************************************/
/* godot_audio.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. */
/**************************************************************************/
#ifndef GODOT_AUDIO_H
#define GODOT_AUDIO_H
#ifdef __cplusplus
extern "C" {
#endif
#include <stddef.h>
#include <stdint.h>
extern int godot_audio_is_available();
extern int godot_audio_has_worklet();
extern int godot_audio_has_script_processor();
extern int godot_audio_init(int *p_mix_rate, int p_latency, void (*_state_cb)(int), void (*_latency_cb)(float));
extern void godot_audio_resume();
extern int godot_audio_input_start();
extern void godot_audio_input_stop();
// Samples
extern int godot_audio_sample_stream_is_registered(const char *p_stream_object_id);
extern void godot_audio_sample_register_stream(const char *p_stream_object_id, float *p_frames_buf, int p_frames_total, const char *p_loop_mode, int p_loop_begin, int p_loop_end);
extern void godot_audio_sample_unregister_stream(const char *p_stream_object_id);
extern void godot_audio_sample_start(const char *p_playback_object_id, const char *p_stream_object_id, int p_bus_index, float p_offset, float *p_volume_ptr);
extern void godot_audio_sample_stop(const char *p_playback_object_id);
extern void godot_audio_sample_set_pause(const char *p_playback_object_id, bool p_pause);
extern int godot_audio_sample_is_active(const char *p_playback_object_id);
extern void godot_audio_sample_update_pitch_scale(const char *p_playback_object_id, float p_pitch_scale);
extern void godot_audio_sample_set_volumes_linear(const char *p_playback_object_id, int *p_buses_buf, int p_buses_size, float *p_volumes_buf, int p_volumes_size);
extern void godot_audio_sample_set_finished_callback(void (*p_callback)(const char *));
extern void godot_audio_sample_bus_set_count(int p_count);
extern void godot_audio_sample_bus_remove(int p_index);
extern void godot_audio_sample_bus_add(int p_at_pos = -1);
extern void godot_audio_sample_bus_move(int p_bus, int p_to_pos);
extern void godot_audio_sample_bus_set_send(int p_bus, int p_send_index);
extern void godot_audio_sample_bus_set_volume_db(int p_bus, float p_volume_db);
extern void godot_audio_sample_bus_set_solo(int p_bus, bool p_enable);
extern void godot_audio_sample_bus_set_mute(int p_bus, bool p_enable);
// Worklet
typedef int32_t GodotAudioState[4];
extern int godot_audio_worklet_create(int p_channels);
extern void godot_audio_worklet_start(float *p_in_buf, int p_in_size, float *p_out_buf, int p_out_size, GodotAudioState p_state);
extern void godot_audio_worklet_start_no_threads(float *p_out_buf, int p_out_size, void (*p_out_cb)(int p_pos, int p_frames), float *p_in_buf, int p_in_size, void (*p_in_cb)(int p_pos, int p_frames));
extern int godot_audio_worklet_state_add(GodotAudioState p_state, int p_idx, int p_value);
extern int godot_audio_worklet_state_get(GodotAudioState p_state, int p_idx);
extern int godot_audio_worklet_state_wait(int32_t *p_state, int p_idx, int32_t p_expected, int p_timeout);
// Script
extern int godot_audio_script_create(int *p_buffer_size, int p_channels);
extern void godot_audio_script_start(float *p_in_buf, int p_in_size, float *p_out_buf, int p_out_size, void (*p_cb)());
#ifdef __cplusplus
}
#endif
#endif // GODOT_AUDIO_H

View file

@ -0,0 +1,140 @@
/**************************************************************************/
/* godot_js.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. */
/**************************************************************************/
#ifndef GODOT_JS_H
#define GODOT_JS_H
#define WASM_EXPORT __attribute__((visibility("default")))
#ifdef __cplusplus
extern "C" {
#endif
#include <stddef.h>
#include <stdint.h>
// Config
extern void godot_js_config_locale_get(char *p_ptr, int p_ptr_max);
extern void godot_js_config_canvas_id_get(char *p_ptr, int p_ptr_max);
// OS
extern void godot_js_os_finish_async(void (*p_callback)());
extern void godot_js_os_request_quit_cb(void (*p_callback)());
extern int godot_js_os_fs_is_persistent();
extern void godot_js_os_fs_sync(void (*p_callback)());
extern int godot_js_os_execute(const char *p_json);
extern void godot_js_os_shell_open(const char *p_uri);
extern int godot_js_os_hw_concurrency_get();
extern int godot_js_os_has_feature(const char *p_ftr);
extern int godot_js_pwa_cb(void (*p_callback)());
extern int godot_js_pwa_update();
// Input
extern void godot_js_input_mouse_button_cb(int (*p_callback)(int p_pressed, int p_button, double p_x, double p_y, int p_modifiers));
extern void godot_js_input_mouse_move_cb(void (*p_callback)(double p_x, double p_y, double p_rel_x, double p_rel_y, int p_modifiers));
extern void godot_js_input_mouse_wheel_cb(int (*p_callback)(double p_delta_x, double p_delta_y));
extern void godot_js_input_touch_cb(void (*p_callback)(int p_type, int p_count), uint32_t *r_identifiers, double *r_coords);
extern void godot_js_input_key_cb(void (*p_callback)(int p_type, int p_repeat, int p_modifiers), char r_code[32], char r_key[32]);
extern void godot_js_input_vibrate_handheld(int p_duration_ms);
extern void godot_js_set_ime_active(int p_active);
extern void godot_js_set_ime_position(int p_x, int p_y);
extern void godot_js_set_ime_cb(void (*p_input)(int p_type, const char *p_text), void (*p_callback)(int p_type, int p_repeat, int p_modifiers), char r_code[32], char r_key[32]);
extern int godot_js_is_ime_focused();
// Input gamepad
extern void godot_js_input_gamepad_cb(void (*p_on_change)(int p_index, int p_connected, const char *p_id, const char *p_guid));
extern int godot_js_input_gamepad_sample();
extern int godot_js_input_gamepad_sample_count();
extern int godot_js_input_gamepad_sample_get(int p_idx, float r_btns[16], int32_t *r_btns_num, float r_axes[10], int32_t *r_axes_num, int32_t *r_standard);
extern void godot_js_input_paste_cb(void (*p_callback)(const char *p_text));
extern void godot_js_input_drop_files_cb(void (*p_callback)(const char **p_filev, int p_filec));
// TTS
extern int godot_js_tts_is_speaking();
extern int godot_js_tts_is_paused();
extern int godot_js_tts_get_voices(void (*p_callback)(int p_size, const char **p_voices));
extern void godot_js_tts_speak(const char *p_text, const char *p_voice, int p_volume, float p_pitch, float p_rate, int p_utterance_id, void (*p_callback)(int p_event, int p_id, int p_pos));
extern void godot_js_tts_pause();
extern void godot_js_tts_resume();
extern void godot_js_tts_stop();
// Display
extern int godot_js_display_screen_dpi_get();
extern double godot_js_display_pixel_ratio_get();
extern void godot_js_display_alert(const char *p_text);
extern int godot_js_display_touchscreen_is_available();
extern int godot_js_display_is_swap_ok_cancel();
extern void godot_js_display_setup_canvas(int p_width, int p_height, int p_fullscreen, int p_hidpi);
// Display canvas
extern void godot_js_display_canvas_focus();
extern int godot_js_display_canvas_is_focused();
// Display window
extern void godot_js_display_desired_size_set(int p_width, int p_height);
extern int godot_js_display_size_update();
extern void godot_js_display_window_size_get(int32_t *p_x, int32_t *p_y);
extern void godot_js_display_screen_size_get(int32_t *p_x, int32_t *p_y);
extern int godot_js_display_fullscreen_request();
extern int godot_js_display_fullscreen_exit();
extern void godot_js_display_window_title_set(const char *p_text);
extern void godot_js_display_window_icon_set(const uint8_t *p_ptr, int p_len);
extern int godot_js_display_has_webgl(int p_version);
// Display clipboard
extern int godot_js_display_clipboard_set(const char *p_text);
extern int godot_js_display_clipboard_get(void (*p_callback)(const char *p_text));
// Display cursor
extern void godot_js_display_cursor_set_shape(const char *p_cursor);
extern int godot_js_display_cursor_is_hidden();
extern void godot_js_display_cursor_set_custom_shape(const char *p_shape, const uint8_t *p_ptr, int p_len, int p_hotspot_x, int p_hotspot_y);
extern void godot_js_display_cursor_set_visible(int p_visible);
extern void godot_js_display_cursor_lock_set(int p_lock);
extern int godot_js_display_cursor_is_locked();
// Display listeners
extern void godot_js_display_fullscreen_cb(void (*p_callback)(int p_fullscreen));
extern void godot_js_display_window_blur_cb(void (*p_callback)());
extern void godot_js_display_notification_cb(void (*p_callback)(int p_notification), int p_enter, int p_exit, int p_in, int p_out);
// Display Virtual Keyboard
extern int godot_js_display_vk_available();
extern int godot_js_display_tts_available();
extern void godot_js_display_vk_cb(void (*p_input)(const char *p_text, int p_cursor));
extern void godot_js_display_vk_show(const char *p_text, int p_type, int p_start, int p_end);
extern void godot_js_display_vk_hide();
#ifdef __cplusplus
}
#endif
#endif // GODOT_JS_H

View file

@ -0,0 +1,57 @@
/**************************************************************************/
/* godot_webgl2.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. */
/**************************************************************************/
#ifndef GODOT_WEBGL2_H
#define GODOT_WEBGL2_H
#include <GLES3/gl3.h>
#include <webgl/webgl2.h>
#define GL_FRAMEBUFFER_ATTACHMENT_TEXTURE_NUM_VIEWS_OVR 0x9630
#define GL_FRAMEBUFFER_ATTACHMENT_TEXTURE_BASE_VIEW_INDEX_OVR 0x9632
#define GL_MAX_VIEWS_OVR 0x9631
#define GL_FRAMEBUFFER_INCOMPLETE_VIEW_TARGETS_OVR 0x9633
#ifdef __cplusplus
extern "C" {
#endif
void godot_webgl2_glFramebufferTextureMultiviewOVR(GLenum target, GLenum attachment, GLuint texture, GLint level, GLint baseViewIndex, GLsizei numViews);
void godot_webgl2_glFramebufferTextureMultisampleMultiviewOVR(GLenum target, GLenum attachment, GLuint texture, GLint level, GLsizei samples, GLint baseViewIndex, GLsizei numViews);
void godot_webgl2_glGetBufferSubData(GLenum target, GLintptr offset, GLsizeiptr size, GLvoid *data);
#define glFramebufferTextureMultiviewOVR godot_webgl2_glFramebufferTextureMultiviewOVR
#define glFramebufferTextureMultisampleMultiviewOVR godot_webgl2_glFramebufferTextureMultisampleMultiviewOVR
#ifdef __cplusplus
}
#endif
#endif // GODOT_WEBGL2_H

View file

@ -0,0 +1,280 @@
/**************************************************************************/
/* http_client_web.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 "http_client_web.h"
void HTTPClientWeb::_parse_headers(int p_len, const char **p_headers, void *p_ref) {
HTTPClientWeb *client = static_cast<HTTPClientWeb *>(p_ref);
for (int i = 0; i < p_len; i++) {
client->response_headers.push_back(String::utf8(p_headers[i]));
}
}
Error HTTPClientWeb::connect_to_host(const String &p_host, int p_port, Ref<TLSOptions> p_tls_options) {
ERR_FAIL_COND_V(p_tls_options.is_valid() && p_tls_options->is_server(), ERR_INVALID_PARAMETER);
close();
port = p_port;
use_tls = p_tls_options.is_valid();
host = p_host;
String host_lower = host.to_lower();
if (host_lower.begins_with("http://")) {
host = host.substr(7, host.length() - 7);
use_tls = false;
} else if (host_lower.begins_with("https://")) {
use_tls = true;
host = host.substr(8, host.length() - 8);
}
ERR_FAIL_COND_V(host.length() < HOST_MIN_LEN, ERR_INVALID_PARAMETER);
if (port < 0) {
if (use_tls) {
port = PORT_HTTPS;
} else {
port = PORT_HTTP;
}
}
status = host.is_valid_ip_address() ? STATUS_CONNECTING : STATUS_RESOLVING;
return OK;
}
void HTTPClientWeb::set_connection(const Ref<StreamPeer> &p_connection) {
ERR_FAIL_MSG("Accessing an HTTPClientWeb's StreamPeer is not supported for the Web platform.");
}
Ref<StreamPeer> HTTPClientWeb::get_connection() const {
ERR_FAIL_V_MSG(Ref<RefCounted>(), "Accessing an HTTPClientWeb's StreamPeer is not supported for the Web platform.");
}
Error HTTPClientWeb::request(Method p_method, const String &p_url, const Vector<String> &p_headers, const uint8_t *p_body, int p_body_len) {
ERR_FAIL_INDEX_V(p_method, METHOD_MAX, ERR_INVALID_PARAMETER);
ERR_FAIL_COND_V_MSG(p_method == METHOD_TRACE || p_method == METHOD_CONNECT, ERR_UNAVAILABLE, "HTTP methods TRACE and CONNECT are not supported for the Web platform.");
ERR_FAIL_COND_V(status != STATUS_CONNECTED, ERR_INVALID_PARAMETER);
ERR_FAIL_COND_V(host.is_empty(), ERR_UNCONFIGURED);
ERR_FAIL_COND_V(port < 0, ERR_UNCONFIGURED);
ERR_FAIL_COND_V(!p_url.begins_with("/"), ERR_INVALID_PARAMETER);
Error err = verify_headers(p_headers);
if (err) {
return err;
}
String url = (use_tls ? "https://" : "http://") + host + ":" + itos(port) + p_url;
Vector<CharString> keeper;
Vector<const char *> c_strings;
for (int i = 0; i < p_headers.size(); i++) {
keeper.push_back(p_headers[i].utf8());
c_strings.push_back(keeper[i].get_data());
}
if (js_id) {
godot_js_fetch_free(js_id);
}
js_id = godot_js_fetch_create(_methods[p_method], url.utf8().get_data(), c_strings.ptrw(), c_strings.size(), p_body, p_body_len);
status = STATUS_REQUESTING;
return OK;
}
void HTTPClientWeb::close() {
host = "";
port = -1;
use_tls = false;
status = STATUS_DISCONNECTED;
polled_response_code = 0;
response_headers.resize(0);
response_buffer.resize(0);
if (js_id) {
godot_js_fetch_free(js_id);
js_id = 0;
}
}
HTTPClientWeb::Status HTTPClientWeb::get_status() const {
return status;
}
bool HTTPClientWeb::has_response() const {
return response_headers.size() > 0;
}
bool HTTPClientWeb::is_response_chunked() const {
return godot_js_fetch_is_chunked(js_id);
}
int HTTPClientWeb::get_response_code() const {
return polled_response_code;
}
Error HTTPClientWeb::get_response_headers(List<String> *r_response) {
if (!response_headers.size()) {
return ERR_INVALID_PARAMETER;
}
for (int i = 0; i < response_headers.size(); i++) {
r_response->push_back(response_headers[i]);
}
response_headers.clear();
return OK;
}
int64_t HTTPClientWeb::get_response_body_length() const {
// Body length cannot be consistently retrieved from the web.
// Reading the "content-length" value will return a meaningless value when the response is compressed,
// as reading will return uncompressed chunks in any case, resulting in a mismatch between the detected
// body size and the actual size returned by repeatedly calling read_response_body_chunk.
// Additionally, while "content-length" is considered a safe CORS header, "content-encoding" is not,
// so using the "content-encoding" to decide if "content-length" is meaningful is not an option either.
// We simply must accept the fact that browsers are awful when it comes to networking APIs.
// See GH-47597, and GH-79327.
return -1;
}
PackedByteArray HTTPClientWeb::read_response_body_chunk() {
ERR_FAIL_COND_V(status != STATUS_BODY, PackedByteArray());
if (response_buffer.size() != read_limit) {
response_buffer.resize(read_limit);
}
int read = godot_js_fetch_read_chunk(js_id, response_buffer.ptrw(), read_limit);
// Check if the stream is over.
godot_js_fetch_state_t state = godot_js_fetch_state_get(js_id);
if (state == GODOT_JS_FETCH_STATE_DONE) {
status = STATUS_DISCONNECTED;
} else if (state != GODOT_JS_FETCH_STATE_BODY) {
status = STATUS_CONNECTION_ERROR;
}
PackedByteArray chunk;
if (!read) {
return chunk;
}
chunk.resize(read);
memcpy(chunk.ptrw(), response_buffer.ptr(), read);
return chunk;
}
void HTTPClientWeb::set_blocking_mode(bool p_enable) {
ERR_FAIL_COND_MSG(p_enable, "HTTPClientWeb blocking mode is not supported for the Web platform.");
}
bool HTTPClientWeb::is_blocking_mode_enabled() const {
return false;
}
void HTTPClientWeb::set_read_chunk_size(int p_size) {
read_limit = p_size;
}
int HTTPClientWeb::get_read_chunk_size() const {
return read_limit;
}
Error HTTPClientWeb::poll() {
switch (status) {
case STATUS_DISCONNECTED:
return ERR_UNCONFIGURED;
case STATUS_RESOLVING:
status = STATUS_CONNECTING;
return OK;
case STATUS_CONNECTING:
status = STATUS_CONNECTED;
return OK;
case STATUS_CONNECTED:
return OK;
case STATUS_BODY: {
godot_js_fetch_state_t state = godot_js_fetch_state_get(js_id);
if (state == GODOT_JS_FETCH_STATE_DONE) {
status = STATUS_DISCONNECTED;
} else if (state != GODOT_JS_FETCH_STATE_BODY) {
status = STATUS_CONNECTION_ERROR;
return ERR_CONNECTION_ERROR;
}
return OK;
}
case STATUS_CONNECTION_ERROR:
return ERR_CONNECTION_ERROR;
case STATUS_REQUESTING: {
#ifdef DEBUG_ENABLED
// forcing synchronous requests is not possible on the web
if (last_polling_frame == Engine::get_singleton()->get_process_frames()) {
WARN_PRINT("HTTPClientWeb polled multiple times in one frame, "
"but request cannot progress more than once per "
"frame on the Web platform.");
}
last_polling_frame = Engine::get_singleton()->get_process_frames();
#endif
polled_response_code = godot_js_fetch_http_status_get(js_id);
godot_js_fetch_state_t js_state = godot_js_fetch_state_get(js_id);
if (js_state == GODOT_JS_FETCH_STATE_REQUESTING) {
return OK;
} else if (js_state == GODOT_JS_FETCH_STATE_ERROR) {
// Fetch is in error state.
status = STATUS_CONNECTION_ERROR;
return ERR_CONNECTION_ERROR;
}
if (godot_js_fetch_read_headers(js_id, &_parse_headers, this)) {
// Failed to parse headers.
status = STATUS_CONNECTION_ERROR;
return ERR_CONNECTION_ERROR;
}
status = STATUS_BODY;
break;
}
default:
ERR_FAIL_V(ERR_BUG);
}
return OK;
}
HTTPClient *HTTPClientWeb::_create_func() {
return memnew(HTTPClientWeb);
}
HTTPClient *(*HTTPClient::_create)() = HTTPClientWeb::_create_func;
HTTPClientWeb::HTTPClientWeb() {
}
HTTPClientWeb::~HTTPClientWeb() {
close();
}

View file

@ -0,0 +1,108 @@
/**************************************************************************/
/* http_client_web.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. */
/**************************************************************************/
#ifndef HTTP_CLIENT_WEB_H
#define HTTP_CLIENT_WEB_H
#include "core/io/http_client.h"
#ifdef __cplusplus
extern "C" {
#endif
#include <stddef.h>
typedef enum {
GODOT_JS_FETCH_STATE_REQUESTING = 0,
GODOT_JS_FETCH_STATE_BODY = 1,
GODOT_JS_FETCH_STATE_DONE = 2,
GODOT_JS_FETCH_STATE_ERROR = -1,
} godot_js_fetch_state_t;
extern int godot_js_fetch_create(const char *p_method, const char *p_url, const char **p_headers, int p_headers_len, const uint8_t *p_body, int p_body_len);
extern int godot_js_fetch_read_headers(int p_id, void (*parse_callback)(int p_size, const char **p_headers, void *p_ref), void *p_ref);
extern int godot_js_fetch_read_chunk(int p_id, uint8_t *p_buf, int p_buf_size);
extern void godot_js_fetch_free(int p_id);
extern godot_js_fetch_state_t godot_js_fetch_state_get(int p_id);
extern int godot_js_fetch_http_status_get(int p_id);
extern int godot_js_fetch_is_chunked(int p_id);
#ifdef __cplusplus
}
#endif
class HTTPClientWeb : public HTTPClient {
private:
int js_id = 0;
Status status = STATUS_DISCONNECTED;
// 64 KiB by default (favors fast download speeds at the cost of memory usage).
int read_limit = 65536;
String host;
int port = -1;
bool use_tls = false;
int polled_response_code = 0;
Vector<String> response_headers;
Vector<uint8_t> response_buffer;
#ifdef DEBUG_ENABLED
uint64_t last_polling_frame = 0;
#endif
static void _parse_headers(int p_len, const char **p_headers, void *p_ref);
public:
static HTTPClient *_create_func();
Error request(Method p_method, const String &p_url, const Vector<String> &p_headers, const uint8_t *p_body, int p_body_size) override;
Error connect_to_host(const String &p_host, int p_port = -1, Ref<TLSOptions> p_tls_options = Ref<TLSOptions>()) override;
void set_connection(const Ref<StreamPeer> &p_connection) override;
Ref<StreamPeer> get_connection() const override;
void close() override;
Status get_status() const override;
bool has_response() const override;
bool is_response_chunked() const override;
int get_response_code() const override;
Error get_response_headers(List<String> *r_response) override;
int64_t get_response_body_length() const override;
PackedByteArray read_response_body_chunk() override;
void set_blocking_mode(bool p_enable) override;
bool is_blocking_mode_enabled() const override;
void set_read_chunk_size(int p_size) override;
int get_read_chunk_size() const override;
Error poll() override;
HTTPClientWeb();
~HTTPClientWeb();
};
#endif // HTTP_CLIENT_WEB_H

View file

@ -0,0 +1,385 @@
/**************************************************************************/
/* javascript_bridge_singleton.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 "api/javascript_bridge_singleton.h"
#include "os_web.h"
#include <emscripten.h>
extern "C" {
extern void godot_js_os_download_buffer(const uint8_t *p_buf, int p_buf_size, const char *p_name, const char *p_mime);
}
#ifdef JAVASCRIPT_EVAL_ENABLED
extern "C" {
typedef union {
int64_t i;
double r;
void *p;
} godot_js_wrapper_ex;
typedef int (*GodotJSWrapperVariant2JSCallback)(const void **p_args, int p_pos, godot_js_wrapper_ex *r_val, void **p_lock);
typedef void (*GodotJSWrapperFreeLockCallback)(void **p_lock, int p_type);
extern int godot_js_wrapper_interface_get(const char *p_name);
extern int godot_js_wrapper_object_call(int p_id, const char *p_method, void **p_args, int p_argc, GodotJSWrapperVariant2JSCallback p_variant2js_callback, godot_js_wrapper_ex *p_cb_rval, void **p_lock, GodotJSWrapperFreeLockCallback p_lock_callback);
extern int godot_js_wrapper_object_get(int p_id, godot_js_wrapper_ex *p_val, const char *p_prop);
extern int godot_js_wrapper_object_getvar(int p_id, int p_type, godot_js_wrapper_ex *p_val);
extern int godot_js_wrapper_object_setvar(int p_id, int p_key_type, godot_js_wrapper_ex *p_key_ex, int p_val_type, godot_js_wrapper_ex *p_val_ex);
extern void godot_js_wrapper_object_set(int p_id, const char *p_name, int p_type, godot_js_wrapper_ex *p_val);
extern void godot_js_wrapper_object_unref(int p_id);
extern int godot_js_wrapper_create_cb(void *p_ref, void (*p_callback)(void *p_ref, int p_arg_id, int p_argc));
extern void godot_js_wrapper_object_set_cb_ret(int p_type, godot_js_wrapper_ex *p_val);
extern int godot_js_wrapper_create_object(const char *p_method, void **p_args, int p_argc, GodotJSWrapperVariant2JSCallback p_variant2js_callback, godot_js_wrapper_ex *p_cb_rval, void **p_lock, GodotJSWrapperFreeLockCallback p_lock_callback);
};
class JavaScriptObjectImpl : public JavaScriptObject {
private:
friend class JavaScriptBridge;
int _js_id = 0;
Callable _callable;
WASM_EXPORT static int _variant2js(const void **p_args, int p_pos, godot_js_wrapper_ex *r_val, void **p_lock);
WASM_EXPORT static void _free_lock(void **p_lock, int p_type);
WASM_EXPORT static Variant _js2variant(int p_type, godot_js_wrapper_ex *p_val);
WASM_EXPORT static void *_alloc_variants(int p_size);
WASM_EXPORT static void callback(void *p_ref, int p_arg_id, int p_argc);
static void _callback(const JavaScriptObjectImpl *obj, Variant arg);
protected:
bool _set(const StringName &p_name, const Variant &p_value) override;
bool _get(const StringName &p_name, Variant &r_ret) const override;
void _get_property_list(List<PropertyInfo> *p_list) const override;
public:
Variant getvar(const Variant &p_key, bool *r_valid = nullptr) const override;
void setvar(const Variant &p_key, const Variant &p_value, bool *r_valid = nullptr) override;
Variant callp(const StringName &p_method, const Variant **p_args, int p_argc, Callable::CallError &r_error) override;
JavaScriptObjectImpl() {}
JavaScriptObjectImpl(int p_id) { _js_id = p_id; }
~JavaScriptObjectImpl() {
if (_js_id) {
godot_js_wrapper_object_unref(_js_id);
}
}
};
bool JavaScriptObjectImpl::_set(const StringName &p_name, const Variant &p_value) {
ERR_FAIL_COND_V_MSG(!_js_id, false, "Invalid JS instance");
const String name = p_name;
godot_js_wrapper_ex exchange;
void *lock = nullptr;
const Variant *v = &p_value;
int type = _variant2js((const void **)&v, 0, &exchange, &lock);
godot_js_wrapper_object_set(_js_id, name.utf8().get_data(), type, &exchange);
if (lock) {
_free_lock(&lock, type);
}
return true;
}
bool JavaScriptObjectImpl::_get(const StringName &p_name, Variant &r_ret) const {
ERR_FAIL_COND_V_MSG(!_js_id, false, "Invalid JS instance");
const String name = p_name;
godot_js_wrapper_ex exchange;
int type = godot_js_wrapper_object_get(_js_id, &exchange, name.utf8().get_data());
r_ret = _js2variant(type, &exchange);
return true;
}
Variant JavaScriptObjectImpl::getvar(const Variant &p_key, bool *r_valid) const {
if (r_valid) {
*r_valid = false;
}
godot_js_wrapper_ex exchange;
void *lock = nullptr;
const Variant *v = &p_key;
int prop_type = _variant2js((const void **)&v, 0, &exchange, &lock);
int type = godot_js_wrapper_object_getvar(_js_id, prop_type, &exchange);
if (lock) {
_free_lock(&lock, prop_type);
}
if (type < 0) {
return Variant();
}
if (r_valid) {
*r_valid = true;
}
return _js2variant(type, &exchange);
}
void JavaScriptObjectImpl::setvar(const Variant &p_key, const Variant &p_value, bool *r_valid) {
if (r_valid) {
*r_valid = false;
}
godot_js_wrapper_ex kex, vex;
void *klock = nullptr;
void *vlock = nullptr;
const Variant *kv = &p_key;
const Variant *vv = &p_value;
int ktype = _variant2js((const void **)&kv, 0, &kex, &klock);
int vtype = _variant2js((const void **)&vv, 0, &vex, &vlock);
int ret = godot_js_wrapper_object_setvar(_js_id, ktype, &kex, vtype, &vex);
if (klock) {
_free_lock(&klock, ktype);
}
if (vlock) {
_free_lock(&vlock, vtype);
}
if (ret == 0 && r_valid) {
*r_valid = true;
}
}
void JavaScriptObjectImpl::_get_property_list(List<PropertyInfo> *p_list) const {
}
void JavaScriptObjectImpl::_free_lock(void **p_lock, int p_type) {
ERR_FAIL_NULL_MSG(*p_lock, "No lock to free!");
const Variant::Type type = (Variant::Type)p_type;
switch (type) {
case Variant::STRING: {
CharString *cs = (CharString *)(*p_lock);
memdelete(cs);
*p_lock = nullptr;
} break;
default:
ERR_FAIL_MSG("Unknown lock type to free. Likely a bug.");
}
}
Variant JavaScriptObjectImpl::_js2variant(int p_type, godot_js_wrapper_ex *p_val) {
Variant::Type type = (Variant::Type)p_type;
switch (type) {
case Variant::BOOL:
return Variant((bool)p_val->i);
case Variant::INT:
return p_val->i;
case Variant::FLOAT:
return p_val->r;
case Variant::STRING: {
String out = String::utf8((const char *)p_val->p);
free(p_val->p);
return out;
}
case Variant::OBJECT: {
return memnew(JavaScriptObjectImpl(p_val->i));
}
default:
return Variant();
}
}
int JavaScriptObjectImpl::_variant2js(const void **p_args, int p_pos, godot_js_wrapper_ex *r_val, void **p_lock) {
const Variant **args = (const Variant **)p_args;
const Variant *v = args[p_pos];
Variant::Type type = v->get_type();
switch (type) {
case Variant::BOOL:
r_val->i = v->operator bool() ? 1 : 0;
break;
case Variant::INT: {
const int64_t tmp = v->operator int64_t();
if (tmp >= 1LL << 31) {
r_val->r = (double)tmp;
return Variant::FLOAT;
}
r_val->i = v->operator int64_t();
} break;
case Variant::FLOAT:
r_val->r = v->operator real_t();
break;
case Variant::STRING: {
CharString *cs = memnew(CharString(v->operator String().utf8()));
r_val->p = (void *)cs->get_data();
*p_lock = (void *)cs;
} break;
case Variant::OBJECT: {
JavaScriptObject *js_obj = Object::cast_to<JavaScriptObject>(v->operator Object *());
r_val->i = js_obj != nullptr ? ((JavaScriptObjectImpl *)js_obj)->_js_id : 0;
} break;
default:
break;
}
return type;
}
Variant JavaScriptObjectImpl::callp(const StringName &p_method, const Variant **p_args, int p_argc, Callable::CallError &r_error) {
godot_js_wrapper_ex exchange;
const String method = p_method;
void *lock = nullptr;
const int type = godot_js_wrapper_object_call(_js_id, method.utf8().get_data(), (void **)p_args, p_argc, &_variant2js, &exchange, &lock, &_free_lock);
r_error.error = Callable::CallError::CALL_OK;
if (type < 0) {
r_error.error = Callable::CallError::CALL_ERROR_INSTANCE_IS_NULL;
return Variant();
}
return _js2variant(type, &exchange);
}
void JavaScriptObjectImpl::callback(void *p_ref, int p_args_id, int p_argc) {
const JavaScriptObjectImpl *obj = (JavaScriptObjectImpl *)p_ref;
ERR_FAIL_COND_MSG(!obj->_callable.is_valid(), "JavaScript callback failed.");
Vector<const Variant *> argp;
Array arg_arr;
for (int i = 0; i < p_argc; i++) {
godot_js_wrapper_ex exchange;
exchange.i = i;
int type = godot_js_wrapper_object_getvar(p_args_id, Variant::INT, &exchange);
arg_arr.push_back(_js2variant(type, &exchange));
}
Variant arg = arg_arr;
#ifdef PROXY_TO_PTHREAD_ENABLED
if (!Thread::is_main_thread()) {
callable_mp_static(JavaScriptObjectImpl::_callback).call_deferred(obj, arg);
return;
}
#endif
_callback(obj, arg);
}
void JavaScriptObjectImpl::_callback(const JavaScriptObjectImpl *obj, Variant arg) {
obj->_callable.call(arg);
// Set return value
godot_js_wrapper_ex exchange;
void *lock = nullptr;
Variant ret;
const Variant *v = &ret;
int type = _variant2js((const void **)&v, 0, &exchange, &lock);
godot_js_wrapper_object_set_cb_ret(type, &exchange);
if (lock) {
_free_lock(&lock, type);
}
}
Ref<JavaScriptObject> JavaScriptBridge::create_callback(const Callable &p_callable) {
Ref<JavaScriptObjectImpl> out = memnew(JavaScriptObjectImpl);
out->_callable = p_callable;
out->_js_id = godot_js_wrapper_create_cb(out.ptr(), JavaScriptObjectImpl::callback);
return out;
}
Ref<JavaScriptObject> JavaScriptBridge::get_interface(const String &p_interface) {
int js_id = godot_js_wrapper_interface_get(p_interface.utf8().get_data());
ERR_FAIL_COND_V_MSG(!js_id, Ref<JavaScriptObject>(), "No interface '" + p_interface + "' registered.");
return Ref<JavaScriptObject>(memnew(JavaScriptObjectImpl(js_id)));
}
Variant JavaScriptBridge::_create_object_bind(const Variant **p_args, int p_argcount, Callable::CallError &r_error) {
if (p_argcount < 1) {
r_error.error = Callable::CallError::CALL_ERROR_TOO_FEW_ARGUMENTS;
r_error.expected = 1;
return Ref<JavaScriptObject>();
}
if (p_args[0]->get_type() != Variant::STRING) {
r_error.error = Callable::CallError::CALL_ERROR_INVALID_ARGUMENT;
r_error.argument = 0;
r_error.expected = Variant::STRING;
return Ref<JavaScriptObject>();
}
godot_js_wrapper_ex exchange;
const String object = *p_args[0];
void *lock = nullptr;
const Variant **args = p_argcount > 1 ? &p_args[1] : nullptr;
const int type = godot_js_wrapper_create_object(object.utf8().get_data(), (void **)args, p_argcount - 1, &JavaScriptObjectImpl::_variant2js, &exchange, &lock, &JavaScriptObjectImpl::_free_lock);
r_error.error = Callable::CallError::CALL_OK;
if (type < 0) {
r_error.error = Callable::CallError::CALL_ERROR_INSTANCE_IS_NULL;
return Ref<JavaScriptObject>();
}
return JavaScriptObjectImpl::_js2variant(type, &exchange);
}
extern "C" {
union js_eval_ret {
uint32_t b;
double d;
char *s;
};
extern int godot_js_eval(const char *p_js, int p_use_global_ctx, union js_eval_ret *p_union_ptr, void *p_byte_arr, void *p_byte_arr_write, void *(*p_callback)(void *p_ptr, void *p_ptr2, int p_len));
}
void *resize_PackedByteArray_and_open_write(void *p_arr, void *r_write, int p_len) {
PackedByteArray *arr = (PackedByteArray *)p_arr;
VectorWriteProxy<uint8_t> *write = (VectorWriteProxy<uint8_t> *)r_write;
arr->resize(p_len);
*write = arr->write;
return arr->ptrw();
}
Variant JavaScriptBridge::eval(const String &p_code, bool p_use_global_exec_context) {
union js_eval_ret js_data;
PackedByteArray arr;
VectorWriteProxy<uint8_t> arr_write;
Variant::Type return_type = static_cast<Variant::Type>(godot_js_eval(p_code.utf8().get_data(), p_use_global_exec_context, &js_data, &arr, &arr_write, resize_PackedByteArray_and_open_write));
switch (return_type) {
case Variant::BOOL:
return js_data.b;
case Variant::FLOAT:
return js_data.d;
case Variant::STRING: {
String str = String::utf8(js_data.s);
free(js_data.s); // Must free the string allocated in JS.
return str;
}
case Variant::PACKED_BYTE_ARRAY:
arr_write = VectorWriteProxy<uint8_t>();
return arr;
default:
return Variant();
}
}
#endif // JAVASCRIPT_EVAL_ENABLED
void JavaScriptBridge::download_buffer(Vector<uint8_t> p_arr, const String &p_name, const String &p_mime) {
godot_js_os_download_buffer(p_arr.ptr(), p_arr.size(), p_name.utf8().get_data(), p_mime.utf8().get_data());
}
bool JavaScriptBridge::pwa_needs_update() const {
return OS_Web::get_singleton()->pwa_needs_update();
}
Error JavaScriptBridge::pwa_update() {
return OS_Web::get_singleton()->pwa_update();
}
void JavaScriptBridge::force_fs_sync() {
OS_Web::get_singleton()->force_fs_sync();
}

View file

@ -0,0 +1,364 @@
/**
* An object used to configure the Engine instance based on godot export options, and to override those in custom HTML
* templates if needed.
*
* @header Engine configuration
* @summary The Engine configuration object. This is just a typedef, create it like a regular object, e.g.:
*
* ``const MyConfig = { executable: 'godot', unloadAfterInit: false }``
*
* @typedef {Object} EngineConfig
*/
const EngineConfig = {}; // eslint-disable-line no-unused-vars
/**
* @struct
* @constructor
* @ignore
*/
const InternalConfig = function (initConfig) { // eslint-disable-line no-unused-vars
const cfg = /** @lends {InternalConfig.prototype} */ {
/**
* Whether to unload the engine automatically after the instance is initialized.
*
* @memberof EngineConfig
* @default
* @type {boolean}
*/
unloadAfterInit: true,
/**
* The HTML DOM Canvas object to use.
*
* By default, the first canvas element in the document will be used is none is specified.
*
* @memberof EngineConfig
* @default
* @type {?HTMLCanvasElement}
*/
canvas: null,
/**
* The name of the WASM file without the extension. (Set by Godot Editor export process).
*
* @memberof EngineConfig
* @default
* @type {string}
*/
executable: '',
/**
* An alternative name for the game pck to load. The executable name is used otherwise.
*
* @memberof EngineConfig
* @default
* @type {?string}
*/
mainPack: null,
/**
* Specify a language code to select the proper localization for the game.
*
* The browser locale will be used if none is specified. See complete list of
* :ref:`supported locales <doc_locales>`.
*
* @memberof EngineConfig
* @type {?string}
* @default
*/
locale: null,
/**
* The canvas resize policy determines how the canvas should be resized by Godot.
*
* ``0`` means Godot won't do any resizing. This is useful if you want to control the canvas size from
* javascript code in your template.
*
* ``1`` means Godot will resize the canvas on start, and when changing window size via engine functions.
*
* ``2`` means Godot will adapt the canvas size to match the whole browser window.
*
* @memberof EngineConfig
* @type {number}
* @default
*/
canvasResizePolicy: 2,
/**
* The arguments to be passed as command line arguments on startup.
*
* See :ref:`command line tutorial <doc_command_line_tutorial>`.
*
* **Note**: :js:meth:`startGame <Engine.prototype.startGame>` will always add the ``--main-pack`` argument.
*
* @memberof EngineConfig
* @type {Array<string>}
* @default
*/
args: [],
/**
* When enabled, the game canvas will automatically grab the focus when the engine starts.
*
* @memberof EngineConfig
* @type {boolean}
* @default
*/
focusCanvas: true,
/**
* When enabled, this will turn on experimental virtual keyboard support on mobile.
*
* @memberof EngineConfig
* @type {boolean}
* @default
*/
experimentalVK: false,
/**
* The progressive web app service worker to install.
* @memberof EngineConfig
* @default
* @type {string}
*/
serviceWorker: '',
/**
* @ignore
* @type {Array.<string>}
*/
persistentPaths: ['/userfs'],
/**
* @ignore
* @type {boolean}
*/
persistentDrops: false,
/**
* @ignore
* @type {Array.<string>}
*/
gdextensionLibs: [],
/**
* @ignore
* @type {Array.<string>}
*/
fileSizes: [],
/**
* A callback function for handling Godot's ``OS.execute`` calls.
*
* This is for example used in the Web Editor template to switch between project manager and editor, and for running the game.
*
* @callback EngineConfig.onExecute
* @param {string} path The path that Godot's wants executed.
* @param {Array.<string>} args The arguments of the "command" to execute.
*/
/**
* @ignore
* @type {?function(string, Array.<string>)}
*/
onExecute: null,
/**
* A callback function for being notified when the Godot instance quits.
*
* **Note**: This function will not be called if the engine crashes or become unresponsive.
*
* @callback EngineConfig.onExit
* @param {number} status_code The status code returned by Godot on exit.
*/
/**
* @ignore
* @type {?function(number)}
*/
onExit: null,
/**
* A callback function for displaying download progress.
*
* The function is called once per frame while downloading files, so the usage of ``requestAnimationFrame()``
* is not necessary.
*
* If the callback function receives a total amount of bytes as 0, this means that it is impossible to calculate.
* Possible reasons include:
*
* - Files are delivered with server-side chunked compression
* - Files are delivered with server-side compression on Chromium
* - Not all file downloads have started yet (usually on servers without multi-threading)
*
* @callback EngineConfig.onProgress
* @param {number} current The current amount of downloaded bytes so far.
* @param {number} total The total amount of bytes to be downloaded.
*/
/**
* @ignore
* @type {?function(number, number)}
*/
onProgress: null,
/**
* A callback function for handling the standard output stream. This method should usually only be used in debug pages.
*
* By default, ``console.log()`` is used.
*
* @callback EngineConfig.onPrint
* @param {...*} [var_args] A variadic number of arguments to be printed.
*/
/**
* @ignore
* @type {?function(...*)}
*/
onPrint: function () {
console.log.apply(console, Array.from(arguments)); // eslint-disable-line no-console
},
/**
* A callback function for handling the standard error stream. This method should usually only be used in debug pages.
*
* By default, ``console.error()`` is used.
*
* @callback EngineConfig.onPrintError
* @param {...*} [var_args] A variadic number of arguments to be printed as errors.
*/
/**
* @ignore
* @type {?function(...*)}
*/
onPrintError: function (var_args) {
console.error.apply(console, Array.from(arguments)); // eslint-disable-line no-console
},
};
/**
* @ignore
* @struct
* @constructor
* @param {EngineConfig} opts
*/
function Config(opts) {
this.update(opts);
}
Config.prototype = cfg;
/**
* @ignore
* @param {EngineConfig} opts
*/
Config.prototype.update = function (opts) {
const config = opts || {};
// NOTE: We must explicitly pass the default, accessing it via
// the key will fail due to closure compiler renames.
function parse(key, def) {
if (typeof (config[key]) === 'undefined') {
return def;
}
return config[key];
}
// Module config
this.unloadAfterInit = parse('unloadAfterInit', this.unloadAfterInit);
this.onPrintError = parse('onPrintError', this.onPrintError);
this.onPrint = parse('onPrint', this.onPrint);
this.onProgress = parse('onProgress', this.onProgress);
// Godot config
this.canvas = parse('canvas', this.canvas);
this.executable = parse('executable', this.executable);
this.mainPack = parse('mainPack', this.mainPack);
this.locale = parse('locale', this.locale);
this.canvasResizePolicy = parse('canvasResizePolicy', this.canvasResizePolicy);
this.persistentPaths = parse('persistentPaths', this.persistentPaths);
this.persistentDrops = parse('persistentDrops', this.persistentDrops);
this.experimentalVK = parse('experimentalVK', this.experimentalVK);
this.focusCanvas = parse('focusCanvas', this.focusCanvas);
this.serviceWorker = parse('serviceWorker', this.serviceWorker);
this.gdextensionLibs = parse('gdextensionLibs', this.gdextensionLibs);
this.fileSizes = parse('fileSizes', this.fileSizes);
this.args = parse('args', this.args);
this.onExecute = parse('onExecute', this.onExecute);
this.onExit = parse('onExit', this.onExit);
};
/**
* @ignore
* @param {string} loadPath
* @param {Response} response
*/
Config.prototype.getModuleConfig = function (loadPath, response) {
let r = response;
const gdext = this.gdextensionLibs;
return {
'print': this.onPrint,
'printErr': this.onPrintError,
'thisProgram': this.executable,
'noExitRuntime': false,
'dynamicLibraries': [`${loadPath}.side.wasm`].concat(this.gdextensionLibs),
'instantiateWasm': function (imports, onSuccess) {
function done(result) {
onSuccess(result['instance'], result['module']);
}
if (typeof (WebAssembly.instantiateStreaming) !== 'undefined') {
WebAssembly.instantiateStreaming(Promise.resolve(r), imports).then(done);
} else {
r.arrayBuffer().then(function (buffer) {
WebAssembly.instantiate(buffer, imports).then(done);
});
}
r = null;
return {};
},
'locateFile': function (path) {
if (!path.startsWith('godot.')) {
return path;
} else if (path.endsWith('.worker.js')) {
return `${loadPath}.worker.js`;
} else if (path.endsWith('.audio.worklet.js')) {
return `${loadPath}.audio.worklet.js`;
} else if (path.endsWith('.js')) {
return `${loadPath}.js`;
} else if (path in gdext) {
return path;
} else if (path.endsWith('.side.wasm')) {
return `${loadPath}.side.wasm`;
} else if (path.endsWith('.wasm')) {
return `${loadPath}.wasm`;
}
return path;
},
};
};
/**
* @ignore
* @param {function()} cleanup
*/
Config.prototype.getGodotConfig = function (cleanup) {
// Try to find a canvas
if (!(this.canvas instanceof HTMLCanvasElement)) {
const nodes = document.getElementsByTagName('canvas');
if (nodes.length && nodes[0] instanceof HTMLCanvasElement) {
const first = nodes[0];
this.canvas = /** @type {!HTMLCanvasElement} */ (first);
}
if (!this.canvas) {
throw new Error('No canvas found in page');
}
}
// Canvas can grab focus on click, or key events won't work.
if (this.canvas.tabIndex < 0) {
this.canvas.tabIndex = 0;
}
// Browser locale, or custom one if defined.
let locale = this.locale;
if (!locale) {
locale = navigator.languages ? navigator.languages[0] : navigator.language;
locale = locale.split('.')[0];
}
locale = locale.replace('-', '_');
const onExit = this.onExit;
// Godot configuration.
return {
'canvas': this.canvas,
'canvasResizePolicy': this.canvasResizePolicy,
'locale': locale,
'persistentDrops': this.persistentDrops,
'virtualKeyboard': this.experimentalVK,
'focusCanvas': this.focusCanvas,
'onExecute': this.onExecute,
'onExit': function (p_code) {
cleanup(); // We always need to call the cleanup callback to free memory.
if (typeof (onExit) === 'function') {
onExit(p_code);
}
},
};
};
return new Config(initConfig);
};

View file

@ -0,0 +1,4 @@
var Godot;
var WebAssembly = {};
WebAssembly.instantiate = function(buffer, imports) {};
WebAssembly.instantiateStreaming = function(response, imports) {};

View file

@ -0,0 +1,282 @@
/**
* Projects exported for the Web expose the :js:class:`Engine` class to the JavaScript environment, that allows
* fine control over the engine's start-up process.
*
* This API is built in an asynchronous manner and requires basic understanding
* of `Promises <https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Using_promises>`__.
*
* @module Engine
* @header Web export JavaScript reference
*/
const Engine = (function () {
const preloader = new Preloader();
let loadPromise = null;
let loadPath = '';
let initPromise = null;
/**
* @classdesc The ``Engine`` class provides methods for loading and starting exported projects on the Web. For default export
* settings, this is already part of the exported HTML page. To understand practical use of the ``Engine`` class,
* see :ref:`Custom HTML page for Web export <doc_customizing_html5_shell>`.
*
* @description Create a new Engine instance with the given configuration.
*
* @global
* @constructor
* @param {EngineConfig} initConfig The initial config for this instance.
*/
function Engine(initConfig) { // eslint-disable-line no-shadow
this.config = new InternalConfig(initConfig);
this.rtenv = null;
}
/**
* Load the engine from the specified base path.
*
* @param {string} basePath Base path of the engine to load.
* @param {number=} [size=0] The file size if known.
* @returns {Promise} A Promise that resolves once the engine is loaded.
*
* @function Engine.load
*/
Engine.load = function (basePath, size) {
if (loadPromise == null) {
loadPath = basePath;
loadPromise = preloader.loadPromise(`${loadPath}.wasm`, size, true);
requestAnimationFrame(preloader.animateProgress);
}
return loadPromise;
};
/**
* Unload the engine to free memory.
*
* This method will be called automatically depending on the configuration. See :js:attr:`unloadAfterInit`.
*
* @function Engine.unload
*/
Engine.unload = function () {
loadPromise = null;
};
/**
* Safe Engine constructor, creates a new prototype for every new instance to avoid prototype pollution.
* @ignore
* @constructor
*/
function SafeEngine(initConfig) {
const proto = /** @lends Engine.prototype */ {
/**
* Initialize the engine instance. Optionally, pass the base path to the engine to load it,
* if it hasn't been loaded yet. See :js:meth:`Engine.load`.
*
* @param {string=} basePath Base path of the engine to load.
* @return {Promise} A ``Promise`` that resolves once the engine is loaded and initialized.
*/
init: function (basePath) {
if (initPromise) {
return initPromise;
}
if (loadPromise == null) {
if (!basePath) {
initPromise = Promise.reject(new Error('A base path must be provided when calling `init` and the engine is not loaded.'));
return initPromise;
}
Engine.load(basePath, this.config.fileSizes[`${basePath}.wasm`]);
}
const me = this;
function doInit(promise) {
// Care! Promise chaining is bogus with old emscripten versions.
// This caused a regression with the Mono build (which uses an older emscripten version).
// Make sure to test that when refactoring.
return new Promise(function (resolve, reject) {
promise.then(function (response) {
const cloned = new Response(response.clone().body, { 'headers': [['content-type', 'application/wasm']] });
Godot(me.config.getModuleConfig(loadPath, cloned)).then(function (module) {
const paths = me.config.persistentPaths;
module['initFS'](paths).then(function (err) {
me.rtenv = module;
if (me.config.unloadAfterInit) {
Engine.unload();
}
resolve();
});
});
});
});
}
preloader.setProgressFunc(this.config.onProgress);
initPromise = doInit(loadPromise);
return initPromise;
},
/**
* Load a file so it is available in the instance's file system once it runs. Must be called **before** starting the
* instance.
*
* If not provided, the ``path`` is derived from the URL of the loaded file.
*
* @param {string|ArrayBuffer} file The file to preload.
*
* If a ``string`` the file will be loaded from that path.
*
* If an ``ArrayBuffer`` or a view on one, the buffer will used as the content of the file.
*
* @param {string=} path Path by which the file will be accessible. Required, if ``file`` is not a string.
*
* @returns {Promise} A Promise that resolves once the file is loaded.
*/
preloadFile: function (file, path) {
return preloader.preload(file, path, this.config.fileSizes[file]);
},
/**
* Start the engine instance using the given override configuration (if any).
* :js:meth:`startGame <Engine.prototype.startGame>` can be used in typical cases instead.
*
* This will initialize the instance if it is not initialized. For manual initialization, see :js:meth:`init <Engine.prototype.init>`.
* The engine must be loaded beforehand.
*
* Fails if a canvas cannot be found on the page, or not specified in the configuration.
*
* @param {EngineConfig} override An optional configuration override.
* @return {Promise} Promise that resolves once the engine started.
*/
start: function (override) {
this.config.update(override);
const me = this;
return me.init().then(function () {
if (!me.rtenv) {
return Promise.reject(new Error('The engine must be initialized before it can be started'));
}
let config = {};
try {
config = me.config.getGodotConfig(function () {
me.rtenv = null;
});
} catch (e) {
return Promise.reject(e);
}
// Godot configuration.
me.rtenv['initConfig'](config);
// Preload GDExtension libraries.
if (me.config.gdextensionLibs.length > 0 && !me.rtenv['loadDynamicLibrary']) {
return Promise.reject(new Error('GDExtension libraries are not supported by this engine version. '
+ 'Enable "Extensions Support" for your export preset and/or build your custom template with "dlink_enabled=yes".'));
}
return new Promise(function (resolve, reject) {
for (const file of preloader.preloadedFiles) {
me.rtenv['copyToFS'](file.path, file.buffer);
}
preloader.preloadedFiles.length = 0; // Clear memory
me.rtenv['callMain'](me.config.args);
initPromise = null;
me.installServiceWorker();
resolve();
});
});
},
/**
* Start the game instance using the given configuration override (if any).
*
* This will initialize the instance if it is not initialized. For manual initialization, see :js:meth:`init <Engine.prototype.init>`.
*
* This will load the engine if it is not loaded, and preload the main pck.
*
* This method expects the initial config (or the override) to have both the :js:attr:`executable` and :js:attr:`mainPack`
* properties set (normally done by the editor during export).
*
* @param {EngineConfig} override An optional configuration override.
* @return {Promise} Promise that resolves once the game started.
*/
startGame: function (override) {
this.config.update(override);
// Add main-pack argument.
const exe = this.config.executable;
const pack = this.config.mainPack || `${exe}.pck`;
this.config.args = ['--main-pack', pack].concat(this.config.args);
// Start and init with execName as loadPath if not inited.
const me = this;
return Promise.all([
this.init(exe),
this.preloadFile(pack, pack),
]).then(function () {
return me.start.apply(me);
});
},
/**
* Create a file at the specified ``path`` with the passed as ``buffer`` in the instance's file system.
*
* @param {string} path The location where the file will be created.
* @param {ArrayBuffer} buffer The content of the file.
*/
copyToFS: function (path, buffer) {
if (this.rtenv == null) {
throw new Error('Engine must be inited before copying files');
}
this.rtenv['copyToFS'](path, buffer);
},
/**
* Request that the current instance quit.
*
* This is akin the user pressing the close button in the window manager, and will
* have no effect if the engine has crashed, or is stuck in a loop.
*
*/
requestQuit: function () {
if (this.rtenv) {
this.rtenv['request_quit']();
}
},
/**
* Install the progressive-web app service worker.
* @returns {Promise} The service worker registration promise.
*/
installServiceWorker: function () {
if (this.config.serviceWorker && 'serviceWorker' in navigator) {
return navigator.serviceWorker.register(this.config.serviceWorker);
}
return Promise.resolve();
},
};
Engine.prototype = proto;
// Closure compiler exported instance methods.
Engine.prototype['init'] = Engine.prototype.init;
Engine.prototype['preloadFile'] = Engine.prototype.preloadFile;
Engine.prototype['start'] = Engine.prototype.start;
Engine.prototype['startGame'] = Engine.prototype.startGame;
Engine.prototype['copyToFS'] = Engine.prototype.copyToFS;
Engine.prototype['requestQuit'] = Engine.prototype.requestQuit;
Engine.prototype['installServiceWorker'] = Engine.prototype.installServiceWorker;
// Also expose static methods as instance methods
Engine.prototype['load'] = Engine.load;
Engine.prototype['unload'] = Engine.unload;
return new Engine(initConfig);
}
// Closure compiler exported static methods.
SafeEngine['load'] = Engine.load;
SafeEngine['unload'] = Engine.unload;
// Feature-detection utilities.
SafeEngine['isWebGLAvailable'] = Features.isWebGLAvailable;
SafeEngine['isFetchAvailable'] = Features.isFetchAvailable;
SafeEngine['isSecureContext'] = Features.isSecureContext;
SafeEngine['isCrossOriginIsolated'] = Features.isCrossOriginIsolated;
SafeEngine['isSharedArrayBufferAvailable'] = Features.isSharedArrayBufferAvailable;
SafeEngine['isAudioWorkletAvailable'] = Features.isAudioWorkletAvailable;
SafeEngine['getMissingFeatures'] = Features.getMissingFeatures;
return SafeEngine;
}());
if (typeof window !== 'undefined') {
window['Engine'] = Engine;
}

View file

@ -0,0 +1,106 @@
const Features = {
/**
* Check whether WebGL is available. Optionally, specify a particular version of WebGL to check for.
*
* @param {number=} [majorVersion=1] The major WebGL version to check for.
* @returns {boolean} If the given major version of WebGL is available.
* @function Engine.isWebGLAvailable
*/
isWebGLAvailable: function (majorVersion = 1) {
try {
return !!document.createElement('canvas').getContext(['webgl', 'webgl2'][majorVersion - 1]);
} catch (e) { /* Not available */ }
return false;
},
/**
* Check whether the Fetch API available and supports streaming responses.
*
* @returns {boolean} If the Fetch API is available and supports streaming responses.
* @function Engine.isFetchAvailable
*/
isFetchAvailable: function () {
return 'fetch' in window && 'Response' in window && 'body' in window.Response.prototype;
},
/**
* Check whether the engine is running in a Secure Context.
*
* @returns {boolean} If the engine is running in a Secure Context.
* @function Engine.isSecureContext
*/
isSecureContext: function () {
return window['isSecureContext'] === true;
},
/**
* Check whether the engine is cross origin isolated.
* This value is dependent on Cross-Origin-Opener-Policy and Cross-Origin-Embedder-Policy headers sent by the server.
*
* @returns {boolean} If the engine is running in a Secure Context.
* @function Engine.isSecureContext
*/
isCrossOriginIsolated: function () {
return window['crossOriginIsolated'] === true;
},
/**
* Check whether SharedBufferArray is available.
*
* Most browsers require the page to be running in a secure context, and the
* the server to provide specific CORS headers for SharedArrayBuffer to be available.
*
* @returns {boolean} If SharedArrayBuffer is available.
* @function Engine.isSharedArrayBufferAvailable
*/
isSharedArrayBufferAvailable: function () {
return 'SharedArrayBuffer' in window;
},
/**
* Check whether the AudioContext supports AudioWorkletNodes.
*
* @returns {boolean} If AudioWorkletNode is available.
* @function Engine.isAudioWorkletAvailable
*/
isAudioWorkletAvailable: function () {
return 'AudioContext' in window && 'audioWorklet' in AudioContext.prototype;
},
/**
* Return an array of missing required features (as string).
*
* @returns {Array<string>} A list of human-readable missing features.
* @function Engine.getMissingFeatures
* @param {{threads: (boolean|undefined)}} supportedFeatures
*/
getMissingFeatures: function (supportedFeatures = {}) {
const {
// Quotes are needed for the Closure compiler.
'threads': supportsThreads = true,
} = supportedFeatures;
const missing = [];
if (!Features.isWebGLAvailable(2)) {
missing.push('WebGL2 - Check web browser configuration and hardware support');
}
if (!Features.isFetchAvailable()) {
missing.push('Fetch - Check web browser version');
}
if (!Features.isSecureContext()) {
missing.push('Secure Context - Check web server configuration (use HTTPS)');
}
if (supportsThreads) {
if (!Features.isCrossOriginIsolated()) {
missing.push('Cross-Origin Isolation - Check that the web server configuration sends the correct headers.');
}
if (!Features.isSharedArrayBufferAvailable()) {
missing.push('SharedArrayBuffer - Check that the web server configuration sends the correct headers.');
}
}
// Audio is normally optional since we have a dummy fallback.
return missing;
},
};

View file

@ -0,0 +1,133 @@
const Preloader = /** @constructor */ function () { // eslint-disable-line no-unused-vars
function getTrackedResponse(response, load_status) {
function onloadprogress(reader, controller) {
return reader.read().then(function (result) {
if (load_status.done) {
return Promise.resolve();
}
if (result.value) {
controller.enqueue(result.value);
load_status.loaded += result.value.length;
}
if (!result.done) {
return onloadprogress(reader, controller);
}
load_status.done = true;
return Promise.resolve();
});
}
const reader = response.body.getReader();
return new Response(new ReadableStream({
start: function (controller) {
onloadprogress(reader, controller).then(function () {
controller.close();
});
},
}), { headers: response.headers });
}
function loadFetch(file, tracker, fileSize, raw) {
tracker[file] = {
total: fileSize || 0,
loaded: 0,
done: false,
};
return fetch(file).then(function (response) {
if (!response.ok) {
return Promise.reject(new Error(`Failed loading file '${file}'`));
}
const tr = getTrackedResponse(response, tracker[file]);
if (raw) {
return Promise.resolve(tr);
}
return tr.arrayBuffer();
});
}
function retry(func, attempts = 1) {
function onerror(err) {
if (attempts <= 1) {
return Promise.reject(err);
}
return new Promise(function (resolve, reject) {
setTimeout(function () {
retry(func, attempts - 1).then(resolve).catch(reject);
}, 1000);
});
}
return func().catch(onerror);
}
const DOWNLOAD_ATTEMPTS_MAX = 4;
const loadingFiles = {};
const lastProgress = { loaded: 0, total: 0 };
let progressFunc = null;
const animateProgress = function () {
let loaded = 0;
let total = 0;
let totalIsValid = true;
let progressIsFinal = true;
Object.keys(loadingFiles).forEach(function (file) {
const stat = loadingFiles[file];
if (!stat.done) {
progressIsFinal = false;
}
if (!totalIsValid || stat.total === 0) {
totalIsValid = false;
total = 0;
} else {
total += stat.total;
}
loaded += stat.loaded;
});
if (loaded !== lastProgress.loaded || total !== lastProgress.total) {
lastProgress.loaded = loaded;
lastProgress.total = total;
if (typeof progressFunc === 'function') {
progressFunc(loaded, total);
}
}
if (!progressIsFinal) {
requestAnimationFrame(animateProgress);
}
};
this.animateProgress = animateProgress;
this.setProgressFunc = function (callback) {
progressFunc = callback;
};
this.loadPromise = function (file, fileSize, raw = false) {
return retry(loadFetch.bind(null, file, loadingFiles, fileSize, raw), DOWNLOAD_ATTEMPTS_MAX);
};
this.preloadedFiles = [];
this.preload = function (pathOrBuffer, destPath, fileSize) {
let buffer = null;
if (typeof pathOrBuffer === 'string') {
const me = this;
return this.loadPromise(pathOrBuffer, fileSize).then(function (buf) {
me.preloadedFiles.push({
path: destPath || pathOrBuffer,
buffer: buf,
});
return Promise.resolve();
});
} else if (pathOrBuffer instanceof ArrayBuffer) {
buffer = new Uint8Array(pathOrBuffer);
} else if (ArrayBuffer.isView(pathOrBuffer)) {
buffer = new Uint8Array(pathOrBuffer.buffer);
}
if (buffer) {
this.preloadedFiles.push({
path: destPath,
buffer: pathOrBuffer,
});
return Promise.resolve();
}
return Promise.reject(new Error('Invalid object for preloading'));
};
};

View file

@ -0,0 +1,354 @@
/* eslint-disable strict */
'use strict';
const fs = require('fs');
class JSDoclet {
constructor(doc) {
this.doc = doc;
this.description = doc['description'] || '';
this.name = doc['name'] || 'unknown';
this.longname = doc['longname'] || '';
this.types = [];
if (doc['type'] && doc['type']['names']) {
this.types = doc['type']['names'].slice();
}
this.type = this.types.length > 0 ? this.types.join('\\|') : '*';
this.variable = doc['variable'] || false;
this.kind = doc['kind'] || '';
this.memberof = doc['memberof'] || null;
this.scope = doc['scope'] || '';
this.members = [];
this.optional = doc['optional'] || false;
this.defaultvalue = doc['defaultvalue'];
this.summary = doc['summary'] || null;
this.classdesc = doc['classdesc'] || null;
// Parameters (functions)
this.params = [];
this.returns = doc['returns'] ? doc['returns'][0]['type']['names'][0] : 'void';
this.returns_desc = doc['returns'] ? doc['returns'][0]['description'] : null;
this.params = (doc['params'] || []).slice().map((p) => new JSDoclet(p));
// Custom tags
this.tags = doc['tags'] || [];
this.header = this.tags.filter((t) => t['title'] === 'header').map((t) => t['text']).pop() || null;
}
add_member(obj) {
this.members.push(obj);
}
is_static() {
return this.scope === 'static';
}
is_instance() {
return this.scope === 'instance';
}
is_object() {
return this.kind === 'Object' || (this.kind === 'typedef' && this.type === 'Object');
}
is_class() {
return this.kind === 'class';
}
is_function() {
return this.kind === 'function' || (this.kind === 'typedef' && this.type === 'function');
}
is_module() {
return this.kind === 'module';
}
}
function format_table(f, data, depth = 0) {
if (!data.length) {
return;
}
const column_sizes = new Array(data[0].length).fill(0);
data.forEach((row) => {
row.forEach((e, idx) => {
column_sizes[idx] = Math.max(e.length, column_sizes[idx]);
});
});
const indent = ' '.repeat(depth);
let sep = indent;
column_sizes.forEach((size) => {
sep += '+';
sep += '-'.repeat(size + 2);
});
sep += '+\n';
f.write(sep);
data.forEach((row) => {
let row_text = `${indent}|`;
row.forEach((entry, idx) => {
row_text += ` ${entry.padEnd(column_sizes[idx])} |`;
});
row_text += '\n';
f.write(row_text);
f.write(sep);
});
f.write('\n');
}
function make_header(header, sep) {
return `${header}\n${sep.repeat(header.length)}\n\n`;
}
function indent_multiline(text, depth) {
const indent = ' '.repeat(depth);
return text.split('\n').map((l) => (l === '' ? l : indent + l)).join('\n');
}
function make_rst_signature(obj, types = false, style = false) {
let out = '';
const fmt = style ? '*' : '';
obj.params.forEach((arg, idx) => {
if (idx > 0) {
if (arg.optional) {
out += ` ${fmt}[`;
}
out += ', ';
} else {
out += ' ';
if (arg.optional) {
out += `${fmt}[ `;
}
}
if (types) {
out += `${arg.type} `;
}
const variable = arg.variable ? '...' : '';
const defval = arg.defaultvalue !== undefined ? `=${arg.defaultvalue}` : '';
out += `${variable}${arg.name}${defval}`;
if (arg.optional) {
out += ` ]${fmt}`;
}
});
out += ' ';
return out;
}
function make_rst_param(f, obj, depth = 0) {
const indent = ' '.repeat(depth * 3);
f.write(indent);
f.write(`:param ${obj.type} ${obj.name}:\n`);
f.write(indent_multiline(obj.description, (depth + 1) * 3));
f.write('\n\n');
}
function make_rst_attribute(f, obj, depth = 0, brief = false) {
const indent = ' '.repeat(depth * 3);
f.write(indent);
f.write(`.. js:attribute:: ${obj.name}\n\n`);
if (brief) {
if (obj.summary) {
f.write(indent_multiline(obj.summary, (depth + 1) * 3));
}
f.write('\n\n');
return;
}
f.write(indent_multiline(obj.description, (depth + 1) * 3));
f.write('\n\n');
f.write(indent);
f.write(` :type: ${obj.type}\n\n`);
if (obj.defaultvalue !== undefined) {
let defval = obj.defaultvalue;
if (defval === '') {
defval = '""';
}
f.write(indent);
f.write(` :value: \`\`${defval}\`\`\n\n`);
}
}
function make_rst_function(f, obj, depth = 0) {
let prefix = '';
if (obj.is_instance()) {
prefix = 'prototype.';
}
const indent = ' '.repeat(depth * 3);
const sig = make_rst_signature(obj);
f.write(indent);
f.write(`.. js:function:: ${prefix}${obj.name}(${sig})\n`);
f.write('\n');
f.write(indent_multiline(obj.description, (depth + 1) * 3));
f.write('\n\n');
obj.params.forEach((param) => {
make_rst_param(f, param, depth + 1);
});
if (obj.returns !== 'void') {
f.write(indent);
f.write(' :return:\n');
f.write(indent_multiline(obj.returns_desc, (depth + 2) * 3));
f.write('\n\n');
f.write(indent);
f.write(` :rtype: ${obj.returns}\n\n`);
}
}
function make_rst_object(f, obj) {
let brief = false;
// Our custom header flag.
if (obj.header !== null) {
f.write(make_header(obj.header, '-'));
f.write(`${obj.description}\n\n`);
brief = true;
}
// Format members table and descriptions
const data = [['type', 'name']].concat(obj.members.map((m) => [m.type, `:js:attr:\`${m.name}\``]));
f.write(make_header('Properties', '^'));
format_table(f, data, 0);
make_rst_attribute(f, obj, 0, brief);
if (!obj.members.length) {
return;
}
f.write(' **Property Descriptions**\n\n');
// Properties first
obj.members.filter((m) => !m.is_function()).forEach((m) => {
make_rst_attribute(f, m, 1);
});
// Callbacks last
obj.members.filter((m) => m.is_function()).forEach((m) => {
make_rst_function(f, m, 1);
});
}
function make_rst_class(f, obj) {
const header = obj.header ? obj.header : obj.name;
f.write(make_header(header, '-'));
if (obj.classdesc) {
f.write(`${obj.classdesc}\n\n`);
}
const funcs = obj.members.filter((m) => m.is_function());
function make_data(m) {
const base = m.is_static() ? obj.name : `${obj.name}.prototype`;
const params = make_rst_signature(m, true, true);
const sig = `:js:attr:\`${m.name} <${base}.${m.name}>\` **(**${params}**)**`;
return [m.returns, sig];
}
const sfuncs = funcs.filter((m) => m.is_static());
const ifuncs = funcs.filter((m) => !m.is_static());
f.write(make_header('Static Methods', '^'));
format_table(f, sfuncs.map((m) => make_data(m)));
f.write(make_header('Instance Methods', '^'));
format_table(f, ifuncs.map((m) => make_data(m)));
const sig = make_rst_signature(obj);
f.write(`.. js:class:: ${obj.name}(${sig})\n\n`);
f.write(indent_multiline(obj.description, 3));
f.write('\n\n');
obj.params.forEach((p) => {
make_rst_param(f, p, 1);
});
f.write(' **Static Methods**\n\n');
sfuncs.forEach((m) => {
make_rst_function(f, m, 1);
});
f.write(' **Instance Methods**\n\n');
ifuncs.forEach((m) => {
make_rst_function(f, m, 1);
});
}
function make_rst_module(f, obj) {
const header = obj.header !== null ? obj.header : obj.name;
f.write(make_header(header, '='));
f.write(obj.description);
f.write('\n\n');
}
function write_base_object(f, obj) {
if (obj.is_object()) {
make_rst_object(f, obj);
} else if (obj.is_function()) {
make_rst_function(f, obj);
} else if (obj.is_class()) {
make_rst_class(f, obj);
} else if (obj.is_module()) {
make_rst_module(f, obj);
}
}
function generate(f, docs) {
const globs = [];
const SYMBOLS = {};
docs.filter((d) => !d.ignore && d.kind !== 'package').forEach((d) => {
SYMBOLS[d.name] = d;
if (d.memberof) {
const up = SYMBOLS[d.memberof];
if (up === undefined) {
console.log(d); // eslint-disable-line no-console
console.log(`Undefined symbol! ${d.memberof}`); // eslint-disable-line no-console
throw new Error('Undefined symbol!');
}
SYMBOLS[d.memberof].add_member(d);
} else {
globs.push(d);
}
});
f.write('.. _doc_html5_shell_classref:\n\n');
globs.forEach((obj) => write_base_object(f, obj));
}
/**
* Generate documentation output.
*
* @param {TAFFY} data - A TaffyDB collection representing
* all the symbols documented in your code.
* @param {object} opts - An object with options information.
*/
exports.publish = function (data, opts) {
const docs = data().get().filter((doc) => !doc.undocumented && !doc.ignore).map((doc) => new JSDoclet(doc));
const dest = opts.destination;
if (dest === 'dry-run') {
process.stdout.write('Dry run... ');
generate({
write: function () { /* noop */ },
}, docs);
process.stdout.write('Okay!\n');
return;
}
if (dest !== '' && !dest.endsWith('.rst')) {
throw new Error('Destination file must be either a ".rst" file, or an empty string (for printing to stdout)');
}
if (dest !== '') {
const f = fs.createWriteStream(dest);
generate(f, docs);
} else {
generate(process.stdout, docs);
}
};

View file

@ -0,0 +1,213 @@
/**************************************************************************/
/* audio.worklet.js */
/**************************************************************************/
/* 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. */
/**************************************************************************/
class RingBuffer {
constructor(p_buffer, p_state, p_threads) {
this.buffer = p_buffer;
this.avail = p_state;
this.threads = p_threads;
this.rpos = 0;
this.wpos = 0;
}
data_left() {
return this.threads ? Atomics.load(this.avail, 0) : this.avail;
}
space_left() {
return this.buffer.length - this.data_left();
}
read(output) {
const size = this.buffer.length;
let from = 0;
let to_write = output.length;
if (this.rpos + to_write > size) {
const high = size - this.rpos;
output.set(this.buffer.subarray(this.rpos, size));
from = high;
to_write -= high;
this.rpos = 0;
}
if (to_write) {
output.set(this.buffer.subarray(this.rpos, this.rpos + to_write), from);
}
this.rpos += to_write;
if (this.threads) {
Atomics.add(this.avail, 0, -output.length);
Atomics.notify(this.avail, 0);
} else {
this.avail -= output.length;
}
}
write(p_buffer) {
const to_write = p_buffer.length;
const mw = this.buffer.length - this.wpos;
if (mw >= to_write) {
this.buffer.set(p_buffer, this.wpos);
this.wpos += to_write;
if (mw === to_write) {
this.wpos = 0;
}
} else {
const high = p_buffer.subarray(0, mw);
const low = p_buffer.subarray(mw);
this.buffer.set(high, this.wpos);
this.buffer.set(low);
this.wpos = low.length;
}
if (this.threads) {
Atomics.add(this.avail, 0, to_write);
Atomics.notify(this.avail, 0);
} else {
this.avail += to_write;
}
}
}
class GodotProcessor extends AudioWorkletProcessor {
constructor() {
super();
this.threads = false;
this.running = true;
this.lock = null;
this.notifier = null;
this.output = null;
this.output_buffer = new Float32Array();
this.input = null;
this.input_buffer = new Float32Array();
this.port.onmessage = (event) => {
const cmd = event.data['cmd'];
const data = event.data['data'];
this.parse_message(cmd, data);
};
}
process_notify() {
if (this.notifier) {
Atomics.add(this.notifier, 0, 1);
Atomics.notify(this.notifier, 0);
}
}
parse_message(p_cmd, p_data) {
if (p_cmd === 'start' && p_data) {
const state = p_data[0];
let idx = 0;
this.threads = true;
this.lock = state.subarray(idx, ++idx);
this.notifier = state.subarray(idx, ++idx);
const avail_in = state.subarray(idx, ++idx);
const avail_out = state.subarray(idx, ++idx);
this.input = new RingBuffer(p_data[1], avail_in, true);
this.output = new RingBuffer(p_data[2], avail_out, true);
} else if (p_cmd === 'stop') {
this.running = false;
this.output = null;
this.input = null;
this.lock = null;
this.notifier = null;
} else if (p_cmd === 'start_nothreads') {
this.output = new RingBuffer(p_data[0], p_data[0].length, false);
} else if (p_cmd === 'chunk') {
this.output.write(p_data);
}
}
static array_has_data(arr) {
return arr.length && arr[0].length && arr[0][0].length;
}
process(inputs, outputs, parameters) {
if (!this.running) {
return false; // Stop processing.
}
if (this.output === null) {
return true; // Not ready yet, keep processing.
}
const process_input = GodotProcessor.array_has_data(inputs);
if (process_input) {
const input = inputs[0];
const chunk = input[0].length * input.length;
if (this.input_buffer.length !== chunk) {
this.input_buffer = new Float32Array(chunk);
}
if (!this.threads) {
GodotProcessor.write_input(this.input_buffer, input);
this.port.postMessage({ 'cmd': 'input', 'data': this.input_buffer });
} else if (this.input.space_left() >= chunk) {
GodotProcessor.write_input(this.input_buffer, input);
this.input.write(this.input_buffer);
} else {
// this.port.postMessage('Input buffer is full! Skipping input frame.'); // Uncomment this line to debug input buffer.
}
}
const process_output = GodotProcessor.array_has_data(outputs);
if (process_output) {
const output = outputs[0];
const chunk = output[0].length * output.length;
if (this.output_buffer.length !== chunk) {
this.output_buffer = new Float32Array(chunk);
}
if (this.output.data_left() >= chunk) {
this.output.read(this.output_buffer);
GodotProcessor.write_output(output, this.output_buffer);
if (!this.threads) {
this.port.postMessage({ 'cmd': 'read', 'data': chunk });
}
} else {
// this.port.postMessage('Output buffer has not enough frames! Skipping output frame.'); // Uncomment this line to debug output buffer.
}
}
this.process_notify();
return true;
}
static write_output(dest, source) {
const channels = dest.length;
for (let ch = 0; ch < channels; ch++) {
for (let sample = 0; sample < dest[ch].length; sample++) {
dest[ch][sample] = source[sample * channels + ch];
}
}
}
static write_input(dest, source) {
const channels = source.length;
for (let ch = 0; ch < channels; ch++) {
for (let sample = 0; sample < source[ch].length; sample++) {
dest[sample * channels + ch] = source[ch][sample];
}
}
}
}
registerProcessor('godot-processor', GodotProcessor);

File diff suppressed because it is too large Load diff

View file

@ -0,0 +1,801 @@
/**************************************************************************/
/* library_godot_display.js */
/**************************************************************************/
/* 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. */
/**************************************************************************/
const GodotDisplayVK = {
$GodotDisplayVK__deps: ['$GodotRuntime', '$GodotConfig', '$GodotEventListeners'],
$GodotDisplayVK__postset: 'GodotOS.atexit(function(resolve, reject) { GodotDisplayVK.clear(); resolve(); });',
$GodotDisplayVK: {
textinput: null,
textarea: null,
available: function () {
return GodotConfig.virtual_keyboard && 'ontouchstart' in window;
},
init: function (input_cb) {
function create(what) {
const elem = document.createElement(what);
elem.style.display = 'none';
elem.style.position = 'absolute';
elem.style.zIndex = '-1';
elem.style.background = 'transparent';
elem.style.padding = '0px';
elem.style.margin = '0px';
elem.style.overflow = 'hidden';
elem.style.width = '0px';
elem.style.height = '0px';
elem.style.border = '0px';
elem.style.outline = 'none';
elem.readonly = true;
elem.disabled = true;
GodotEventListeners.add(elem, 'input', function (evt) {
const c_str = GodotRuntime.allocString(elem.value);
input_cb(c_str, elem.selectionEnd);
GodotRuntime.free(c_str);
}, false);
GodotEventListeners.add(elem, 'blur', function (evt) {
elem.style.display = 'none';
elem.readonly = true;
elem.disabled = true;
}, false);
GodotConfig.canvas.insertAdjacentElement('beforebegin', elem);
return elem;
}
GodotDisplayVK.textinput = create('input');
GodotDisplayVK.textarea = create('textarea');
GodotDisplayVK.updateSize();
},
show: function (text, type, start, end) {
if (!GodotDisplayVK.textinput || !GodotDisplayVK.textarea) {
return;
}
if (GodotDisplayVK.textinput.style.display !== '' || GodotDisplayVK.textarea.style.display !== '') {
GodotDisplayVK.hide();
}
GodotDisplayVK.updateSize();
let elem = GodotDisplayVK.textinput;
switch (type) {
case 0: // KEYBOARD_TYPE_DEFAULT
elem.type = 'text';
elem.inputmode = '';
break;
case 1: // KEYBOARD_TYPE_MULTILINE
elem = GodotDisplayVK.textarea;
break;
case 2: // KEYBOARD_TYPE_NUMBER
elem.type = 'text';
elem.inputmode = 'numeric';
break;
case 3: // KEYBOARD_TYPE_NUMBER_DECIMAL
elem.type = 'text';
elem.inputmode = 'decimal';
break;
case 4: // KEYBOARD_TYPE_PHONE
elem.type = 'tel';
elem.inputmode = '';
break;
case 5: // KEYBOARD_TYPE_EMAIL_ADDRESS
elem.type = 'email';
elem.inputmode = '';
break;
case 6: // KEYBOARD_TYPE_PASSWORD
elem.type = 'password';
elem.inputmode = '';
break;
case 7: // KEYBOARD_TYPE_URL
elem.type = 'url';
elem.inputmode = '';
break;
default:
elem.type = 'text';
elem.inputmode = '';
break;
}
elem.readonly = false;
elem.disabled = false;
elem.value = text;
elem.style.display = 'block';
elem.focus();
elem.setSelectionRange(start, end);
},
hide: function () {
if (!GodotDisplayVK.textinput || !GodotDisplayVK.textarea) {
return;
}
[GodotDisplayVK.textinput, GodotDisplayVK.textarea].forEach(function (elem) {
elem.blur();
elem.style.display = 'none';
elem.value = '';
});
},
updateSize: function () {
if (!GodotDisplayVK.textinput || !GodotDisplayVK.textarea) {
return;
}
const rect = GodotConfig.canvas.getBoundingClientRect();
function update(elem) {
elem.style.left = `${rect.left}px`;
elem.style.top = `${rect.top}px`;
elem.style.width = `${rect.width}px`;
elem.style.height = `${rect.height}px`;
}
update(GodotDisplayVK.textinput);
update(GodotDisplayVK.textarea);
},
clear: function () {
if (GodotDisplayVK.textinput) {
GodotDisplayVK.textinput.remove();
GodotDisplayVK.textinput = null;
}
if (GodotDisplayVK.textarea) {
GodotDisplayVK.textarea.remove();
GodotDisplayVK.textarea = null;
}
},
},
};
mergeInto(LibraryManager.library, GodotDisplayVK);
/*
* Display server cursor helper.
* Keeps track of cursor status and custom shapes.
*/
const GodotDisplayCursor = {
$GodotDisplayCursor__deps: ['$GodotOS', '$GodotConfig'],
$GodotDisplayCursor__postset: 'GodotOS.atexit(function(resolve, reject) { GodotDisplayCursor.clear(); resolve(); });',
$GodotDisplayCursor: {
shape: 'default',
visible: true,
cursors: {},
set_style: function (style) {
GodotConfig.canvas.style.cursor = style;
},
set_shape: function (shape) {
GodotDisplayCursor.shape = shape;
let css = shape;
if (shape in GodotDisplayCursor.cursors) {
const c = GodotDisplayCursor.cursors[shape];
css = `url("${c.url}") ${c.x} ${c.y}, default`;
}
if (GodotDisplayCursor.visible) {
GodotDisplayCursor.set_style(css);
}
},
clear: function () {
GodotDisplayCursor.set_style('');
GodotDisplayCursor.shape = 'default';
GodotDisplayCursor.visible = true;
Object.keys(GodotDisplayCursor.cursors).forEach(function (key) {
URL.revokeObjectURL(GodotDisplayCursor.cursors[key]);
delete GodotDisplayCursor.cursors[key];
});
},
lockPointer: function () {
const canvas = GodotConfig.canvas;
if (canvas.requestPointerLock) {
canvas.requestPointerLock();
}
},
releasePointer: function () {
if (document.exitPointerLock) {
document.exitPointerLock();
}
},
isPointerLocked: function () {
return document.pointerLockElement === GodotConfig.canvas;
},
},
};
mergeInto(LibraryManager.library, GodotDisplayCursor);
const GodotDisplayScreen = {
$GodotDisplayScreen__deps: ['$GodotConfig', '$GodotOS', '$GL', 'emscripten_webgl_get_current_context'],
$GodotDisplayScreen: {
desired_size: [0, 0],
hidpi: true,
getPixelRatio: function () {
return GodotDisplayScreen.hidpi ? window.devicePixelRatio || 1 : 1;
},
isFullscreen: function () {
const elem = document.fullscreenElement || document.mozFullscreenElement
|| document.webkitFullscreenElement || document.msFullscreenElement;
if (elem) {
return elem === GodotConfig.canvas;
}
// But maybe knowing the element is not supported.
return document.fullscreen || document.mozFullScreen
|| document.webkitIsFullscreen;
},
hasFullscreen: function () {
return document.fullscreenEnabled || document.mozFullScreenEnabled
|| document.webkitFullscreenEnabled;
},
requestFullscreen: function () {
if (!GodotDisplayScreen.hasFullscreen()) {
return 1;
}
const canvas = GodotConfig.canvas;
try {
const promise = (canvas.requestFullscreen || canvas.msRequestFullscreen
|| canvas.mozRequestFullScreen || canvas.mozRequestFullscreen
|| canvas.webkitRequestFullscreen
).call(canvas);
// Some browsers (Safari) return undefined.
// For the standard ones, we need to catch it.
if (promise) {
promise.catch(function () {
// nothing to do.
});
}
} catch (e) {
return 1;
}
return 0;
},
exitFullscreen: function () {
if (!GodotDisplayScreen.isFullscreen()) {
return 0;
}
try {
const promise = document.exitFullscreen();
if (promise) {
promise.catch(function () {
// nothing to do.
});
}
} catch (e) {
return 1;
}
return 0;
},
_updateGL: function () {
const gl_context_handle = _emscripten_webgl_get_current_context();
const gl = GL.getContext(gl_context_handle);
if (gl) {
GL.resizeOffscreenFramebuffer(gl);
}
},
updateSize: function () {
const isFullscreen = GodotDisplayScreen.isFullscreen();
const wantsFullWindow = GodotConfig.canvas_resize_policy === 2;
const noResize = GodotConfig.canvas_resize_policy === 0;
const dWidth = GodotDisplayScreen.desired_size[0];
const dHeight = GodotDisplayScreen.desired_size[1];
const canvas = GodotConfig.canvas;
let width = dWidth;
let height = dHeight;
if (noResize) {
// Don't resize canvas, just update GL if needed.
if (canvas.width !== width || canvas.height !== height) {
GodotDisplayScreen.desired_size = [canvas.width, canvas.height];
GodotDisplayScreen._updateGL();
return 1;
}
return 0;
}
const scale = GodotDisplayScreen.getPixelRatio();
if (isFullscreen || wantsFullWindow) {
// We need to match screen size.
width = window.innerWidth * scale;
height = window.innerHeight * scale;
}
const csw = `${width / scale}px`;
const csh = `${height / scale}px`;
if (canvas.style.width !== csw || canvas.style.height !== csh || canvas.width !== width || canvas.height !== height) {
// Size doesn't match.
// Resize canvas, set correct CSS pixel size, update GL.
canvas.width = width;
canvas.height = height;
canvas.style.width = csw;
canvas.style.height = csh;
GodotDisplayScreen._updateGL();
return 1;
}
return 0;
},
},
};
mergeInto(LibraryManager.library, GodotDisplayScreen);
/**
* Display server interface.
*
* Exposes all the functions needed by DisplayServer implementation.
*/
const GodotDisplay = {
$GodotDisplay__deps: ['$GodotConfig', '$GodotRuntime', '$GodotDisplayCursor', '$GodotEventListeners', '$GodotDisplayScreen', '$GodotDisplayVK'],
$GodotDisplay: {
window_icon: '',
getDPI: function () {
// devicePixelRatio is given in dppx
// https://drafts.csswg.org/css-values/#resolution
// > due to the 1:96 fixed ratio of CSS *in* to CSS *px*, 1dppx is equivalent to 96dpi.
const dpi = Math.round(window.devicePixelRatio * 96);
return dpi >= 96 ? dpi : 96;
},
},
godot_js_display_is_swap_ok_cancel__proxy: 'sync',
godot_js_display_is_swap_ok_cancel__sig: 'i',
godot_js_display_is_swap_ok_cancel: function () {
const win = (['Windows', 'Win64', 'Win32', 'WinCE']);
const plat = navigator.platform || '';
if (win.indexOf(plat) !== -1) {
return 1;
}
return 0;
},
godot_js_tts_is_speaking__proxy: 'sync',
godot_js_tts_is_speaking__sig: 'i',
godot_js_tts_is_speaking: function () {
return window.speechSynthesis.speaking;
},
godot_js_tts_is_paused__proxy: 'sync',
godot_js_tts_is_paused__sig: 'i',
godot_js_tts_is_paused: function () {
return window.speechSynthesis.paused;
},
godot_js_tts_get_voices__proxy: 'sync',
godot_js_tts_get_voices__sig: 'vi',
godot_js_tts_get_voices: function (p_callback) {
const func = GodotRuntime.get_func(p_callback);
try {
const arr = [];
const voices = window.speechSynthesis.getVoices();
for (let i = 0; i < voices.length; i++) {
arr.push(`${voices[i].lang};${voices[i].name}`);
}
const c_ptr = GodotRuntime.allocStringArray(arr);
func(arr.length, c_ptr);
GodotRuntime.freeStringArray(c_ptr, arr.length);
} catch (e) {
// Fail graciously.
}
},
godot_js_tts_speak__proxy: 'sync',
godot_js_tts_speak__sig: 'viiiffii',
godot_js_tts_speak: function (p_text, p_voice, p_volume, p_pitch, p_rate, p_utterance_id, p_callback) {
const func = GodotRuntime.get_func(p_callback);
function listener_end(evt) {
evt.currentTarget.cb(1 /* TTS_UTTERANCE_ENDED */, evt.currentTarget.id, 0);
}
function listener_start(evt) {
evt.currentTarget.cb(0 /* TTS_UTTERANCE_STARTED */, evt.currentTarget.id, 0);
}
function listener_error(evt) {
evt.currentTarget.cb(2 /* TTS_UTTERANCE_CANCELED */, evt.currentTarget.id, 0);
}
function listener_bound(evt) {
evt.currentTarget.cb(3 /* TTS_UTTERANCE_BOUNDARY */, evt.currentTarget.id, evt.charIndex);
}
const utterance = new SpeechSynthesisUtterance(GodotRuntime.parseString(p_text));
utterance.rate = p_rate;
utterance.pitch = p_pitch;
utterance.volume = p_volume / 100.0;
utterance.addEventListener('end', listener_end);
utterance.addEventListener('start', listener_start);
utterance.addEventListener('error', listener_error);
utterance.addEventListener('boundary', listener_bound);
utterance.id = p_utterance_id;
utterance.cb = func;
const voice = GodotRuntime.parseString(p_voice);
const voices = window.speechSynthesis.getVoices();
for (let i = 0; i < voices.length; i++) {
if (voices[i].name === voice) {
utterance.voice = voices[i];
break;
}
}
window.speechSynthesis.resume();
window.speechSynthesis.speak(utterance);
},
godot_js_tts_pause__proxy: 'sync',
godot_js_tts_pause__sig: 'v',
godot_js_tts_pause: function () {
window.speechSynthesis.pause();
},
godot_js_tts_resume__proxy: 'sync',
godot_js_tts_resume__sig: 'v',
godot_js_tts_resume: function () {
window.speechSynthesis.resume();
},
godot_js_tts_stop__proxy: 'sync',
godot_js_tts_stop__sig: 'v',
godot_js_tts_stop: function () {
window.speechSynthesis.cancel();
window.speechSynthesis.resume();
},
godot_js_display_alert__proxy: 'sync',
godot_js_display_alert__sig: 'vi',
godot_js_display_alert: function (p_text) {
window.alert(GodotRuntime.parseString(p_text)); // eslint-disable-line no-alert
},
godot_js_display_screen_dpi_get__proxy: 'sync',
godot_js_display_screen_dpi_get__sig: 'i',
godot_js_display_screen_dpi_get: function () {
return GodotDisplay.getDPI();
},
godot_js_display_pixel_ratio_get__proxy: 'sync',
godot_js_display_pixel_ratio_get__sig: 'f',
godot_js_display_pixel_ratio_get: function () {
return GodotDisplayScreen.getPixelRatio();
},
godot_js_display_fullscreen_request__proxy: 'sync',
godot_js_display_fullscreen_request__sig: 'i',
godot_js_display_fullscreen_request: function () {
return GodotDisplayScreen.requestFullscreen();
},
godot_js_display_fullscreen_exit__proxy: 'sync',
godot_js_display_fullscreen_exit__sig: 'i',
godot_js_display_fullscreen_exit: function () {
return GodotDisplayScreen.exitFullscreen();
},
godot_js_display_desired_size_set__proxy: 'sync',
godot_js_display_desired_size_set__sig: 'vii',
godot_js_display_desired_size_set: function (width, height) {
GodotDisplayScreen.desired_size = [width, height];
GodotDisplayScreen.updateSize();
},
godot_js_display_size_update__proxy: 'sync',
godot_js_display_size_update__sig: 'i',
godot_js_display_size_update: function () {
const updated = GodotDisplayScreen.updateSize();
if (updated) {
GodotDisplayVK.updateSize();
}
return updated;
},
godot_js_display_screen_size_get__proxy: 'sync',
godot_js_display_screen_size_get__sig: 'vii',
godot_js_display_screen_size_get: function (width, height) {
const scale = GodotDisplayScreen.getPixelRatio();
GodotRuntime.setHeapValue(width, window.screen.width * scale, 'i32');
GodotRuntime.setHeapValue(height, window.screen.height * scale, 'i32');
},
godot_js_display_window_size_get__proxy: 'sync',
godot_js_display_window_size_get__sig: 'vii',
godot_js_display_window_size_get: function (p_width, p_height) {
GodotRuntime.setHeapValue(p_width, GodotConfig.canvas.width, 'i32');
GodotRuntime.setHeapValue(p_height, GodotConfig.canvas.height, 'i32');
},
godot_js_display_has_webgl__proxy: 'sync',
godot_js_display_has_webgl__sig: 'ii',
godot_js_display_has_webgl: function (p_version) {
if (p_version !== 1 && p_version !== 2) {
return false;
}
try {
return !!document.createElement('canvas').getContext(p_version === 2 ? 'webgl2' : 'webgl');
} catch (e) { /* Not available */ }
return false;
},
/*
* Canvas
*/
godot_js_display_canvas_focus__proxy: 'sync',
godot_js_display_canvas_focus__sig: 'v',
godot_js_display_canvas_focus: function () {
GodotConfig.canvas.focus();
},
godot_js_display_canvas_is_focused__proxy: 'sync',
godot_js_display_canvas_is_focused__sig: 'i',
godot_js_display_canvas_is_focused: function () {
return document.activeElement === GodotConfig.canvas;
},
/*
* Touchscreen
*/
godot_js_display_touchscreen_is_available__proxy: 'sync',
godot_js_display_touchscreen_is_available__sig: 'i',
godot_js_display_touchscreen_is_available: function () {
return 'ontouchstart' in window;
},
/*
* Clipboard
*/
godot_js_display_clipboard_set__proxy: 'sync',
godot_js_display_clipboard_set__sig: 'ii',
godot_js_display_clipboard_set: function (p_text) {
const text = GodotRuntime.parseString(p_text);
if (!navigator.clipboard || !navigator.clipboard.writeText) {
return 1;
}
navigator.clipboard.writeText(text).catch(function (e) {
// Setting OS clipboard is only possible from an input callback.
GodotRuntime.error('Setting OS clipboard is only possible from an input callback for the Web platform. Exception:', e);
});
return 0;
},
godot_js_display_clipboard_get__proxy: 'sync',
godot_js_display_clipboard_get__sig: 'ii',
godot_js_display_clipboard_get: function (callback) {
const func = GodotRuntime.get_func(callback);
try {
navigator.clipboard.readText().then(function (result) {
const ptr = GodotRuntime.allocString(result);
func(ptr);
GodotRuntime.free(ptr);
}).catch(function (e) {
// Fail graciously.
});
} catch (e) {
// Fail graciously.
}
},
/*
* Window
*/
godot_js_display_window_title_set__proxy: 'sync',
godot_js_display_window_title_set__sig: 'vi',
godot_js_display_window_title_set: function (p_data) {
document.title = GodotRuntime.parseString(p_data);
},
godot_js_display_window_icon_set__proxy: 'sync',
godot_js_display_window_icon_set__sig: 'vii',
godot_js_display_window_icon_set: function (p_ptr, p_len) {
let link = document.getElementById('-gd-engine-icon');
const old_icon = GodotDisplay.window_icon;
if (p_ptr) {
if (link === null) {
link = document.createElement('link');
link.rel = 'icon';
link.id = '-gd-engine-icon';
document.head.appendChild(link);
}
const png = new Blob([GodotRuntime.heapSlice(HEAPU8, p_ptr, p_len)], { type: 'image/png' });
GodotDisplay.window_icon = URL.createObjectURL(png);
link.href = GodotDisplay.window_icon;
} else {
if (link) {
link.remove();
}
GodotDisplay.window_icon = null;
}
if (old_icon) {
URL.revokeObjectURL(old_icon);
}
},
/*
* Cursor
*/
godot_js_display_cursor_set_visible__proxy: 'sync',
godot_js_display_cursor_set_visible__sig: 'vi',
godot_js_display_cursor_set_visible: function (p_visible) {
const visible = p_visible !== 0;
if (visible === GodotDisplayCursor.visible) {
return;
}
GodotDisplayCursor.visible = visible;
if (visible) {
GodotDisplayCursor.set_shape(GodotDisplayCursor.shape);
} else {
GodotDisplayCursor.set_style('none');
}
},
godot_js_display_cursor_is_hidden__proxy: 'sync',
godot_js_display_cursor_is_hidden__sig: 'i',
godot_js_display_cursor_is_hidden: function () {
return !GodotDisplayCursor.visible;
},
godot_js_display_cursor_set_shape__proxy: 'sync',
godot_js_display_cursor_set_shape__sig: 'vi',
godot_js_display_cursor_set_shape: function (p_string) {
GodotDisplayCursor.set_shape(GodotRuntime.parseString(p_string));
},
godot_js_display_cursor_set_custom_shape__proxy: 'sync',
godot_js_display_cursor_set_custom_shape__sig: 'viiiii',
godot_js_display_cursor_set_custom_shape: function (p_shape, p_ptr, p_len, p_hotspot_x, p_hotspot_y) {
const shape = GodotRuntime.parseString(p_shape);
const old_shape = GodotDisplayCursor.cursors[shape];
if (p_len > 0) {
const png = new Blob([GodotRuntime.heapSlice(HEAPU8, p_ptr, p_len)], { type: 'image/png' });
const url = URL.createObjectURL(png);
GodotDisplayCursor.cursors[shape] = {
url: url,
x: p_hotspot_x,
y: p_hotspot_y,
};
} else {
delete GodotDisplayCursor.cursors[shape];
}
if (shape === GodotDisplayCursor.shape) {
GodotDisplayCursor.set_shape(GodotDisplayCursor.shape);
}
if (old_shape) {
URL.revokeObjectURL(old_shape.url);
}
},
godot_js_display_cursor_lock_set__proxy: 'sync',
godot_js_display_cursor_lock_set__sig: 'vi',
godot_js_display_cursor_lock_set: function (p_lock) {
if (p_lock) {
GodotDisplayCursor.lockPointer();
} else {
GodotDisplayCursor.releasePointer();
}
},
godot_js_display_cursor_is_locked__proxy: 'sync',
godot_js_display_cursor_is_locked__sig: 'i',
godot_js_display_cursor_is_locked: function () {
return GodotDisplayCursor.isPointerLocked() ? 1 : 0;
},
/*
* Listeners
*/
godot_js_display_fullscreen_cb__proxy: 'sync',
godot_js_display_fullscreen_cb__sig: 'vi',
godot_js_display_fullscreen_cb: function (callback) {
const canvas = GodotConfig.canvas;
const func = GodotRuntime.get_func(callback);
function change_cb(evt) {
if (evt.target === canvas) {
func(GodotDisplayScreen.isFullscreen());
}
}
GodotEventListeners.add(document, 'fullscreenchange', change_cb, false);
GodotEventListeners.add(document, 'mozfullscreenchange', change_cb, false);
GodotEventListeners.add(document, 'webkitfullscreenchange', change_cb, false);
},
godot_js_display_window_blur_cb__proxy: 'sync',
godot_js_display_window_blur_cb__sig: 'vi',
godot_js_display_window_blur_cb: function (callback) {
const func = GodotRuntime.get_func(callback);
GodotEventListeners.add(window, 'blur', function () {
func();
}, false);
},
godot_js_display_notification_cb__proxy: 'sync',
godot_js_display_notification_cb__sig: 'viiiii',
godot_js_display_notification_cb: function (callback, p_enter, p_exit, p_in, p_out) {
const canvas = GodotConfig.canvas;
const func = GodotRuntime.get_func(callback);
const notif = [p_enter, p_exit, p_in, p_out];
['mouseover', 'mouseleave', 'focus', 'blur'].forEach(function (evt_name, idx) {
GodotEventListeners.add(canvas, evt_name, function () {
func(notif[idx]);
}, true);
});
},
godot_js_display_setup_canvas__proxy: 'sync',
godot_js_display_setup_canvas__sig: 'viiii',
godot_js_display_setup_canvas: function (p_width, p_height, p_fullscreen, p_hidpi) {
const canvas = GodotConfig.canvas;
GodotEventListeners.add(canvas, 'contextmenu', function (ev) {
ev.preventDefault();
}, false);
GodotEventListeners.add(canvas, 'webglcontextlost', function (ev) {
alert('WebGL context lost, please reload the page'); // eslint-disable-line no-alert
ev.preventDefault();
}, false);
GodotDisplayScreen.hidpi = !!p_hidpi;
switch (GodotConfig.canvas_resize_policy) {
case 0: // None
GodotDisplayScreen.desired_size = [canvas.width, canvas.height];
break;
case 1: // Project
GodotDisplayScreen.desired_size = [p_width, p_height];
break;
default: // Full window
// Ensure we display in the right place, the size will be handled by updateSize
canvas.style.position = 'absolute';
canvas.style.top = 0;
canvas.style.left = 0;
break;
}
GodotDisplayScreen.updateSize();
if (p_fullscreen) {
GodotDisplayScreen.requestFullscreen();
}
},
/*
* Virtual Keyboard
*/
godot_js_display_vk_show__proxy: 'sync',
godot_js_display_vk_show__sig: 'viiii',
godot_js_display_vk_show: function (p_text, p_type, p_start, p_end) {
const text = GodotRuntime.parseString(p_text);
const start = p_start > 0 ? p_start : 0;
const end = p_end > 0 ? p_end : start;
GodotDisplayVK.show(text, p_type, start, end);
},
godot_js_display_vk_hide__proxy: 'sync',
godot_js_display_vk_hide__sig: 'v',
godot_js_display_vk_hide: function () {
GodotDisplayVK.hide();
},
godot_js_display_vk_available__proxy: 'sync',
godot_js_display_vk_available__sig: 'i',
godot_js_display_vk_available: function () {
return GodotDisplayVK.available();
},
godot_js_display_tts_available__proxy: 'sync',
godot_js_display_tts_available__sig: 'i',
godot_js_display_tts_available: function () {
return 'speechSynthesis' in window;
},
godot_js_display_vk_cb__proxy: 'sync',
godot_js_display_vk_cb__sig: 'vi',
godot_js_display_vk_cb: function (p_input_cb) {
const input_cb = GodotRuntime.get_func(p_input_cb);
if (GodotDisplayVK.available()) {
GodotDisplayVK.init(input_cb);
}
},
};
autoAddDeps(GodotDisplay, '$GodotDisplay');
mergeInto(LibraryManager.library, GodotDisplay);

View file

@ -0,0 +1,244 @@
/**************************************************************************/
/* library_godot_fetch.js */
/**************************************************************************/
/* 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. */
/**************************************************************************/
const GodotFetch = {
$GodotFetch__deps: ['$IDHandler', '$GodotRuntime'],
$GodotFetch: {
onread: function (id, result) {
const obj = IDHandler.get(id);
if (!obj) {
return;
}
if (result.value) {
obj.chunks.push(result.value);
}
obj.reading = false;
obj.done = result.done;
},
onresponse: function (id, response) {
const obj = IDHandler.get(id);
if (!obj) {
return;
}
let chunked = false;
response.headers.forEach(function (value, header) {
const v = value.toLowerCase().trim();
const h = header.toLowerCase().trim();
if (h === 'transfer-encoding' && v === 'chunked') {
chunked = true;
}
});
obj.status = response.status;
obj.response = response;
obj.reader = response.body.getReader();
obj.chunked = chunked;
},
onerror: function (id, err) {
GodotRuntime.error(err);
const obj = IDHandler.get(id);
if (!obj) {
return;
}
obj.error = err;
},
create: function (method, url, headers, body) {
const obj = {
request: null,
response: null,
reader: null,
error: null,
done: false,
reading: false,
status: 0,
chunks: [],
};
const id = IDHandler.add(obj);
const init = {
method: method,
headers: headers,
body: body,
};
obj.request = fetch(url, init);
obj.request.then(GodotFetch.onresponse.bind(null, id)).catch(GodotFetch.onerror.bind(null, id));
return id;
},
free: function (id) {
const obj = IDHandler.get(id);
if (!obj) {
return;
}
IDHandler.remove(id);
if (!obj.request) {
return;
}
// Try to abort
obj.request.then(function (response) {
response.abort();
}).catch(function (e) { /* nothing to do */ });
},
read: function (id) {
const obj = IDHandler.get(id);
if (!obj) {
return;
}
if (obj.reader && !obj.reading) {
if (obj.done) {
obj.reader = null;
return;
}
obj.reading = true;
obj.reader.read().then(GodotFetch.onread.bind(null, id)).catch(GodotFetch.onerror.bind(null, id));
}
},
},
godot_js_fetch_create__proxy: 'sync',
godot_js_fetch_create__sig: 'iiiiiii',
godot_js_fetch_create: function (p_method, p_url, p_headers, p_headers_size, p_body, p_body_size) {
const method = GodotRuntime.parseString(p_method);
const url = GodotRuntime.parseString(p_url);
const headers = GodotRuntime.parseStringArray(p_headers, p_headers_size);
const body = p_body_size ? GodotRuntime.heapSlice(HEAP8, p_body, p_body_size) : null;
return GodotFetch.create(method, url, headers.map(function (hv) {
const idx = hv.indexOf(':');
if (idx <= 0) {
return [];
}
return [
hv.slice(0, idx).trim(),
hv.slice(idx + 1).trim(),
];
}).filter(function (v) {
return v.length === 2;
}), body);
},
godot_js_fetch_state_get__proxy: 'sync',
godot_js_fetch_state_get__sig: 'ii',
godot_js_fetch_state_get: function (p_id) {
const obj = IDHandler.get(p_id);
if (!obj) {
return -1;
}
if (obj.error) {
return -1;
}
if (!obj.response) {
return 0;
}
if (obj.reader) {
return 1;
}
if (obj.done) {
return 2;
}
return -1;
},
godot_js_fetch_http_status_get__proxy: 'sync',
godot_js_fetch_http_status_get__sig: 'ii',
godot_js_fetch_http_status_get: function (p_id) {
const obj = IDHandler.get(p_id);
if (!obj || !obj.response) {
return 0;
}
return obj.status;
},
godot_js_fetch_read_headers__proxy: 'sync',
godot_js_fetch_read_headers__sig: 'iiii',
godot_js_fetch_read_headers: function (p_id, p_parse_cb, p_ref) {
const obj = IDHandler.get(p_id);
if (!obj || !obj.response) {
return 1;
}
const cb = GodotRuntime.get_func(p_parse_cb);
const arr = [];
obj.response.headers.forEach(function (v, h) {
arr.push(`${h}:${v}`);
});
const c_ptr = GodotRuntime.allocStringArray(arr);
cb(arr.length, c_ptr, p_ref);
GodotRuntime.freeStringArray(c_ptr, arr.length);
return 0;
},
godot_js_fetch_read_chunk__proxy: 'sync',
godot_js_fetch_read_chunk__sig: 'iiii',
godot_js_fetch_read_chunk: function (p_id, p_buf, p_buf_size) {
const obj = IDHandler.get(p_id);
if (!obj || !obj.response) {
return 0;
}
let to_read = p_buf_size;
const chunks = obj.chunks;
while (to_read && chunks.length) {
const chunk = obj.chunks[0];
if (chunk.length > to_read) {
GodotRuntime.heapCopy(HEAP8, chunk.slice(0, to_read), p_buf);
chunks[0] = chunk.slice(to_read);
to_read = 0;
} else {
GodotRuntime.heapCopy(HEAP8, chunk, p_buf);
to_read -= chunk.length;
chunks.pop();
}
}
if (!chunks.length) {
GodotFetch.read(p_id);
}
return p_buf_size - to_read;
},
godot_js_fetch_is_chunked__proxy: 'sync',
godot_js_fetch_is_chunked__sig: 'ii',
godot_js_fetch_is_chunked: function (p_id) {
const obj = IDHandler.get(p_id);
if (!obj || !obj.response) {
return -1;
}
return obj.chunked ? 1 : 0;
},
godot_js_fetch_free__proxy: 'sync',
godot_js_fetch_free__sig: 'vi',
godot_js_fetch_free: function (id) {
GodotFetch.free(id);
},
};
autoAddDeps(GodotFetch, '$GodotFetch');
mergeInto(LibraryManager.library, GodotFetch);

View file

@ -0,0 +1,704 @@
/**************************************************************************/
/* library_godot_input.js */
/**************************************************************************/
/* 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. */
/**************************************************************************/
/*
* IME API helper.
*/
const GodotIME = {
$GodotIME__deps: ['$GodotRuntime', '$GodotEventListeners'],
$GodotIME__postset: 'GodotOS.atexit(function(resolve, reject) { GodotIME.clear(); resolve(); });',
$GodotIME: {
ime: null,
active: false,
getModifiers: function (evt) {
return (evt.shiftKey + 0) + ((evt.altKey + 0) << 1) + ((evt.ctrlKey + 0) << 2) + ((evt.metaKey + 0) << 3);
},
ime_active: function (active) {
function focus_timer() {
GodotIME.active = true;
GodotIME.ime.focus();
}
if (GodotIME.ime) {
if (active) {
GodotIME.ime.style.display = 'block';
setInterval(focus_timer, 100);
} else {
GodotIME.ime.style.display = 'none';
GodotConfig.canvas.focus();
GodotIME.active = false;
}
}
},
ime_position: function (x, y) {
if (GodotIME.ime) {
const canvas = GodotConfig.canvas;
const rect = canvas.getBoundingClientRect();
const rw = canvas.width / rect.width;
const rh = canvas.height / rect.height;
const clx = (x / rw) + rect.x;
const cly = (y / rh) + rect.y;
GodotIME.ime.style.left = `${clx}px`;
GodotIME.ime.style.top = `${cly}px`;
}
},
init: function (ime_cb, key_cb, code, key) {
function key_event_cb(pressed, evt) {
const modifiers = GodotIME.getModifiers(evt);
GodotRuntime.stringToHeap(evt.code, code, 32);
GodotRuntime.stringToHeap(evt.key, key, 32);
key_cb(pressed, evt.repeat, modifiers);
evt.preventDefault();
}
function ime_event_cb(event) {
if (GodotIME.ime) {
if (event.type === 'compositionstart') {
ime_cb(0, null);
GodotIME.ime.innerHTML = '';
} else if (event.type === 'compositionupdate') {
const ptr = GodotRuntime.allocString(event.data);
ime_cb(1, ptr);
GodotRuntime.free(ptr);
} else if (event.type === 'compositionend') {
const ptr = GodotRuntime.allocString(event.data);
ime_cb(2, ptr);
GodotRuntime.free(ptr);
GodotIME.ime.innerHTML = '';
}
}
}
const ime = document.createElement('div');
ime.className = 'ime';
ime.style.background = 'none';
ime.style.opacity = 0.0;
ime.style.position = 'fixed';
ime.style.textAlign = 'left';
ime.style.fontSize = '1px';
ime.style.left = '0px';
ime.style.top = '0px';
ime.style.width = '100%';
ime.style.height = '40px';
ime.style.pointerEvents = 'none';
ime.style.display = 'none';
ime.contentEditable = 'true';
GodotEventListeners.add(ime, 'compositionstart', ime_event_cb, false);
GodotEventListeners.add(ime, 'compositionupdate', ime_event_cb, false);
GodotEventListeners.add(ime, 'compositionend', ime_event_cb, false);
GodotEventListeners.add(ime, 'keydown', key_event_cb.bind(null, 1), false);
GodotEventListeners.add(ime, 'keyup', key_event_cb.bind(null, 0), false);
ime.onblur = function () {
this.style.display = 'none';
GodotConfig.canvas.focus();
GodotIME.active = false;
};
GodotConfig.canvas.parentElement.appendChild(ime);
GodotIME.ime = ime;
},
clear: function () {
if (GodotIME.ime) {
GodotIME.ime.remove();
GodotIME.ime = null;
}
},
},
};
mergeInto(LibraryManager.library, GodotIME);
/*
* Gamepad API helper.
*/
const GodotInputGamepads = {
$GodotInputGamepads__deps: ['$GodotRuntime', '$GodotEventListeners'],
$GodotInputGamepads: {
samples: [],
get_pads: function () {
try {
// Will throw in iframe when permission is denied.
// Will throw/warn in the future for insecure contexts.
// See https://github.com/w3c/gamepad/pull/120
const pads = navigator.getGamepads();
if (pads) {
return pads;
}
return [];
} catch (e) {
return [];
}
},
get_samples: function () {
return GodotInputGamepads.samples;
},
get_sample: function (index) {
const samples = GodotInputGamepads.samples;
return index < samples.length ? samples[index] : null;
},
sample: function () {
const pads = GodotInputGamepads.get_pads();
const samples = [];
for (let i = 0; i < pads.length; i++) {
const pad = pads[i];
if (!pad) {
samples.push(null);
continue;
}
const s = {
standard: pad.mapping === 'standard',
buttons: [],
axes: [],
connected: pad.connected,
};
for (let b = 0; b < pad.buttons.length; b++) {
s.buttons.push(pad.buttons[b].value);
}
for (let a = 0; a < pad.axes.length; a++) {
s.axes.push(pad.axes[a]);
}
samples.push(s);
}
GodotInputGamepads.samples = samples;
},
init: function (onchange) {
GodotInputGamepads.samples = [];
function add(pad) {
const guid = GodotInputGamepads.get_guid(pad);
const c_id = GodotRuntime.allocString(pad.id);
const c_guid = GodotRuntime.allocString(guid);
onchange(pad.index, 1, c_id, c_guid);
GodotRuntime.free(c_id);
GodotRuntime.free(c_guid);
}
const pads = GodotInputGamepads.get_pads();
for (let i = 0; i < pads.length; i++) {
// Might be reserved space.
if (pads[i]) {
add(pads[i]);
}
}
GodotEventListeners.add(window, 'gamepadconnected', function (evt) {
if (evt.gamepad) {
add(evt.gamepad);
}
}, false);
GodotEventListeners.add(window, 'gamepaddisconnected', function (evt) {
if (evt.gamepad) {
onchange(evt.gamepad.index, 0);
}
}, false);
},
get_guid: function (pad) {
if (pad.mapping) {
return pad.mapping;
}
const ua = navigator.userAgent;
let os = 'Unknown';
if (ua.indexOf('Android') >= 0) {
os = 'Android';
} else if (ua.indexOf('Linux') >= 0) {
os = 'Linux';
} else if (ua.indexOf('iPhone') >= 0) {
os = 'iOS';
} else if (ua.indexOf('Macintosh') >= 0) {
// Updated iPads will fall into this category.
os = 'MacOSX';
} else if (ua.indexOf('Windows') >= 0) {
os = 'Windows';
}
const id = pad.id;
// Chrom* style: NAME (Vendor: xxxx Product: xxxx).
const exp1 = /vendor: ([0-9a-f]{4}) product: ([0-9a-f]{4})/i;
// Firefox/Safari style (Safari may remove leading zeroes).
const exp2 = /^([0-9a-f]+)-([0-9a-f]+)-/i;
let vendor = '';
let product = '';
if (exp1.test(id)) {
const match = exp1.exec(id);
vendor = match[1].padStart(4, '0');
product = match[2].padStart(4, '0');
} else if (exp2.test(id)) {
const match = exp2.exec(id);
vendor = match[1].padStart(4, '0');
product = match[2].padStart(4, '0');
}
if (!vendor || !product) {
return `${os}Unknown`;
}
return os + vendor + product;
},
},
};
mergeInto(LibraryManager.library, GodotInputGamepads);
/*
* Drag and drop helper.
* This is pretty big, but basically detect dropped files on GodotConfig.canvas,
* process them one by one (recursively for directories), and copies them to
* the temporary FS path '/tmp/drop-[random]/' so it can be emitted as a godot
* event (that requires a string array of paths).
*
* NOTE: The temporary files are removed after the callback. This means that
* deferred callbacks won't be able to access the files.
*/
const GodotInputDragDrop = {
$GodotInputDragDrop__deps: ['$FS', '$GodotFS'],
$GodotInputDragDrop: {
promises: [],
pending_files: [],
add_entry: function (entry) {
if (entry.isDirectory) {
GodotInputDragDrop.add_dir(entry);
} else if (entry.isFile) {
GodotInputDragDrop.add_file(entry);
} else {
GodotRuntime.error('Unrecognized entry...', entry);
}
},
add_dir: function (entry) {
GodotInputDragDrop.promises.push(new Promise(function (resolve, reject) {
const reader = entry.createReader();
reader.readEntries(function (entries) {
for (let i = 0; i < entries.length; i++) {
GodotInputDragDrop.add_entry(entries[i]);
}
resolve();
});
}));
},
add_file: function (entry) {
GodotInputDragDrop.promises.push(new Promise(function (resolve, reject) {
entry.file(function (file) {
const reader = new FileReader();
reader.onload = function () {
const f = {
'path': file.relativePath || file.webkitRelativePath,
'name': file.name,
'type': file.type,
'size': file.size,
'data': reader.result,
};
if (!f['path']) {
f['path'] = f['name'];
}
GodotInputDragDrop.pending_files.push(f);
resolve();
};
reader.onerror = function () {
GodotRuntime.print('Error reading file');
reject();
};
reader.readAsArrayBuffer(file);
}, function (err) {
GodotRuntime.print('Error!');
reject();
});
}));
},
process: function (resolve, reject) {
if (GodotInputDragDrop.promises.length === 0) {
resolve();
return;
}
GodotInputDragDrop.promises.pop().then(function () {
setTimeout(function () {
GodotInputDragDrop.process(resolve, reject);
}, 0);
});
},
_process_event: function (ev, callback) {
ev.preventDefault();
if (ev.dataTransfer.items) {
// Use DataTransferItemList interface to access the file(s)
for (let i = 0; i < ev.dataTransfer.items.length; i++) {
const item = ev.dataTransfer.items[i];
let entry = null;
if ('getAsEntry' in item) {
entry = item.getAsEntry();
} else if ('webkitGetAsEntry' in item) {
entry = item.webkitGetAsEntry();
}
if (entry) {
GodotInputDragDrop.add_entry(entry);
}
}
} else {
GodotRuntime.error('File upload not supported');
}
new Promise(GodotInputDragDrop.process).then(function () {
const DROP = `/tmp/drop-${parseInt(Math.random() * (1 << 30), 10)}/`;
const drops = [];
const files = [];
FS.mkdir(DROP.slice(0, -1)); // Without trailing slash
GodotInputDragDrop.pending_files.forEach((elem) => {
const path = elem['path'];
GodotFS.copy_to_fs(DROP + path, elem['data']);
let idx = path.indexOf('/');
if (idx === -1) {
// Root file
drops.push(DROP + path);
} else {
// Subdir
const sub = path.substr(0, idx);
idx = sub.indexOf('/');
if (idx < 0 && drops.indexOf(DROP + sub) === -1) {
drops.push(DROP + sub);
}
}
files.push(DROP + path);
});
GodotInputDragDrop.promises = [];
GodotInputDragDrop.pending_files = [];
callback(drops);
if (GodotConfig.persistent_drops) {
// Delay removal at exit.
GodotOS.atexit(function (resolve, reject) {
GodotInputDragDrop.remove_drop(files, DROP);
resolve();
});
} else {
GodotInputDragDrop.remove_drop(files, DROP);
}
});
},
remove_drop: function (files, drop_path) {
const dirs = [drop_path.substr(0, drop_path.length - 1)];
// Remove temporary files
files.forEach(function (file) {
FS.unlink(file);
let dir = file.replace(drop_path, '');
let idx = dir.lastIndexOf('/');
while (idx > 0) {
dir = dir.substr(0, idx);
if (dirs.indexOf(drop_path + dir) === -1) {
dirs.push(drop_path + dir);
}
idx = dir.lastIndexOf('/');
}
});
// Remove dirs.
dirs.sort(function (a, b) {
const al = (a.match(/\//g) || []).length;
const bl = (b.match(/\//g) || []).length;
if (al > bl) {
return -1;
} else if (al < bl) {
return 1;
}
return 0;
}).forEach(function (dir) {
FS.rmdir(dir);
});
},
handler: function (callback) {
return function (ev) {
GodotInputDragDrop._process_event(ev, callback);
};
},
},
};
mergeInto(LibraryManager.library, GodotInputDragDrop);
/*
* Godot exposed input functions.
*/
const GodotInput = {
$GodotInput__deps: ['$GodotRuntime', '$GodotConfig', '$GodotEventListeners', '$GodotInputGamepads', '$GodotInputDragDrop', '$GodotIME'],
$GodotInput: {
getModifiers: function (evt) {
return (evt.shiftKey + 0) + ((evt.altKey + 0) << 1) + ((evt.ctrlKey + 0) << 2) + ((evt.metaKey + 0) << 3);
},
computePosition: function (evt, rect) {
const canvas = GodotConfig.canvas;
const rw = canvas.width / rect.width;
const rh = canvas.height / rect.height;
const x = (evt.clientX - rect.x) * rw;
const y = (evt.clientY - rect.y) * rh;
return [x, y];
},
},
/*
* Mouse API
*/
godot_js_input_mouse_move_cb__proxy: 'sync',
godot_js_input_mouse_move_cb__sig: 'vi',
godot_js_input_mouse_move_cb: function (callback) {
const func = GodotRuntime.get_func(callback);
const canvas = GodotConfig.canvas;
function move_cb(evt) {
const rect = canvas.getBoundingClientRect();
const pos = GodotInput.computePosition(evt, rect);
// Scale movement
const rw = canvas.width / rect.width;
const rh = canvas.height / rect.height;
const rel_pos_x = evt.movementX * rw;
const rel_pos_y = evt.movementY * rh;
const modifiers = GodotInput.getModifiers(evt);
func(pos[0], pos[1], rel_pos_x, rel_pos_y, modifiers);
}
GodotEventListeners.add(window, 'mousemove', move_cb, false);
},
godot_js_input_mouse_wheel_cb__proxy: 'sync',
godot_js_input_mouse_wheel_cb__sig: 'vi',
godot_js_input_mouse_wheel_cb: function (callback) {
const func = GodotRuntime.get_func(callback);
function wheel_cb(evt) {
if (func(evt['deltaX'] || 0, evt['deltaY'] || 0)) {
evt.preventDefault();
}
}
GodotEventListeners.add(GodotConfig.canvas, 'wheel', wheel_cb, false);
},
godot_js_input_mouse_button_cb__proxy: 'sync',
godot_js_input_mouse_button_cb__sig: 'vi',
godot_js_input_mouse_button_cb: function (callback) {
const func = GodotRuntime.get_func(callback);
const canvas = GodotConfig.canvas;
function button_cb(p_pressed, evt) {
const rect = canvas.getBoundingClientRect();
const pos = GodotInput.computePosition(evt, rect);
const modifiers = GodotInput.getModifiers(evt);
// Since the event is consumed, focus manually.
// NOTE: The iframe container may not have focus yet, so focus even when already active.
if (p_pressed) {
GodotConfig.canvas.focus();
}
if (func(p_pressed, evt.button, pos[0], pos[1], modifiers)) {
evt.preventDefault();
}
}
GodotEventListeners.add(canvas, 'mousedown', button_cb.bind(null, 1), false);
GodotEventListeners.add(window, 'mouseup', button_cb.bind(null, 0), false);
},
/*
* Touch API
*/
godot_js_input_touch_cb__proxy: 'sync',
godot_js_input_touch_cb__sig: 'viii',
godot_js_input_touch_cb: function (callback, ids, coords) {
const func = GodotRuntime.get_func(callback);
const canvas = GodotConfig.canvas;
function touch_cb(type, evt) {
// Since the event is consumed, focus manually.
// NOTE: The iframe container may not have focus yet, so focus even when already active.
if (type === 0) {
GodotConfig.canvas.focus();
}
const rect = canvas.getBoundingClientRect();
const touches = evt.changedTouches;
for (let i = 0; i < touches.length; i++) {
const touch = touches[i];
const pos = GodotInput.computePosition(touch, rect);
GodotRuntime.setHeapValue(coords + (i * 2) * 8, pos[0], 'double');
GodotRuntime.setHeapValue(coords + (i * 2 + 1) * 8, pos[1], 'double');
GodotRuntime.setHeapValue(ids + i * 4, touch.identifier, 'i32');
}
func(type, touches.length);
if (evt.cancelable) {
evt.preventDefault();
}
}
GodotEventListeners.add(canvas, 'touchstart', touch_cb.bind(null, 0), false);
GodotEventListeners.add(canvas, 'touchend', touch_cb.bind(null, 1), false);
GodotEventListeners.add(canvas, 'touchcancel', touch_cb.bind(null, 1), false);
GodotEventListeners.add(canvas, 'touchmove', touch_cb.bind(null, 2), false);
},
/*
* Key API
*/
godot_js_input_key_cb__proxy: 'sync',
godot_js_input_key_cb__sig: 'viii',
godot_js_input_key_cb: function (callback, code, key) {
const func = GodotRuntime.get_func(callback);
function key_cb(pressed, evt) {
const modifiers = GodotInput.getModifiers(evt);
GodotRuntime.stringToHeap(evt.code, code, 32);
GodotRuntime.stringToHeap(evt.key, key, 32);
func(pressed, evt.repeat, modifiers);
evt.preventDefault();
}
GodotEventListeners.add(GodotConfig.canvas, 'keydown', key_cb.bind(null, 1), false);
GodotEventListeners.add(GodotConfig.canvas, 'keyup', key_cb.bind(null, 0), false);
},
/*
* IME API
*/
godot_js_set_ime_active__proxy: 'sync',
godot_js_set_ime_active__sig: 'vi',
godot_js_set_ime_active: function (p_active) {
GodotIME.ime_active(p_active);
},
godot_js_set_ime_position__proxy: 'sync',
godot_js_set_ime_position__sig: 'vii',
godot_js_set_ime_position: function (p_x, p_y) {
GodotIME.ime_position(p_x, p_y);
},
godot_js_set_ime_cb__proxy: 'sync',
godot_js_set_ime_cb__sig: 'viiii',
godot_js_set_ime_cb: function (p_ime_cb, p_key_cb, code, key) {
const ime_cb = GodotRuntime.get_func(p_ime_cb);
const key_cb = GodotRuntime.get_func(p_key_cb);
GodotIME.init(ime_cb, key_cb, code, key);
},
godot_js_is_ime_focused__proxy: 'sync',
godot_js_is_ime_focused__sig: 'i',
godot_js_is_ime_focused: function () {
return GodotIME.active;
},
/*
* Gamepad API
*/
godot_js_input_gamepad_cb__proxy: 'sync',
godot_js_input_gamepad_cb__sig: 'vi',
godot_js_input_gamepad_cb: function (change_cb) {
const onchange = GodotRuntime.get_func(change_cb);
GodotInputGamepads.init(onchange);
},
godot_js_input_gamepad_sample_count__proxy: 'sync',
godot_js_input_gamepad_sample_count__sig: 'i',
godot_js_input_gamepad_sample_count: function () {
return GodotInputGamepads.get_samples().length;
},
godot_js_input_gamepad_sample__proxy: 'sync',
godot_js_input_gamepad_sample__sig: 'i',
godot_js_input_gamepad_sample: function () {
GodotInputGamepads.sample();
return 0;
},
godot_js_input_gamepad_sample_get__proxy: 'sync',
godot_js_input_gamepad_sample_get__sig: 'iiiiiii',
godot_js_input_gamepad_sample_get: function (p_index, r_btns, r_btns_num, r_axes, r_axes_num, r_standard) {
const sample = GodotInputGamepads.get_sample(p_index);
if (!sample || !sample.connected) {
return 1;
}
const btns = sample.buttons;
const btns_len = btns.length < 16 ? btns.length : 16;
for (let i = 0; i < btns_len; i++) {
GodotRuntime.setHeapValue(r_btns + (i << 2), btns[i], 'float');
}
GodotRuntime.setHeapValue(r_btns_num, btns_len, 'i32');
const axes = sample.axes;
const axes_len = axes.length < 10 ? axes.length : 10;
for (let i = 0; i < axes_len; i++) {
GodotRuntime.setHeapValue(r_axes + (i << 2), axes[i], 'float');
}
GodotRuntime.setHeapValue(r_axes_num, axes_len, 'i32');
const is_standard = sample.standard ? 1 : 0;
GodotRuntime.setHeapValue(r_standard, is_standard, 'i32');
return 0;
},
/*
* Drag/Drop API
*/
godot_js_input_drop_files_cb__proxy: 'sync',
godot_js_input_drop_files_cb__sig: 'vi',
godot_js_input_drop_files_cb: function (callback) {
const func = GodotRuntime.get_func(callback);
const dropFiles = function (files) {
const args = files || [];
if (!args.length) {
return;
}
const argc = args.length;
const argv = GodotRuntime.allocStringArray(args);
func(argv, argc);
GodotRuntime.freeStringArray(argv, argc);
};
const canvas = GodotConfig.canvas;
GodotEventListeners.add(canvas, 'dragover', function (ev) {
// Prevent default behavior (which would try to open the file(s))
ev.preventDefault();
}, false);
GodotEventListeners.add(canvas, 'drop', GodotInputDragDrop.handler(dropFiles));
},
/* Paste API */
godot_js_input_paste_cb__proxy: 'sync',
godot_js_input_paste_cb__sig: 'vi',
godot_js_input_paste_cb: function (callback) {
const func = GodotRuntime.get_func(callback);
GodotEventListeners.add(window, 'paste', function (evt) {
const text = evt.clipboardData.getData('text');
const ptr = GodotRuntime.allocString(text);
func(ptr);
GodotRuntime.free(ptr);
}, false);
},
godot_js_input_vibrate_handheld__proxy: 'sync',
godot_js_input_vibrate_handheld__sig: 'vi',
godot_js_input_vibrate_handheld: function (p_duration_ms) {
if (typeof navigator.vibrate !== 'function') {
GodotRuntime.print('This browser does not support vibration.');
} else {
navigator.vibrate(p_duration_ms);
}
},
};
autoAddDeps(GodotInput, '$GodotInput');
mergeInto(LibraryManager.library, GodotInput);

View file

@ -0,0 +1,366 @@
/**************************************************************************/
/* library_godot_javascript_singleton.js */
/**************************************************************************/
/* 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. */
/**************************************************************************/
const GodotJSWrapper = {
$GodotJSWrapper__deps: ['$GodotRuntime', '$IDHandler'],
$GodotJSWrapper__postset: 'GodotJSWrapper.proxies = new Map();',
$GodotJSWrapper: {
proxies: null,
cb_ret: null,
MyProxy: function (val) {
const id = IDHandler.add(this);
GodotJSWrapper.proxies.set(val, id);
let refs = 1;
this.ref = function () {
refs++;
};
this.unref = function () {
refs--;
if (refs === 0) {
IDHandler.remove(id);
GodotJSWrapper.proxies.delete(val);
}
};
this.get_val = function () {
return val;
};
this.get_id = function () {
return id;
};
},
get_proxied: function (val) {
const id = GodotJSWrapper.proxies.get(val);
if (id === undefined) {
const proxy = new GodotJSWrapper.MyProxy(val);
return proxy.get_id();
}
IDHandler.get(id).ref();
return id;
},
get_proxied_value: function (id) {
const proxy = IDHandler.get(id);
if (proxy === undefined) {
return undefined;
}
return proxy.get_val();
},
variant2js: function (type, val) {
switch (type) {
case 0:
return null;
case 1:
return Boolean(GodotRuntime.getHeapValue(val, 'i64'));
case 2: {
// `heap_value` may be a bigint.
const heap_value = GodotRuntime.getHeapValue(val, 'i64');
return heap_value >= Number.MIN_SAFE_INTEGER && heap_value <= Number.MAX_SAFE_INTEGER
? Number(heap_value)
: heap_value;
}
case 3:
return Number(GodotRuntime.getHeapValue(val, 'double'));
case 4:
return GodotRuntime.parseString(GodotRuntime.getHeapValue(val, '*'));
case 24: // OBJECT
return GodotJSWrapper.get_proxied_value(GodotRuntime.getHeapValue(val, 'i64'));
default:
return undefined;
}
},
js2variant: function (p_val, p_exchange) {
if (p_val === undefined || p_val === null) {
return 0; // NIL
}
const type = typeof (p_val);
if (type === 'boolean') {
GodotRuntime.setHeapValue(p_exchange, p_val, 'i64');
return 1; // BOOL
} else if (type === 'number') {
if (Number.isInteger(p_val)) {
GodotRuntime.setHeapValue(p_exchange, p_val, 'i64');
return 2; // INT
}
GodotRuntime.setHeapValue(p_exchange, p_val, 'double');
return 3; // FLOAT
} else if (type === 'bigint') {
GodotRuntime.setHeapValue(p_exchange, p_val, 'i64');
return 2; // INT
} else if (type === 'string') {
const c_str = GodotRuntime.allocString(p_val);
GodotRuntime.setHeapValue(p_exchange, c_str, '*');
return 4; // STRING
}
const id = GodotJSWrapper.get_proxied(p_val);
GodotRuntime.setHeapValue(p_exchange, id, 'i64');
return 24; // OBJECT
},
},
godot_js_wrapper_interface_get__proxy: 'sync',
godot_js_wrapper_interface_get__sig: 'ii',
godot_js_wrapper_interface_get: function (p_name) {
const name = GodotRuntime.parseString(p_name);
if (typeof (window[name]) !== 'undefined') {
return GodotJSWrapper.get_proxied(window[name]);
}
return 0;
},
godot_js_wrapper_object_get__proxy: 'sync',
godot_js_wrapper_object_get__sig: 'iiii',
godot_js_wrapper_object_get: function (p_id, p_exchange, p_prop) {
const obj = GodotJSWrapper.get_proxied_value(p_id);
if (obj === undefined) {
return 0;
}
if (p_prop) {
const prop = GodotRuntime.parseString(p_prop);
try {
return GodotJSWrapper.js2variant(obj[prop], p_exchange);
} catch (e) {
GodotRuntime.error(`Error getting variable ${prop} on object`, obj);
return 0; // NIL
}
}
return GodotJSWrapper.js2variant(obj, p_exchange);
},
godot_js_wrapper_object_set__proxy: 'sync',
godot_js_wrapper_object_set__sig: 'viiii',
godot_js_wrapper_object_set: function (p_id, p_name, p_type, p_exchange) {
const obj = GodotJSWrapper.get_proxied_value(p_id);
if (obj === undefined) {
return;
}
const name = GodotRuntime.parseString(p_name);
try {
obj[name] = GodotJSWrapper.variant2js(p_type, p_exchange);
} catch (e) {
GodotRuntime.error(`Error setting variable ${name} on object`, obj);
}
},
godot_js_wrapper_object_call__proxy: 'sync',
godot_js_wrapper_object_call__sig: 'iiiiiiiii',
godot_js_wrapper_object_call: function (p_id, p_method, p_args, p_argc, p_convert_callback, p_exchange, p_lock, p_free_lock_callback) {
const obj = GodotJSWrapper.get_proxied_value(p_id);
if (obj === undefined) {
return -1;
}
const method = GodotRuntime.parseString(p_method);
const convert = GodotRuntime.get_func(p_convert_callback);
const freeLock = GodotRuntime.get_func(p_free_lock_callback);
const args = new Array(p_argc);
for (let i = 0; i < p_argc; i++) {
const type = convert(p_args, i, p_exchange, p_lock);
const lock = GodotRuntime.getHeapValue(p_lock, '*');
args[i] = GodotJSWrapper.variant2js(type, p_exchange);
if (lock) {
freeLock(p_lock, type);
}
}
try {
const res = obj[method](...args);
return GodotJSWrapper.js2variant(res, p_exchange);
} catch (e) {
GodotRuntime.error(`Error calling method ${method} on:`, obj, 'error:', e);
return -1;
}
},
godot_js_wrapper_object_unref__proxy: 'sync',
godot_js_wrapper_object_unref__sig: 'vi',
godot_js_wrapper_object_unref: function (p_id) {
const proxy = IDHandler.get(p_id);
if (proxy !== undefined) {
proxy.unref();
}
},
godot_js_wrapper_create_cb__proxy: 'sync',
godot_js_wrapper_create_cb__sig: 'iii',
godot_js_wrapper_create_cb: function (p_ref, p_func) {
const func = GodotRuntime.get_func(p_func);
let id = 0;
const cb = function () {
if (!GodotJSWrapper.get_proxied_value(id)) {
return undefined;
}
// The callback will store the returned value in this variable via
// "godot_js_wrapper_object_set_cb_ret" upon calling the user function.
// This is safe! JavaScript is single threaded (and using it in threads is not a good idea anyway).
GodotJSWrapper.cb_ret = null;
const args = Array.from(arguments);
const argsProxy = new GodotJSWrapper.MyProxy(args);
func(p_ref, argsProxy.get_id(), args.length);
argsProxy.unref();
const ret = GodotJSWrapper.cb_ret;
GodotJSWrapper.cb_ret = null;
return ret;
};
id = GodotJSWrapper.get_proxied(cb);
return id;
},
godot_js_wrapper_object_set_cb_ret__proxy: 'sync',
godot_js_wrapper_object_set_cb_ret__sig: 'vii',
godot_js_wrapper_object_set_cb_ret: function (p_val_type, p_val_ex) {
GodotJSWrapper.cb_ret = GodotJSWrapper.variant2js(p_val_type, p_val_ex);
},
godot_js_wrapper_object_getvar__proxy: 'sync',
godot_js_wrapper_object_getvar__sig: 'iiii',
godot_js_wrapper_object_getvar: function (p_id, p_type, p_exchange) {
const obj = GodotJSWrapper.get_proxied_value(p_id);
if (obj === undefined) {
return -1;
}
const prop = GodotJSWrapper.variant2js(p_type, p_exchange);
if (prop === undefined || prop === null) {
return -1;
}
try {
return GodotJSWrapper.js2variant(obj[prop], p_exchange);
} catch (e) {
GodotRuntime.error(`Error getting variable ${prop} on object`, obj, e);
return -1;
}
},
godot_js_wrapper_object_setvar__proxy: 'sync',
godot_js_wrapper_object_setvar__sig: 'iiiiii',
godot_js_wrapper_object_setvar: function (p_id, p_key_type, p_key_ex, p_val_type, p_val_ex) {
const obj = GodotJSWrapper.get_proxied_value(p_id);
if (obj === undefined) {
return -1;
}
const key = GodotJSWrapper.variant2js(p_key_type, p_key_ex);
try {
obj[key] = GodotJSWrapper.variant2js(p_val_type, p_val_ex);
return 0;
} catch (e) {
GodotRuntime.error(`Error setting variable ${key} on object`, obj);
return -1;
}
},
godot_js_wrapper_create_object__proxy: 'sync',
godot_js_wrapper_create_object__sig: 'iiiiiiii',
godot_js_wrapper_create_object: function (p_object, p_args, p_argc, p_convert_callback, p_exchange, p_lock, p_free_lock_callback) {
const name = GodotRuntime.parseString(p_object);
if (typeof (window[name]) === 'undefined') {
return -1;
}
const convert = GodotRuntime.get_func(p_convert_callback);
const freeLock = GodotRuntime.get_func(p_free_lock_callback);
const args = new Array(p_argc);
for (let i = 0; i < p_argc; i++) {
const type = convert(p_args, i, p_exchange, p_lock);
const lock = GodotRuntime.getHeapValue(p_lock, '*');
args[i] = GodotJSWrapper.variant2js(type, p_exchange);
if (lock) {
freeLock(p_lock, type);
}
}
try {
const res = new window[name](...args);
return GodotJSWrapper.js2variant(res, p_exchange);
} catch (e) {
GodotRuntime.error(`Error calling constructor ${name} with args:`, args, 'error:', e);
return -1;
}
},
};
autoAddDeps(GodotJSWrapper, '$GodotJSWrapper');
mergeInto(LibraryManager.library, GodotJSWrapper);
const GodotEval = {
godot_js_eval__deps: ['$GodotRuntime'],
godot_js_eval__sig: 'iiiiiii',
godot_js_eval: function (p_js, p_use_global_ctx, p_union_ptr, p_byte_arr, p_byte_arr_write, p_callback) {
const js_code = GodotRuntime.parseString(p_js);
let eval_ret = null;
try {
if (p_use_global_ctx) {
// indirect eval call grants global execution context
const global_eval = eval; // eslint-disable-line no-eval
eval_ret = global_eval(js_code);
} else {
eval_ret = eval(js_code); // eslint-disable-line no-eval
}
} catch (e) {
GodotRuntime.error(e);
}
switch (typeof eval_ret) {
case 'boolean':
GodotRuntime.setHeapValue(p_union_ptr, eval_ret, 'i32');
return 1; // BOOL
case 'number':
GodotRuntime.setHeapValue(p_union_ptr, eval_ret, 'double');
return 3; // FLOAT
case 'string':
GodotRuntime.setHeapValue(p_union_ptr, GodotRuntime.allocString(eval_ret), '*');
return 4; // STRING
case 'object':
if (eval_ret === null) {
break;
}
if (ArrayBuffer.isView(eval_ret) && !(eval_ret instanceof Uint8Array)) {
eval_ret = new Uint8Array(eval_ret.buffer);
} else if (eval_ret instanceof ArrayBuffer) {
eval_ret = new Uint8Array(eval_ret);
}
if (eval_ret instanceof Uint8Array) {
const func = GodotRuntime.get_func(p_callback);
const bytes_ptr = func(p_byte_arr, p_byte_arr_write, eval_ret.length);
HEAPU8.set(eval_ret, bytes_ptr);
return 29; // PACKED_BYTE_ARRAY
}
break;
// no default
}
return 0; // NIL
},
};
mergeInto(LibraryManager.library, GodotEval);

View file

@ -0,0 +1,466 @@
/**************************************************************************/
/* library_godot_os.js */
/**************************************************************************/
/* 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. */
/**************************************************************************/
const IDHandler = {
$IDHandler: {
_last_id: 0,
_references: {},
get: function (p_id) {
return IDHandler._references[p_id];
},
add: function (p_data) {
const id = ++IDHandler._last_id;
IDHandler._references[id] = p_data;
return id;
},
remove: function (p_id) {
delete IDHandler._references[p_id];
},
},
};
autoAddDeps(IDHandler, '$IDHandler');
mergeInto(LibraryManager.library, IDHandler);
const GodotConfig = {
$GodotConfig__postset: 'Module["initConfig"] = GodotConfig.init_config;',
$GodotConfig__deps: ['$GodotRuntime'],
$GodotConfig: {
canvas: null,
locale: 'en',
canvas_resize_policy: 2, // Adaptive
virtual_keyboard: false,
persistent_drops: false,
on_execute: null,
on_exit: null,
init_config: function (p_opts) {
GodotConfig.canvas_resize_policy = p_opts['canvasResizePolicy'];
GodotConfig.canvas = p_opts['canvas'];
GodotConfig.locale = p_opts['locale'] || GodotConfig.locale;
GodotConfig.virtual_keyboard = p_opts['virtualKeyboard'];
GodotConfig.persistent_drops = !!p_opts['persistentDrops'];
GodotConfig.on_execute = p_opts['onExecute'];
GodotConfig.on_exit = p_opts['onExit'];
if (p_opts['focusCanvas']) {
GodotConfig.canvas.focus();
}
},
locate_file: function (file) {
return Module['locateFile'](file);
},
clear: function () {
GodotConfig.canvas = null;
GodotConfig.locale = 'en';
GodotConfig.canvas_resize_policy = 2;
GodotConfig.virtual_keyboard = false;
GodotConfig.persistent_drops = false;
GodotConfig.on_execute = null;
GodotConfig.on_exit = null;
},
},
godot_js_config_canvas_id_get__proxy: 'sync',
godot_js_config_canvas_id_get__sig: 'vii',
godot_js_config_canvas_id_get: function (p_ptr, p_ptr_max) {
GodotRuntime.stringToHeap(`#${GodotConfig.canvas.id}`, p_ptr, p_ptr_max);
},
godot_js_config_locale_get__proxy: 'sync',
godot_js_config_locale_get__sig: 'vii',
godot_js_config_locale_get: function (p_ptr, p_ptr_max) {
GodotRuntime.stringToHeap(GodotConfig.locale, p_ptr, p_ptr_max);
},
};
autoAddDeps(GodotConfig, '$GodotConfig');
mergeInto(LibraryManager.library, GodotConfig);
const GodotFS = {
$GodotFS__deps: ['$FS', '$IDBFS', '$GodotRuntime'],
$GodotFS__postset: [
'Module["initFS"] = GodotFS.init;',
'Module["copyToFS"] = GodotFS.copy_to_fs;',
].join(''),
$GodotFS: {
// ERRNO_CODES works every odd version of emscripten, but this will break too eventually.
ENOENT: 44,
_idbfs: false,
_syncing: false,
_mount_points: [],
is_persistent: function () {
return GodotFS._idbfs ? 1 : 0;
},
// Initialize godot file system, setting up persistent paths.
// Returns a promise that resolves when the FS is ready.
// We keep track of mount_points, so that we can properly close the IDBFS
// since emscripten is not doing it by itself. (emscripten GH#12516).
init: function (persistentPaths) {
GodotFS._idbfs = false;
if (!Array.isArray(persistentPaths)) {
return Promise.reject(new Error('Persistent paths must be an array'));
}
if (!persistentPaths.length) {
return Promise.resolve();
}
GodotFS._mount_points = persistentPaths.slice();
function createRecursive(dir) {
try {
FS.stat(dir);
} catch (e) {
if (e.errno !== GodotFS.ENOENT) {
// Let mkdirTree throw in case, we cannot trust the above check.
GodotRuntime.error(e);
}
FS.mkdirTree(dir);
}
}
GodotFS._mount_points.forEach(function (path) {
createRecursive(path);
FS.mount(IDBFS, {}, path);
});
return new Promise(function (resolve, reject) {
FS.syncfs(true, function (err) {
if (err) {
GodotFS._mount_points = [];
GodotFS._idbfs = false;
GodotRuntime.print(`IndexedDB not available: ${err.message}`);
} else {
GodotFS._idbfs = true;
}
resolve(err);
});
});
},
// Deinit godot file system, making sure to unmount file systems, and close IDBFS(s).
deinit: function () {
GodotFS._mount_points.forEach(function (path) {
try {
FS.unmount(path);
} catch (e) {
GodotRuntime.print('Already unmounted', e);
}
if (GodotFS._idbfs && IDBFS.dbs[path]) {
IDBFS.dbs[path].close();
delete IDBFS.dbs[path];
}
});
GodotFS._mount_points = [];
GodotFS._idbfs = false;
GodotFS._syncing = false;
},
sync: function () {
if (GodotFS._syncing) {
GodotRuntime.error('Already syncing!');
return Promise.resolve();
}
GodotFS._syncing = true;
return new Promise(function (resolve, reject) {
FS.syncfs(false, function (error) {
if (error) {
GodotRuntime.error(`Failed to save IDB file system: ${error.message}`);
}
GodotFS._syncing = false;
resolve(error);
});
});
},
// Copies a buffer to the internal file system. Creating directories recursively.
copy_to_fs: function (path, buffer) {
const idx = path.lastIndexOf('/');
let dir = '/';
if (idx > 0) {
dir = path.slice(0, idx);
}
try {
FS.stat(dir);
} catch (e) {
if (e.errno !== GodotFS.ENOENT) {
// Let mkdirTree throw in case, we cannot trust the above check.
GodotRuntime.error(e);
}
FS.mkdirTree(dir);
}
FS.writeFile(path, new Uint8Array(buffer));
},
},
};
mergeInto(LibraryManager.library, GodotFS);
const GodotOS = {
$GodotOS__deps: ['$GodotRuntime', '$GodotConfig', '$GodotFS'],
$GodotOS__postset: [
'Module["request_quit"] = function() { GodotOS.request_quit() };',
'Module["onExit"] = GodotOS.cleanup;',
'GodotOS._fs_sync_promise = Promise.resolve();',
].join(''),
$GodotOS: {
request_quit: function () {},
_async_cbs: [],
_fs_sync_promise: null,
atexit: function (p_promise_cb) {
GodotOS._async_cbs.push(p_promise_cb);
},
cleanup: function (exit_code) {
const cb = GodotConfig.on_exit;
GodotFS.deinit();
GodotConfig.clear();
if (cb) {
cb(exit_code);
}
},
finish_async: function (callback) {
GodotOS._fs_sync_promise.then(function (err) {
const promises = [];
GodotOS._async_cbs.forEach(function (cb) {
promises.push(new Promise(cb));
});
return Promise.all(promises);
}).then(function () {
return GodotFS.sync(); // Final FS sync.
}).then(function (err) {
// Always deferred.
setTimeout(function () {
callback();
}, 0);
});
},
},
godot_js_os_finish_async__proxy: 'sync',
godot_js_os_finish_async__sig: 'vi',
godot_js_os_finish_async: function (p_callback) {
const func = GodotRuntime.get_func(p_callback);
GodotOS.finish_async(func);
},
godot_js_os_request_quit_cb__proxy: 'sync',
godot_js_os_request_quit_cb__sig: 'vi',
godot_js_os_request_quit_cb: function (p_callback) {
GodotOS.request_quit = GodotRuntime.get_func(p_callback);
},
godot_js_os_fs_is_persistent__proxy: 'sync',
godot_js_os_fs_is_persistent__sig: 'i',
godot_js_os_fs_is_persistent: function () {
return GodotFS.is_persistent();
},
godot_js_os_fs_sync__proxy: 'sync',
godot_js_os_fs_sync__sig: 'vi',
godot_js_os_fs_sync: function (callback) {
const func = GodotRuntime.get_func(callback);
GodotOS._fs_sync_promise = GodotFS.sync();
GodotOS._fs_sync_promise.then(function (err) {
func();
});
},
godot_js_os_has_feature__proxy: 'sync',
godot_js_os_has_feature__sig: 'ii',
godot_js_os_has_feature: function (p_ftr) {
const ftr = GodotRuntime.parseString(p_ftr);
const ua = navigator.userAgent;
if (ftr === 'web_macos') {
return (ua.indexOf('Mac') !== -1) ? 1 : 0;
}
if (ftr === 'web_windows') {
return (ua.indexOf('Windows') !== -1) ? 1 : 0;
}
if (ftr === 'web_android') {
return (ua.indexOf('Android') !== -1) ? 1 : 0;
}
if (ftr === 'web_ios') {
return ((ua.indexOf('iPhone') !== -1) || (ua.indexOf('iPad') !== -1) || (ua.indexOf('iPod') !== -1)) ? 1 : 0;
}
if (ftr === 'web_linuxbsd') {
return ((ua.indexOf('CrOS') !== -1) || (ua.indexOf('BSD') !== -1) || (ua.indexOf('Linux') !== -1) || (ua.indexOf('X11') !== -1)) ? 1 : 0;
}
return 0;
},
godot_js_os_execute__proxy: 'sync',
godot_js_os_execute__sig: 'ii',
godot_js_os_execute: function (p_json) {
const json_args = GodotRuntime.parseString(p_json);
const args = JSON.parse(json_args);
if (GodotConfig.on_execute) {
GodotConfig.on_execute(args);
return 0;
}
return 1;
},
godot_js_os_shell_open__proxy: 'sync',
godot_js_os_shell_open__sig: 'vi',
godot_js_os_shell_open: function (p_uri) {
window.open(GodotRuntime.parseString(p_uri), '_blank');
},
godot_js_os_hw_concurrency_get__proxy: 'sync',
godot_js_os_hw_concurrency_get__sig: 'i',
godot_js_os_hw_concurrency_get: function () {
// TODO Godot core needs fixing to avoid spawning too many threads (> 24).
const concurrency = navigator.hardwareConcurrency || 1;
return concurrency < 2 ? concurrency : 2;
},
godot_js_os_download_buffer__proxy: 'sync',
godot_js_os_download_buffer__sig: 'viiii',
godot_js_os_download_buffer: function (p_ptr, p_size, p_name, p_mime) {
const buf = GodotRuntime.heapSlice(HEAP8, p_ptr, p_size);
const name = GodotRuntime.parseString(p_name);
const mime = GodotRuntime.parseString(p_mime);
const blob = new Blob([buf], { type: mime });
const url = window.URL.createObjectURL(blob);
const a = document.createElement('a');
a.href = url;
a.download = name;
a.style.display = 'none';
document.body.appendChild(a);
a.click();
a.remove();
window.URL.revokeObjectURL(url);
},
};
autoAddDeps(GodotOS, '$GodotOS');
mergeInto(LibraryManager.library, GodotOS);
/*
* Godot event listeners.
* Keeps track of registered event listeners so it can remove them on shutdown.
*/
const GodotEventListeners = {
$GodotEventListeners__deps: ['$GodotOS'],
$GodotEventListeners__postset: 'GodotOS.atexit(function(resolve, reject) { GodotEventListeners.clear(); resolve(); });',
$GodotEventListeners: {
handlers: [],
has: function (target, event, method, capture) {
return GodotEventListeners.handlers.findIndex(function (e) {
return e.target === target && e.event === event && e.method === method && e.capture === capture;
}) !== -1;
},
add: function (target, event, method, capture) {
if (GodotEventListeners.has(target, event, method, capture)) {
return;
}
function Handler(p_target, p_event, p_method, p_capture) {
this.target = p_target;
this.event = p_event;
this.method = p_method;
this.capture = p_capture;
}
GodotEventListeners.handlers.push(new Handler(target, event, method, capture));
target.addEventListener(event, method, capture);
},
clear: function () {
GodotEventListeners.handlers.forEach(function (h) {
h.target.removeEventListener(h.event, h.method, h.capture);
});
GodotEventListeners.handlers.length = 0;
},
},
};
mergeInto(LibraryManager.library, GodotEventListeners);
const GodotPWA = {
$GodotPWA__deps: ['$GodotRuntime', '$GodotEventListeners'],
$GodotPWA: {
hasUpdate: false,
updateState: function (cb, reg) {
if (!reg) {
return;
}
if (!reg.active) {
return;
}
if (reg.waiting) {
GodotPWA.hasUpdate = true;
cb();
}
GodotEventListeners.add(reg, 'updatefound', function () {
const installing = reg.installing;
GodotEventListeners.add(installing, 'statechange', function () {
if (installing.state === 'installed') {
GodotPWA.hasUpdate = true;
cb();
}
});
});
},
},
godot_js_pwa_cb__proxy: 'sync',
godot_js_pwa_cb__sig: 'vi',
godot_js_pwa_cb: function (p_update_cb) {
if ('serviceWorker' in navigator) {
const cb = GodotRuntime.get_func(p_update_cb);
navigator.serviceWorker.getRegistration().then(GodotPWA.updateState.bind(null, cb));
}
},
godot_js_pwa_update__proxy: 'sync',
godot_js_pwa_update__sig: 'i',
godot_js_pwa_update: function () {
if ('serviceWorker' in navigator && GodotPWA.hasUpdate) {
navigator.serviceWorker.getRegistration().then(function (reg) {
if (!reg || !reg.waiting) {
return;
}
reg.waiting.postMessage('update');
});
return 0;
}
return 1;
},
};
autoAddDeps(GodotPWA, '$GodotPWA');
mergeInto(LibraryManager.library, GodotPWA);

View file

@ -0,0 +1,134 @@
/**************************************************************************/
/* library_godot_runtime.js */
/**************************************************************************/
/* 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. */
/**************************************************************************/
const GodotRuntime = {
$GodotRuntime: {
/*
* Functions
*/
get_func: function (ptr) {
return wasmTable.get(ptr);
},
/*
* Prints
*/
error: function () {
err.apply(null, Array.from(arguments)); // eslint-disable-line no-undef
},
print: function () {
out.apply(null, Array.from(arguments)); // eslint-disable-line no-undef
},
/*
* Memory
*/
malloc: function (p_size) {
return _malloc(p_size);
},
free: function (p_ptr) {
_free(p_ptr);
},
getHeapValue: function (p_ptr, p_type) {
return getValue(p_ptr, p_type);
},
setHeapValue: function (p_ptr, p_value, p_type) {
setValue(p_ptr, p_value, p_type);
},
heapSub: function (p_heap, p_ptr, p_len) {
const bytes = p_heap.BYTES_PER_ELEMENT;
return p_heap.subarray(p_ptr / bytes, p_ptr / bytes + p_len);
},
heapSlice: function (p_heap, p_ptr, p_len) {
const bytes = p_heap.BYTES_PER_ELEMENT;
return p_heap.slice(p_ptr / bytes, p_ptr / bytes + p_len);
},
heapCopy: function (p_dst, p_src, p_ptr) {
const bytes = p_src.BYTES_PER_ELEMENT;
return p_dst.set(p_src, p_ptr / bytes);
},
/*
* Strings
*/
parseString: function (p_ptr) {
return UTF8ToString(p_ptr);
},
parseStringArray: function (p_ptr, p_size) {
const strings = [];
const ptrs = GodotRuntime.heapSub(HEAP32, p_ptr, p_size); // TODO wasm64
ptrs.forEach(function (ptr) {
strings.push(GodotRuntime.parseString(ptr));
});
return strings;
},
strlen: function (p_str) {
return lengthBytesUTF8(p_str);
},
allocString: function (p_str) {
const length = GodotRuntime.strlen(p_str) + 1;
const c_str = GodotRuntime.malloc(length);
stringToUTF8(p_str, c_str, length);
return c_str;
},
allocStringArray: function (p_strings) {
const size = p_strings.length;
const c_ptr = GodotRuntime.malloc(size * 4);
for (let i = 0; i < size; i++) {
HEAP32[(c_ptr >> 2) + i] = GodotRuntime.allocString(p_strings[i]);
}
return c_ptr;
},
freeStringArray: function (p_ptr, p_len) {
for (let i = 0; i < p_len; i++) {
GodotRuntime.free(HEAP32[(p_ptr >> 2) + i]);
}
GodotRuntime.free(p_ptr);
},
stringToHeap: function (p_str, p_ptr, p_len) {
return stringToUTF8Array(p_str, HEAP8, p_ptr, p_len);
},
},
};
autoAddDeps(GodotRuntime, '$GodotRuntime');
mergeInto(LibraryManager.library, GodotRuntime);

View file

@ -0,0 +1,52 @@
/**
* @constructor OVR_multiview2
*/
function OVR_multiview2() {}
/**
* @type {number}
*/
OVR_multiview2.prototype.FRAMEBUFFER_ATTACHMENT_TEXTURE_NUM_VIEWS_OVR;
/**
* @type {number}
*/
OVR_multiview2.prototype.FRAMEBUFFER_ATTACHMENT_TEXTURE_BASE_VIEW_INDEX_OVR;
/**
* @type {number}
*/
OVR_multiview2.prototype.MAX_VIEWS_OVR;
/**
* @type {number}
*/
OVR_multiview2.prototype.FRAMEBUFFER_INCOMPLETE_VIEW_TARGETS_OVR;
/**
* @param {number} target
* @param {number} attachment
* @param {WebGLTexture} texture
* @param {number} level
* @param {number} baseViewIndex
* @param {number} numViews
* @return {void}
*/
OVR_multiview2.prototype.framebufferTextureMultiviewOVR = function(target, attachment, texture, level, baseViewIndex, numViews) {};
/**
* @constructor OCULUS_multiview
*/
function OCULUS_multiview() {}
/**
* @param {number} target
* @param {number} attachment
* @param {WebGLTexture} texture
* @param {number} level
* @param {number} baseViewIndex
* @param {number} numViews
* @return {void}
*/
OCULUS_multiview.prototype.framebufferTextureMultisampleMultiviewOVR = function(target, attachment, texture, level, samples, baseViewIndex, numViews) {};

View file

@ -0,0 +1,84 @@
/**************************************************************************/
/* library_godot_webgl2.js */
/**************************************************************************/
/* 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. */
/**************************************************************************/
const GodotWebGL2 = {
$GodotWebGL2__deps: ['$GL', '$GodotRuntime'],
$GodotWebGL2: {},
// This is implemented as "glGetBufferSubData" in new emscripten versions.
// Since we have to support older (pre 2.0.17) emscripten versions, we add this wrapper function instead.
godot_webgl2_glGetBufferSubData__proxy: 'sync',
godot_webgl2_glGetBufferSubData__sig: 'vippp',
godot_webgl2_glGetBufferSubData__deps: ['$GL', 'emscripten_webgl_get_current_context'],
godot_webgl2_glGetBufferSubData: function (target, offset, size, data) {
const gl_context_handle = _emscripten_webgl_get_current_context();
const gl = GL.getContext(gl_context_handle);
if (gl) {
gl.GLctx['getBufferSubData'](target, offset, HEAPU8, data, size);
}
},
godot_webgl2_glFramebufferTextureMultiviewOVR__deps: ['emscripten_webgl_get_current_context'],
godot_webgl2_glFramebufferTextureMultiviewOVR__proxy: 'sync',
godot_webgl2_glFramebufferTextureMultiviewOVR__sig: 'viiiiii',
godot_webgl2_glFramebufferTextureMultiviewOVR: function (target, attachment, texture, level, base_view_index, num_views) {
const context = GL.currentContext;
if (typeof context.multiviewExt === 'undefined') {
const /** OVR_multiview2 */ ext = context.GLctx.getExtension('OVR_multiview2');
if (!ext) {
GodotRuntime.error('Trying to call glFramebufferTextureMultiviewOVR() without the OVR_multiview2 extension');
return;
}
context.multiviewExt = ext;
}
const /** OVR_multiview2 */ ext = context.multiviewExt;
ext.framebufferTextureMultiviewOVR(target, attachment, GL.textures[texture], level, base_view_index, num_views);
},
godot_webgl2_glFramebufferTextureMultisampleMultiviewOVR__deps: ['emscripten_webgl_get_current_context'],
godot_webgl2_glFramebufferTextureMultisampleMultiviewOVR__proxy: 'sync',
godot_webgl2_glFramebufferTextureMultisampleMultiviewOVR__sig: 'viiiiiii',
godot_webgl2_glFramebufferTextureMultisampleMultiviewOVR: function (target, attachment, texture, level, samples, base_view_index, num_views) {
const context = GL.currentContext;
if (typeof context.oculusMultiviewExt === 'undefined') {
const /** OCULUS_multiview */ ext = context.GLctx.getExtension('OCULUS_multiview');
if (!ext) {
GodotRuntime.error('Trying to call glFramebufferTextureMultisampleMultiviewOVR() without the OCULUS_multiview extension');
return;
}
context.oculusMultiviewExt = ext;
}
const /** OCULUS_multiview */ ext = context.oculusMultiviewExt;
ext.framebufferTextureMultisampleMultiviewOVR(target, attachment, GL.textures[texture], level, samples, base_view_index, num_views);
},
};
autoAddDeps(GodotWebGL2, '$GodotWebGL2');
mergeInto(LibraryManager.library, GodotWebGL2);

View file

@ -0,0 +1,290 @@
/**************************************************************************/
/* os_web.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 "os_web.h"
#include "api/javascript_bridge_singleton.h"
#include "display_server_web.h"
#include "godot_js.h"
#include "core/config/project_settings.h"
#include "core/debugger/engine_debugger.h"
#include "drivers/unix/dir_access_unix.h"
#include "drivers/unix/file_access_unix.h"
#include "main/main.h"
#include "modules/modules_enabled.gen.h" // For websocket.
#include <dlfcn.h>
#include <emscripten.h>
#include <stdlib.h>
void OS_Web::alert(const String &p_alert, const String &p_title) {
godot_js_display_alert(p_alert.utf8().get_data());
}
// Lifecycle
void OS_Web::initialize() {
OS_Unix::initialize_core();
DisplayServerWeb::register_web_driver();
}
void OS_Web::resume_audio() {
AudioDriverWeb::resume();
}
void OS_Web::set_main_loop(MainLoop *p_main_loop) {
main_loop = p_main_loop;
}
MainLoop *OS_Web::get_main_loop() const {
return main_loop;
}
void OS_Web::fs_sync_callback() {
get_singleton()->idb_is_syncing = false;
}
bool OS_Web::main_loop_iterate() {
if (is_userfs_persistent() && idb_needs_sync && !idb_is_syncing) {
idb_is_syncing = true;
idb_needs_sync = false;
godot_js_os_fs_sync(&fs_sync_callback);
}
DisplayServer::get_singleton()->process_events();
return Main::iteration();
}
void OS_Web::delete_main_loop() {
if (main_loop) {
memdelete(main_loop);
}
main_loop = nullptr;
}
void OS_Web::finalize() {
delete_main_loop();
for (AudioDriverWeb *driver : audio_drivers) {
memdelete(driver);
}
audio_drivers.clear();
}
// Miscellaneous
Error OS_Web::execute(const String &p_path, const List<String> &p_arguments, String *r_pipe, int *r_exitcode, bool read_stderr, Mutex *p_pipe_mutex, bool p_open_console) {
return create_process(p_path, p_arguments);
}
Dictionary OS_Web::execute_with_pipe(const String &p_path, const List<String> &p_arguments) {
ERR_FAIL_V_MSG(Dictionary(), "OS::execute_with_pipe is not available on the Web platform.");
}
Error OS_Web::create_process(const String &p_path, const List<String> &p_arguments, ProcessID *r_child_id, bool p_open_console) {
Array args;
for (const String &E : p_arguments) {
args.push_back(E);
}
String json_args = Variant(args).to_json_string();
int failed = godot_js_os_execute(json_args.utf8().get_data());
ERR_FAIL_COND_V_MSG(failed, ERR_UNAVAILABLE, "OS::execute() or create_process() must be implemented in Web via 'engine.setOnExecute' if required.");
return OK;
}
Error OS_Web::kill(const ProcessID &p_pid) {
ERR_FAIL_V_MSG(ERR_UNAVAILABLE, "OS::kill() is not available on the Web platform.");
}
int OS_Web::get_process_id() const {
ERR_FAIL_V_MSG(0, "OS::get_process_id() is not available on the Web platform.");
}
bool OS_Web::is_process_running(const ProcessID &p_pid) const {
return false;
}
int OS_Web::get_process_exit_code(const ProcessID &p_pid) const {
return -1;
}
int OS_Web::get_processor_count() const {
return godot_js_os_hw_concurrency_get();
}
String OS_Web::get_unique_id() const {
ERR_FAIL_V_MSG("", "OS::get_unique_id() is not available on the Web platform.");
}
bool OS_Web::_check_internal_feature_support(const String &p_feature) {
if (p_feature == "web") {
return true;
}
if (godot_js_os_has_feature(p_feature.utf8().get_data())) {
return true;
}
return false;
}
String OS_Web::get_executable_path() const {
return OS::get_executable_path();
}
Error OS_Web::shell_open(const String &p_uri) {
// Open URI in a new tab, browser will deal with it by protocol.
godot_js_os_shell_open(p_uri.utf8().get_data());
return OK;
}
String OS_Web::get_name() const {
return "Web";
}
void OS_Web::add_frame_delay(bool p_can_draw) {
#ifndef PROXY_TO_PTHREAD_ENABLED
OS::add_frame_delay(p_can_draw);
#endif
}
void OS_Web::vibrate_handheld(int p_duration_ms, float p_amplitude) {
godot_js_input_vibrate_handheld(p_duration_ms);
}
String OS_Web::get_user_data_dir() const {
String userfs = "/userfs";
String appname = get_safe_dir_name(GLOBAL_GET("application/config/name"));
if (!appname.is_empty()) {
bool use_custom_dir = GLOBAL_GET("application/config/use_custom_user_dir");
if (use_custom_dir) {
String custom_dir = get_safe_dir_name(GLOBAL_GET("application/config/custom_user_dir_name"), true);
if (custom_dir.is_empty()) {
custom_dir = appname;
}
return userfs.path_join(custom_dir).replace("\\", "/");
} else {
return userfs.path_join(get_godot_dir_name()).path_join("app_userdata").path_join(appname).replace("\\", "/");
}
}
return userfs.path_join(get_godot_dir_name()).path_join("app_userdata").path_join("[unnamed project]");
}
String OS_Web::get_cache_path() const {
return "/home/web_user/.cache";
}
String OS_Web::get_config_path() const {
return "/home/web_user/.config";
}
String OS_Web::get_data_path() const {
return "/home/web_user/.local/share";
}
void OS_Web::file_access_close_callback(const String &p_file, int p_flags) {
OS_Web *os = OS_Web::get_singleton();
if (!(os->is_userfs_persistent() && (p_flags & FileAccess::WRITE))) {
return; // FS persistence is not working or we are not writing.
}
bool is_file_persistent = p_file.begins_with("/userfs");
#ifdef TOOLS_ENABLED
// Hack for editor persistence (can we track).
is_file_persistent = is_file_persistent || p_file.begins_with("/home/web_user/");
#endif
if (is_file_persistent) {
os->idb_needs_sync = true;
}
}
void OS_Web::update_pwa_state_callback() {
if (OS_Web::get_singleton()) {
OS_Web::get_singleton()->pwa_is_waiting = true;
}
if (JavaScriptBridge::get_singleton()) {
JavaScriptBridge::get_singleton()->emit_signal("pwa_update_available");
}
}
void OS_Web::force_fs_sync() {
if (is_userfs_persistent()) {
idb_needs_sync = true;
}
}
Error OS_Web::pwa_update() {
return godot_js_pwa_update() ? FAILED : OK;
}
bool OS_Web::is_userfs_persistent() const {
return idb_available;
}
Error OS_Web::open_dynamic_library(const String &p_path, void *&p_library_handle, GDExtensionData *p_data) {
String path = p_path.get_file();
p_library_handle = dlopen(path.utf8().get_data(), RTLD_NOW);
ERR_FAIL_NULL_V_MSG(p_library_handle, ERR_CANT_OPEN, vformat("Can't open dynamic library: %s. Error: %s.", p_path, dlerror()));
if (p_data != nullptr && p_data->r_resolved_path != nullptr) {
*p_data->r_resolved_path = path;
}
return OK;
}
OS_Web *OS_Web::get_singleton() {
return static_cast<OS_Web *>(OS::get_singleton());
}
void OS_Web::initialize_joypads() {
}
OS_Web::OS_Web() {
char locale_ptr[16];
godot_js_config_locale_get(locale_ptr, 16);
setenv("LANG", locale_ptr, true);
godot_js_pwa_cb(&OS_Web::update_pwa_state_callback);
if (AudioDriverWeb::is_available()) {
audio_drivers.push_back(memnew(AudioDriverWorklet));
}
for (AudioDriverWeb *audio_driver : audio_drivers) {
AudioDriverManager::add_driver(audio_driver);
}
idb_available = godot_js_os_fs_is_persistent();
Vector<Logger *> loggers;
loggers.push_back(memnew(StdLogger));
_set_logger(memnew(CompositeLogger(loggers)));
FileAccessUnix::close_notification_func = file_access_close_callback;
}

View file

@ -0,0 +1,119 @@
/**************************************************************************/
/* os_web.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. */
/**************************************************************************/
#ifndef OS_WEB_H
#define OS_WEB_H
#include "audio_driver_web.h"
#include "godot_js.h"
#include "core/input/input.h"
#include "drivers/unix/os_unix.h"
#include "servers/audio_server.h"
#include <emscripten/html5.h>
class OS_Web : public OS_Unix {
MainLoop *main_loop = nullptr;
List<AudioDriverWeb *> audio_drivers;
bool idb_is_syncing = false;
bool idb_available = false;
bool idb_needs_sync = false;
bool pwa_is_waiting = false;
WASM_EXPORT static void main_loop_callback();
WASM_EXPORT static void file_access_close_callback(const String &p_file, int p_flags);
WASM_EXPORT static void fs_sync_callback();
WASM_EXPORT static void update_pwa_state_callback();
protected:
void initialize() override;
void set_main_loop(MainLoop *p_main_loop) override;
void delete_main_loop() override;
void finalize() override;
bool _check_internal_feature_support(const String &p_feature) override;
public:
// Override return type to make writing static callbacks less tedious.
static OS_Web *get_singleton();
bool pwa_needs_update() const { return pwa_is_waiting; }
Error pwa_update();
void force_fs_sync();
void initialize_joypads() override;
MainLoop *get_main_loop() const override;
bool main_loop_iterate();
Error execute(const String &p_path, const List<String> &p_arguments, String *r_pipe = nullptr, int *r_exitcode = nullptr, bool read_stderr = false, Mutex *p_pipe_mutex = nullptr, bool p_open_console = false) override;
Dictionary execute_with_pipe(const String &p_path, const List<String> &p_arguments) override;
Error create_process(const String &p_path, const List<String> &p_arguments, ProcessID *r_child_id = nullptr, bool p_open_console = false) override;
Error kill(const ProcessID &p_pid) override;
int get_process_id() const override;
bool is_process_running(const ProcessID &p_pid) const override;
int get_process_exit_code(const ProcessID &p_pid) const override;
int get_processor_count() const override;
String get_unique_id() const override;
int get_default_thread_pool_size() const override { return 1; }
String get_executable_path() const override;
Error shell_open(const String &p_uri) override;
String get_name() const override;
// Override default OS implementation which would block the main thread with delay_usec.
// Implemented in web_main.cpp loop callback instead.
void add_frame_delay(bool p_can_draw) override;
void vibrate_handheld(int p_duration_ms, float p_amplitude) override;
String get_cache_path() const override;
String get_config_path() const override;
String get_data_path() const override;
String get_user_data_dir() const override;
bool is_userfs_persistent() const override;
void alert(const String &p_alert, const String &p_title = "ALERT!") override;
Error open_dynamic_library(const String &p_path, void *&p_library_handle, GDExtensionData *p_data = nullptr) override;
void resume_audio();
OS_Web();
};
#endif // OS_WEB_H

1892
engine/platform/web/package-lock.json generated Normal file

File diff suppressed because it is too large Load diff

View file

@ -0,0 +1,24 @@
{
"name": "godot",
"private": true,
"version": "1.0.0",
"description": "Development and linting setup for Godot's Web platform code",
"author": "Godot Engine contributors",
"license": "MIT",
"scripts": {
"docs": "jsdoc --template js/jsdoc2rst/ js/engine/engine.js js/engine/config.js js/engine/features.js --destination ''",
"lint": "cd ../.. && eslint --no-config-lookup --config ./platform/web/eslint.config.cjs ./platform/web/js ./modules ./misc/dist/html",
"format": "npm run lint -- --fix"
},
"devDependencies": {
"@eslint/js": "^9.3.0",
"@html-eslint/eslint-plugin": "^0.24.1",
"@html-eslint/parser": "^0.24.1",
"@stylistic/eslint-plugin": "^2.1.0",
"eslint": "^9.3.0",
"eslint-plugin-html": "^8.1.1",
"espree": "^10.0.1",
"globals": "^15.3.0",
"jsdoc": "^4.0.3"
}
}

View file

@ -0,0 +1,31 @@
/**************************************************************************/
/* platform_config.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. */
/**************************************************************************/
#include <alloca.h>

View file

@ -0,0 +1,44 @@
/**************************************************************************/
/* platform_gl.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. */
/**************************************************************************/
#ifndef PLATFORM_GL_H
#define PLATFORM_GL_H
#ifndef GLES_API_ENABLED
#define GLES_API_ENABLED // Allow using GLES.
#endif
// Make using *glGetProcAddress() an error on the web.
#define glGetProcAddress(n) static_assert(false, "Usage of glGetProcessAddress() on the web is a bug.")
#define eglGetProcAddress(n) static_assert(false, "Usage of eglGetProcessAddress() on the web is a bug.")
#include "platform/web/godot_webgl2.h"
#endif // PLATFORM_GL_H

66
engine/platform/web/serve.py Executable file
View file

@ -0,0 +1,66 @@
#!/usr/bin/env python3
import argparse
import contextlib
import os
import socket
import subprocess
import sys
from http.server import HTTPServer, SimpleHTTPRequestHandler, test # type: ignore
from pathlib import Path
# See cpython GH-17851 and GH-17864.
class DualStackServer(HTTPServer):
def server_bind(self):
# Suppress exception when protocol is IPv4.
with contextlib.suppress(Exception):
self.socket.setsockopt(socket.IPPROTO_IPV6, socket.IPV6_V6ONLY, 0)
return super().server_bind()
class CORSRequestHandler(SimpleHTTPRequestHandler):
def end_headers(self):
self.send_header("Cross-Origin-Opener-Policy", "same-origin")
self.send_header("Cross-Origin-Embedder-Policy", "require-corp")
self.send_header("Access-Control-Allow-Origin", "*")
super().end_headers()
def shell_open(url):
if sys.platform == "win32":
os.startfile(url)
else:
opener = "open" if sys.platform == "darwin" else "xdg-open"
subprocess.call([opener, url])
def serve(root, port, run_browser):
os.chdir(root)
if run_browser:
# Open the served page in the user's default browser.
print("Opening the served URL in the default browser (use `--no-browser` or `-n` to disable this).")
shell_open(f"http://127.0.0.1:{port}")
test(CORSRequestHandler, DualStackServer, port=port)
if __name__ == "__main__":
parser = argparse.ArgumentParser()
parser.add_argument("-p", "--port", help="port to listen on", default=8060, type=int)
parser.add_argument(
"-r", "--root", help="path to serve as root (relative to `platform/web/`)", default="../../bin", type=Path
)
browser_parser = parser.add_mutually_exclusive_group(required=False)
browser_parser.add_argument(
"-n", "--no-browser", help="don't open default web browser automatically", dest="browser", action="store_false"
)
parser.set_defaults(browser=True)
args = parser.parse_args()
# Change to the directory where the script is located,
# so that the script can be run from any location.
os.chdir(Path(__file__).resolve().parent)
serve(args.root, args.port, args.browser)

View file

@ -0,0 +1,144 @@
/**************************************************************************/
/* web_main.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 "display_server_web.h"
#include "godot_js.h"
#include "os_web.h"
#include "core/config/engine.h"
#include "core/io/resource_loader.h"
#include "main/main.h"
#include "scene/main/scene_tree.h"
#include "scene/main/window.h" // SceneTree only forward declares it.
#include <emscripten/emscripten.h>
#include <stdlib.h>
static OS_Web *os = nullptr;
#ifndef PROXY_TO_PTHREAD_ENABLED
static uint64_t target_ticks = 0;
#endif
static bool main_started = false;
static bool shutdown_complete = false;
void exit_callback() {
if (!shutdown_complete) {
return; // Still waiting.
}
if (main_started) {
Main::cleanup();
main_started = false;
}
int exit_code = OS_Web::get_singleton()->get_exit_code();
memdelete(os);
os = nullptr;
emscripten_force_exit(exit_code); // Exit runtime.
}
void cleanup_after_sync() {
shutdown_complete = true;
}
void main_loop_callback() {
#ifndef PROXY_TO_PTHREAD_ENABLED
uint64_t current_ticks = os->get_ticks_usec();
#endif
bool force_draw = DisplayServerWeb::get_singleton()->check_size_force_redraw();
if (force_draw) {
Main::force_redraw();
#ifndef PROXY_TO_PTHREAD_ENABLED
} else if (current_ticks < target_ticks) {
return; // Skip frame.
#endif
}
#ifndef PROXY_TO_PTHREAD_ENABLED
int max_fps = Engine::get_singleton()->get_max_fps();
if (max_fps > 0) {
if (current_ticks - target_ticks > 1000000) {
// When the window loses focus, we stop getting updates and accumulate delay.
// For this reason, if the difference is too big, we reset target ticks to the current ticks.
target_ticks = current_ticks;
}
target_ticks += (uint64_t)(1000000 / max_fps);
}
#endif
if (os->main_loop_iterate()) {
emscripten_cancel_main_loop(); // Cancel current loop and set the cleanup one.
emscripten_set_main_loop(exit_callback, -1, false);
godot_js_os_finish_async(cleanup_after_sync);
}
}
/// When calling main, it is assumed FS is setup and synced.
extern EMSCRIPTEN_KEEPALIVE int godot_web_main(int argc, char *argv[]) {
os = new OS_Web();
// We must override main when testing is enabled
TEST_MAIN_OVERRIDE
Error err = Main::setup(argv[0], argc - 1, &argv[1]);
// Proper shutdown in case of setup failure.
if (err != OK) {
// Will only exit after sync.
emscripten_set_main_loop(exit_callback, -1, false);
godot_js_os_finish_async(cleanup_after_sync);
if (err == ERR_HELP) { // Returned by --help and --version, so success.
return EXIT_SUCCESS;
}
return EXIT_FAILURE;
}
main_started = true;
// Ease up compatibility.
ResourceLoader::set_abort_on_missing_resources(false);
int ret = Main::start();
os->set_exit_code(ret);
os->get_main_loop()->initialize();
#ifdef TOOLS_ENABLED
if (Engine::get_singleton()->is_project_manager_hint() && FileAccess::exists("/tmp/preload.zip")) {
PackedStringArray ps;
ps.push_back("/tmp/preload.zip");
SceneTree::get_singleton()->get_root()->emit_signal(SNAME("files_dropped"), ps);
}
#endif
emscripten_set_main_loop(main_loop_callback, -1, false);
// Immediately run the first iteration.
// We are inside an animation frame, we want to immediately draw on the newly setup canvas.
main_loop_callback();
return os->get_exit_code();
}

View file

@ -0,0 +1,35 @@
/**************************************************************************/
/* web_runtime.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. */
/**************************************************************************/
extern int godot_web_main(int argc, char *argv[]);
int main(int argc, char *argv[]) {
return godot_web_main(argc, argv);
}