mkxp/src/emscripten.cpp

58 lines
1.3 KiB
C++
Raw Normal View History

#include "emscripten.hpp"
2020-05-04 12:57:45 +00:00
#include "filesystem.h"
#include "sharedstate.h"
2020-05-04 12:57:45 +00:00
EM_JS(void, load_file_async_js, (const char* fullPathC), {
Asyncify.handleSleep(function(wakeUp) {
2020-05-04 17:08:59 +00:00
// Get argument
const fullPath = UTF8ToString(fullPathC);
// Make cache object
if (!window.fileAsyncCache) window.fileAsyncCache = {};
// Check if already loaded
if (window.fileAsyncCache.hasOwnProperty(fullPath)) return wakeUp();
2020-05-04 17:08:59 +00:00
// Show spinner
if (window.setBusy) window.setBusy();
// Get full destination
const file = "game/" + fullPath;
// Get path and filename
const path = "/" + file.substring(0, file.lastIndexOf("/"));
const filename = file.substring(file.lastIndexOf("/") + 1);
// Get target URL
const iurl = "gameasync/" + fullPath;
// Delete original file
FS.unlink(path + "/" + filename);
// Get the new file
FS.createPreloadedFile(path, filename, iurl, true, true, function() {
window.fileAsyncCache[fullPath] = 1;
2020-05-04 17:08:59 +00:00
if (window.setNotBusy) window.setNotBusy();
wakeUp();
}, console.error);
});
});
2020-05-04 12:57:45 +00:00
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);
}