FileDownloader now works in Emscripten
This commit is contained in:
parent
dc834f84f8
commit
f972fd5024
5 changed files with 79 additions and 27 deletions
|
|
@ -106,6 +106,7 @@ set(sources "src/main.cpp"
|
|||
"include/Helpers/StringHelpers.cpp"
|
||||
"include/Helpers/Serializers.cpp"
|
||||
"include/Helpers/FileDownloader.cpp"
|
||||
"include/Helpers/FileDownloaderEmscripten.cpp"
|
||||
"include/Helpers/VersionNumber.cpp"
|
||||
)
|
||||
|
||||
|
|
@ -197,7 +198,7 @@ if(EMSCRIPTEN)
|
|||
endif()
|
||||
|
||||
target_compile_options(infinipaint PRIVATE -Wall -fdiagnostics-color -sUSE_SDL=0 -include functional -fexceptions)
|
||||
target_link_options(infinipaint PRIVATE --emrun -sUSE_SDL=0 -sFULL_ES2 -sUSE_WEBGL2=1 --embed-file data -sALLOW_MEMORY_GROWTH -sEXPORTED_RUNTIME_METHODS=ccall -sEXPORTED_FUNCTIONS=[_main,_malloc,_free] -sMAXIMUM_MEMORY=4GB -sSINGLE_FILE -lidbfs.js -fexceptions)
|
||||
target_link_options(infinipaint PRIVATE --emrun -sUSE_SDL=0 -sFULL_ES2 -sUSE_WEBGL2=1 -sFETCH --embed-file data -sALLOW_MEMORY_GROWTH -sEXPORTED_RUNTIME_METHODS=ccall -sEXPORTED_FUNCTIONS=[_main,_malloc,_free] -sMAXIMUM_MEMORY=4GB -sSINGLE_FILE -lidbfs.js -fexceptions)
|
||||
if(EMSCRIPTEN_SINGLE_HTML_FILE)
|
||||
target_link_options(infinipaint PRIVATE --shell-file emscripteninstall/singlefilereleaseshell.html)
|
||||
set(CMAKE_EXECUTABLE_SUFFIX ".html")
|
||||
|
|
|
|||
|
|
@ -1,6 +1,7 @@
|
|||
#include "FileDownloader.hpp"
|
||||
#ifndef __EMSCRIPTEN__
|
||||
|
||||
#include "FileDownloader.hpp"
|
||||
|
||||
#include <curl/multi.h>
|
||||
#include <iostream>
|
||||
#include <curl/curl.h>
|
||||
|
|
|
|||
|
|
@ -1,12 +1,15 @@
|
|||
#pragma once
|
||||
#ifndef __EMSCRIPTEN__
|
||||
|
||||
#include <curl/curl.h>
|
||||
#include <memory>
|
||||
#include <functional>
|
||||
#include <atomic>
|
||||
#include <thread>
|
||||
#include <mutex>
|
||||
|
||||
#ifndef __EMSCRIPTEN__
|
||||
#include <mutex>
|
||||
#include <thread>
|
||||
#include <curl/curl.h>
|
||||
#else
|
||||
#include <emscripten/fetch.h>
|
||||
#endif
|
||||
|
||||
class FileDownloader {
|
||||
public:
|
||||
|
|
@ -24,28 +27,30 @@ class FileDownloader {
|
|||
static std::shared_ptr<DownloadData> download_data_from_url(std::string_view url);
|
||||
static void cleanup();
|
||||
private:
|
||||
static void add_new_handlers_to_multi();
|
||||
static void download_thread_update();
|
||||
|
||||
struct DownloadHandler {
|
||||
CURL* eHandle;
|
||||
#ifndef __EMSCRIPTEN__
|
||||
CURL* eHandle;
|
||||
#endif
|
||||
std::shared_ptr<DownloadData> data;
|
||||
};
|
||||
|
||||
static std::mutex newDownloadsMutex;
|
||||
static std::vector<DownloadHandler> newDownloads;
|
||||
static std::atomic<bool> shutdownThread;
|
||||
static CURLM* mHandle;
|
||||
static std::vector<DownloadHandler> currentDownloads;
|
||||
static std::unique_ptr<std::thread> downloadThread;
|
||||
|
||||
static void save_to_string(CURL* curl, std::string* str);
|
||||
static void disable_ssl_verification(CURL* curl);
|
||||
#ifndef __EMSCRIPTEN__
|
||||
static std::mutex newDownloadsMutex;
|
||||
static std::vector<DownloadHandler> newDownloads;
|
||||
static std::atomic<bool> shutdownThread;
|
||||
static CURLM* mHandle;
|
||||
static std::unique_ptr<std::thread> downloadThread;
|
||||
|
||||
static timeval get_timeout(CURLM* mCurl);
|
||||
static int wait_if_needed(CURLM* mCurl, timeval& timeout);
|
||||
|
||||
static size_t write_to_string(void* contents, size_t size, size_t nmemb, void* userp);
|
||||
static void save_to_string(CURL* curl, std::string* str);
|
||||
static void disable_ssl_verification(CURL* curl);
|
||||
static size_t write_to_string(void* contents, size_t size, size_t nmemb, void* userp);
|
||||
static void add_new_handlers_to_multi();
|
||||
static void download_thread_update();
|
||||
#else
|
||||
static void download_succeeded(emscripten_fetch_t *fetch);
|
||||
static void download_failed(emscripten_fetch_t *fetch);
|
||||
#endif
|
||||
};
|
||||
|
||||
#endif
|
||||
|
|
|
|||
49
include/Helpers/FileDownloaderEmscripten.cpp
Normal file
49
include/Helpers/FileDownloaderEmscripten.cpp
Normal file
|
|
@ -0,0 +1,49 @@
|
|||
#ifdef __EMSCRIPTEN__
|
||||
#include "FileDownloader.hpp"
|
||||
#include <cstdio>
|
||||
|
||||
std::vector<FileDownloader::DownloadHandler> FileDownloader::currentDownloads;
|
||||
|
||||
void FileDownloader::download_succeeded(emscripten_fetch_t *fetch) {
|
||||
DownloadData* downData = static_cast<DownloadData*>(fetch->userData);
|
||||
downData->str = std::string(fetch->data, fetch->numBytes);
|
||||
downData->status = DownloadData::Status::SUCCESS;
|
||||
std::erase_if(currentDownloads, [fetch](DownloadHandler& d) {
|
||||
return (DownloadData*)fetch->userData == d.data.get();
|
||||
});
|
||||
emscripten_fetch_close(fetch);
|
||||
}
|
||||
|
||||
void FileDownloader::download_failed(emscripten_fetch_t *fetch) {
|
||||
DownloadData* downData = static_cast<DownloadData*>(fetch->userData);
|
||||
downData->status = DownloadData::Status::FAILURE;
|
||||
std::erase_if(currentDownloads, [fetch](DownloadHandler& d) {
|
||||
return (DownloadData*)fetch->userData == d.data.get();
|
||||
});
|
||||
emscripten_fetch_close(fetch);
|
||||
}
|
||||
|
||||
void FileDownloader::init() {
|
||||
}
|
||||
|
||||
std::shared_ptr<FileDownloader::DownloadData> FileDownloader::download_data_from_url(std::string_view url) {
|
||||
DownloadHandler& newDownload = currentDownloads.emplace_back();
|
||||
newDownload.data = std::make_shared<DownloadData>();
|
||||
newDownload.data->status = DownloadData::Status::IN_PROGRESS;
|
||||
|
||||
emscripten_fetch_attr_t attr;
|
||||
emscripten_fetch_attr_init(&attr);
|
||||
std::strcpy(attr.requestMethod, "GET");
|
||||
attr.attributes = EMSCRIPTEN_FETCH_LOAD_TO_MEMORY;
|
||||
attr.onsuccess = download_succeeded;
|
||||
attr.onerror = download_failed;
|
||||
attr.userData = static_cast<void*>(newDownload.data.get());
|
||||
emscripten_fetch(&attr, url.data());
|
||||
|
||||
return newDownload.data;
|
||||
}
|
||||
|
||||
void FileDownloader::cleanup() {
|
||||
}
|
||||
|
||||
#endif
|
||||
|
|
@ -391,9 +391,7 @@ SDL_AppResult SDL_AppInit(void **appstate, int argc, char **argv) {
|
|||
|
||||
init_logs(mS);
|
||||
|
||||
#ifndef __EMSCRIPTEN__
|
||||
FileDownloader::init();
|
||||
#endif
|
||||
|
||||
#ifdef NDEBUG
|
||||
try {
|
||||
|
|
@ -779,9 +777,7 @@ void SDL_AppQuit(void *appstate, SDL_AppResult result) {
|
|||
|
||||
delete (&mS);
|
||||
|
||||
#ifndef __EMSCRIPTEN__
|
||||
FileDownloader::cleanup();
|
||||
#endif
|
||||
|
||||
SDL_Quit();
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue