From 255a6cf06b5e3615250d45ba3bc6645c366f80af Mon Sep 17 00:00:00 2001 From: Tobias Berger Date: Wed, 3 Nov 2021 14:44:01 +0100 Subject: [PATCH] Move LZString out of shark folder, use UTF16 instead of Base64 --- src/{shark => }/LZString.ts | 10 +++++++++- src/global.d.ts | 2 +- src/shark/handlers/SaveHandler.ts | 7 ++++--- 3 files changed, 14 insertions(+), 5 deletions(-) rename src/{shark => }/LZString.ts (97%) diff --git a/src/shark/LZString.ts b/src/LZString.ts similarity index 97% rename from src/shark/LZString.ts rename to src/LZString.ts index bfdaba6..1ef42c9 100644 --- a/src/shark/LZString.ts +++ b/src/LZString.ts @@ -13,13 +13,21 @@ export const LZString = new (class LZString { compressToBase64(input: string): string { const result = this.#compress(input, 6, (a) => this.#KeyStrBase64.charAt(a)); - return result + "=".repeat((4 - (result.length % 4)) % 4); + return result + "=".repeat(result.length % 4); } decompressFromBase64(input: string | null): string | null { if (input === null || input === "") return null; return this.#decompress(input.length, 32, (index) => this.#KeyStrBase64Dict[input.charAt(index)]); } + compressToUTF16(input: string): string { + return this.#compress(input, 15, (num) => String.fromCharCode(num + 32)); + } + decompressFromUTF16(input: string | null): string | null { + if (input === null || input === "") return null; + return this.#decompress(input.length, 16384, (index) => input.charCodeAt(index) - 32); + } + compress(uncompressed: string): string { return this.#compress(uncompressed); } diff --git a/src/global.d.ts b/src/global.d.ts index 698febf..ccefc5e 100644 --- a/src/global.d.ts +++ b/src/global.d.ts @@ -1,6 +1,6 @@ /// -import type { LZString } from "./shark/LZString"; +import type { LZString } from "./LZString"; import type { SharkGame } from "./shark/SharkGame"; declare global { diff --git a/src/shark/handlers/SaveHandler.ts b/src/shark/handlers/SaveHandler.ts index 7c1b0ce..4f9b709 100644 --- a/src/shark/handlers/SaveHandler.ts +++ b/src/shark/handlers/SaveHandler.ts @@ -11,7 +11,7 @@ import { Message, MessageType } from "../helperTypes/Message"; import { ResourceData } from "../data/Resources"; -import { LZString } from "../LZString"; +import { LZString } from "../../LZString"; import type { SettingsData } from "../data/Settings"; const __EMPTY_OBJECT = {}; @@ -111,7 +111,7 @@ export const SaveHandler = new (class SaveHandler implements BaseHandler { resources: saveResources, }; const stringifiedSave = JSON.stringify(save); - const encodedSave = LZString.compressToBase64(stringifiedSave); + const encodedSave = LZString.compressToUTF16(stringifiedSave); console.debug( `${new Date(Date.now()).toISOString()} - saving - save encoded to ${ Math.round((encodedSave.length / stringifiedSave.length) * 100 * 100) / 100 @@ -126,7 +126,7 @@ export const SaveHandler = new (class SaveHandler implements BaseHandler { async load(game: typeof SharkGame): Promise { console.debug("Loading"); console.time("Done loading"); - const localSave = LZString.decompressFromBase64(localStorage.getItem(this.saveName)); + const localSave = LZString.decompressFromUTF16(localStorage.getItem(this.saveName)); const loadedSave = JSON.parse(localSave ?? "{}"); const saveVersion = loadedSave.version ?? 0; @@ -182,6 +182,7 @@ export const SaveHandler = new (class SaveHandler implements BaseHandler { } resetSave(): void { + if (this.#saveInterval) clearInterval(this.#saveInterval); const localSave = localStorage.getItem(this.saveName); if (localSave !== null) { localStorage.setItem(this.saveName + "-backup", localSave);