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-08-30 22:07:16 -04:00
|
|
|
import { mergeDefaults } from '../utils/misc';
|
|
|
|
|
|
|
|
interface Settings {
|
|
|
|
unsafeRequire: boolean;
|
2022-09-03 11:49:16 -04:00
|
|
|
useQuickCss: boolean;
|
2022-08-30 22:07:16 -04:00
|
|
|
plugins: {
|
|
|
|
[plugin: string]: {
|
|
|
|
enabled: boolean;
|
|
|
|
[setting: string]: any;
|
|
|
|
};
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
const DefaultSettings: Settings = {
|
|
|
|
unsafeRequire: false,
|
2022-09-03 11:49:16 -04:00
|
|
|
useQuickCss: true,
|
2022-08-30 22:07:16 -04:00
|
|
|
plugins: {}
|
2022-09-03 11:49:16 -04:00
|
|
|
} as any;
|
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;
|
|
|
|
for (const key in DefaultSettings) {
|
|
|
|
settings[key] ??= DefaultSettings[key];
|
|
|
|
}
|
|
|
|
mergeDefaults(settings, DefaultSettings);
|
|
|
|
} catch (err) {
|
|
|
|
console.error("Corrupt settings file. ", err);
|
|
|
|
var settings = mergeDefaults({} as Settings, DefaultSettings);
|
|
|
|
}
|
|
|
|
|
2022-09-03 11:49:16 -04:00
|
|
|
type SubscriptionCallback = ((newValue: any) => void) & { _path?: string; };
|
|
|
|
const subscriptions = new Set<SubscriptionCallback>();
|
2022-08-30 22:07:16 -04:00
|
|
|
|
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-09-03 11:49:16 -04:00
|
|
|
if (typeof v === "object" && !Array.isArray(v))
|
|
|
|
return makeProxy(v, root, `${path}${path && "."}${p}`);
|
2022-08-30 22:07:16 -04:00
|
|
|
return v;
|
|
|
|
},
|
2022-09-03 11:49:16 -04:00
|
|
|
set(target, p: string, v) {
|
2022-08-30 22:07:16 -04:00
|
|
|
if (target[p] === v) return true;
|
|
|
|
|
|
|
|
target[p] = v;
|
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) {
|
|
|
|
subscription(v);
|
|
|
|
}
|
2022-08-30 22:07:16 -04:00
|
|
|
}
|
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;
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* A smart settings object. Altering props automagically saves
|
|
|
|
* the updated settings to disk.
|
|
|
|
*/
|
|
|
|
export const Settings = makeProxy(settings);
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 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() {
|
|
|
|
const [, forceUpdate] = React.useReducer(x => ({}), {});
|
|
|
|
|
|
|
|
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;
|
|
|
|
|
|
|
|
export function addSettingsListener<Path extends keyof Settings>(path: Path, onUpdate: (newValue: Settings[Path]) => void): void;
|
|
|
|
export function addSettingsListener<Path extends string>(path: Path, onUpdate: (newValue: ResolvePropDeep<Settings, Path>) => void): void;
|
|
|
|
export function addSettingsListener(path: string, onUpdate: (newValue: any) => void) {
|
|
|
|
(onUpdate as SubscriptionCallback)._path = path;
|
|
|
|
subscriptions.add(onUpdate);
|
2022-08-30 22:07:16 -04:00
|
|
|
}
|