Profectus/src/game/settings.ts

56 lines
1.4 KiB
TypeScript
Raw Normal View History

import modInfo from "@/data/modInfo.json";
import { Themes } from "@/data/themes";
2022-01-14 04:25:47 +00:00
import { globalBus } from "@/game/events";
import { hardReset } from "@/util/save";
2022-01-14 04:25:47 +00:00
import { reactive, watch } from "vue";
2022-01-14 04:25:47 +00:00
export interface Settings {
active: string;
saves: string[];
showTPS: boolean;
theme: Themes;
unthrottled: boolean;
}
const state = reactive<Partial<Settings>>({
active: "",
saves: [],
showTPS: true,
2021-09-06 00:13:56 +00:00
theme: Themes.Nordic,
unthrottled: false
});
2022-01-14 04:25:47 +00:00
watch(
() => state,
state =>
localStorage.setItem(modInfo.id, btoa(unescape(encodeURIComponent(JSON.stringify(state))))),
{ deep: true }
);
export default window.settings = state as Settings;
export function loadSettings(): void {
try {
const item: string | null = localStorage.getItem(modInfo.id);
if (item != null && item !== "") {
const settings = JSON.parse(decodeURIComponent(escape(atob(item))));
if (typeof settings === "object") {
Object.assign(state, settings);
}
}
2022-01-14 04:25:47 +00:00
globalBus.emit("loadSettings", state);
// eslint-disable-next-line no-empty
} catch {}
}
export const hardResetSettings = (window.hardResetSettings = () => {
2022-01-14 04:25:47 +00:00
const settings = {
active: "",
saves: [],
showTPS: true,
theme: Themes.Nordic
2022-01-14 04:25:47 +00:00
};
globalBus.emit("loadSettings", settings);
Object.assign(state, settings);
hardReset();
});