From 0e4724ec0d2ee3c9bd15f81d29dcbd8fec1211ce Mon Sep 17 00:00:00 2001 From: Vendicated Date: Wed, 15 May 2024 03:14:02 +0200 Subject: [PATCH] Settings: remove obsolete patch; add redundancy & more useful dbg copy --- src/plugins/_core/settings.tsx | 77 +++++++++++++++++++++------------- 1 file changed, 47 insertions(+), 30 deletions(-) diff --git a/src/plugins/_core/settings.tsx b/src/plugins/_core/settings.tsx index b09af2c0..81d1d179 100644 --- a/src/plugins/_core/settings.tsx +++ b/src/plugins/_core/settings.tsx @@ -30,6 +30,9 @@ import { i18n, React } from "@webpack/common"; import gitHash from "~git-hash"; +type SectionType = "HEADER" | "DIVIDER" | "CUSTOM"; +type SectionTypes = Record; + export default definePlugin({ name: "Settings", description: "Adds Settings UI and debug info", @@ -41,40 +44,31 @@ export default definePlugin({ find: ".versionHash", replacement: [ { - match: /\[\(0,.{1,3}\.jsxs?\)\((.{1,10}),(\{[^{}}]+\{.{0,20}.versionHash,.+?\})\)," "/, + match: /\[\(0,\i\.jsxs?\)\((.{1,10}),(\{[^{}}]+\{.{0,20}.versionHash,.+?\})\)," "/, replace: (m, component, props) => { props = props.replace(/children:\[.+\]/, ""); return `${m},$self.makeInfoElements(${component}, ${props})`; } + }, + { + match: /copyValue:\i\.join\(" "\)/, + replace: "$& + $self.getInfoString()" } ] }, - // Discord Stable - // FIXME: remove once change merged to stable + // Discord Canary { find: "Messages.ACTIVITY_SETTINGS", replacement: { - get match() { - switch (Settings.plugins.Settings.settingsLocation) { - case "top": return /\{section:(\i\.\i)\.HEADER,\s*label:(\i)\.\i\.Messages\.USER_SETTINGS/; - case "aboveNitro": return /\{section:(\i\.\i)\.HEADER,\s*label:(\i)\.\i\.Messages\.BILLING_SETTINGS/; - case "belowNitro": return /\{section:(\i\.\i)\.HEADER,\s*label:(\i)\.\i\.Messages\.APP_SETTINGS/; - case "belowActivity": return /(?<=\{section:(\i\.\i)\.DIVIDER},)\{section:"changelog"/; - case "bottom": return /\{section:(\i\.\i)\.CUSTOM,\s*element:.+?}/; - case "aboveActivity": - default: - return /\{section:(\i\.\i)\.HEADER,\s*label:(\i)\.\i\.Messages\.ACTIVITY_SETTINGS/; - } - }, - replace: "...$self.makeSettingsCategories($1),$&" + match: /(?<=section:(.{0,50})\.DIVIDER\}\))([,;])(?=.{0,200}(\i)\.push.{0,100}label:(\i)\.header)/, + replace: (_, sectionTypes, commaOrSemi, elements, element) => `${commaOrSemi} $self.addSettings(${elements}, ${element}, ${sectionTypes}) ${commaOrSemi}` } }, - // Discord Canary { - find: "Messages.ACTIVITY_SETTINGS", + find: "useDefaultUserSettingsSections:function", replacement: { - match: /(?<=section:(.{0,50})\.DIVIDER\}\))([,;])(?=.{0,200}(\i)\.push.{0,100}label:(\i)\.header)/, - replace: (_, sectionTypes, commaOrSemi, elements, element) => `${commaOrSemi} $self.addSettings(${elements}, ${element}, ${sectionTypes}) ${commaOrSemi}` + match: /(?<=useDefaultUserSettingsSections:function\(\){return )(\i)\}/, + replace: "$self.wrapSettingsHook($1)}" } }, { @@ -86,9 +80,9 @@ export default definePlugin({ } ], - customSections: [] as ((SectionTypes: Record) => any)[], + customSections: [] as ((SectionTypes: SectionTypes) => any)[], - makeSettingsCategories(SectionTypes: Record) { + makeSettingsCategories(SectionTypes: SectionTypes) { return [ { section: SectionTypes.HEADER, @@ -167,7 +161,7 @@ export default definePlugin({ patchedSettings: new WeakSet(), - addSettings(elements: any[], element: { header?: string; settings: string[]; }, sectionTypes: Record) { + addSettings(elements: any[], element: { header?: string; settings: string[]; }, sectionTypes: SectionTypes) { if (this.patchedSettings.has(elements) || !this.isRightSpot(element)) return; this.patchedSettings.add(elements); @@ -175,6 +169,20 @@ export default definePlugin({ elements.push(...this.makeSettingsCategories(sectionTypes)); }, + wrapSettingsHook(originalHook: (...args: any[]) => Record[]) { + return (...args: any[]) => { + const elements = originalHook(...args); + if (!this.patchedSettings.has(elements)) + elements.unshift(...this.makeSettingsCategories({ + HEADER: "HEADER", + DIVIDER: "DIVIDER", + CUSTOM: "CUSTOM" + })); + + return elements; + }; + }, + options: { settingsLocation: { type: OptionType.SELECT, @@ -213,15 +221,24 @@ export default definePlugin({ return ""; }, - makeInfoElements(Component: React.ComponentType, props: React.PropsWithChildren) { + getInfoRows() { const { electronVersion, chromiumVersion, additionalInfo } = this; - return ( - <> - Vencord {gitHash}{additionalInfo} - {electronVersion && Electron {electronVersion}} - {chromiumVersion && Chromium {chromiumVersion}} - + const rows = [`Vencord ${gitHash}${additionalInfo}`]; + + if (electronVersion) rows.push(`Electron ${electronVersion}`); + if (chromiumVersion) rows.push(`Chromium ${chromiumVersion}`); + + return rows; + }, + + getInfoString() { + return "\n" + this.getInfoRows().join("\n"); + }, + + makeInfoElements(Component: React.ComponentType, props: React.PropsWithChildren) { + return this.getInfoRows().map((text, i) => + {text} ); } });