2023-04-09 22:49:50 +02:00
|
|
|
/*
|
|
|
|
* SPDX-License-Identifier: GPL-3.0
|
2023-07-13 19:03:13 +02:00
|
|
|
* Vesktop, a desktop app aiming to give you a snappier Discord Experience
|
2023-04-09 22:49:50 +02:00
|
|
|
* Copyright (c) 2023 Vendicated and Vencord contributors
|
|
|
|
*/
|
|
|
|
|
2024-01-07 02:26:18 +01:00
|
|
|
import { existsSync, mkdirSync, readFileSync, writeFileSync } from "fs";
|
2023-06-21 17:24:50 +02:00
|
|
|
import { dirname, join } from "path";
|
2024-01-07 02:26:18 +01:00
|
|
|
import type { Settings as TSettings, State as TState } from "shared/settings";
|
2023-04-10 01:16:44 +02:00
|
|
|
import { SettingsStore } from "shared/utils/SettingsStore";
|
2023-04-09 22:49:50 +02:00
|
|
|
|
2023-04-09 05:25:45 +02:00
|
|
|
import { DATA_DIR, VENCORD_SETTINGS_FILE } from "./constants";
|
2023-04-04 04:17:38 +02:00
|
|
|
|
|
|
|
const SETTINGS_FILE = join(DATA_DIR, "settings.json");
|
2024-01-07 02:26:18 +01:00
|
|
|
const STATE_FILE = join(DATA_DIR, "state.json");
|
2023-04-04 04:17:38 +02:00
|
|
|
|
2023-04-09 05:25:45 +02:00
|
|
|
function loadSettings<T extends object = any>(file: string, name: string) {
|
|
|
|
let settings = {} as T;
|
2023-04-04 04:17:38 +02:00
|
|
|
try {
|
2023-04-09 05:25:45 +02:00
|
|
|
const content = readFileSync(file, "utf8");
|
|
|
|
try {
|
|
|
|
settings = JSON.parse(content);
|
|
|
|
} catch (err) {
|
2024-01-07 02:26:18 +01:00
|
|
|
console.error(`Failed to parse ${name}.json:`, err);
|
2023-04-09 05:25:45 +02:00
|
|
|
}
|
2023-04-09 22:49:50 +02:00
|
|
|
} catch {}
|
2023-04-04 04:17:38 +02:00
|
|
|
|
2023-04-10 01:04:41 +02:00
|
|
|
const store = new SettingsStore(settings);
|
2023-06-21 17:24:50 +02:00
|
|
|
store.addGlobalChangeListener(o => {
|
|
|
|
mkdirSync(dirname(file), { recursive: true });
|
|
|
|
writeFileSync(file, JSON.stringify(o, null, 4));
|
|
|
|
});
|
2023-04-04 04:40:03 +02:00
|
|
|
|
2023-04-10 01:04:41 +02:00
|
|
|
return store;
|
2023-04-09 05:25:45 +02:00
|
|
|
}
|
|
|
|
|
2024-01-07 02:26:18 +01:00
|
|
|
export const Settings = loadSettings<TSettings>(SETTINGS_FILE, "Vesktop settings");
|
|
|
|
export const VencordSettings = loadSettings<any>(VENCORD_SETTINGS_FILE, "Vencord settings");
|
|
|
|
|
|
|
|
if (Object.hasOwn(Settings.store, "firstLaunch") && !existsSync(STATE_FILE)) {
|
|
|
|
console.warn("legacy state in settings.json detected. migrating to state.json");
|
|
|
|
const state = {} as TState;
|
|
|
|
for (const prop of [
|
|
|
|
"firstLaunch",
|
|
|
|
"maximized",
|
|
|
|
"minimized",
|
|
|
|
"skippedUpdate",
|
|
|
|
"steamOSLayoutVersion",
|
|
|
|
"windowBounds"
|
|
|
|
]) {
|
|
|
|
state[prop] = Settings.plain[prop];
|
|
|
|
delete Settings.plain[prop];
|
|
|
|
}
|
|
|
|
Settings.markAsChanged();
|
|
|
|
writeFileSync(STATE_FILE, JSON.stringify(state, null, 4));
|
|
|
|
}
|
|
|
|
|
|
|
|
export const State = loadSettings<TState>(STATE_FILE, "Vesktop state");
|