From 7bf24f4016197ea03dd63abae7e64c45fb862a0f Mon Sep 17 00:00:00 2001 From: UnknownShadow200 Date: Mon, 7 Jun 2021 21:08:07 +1000 Subject: [PATCH] add function for saving node to IndexedDB directly (doesn't work properly yet) --- src/Platform_Web.c | 10 ++++------ src/interop_web.js | 47 ++++++++++++++++++++++++++++++++++++++++++---- 2 files changed, 47 insertions(+), 10 deletions(-) diff --git a/src/Platform_Web.c b/src/Platform_Web.c index 598c88861..8bc0c8ca4 100644 --- a/src/Platform_Web.c +++ b/src/Platform_Web.c @@ -189,12 +189,9 @@ cc_result File_Write(cc_file file, const void* data, cc_uint32 count, cc_uint32* } extern int interop_FileClose(int fd); -extern void interop_SyncFS(void); cc_result File_Close(cc_file file) { /* returned result is negative for error */ - int res = -interop_FileClose(file); - interop_SyncFS(); - return res; + return -interop_FileClose(file); } extern int interop_FileSeek(int fd, int offset, int whence); @@ -258,6 +255,9 @@ void Platform_LoadSysFonts(void) { } /*########################################################################################################################* *---------------------------------------------------------Socket----------------------------------------------------------* *#########################################################################################################################*/ +extern void interop_InitSockets(void); +int Socket_ValidAddress(const cc_string* address) { return true; } + extern int interop_SocketGetPending(int sock); cc_result Socket_Available(cc_socket s, int* available) { int res = interop_SocketGetPending(s); @@ -281,7 +281,6 @@ cc_result Socket_GetError(cc_socket s, cc_result* result) { *result = 0; return -res; } } -int Socket_ValidAddress(const cc_string* address) { return true; } extern int interop_SocketCreate(void); extern int interop_SocketConnect(int sock, const char* addr, int port); @@ -428,7 +427,6 @@ EMSCRIPTEN_KEEPALIVE void Platform_LogError(const char* msg) { } extern void interop_InitModule(void); -extern void interop_InitSockets(void); extern void interop_GetIndexedDBError(char* buffer); void Platform_Init(void) { diff --git a/src/interop_web.js b/src/interop_web.js index 63c8e845b..8f5ef9655 100644 --- a/src/interop_web.js +++ b/src/interop_web.js @@ -129,13 +129,49 @@ mergeInto(LibraryManager.library, { interop_GetIndexedDBError: function(buffer) { if (window.cc_idbErr) stringToUTF8(window.cc_idbErr, buffer, 64); }, - interop_SyncFS: function() { - FS.syncfs(false, function(err) { + interop_SaveNode: function (path) { + var callback = function(err) { if (!err) return; console.log(err); - ccall('Platform_LogError', 'void', ['string'], ['&cError saving files to IndexedDB:']); + ccall('Platform_LogError', 'void', ['string'], ['&cError saving ' + path]); ccall('Platform_LogError', 'void', ['string'], [' &c' + err]); - }); + }; + + var stat, node, entry; + try { + var lookup = FS.lookupPath(path); + node = lookup.node; + stat = FS.stat(path); + } 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); + + transaction.onerror = function(e) { + callback(this.error); + e.preventDefault(); + }; + + var req = store.put(entry, node.path); + req.onsuccess = function() { callback(null); }; + req.onerror = function(e) { + callback(this.error); + e.preventDefault(); + }; + }); }, interop_DirectorySetWorking: function (raw) { var path = UTF8ToString(raw); @@ -151,6 +187,7 @@ mergeInto(LibraryManager.library, { var path = UTF8ToString(raw); try { FS.mkdir(path, mode, 0); + interop_SaveNode(path); return 0; } catch (e) { if (typeof FS === 'undefined' || !(e instanceof FS.ErrnoError)) abort(e); @@ -232,6 +269,8 @@ mergeInto(LibraryManager.library, { try { var stream = FS.getStream(fd); FS.close(stream); + // save writable files to IndexedDB + if (stream.isWrite.get()) interop_SaveNode(stream.path); return 0; } catch (e) { if (typeof FS === 'undefined' || !(e instanceof FS.ErrnoError)) abort(e);