2023-04-10 05:49:50 +09:00
|
|
|
/*
|
|
|
|
* SPDX-License-Identifier: GPL-3.0
|
|
|
|
* Vencord Desktop, a desktop app aiming to give you a snappier Discord Experience
|
|
|
|
* Copyright (c) 2023 Vendicated and Vencord contributors
|
|
|
|
*/
|
|
|
|
|
2023-04-04 11:17:38 +09:00
|
|
|
import { readFileSync, writeFileSync } from "fs";
|
|
|
|
import { join } from "path";
|
2023-04-09 07:49:47 +09:00
|
|
|
import type { Settings as TSettings } from "shared/settings";
|
2023-04-10 08:16:44 +09:00
|
|
|
import { SettingsStore } from "shared/utils/SettingsStore";
|
2023-04-10 05:49:50 +09:00
|
|
|
|
2023-04-09 12:25:45 +09:00
|
|
|
import { DATA_DIR, VENCORD_SETTINGS_FILE } from "./constants";
|
2023-04-04 11:17:38 +09:00
|
|
|
|
|
|
|
const SETTINGS_FILE = join(DATA_DIR, "settings.json");
|
|
|
|
|
2023-04-09 12:25:45 +09:00
|
|
|
function loadSettings<T extends object = any>(file: string, name: string) {
|
|
|
|
let settings = {} as T;
|
2023-04-04 11:17:38 +09:00
|
|
|
try {
|
2023-04-09 12:25:45 +09:00
|
|
|
const content = readFileSync(file, "utf8");
|
|
|
|
try {
|
|
|
|
settings = JSON.parse(content);
|
|
|
|
} catch (err) {
|
|
|
|
console.error(`Failed to parse ${name} settings.json:`, err);
|
|
|
|
}
|
2023-04-10 05:49:50 +09:00
|
|
|
} catch {}
|
2023-04-04 11:17:38 +09:00
|
|
|
|
2023-04-10 08:04:41 +09:00
|
|
|
const store = new SettingsStore(settings);
|
|
|
|
store.addGlobalChangeListener(o => writeFileSync(file, JSON.stringify(o, null, 4)));
|
2023-04-04 11:40:03 +09:00
|
|
|
|
2023-04-10 08:04:41 +09:00
|
|
|
return store;
|
2023-04-09 12:25:45 +09:00
|
|
|
}
|
|
|
|
|
2023-04-10 08:04:41 +09:00
|
|
|
export const Settings = loadSettings<TSettings>(SETTINGS_FILE, "Vencord Desktop");
|
|
|
|
export const VencordSettings = loadSettings<any>(VENCORD_SETTINGS_FILE, "Vencord");
|