mirror of
https://github.com/scummvm/scummvm.git
synced 2026-05-21 05:40:43 +00:00
EMSCRIPTEN: CLOUD: Implement cloud support in Emscripten
This commit is contained in:
+16
-5
@@ -80,15 +80,15 @@ endif
|
||||
ifdef USE_LIBCURL
|
||||
MODULE_OBJS += \
|
||||
networking/http/connectionmanager.o \
|
||||
networking/http/networkreadstream.o \
|
||||
networking/http/httprequest.o \
|
||||
networking/http/curl/networkreadstream-curl.o \
|
||||
networking/http/curl/socket.o \
|
||||
networking/http/curl/url.o \
|
||||
networking/http/httpjsonrequest.o \
|
||||
networking/http/httprequest.o \
|
||||
networking/http/postrequest.o \
|
||||
networking/http/request.o \
|
||||
networking/http/session.o \
|
||||
networking/http/sessionrequest.o \
|
||||
networking/http/socket.o \
|
||||
networking/http/url.o
|
||||
networking/http/sessionrequest.o
|
||||
endif
|
||||
|
||||
ifdef EMSCRIPTEN
|
||||
@@ -97,6 +97,17 @@ MODULE_OBJS += \
|
||||
fs/emscripten/emscripten-posix-fs.o \
|
||||
fs/emscripten/http-fs.o \
|
||||
midi/webmidi.o
|
||||
ifdef USE_CLOUD
|
||||
MODULE_OBJS += \
|
||||
networking/http/connectionmanager.o \
|
||||
networking/http/httpjsonrequest.o \
|
||||
networking/http/httprequest.o \
|
||||
networking/http/postrequest.o \
|
||||
networking/http/request.o \
|
||||
networking/http/session.o \
|
||||
networking/http/sessionrequest.o \
|
||||
networking/http/emscripten/networkreadstream-emscripten.o
|
||||
endif
|
||||
ifdef USE_TTS
|
||||
MODULE_OBJS += \
|
||||
text-to-speech/emscripten/emscripten-text-to-speech.o
|
||||
|
||||
@@ -21,7 +21,6 @@
|
||||
|
||||
#define FORBIDDEN_SYMBOL_ALLOW_ALL
|
||||
|
||||
#include <curl/curl.h>
|
||||
#include "backends/networking/http/connectionmanager.h"
|
||||
#include "backends/networking/http/networkreadstream.h"
|
||||
#include "common/debug.h"
|
||||
@@ -29,6 +28,12 @@
|
||||
#include "common/system.h"
|
||||
#include "common/timer.h"
|
||||
|
||||
#if defined(USE_LIBCURL)
|
||||
#include <curl/curl.h>
|
||||
#endif
|
||||
#if defined(EMSCRIPTEN)
|
||||
#include <emscripten.h>
|
||||
#endif
|
||||
#if defined(ANDROID_BACKEND)
|
||||
#include "backends/platform/android/jni-android.h"
|
||||
#endif
|
||||
@@ -42,8 +47,10 @@ DECLARE_SINGLETON(Networking::ConnectionManager);
|
||||
namespace Networking {
|
||||
|
||||
ConnectionManager::ConnectionManager(): _multi(nullptr), _timerStarted(false), _frame(0) {
|
||||
#ifdef USE_LIBCURL
|
||||
curl_global_init(CURL_GLOBAL_ALL);
|
||||
_multi = curl_multi_init();
|
||||
#endif
|
||||
}
|
||||
|
||||
ConnectionManager::~ConnectionManager() {
|
||||
@@ -81,15 +88,19 @@ ConnectionManager::~ConnectionManager() {
|
||||
_requests.clear();
|
||||
|
||||
//cleanup
|
||||
#ifdef USE_LIBCURL
|
||||
curl_multi_cleanup(_multi);
|
||||
curl_global_cleanup();
|
||||
_multi = nullptr;
|
||||
#endif
|
||||
_handleMutex.unlock();
|
||||
}
|
||||
|
||||
#ifdef USE_LIBCURL
|
||||
void ConnectionManager::registerEasyHandle(CURL *easy) const {
|
||||
curl_multi_add_handle(_multi, easy);
|
||||
}
|
||||
#endif
|
||||
|
||||
Request *ConnectionManager::addRequest(Request *request, RequestCallback callback) {
|
||||
_addedRequestsMutex.lock();
|
||||
@@ -101,6 +112,9 @@ Request *ConnectionManager::addRequest(Request *request, RequestCallback callbac
|
||||
}
|
||||
|
||||
Common::String ConnectionManager::urlEncode(const Common::String &s) const {
|
||||
Common::String result = "";
|
||||
debug(5, "ConnectionManager::urlEncode(%s)", s.c_str());
|
||||
#ifdef USE_LIBCURL
|
||||
if (!_multi)
|
||||
return "";
|
||||
#if LIBCURL_VERSION_NUM >= 0x070F04
|
||||
@@ -109,11 +123,22 @@ Common::String ConnectionManager::urlEncode(const Common::String &s) const {
|
||||
char *output = curl_escape(s.c_str(), s.size());
|
||||
#endif
|
||||
if (output) {
|
||||
Common::String result = output;
|
||||
result = output;
|
||||
curl_free(output);
|
||||
return result;
|
||||
}
|
||||
return "";
|
||||
#elif defined(EMSCRIPTEN)
|
||||
const char *input = s.c_str();
|
||||
result = (char*) EM_ASM_PTR({
|
||||
var jsString = encodeURIComponent(UTF8ToString($0));
|
||||
var size = lengthBytesUTF8(jsString) + 1;
|
||||
var ret = Module._malloc(size);
|
||||
stringToUTF8Array(jsString, HEAP8, ret, size);
|
||||
return ret;
|
||||
}, input);
|
||||
#endif
|
||||
debug(5, "ConnectionManager::urlEncode(%s) = %s", s.c_str(), result.c_str());
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
uint32 ConnectionManager::getCloudRequestsPeriodInMicroseconds() {
|
||||
@@ -227,6 +252,7 @@ void ConnectionManager::interateRequests() {
|
||||
}
|
||||
|
||||
void ConnectionManager::processTransfers() {
|
||||
#ifdef USE_LIBCURL
|
||||
if (!_multi) return;
|
||||
|
||||
//check libcurl's transfers and notify requests of messages from queue (transfer completion or failure)
|
||||
@@ -250,6 +276,7 @@ void ConnectionManager::processTransfers() {
|
||||
warning("Unknown libcurl message type %d", curlMsg->msg);
|
||||
}
|
||||
}
|
||||
#endif
|
||||
}
|
||||
|
||||
} // End of namespace Cloud
|
||||
|
||||
@@ -91,12 +91,14 @@ public:
|
||||
ConnectionManager();
|
||||
~ConnectionManager() override;
|
||||
|
||||
#ifdef USE_LIBCURL
|
||||
/**
|
||||
* All libcurl transfers are going through this ConnectionManager.
|
||||
* So, if you want to start any libcurl transfer, you must create
|
||||
* an easy handle and register it using this method.
|
||||
*/
|
||||
void registerEasyHandle(CURL *easy) const;
|
||||
#endif
|
||||
|
||||
/**
|
||||
* Use this method to add new Request into manager's queue.
|
||||
|
||||
@@ -21,8 +21,8 @@
|
||||
|
||||
#define FORBIDDEN_SYMBOL_ALLOW_ALL
|
||||
#include <curl/curl.h>
|
||||
#include "backends/networking/http/socket.h"
|
||||
#include "backends/networking/http/connectionmanager.h"
|
||||
#include "backends/networking/http/curl/socket.h"
|
||||
#include "common/debug.h"
|
||||
#include "common/system.h"
|
||||
|
||||
@@ -18,8 +18,8 @@
|
||||
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*
|
||||
*/
|
||||
#ifndef BACKENDS_NETWORKING_HTTP_SOCKET_H
|
||||
#define BACKENDS_NETWORKING_HTTP_SOCKET_H
|
||||
#ifndef BACKENDS_NETWORKING_CURL_SOCKET_H
|
||||
#define BACKENDS_NETWORKING_CURL_SOCKET_H
|
||||
|
||||
typedef void CURL;
|
||||
#ifdef WIN32
|
||||
@@ -21,7 +21,7 @@
|
||||
|
||||
#define FORBIDDEN_SYMBOL_ALLOW_ALL
|
||||
#include <curl/curl.h>
|
||||
#include "backends/networking/http/url.h"
|
||||
#include "backends/networking/http/curl/url.h"
|
||||
#include "common/debug.h"
|
||||
#include "common/textconsole.h"
|
||||
|
||||
@@ -18,8 +18,8 @@
|
||||
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*
|
||||
*/
|
||||
#ifndef BACKENDS_NETWORKING_HTTP_URL_H
|
||||
#define BACKENDS_NETWORKING_HTTP_URL_H
|
||||
#ifndef BACKENDS_NETWORKING_CURL_URL_H
|
||||
#define BACKENDS_NETWORKING_CURL_URL_H
|
||||
|
||||
typedef struct Curl_URL CURLU;
|
||||
|
||||
@@ -0,0 +1,349 @@
|
||||
/* ScummVM - Graphic Adventure Engine
|
||||
*
|
||||
* ScummVM is the legal property of its developers, whose names
|
||||
* are too numerous to list here. Please refer to the COPYRIGHT
|
||||
* file distributed with this source distribution.
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*
|
||||
*/
|
||||
|
||||
#define FORBIDDEN_SYMBOL_EXCEPTION_asctime
|
||||
#define FORBIDDEN_SYMBOL_EXCEPTION_clock
|
||||
#define FORBIDDEN_SYMBOL_EXCEPTION_ctime
|
||||
#define FORBIDDEN_SYMBOL_EXCEPTION_difftime
|
||||
#define FORBIDDEN_SYMBOL_EXCEPTION_FILE
|
||||
#define FORBIDDEN_SYMBOL_EXCEPTION_getdate
|
||||
#define FORBIDDEN_SYMBOL_EXCEPTION_gmtime
|
||||
#define FORBIDDEN_SYMBOL_EXCEPTION_localtime
|
||||
#define FORBIDDEN_SYMBOL_EXCEPTION_mktime
|
||||
#define FORBIDDEN_SYMBOL_EXCEPTION_strcpy
|
||||
#define FORBIDDEN_SYMBOL_EXCEPTION_strdup
|
||||
#define FORBIDDEN_SYMBOL_EXCEPTION_time
|
||||
#include <emscripten.h>
|
||||
#include <emscripten/fetch.h>
|
||||
|
||||
#include "backends/networking/http/networkreadstream.h"
|
||||
#include "base/version.h"
|
||||
#include "common/debug.h"
|
||||
|
||||
#define CURLE_OK 0x0 // the only CURLcode value used/checked in ScummVM
|
||||
|
||||
namespace Networking {
|
||||
|
||||
void NetworkReadStream::emscriptenOnReadyStateChange(emscripten_fetch_t *fetch) {
|
||||
if (fetch->readyState != 2)
|
||||
return;
|
||||
|
||||
size_t headersLengthBytes = emscripten_fetch_get_response_headers_length(fetch) + 1;
|
||||
char *headerString = (char *)malloc(headersLengthBytes);
|
||||
|
||||
assert(headerString);
|
||||
emscripten_fetch_get_response_headers(fetch, headerString, headersLengthBytes);
|
||||
NetworkReadStream *stream = (NetworkReadStream *)fetch->userData;
|
||||
stream->addResponseHeaders(headerString, headersLengthBytes);
|
||||
free(headerString);
|
||||
}
|
||||
|
||||
void NetworkReadStream::emscriptenOnProgress(emscripten_fetch_t *fetch) {
|
||||
/*
|
||||
if (fetch->totalBytes) {
|
||||
debug(5,"Downloading %s.. %.2f percent complete.", fetch->url, fetch->dataOffset * 100.0 / fetch->totalBytes);
|
||||
} else {
|
||||
debug(5,"Downloading %s.. %lld bytes complete.", fetch->url, fetch->dataOffset + fetch->numBytes);
|
||||
}
|
||||
debug(5,"Downloading %s.. %.2f %s complete. HTTP readyState: %hu. HTTP status: %hu - "
|
||||
"HTTP statusText: %s. Received chunk [%llu, %llu]",
|
||||
fetch->url,
|
||||
fetch->totalBytes > 0 ? (fetch->dataOffset + fetch->numBytes) * 100.0 / fetch->totalBytes : (fetch->dataOffset + fetch->numBytes),
|
||||
fetch->totalBytes > 0 ? "percent" : " bytes",
|
||||
fetch->readyState,
|
||||
fetch->status,
|
||||
fetch->statusText,
|
||||
fetch->dataOffset,
|
||||
fetch->dataOffset + fetch->numBytes);
|
||||
*/
|
||||
NetworkReadStream *stream = (NetworkReadStream *)fetch->userData;
|
||||
if (stream) {
|
||||
stream->setProgress(fetch->dataOffset, fetch->totalBytes);
|
||||
}
|
||||
}
|
||||
|
||||
void NetworkReadStream::emscriptenOnSuccess(emscripten_fetch_t *fetch) {
|
||||
NetworkReadStream *stream = (NetworkReadStream *)fetch->userData;
|
||||
stream->emscriptenDownloadFinished(true);
|
||||
}
|
||||
|
||||
void NetworkReadStream::emscriptenOnError(emscripten_fetch_t *fetch) {
|
||||
NetworkReadStream *stream = (NetworkReadStream *)fetch->userData;
|
||||
stream->emscriptenDownloadFinished(false);
|
||||
}
|
||||
|
||||
void NetworkReadStream::emscriptenDownloadFinished(bool success) {
|
||||
|
||||
if (success) {
|
||||
debug(5, "NetworkReadStream::emscriptenHandleDownload Finished downloading %llu bytes from URL %s. HTTP status code: %d", _emscripten_fetch->numBytes, _emscripten_fetch->url, _emscripten_fetch->status);
|
||||
} else {
|
||||
debug(5, "NetworkReadStream::emscriptenHandleDownload Downloading %s failed, HTTP failure status code: %d, status text: %s", _emscripten_fetch->url, _emscripten_fetch->status, _emscripten_fetch->statusText);
|
||||
}
|
||||
if (_emscripten_fetch->numBytes > 0) {
|
||||
// TODO: This could be done continuously during emscriptenOnProgress?
|
||||
this->_backingStream.write(_emscripten_fetch->data, _emscripten_fetch->numBytes);
|
||||
}
|
||||
this->setProgress(_emscripten_fetch->numBytes, _emscripten_fetch->numBytes);
|
||||
if (success) {
|
||||
this->finished(CURLE_OK); // TODO: actually pass the result code from emscripten_fetch
|
||||
|
||||
} else {
|
||||
this->finished(-1);
|
||||
}
|
||||
}
|
||||
|
||||
void NetworkReadStream::resetStream() {
|
||||
_eos = _requestComplete = false;
|
||||
_sendingContentsSize = _sendingContentsPos = 0;
|
||||
_progressDownloaded = _progressTotal = 0;
|
||||
_bufferCopy = nullptr;
|
||||
_emscripten_fetch = nullptr;
|
||||
_emscripten_request_headers = nullptr;
|
||||
}
|
||||
|
||||
void NetworkReadStream::initEmscripten(const char *url, RequestHeaders *headersList) {
|
||||
|
||||
resetStream();
|
||||
emscripten_fetch_attr_init(_emscripten_fetch_attr);
|
||||
|
||||
// convert header list
|
||||
// first get size of list
|
||||
int size = 0;
|
||||
if (headersList) {
|
||||
size = headersList->size();
|
||||
debug(5, "_emscripten_request_headers count: %d", size);
|
||||
}
|
||||
_emscripten_request_headers = new char *[size * 2 + 1];
|
||||
_emscripten_request_headers[size * 2] = 0; // header array needs to be null-terminated.
|
||||
|
||||
int i = 0;
|
||||
if (headersList) {
|
||||
for (const Common::String &header : *headersList) {
|
||||
// Find the colon separator
|
||||
uint colonPos = header.findFirstOf(':');
|
||||
if (colonPos == Common::String::npos) {
|
||||
warning("NetworkReadStream: Malformed header (no colon): %s", header.c_str());
|
||||
continue;
|
||||
}
|
||||
|
||||
// Split into key and value parts
|
||||
Common::String key = header.substr(0, colonPos);
|
||||
Common::String value = header.substr(colonPos + 1);
|
||||
|
||||
// Trim whitespace from key and value
|
||||
key.trim();
|
||||
value.trim();
|
||||
|
||||
// Store key and value as separate strings
|
||||
_emscripten_request_headers[i++] = strdup(key.c_str());
|
||||
_emscripten_request_headers[i++] = strdup(value.c_str());
|
||||
debug(9, "_emscripten_request_headers key='%s' value='%s'", key.c_str(), value.c_str());
|
||||
}
|
||||
}
|
||||
|
||||
_emscripten_fetch_attr->requestHeaders = _emscripten_request_headers;
|
||||
strcpy(_emscripten_fetch_attr->requestMethod, "GET"); // todo: move down to setup buffer contents
|
||||
_emscripten_fetch_attr->attributes = EMSCRIPTEN_FETCH_LOAD_TO_MEMORY;
|
||||
_emscripten_fetch_attr->onerror = emscriptenOnError;
|
||||
_emscripten_fetch_attr->onprogress = emscriptenOnProgress;
|
||||
_emscripten_fetch_attr->onreadystatechange = emscriptenOnReadyStateChange;
|
||||
_emscripten_fetch_attr->onsuccess = emscriptenOnSuccess;
|
||||
_emscripten_fetch_attr->userData = this;
|
||||
}
|
||||
void NetworkReadStream::setupBufferContents(const byte *buffer, uint32 bufferSize, bool uploading, bool usingPatch, bool post) {
|
||||
if (uploading) {
|
||||
strcpy(_emscripten_fetch_attr->requestMethod, "PUT");
|
||||
_emscripten_fetch_attr->requestDataSize = bufferSize;
|
||||
_emscripten_fetch_attr->requestData = (const char *)buffer;
|
||||
} else if (usingPatch) {
|
||||
strcpy(_emscripten_fetch_attr->requestMethod, "PATCH");
|
||||
} else {
|
||||
if (post || bufferSize != 0) {
|
||||
strcpy(_emscripten_fetch_attr->requestMethod, "POST");
|
||||
_emscripten_fetch_attr->requestDataSize = bufferSize;
|
||||
_emscripten_fetch_attr->requestData = (const char *)buffer;
|
||||
}
|
||||
}
|
||||
debug(5, "NetworkReadStream::setupBufferContents uploading %s usingPatch %s post %s ->method %s", uploading ? "true" : "false", usingPatch ? "true" : "false", post ? "true" : "false", _emscripten_fetch_attr->requestMethod);
|
||||
_emscripten_fetch = emscripten_fetch(_emscripten_fetch_attr, _emscripten_fetch_url);
|
||||
}
|
||||
|
||||
void NetworkReadStream::setupFormMultipart(const Common::HashMap<Common::String, Common::String> &formFields, const Common::HashMap<Common::String, Common::Path> &formFiles) {
|
||||
// set POST multipart upload form fields/files
|
||||
error("NetworkReadStream::setupFormMultipart not implemented");
|
||||
}
|
||||
|
||||
/** Send <postFields>, using POST by default. */
|
||||
NetworkReadStream::NetworkReadStream(const char *url, RequestHeaders *headersList, const Common::String &postFields, bool uploading, bool usingPatch, bool keepAlive, long keepAliveIdle, long keepAliveInterval) : _emscripten_fetch_attr(new emscripten_fetch_attr_t()), _emscripten_fetch_url(url), _backingStream(DisposeAfterUse::YES), _keepAlive(keepAlive), _errorBuffer(nullptr), _errorCode(CURLE_OK) {
|
||||
initEmscripten(url, headersList);
|
||||
setupBufferContents((const byte *)postFields.c_str(), postFields.size(), uploading, usingPatch, false);
|
||||
}
|
||||
/** Send <formFields>, <formFiles>, using POST multipart/form. */
|
||||
NetworkReadStream::NetworkReadStream(const char *url, RequestHeaders *headersList, const Common::HashMap<Common::String, Common::String> &formFields, const Common::HashMap<Common::String, Common::Path> &formFiles, bool keepAlive, long keepAliveIdle, long keepAliveInterval) : _emscripten_fetch_attr(new emscripten_fetch_attr_t()), _emscripten_fetch_url(url), _backingStream(DisposeAfterUse::YES), _keepAlive(keepAlive), _errorBuffer(nullptr), _errorCode(CURLE_OK) {
|
||||
initEmscripten(url, headersList);
|
||||
setupFormMultipart(formFields, formFiles);
|
||||
}
|
||||
|
||||
/** Send <buffer>, using POST by default. */
|
||||
NetworkReadStream::NetworkReadStream(const char *url, RequestHeaders *headersList, const byte *buffer, uint32 bufferSize, bool uploading, bool usingPatch, bool post, bool keepAlive, long keepAliveIdle, long keepAliveInterval) : _emscripten_fetch_attr(new emscripten_fetch_attr_t()), _emscripten_fetch_url(url), _backingStream(DisposeAfterUse::YES), _keepAlive(keepAlive), _errorBuffer(nullptr), _errorCode(CURLE_OK) {
|
||||
initEmscripten(url, headersList);
|
||||
setupBufferContents(buffer, bufferSize, uploading, usingPatch, post);
|
||||
}
|
||||
|
||||
bool NetworkReadStream::reuse(const char *url, RequestHeaders *headersList, const Common::String &postFields, bool uploading, bool usingPatch) {
|
||||
return false; // Not implemented for Emscripten
|
||||
}
|
||||
|
||||
bool NetworkReadStream::reuse(const char *url, RequestHeaders *headersList, const Common::HashMap<Common::String, Common::String> &formFields, const Common::HashMap<Common::String, Common::Path> &formFiles) {
|
||||
return false; // Not implemented for Emscripten
|
||||
}
|
||||
|
||||
bool NetworkReadStream::reuse(const char *url, RequestHeaders *headersList, const byte *buffer, uint32 bufferSize, bool uploading, bool usingPatch, bool post) {
|
||||
return false; // Not implemented for Emscripten
|
||||
}
|
||||
|
||||
NetworkReadStream::~NetworkReadStream() {
|
||||
if (_emscripten_fetch) {
|
||||
debug(5, "~NetworkReadStream: emscripten_fetch_close");
|
||||
emscripten_fetch_close(_emscripten_fetch);
|
||||
}
|
||||
|
||||
// Free the headers array and its contents
|
||||
if (_emscripten_request_headers) {
|
||||
for (int i = 0; _emscripten_request_headers[i] != nullptr; ++i) {
|
||||
free(_emscripten_request_headers[i]); // Free each strdup'd string
|
||||
}
|
||||
delete[] _emscripten_request_headers;
|
||||
}
|
||||
|
||||
free(_bufferCopy);
|
||||
free(_errorBuffer);
|
||||
}
|
||||
|
||||
bool NetworkReadStream::eos() const {
|
||||
return _eos;
|
||||
}
|
||||
|
||||
uint32 NetworkReadStream::read(void *dataPtr, uint32 dataSize) {
|
||||
uint32 actuallyRead = _backingStream.read(dataPtr, dataSize);
|
||||
|
||||
// Only access _emscripten_fetch->url if _emscripten_fetch is valid
|
||||
// debug(5,"NetworkReadStream::read %u %s %s %s", actuallyRead, _eos ? "_eos" : "not _eos", _requestComplete ? "_requestComplete" : "_request not Complete", _emscripten_fetch ? _emscripten_fetch->url : "no-url");
|
||||
if (actuallyRead == 0) {
|
||||
if (_requestComplete)
|
||||
_eos = true;
|
||||
return 0;
|
||||
}
|
||||
|
||||
return actuallyRead;
|
||||
}
|
||||
|
||||
void NetworkReadStream::finished(uint32 errorCode) {
|
||||
_requestComplete = true;
|
||||
_errorCode = errorCode;
|
||||
|
||||
if (_errorCode == CURLE_OK) {
|
||||
debug(9, "NetworkReadStream::finished %s - Request succeeded", _emscripten_fetch_url);
|
||||
} else {
|
||||
// Make a copy of the error message since _emscripten_fetch might be cleaned up
|
||||
if (_emscripten_fetch && _emscripten_fetch->statusText) {
|
||||
_errorBuffer = strdup(_emscripten_fetch->statusText);
|
||||
} else {
|
||||
_errorBuffer = strdup("Unknown error");
|
||||
}
|
||||
warning("NetworkReadStream::finished %s - Request failed (%d - %s)", _emscripten_fetch_url, _errorCode, getError());
|
||||
}
|
||||
}
|
||||
|
||||
bool NetworkReadStream::hasError() const {
|
||||
return _errorCode != CURLE_OK;
|
||||
}
|
||||
|
||||
const char *NetworkReadStream::getError() const {
|
||||
return _errorBuffer;
|
||||
}
|
||||
|
||||
long NetworkReadStream::httpResponseCode() const {
|
||||
// return 200;
|
||||
unsigned short responseCode = 0;
|
||||
if (_emscripten_fetch)
|
||||
responseCode = _emscripten_fetch->status;
|
||||
debug(5, "NetworkReadStream::httpResponseCode %hu %hu ", _emscripten_fetch->status, responseCode);
|
||||
return responseCode;
|
||||
}
|
||||
|
||||
Common::String NetworkReadStream::currentLocation() const {
|
||||
debug(5, "NetworkReadStream::currentLocation %s", _emscripten_fetch_url);
|
||||
return Common::String(_emscripten_fetch_url);
|
||||
}
|
||||
|
||||
Common::String NetworkReadStream::responseHeaders() const {
|
||||
return _responseHeaders;
|
||||
}
|
||||
|
||||
Common::HashMap<Common::String, Common::String> NetworkReadStream::responseHeadersMap() const {
|
||||
|
||||
Common::HashMap<Common::String, Common::String> headers;
|
||||
|
||||
const char *headerString = responseHeaders().c_str();
|
||||
char **responseHeaders = emscripten_fetch_unpack_response_headers(headerString);
|
||||
assert(responseHeaders);
|
||||
|
||||
int numHeaders = 0;
|
||||
for (; responseHeaders[numHeaders * 2]; ++numHeaders) {
|
||||
// Check both the header and its value are present.
|
||||
assert(responseHeaders[(numHeaders * 2) + 1]);
|
||||
headers[responseHeaders[numHeaders * 2]] = responseHeaders[(numHeaders * 2) + 1];
|
||||
}
|
||||
|
||||
emscripten_fetch_free_unpacked_response_headers(responseHeaders);
|
||||
|
||||
return headers;
|
||||
}
|
||||
|
||||
uint32 NetworkReadStream::fillWithSendingContents(char *bufferToFill, uint32 maxSize) {
|
||||
uint32 sendSize = _sendingContentsSize - _sendingContentsPos;
|
||||
if (sendSize > maxSize)
|
||||
sendSize = maxSize;
|
||||
for (uint32 i = 0; i < sendSize; ++i) {
|
||||
bufferToFill[i] = _sendingContentsBuffer[_sendingContentsPos + i];
|
||||
}
|
||||
_sendingContentsPos += sendSize;
|
||||
return sendSize;
|
||||
}
|
||||
|
||||
uint32 NetworkReadStream::addResponseHeaders(char *buffer, uint32 bufferSize) {
|
||||
_responseHeaders += Common::String(buffer, bufferSize);
|
||||
return bufferSize;
|
||||
}
|
||||
|
||||
double NetworkReadStream::getProgress() const {
|
||||
if (_progressTotal < 1)
|
||||
return 0;
|
||||
return (double)_progressDownloaded / (double)_progressTotal;
|
||||
}
|
||||
|
||||
void NetworkReadStream::setProgress(uint64 downloaded, uint64 total) {
|
||||
_progressDownloaded = downloaded;
|
||||
_progressTotal = total;
|
||||
}
|
||||
|
||||
} // namespace Networking
|
||||
@@ -27,16 +27,31 @@
|
||||
#include "common/stream.h"
|
||||
#include "common/str.h"
|
||||
#include "common/hashmap.h"
|
||||
#include "common/array.h"
|
||||
#include "common/hash-str.h"
|
||||
|
||||
#ifdef USE_LIBCURL
|
||||
typedef void CURL;
|
||||
#endif
|
||||
#ifdef EMSCRIPTEN
|
||||
struct emscripten_fetch_attr_t;
|
||||
struct emscripten_fetch_t;
|
||||
#endif
|
||||
|
||||
namespace Networking {
|
||||
typedef Common::Array<Common::String> RequestHeaders;
|
||||
|
||||
|
||||
class NetworkReadStream : public Common::ReadStream {
|
||||
#ifdef USE_LIBCURL
|
||||
CURL *_easy;
|
||||
struct curl_slist *_headersSlist; // Track the curl headers list for cleanup
|
||||
#endif
|
||||
#ifdef EMSCRIPTEN
|
||||
emscripten_fetch_attr_t *_emscripten_fetch_attr;
|
||||
emscripten_fetch_t *_emscripten_fetch;
|
||||
const char *_emscripten_fetch_url = nullptr;
|
||||
char **_emscripten_request_headers;
|
||||
#endif
|
||||
Common::MemoryReadWriteStream _backingStream;
|
||||
bool _keepAlive;
|
||||
long _keepAliveIdle, _keepAliveInterval;
|
||||
@@ -51,9 +66,14 @@ class NetworkReadStream : public Common::ReadStream {
|
||||
uint64 _progressDownloaded, _progressTotal;
|
||||
|
||||
void resetStream();
|
||||
#ifdef USE_LIBCURL
|
||||
static struct curl_slist *requestHeadersToSlist(const RequestHeaders *headersList);
|
||||
void initCurl(const char *url, RequestHeaders *headersList);
|
||||
bool reuseCurl(const char *url, RequestHeaders *headersList);
|
||||
#endif
|
||||
#ifdef EMSCRIPTEN
|
||||
void initEmscripten(const char *url, RequestHeaders *headersList);
|
||||
#endif
|
||||
void setupBufferContents(const byte *buffer, uint32 bufferSize, bool uploading, bool usingPatch, bool post);
|
||||
void setupFormMultipart(const Common::HashMap<Common::String, Common::String> &formFields, const Common::HashMap<Common::String, Common::Path> &formFiles);
|
||||
|
||||
@@ -73,10 +93,19 @@ class NetworkReadStream : public Common::ReadStream {
|
||||
*/
|
||||
uint32 addResponseHeaders(char *buffer, uint32 bufferSize);
|
||||
|
||||
#ifdef USE_LIBCURL
|
||||
static size_t curlDataCallback(char *d, size_t n, size_t l, void *p);
|
||||
static size_t curlReadDataCallback(char *d, size_t n, size_t l, void *p);
|
||||
static size_t curlHeadersCallback(char *d, size_t n, size_t l, void *p);
|
||||
static int curlProgressCallbackOlder(void *p, double dltotal, double dlnow, double ultotal, double ulnow);
|
||||
#endif
|
||||
#ifdef EMSCRIPTEN
|
||||
static void emscriptenOnSuccess(emscripten_fetch_t *fetch);
|
||||
static void emscriptenOnError(emscripten_fetch_t *fetch);
|
||||
static void emscriptenOnProgress(emscripten_fetch_t *fetch);
|
||||
static void emscriptenOnReadyStateChange(emscripten_fetch_t *fetch);
|
||||
void emscriptenDownloadFinished(bool success);
|
||||
#endif
|
||||
public:
|
||||
/** Send <postFields>, using POST by default. */
|
||||
NetworkReadStream(const char *url, RequestHeaders *headersList, const Common::String &postFields, bool uploading = false, bool usingPatch = false, bool keepAlive = false, long keepAliveIdle = 120, long keepAliveInterval = 60);
|
||||
|
||||
@@ -71,6 +71,35 @@ EM_JS(void, downloadFile, (const char *filenamePtr, char *dataPtr, int dataSize)
|
||||
}, 0);
|
||||
});
|
||||
|
||||
#ifdef USE_CLOUD
|
||||
/* Listener to feed the activation JSON from the wizard at cloud.scummvm.org back
|
||||
* Usage: Run the following on the final page of the activation flow:
|
||||
* window.opener.postMessage(document.getElementById("json").value,"*")
|
||||
*/
|
||||
EM_JS(bool, cloud_connection_open_oauth_window, (char const *url), {
|
||||
oauth_window = window.open(UTF8ToString(url));
|
||||
window.addEventListener("message", (event) => {
|
||||
Module._cloud_connection_json_callback(stringToNewUTF8( JSON.stringify(event.data)));
|
||||
oauth_window.close()
|
||||
}, {once : true});
|
||||
return true;
|
||||
});
|
||||
#endif
|
||||
|
||||
extern "C" {
|
||||
#ifdef USE_CLOUD
|
||||
void EMSCRIPTEN_KEEPALIVE cloud_connection_json_callback(char *str) {
|
||||
warning("cloud_connection_callback: %s", str);
|
||||
OSystem_Emscripten *emscripten_g_system = dynamic_cast<OSystem_Emscripten *>(g_system);
|
||||
if (emscripten_g_system->_cloudConnectionCallback) {
|
||||
(*emscripten_g_system->_cloudConnectionCallback)(new Common::String(str));
|
||||
} else {
|
||||
warning("No Storage Connection Callback Registered!");
|
||||
}
|
||||
}
|
||||
#endif
|
||||
}
|
||||
|
||||
// Overridden functions
|
||||
|
||||
void OSystem_Emscripten::initBackend() {
|
||||
@@ -219,4 +248,14 @@ void OSystem_Emscripten::delayMillis(uint msecs) {
|
||||
} while (pause > 0);
|
||||
lastThreshold = threshold;
|
||||
}
|
||||
|
||||
#ifdef USE_CLOUD
|
||||
bool OSystem_Emscripten::openUrl(const Common::String &url) {
|
||||
if(url.hasPrefix("https://cloud.scummvm.org/")){
|
||||
return cloud_connection_open_oauth_window(url.c_str());
|
||||
}
|
||||
return OSystem_SDL::openUrl(url);
|
||||
}
|
||||
#endif
|
||||
|
||||
#endif
|
||||
|
||||
@@ -23,8 +23,24 @@
|
||||
#define PLATFORM_SDL_EMSCRIPTEN_H
|
||||
|
||||
#include "backends/platform/sdl/posix/posix.h"
|
||||
#ifdef USE_CLOUD
|
||||
#include "backends/networking/http/request.h"
|
||||
#include "common/ustr.h"
|
||||
typedef Common::BaseCallback<const Common::String *> *CloudConnectionCallback;
|
||||
#endif
|
||||
|
||||
extern "C" {
|
||||
void cloud_connection_json_callback(char *str); // pass cloud storage activation data from JS to setup wizard
|
||||
}
|
||||
class OSystem_Emscripten : public OSystem_POSIX {
|
||||
#ifdef USE_CLOUD
|
||||
friend void ::cloud_connection_json_callback(char *str);
|
||||
#endif
|
||||
protected:
|
||||
#ifdef USE_CLOUD
|
||||
CloudConnectionCallback _cloudConnectionCallback;
|
||||
#endif
|
||||
|
||||
public:
|
||||
void initBackend() override;
|
||||
bool hasFeature(Feature f) override;
|
||||
@@ -42,6 +58,11 @@ public:
|
||||
void init() override;
|
||||
void addSysArchivesToSearchSet(Common::SearchSet &s, int priority) override;
|
||||
|
||||
#ifdef USE_CLOUD
|
||||
void setCloudConnectionCallback(CloudConnectionCallback cb) { _cloudConnectionCallback = cb; }
|
||||
bool openUrl(const Common::String &url) override;
|
||||
#endif // USE_CLOUD
|
||||
|
||||
protected:
|
||||
Common::Path getDefaultConfigFileName() override;
|
||||
Common::Path getDefaultLogFileName() override;
|
||||
|
||||
+4
-1
@@ -198,7 +198,7 @@ const char gScummVMFeatures[] = ""
|
||||
|
||||
#ifdef USE_CLOUD
|
||||
"cloud ("
|
||||
# ifdef USE_LIBCURL
|
||||
# if defined(USE_LIBCURL) || defined(EMSCRIPTEN)
|
||||
"servers"
|
||||
# ifdef USE_SDL_NET
|
||||
", local) "
|
||||
@@ -210,6 +210,9 @@ const char gScummVMFeatures[] = ""
|
||||
# ifdef USE_LIBCURL
|
||||
"libcurl "
|
||||
# endif
|
||||
# ifdef EMSCRIPTEN
|
||||
"emscripten_fetch "
|
||||
# endif
|
||||
# ifdef USE_SDL_NET
|
||||
"SDL_net "
|
||||
# endif
|
||||
|
||||
@@ -3646,12 +3646,15 @@ if test -n "$_host"; then
|
||||
;;
|
||||
wasm*-emscripten)
|
||||
_backend="emscripten"
|
||||
# Disable cloud and SDL_Net as this is handled in the browser
|
||||
_cloud=no
|
||||
_sdlnet=no
|
||||
_libcurl=no
|
||||
_curl=no
|
||||
_enet=no
|
||||
_enet=no # Disable ENet (no sockets in the browser)
|
||||
_sdlnet=no # Disable SDL_Net (no sockets in the browser)
|
||||
_libcurl=no # Emscripten backend uses emscripten_fetch not libcurl
|
||||
_curl=no # Disable cloud unless it's manually enabled
|
||||
if test "$_cloud" != yes; then
|
||||
_cloud=no
|
||||
else
|
||||
append_var LDFLAGS "-sFETCH"
|
||||
fi
|
||||
_seq_midi=no
|
||||
_timidity=no
|
||||
_sndio=no
|
||||
@@ -6097,6 +6100,10 @@ else
|
||||
_cloud=yes
|
||||
echo_n "servers"
|
||||
fi
|
||||
if test "$_host_os" = "emscripten"; then
|
||||
_cloud=yes
|
||||
echo_n "servers"
|
||||
fi
|
||||
if test "$_cloud" = "no"; then
|
||||
echo_n "no"
|
||||
fi
|
||||
|
||||
@@ -79,9 +79,11 @@ ScummVM relies heavily on Asyncify (see note above), and this comes with a quite
|
||||
* Persist last game and last plugin for offline use
|
||||
* Pre-load assets asynchronously (not blocking) - i.e. rest of the data of a game which has been launched
|
||||
* Loading indicators (doesn't work with the current synchronous/blocking filesystem)
|
||||
* Add support for save games (and game data?) on personal cloud storage (Dropbox, Google Drive).
|
||||
|
||||
Emscripten is currently re-doing their filesystem code, which could help address some of the above issues ( emscripten-core/emscripten#15041 ).
|
||||
* Locally persisted file system for saved games and settings using the Browser IndexedDB. (using [Emscripten IDBFS](https://emscripten.org/docs/api_reference/Filesystem-API.html#filesystem-api-idbfs))
|
||||
* Cloud storage (Dropbox, Google Drive etc.) is exposed as a special folder on the file system. Only read access is implemented, but saved games can be synchronized via the regular cloud sync feature
|
||||
* Screenshots and Logfiles are automatically downloaded after creation
|
||||
* All other data is stored in memory and removed on reload (incl. temporarily stored logfiles and screenshots)
|
||||
|
||||
### UI Integration
|
||||
* Build a nice webpage around the canvas.
|
||||
|
||||
@@ -22,8 +22,8 @@
|
||||
#ifndef SCUMM_HE_NET_LOBBY_H
|
||||
#define SCUMM_HE_NET_LOBBY_H
|
||||
|
||||
#include "backends/networking/http/socket.h"
|
||||
#include "backends/networking/http/url.h"
|
||||
#include "backends/networking/http/curl/socket.h"
|
||||
#include "backends/networking/http/curl/url.h"
|
||||
#include "common/formats/json.h"
|
||||
|
||||
#include "scumm/he/net/net_main.h"
|
||||
|
||||
@@ -27,6 +27,9 @@
|
||||
#include "backends/networking/sdl_net/localwebserver.h"
|
||||
#endif // USE_SDL_NET
|
||||
|
||||
#ifdef EMSCRIPTEN
|
||||
#include "backends/platform/sdl/emscripten/emscripten.h"
|
||||
#endif
|
||||
#include "common/formats/json.h"
|
||||
#include "common/file.h"
|
||||
#include "common/memstream.h"
|
||||
@@ -94,6 +97,17 @@ CloudConnectionWizard::~CloudConnectionWizard() {
|
||||
delete _callback;
|
||||
}
|
||||
|
||||
#ifdef EMSCRIPTEN
|
||||
// allow browser to pass in JSON tokens
|
||||
void CloudConnectionWizard::emscriptenCloudConnectionCallback(const Common::String *message ){
|
||||
showStep(Step::MANUAL_MODE_STEP_2);
|
||||
if (!message->empty()) {
|
||||
_codeBox->setEditString(*message);
|
||||
}
|
||||
manualModeConnect();
|
||||
}
|
||||
#endif
|
||||
|
||||
void CloudConnectionWizard::showStep(Step newStep) {
|
||||
if (newStep == _currentStep)
|
||||
return;
|
||||
@@ -318,6 +332,10 @@ void CloudConnectionWizard::hideStepQuickModeSuccess() {
|
||||
// manual mode
|
||||
|
||||
void CloudConnectionWizard::showStepManualMode1() {
|
||||
#ifdef EMSCRIPTEN
|
||||
OSystem_Emscripten *emscripten_g_system = dynamic_cast<OSystem_Emscripten*>(g_system);
|
||||
emscripten_g_system->setCloudConnectionCallback(new Common::Callback<CloudConnectionWizard, const Common::String *>(this, &CloudConnectionWizard::emscriptenCloudConnectionCallback));
|
||||
#endif
|
||||
_headlineLabel->setLabel(_("Manual Mode: Step 1"));
|
||||
showContainer("ConnectionWizard_ManualModeStep1");
|
||||
#ifdef USE_SDL_NET
|
||||
@@ -364,12 +382,19 @@ void CloudConnectionWizard::showStepManualMode2() {
|
||||
|
||||
_label0 = new StaticTextWidget(_container, "ConnectionWizard_ManualModeStep2.Line1", _("Copy the JSON code from the browser here and press Next:"));
|
||||
_codeBox = new EditTextWidget(_container, "ConnectionWizard_ManualModeStep2.CodeBox", Common::U32String(), Common::U32String(), 0, 0, ThemeEngine::kFontStyleConsole);
|
||||
#ifndef EMSCRIPTEN
|
||||
_button0 = new ButtonWidget(_container, "ConnectionWizard_ManualModeStep2.PasteButton", _("Paste"), _("Paste code from clipboard"), kCloudConnectionWizardPasteCodeCmd);
|
||||
_button1 = new ButtonWidget(_container, "ConnectionWizard_ManualModeStep2.LoadButton", _("Load"), _("Load code from file"), kCloudConnectionWizardLoadCodeCmd);
|
||||
#endif
|
||||
_label1 = new StaticTextWidget(_container, "ConnectionWizard_ManualModeStep2.Line2", Common::U32String());
|
||||
|
||||
}
|
||||
|
||||
void CloudConnectionWizard::hideStepManualMode2() {
|
||||
#ifdef EMSCRIPTEN
|
||||
OSystem_Emscripten *emscripten_g_system = dynamic_cast<OSystem_Emscripten*>(g_system);
|
||||
emscripten_g_system->setCloudConnectionCallback(nullptr);
|
||||
#endif
|
||||
hideContainer();
|
||||
_closeButton->setEnabled(true);
|
||||
hideBackButton();
|
||||
|
||||
@@ -124,6 +124,10 @@ class CloudConnectionWizard : public Dialog {
|
||||
void manualModeConnect();
|
||||
void manualModeStorageConnectionCallback(const Networking::ErrorResponse &response);
|
||||
|
||||
#ifdef EMSCRIPTEN
|
||||
void emscriptenCloudConnectionCallback(const Common::String *message);
|
||||
#endif
|
||||
|
||||
public:
|
||||
CloudConnectionWizard();
|
||||
~CloudConnectionWizard() override;
|
||||
|
||||
Reference in New Issue
Block a user