Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[HTML5] Replace XMLHttpRequest(s) with Fetch. #46728

Merged
merged 4 commits into from
Mar 6, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion platform/javascript/SCsub
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ sys_env.AddJSLibraries(
[
"js/libs/library_godot_audio.js",
"js/libs/library_godot_display.js",
"js/libs/library_godot_http_request.js",
"js/libs/library_godot_fetch.js",
"js/libs/library_godot_os.js",
"js/libs/library_godot_runtime.js",
]
Expand Down
46 changes: 30 additions & 16 deletions platform/javascript/export/export.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -242,7 +242,7 @@ class EditorExportPlatformJavaScript : public EditorExportPlatform {
return name;
}

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);
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);

static void _server_thread_poll(void *data);

Expand Down Expand Up @@ -281,7 +281,7 @@ class EditorExportPlatformJavaScript : public EditorExportPlatform {
~EditorExportPlatformJavaScript();
};

void EditorExportPlatformJavaScript::_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) {
void EditorExportPlatformJavaScript::_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) {
String str_template = String::utf8(reinterpret_cast<const char *>(p_html.ptr()), p_html.size());
String str_export;
Vector<String> lines = str_template.split("\n");
Expand All @@ -300,6 +300,7 @@ void EditorExportPlatformJavaScript::_fix_html(Vector<uint8_t> &p_html, const Re
config["gdnativeLibs"] = libs;
config["executable"] = p_name;
config["args"] = args;
config["fileSizes"] = p_file_sizes;
const String str_config = JSON::print(config);

for (int i = 0; i < lines.size(); i++) {
Expand Down Expand Up @@ -472,6 +473,8 @@ Error EditorExportPlatformJavaScript::export_project(const Ref<EditorExportPrese
return ERR_FILE_CORRUPT;
}

Vector<uint8_t> html;
Dictionary file_sizes;
do {
//get filename
unz_file_info info;
Expand All @@ -480,6 +483,16 @@ Error EditorExportPlatformJavaScript::export_project(const Ref<EditorExportPrese

String file = fname;

// HTML is handled later
if (file == "godot.html") {
if (custom_html.is_empty()) {
html.resize(info.uncompressed_size);
unzOpenCurrentFile(pkg);
unzReadCurrentFile(pkg, html.ptrw(), html.size());
unzCloseCurrentFile(pkg);
}
continue;
}
Vector<uint8_t> data;
data.resize(info.uncompressed_size);

Expand All @@ -490,14 +503,7 @@ Error EditorExportPlatformJavaScript::export_project(const Ref<EditorExportPrese

//write

if (file == "godot.html") {
if (!custom_html.is_empty()) {
continue;
}
_fix_html(data, p_preset, p_path.get_file().get_basename(), p_debug, p_flags, shared_objects);
file = p_path.get_file();

} else if (file == "godot.js") {
if (file == "godot.js") {
file = p_path.get_file().get_basename() + ".js";

} else if (file == "godot.worker.js") {
Expand All @@ -511,6 +517,7 @@ Error EditorExportPlatformJavaScript::export_project(const Ref<EditorExportPrese

} else if (file == "godot.wasm") {
file = p_path.get_file().get_basename() + ".wasm";
file_sizes[file.get_file()] = (uint64_t)info.uncompressed_size;
}

String dst = p_path.get_base_dir().plus_file(file);
Expand All @@ -532,19 +539,26 @@ Error EditorExportPlatformJavaScript::export_project(const Ref<EditorExportPrese
EditorNode::get_singleton()->show_warning(TTR("Could not read custom HTML shell:") + "\n" + custom_html);
return ERR_FILE_CANT_READ;
}
Vector<uint8_t> buf;
buf.resize(f->get_len());
f->get_buffer(buf.ptrw(), buf.size());
html.resize(f->get_len());
f->get_buffer(html.ptrw(), html.size());
memdelete(f);
_fix_html(buf, p_preset, p_path.get_file().get_basename(), p_debug, p_flags, shared_objects);

}
{
FileAccess *f = FileAccess::open(pck_path, FileAccess::READ);
if (f) {
file_sizes[pck_path.get_file()] = (uint64_t)f->get_len();
memdelete(f);
f = NULL;
}
_fix_html(html, p_preset, p_path.get_file().get_basename(), p_debug, p_flags, shared_objects, file_sizes);
f = FileAccess::open(p_path, FileAccess::WRITE);
if (!f) {
EditorNode::get_singleton()->show_warning(TTR("Could not write file:") + "\n" + p_path);
return ERR_FILE_CANT_WRITE;
}
f->store_buffer(buf.ptr(), buf.size());
f->store_buffer(html.ptr(), html.size());
memdelete(f);
html.resize(0);
}

Ref<Image> splash;
Expand Down
13 changes: 5 additions & 8 deletions platform/javascript/http_client.h.inc
Original file line number Diff line number Diff line change
Expand Up @@ -30,24 +30,21 @@

// HTTPClient's additional private members in the javascript platform

Error prepare_request(Method p_method, const String &p_url, const Vector<String> &p_headers);
Error make_request(Method p_method, const String &p_url, const Vector<String> &p_headers, const uint8_t *p_body, int p_body_len);
static void _parse_headers(int p_len, const char **p_headers, void *p_ref);

int xhr_id;
int js_id = 0;
int read_limit = 4096;
int response_read_offset = 0;
Status status = STATUS_DISCONNECTED;

String host;
int port = -1;
bool use_tls = false;
String username;
String password;

int polled_response_code = 0;
String polled_response_header;
PackedByteArray polled_response;
Vector<String> response_headers;
Vector<uint8_t> response_buffer;

#ifdef DEBUG_ENABLED
bool has_polled = false;
uint64_t last_polling_frame = 0;
#endif
Loading