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

@ -455,7 +455,7 @@ SET(ASYNCIFY "-s ASYNCIFY=1 -s 'ASYNCIFY_IMPORTS=[\"load_file_async_js\"]'")
SET(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} ${EMS_FLAGS} ${ERR_FLAGS} ${ASYNCIFY}") SET(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} ${EMS_FLAGS} ${ERR_FLAGS} ${ASYNCIFY}")
SET(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} ${EMS_FLAGS} ${ERR_FLAGS} ${ASYNCIFY}") SET(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} ${EMS_FLAGS} ${ERR_FLAGS} ${ASYNCIFY}")
SET(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} ${EMS_FLAGS} ${ASYNCIFY} -s INITIAL_MEMORY=536870912 -lopenal --preload-file game -s DISABLE_EXCEPTION_CATCHING=1 -s ASSERTIONS=0 -s SAFE_HEAP=0 -s MINIFY_HTML=0 --shell-file shell.html -s EMULATE_FUNCTION_POINTER_CASTS=0 -s ALLOW_MEMORY_GROWTH=1 -s MAX_WEBGL_VERSION=2") SET(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} ${EMS_FLAGS} ${ASYNCIFY} -s INITIAL_MEMORY=536870912 -lopenal -s DISABLE_EXCEPTION_CATCHING=1 -s ASSERTIONS=0 -s SAFE_HEAP=0 -s MINIFY_HTML=0 --shell-file shell.html -s EMULATE_FUNCTION_POINTER_CASTS=0 -s ALLOW_MEMORY_GROWTH=1 -s MAX_WEBGL_VERSION=2")
set_target_properties( set_target_properties(
${PROJECT_NAME} ${PROJECT_NAME}

17
extra/make_mapping.sh Executable file
View File

@ -0,0 +1,17 @@
#!/bin/bash
echo "var mapping = {" > mapping.js
for file in {*,*/*,*/**/*}
do
filename="${file%.*}"
fl="$(echo "$filename" | tr '[:upper:]' '[:lower:]')"
md5=`md5sum ${file} | awk '{ print $1 }'`
echo "\"$fl\": \"${file}?h=${md5}\"," >> mapping.js
done
echo "};" >> mapping.js

View File

@ -4,6 +4,7 @@
<meta charset="utf-8"> <meta charset="utf-8">
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"> <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<script src="js/localforage.min.js"></script> <script src="js/localforage.min.js"></script>
<script src="gameasync/mapping.js"></script>
<title>MKXP</title> <title>MKXP</title>
<style> <style>
@ -134,8 +135,27 @@
}); });
} }
var createDummies = function() {
// Base directory
FS.mkdir('/game');
// Create dummy objects
Object.values(mapping).forEach((file) => {
// Get filename
const filename = '/game/' + file.split("?")[0];
// Check if folder
if (file.endsWith('h=')) {
return FS.mkdir(filename);
}
// Create dummy file
FS.writeFile(filename, '1');
});
};
var Module = { var Module = {
preRun: [], preRun: [createDummies],
postRun: [loadFiles], postRun: [loadFiles],
noAudioDecoding: true, noAudioDecoding: true,
print: (function() { print: (function() {

View File

@ -263,7 +263,7 @@ struct ALStreamOpenHandler : FileSystem::OpenHandler
void ALStream::openSource(const std::string &filename) void ALStream::openSource(const std::string &filename)
{ {
#ifdef __EMSCRIPTEN__ #ifdef __EMSCRIPTEN__
load_file_async(filename.c_str()); load_file_async_js(filename.c_str());
#endif #endif
ALStreamOpenHandler handler(srcOps, looped); 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) bool tryRead(SDL_RWops &ops, const char *ext, const char * fullPath)
{ {
#ifdef __EMSCRIPTEN__ #ifdef __EMSCRIPTEN__
load_file_async_js(fullPath);
surf = IMG_Load(fullPath); surf = IMG_Load(fullPath);
#else #else
surf = IMG_LoadTyped_RW(&ops, 1, ext); surf = IMG_LoadTyped_RW(&ops, 1, ext);
@ -259,6 +258,10 @@ struct BitmapOpenHandler : FileSystem::OpenHandler
Bitmap::Bitmap(const char *filename) Bitmap::Bitmap(const char *filename)
{ {
#ifdef __EMSCRIPTEN__
load_file_async_js(filename);
#endif
BitmapOpenHandler handler; BitmapOpenHandler handler;
shState->fileSystem().openRead(handler, filename); shState->fileSystem().openRead(handler, filename);
SDL_Surface *imgSurf = handler.surf; SDL_Surface *imgSurf = handler.surf;

View File

@ -1,6 +1,4 @@
#include "emscripten.hpp" #include "emscripten.hpp"
#include "filesystem.h"
#include "sharedstate.h"
#ifdef __EMSCRIPTEN__ #ifdef __EMSCRIPTEN__
@ -18,15 +16,22 @@ EM_JS(void, load_file_async_js, (const char* fullPathC), {
// Show spinner // Show spinner
if (window.setBusy) window.setBusy(); if (window.setBusy) window.setBusy();
// Get full destination // Get mapping key
const file = "game/" + fullPath; const mappingKey = fullPath.toLowerCase().replace(new RegExp("\\\\.[^/.]+$"), "");
const mappingValue = mapping[mappingKey];
// Get path and filename // Check if this is a folder
const path = "/" + file.substring(0, file.lastIndexOf("/")); if (!mappingValue || mappingValue.endsWith("h=")) {
const filename = file.substring(file.lastIndexOf("/") + 1); console.error("Skipping loading", fullPath, mappingValue);
return wakeUp();
}
// Get target URL // 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 // Delete original file if existent
try { 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), { EM_JS(void, save_file_async_js, (const char* fullPathC), {
if (window.saveFile) window.saveFile(UTF8ToString(fullPathC)); if (window.saveFile) window.saveFile(UTF8ToString(fullPathC));
}); });

View File

@ -7,8 +7,6 @@
extern "C" { extern "C" {
void load_file_async_js(const char* fullPathC); void load_file_async_js(const char* fullPathC);
void load_file_async(const char* filename);
void save_file_async_js(const char* fullPathC); 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 */ /* Buffer not in cache, needs to be loaded */
#ifdef __EMSCRIPTEN__ #ifdef __EMSCRIPTEN__
load_file_async(filename.c_str()); load_file_async_js(filename.c_str());
#endif #endif
SoundOpenHandler handler; SoundOpenHandler handler;