Move LZString out of shark folder, use UTF16 instead of Base64

This commit is contained in:
Tobias Berger 2021-11-03 14:44:01 +01:00
parent 678302640f
commit 255a6cf06b
3 changed files with 14 additions and 5 deletions

View file

@ -13,13 +13,21 @@ export const LZString = new (class LZString {
compressToBase64(input: string): string { compressToBase64(input: string): string {
const result = this.#compress(input, 6, (a) => this.#KeyStrBase64.charAt(a)); 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 { decompressFromBase64(input: string | null): string | null {
if (input === null || input === "") return null; if (input === null || input === "") return null;
return this.#decompress(input.length, 32, (index) => this.#KeyStrBase64Dict[input.charAt(index)]); 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 { compress(uncompressed: string): string {
return this.#compress(uncompressed); return this.#compress(uncompressed);
} }

2
src/global.d.ts vendored
View file

@ -1,6 +1,6 @@
/// <reference types="svelte" /> /// <reference types="svelte" />
import type { LZString } from "./shark/LZString"; import type { LZString } from "./LZString";
import type { SharkGame } from "./shark/SharkGame"; import type { SharkGame } from "./shark/SharkGame";
declare global { declare global {

View file

@ -11,7 +11,7 @@ import { Message, MessageType } from "../helperTypes/Message";
import { ResourceData } from "../data/Resources"; import { ResourceData } from "../data/Resources";
import { LZString } from "../LZString"; import { LZString } from "../../LZString";
import type { SettingsData } from "../data/Settings"; import type { SettingsData } from "../data/Settings";
const __EMPTY_OBJECT = {}; const __EMPTY_OBJECT = {};
@ -111,7 +111,7 @@ export const SaveHandler = new (class SaveHandler implements BaseHandler {
resources: saveResources, resources: saveResources,
}; };
const stringifiedSave = JSON.stringify(save); const stringifiedSave = JSON.stringify(save);
const encodedSave = LZString.compressToBase64(stringifiedSave); const encodedSave = LZString.compressToUTF16(stringifiedSave);
console.debug( console.debug(
`${new Date(Date.now()).toISOString()} - saving - save encoded to ${ `${new Date(Date.now()).toISOString()} - saving - save encoded to ${
Math.round((encodedSave.length / stringifiedSave.length) * 100 * 100) / 100 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<void> { async load(game: typeof SharkGame): Promise<void> {
console.debug("Loading"); console.debug("Loading");
console.time("Done 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 loadedSave = JSON.parse(localSave ?? "{}");
const saveVersion = loadedSave.version ?? 0; const saveVersion = loadedSave.version ?? 0;
@ -182,6 +182,7 @@ export const SaveHandler = new (class SaveHandler implements BaseHandler {
} }
resetSave(): void { resetSave(): void {
if (this.#saveInterval) clearInterval(this.#saveInterval);
const localSave = localStorage.getItem(this.saveName); const localSave = localStorage.getItem(this.saveName);
if (localSave !== null) { if (localSave !== null) {
localStorage.setItem(this.saveName + "-backup", localSave); localStorage.setItem(this.saveName + "-backup", localSave);