mirror of
https://github.com/ClassiCube/ClassiCube.git
synced 2025-09-15 18:45:23 -04:00
add function for saving node to IndexedDB directly (doesn't work properly yet)
This commit is contained in:
parent
d690981fc2
commit
7bf24f4016
@ -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 int interop_FileClose(int fd);
|
||||||
extern void interop_SyncFS(void);
|
|
||||||
cc_result File_Close(cc_file file) {
|
cc_result File_Close(cc_file file) {
|
||||||
/* returned result is negative for error */
|
/* returned result is negative for error */
|
||||||
int res = -interop_FileClose(file);
|
return -interop_FileClose(file);
|
||||||
interop_SyncFS();
|
|
||||||
return res;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
extern int interop_FileSeek(int fd, int offset, int whence);
|
extern int interop_FileSeek(int fd, int offset, int whence);
|
||||||
@ -258,6 +255,9 @@ void Platform_LoadSysFonts(void) { }
|
|||||||
/*########################################################################################################################*
|
/*########################################################################################################################*
|
||||||
*---------------------------------------------------------Socket----------------------------------------------------------*
|
*---------------------------------------------------------Socket----------------------------------------------------------*
|
||||||
*#########################################################################################################################*/
|
*#########################################################################################################################*/
|
||||||
|
extern void interop_InitSockets(void);
|
||||||
|
int Socket_ValidAddress(const cc_string* address) { return true; }
|
||||||
|
|
||||||
extern int interop_SocketGetPending(int sock);
|
extern int interop_SocketGetPending(int sock);
|
||||||
cc_result Socket_Available(cc_socket s, int* available) {
|
cc_result Socket_Available(cc_socket s, int* available) {
|
||||||
int res = interop_SocketGetPending(s);
|
int res = interop_SocketGetPending(s);
|
||||||
@ -281,7 +281,6 @@ cc_result Socket_GetError(cc_socket s, cc_result* result) {
|
|||||||
*result = 0; return -res;
|
*result = 0; return -res;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
int Socket_ValidAddress(const cc_string* address) { return true; }
|
|
||||||
|
|
||||||
extern int interop_SocketCreate(void);
|
extern int interop_SocketCreate(void);
|
||||||
extern int interop_SocketConnect(int sock, const char* addr, int port);
|
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_InitModule(void);
|
||||||
extern void interop_InitSockets(void);
|
|
||||||
extern void interop_GetIndexedDBError(char* buffer);
|
extern void interop_GetIndexedDBError(char* buffer);
|
||||||
|
|
||||||
void Platform_Init(void) {
|
void Platform_Init(void) {
|
||||||
|
@ -129,12 +129,48 @@ mergeInto(LibraryManager.library, {
|
|||||||
interop_GetIndexedDBError: function(buffer) {
|
interop_GetIndexedDBError: function(buffer) {
|
||||||
if (window.cc_idbErr) stringToUTF8(window.cc_idbErr, buffer, 64);
|
if (window.cc_idbErr) stringToUTF8(window.cc_idbErr, buffer, 64);
|
||||||
},
|
},
|
||||||
interop_SyncFS: function() {
|
interop_SaveNode: function (path) {
|
||||||
FS.syncfs(false, function(err) {
|
var callback = function(err) {
|
||||||
if (!err) return;
|
if (!err) return;
|
||||||
console.log(err);
|
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]);
|
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) {
|
interop_DirectorySetWorking: function (raw) {
|
||||||
@ -151,6 +187,7 @@ mergeInto(LibraryManager.library, {
|
|||||||
var path = UTF8ToString(raw);
|
var path = UTF8ToString(raw);
|
||||||
try {
|
try {
|
||||||
FS.mkdir(path, mode, 0);
|
FS.mkdir(path, mode, 0);
|
||||||
|
interop_SaveNode(path);
|
||||||
return 0;
|
return 0;
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
if (typeof FS === 'undefined' || !(e instanceof FS.ErrnoError)) abort(e);
|
if (typeof FS === 'undefined' || !(e instanceof FS.ErrnoError)) abort(e);
|
||||||
@ -232,6 +269,8 @@ mergeInto(LibraryManager.library, {
|
|||||||
try {
|
try {
|
||||||
var stream = FS.getStream(fd);
|
var stream = FS.getStream(fd);
|
||||||
FS.close(stream);
|
FS.close(stream);
|
||||||
|
// save writable files to IndexedDB
|
||||||
|
if (stream.isWrite.get()) interop_SaveNode(stream.path);
|
||||||
return 0;
|
return 0;
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
if (typeof FS === 'undefined' || !(e instanceof FS.ErrnoError)) abort(e);
|
if (typeof FS === 'undefined' || !(e instanceof FS.ErrnoError)) abort(e);
|
||||||
|
Loading…
x
Reference in New Issue
Block a user