Store mtime in indexeddb

This commit is contained in:
Varun Patil 2020-10-20 17:13:37 +05:30
parent 709bcdb5bc
commit e7fc3f7025
1 changed files with 23 additions and 16 deletions

View File

@ -86,39 +86,46 @@ window.loadFileAsync = function(fullPath, bitmap, callback) {
}
window.saveFile = function(filename) {
const buf = FS.readFile('/game/' + filename);
window.saveFile = function(filename, localOnly) {
const fpath = '/game/' + filename;
if (!FS.analyzePath(fpath).exists) return;
const buf = FS.readFile(fpath);
localforage.setItem(namespace + filename, buf);
localforage.getItem(namespace, function(err, res) {
if (err || !res) res = {};
res[filename] = 1;
res[filename] = { t: Number(FS.stat(fpath).mtime) };
localforage.setItem(namespace, res);
});
if (!localOnly) {
(window.saveCloudFile || (()=>{}))(filename, buf);
}
};
var loadFiles = function() {
localforage.getItem(namespace, function(err, res) {
localforage.getItem(namespace, function(err, folder) {
if (err || !folder) return;
console.log('Locally stored savefiles:', folder);
Object.keys(folder).forEach((key) => {
const meta = folder[key];
localforage.getItem(namespace + key, (err, res) => {
if (err || !res) return;
const keys = Object.keys(res);
console.log('Locally stored savefiles', keys);
keys.forEach((key) => {
localforage.getItem(namespace + key, (err, res) => {
if (err) return;
// Don't overwrite existing files
const fname = '/game/' + key;
if (FS.analyzePath(fname).exists) return;
const fpath = '/game/' + key;
if (FS.analyzePath(fpath).exists) return;
FS.writeFile(fname, res);
});
FS.writeFile(fpath, res);
if (Number.isInteger(meta.t)) {
FS.utime(fpath, meta.t, meta.t);
}
});
});
}, console.error);
(window.loadCloudFiles || (()=>{}))();
}