diff --git a/extra/js/drive.js b/extra/js/drive.js index 54391c3..ed8748f 100644 --- a/extra/js/drive.js +++ b/extra/js/drive.js @@ -18,3 +18,46 @@ function _base64ToBytes(base64) { } return bytes; } + +window.loadFileAsync = function(fullPath, callback) { + // Make cache object + if (!window.fileAsyncCache) window.fileAsyncCache = {}; + + // Check if already loaded + if (window.fileAsyncCache.hasOwnProperty(fullPath)) return callback(); + + // Show spinner + if (window.setBusy) window.setBusy(); + + // Get mapping key + const mappingKey = fullPath.toLowerCase().replace(new RegExp("\\.[^/.]+$"), ""); + const mappingValue = mapping[mappingKey]; + + // Check if this is a folder + if (!mappingValue || mappingValue.endsWith("h=")) { + console.error("Skipping loading", fullPath, mappingValue); + return callback(); + } + + // Get target URL + 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 { + FS.unlink(path + "/" + filename); + } catch (err) {} + + // Get the new file + getLazyAsset(iurl, filename, () => { + FS.createPreloadedFile(path, filename, iurl, true, true, function() { + window.fileAsyncCache[fullPath] = 1; + if (window.setNotBusy) window.setNotBusy(); + if (window.fileLoadedAsync) window.fileLoadedAsync(fullPath); + callback(); + }, console.error); + }); +} diff --git a/src/emscripten.cpp b/src/emscripten.cpp index 5a8dd8e..9fa1317 100644 --- a/src/emscripten.cpp +++ b/src/emscripten.cpp @@ -4,49 +4,7 @@ EM_JS(void, load_file_async_js, (const char* fullPathC), { Asyncify.handleSleep(function(wakeUp) { - // 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(); - - // Show spinner - if (window.setBusy) window.setBusy(); - - // Get mapping key - const mappingKey = fullPath.toLowerCase().replace(new RegExp("\\\\.[^/.]+$"), ""); - const mappingValue = mapping[mappingKey]; - - // 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/" + 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 { - FS.unlink(path + "/" + filename); - } catch (err) {} - - // Get the new file - getLazyAsset(iurl, filename, () => { - FS.createPreloadedFile(path, filename, iurl, true, true, function() { - window.fileAsyncCache[fullPath] = 1; - if (window.setNotBusy) window.setNotBusy(); - if (window.fileLoadedAsync) window.fileLoadedAsync(fullPath); - wakeUp(); - }, console.error); - }); + window.loadFileAsync(UTF8ToString(fullPathC), wakeUp); }); });