Skip to content

Commit 2cf9d05

Browse files
authored
Merge pull request #839 from yamt/diskw-netplay
make diskw skip localStorage.setItem for non-host players
2 parents e91de14 + 5280529 commit 2cf9d05

1 file changed

Lines changed: 16 additions & 8 deletions

File tree

runtimes/web/src/runtime.ts

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ export class Runtime {
1919
wasmBufferByteLen: number;
2020
wasm: WebAssembly.Instance | null = null;
2121
warnedFileSize = false;
22+
playerIdx : number;
2223

2324
diskName: string;
2425
diskBuffer: ArrayBuffer;
@@ -66,6 +67,7 @@ export class Runtime {
6667
this.reset();
6768

6869
this.pauseState = 0;
70+
this.playerIdx = 0;
6971
this.wasmBufferByteLen = 0;
7072
}
7173

@@ -85,6 +87,7 @@ export class Runtime {
8587

8688
setNetplay (localPlayerIdx: number) {
8789
this.data.setUint8(constants.ADDR_NETPLAY, 0b100 | (localPlayerIdx & 0b11));
90+
this.playerIdx = localPlayerIdx;
8891
}
8992

9093
getSystemFlag (mask: number) {
@@ -231,15 +234,20 @@ export class Runtime {
231234
const src = new Uint8Array(this.memory.buffer, srcPtr, bytesWritten);
232235
const dest = new Uint8Array(this.diskBuffer);
233236

234-
// Try to save to localStorage
235-
const str = z85.encode(src);
236-
try {
237-
localStorage.setItem(this.diskName, str);
238-
} catch (error) {
239-
// TODO(2022-02-13): Show a warning to the user that storage is not persisted
240-
console.error("Error writing disk", error);
237+
// save to localStorage only for the hosting player. (index 0)
238+
// as the disk buffer is a part of the shared state provided by
239+
// the hosting player, don't risk overwriting the local data
240+
// for others.
241+
if (this.playerIdx == 0) {
242+
// Try to save to localStorage
243+
const str = z85.encode(src);
244+
try {
245+
localStorage.setItem(this.diskName, str);
246+
} catch (error) {
247+
// TODO(2022-02-13): Show a warning to the user that storage is not persisted
248+
console.error("Error writing disk", error);
249+
}
241250
}
242-
243251
dest.set(src);
244252
this.diskSize = bytesWritten;
245253
return bytesWritten;

0 commit comments

Comments
 (0)