Roughly got save files working.
This commit is contained in:
1 parent
61d1859437
commit
3b6850f20a
4 files changed
+93
-17
No files matched your search
@@ -5,6 +5,7 @@
|
||||
<title>Emulator Window</title>
|
||||
<link href="/emulator.css" rel="stylesheet" type="text/css" />
|
||||
<script src="https://cdnjs.cloudflare.com/ajax/libs/crypto-js/4.2.0/crypto-js.min.js" integrity="sha512-a+SUDuwNzXDvz4XrIcXHuCf089/iJAoN4lmrXJg18XnduKK6YlDHNRalv4yd1N40OKI80tFidF+rqTFKGPoWFQ==" crossorigin="anonymous" referrerpolicy="no-referrer"></script>
|
||||
<script src="/jszip.min.js" type="text/javascript"></script>
|
||||
</head>
|
||||
|
||||
<body>
|
||||
|
||||
+69
-16
@@ -3,6 +3,11 @@ class HomeplayEmulator {
|
||||
scriptElement = null;
|
||||
initInterval = null;
|
||||
oldGetRetroArchCfg = null;
|
||||
gameStarted = false;
|
||||
|
||||
saveUpdates = false;
|
||||
saveInterval = null;
|
||||
saveLock = false;
|
||||
|
||||
constructor() {
|
||||
// Set up the EJS stuff
|
||||
@@ -50,6 +55,23 @@ class HomeplayEmulator {
|
||||
this.initInterval = setInterval(() => {
|
||||
this.send({ message: 'iframe_loaded' });
|
||||
}, 1000);
|
||||
|
||||
// Check for save updates
|
||||
this.saveInterval = setInterval(async () => {
|
||||
if(this.saveLock) return;
|
||||
this.saveLock = true;
|
||||
try {
|
||||
this.saveUpdates = await this.hasSaveUpdates();
|
||||
if(this.saveUpdates) {
|
||||
console.log('Saving');
|
||||
await this.save();
|
||||
}
|
||||
} catch(e) {
|
||||
console.error('Error checking for save updates', e);
|
||||
} finally {
|
||||
this.saveLock = false;
|
||||
}
|
||||
}, 2000);
|
||||
}
|
||||
|
||||
send = data => {
|
||||
@@ -86,6 +108,11 @@ class HomeplayEmulator {
|
||||
|
||||
onEJSGameStart = () => {
|
||||
console.log('EJS Game Start');
|
||||
|
||||
setTimeout(() => {
|
||||
window.EJS_emulator.gameManager.saveSaveFiles();
|
||||
this.gameStarted = true;
|
||||
}, 1000)
|
||||
}
|
||||
|
||||
onEJSGameEnd = () => {
|
||||
@@ -127,7 +154,7 @@ class HomeplayEmulator {
|
||||
}
|
||||
}
|
||||
|
||||
md5File = async path => {
|
||||
getFileMD5 = async path => {
|
||||
const buffer = window.EJS_emulator.gameManager.FS.readFile(path);
|
||||
const hash = await crypto.subtle.digest('SHA-256', buffer);
|
||||
const hashArray = Array.from(new Uint8Array(hash));
|
||||
@@ -155,21 +182,47 @@ class HomeplayEmulator {
|
||||
// Return back as RA config string
|
||||
return Object.entries(raConfig).map(([k, v]) => `${k} = ${v}`).join('\n');
|
||||
}
|
||||
|
||||
getSaveFiles = () => {
|
||||
return [ 'rom.srm' ];
|
||||
}
|
||||
|
||||
hasSaveUpdates = async () => {
|
||||
if(!window.EJS_emulator) return false;
|
||||
if(!window.EJS_emulator.gameManager) return false;
|
||||
if(!this.gameStarted) return false;
|
||||
|
||||
const saveFiles = this.getSaveFiles();
|
||||
const hashes = await Promise.all(saveFiles.map(f => this.getFileMD5(`/homeplay/saves/${f}`)));
|
||||
|
||||
window.EJS_emulator.gameManager.saveSaveFiles();
|
||||
|
||||
const newHashes = await Promise.all(saveFiles.map(f => this.getFileMD5(`/homeplay/saves/${f}`)));
|
||||
const changedFiles = saveFiles.filter((f, i) => hashes[i] !== newHashes[i]);
|
||||
|
||||
return !!changedFiles.length;
|
||||
}
|
||||
|
||||
save = async () => {
|
||||
// Check if updates
|
||||
if(!this.hasSaveUpdates) return;
|
||||
|
||||
// Get save file contents
|
||||
const saveFiles = this.getSaveFiles();
|
||||
|
||||
// Now we need to create a zip archive. EJS provides a nice ZIP library.
|
||||
const zip = new JSZip();
|
||||
|
||||
await Promise.all(saveFiles.map(async f => {
|
||||
const data = window.EJS_emulator.gameManager.FS.readFile(`/homeplay/saves/${f}`);
|
||||
zip.file(f, data);
|
||||
}));
|
||||
|
||||
this.send({
|
||||
message: 'save',
|
||||
data: await zip.generateAsync({ type:"base64" })
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
// const emulatorSave = async () => {
|
||||
// // First, we get the currently saved MD5 hash of all files in the save
|
||||
// // directory
|
||||
// const contents = EJS_emulator.gameManager.FS.readdir('/homeplay/saves').filter(f => !f.startsWith('.'));
|
||||
// const hashes = await Promise.all(contents.map(f => md5File(`/homeplay/saves/${f}`)));
|
||||
// EJS_emulator.gameManager.saveSaveFiles();
|
||||
// const newHashes = await Promise.all(contents.map(f => md5File(`/homeplay/saves/${f}`)));
|
||||
// const changedFiles = contents.filter((f, i) => hashes[i] !== newHashes[i]);
|
||||
// console.log('Changed files', changedFiles);
|
||||
|
||||
// send({
|
||||
// message: ''
|
||||
// })
|
||||
// }
|
||||
|
||||
window.homeplay = new HomeplayEmulator();
|
||||
Vendored
+13
@@ -0,0 +1,13 @@
|
||||
/*!
|
||||
|
||||
JSZip v3.10.1 - A JavaScript class for generating and reading zip files
|
||||
<http://stuartk.com/jszip>
|
||||
|
||||
(c) 2009-2016 Stuart Knightley <stuart [at] stuartk.com>
|
||||
Dual licenced under the MIT license or GPLv3. See https://raw.github.com/Stuk/jszip/main/LICENSE.markdown.
|
||||
|
||||
JSZip uses the library pako released under the MIT license :
|
||||
https://github.com/nodeca/pako/blob/main/LICENSE
|
||||
*/
|
||||
|
||||
!function(e){if("object"==typeof exports&&"undefined"!=typeof module)module.exports=e();else if("function"==typeof define&&define.amd)define([],e);else{("undefined"!=typeof window?window:"undefined"!=typeof global?global:"undefined"!=typeof self?self:this).JSZip=e()}}(function(){return function s(a,o,h){function u(r,e){if(!o[r]){if(!a[r]){var t="function"==typeof require&&require;if(!e&&t)return t(r,!0);if(l)return l(r,!0);var n=new Error("Cannot find module '"+r+"'");throw n.code="MODULE_NOT_FOUND",n}var i=o[r]={exports:{}};a[r][0].call(i.exports,function(e){var t=a[r][1][e];return u(t||e)},i,i.exports,s,a,o,h)}return o[r].exports}for(var l="function"==typeof require&&require,e=0;e<h.length;e++)u(h[e]);return u}({1:[function(e,t,r){"use strict";var d=e("./utils"),c=e("./support"),p="ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=";r.encode=function(e){for(var t,r,n,i,s,a,o,h=[],u=0,l=e.length,f=l,c="string"!==d.getTypeOf(e);u<e.length;)f=l-u,n=c?(t=e[u++],r=u<l?e[u++]:0,u<l?e[u++]:0):(t=e.charCodeAt(u++),r=u<l?e.charCodeAt(u++):0,u<l?e.charCodeAt(u++):0),i=t>>2,s=(3&t)<<4|r>>4,a=1<f?(15&r)<<2|n>>6:64,o=2<f?63&n:64,h.push(p.charAt(i)+p.charAt(s)+p.charAt(a)+p.charAt(o));return h.join("")},r.decode=function(e){var t,r,n,i,s,a,o=0,h=0,u="data:";if(e.substr(0,u.length)===u)throw new Error("Invalid base64 input, it looks like a data url.");var l,f=3*(e=e.replace(/[^A-Za-z0-9+/=]/g,"")).length/4;if(e.charAt(e.length-1)===p.charAt(64)&&f--,e.charAt(e.length-2)===p.charAt(64)&&f--,f%1!=0)throw new Error("Invalid base64 input, bad content length.");for(l=c.uint8array?new Uint8Array(0|f):new Array(0|f);o<e.length;)t=p.indexOf(e.charAt(o++))<<2|(i=p.indexOf(e.charAt(o++)))>>4,r=(15&i)<<4|(s=p.indexOf(e.charAt(o++)))>>2,n=(3&s)<<6|(a=p.indexOf(e.charAt(o++))),l[h++]=t,64!==s&&(l[h++]=r),64!==a&&(l[h++]=n);return l}},{"./support":30,"./utils":32}],2:[function(e,t,r){"use strict";var n=e("./external"),i=e("./stream/DataWorker"),s=e("./stream/Crc32Probe"),a=e("./stream/DataLengthProbe");function o(e,t,r,n,i){this.compressedSize=e,this.uncompressedSize=t,this.crc32=r,this.compression=n,this.compressedContent=i}o.prototype={getContentWorker:function(){var e=new i(n.Promise.resolve(this.compressedContent)).pipe(this.compression.uncompressWorker()).pipe(new a("data_length")),t=this;return e.on("end",function(){if(this.streamInfo.data_length!==t.uncompressedSize)throw new Error("Bug : uncompressed data size mismatch")}),e},getCompressedWorker:function(){return new i(n.Promise.resolve(this.compressedContent)).withStreamInfo("compressedSize",this.compressedSize).withStreamInfo("uncompressedSize",this.uncompressedSize).withStreamInfo("crc32",this.crc32).withStreamInfo("compression",this.compression)}},o.createWorkerFrom=function(e,t,r){return e.pipe(new s).pipe(new a("uncompressedSize")).pipe(t.compressWorker(r)).pipe(new a("compressedSize")).withStreamInfo("compression",t)},t.exports=o},{"./external":6,"./stream/Crc32Probe":25,"./stream/DataLengthProbe":26,"./stream/DataWorker":27}],3:[function(e,t,r){"use strict";var n=e("./stream/GenericWorker");r.STORE={magic:"\0\0",compressWorker:function(){return new n("STORE compression")},uncompressWorker:function(){return new n("STORE decompression")}},r.DEFLATE=e("./flate")},{"./flate":7,"./stream/GenericWorker":28}],4:[function(e,t,r){"use strict";var n=e("./utils");var o=function(){for(var e,t=[],r=0;r<256;r++){e=r;for(var n=0;n<8;n++)e=1&e?3988292384^e>>>1:e>>>1;t[r]=e}return t}();t.exports=function(e,t){return void 0!==e&&e.length?"string"!==n.getTypeOf(e)?function(e,t,r,n){var i=o,s=n+r;e^=-1;for(var a=n;a<s;a++)e=e>>>8^i[255&(e^t[a])];return-1^e}(0|t,e,e.length,0):function(e,t,r,n){var i=o,s=n+r;e^=-1;for(var a=n;a<s;a++)e=e>>>8^i[255&(e^t.charCodeAt(a))];return-1^e}(0|t,e,e.length,0):0}},{"./utils":32}],5:[function(e,t,r){"use strict";r.base64=!1,r.binary=!1,r.dir=!1,r.createFolders=!0,r.date=null,r.compression=null,r.compressionOptions=null,r.comment=null,r.unixPermissions=null,r.dosPermissions=null},{}],6:[function(e,t,r){"use strict";var n=null;n="undefined"!=typeof Promise?Promise:e("lie"),t.exports={Promise:n}},{lie:37}],7:[function(e,t,r){"use strict";var n="undefined"!=typeof Uint8Array&&"undefined"!=typeof Uint16Array&&"undefined"!=typeof Uint32Array,i=e("pako"),s=e("./utils"),a=e("./stream/GenericWorker"),o=n?"uint8array":"array";function h(e,t){a.call(this,"FlateWorker/"+e),this._pako=null,this._pakoAction=e,this._pakoOptions=t,this.meta={}}r.magic="\b\0",s.inherits(h,a),h.prototype.processChunk=function(e){this.meta=e.meta,null===this._pako&&this._createPako(),this._pako.push(s.transformTo(o,e.data),!1)},h.prototype.flush=function(){a.prototype.flush.call(this),null===this._pako&&this._createPako(),this._pako.push([],!0)},h.prototype.cleanUp=function(){a.prototype.cleanUp.call(this),this._pako=null},h.prototype._createPako=function(){this._pako=new i[this._pakoAction]({raw:!0,level:this._pakoOptions.level||-1});var t=this;this._pako.onData=function(Line truncated
|
||||
@@ -27,7 +27,8 @@ type ReceiveEmulatorMessage = (
|
||||
{ message: 'start' } |
|
||||
{ message: 'ready' } |
|
||||
{ message: 'save_state' } |
|
||||
{ message: 'load_state' }
|
||||
{ message: 'load_state' } |
|
||||
{ message: 'save', data:string }
|
||||
);
|
||||
|
||||
export type EmulatorProps = {
|
||||
@@ -68,6 +69,14 @@ export const Emulator:React.FC<EmulatorProps> = props => {
|
||||
});
|
||||
break;
|
||||
|
||||
case 'save':
|
||||
// Download save data
|
||||
const a = document.createElement('a');
|
||||
a.href = `data:application/zip;base64,${msg.data}`;
|
||||
a.download = `${props.gameId}.zip`;
|
||||
a.click();
|
||||
break;
|
||||
|
||||
case 'start':
|
||||
case 'ready':
|
||||
case 'load_state':
|
||||
|
||||
Reference in new issue
Block a user