From 40f85073420460b4e8ed20b7f0d3b56712d8a732 Mon Sep 17 00:00:00 2001 From: Sergii Pylypenko Date: Sun, 19 Dec 2021 02:37:05 +0200 Subject: [PATCH] Emscripten: Download savegames automatically --- src/openttd.cpp | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) diff --git a/src/openttd.cpp b/src/openttd.cpp index a4adfa6b3d..5399062575 100644 --- a/src/openttd.cpp +++ b/src/openttd.cpp @@ -1158,6 +1158,26 @@ void ProcessCloudSaveFromVideoThread() int status = 0; #ifdef __ANDROID__ status = SDL_ANDROID_CloudSave(_file_to_saveload.name.c_str(), lastPart, "OpenTTD", lastPart, screenshotFile.c_str(), playedTime); +#endif +#ifdef __EMSCRIPTEN__ + FILE *ff = fopen(_file_to_saveload.name.c_str(), "rb"); + if (ff) { + fseek(ff, 0, SEEK_END); + long size = ftell(ff); + std::unique_ptr data(new byte[size]); + fseek(ff, 0, SEEK_SET); + fread(data.get(), 1, size, ff); + fclose(ff); + EM_ASM( { + const blob = new Blob([new Uint8Array(Module.HEAPU8.buffer, $0, $1)], {type: "application/octet-stream"}); + const elem = window.document.createElement("a"); + elem.href = window.URL.createObjectURL(blob); + elem.download = UTF8ToString($2); + document.body.appendChild(elem); + elem.click(); + document.body.removeChild(elem); + }, data.get(), size, lastPart ); + } #endif if (_settings_client.gui.save_to_network == 2) { _settings_client.gui.save_to_network = status ? 1 : 0;