From dfb14acc6e80bd02d16034f675089968b62e1bfa Mon Sep 17 00:00:00 2001 From: thepaperpilot Date: Sun, 3 Mar 2024 19:24:24 -0600 Subject: [PATCH] Allow null and undefined values in persistent refs --- .eslintrc.js | 7 +++++++ src/game/persistence.ts | 6 ++++-- src/game/player.ts | 3 ++- src/util/save.ts | 2 +- 4 files changed, 14 insertions(+), 4 deletions(-) diff --git a/.eslintrc.js b/.eslintrc.js index a881f38..f7aac50 100644 --- a/.eslintrc.js +++ b/.eslintrc.js @@ -27,6 +27,13 @@ module.exports = { allowNullableObject: true, allowNullableBoolean: true } + ], + "eqeqeq": [ + "error", + "always", + { + "null": "never" + } ] }, globals: { diff --git a/src/game/persistence.ts b/src/game/persistence.ts index e760411..4e7704d 100644 --- a/src/game/persistence.ts +++ b/src/game/persistence.ts @@ -62,6 +62,8 @@ export type State = | number | boolean | DecimalSource + | null + | undefined | { [key: string]: State } | { [key: number]: State }; @@ -227,7 +229,7 @@ export function noPersist, S extends State>(persistent: if (key === PersistentState) { return false; } - if (key == SkipPersistence) { + if (key === SkipPersistence) { return true; } return Reflect.has(target, key); @@ -279,7 +281,7 @@ globalBus.on("addLayer", (layer: GenericLayer, saveData: Record // Handle SaveDataPath const newPath = [layer.id, ...path, key]; if ( - value[SaveDataPath] != undefined && + value[SaveDataPath] != null && JSON.stringify(newPath) !== JSON.stringify(value[SaveDataPath]) ) { console.error( diff --git a/src/game/player.ts b/src/game/player.ts index 5d1142e..322e4db 100644 --- a/src/game/player.ts +++ b/src/game/player.ts @@ -64,7 +64,8 @@ export default window.player = player; /** Convert a player save data object into a JSON string. Unwraps refs. */ export function stringifySave(player: Player): string { - return JSON.stringify(player, (key, value) => unref(value)); + // Convert undefineds into nulls for proper parsing + return JSON.stringify(player, (key, value) => unref(value) ?? null); } declare global { diff --git a/src/util/save.ts b/src/util/save.ts index a7830cd..86c7691 100644 --- a/src/util/save.ts +++ b/src/util/save.ts @@ -119,7 +119,7 @@ export async function loadSave(playerObj: Partial): Promise { playerObj.time && playerObj.devSpeed !== 0 ) { - if (playerObj.offlineTime == undefined) playerObj.offlineTime = 0; + if (playerObj.offlineTime == null) playerObj.offlineTime = 0; playerObj.offlineTime += Math.min( playerObj.offlineTime + (Date.now() - playerObj.time) / 1000, projInfo.offlineLimit * 3600