From 50d7450fea78586530fcf2560306462bb007dc9f Mon Sep 17 00:00:00 2001 From: Oj18 Date: Sat, 30 Oct 2021 18:20:10 +0100 Subject: [PATCH] [Settings] Rewrite GMSettings to use proxy, add new experimental option --- src/genDebugInfo.js | 2 +- src/gmBadges.js | 8 ++--- src/gmSettings.js | 38 ++++++++++++---------- src/index.js | 10 +++--- src/moduleStore/index.js | 2 +- src/ui/settings/baseItems.js | 51 ++++++++++++++++++------------ src/ui/settings/home/index.js | 6 ++-- src/ui/settings/index.js | 6 ++-- src/ui/settings/items/card.js | 2 +- src/ui/settings/settingsSidebar.js | 2 +- src/ui/toast.js | 2 +- src/util/patcher/username.js | 46 ++++++++++++++------------- 12 files changed, 97 insertions(+), 78 deletions(-) diff --git a/src/genDebugInfo.js b/src/genDebugInfo.js index 2bc3d7e8..219be19e 100644 --- a/src/genDebugInfo.js +++ b/src/genDebugInfo.js @@ -14,7 +14,7 @@ Other Mods: ${Object.keys(mods).filter((x) => Object.keys(window).includes(x)).m GooseMod: GM Version: ${goosemod.versioning.version} (${goosemod.versioning.hash}) -GM Branch: ${goosemod.storage.get('goosemodUntetheredBranch')} +GM Branch: ${goosemod.versioning.branch} GM Extension Version: ${window.gmExtension} GM Storage Impl: ${goosemod.storage.type} Modules: ${Object.keys(goosemod.modules).join(', ')}`; diff --git a/src/gmBadges.js b/src/gmBadges.js index 982af59c..a24e0254 100644 --- a/src/gmBadges.js +++ b/src/gmBadges.js @@ -130,7 +130,7 @@ export const addBadges = () => { 'https://goosemod.com/img/goose_gold.jpg', // Force check via query because Discord not properly rerendering - () => goosemodScope.settings.gmSettings.get().gmBadges ? badgeUsers.sponsor : [], + () => goosemodScope.settings.gmSettings.gmBadges ? badgeUsers.sponsor : [], () => { showSponsorModal(); @@ -143,7 +143,7 @@ export const addBadges = () => { 'https://goosemod.com/img/goose_globe.png', // Force check via query because Discord not properly rerendering - () => goosemodScope.settings.gmSettings.get().gmBadges ? badgeUsers.translator : [], + () => goosemodScope.settings.gmSettings.gmBadges ? badgeUsers.translator : [], () => { @@ -156,7 +156,7 @@ export const addBadges = () => { 'https://goosemod.com/img/goose_glitch.jpg', // Force check via query because Discord not properly rerendering - () => goosemodScope.settings.gmSettings.get().gmBadges ? badgeUsers.dev : [], + () => goosemodScope.settings.gmSettings.gmBadges ? badgeUsers.dev : [], () => { @@ -170,7 +170,7 @@ export const addBadges = () => { 'https://goosemod.com/img/logo.jpg', // Force check via query because Discord not properly rerendering - () => goosemodScope.settings.gmSettings.get().gmBadges ? ['756146058320674998'] : [], + () => goosemodScope.settings.gmSettings.gmBadges ? ['756146058320674998'] : [], () => { diff --git a/src/gmSettings.js b/src/gmSettings.js index 726e0739..585b72be 100644 --- a/src/gmSettings.js +++ b/src/gmSettings.js @@ -1,5 +1,3 @@ -let cache; - const defaultSettings = { changelog: true, separators: true, @@ -20,26 +18,32 @@ const defaultSettings = { debugToasts: false }; -export const get = () => { - // Cache as this function is called frequently - if (cache) return cache; - - cache = JSON.parse(goosemod.storage.get('goosemodGMSettings')) || {}; +const loadStore = () => { + const loaded = JSON.parse(goosemod.storage.get('goosemodGMSettings')) || {}; - cache = { + return { ...defaultSettings, - ...cache - }; + ...loaded, - return cache; + get: () => target // gmSettings.get compat (because of all theme settings using old) + }; }; -export const set = (key, value) => { - const settings = get(); +const target = { uninit: true }; +export default new Proxy({ uninit: true }, { + get(target, prop) { + if (target.uninit) target = loadStore(); + + return target[prop] ?? false; + }, + + set(target, prop, value) { + if (target.uninit) target = loadStore(); - settings[key] = value; + target[prop] = value; - goosemod.storage.set('goosemodGMSettings', JSON.stringify(settings)); + goosemod.storage.set('goosemodGMSettings', JSON.stringify(target)); - cache = settings; // Set cache to new value -}; \ No newline at end of file + return true; + } +}); \ No newline at end of file diff --git a/src/index.js b/src/index.js index 15b481a8..8cee4d5a 100644 --- a/src/index.js +++ b/src/index.js @@ -109,7 +109,9 @@ const init = async function () { this.versioning = { version: `12.3-dev`, - hash: '', // Hash of built final js file is inserted here via build script + hash: '', // Hash of git commit + + branch: goosemod.storage.get('goosemodUntetheredBranch'), lastUsedVersion: this.storage.get('goosemodLastVersion') }; @@ -192,8 +194,8 @@ const init = async function () { this.cssCache.removeStyle(); - if (this.settings.gmSettings.get().gmBadges) this.gmBadges.addBadges(); - if (this.settings.gmSettings.get().attrs) this.attrs.patch(); + if (this.settings.gmSettings.gmBadges) this.gmBadges.addBadges(); + if (this.settings.gmSettings.attrs) this.attrs.patch(); this.saveInterval = setInterval(() => { this.moduleSettingsStore.saveModuleSettings(); @@ -222,7 +224,7 @@ const init = async function () { // Hotupdate every hour this.hotupdateInterval = setInterval(() => { - if (!this.settings.gmSettings.get().autoupdate) return; // Check main GM setting + if (!this.settings.gmSettings.autoupdate) return; // Check main GM setting this.moduleStoreAPI.hotupdate(); }, 1000 * 60 * 60); diff --git a/src/moduleStore/index.js b/src/moduleStore/index.js index 989e83da..fa55ba1e 100644 --- a/src/moduleStore/index.js +++ b/src/moduleStore/index.js @@ -130,7 +130,7 @@ export default { return repo; }))).sort((a, b) => goosemodScope.moduleStoreAPI.repos.indexOf(a.url) - goosemodScope.moduleStoreAPI.repos.indexOf(b.url)); - if (goosemodScope.settings.gmSettings.get().newModuleNotifications) for (const newModule of newModules) { + if (goosemodScope.settings.gmSettings.newModuleNotifications) for (const newModule of newModules) { const currentModule = goosemodScope.moduleStoreAPI.modules.find((x) => newModule.name === x.name); if (!currentModule) { // Is new module (not existing before) diff --git a/src/ui/settings/baseItems.js b/src/ui/settings/baseItems.js index 2abcc37a..4026b413 100644 --- a/src/ui/settings/baseItems.js +++ b/src/ui/settings/baseItems.js @@ -19,13 +19,13 @@ export default (goosemodScope, gmSettings, Items) => { }, false] ]; - if (gmSettings.get().separators) items.unshift(['separator']); + if (gmSettings.separators) items.unshift(['separator']); goosemodScope.settings.items.splice(goosemodScope.settings.items.indexOf(goosemodScope.settings.items.find(x => x[1] === 'Themes')) + 1, 0, ...items ); } else { - goosemodScope.settings.items.splice(goosemodScope.settings.items.indexOf(goosemodScope.settings.items.find(x => x[1] === 'Change Log')), gmSettings.get().separators ? 2 : 1); + goosemodScope.settings.items.splice(goosemodScope.settings.items.indexOf(goosemodScope.settings.items.find(x => x[1] === 'Change Log')), gmSettings.separators ? 2 : 1); } await goosemodScope.settings.reopenSettings(); @@ -46,8 +46,8 @@ export default (goosemodScope, gmSettings, Items) => { case 'separators': { if (value) { - if (!gmSettings.get().home) goosemod.settings.items.splice(2, 0, ['separator']); - if (gmSettings.get().changelog) goosemod.settings.items.splice(4, 0, ['separator']); + if (!gmSettings.home) goosemod.settings.items.splice(2, 0, ['separator']); + if (gmSettings.changelog) goosemod.settings.items.splice(4, 0, ['separator']); } else { let main = true; @@ -71,7 +71,7 @@ export default (goosemodScope, gmSettings, Items) => { } } - gmSettings.set(key, value); + gmSettings[key] = value; }; const refreshPrompt = async () => { @@ -95,7 +95,7 @@ export default (goosemodScope, gmSettings, Items) => { subtext: '#settings.items.goosemod_change_log.note#', onToggle: (c) => changeSetting('changelog', c), - isToggled: () => gmSettings.get().changelog + isToggled: () => gmSettings.changelog }, { @@ -105,7 +105,7 @@ export default (goosemodScope, gmSettings, Items) => { subtext: '#settings.items.main_separators.note#', onToggle: (c) => changeSetting('separators', c), - isToggled: () => gmSettings.get().separators + isToggled: () => gmSettings.separators }, { @@ -118,7 +118,7 @@ export default (goosemodScope, gmSettings, Items) => { changeSetting('home', c); refreshPrompt(); }, - isToggled: () => gmSettings.get().home + isToggled: () => gmSettings.home }, { @@ -133,7 +133,7 @@ export default (goosemodScope, gmSettings, Items) => { subtext: '#settings.items.auto_update.note#', onToggle: (c) => changeSetting('autoupdate', c), - isToggled: () => gmSettings.get().autoupdate + isToggled: () => gmSettings.autoupdate }, { @@ -143,7 +143,7 @@ export default (goosemodScope, gmSettings, Items) => { subtext: 'Shows a toast each time a new module is added to the Store', // todo: i18n onToggle: (c) => changeSetting('newModuleNotifications', c), - isToggled: () => gmSettings.get().newModuleNotifications + isToggled: () => gmSettings.newModuleNotifications }, { @@ -158,7 +158,7 @@ export default (goosemodScope, gmSettings, Items) => { subtext: '#settings.items.goosemod_badges.note#', onToggle: (c) => changeSetting('gmBadges', c), - isToggled: () => gmSettings.get().gmBadges + isToggled: () => gmSettings.gmBadges }, { @@ -319,7 +319,7 @@ export default (goosemodScope, gmSettings, Items) => { onToggle: (c) => { changeSetting('placeholderimage', c); }, - isToggled: () => gmSettings.get().placeholderimage + isToggled: () => gmSettings.placeholderimage }, { @@ -331,7 +331,7 @@ export default (goosemodScope, gmSettings, Items) => { onToggle: (c) => { changeSetting('collapsiblehome', c); }, - isToggled: () => gmSettings.get().collapsiblehome + isToggled: () => gmSettings.collapsiblehome }, { @@ -372,7 +372,7 @@ export default (goosemodScope, gmSettings, Items) => { changeSetting('attrs', c); refreshPrompt(); }, - isToggled: () => gmSettings.get().attrs + isToggled: () => gmSettings.attrs }, { @@ -386,7 +386,7 @@ export default (goosemodScope, gmSettings, Items) => { changeSetting('snippets', c); refreshPrompt(); }, - isToggled: () => gmSettings.get().snippets + isToggled: () => gmSettings.snippets }, { @@ -400,7 +400,18 @@ export default (goosemodScope, gmSettings, Items) => { changeSetting('allThemeSettings', c); refreshPrompt(); }, - isToggled: () => gmSettings.get().allThemeSettings + isToggled: () => gmSettings.allThemeSettings + }, + + { + type: 'toggle', + + experimental: true, + text: 'Patcher > Username | Next', + subtext: 'Potenetial next version of Username API: Inner patching of MessageHeader for going into Username-likely component', + + onToggle: (c) => gmSettings.username_next = c, + isToggled: () => gmSettings.username_next }, /* { @@ -457,13 +468,13 @@ export default (goosemodScope, gmSettings, Items) => { subtext: 'Shows some debug toasts on some events', onToggle: (c) => changeSetting('debugToasts', c), - isToggled: () => gmSettings.get().debugToasts + isToggled: () => gmSettings.debugToasts }, */ { type: 'gm-footer' } ]); - if (gmSettings.get().separators && !gmSettings.get().home) goosemodScope.settings.createSeparator(); + if (gmSettings.separators && !gmSettings.home) goosemodScope.settings.createSeparator(); let sortedVal = '#store.options.sort.stars#'; let authorVal = '#store.options.author.all#'; @@ -764,8 +775,8 @@ export default (goosemodScope, gmSettings, Items) => { } ]); - if (gmSettings.get().changelog) { - if (gmSettings.get().separators) goosemodScope.settings.createSeparator(); + if (gmSettings.changelog) { + if (gmSettings.separators) goosemodScope.settings.createSeparator(); goosemodScope.settings.createItem('#terms.changelog#', [""], async () => { GoosemodChangelog.show(); diff --git a/src/ui/settings/home/index.js b/src/ui/settings/home/index.js index 67e7d6d8..986d7adb 100644 --- a/src/ui/settings/home/index.js +++ b/src/ui/settings/home/index.js @@ -79,7 +79,7 @@ export default async (goosemodScope) => { return (parentEl.classList.contains(className) && parentEl) || findClassInParentTree(parentEl, className, depth + 1); }; - let expanded = goosemodScope.settings.gmSettings.get().collapsiblehome ? (goosemod.storage.get('goosemodHomeExpanded') || true) : true; + let expanded = goosemodScope.settings.gmSettings.collapsiblehome ? (goosemod.storage.get('goosemodHomeExpanded') || true) : true; let settings = { plugins: goosemodScope.settings.items.find((x) => x[1] === '#terms.store.plugins#'), @@ -160,7 +160,7 @@ export default async (goosemodScope) => { } }; - const snippetsEnabled = goosemodScope.settings.gmSettings.get().snippets; + const snippetsEnabled = goosemodScope.settings.gmSettings.snippets; goosemodScope.settingsUninjects.push(goosemodScope.patcher.patch(ConnectedPrivateChannelsList, 'default', (_args, res) => { if (res.props.children.slice(3).find((x) => x?.toString()?.includes('GooseMod'))) return; @@ -189,7 +189,7 @@ export default async (goosemodScope) => { className: HeaderClasses.headerText }, 'GooseMod'), - goosemodScope.settings.gmSettings.get().collapsiblehome ? React.createElement('div', { + goosemodScope.settings.gmSettings.collapsiblehome ? React.createElement('div', { className: `${HeaderClasses.privateChannelRecipientsInviteButtonIcon} ${IconClasses.iconWrapper} ${IconClasses.clickable}`, style: { diff --git a/src/ui/settings/index.js b/src/ui/settings/index.js index 96ac2306..31c4ff2c 100644 --- a/src/ui/settings/index.js +++ b/src/ui/settings/index.js @@ -1,6 +1,6 @@ import sleep from '../../util/sleep'; -import * as GMSettings from '../../gmSettings'; +import GMSettings from '../../gmSettings'; export const gmSettings = GMSettings; import addToHome from './home/index'; @@ -149,8 +149,8 @@ export const makeGooseModSettings = () => { addBaseItems(goosemodScope, gmSettings, Items); addToSettingsSidebar(goosemodScope, gmSettings); - addToContextMenu(goosemodScope, gmSettings.get().home); - if (gmSettings.get().home) addToHome(goosemodScope); + addToContextMenu(goosemodScope, gmSettings.home); + if (gmSettings.home) addToHome(goosemodScope); loadColorPicker(); }; diff --git a/src/ui/settings/items/card.js b/src/ui/settings/items/card.js index 1da7a5fb..1311b918 100644 --- a/src/ui/settings/items/card.js +++ b/src/ui/settings/items/card.js @@ -104,7 +104,7 @@ return class Card extends React.PureComponent { React.createElement(SmallMediaCarousel, { autoplayInterval: 0, - items: (this.props.images?.[0] ? this.props.images : [ (goosemod.settings.gmSettings.get().placeholderimage ? '/assets/2366391afb15ed6c2a019a0c0caa0797.svg' : 'data:image/gif;base64,R0lGODlhAQABAAAAACwAAAAAAQABAAA=') ]).map((x) => ({ type: 1, src: x })), + items: (this.props.images?.[0] ? this.props.images : [ (goosemod.settings.gmSettings.placeholderimage ? '/assets/2366391afb15ed6c2a019a0c0caa0797.svg' : 'data:image/gif;base64,R0lGODlhAQABAAAAACwAAAAAAQABAAA=') ]).map((x) => ({ type: 1, src: x })), paused: true, onCurrentItemClick: () => { diff --git a/src/ui/settings/settingsSidebar.js b/src/ui/settings/settingsSidebar.js index 2b46c9fa..698f1e37 100644 --- a/src/ui/settings/settingsSidebar.js +++ b/src/ui/settings/settingsSidebar.js @@ -13,7 +13,7 @@ export default (goosemodScope, gmSettings) => { sections.splice( sections.indexOf(logout), 0, - ...goosemodScope.settings.items.filter((x) => (gmSettings.get().home ? x[1] !== '#terms.store.plugins#' && x[1] !== '#terms.store.themes#' && x[1] !== '#terms.store.snippets#': true) && (!gmSettings.get().snippets ? x[1] !== '#terms.store.snippets#' : true)).map((i) => { + ...goosemodScope.settings.items.filter((x) => (gmSettings.home ? x[1] !== '#terms.store.plugins#' && x[1] !== '#terms.store.themes#' && x[1] !== '#terms.store.snippets#': true) && (!gmSettings.snippets ? x[1] !== '#terms.store.snippets#' : true)).map((i) => { switch (i[0]) { case 'item': let id = i[1]; diff --git a/src/ui/toast.js b/src/ui/toast.js index 516276a1..e42cfd67 100644 --- a/src/ui/toast.js +++ b/src/ui/toast.js @@ -90,7 +90,7 @@ document.head.appendChild(styleSheet); export default (text, options = {}) => { if (options?.type?.startsWith('debug')) { - if (!goosemod.settings.gmSettings.get().debugToasts) return; + if (!goosemod.settings.gmSettings.debugToasts) return; options.type = options.type.replace('debug', ''); } diff --git a/src/util/patcher/username.js b/src/util/patcher/username.js index 70f0e331..8837372d 100644 --- a/src/util/patcher/username.js +++ b/src/util/patcher/username.js @@ -8,33 +8,35 @@ export const setThisScope = (scope) => { export const patch = (generateElement) => { const MessageHeader = goosemodScope.webpackModules.find((x) => x.default && !x.default.displayName && x.default.toString().indexOf('headerText') > -1); - - /* Advanced-ish patching inside of Username but glitched and overcomplicated - return PatcherBase.patch(MessageHeader, 'default', (_args, res) => { - const header = goosemod.reactUtils.findInReactTree(res, el => Array.isArray(el?.props?.children) && el.props.children.find(c => c?.props?.message)); - const [ Username ] = header.props.children; - - PatcherBase.patch(Username, 'type', (_args, res) => { - const [ , Inner ] = res.props.children; - - PatcherBase.patch(Inner.props, 'children', (_args, res) => { - res.props.children = [ - res.props.children, - generateElement(Username.props) - ]; - - return res; + + // Advanced-ish patching inside of Username but glitched and overcomplicated + if (goosemod.settings.gmSettings.username_next) { + return PatcherBase.patch(MessageHeader, 'default', (_args, res) => { + const header = goosemod.reactUtils.findInReactTree(res, el => Array.isArray(el?.props?.children) && el.props.children.find(c => c?.props?.message)); + const [ Username ] = header.props.children; + + PatcherBase.patch(Username, 'type', (_args, res) => { + const [ , Inner ] = res.props.children; + + PatcherBase.patch(Inner.props, 'children', (_args, res) => { + res.props.children = [ + res.props.children, + generateElement(Username.props) + ]; + + return res; + }); }); + + return res; }); - - return res; - }); */ - + } + return PatcherBase.patch(MessageHeader, 'default', (_args, res) => { const header = goosemod.reactUtils.findInReactTree(res, el => Array.isArray(el?.props?.children) && el.props.children.find(c => c?.props?.message)); - + header.props.children.push(generateElement(header.props.children[0].props)); - + return res; }); };