From a801bf6fd54196bc85947b2a4441334d8d14f300 Mon Sep 17 00:00:00 2001 From: UnknownShadow200 Date: Fri, 9 Jul 2021 07:33:25 +1000 Subject: [PATCH] Try to fix rare case where Module.saveNode would still throw an error and hence abort the game (Thanks Swoozy) --- src/interop_web.js | 31 +++++++++++++++++++------------ 1 file changed, 19 insertions(+), 12 deletions(-) diff --git a/src/interop_web.js b/src/interop_web.js index f80f6cbc8..99be07c3e 100644 --- a/src/interop_web.js +++ b/src/interop_web.js @@ -132,7 +132,7 @@ mergeInto(LibraryManager.library, { //######################################################################################################################## interop_InitFilesystem: function(buffer) { // if interop_SaveNode is directly defined as a function, it is wrongly optimised - // out when compilingas the function is not directly referenced by any C code + // out when compiling as the function is not directly referenced by any C code Module.saveNode = function(path) { var callback = function(err) { if (!err) return; @@ -147,23 +147,30 @@ mergeInto(LibraryManager.library, { path = lookup.path; node = lookup.node; stat = node.node_ops.getattr(node); + + if (FS.isDir(stat.mode)) { + entry = { timestamp: stat.mtime, mode: stat.mode }; + } else { + // Performance consideration: storing a normal JavaScript array to a IndexedDB is much slower than storing a typed array. + // Therefore always convert the file contents to a typed array first before writing the data to IndexedDB. + node.contents = MEMFS.getFileDataAsTypedArray(node); + entry = { timestamp: stat.mtime, mode: stat.mode, contents: node.contents }; + } } catch (err) { return callback(err); } - if (FS.isDir(stat.mode)) { - entry = { timestamp: stat.mtime, mode: stat.mode }; - } else { - // Performance consideration: storing a normal JavaScript array to a IndexedDB is much slower than storing a typed array. - // Therefore always convert the file contents to a typed array first before writing the data to IndexedDB. - node.contents = MEMFS.getFileDataAsTypedArray(node); - entry = { timestamp: stat.mtime, mode: stat.mode, contents: node.contents }; - } - IDBFS.getDB('/classicube', function(err, db) { if (err) return callback(err); - var transaction = db.transaction([IDBFS.DB_STORE_NAME], 'readwrite'); - var store = transaction.objectStore(IDBFS.DB_STORE_NAME); + var transaction, store; + + // can still throw errors here + try { + transaction = db.transaction([IDBFS.DB_STORE_NAME], 'readwrite'); + store = transaction.objectStore(IDBFS.DB_STORE_NAME); + } catch (err) { + return callback(err); + } transaction.onerror = function(e) { callback(this.error);