Use file mapping

This commit is contained in:
Varun Patil 2020-05-07 12:27:51 +05:30
parent 80df1ee771
commit fd6375e17d
8 changed files with 58 additions and 32 deletions

View file

@ -263,7 +263,7 @@ struct ALStreamOpenHandler : FileSystem::OpenHandler
void ALStream::openSource(const std::string &filename)
{
#ifdef __EMSCRIPTEN__
load_file_async(filename.c_str());
load_file_async_js(filename.c_str());
#endif
ALStreamOpenHandler handler(srcOps, looped);

View file

@ -248,7 +248,6 @@ struct BitmapOpenHandler : FileSystem::OpenHandler
bool tryRead(SDL_RWops &ops, const char *ext, const char * fullPath)
{
#ifdef __EMSCRIPTEN__
load_file_async_js(fullPath);
surf = IMG_Load(fullPath);
#else
surf = IMG_LoadTyped_RW(&ops, 1, ext);
@ -259,6 +258,10 @@ struct BitmapOpenHandler : FileSystem::OpenHandler
Bitmap::Bitmap(const char *filename)
{
#ifdef __EMSCRIPTEN__
load_file_async_js(filename);
#endif
BitmapOpenHandler handler;
shState->fileSystem().openRead(handler, filename);
SDL_Surface *imgSurf = handler.surf;

View file

@ -1,6 +1,4 @@
#include "emscripten.hpp"
#include "filesystem.h"
#include "sharedstate.h"
#ifdef __EMSCRIPTEN__
@ -18,15 +16,22 @@ EM_JS(void, load_file_async_js, (const char* fullPathC), {
// Show spinner
if (window.setBusy) window.setBusy();
// Get full destination
const file = "game/" + fullPath;
// Get mapping key
const mappingKey = fullPath.toLowerCase().replace(new RegExp("\\\\.[^/.]+$"), "");
const mappingValue = mapping[mappingKey];
// Get path and filename
const path = "/" + file.substring(0, file.lastIndexOf("/"));
const filename = file.substring(file.lastIndexOf("/") + 1);
// Check if this is a folder
if (!mappingValue || mappingValue.endsWith("h=")) {
console.error("Skipping loading", fullPath, mappingValue);
return wakeUp();
}
// Get target URL
const iurl = "gameasync/" + fullPath;
const iurl = "gameasync/" + mappingValue;
// Get path and filename
const path = "/game/" + mappingValue.substring(0, mappingValue.lastIndexOf("/"));
const filename = mappingValue.substring(mappingValue.lastIndexOf("/") + 1).split("?")[0];
// Delete original file if existent
try {
@ -43,23 +48,6 @@ EM_JS(void, load_file_async_js, (const char* fullPathC), {
});
});
struct LoadOpenHandler : FileSystem::OpenHandler
{
LoadOpenHandler()
{}
bool tryRead(SDL_RWops &ops, const char *ext, const char *fullPath)
{
load_file_async_js(fullPath);
return true;
}
};
void load_file_async(const char * filename) {
LoadOpenHandler handler;
shState->fileSystem().openRead(handler, filename);
}
EM_JS(void, save_file_async_js, (const char* fullPathC), {
if (window.saveFile) window.saveFile(UTF8ToString(fullPathC));
});

View file

@ -7,8 +7,6 @@
extern "C" {
void load_file_async_js(const char* fullPathC);
void load_file_async(const char* filename);
void save_file_async_js(const char* fullPathC);
}

View file

@ -250,7 +250,7 @@ SoundBuffer *SoundEmitter::allocateBuffer(const std::string &filename)
{
/* Buffer not in cache, needs to be loaded */
#ifdef __EMSCRIPTEN__
load_file_async(filename.c_str());
load_file_async_js(filename.c_str());
#endif
SoundOpenHandler handler;