2022-03-04 03:39:48 +00:00
|
|
|
import projInfo from "data/projInfo.json";
|
|
|
|
import { Themes } from "data/themes";
|
2022-06-27 00:17:22 +00:00
|
|
|
import type { CoercableComponent } from "features/feature";
|
2022-03-04 03:39:48 +00:00
|
|
|
import { globalBus } from "game/events";
|
2022-04-18 02:15:38 +00:00
|
|
|
import LZString from "lz-string";
|
2022-03-04 03:39:48 +00:00
|
|
|
import { hardReset } from "util/save";
|
2022-02-27 19:49:34 +00:00
|
|
|
import { reactive, watch } from "vue";
|
2021-09-05 23:53:04 +00:00
|
|
|
|
2022-01-14 04:25:47 +00:00
|
|
|
export interface Settings {
|
|
|
|
active: string;
|
|
|
|
saves: string[];
|
|
|
|
showTPS: boolean;
|
|
|
|
theme: Themes;
|
|
|
|
unthrottled: boolean;
|
|
|
|
}
|
|
|
|
|
2022-02-27 19:49:34 +00:00
|
|
|
const state = reactive<Partial<Settings>>({
|
2021-09-05 23:53:04 +00:00
|
|
|
active: "",
|
|
|
|
saves: [],
|
|
|
|
showTPS: true,
|
2021-09-06 00:13:56 +00:00
|
|
|
theme: Themes.Nordic,
|
|
|
|
unthrottled: false
|
2021-09-05 23:53:04 +00:00
|
|
|
});
|
|
|
|
|
2022-01-14 04:25:47 +00:00
|
|
|
watch(
|
2022-02-27 19:49:34 +00:00
|
|
|
state,
|
2022-04-18 02:15:38 +00:00
|
|
|
state => {
|
2022-05-11 01:58:13 +00:00
|
|
|
const stringifiedSettings = LZString.compressToUTF16(JSON.stringify(state));
|
2022-04-18 02:15:38 +00:00
|
|
|
localStorage.setItem(projInfo.id, stringifiedSettings);
|
|
|
|
},
|
2022-01-14 04:25:47 +00:00
|
|
|
{ deep: true }
|
|
|
|
);
|
2022-07-10 03:09:25 +00:00
|
|
|
|
|
|
|
declare global {
|
|
|
|
/**
|
|
|
|
* Augment the window object so the settings, and hard resetting the settings,
|
|
|
|
* can be accessed from the console
|
|
|
|
*/
|
|
|
|
interface Window {
|
|
|
|
settings: Settings;
|
|
|
|
hardResetSettings: VoidFunction;
|
|
|
|
}
|
|
|
|
}
|
2022-01-14 04:25:47 +00:00
|
|
|
export default window.settings = state as Settings;
|
2022-07-10 03:09:25 +00:00
|
|
|
export const hardResetSettings = (window.hardResetSettings = () => {
|
|
|
|
const settings = {
|
|
|
|
active: "",
|
|
|
|
saves: [],
|
|
|
|
showTPS: true,
|
|
|
|
theme: Themes.Nordic
|
|
|
|
};
|
|
|
|
globalBus.emit("loadSettings", settings);
|
|
|
|
Object.assign(state, settings);
|
|
|
|
hardReset();
|
|
|
|
});
|
2021-09-05 23:53:04 +00:00
|
|
|
|
|
|
|
export function loadSettings(): void {
|
|
|
|
try {
|
2022-04-18 02:15:38 +00:00
|
|
|
let item: string | null = localStorage.getItem(projInfo.id);
|
2021-09-05 23:53:04 +00:00
|
|
|
if (item != null && item !== "") {
|
2022-04-18 02:15:38 +00:00
|
|
|
if (item[0] === "{") {
|
|
|
|
// plaintext. No processing needed
|
|
|
|
} else if (item[0] === "e") {
|
|
|
|
// Assumed to be base64, which starts with e
|
|
|
|
item = decodeURIComponent(escape(atob(item)));
|
|
|
|
} else if (item[0] === "ᯡ") {
|
|
|
|
// Assumed to be lz, which starts with ᯡ
|
|
|
|
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
|
|
|
|
item = LZString.decompressFromUTF16(item)!;
|
|
|
|
} else {
|
|
|
|
console.warn("Unable to determine settings encoding", item);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
const settings = JSON.parse(item);
|
2021-09-05 23:53:04 +00:00
|
|
|
if (typeof settings === "object") {
|
|
|
|
Object.assign(state, settings);
|
|
|
|
}
|
|
|
|
}
|
2022-01-14 04:25:47 +00:00
|
|
|
globalBus.emit("loadSettings", state);
|
2021-09-05 23:53:04 +00:00
|
|
|
// eslint-disable-next-line no-empty
|
|
|
|
} catch {}
|
|
|
|
}
|
|
|
|
|
2022-02-27 22:41:39 +00:00
|
|
|
export const settingFields: CoercableComponent[] = reactive([]);
|
|
|
|
export function registerSettingField(component: CoercableComponent) {
|
|
|
|
settingFields.push(component);
|
|
|
|
}
|
2022-03-11 22:38:49 +00:00
|
|
|
|
|
|
|
export const infoComponents: CoercableComponent[] = reactive([]);
|
|
|
|
export function registerInfoComponent(component: CoercableComponent) {
|
|
|
|
infoComponents.push(component);
|
|
|
|
}
|
2022-03-20 04:59:52 +00:00
|
|
|
|
|
|
|
export const gameComponents: CoercableComponent[] = reactive([]);
|
|
|
|
export function registerGameComponent(component: CoercableComponent) {
|
|
|
|
gameComponents.push(component);
|
|
|
|
}
|