Skip to content

Commit

Permalink
Add hooks for savign and loading save files (#771)
Browse files Browse the repository at this point in the history
  • Loading branch information
gantoine committed Feb 11, 2024
1 parent 838d01d commit 2b22ec1
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 4 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,4 @@ roms/
data/emulator.min.js
data/emulator.min.css
data/cores
.DS_Store
11 changes: 9 additions & 2 deletions data/emulator.js
Original file line number Diff line number Diff line change
Expand Up @@ -1719,7 +1719,7 @@ class EmulatorJS {
let stateUrl;
const saveState = addButton("Save State", '<svg viewBox="0 0 448 512"><path fill="currentColor" d="M433.941 129.941l-83.882-83.882A48 48 0 0 0 316.118 32H48C21.49 32 0 53.49 0 80v352c0 26.51 21.49 48 48 48h352c26.51 0 48-21.49 48-48V163.882a48 48 0 0 0-14.059-33.941zM224 416c-35.346 0-64-28.654-64-64 0-35.346 28.654-64 64-64s64 28.654 64 64c0 35.346-28.654 64-64 64zm96-304.52V212c0 6.627-5.373 12-12 12H76c-6.627 0-12-5.373-12-12V108c0-6.627 5.373-12 12-12h228.52c3.183 0 6.235 1.264 8.485 3.515l3.48 3.48A11.996 11.996 0 0 1 320 111.48z"/></svg>', async () => {
const state = this.gameManager.getState();
const called = this.callEvent("save", {
const called = this.callEvent("saveState", {
screenshot: await this.gameManager.screenshot(),
state: state
});
Expand All @@ -1738,7 +1738,7 @@ class EmulatorJS {
}
});
const loadState = addButton("Load State", '<svg viewBox="0 0 576 512"><path fill="currentColor" d="M572.694 292.093L500.27 416.248A63.997 63.997 0 0 1 444.989 448H45.025c-18.523 0-30.064-20.093-20.731-36.093l72.424-124.155A64 64 0 0 1 152 256h399.964c18.523 0 30.064 20.093 20.73 36.093zM152 224h328v-48c0-26.51-21.49-48-48-48H272l-64-64H48C21.49 64 0 85.49 0 112v278.046l69.077-118.418C86.214 242.25 117.989 224 152 224z"/></svg>', async () => {
const called = this.callEvent("load");
const called = this.callEvent("loadState");
if (called > 0) return;
if (this.settings['save-state-location'] === "browser" && this.saveInBrowserSupported()) {
this.storage.states.get(this.getBaseFileName()+".state").then(e => {
Expand Down Expand Up @@ -1767,6 +1767,11 @@ class EmulatorJS {

const saveSavFiles = addButton("Export Save File", '<svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" viewBox="0 0 23 23"><path d="M3 6.5V5C3 3.89543 3.89543 3 5 3H16.1716C16.702 3 17.2107 3.21071 17.5858 3.58579L20.4142 6.41421C20.7893 6.78929 21 7.29799 21 7.82843V19C21 20.1046 20.1046 21 19 21H5C3.89543 21 3 20.1046 3 19V17.5" stroke="currentColor" stroke-linecap="round" stroke-linejoin="round" fill="transparent"></path><path d="M8 3H16V8.4C16 8.73137 15.7314 9 15.4 9H8.6C8.26863 9 8 8.73137 8 8.4V3Z" stroke="currentColor" stroke-linecap="round" stroke-linejoin="round" fill="transparent"></path><path d="M18 21V13.6C18 13.2686 17.7314 13 17.4 13H15" stroke="currentColor" stroke-linecap="round" stroke-linejoin="round" fill="transparent"></path><path d="M6 21V17.5" stroke="currentColor" stroke-linecap="round" stroke-linejoin="round" fill="transparent"></path><path d="M12 12H1M1 12L4 9M1 12L4 15" stroke="currentColor" stroke-linecap="round" stroke-linejoin="round"></path></svg>', async () => {
const file = await this.gameManager.getSaveFile();
const called = this.callEvent("saveSave", {
screenshot: await this.gameManager.screenshot(),
save: file
});
if (called > 0) return;
const blob = new Blob([file]);
savUrl = URL.createObjectURL(blob);
const a = this.createElement("a");
Expand All @@ -1775,6 +1780,8 @@ class EmulatorJS {
a.click();
});
const loadSavFiles = addButton("Import Save File", '<svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" viewBox="0 0 23 23"><path d="M3 7.5V5C3 3.89543 3.89543 3 5 3H16.1716C16.702 3 17.2107 3.21071 17.5858 3.58579L20.4142 6.41421C20.7893 6.78929 21 7.29799 21 7.82843V19C21 20.1046 20.1046 21 19 21H5C3.89543 21 3 20.1046 3 19V16.5" stroke="currentColor" stroke-linecap="round" stroke-linejoin="round" fill="transparent"></path><path d="M6 21V17" stroke="currentColor" stroke-linecap="round" stroke-linejoin="round"></path><path d="M18 21V13.6C18 13.2686 17.7314 13 17.4 13H15" stroke="currentColor" stroke-linecap="round" stroke-linejoin="round" fill="transparent"></path><path d="M16 3V8.4C16 8.73137 15.7314 9 15.4 9H13.5" stroke="currentColor" stroke-linecap="round" stroke-linejoin="round" fill="transparent"></path><path d="M8 3V6" stroke="currentColor" stroke-linecap="round" stroke-linejoin="round"></path><path d="M1 12H12M12 12L9 9M12 12L9 15" stroke="currentColor" stroke-linecap="round" stroke-linejoin="round"></path></svg>', async () => {
const called = this.callEvent("loadSave");
if (called > 0) return;
const file = await this.selectFile();
const sav = new Uint8Array(await file.arrayBuffer());
const path = this.gameManager.getSaveFilePath();
Expand Down
10 changes: 8 additions & 2 deletions data/loader.js
Original file line number Diff line number Diff line change
Expand Up @@ -136,9 +136,15 @@
window.EJS_emulator.on("start", window.EJS_onGameStart);
}
if (typeof window.EJS_onLoadState === "function") {
window.EJS_emulator.on("load", window.EJS_onLoadState);
window.EJS_emulator.on("loadState", window.EJS_onLoadState);
}
if (typeof window.EJS_onSaveState === "function") {
window.EJS_emulator.on("save", window.EJS_onSaveState);
window.EJS_emulator.on("saveState", window.EJS_onSaveState);
}
if (typeof window.EJS_onLoadSave === "function") {
window.EJS_emulator.on("loadSave", window.EJS_onLoadSave);
}
if (typeof window.EJS_onSaveSave === "function") {
window.EJS_emulator.on("saveSave", window.EJS_onSaveSave);
}
})();

6 comments on commit 2b22ec1

@pavelbv2
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hello.
After this edit, I get an error when I try to save -
1

@ethanaobrien
Copy link
Member

@ethanaobrien ethanaobrien commented on 2b22ec1 Mar 3, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@pavelbv2 clear browser cache and website data

@pavelbv2
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I cleaned the cache, downloading works but saving does not

@ethanaobrien
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

and site data

@pavelbv2
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Perhaps the problem is only in the Opera browser, in Chrome everything works fine.

@ethanaobrien
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Seeing that opera is just chromium with a skin and Chinese spyware I’m assuming you’re not properly clearing site data

Please sign in to comment.