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

Addons available for download in WASM builds #1891

Merged
merged 4 commits into from
Nov 14, 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 CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -925,7 +925,7 @@ target_link_libraries(supertux2 supertux2_lib Boost::filesystem Boost::locale)
set_target_properties(supertux2_lib PROPERTIES OUTPUT_NAME supertux2_lib)
set_target_properties(supertux2_lib PROPERTIES COMPILE_FLAGS "${SUPERTUX2_EXTRA_WARNING_FLAGS}")
if(EMSCRIPTEN)
target_link_options(supertux2 PUBLIC -sEXPORTED_FUNCTIONS=['_main','_set_resolution','_save_config'] PUBLIC -sEXPORTED_RUNTIME_METHODS=['ccall','cwrap'] PUBLIC -sEXTRA_EXPORTED_RUNTIME_METHODS=['ccall','cwrap'])
target_link_options(supertux2 PUBLIC -sEXPORTED_FUNCTIONS=['_main','_set_resolution','_save_config','_onDownloadProgress','_onDownloadFinished','_onDownloadError','_onDownloadAborted'] PUBLIC -sEXPORTED_RUNTIME_METHODS=['ccall','cwrap'] PUBLIC -sEXTRA_EXPORTED_RUNTIME_METHODS=['ccall','cwrap'])
endif(EMSCRIPTEN)

if(WIN32 AND NOT VCPKG_BUILD)
Expand Down
37 changes: 37 additions & 0 deletions mk/emscripten/template.html.in
Original file line number Diff line number Diff line change
Expand Up @@ -506,6 +506,43 @@
});
}

var onDownloadProgress, onDownloadFinished, onDownloadError, onDownloadAborted;
window.supertux_xhr_download = function(id, url, file) {
console.log('id: ' + id);
console.log('url: ' + url);
console.log('file: ' + file);

if (!onDownloadProgress)
onDownloadProgress = Module.cwrap('onDownloadProgress', 'void', ['number', 'number', 'number']);

if (!onDownloadFinished)
onDownloadFinished = Module.cwrap('onDownloadFinished', 'void', ['number']);

if (!onDownloadError)
onDownloadError = Module.cwrap('onDownloadError', 'void', ['number']);

if (!onDownloadAborted)
onDownloadAborted = Module.cwrap('onDownloadAborted', 'void', ['number']);

var xhr = new XMLHttpRequest();
xhr.onreadystatechange = function() {
if (xhr.readyState == 4) {
var data = new Uint8Array(xhr.response);
var stream = FS.open(file, 'w+');
FS.write(stream, data, 0, data.length, 0);
FS.close(stream);
onDownloadFinished(id);
}
};
xhr.responseType = "arraybuffer";
xhr.open("GET", url);
xhr.send();

xhr.addEventListener('progress', (e) => { onDownloadProgress(id, e.loaded, e.total); });
xhr.addEventListener('error', (e) => { onDownloadError(id); });
xhr.addEventListener('abort', (e) => { onDownloadAborted(id); })
}

window.addEventListener('resize', (e) => {
tryResize();
});
Expand Down
4 changes: 0 additions & 4 deletions src/addon/addon.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,6 @@
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.

#ifndef __EMSCRIPTEN__

#include "addon/addon.hpp"

#include <sstream>
Expand Down Expand Up @@ -180,6 +178,4 @@ Addon::set_enabled(bool v)
m_enabled = v;
}

#endif

/* EOF */
4 changes: 0 additions & 4 deletions src/addon/addon.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,6 @@
#ifndef HEADER_SUPERTUX_ADDON_ADDON_HPP
#define HEADER_SUPERTUX_ADDON_ADDON_HPP

#ifndef __EMSCRIPTEN__

#include <memory>
#include <string>

Expand Down Expand Up @@ -88,6 +86,4 @@ class Addon final

#endif

#endif

/* EOF */
26 changes: 24 additions & 2 deletions src/addon/addon_manager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,6 @@
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.

#ifndef __EMSCRIPTEN__

#include "addon/addon_manager.hpp"

#include <physfs.h>
Expand Down Expand Up @@ -787,6 +785,30 @@ AddonManager::check_for_langpack_updates()
}
}

#ifdef EMSCRIPTEN
void
AddonManager::onDownloadProgress(int id, int loaded, int total)
{
m_downloader.onDownloadProgress(id, loaded, total);
}

void
AddonManager::onDownloadFinished(int id)
{
m_downloader.onDownloadFinished(id);
}

void
AddonManager::onDownloadError(int id)
{
m_downloader.onDownloadError(id);
}

void
AddonManager::onDownloadAborted(int id)
{
m_downloader.onDownloadAborted(id);
}
#endif

/* EOF */
11 changes: 7 additions & 4 deletions src/addon/addon_manager.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,6 @@
#ifndef HEADER_SUPERTUX_ADDON_ADDON_MANAGER_HPP
#define HEADER_SUPERTUX_ADDON_ADDON_MANAGER_HPP

#ifndef __EMSCRIPTEN__

#include <memory>
#include <string>
#include <vector>
Expand Down Expand Up @@ -86,6 +84,13 @@ class AddonManager final : public Currenton<AddonManager>
void update();
void check_for_langpack_updates();

#ifdef EMSCRIPTEN
void onDownloadProgress(int id, int loaded, int total);
void onDownloadFinished(int id);
void onDownloadError(int id);
void onDownloadAborted(int id);
#endif

private:
std::vector<std::string> scan_for_archives() const;
void add_installed_addons();
Expand All @@ -106,6 +111,4 @@ class AddonManager final : public Currenton<AddonManager>

#endif

#endif

/* EOF */
Loading