From 45187051858a090fef5bc81c9554417b0092b690 Mon Sep 17 00:00:00 2001 From: Sqaaakoi Date: Mon, 19 Aug 2024 21:31:50 +1200 Subject: [PATCH] Create new "Known Settings" system --- knownSettings.ts | 79 ++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 79 insertions(+) create mode 100644 knownSettings.ts diff --git a/knownSettings.ts b/knownSettings.ts new file mode 100644 index 0000000..e1417ac --- /dev/null +++ b/knownSettings.ts @@ -0,0 +1,79 @@ +/* + * Vencord, a Discord client mod + * Copyright (c) 2024 Vendicated and contributors + * SPDX-License-Identifier: GPL-3.0-or-later + */ + +import { DataStore } from "@api/index"; +import { Settings } from "@api/Settings"; + +import plugins from "~plugins"; + +export type KnownPluginSettingsMap = Map>; + +type PluginSettings = { + [setting: string]: any; + enabled?: boolean; +}; + +export const KNOWN_PLUGINS_LEGACY_DATA_KEY = "NewPluginsManager_KnownPlugins"; +export const KNOWN_SETTINGS_DATA_KEY = "NewPluginsManager_KnownSettings"; + +function getSettingsSetForPlugin(plugin: string): Set { + const settings = Settings.plugins[plugin] || {}; + return new Set(Object.keys(settings).filter(setting => setting !== "enabled")); +} + +function getCurrentSettings(pluginList: string[]): KnownPluginSettingsMap { + return new Map(pluginList.map(name => [ + name, + getSettingsSetForPlugin(name) + ])); +} + +export async function getKnownSettings(): Promise { + let map = await DataStore.get(KNOWN_SETTINGS_DATA_KEY) as KnownPluginSettingsMap; + if (map === undefined) { + const knownPlugins = await DataStore.get(KNOWN_PLUGINS_LEGACY_DATA_KEY) ?? [] as string[]; + DataStore.del(KNOWN_PLUGINS_LEGACY_DATA_KEY); + const Plugins = [...Object.keys(plugins), ...knownPlugins]; + map = getCurrentSettings(Plugins); + DataStore.set(KNOWN_SETTINGS_DATA_KEY, map); + } + return map; +} + +export async function getNewSettings(): Promise { + const map = getCurrentSettings(Object.keys(plugins)); + const knownSettings = await getKnownSettings(); + map.forEach((settings, plugin) => { + const filteredSettings = [...settings].filter(setting => !knownSettings.get(plugin)?.has(setting)); + if (filteredSettings.length === 0 && knownSettings.has(plugin)) return map.delete(plugin); + map.set(plugin, new Set(filteredSettings)); + }); + return map; +} + +export async function getKnownPlugins(): Promise> { + const knownSettings = await getKnownSettings(); + return new Set(knownSettings.keys()); +} + +export async function getNewPlugins(): Promise> { + const currentPlugins = Object.keys(plugins); + const knownPlugins = await getKnownPlugins(); + return new Set(currentPlugins.filter(p => !knownPlugins.has(p))); +} + +export async function writeKnownSettings() { + const currentSettings = getCurrentSettings(Object.keys(plugins)); + const knownSettings = await getKnownSettings(); + const allSettings = new Map(); + new Set([...currentSettings.keys(), ...knownSettings.keys()]).forEach(plugin => { + allSettings.set(plugin, new Set([ + ...(currentSettings.get(plugin) || []), + ...(knownSettings.get(plugin) || []) + ])); + }); + await DataStore.set(KNOWN_SETTINGS_DATA_KEY, allSettings); +}