BinBashBanana / webretro

RetroArch in your browser
https://binbashbanana.github.io/webretro/
MIT License
249 stars 350 forks source link

get save files and/or savestates from JS #32

Closed logan-granberry closed 1 year ago

logan-granberry commented 2 years ago

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?

nenge123 commented 2 years ago

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 }); }))(); }

BinBashBanana commented 2 years ago

@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.