webretro icon indicating copy to clipboard operation
webretro copied to clipboard

get save files and/or savestates from JS

Open logan-granberry opened this issue 2 years ago • 2 comments

I'm interested in launching a rom and when the user navigates back to the main page, I want to send either a save file or savestate back with them to do further processing. Is this possible or something that can be added without much difficulty?

logan-granberry avatar Aug 17 '22 04:08 logan-granberry

one way replace can auto save file

MEMFS.steam_ops.write

in the "node.useBytes === 0" next line add

Module.saveFIle(stream);


saveFIle:async stream=>{ let type = stream.path&&stream.path.split("/").pop(); if(!(/(srm$|state\d*)/.test(type))||Module[stream.path])return ;

let data = await (()=>new Promise(c=>{ let time = setInterval(()=>{ if(strem.fd==null){clearInterval(time);c(FS.readFile(stream.path))},1000/60 }); }))(); }

nenge123 avatar Aug 18 '22 03:08 nenge123

@DefaultAlias sorry for the late response, saves and states are stored in indexedDB, you can access them using this code:

let wIdb;
function openwIdb() {
	var request = indexedDB.open("webretro", 2);
	request.onsuccess = function(e) {
		wIdb = e.target.result;
	}
}
openwIdb();

function setIdbItem(key, value, customTransaction) {
	(customTransaction || wIdb.transaction("main", "readwrite")).objectStore("main").put({key: key, value: value});
}

function getIdbItem(key, customTransaction) {
	return new Promise(function(resolve) {
		(customTransaction || wIdb.transaction("main", "readwrite")).objectStore("main").get(key).onsuccess = function(e) {
			resolve(e.target.result ? e.target.result.value : null);
		}
	});
}

function getAllIdbItems(customTransaction) {
	return new Promise(function(resolve) {
		(customTransaction || wIdb.transaction("main", "readwrite")).objectStore("main").getAll().onsuccess = function(e) {
			resolve(e.target.result ? e.target.result : null);
		}
	});
}

function removeIdbItem(key, customTransaction) {
	(customTransaction || wIdb.transaction("main", "readwrite")).objectStore("main").delete(key);
}

getIdbItem and getAllIdbItems return promises, and can be awaited

You should use await getAllIdbItems() in the browser console to view the structure of the objects.

BinBashBanana avatar Aug 22 '22 19:08 BinBashBanana