2025-05-22 16:24:53 +02:00
|
|
|
|
// this is restoring state to/from the v86 emulator
|
|
|
|
|
|
// however instead of passing the huge blob between webworker/browser
|
|
|
|
|
|
// we transfer it via localforage as a base64 string
|
|
|
|
|
|
|
2024-10-04 09:08:39 +00:00
|
|
|
|
if( typeof emulator != 'undefined' ){
|
|
|
|
|
|
// inside worker-thread
|
2024-10-23 16:50:07 +00:00
|
|
|
|
importScripts("localforage.js") // we don't instance it again here (just use its functions)
|
2024-10-04 09:08:39 +00:00
|
|
|
|
|
2024-11-01 16:39:56 +00:00
|
|
|
|
this.restore_state = async function(data){
|
2025-05-22 16:24:53 +02:00
|
|
|
|
// fastforward instance state
|
|
|
|
|
|
this.opts.muteUntilPrompt = false
|
|
|
|
|
|
this.ready = true
|
|
|
|
|
|
|
2024-10-23 16:50:07 +00:00
|
|
|
|
return new Promise( (resolve,reject) => {
|
|
|
|
|
|
localforage.getItem("state", async (err,stateBase64) => {
|
|
|
|
|
|
if( stateBase64 && !err ){
|
|
|
|
|
|
state = ISOTerminal.prototype.convert.base64ToArrayBuffer( stateBase64 )
|
|
|
|
|
|
await emulator.restore_state(state)
|
|
|
|
|
|
console.log("restored state")
|
|
|
|
|
|
}else return reject("worker.js: emulator.restore_state (could not get state from localforage)")
|
|
|
|
|
|
resolve()
|
|
|
|
|
|
})
|
|
|
|
|
|
})
|
2024-10-04 09:08:39 +00:00
|
|
|
|
}
|
2024-11-01 16:39:56 +00:00
|
|
|
|
this.save_state = async function(){
|
2025-05-22 16:24:53 +02:00
|
|
|
|
return new Promise( async (resolve,reject ) => {
|
|
|
|
|
|
console.log("saving session")
|
|
|
|
|
|
let state = await emulator.save_state()
|
|
|
|
|
|
localforage.setDriver([
|
|
|
|
|
|
localforage.INDEXEDDB,
|
|
|
|
|
|
localforage.WEBSQL,
|
|
|
|
|
|
localforage.LOCALSTORAGE
|
|
|
|
|
|
])
|
|
|
|
|
|
.then( () => {
|
|
|
|
|
|
localforage.setItem("state", ISOTerminal.prototype.convert.arrayBufferToBase64(state) )
|
|
|
|
|
|
console.log("state saved")
|
|
|
|
|
|
resolve()
|
|
|
|
|
|
})
|
|
|
|
|
|
.catch( reject )
|
2024-10-23 16:50:07 +00:00
|
|
|
|
})
|
2024-10-04 09:08:39 +00:00
|
|
|
|
}
|
2024-09-23 14:58:48 +00:00
|
|
|
|
|
|
|
|
|
|
|
2024-10-04 09:08:39 +00:00
|
|
|
|
}else{
|
|
|
|
|
|
// inside browser-thread
|
|
|
|
|
|
ISOTerminal.addEventListener('emulator-started', function(e){
|
|
|
|
|
|
this.autorestore(e)
|
2025-05-22 16:24:53 +02:00
|
|
|
|
this.emit("autorestore-installed")
|
2024-09-17 16:59:38 +00:00
|
|
|
|
})
|
|
|
|
|
|
|
2025-05-22 16:24:53 +02:00
|
|
|
|
ISOTerminal.prototype.restore = async function(e){
|
2024-10-04 09:08:39 +00:00
|
|
|
|
|
2025-05-22 16:24:53 +02:00
|
|
|
|
const onGetItem = (err,stateBase64) => {
|
|
|
|
|
|
const askConfirm = () => {
|
|
|
|
|
|
if( window.localStorage.getItem("restorestate") == "true" ) return true
|
|
|
|
|
|
try{
|
|
|
|
|
|
const scene = document.querySelector('a-scene');
|
|
|
|
|
|
if( scene.is('ar-mode') ) scene.exitAR()
|
|
|
|
|
|
if( scene.is('vr-mode') ) scene.exitVR()
|
|
|
|
|
|
}catch(e){}
|
|
|
|
|
|
return confirm( "Continue old session?" )
|
|
|
|
|
|
}
|
2024-10-04 09:08:39 +00:00
|
|
|
|
|
2025-05-22 16:24:53 +02:00
|
|
|
|
if( stateBase64 && !err && document.location.hash.length < 2 && askConfirm() ){
|
|
|
|
|
|
this.noboot = true // see feat/boot.js
|
|
|
|
|
|
try{
|
|
|
|
|
|
this.worker.restore_state()
|
|
|
|
|
|
.then( () => {
|
2024-10-04 15:49:15 +00:00
|
|
|
|
// simulate / fastforward boot events
|
2024-10-18 11:50:56 +00:00
|
|
|
|
this.postBoot( () => {
|
2025-05-22 16:24:53 +02:00
|
|
|
|
// force redraw terminal issue
|
|
|
|
|
|
this.send("l")
|
|
|
|
|
|
setTimeout( () => this.send("l"), 200 )
|
|
|
|
|
|
//this.send("12")
|
|
|
|
|
|
this.emit("exec",["source /etc/profile.sh; hook wakeup\n"])
|
|
|
|
|
|
this.emit("restored")
|
2024-10-18 11:50:56 +00:00
|
|
|
|
})
|
2025-05-22 16:24:53 +02:00
|
|
|
|
})
|
|
|
|
|
|
}catch(e){ console.error(e) }
|
2024-10-04 09:08:39 +00:00
|
|
|
|
}
|
2025-05-22 16:24:53 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
const doRestore = () => {
|
|
|
|
|
|
|
|
|
|
|
|
localforage.getItem("state", (err,stateBase64) => onGetItem(err,stateBase64) )
|
2024-10-04 09:08:39 +00:00
|
|
|
|
|
|
|
|
|
|
window.addEventListener("beforeunload", function (e) {
|
|
|
|
|
|
var confirmationMessage = "Sure you want to leave?\nTIP: enter 'save' to continue this session later";
|
|
|
|
|
|
(e || window.event).returnValue = confirmationMessage; //Gecko + IE
|
|
|
|
|
|
return confirmationMessage; //Webkit, Safari, Chrome
|
|
|
|
|
|
});
|
|
|
|
|
|
|
2025-05-22 16:24:53 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
localforage.setDriver([
|
|
|
|
|
|
localforage.INDEXEDDB,
|
|
|
|
|
|
localforage.WEBSQL,
|
|
|
|
|
|
localforage.LOCALSTORAGE
|
|
|
|
|
|
])
|
|
|
|
|
|
.then( () => doRestore() )
|
|
|
|
|
|
|
2024-10-04 09:08:39 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
2025-05-22 16:24:53 +02:00
|
|
|
|
ISOTerminal.prototype.autorestore = ISOTerminal.prototype.restore // alias to launch during boot
|
|
|
|
|
|
|
2024-10-04 09:08:39 +00:00
|
|
|
|
}
|