2022-10-21 19:17:06 -04:00
|
|
|
/*
|
|
|
|
* Vencord, a modification for Discord's desktop app
|
|
|
|
* Copyright (c) 2022 Vendicated and contributors
|
|
|
|
*
|
|
|
|
* This program is free software: you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation, either version 3 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with this program. If not, see <https://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
|
2022-08-30 22:07:16 -04:00
|
|
|
import plugins from "plugins";
|
|
|
|
import IpcEvents from "../utils/IpcEvents";
|
2022-08-31 14:47:07 -04:00
|
|
|
import { React } from "../webpack/common";
|
2022-10-05 18:42:58 -04:00
|
|
|
import { mergeDefaults } from "../utils/misc";
|
2022-10-18 16:53:37 -04:00
|
|
|
import { OptionType } from "../utils/types";
|
2022-08-30 22:07:16 -04:00
|
|
|
|
2022-10-11 15:48:28 -04:00
|
|
|
export interface Settings {
|
2022-09-30 18:42:50 -04:00
|
|
|
notifyAboutUpdates: boolean;
|
2022-09-03 11:49:16 -04:00
|
|
|
useQuickCss: boolean;
|
2022-10-11 15:48:28 -04:00
|
|
|
enableReactDevtools: boolean;
|
2022-08-30 22:07:16 -04:00
|
|
|
plugins: {
|
|
|
|
[plugin: string]: {
|
|
|
|
enabled: boolean;
|
|
|
|
[setting: string]: any;
|
|
|
|
};
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
const DefaultSettings: Settings = {
|
2022-09-30 18:42:50 -04:00
|
|
|
notifyAboutUpdates: true,
|
2022-09-03 11:49:16 -04:00
|
|
|
useQuickCss: true,
|
2022-10-11 15:48:28 -04:00
|
|
|
enableReactDevtools: false,
|
2022-08-30 22:07:16 -04:00
|
|
|
plugins: {}
|
2022-09-30 18:42:50 -04:00
|
|
|
};
|
2022-08-30 22:07:16 -04:00
|
|
|
|
2022-08-31 17:04:18 -04:00
|
|
|
for (const plugin in plugins) {
|
|
|
|
DefaultSettings.plugins[plugin] = {
|
|
|
|
enabled: plugins[plugin].required ?? false
|
2022-08-30 22:07:16 -04:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
try {
|
|
|
|
var settings = JSON.parse(VencordNative.ipc.sendSync(IpcEvents.GET_SETTINGS)) as Settings;
|
|
|
|
mergeDefaults(settings, DefaultSettings);
|
|
|
|
} catch (err) {
|
|
|
|
console.error("Corrupt settings file. ", err);
|
|
|
|
var settings = mergeDefaults({} as Settings, DefaultSettings);
|
|
|
|
}
|
|
|
|
|
2022-09-03 12:01:06 -04:00
|
|
|
type SubscriptionCallback = ((newValue: any, path: string) => void) & { _path?: string; };
|
2022-09-03 11:49:16 -04:00
|
|
|
const subscriptions = new Set<SubscriptionCallback>();
|
2022-08-30 22:07:16 -04:00
|
|
|
|
2022-10-18 16:53:37 -04:00
|
|
|
// Wraps the passed settings object in a Proxy to nicely handle change listeners and default values
|
2022-09-03 11:49:16 -04:00
|
|
|
function makeProxy(settings: Settings, root = settings, path = ""): Settings {
|
2022-08-30 22:07:16 -04:00
|
|
|
return new Proxy(settings, {
|
2022-09-03 11:49:16 -04:00
|
|
|
get(target, p: string) {
|
2022-08-30 22:07:16 -04:00
|
|
|
const v = target[p];
|
2022-10-18 16:53:37 -04:00
|
|
|
|
|
|
|
// using "in" is important in the following cases to properly handle falsy or nullish values
|
|
|
|
if (!(p in target)) {
|
|
|
|
// Since the property is not set, check if this is a plugin's setting and if so, try to resolve
|
|
|
|
// the default value.
|
|
|
|
if (path.startsWith("plugins.")) {
|
|
|
|
const plugin = path.slice("plugins.".length);
|
|
|
|
if (plugin in plugins) {
|
|
|
|
const setting = plugins[plugin].options?.[p];
|
|
|
|
if (!setting) return v;
|
|
|
|
if ("default" in setting)
|
|
|
|
// normal setting with a default value
|
|
|
|
return setting.default;
|
|
|
|
if (setting.type === OptionType.SELECT)
|
|
|
|
return setting.options.find(o => o.default)?.value;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return v;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Recursively proxy Objects with the updated property path
|
2022-10-13 18:36:44 -04:00
|
|
|
if (typeof v === "object" && !Array.isArray(v) && v !== null)
|
2022-09-03 11:49:16 -04:00
|
|
|
return makeProxy(v, root, `${path}${path && "."}${p}`);
|
2022-10-18 16:53:37 -04:00
|
|
|
|
|
|
|
// primitive or similar, no need to proxy further
|
2022-08-30 22:07:16 -04:00
|
|
|
return v;
|
|
|
|
},
|
2022-10-18 16:53:37 -04:00
|
|
|
|
2022-09-03 11:49:16 -04:00
|
|
|
set(target, p: string, v) {
|
2022-10-18 16:53:37 -04:00
|
|
|
// avoid unnecessary updates to React Components and other listeners
|
2022-08-30 22:07:16 -04:00
|
|
|
if (target[p] === v) return true;
|
|
|
|
|
|
|
|
target[p] = v;
|
2022-10-18 16:53:37 -04:00
|
|
|
// Call any listeners that are listening to a setting of this path
|
2022-09-03 11:49:16 -04:00
|
|
|
const setPath = `${path}${path && "."}${p}`;
|
2022-08-30 22:07:16 -04:00
|
|
|
for (const subscription of subscriptions) {
|
2022-09-03 11:49:16 -04:00
|
|
|
if (!subscription._path || subscription._path === setPath) {
|
2022-09-03 12:01:06 -04:00
|
|
|
subscription(v, setPath);
|
2022-09-03 11:49:16 -04:00
|
|
|
}
|
2022-08-30 22:07:16 -04:00
|
|
|
}
|
2022-10-18 16:53:37 -04:00
|
|
|
// And don't forget to persist the settings!
|
2022-08-31 17:04:18 -04:00
|
|
|
VencordNative.ipc.invoke(IpcEvents.SET_SETTINGS, JSON.stringify(root, null, 4));
|
2022-08-30 22:07:16 -04:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2022-10-13 18:36:44 -04:00
|
|
|
/**
|
|
|
|
* Same as {@link Settings} but unproxied. You should treat this as readonly,
|
|
|
|
* as modifying properties on this will not save to disk or call settings
|
|
|
|
* listeners.
|
2022-10-18 16:53:37 -04:00
|
|
|
* WARNING: default values specified in plugin.options will not be ensured here. In other words,
|
|
|
|
* settings for which you specified a default value may be uninitialised. If you need proper
|
|
|
|
* handling for default values, use {@link Settings}
|
2022-10-13 18:36:44 -04:00
|
|
|
*/
|
|
|
|
export const PlainSettings = settings;
|
2022-08-30 22:07:16 -04:00
|
|
|
/**
|
|
|
|
* A smart settings object. Altering props automagically saves
|
|
|
|
* the updated settings to disk.
|
2022-10-13 18:36:44 -04:00
|
|
|
* This recursively proxies objects. If you need the object non proxied, use {@link PlainSettings}
|
2022-08-30 22:07:16 -04:00
|
|
|
*/
|
|
|
|
export const Settings = makeProxy(settings);
|
2022-10-18 16:53:37 -04:00
|
|
|
|
2022-08-30 22:07:16 -04:00
|
|
|
/**
|
|
|
|
* Settings hook for React components. Returns a smart settings
|
|
|
|
* object that automagically triggers a rerender if any properties
|
|
|
|
* are altered
|
|
|
|
* @returns Settings
|
|
|
|
*/
|
|
|
|
export function useSettings() {
|
2022-09-30 18:42:50 -04:00
|
|
|
const [, forceUpdate] = React.useReducer(() => ({}), {});
|
2022-08-30 22:07:16 -04:00
|
|
|
|
|
|
|
React.useEffect(() => {
|
|
|
|
subscriptions.add(forceUpdate);
|
|
|
|
return () => void subscriptions.delete(forceUpdate);
|
|
|
|
}, []);
|
|
|
|
|
|
|
|
return Settings;
|
2022-09-03 11:49:16 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// Resolves a possibly nested prop in the form of "some.nested.prop" to type of T.some.nested.prop
|
|
|
|
type ResolvePropDeep<T, P> = P extends "" ? T :
|
|
|
|
P extends `${infer Pre}.${infer Suf}` ?
|
|
|
|
Pre extends keyof T ? ResolvePropDeep<T[Pre], Suf> : never : P extends keyof T ? T[P] : never;
|
|
|
|
|
2022-09-03 12:01:06 -04:00
|
|
|
/**
|
|
|
|
* Add a settings listener that will be invoked whenever the desired setting is updated
|
|
|
|
* @param path Path to the setting that you want to watch, for example "plugins.Unindent.enabled" will fire your callback
|
|
|
|
* whenever Unindent is toggled. Pass an empty string to get notified for all changes
|
|
|
|
* @param onUpdate Callback function whenever a setting matching path is updated. It gets passed the new value and the path
|
|
|
|
* to the updated setting. This path will be the same as your path argument, unless it was an empty string.
|
2022-09-16 16:59:34 -04:00
|
|
|
*
|
2022-09-03 12:01:06 -04:00
|
|
|
* @example addSettingsListener("", (newValue, path) => console.log(`${path} is now ${newValue}`))
|
|
|
|
* addSettingsListener("plugins.Unindent.enabled", v => console.log("Unindent is now", v ? "enabled" : "disabled"))
|
|
|
|
*/
|
|
|
|
export function addSettingsListener<Path extends keyof Settings>(path: Path, onUpdate: (newValue: Settings[Path], path: Path) => void): void;
|
|
|
|
export function addSettingsListener<Path extends string>(path: Path, onUpdate: (newValue: Path extends "" ? any : ResolvePropDeep<Settings, Path>, path: Path extends "" ? string : Path) => void): void;
|
|
|
|
export function addSettingsListener(path: string, onUpdate: (newValue: any, path: string) => void) {
|
2022-09-03 11:49:16 -04:00
|
|
|
(onUpdate as SubscriptionCallback)._path = path;
|
|
|
|
subscriptions.add(onUpdate);
|
2022-09-03 12:01:06 -04:00
|
|
|
}
|