Allow mapMangledModule to be destructured again

This commit is contained in:
Nuckyz 2024-06-27 18:39:57 -03:00
parent 1511260666
commit 718ebd56ac
No known key found for this signature in database
GPG key ID: 440BF8296E1C4AD9
8 changed files with 87 additions and 41 deletions

View file

@ -58,23 +58,32 @@ async function runReporter() {
if (findResult != null) { if (findResult != null) {
if (findResult.$$vencordCallbackCalled != null && findResult.$$vencordCallbackCalled()) { if (findResult.$$vencordCallbackCalled != null && findResult.$$vencordCallbackCalled()) {
result = findResult; result = findResult;
break;
} }
if (findResult[SYM_PROXY_INNER_GET] != null) { if (findResult[SYM_PROXY_INNER_GET] != null) {
result = findResult[SYM_PROXY_INNER_VALUE]; result = findResult[SYM_PROXY_INNER_VALUE];
if (result != null && searchType === "mapMangledModule") { break;
for (const innerMap in result) {
if (result[innerMap][SYM_PROXY_INNER_GET] != null) {
throw new Error("Webpack Find Fail");
}
}
}
} }
if (findResult[SYM_LAZY_COMPONENT_INNER] != null) { if (findResult[SYM_LAZY_COMPONENT_INNER] != null) {
result = findResult[SYM_LAZY_COMPONENT_INNER](); result = findResult[SYM_LAZY_COMPONENT_INNER]();
break;
} }
if (searchType === "mapMangledModule") {
result = findResult;
for (const innerMap in result) {
if (result[innerMap][SYM_PROXY_INNER_GET] != null && result[innerMap][SYM_PROXY_INNER_VALUE] == null) {
throw new Error("Webpack Find Fail");
}
}
}
// This can happen if a `find` was immediately found
result = findResult;
} }
break; break;
@ -125,7 +134,7 @@ async function runReporter() {
const [code, mappers] = parsedArgs; const [code, mappers] = parsedArgs;
const parsedFailedMappers = Object.entries<any>(mappers) const parsedFailedMappers = Object.entries<any>(mappers)
.filter(([key]) => result == null || result[key][SYM_PROXY_INNER_GET] != null) .filter(([key]) => result == null || (result[key][SYM_PROXY_INNER_GET] != null && result[key][SYM_PROXY_INNER_VALUE] == null))
.map(([key, filter]) => { .map(([key, filter]) => {
let parsedFilter: string; let parsedFilter: string;

View file

@ -9,7 +9,7 @@ import { Link } from "@components/Link";
import { openInviteModal } from "@utils/discord"; import { openInviteModal } from "@utils/discord";
import { Margins } from "@utils/margins"; import { Margins } from "@utils/margins";
import { closeAllModals, ModalCloseButton, ModalContent, ModalFooter, ModalHeader, ModalProps, ModalRoot, ModalSize, openModal } from "@utils/modal"; import { closeAllModals, ModalCloseButton, ModalContent, ModalFooter, ModalHeader, ModalProps, ModalRoot, ModalSize, openModal } from "@utils/modal";
import { findByProps, findComponentByCode } from "@webpack"; import { filters, findComponentByCode, mapMangledModule } from "@webpack";
import { Button, FluxDispatcher, Forms, GuildStore, NavigationRouter, Text, TextInput, useEffect, useMemo, UserStore, useState } from "@webpack/common"; import { Button, FluxDispatcher, Forms, GuildStore, NavigationRouter, Text, TextInput, useEffect, useMemo, UserStore, useState } from "@webpack/common";
import { GUILD_ID, INVITE_KEY, RAW_SKU_ID } from "../../lib/constants"; import { GUILD_ID, INVITE_KEY, RAW_SKU_ID } from "../../lib/constants";
@ -19,7 +19,10 @@ import { AvatarDecorationModalPreview } from "../components";
const FileUpload = findComponentByCode("fileUploadInput,"); const FileUpload = findComponentByCode("fileUploadInput,");
const HelpMessage = findComponentByCode(".iconDiv,", "messageType"); const HelpMessage = findComponentByCode(".iconDiv,", "messageType");
const HelpMessageTypes = findByProps("POSITIVE", "WARNING");
const { HelpMessageTypes } = mapMangledModule('POSITIVE=3]="POSITIVE', {
HelpMessageTypes: filters.byProps("POSITIVE", "WARNING"),
});
function useObjectURL(object: Blob | MediaSource | null) { function useObjectURL(object: Blob | MediaSource | null) {
const [url, setUrl] = useState<string | null>(null); const [url, setUrl] = useState<string | null>(null);

View file

@ -29,7 +29,7 @@ import { Menu } from "@webpack/common";
import { Guild } from "discord-types/general"; import { Guild } from "discord-types/general";
const updateGuildNotificationSettings = findByPropsAndExtract("updateGuildNotificationSettings"); const updateGuildNotificationSettings = findByPropsAndExtract("updateGuildNotificationSettings");
const OnboardingChannelUtils = mapMangledModule(".onboardExistingMember(", { const { toggleShowAllChannels } = mapMangledModule(".onboardExistingMember(", {
toggleShowAllChannels: m => { toggleShowAllChannels: m => {
const s = String(m); const s = String(m);
return s.length < 100 && !s.includes("onboardExistingMember") && !s.includes("getOptedInChannels"); return s.length < 100 && !s.includes("onboardExistingMember") && !s.includes("getOptedInChannels");
@ -111,7 +111,7 @@ function applyDefaultSettings(guildId: string | null) {
}); });
} }
if (settings.store.showAllChannels && isOptInEnabledForGuild(guildId)) { if (settings.store.showAllChannels && isOptInEnabledForGuild(guildId)) {
OnboardingChannelUtils.toggleShowAllChannels(guildId); toggleShowAllChannels(guildId);
} }
} }

View file

@ -14,7 +14,7 @@ import { Timestamp } from "@webpack/common";
import type { Message } from "discord-types/general"; import type { Message } from "discord-types/general";
import type { HTMLAttributes } from "react"; import type { HTMLAttributes } from "react";
const DateFormatUtils = mapMangledModule("millisecondsInUnit:", { const { calendarFormat, dateFormat, isSameDay } = mapMangledModule("millisecondsInUnit:", {
calendarFormat: filters.byCode("sameElse"), calendarFormat: filters.byCode("sameElse"),
dateFormat: filters.byCode(':").concat'), dateFormat: filters.byCode(':").concat'),
isSameDay: filters.byCode("Math.abs(+"), isSameDay: filters.byCode("Math.abs(+"),
@ -46,14 +46,14 @@ function ReplyTimestamp({
return ( return (
<Timestamp <Timestamp
className="vc-reply-timestamp" className="vc-reply-timestamp"
compact={DateFormatUtils.isSameDay(refTimestamp, baseTimestamp)} compact={isSameDay(refTimestamp, baseTimestamp)}
timestamp={refTimestamp} timestamp={refTimestamp}
isInline={false} isInline={false}
> >
<Sep>[</Sep> <Sep>[</Sep>
{DateFormatUtils.isSameDay(refTimestamp, baseTimestamp) {isSameDay(refTimestamp, baseTimestamp)
? DateFormatUtils.dateFormat(refTimestamp, "LT") ? dateFormat(refTimestamp, "LT")
: DateFormatUtils.calendarFormat(refTimestamp) : calendarFormat(refTimestamp)
} }
<Sep>]</Sep> <Sep>]</Sep>
</Timestamp> </Timestamp>

View file

@ -22,12 +22,11 @@ import { isNonNullish } from "@utils/guards";
import { sleep } from "@utils/misc"; import { sleep } from "@utils/misc";
import { Queue } from "@utils/Queue"; import { Queue } from "@utils/Queue";
import definePlugin from "@utils/types"; import definePlugin from "@utils/types";
import { webpackDependantLazy } from "@webpack";
import { Constants, FluxDispatcher, RestAPI, UserProfileStore, UserStore, useState } from "@webpack/common"; import { Constants, FluxDispatcher, RestAPI, UserProfileStore, UserStore, useState } from "@webpack/common";
import { type ComponentType, type ReactNode } from "react"; import { type ComponentType, type ReactNode } from "react";
// LYING to the type checker here // LYING to the type checker here
const UserFlags = webpackDependantLazy(() => Constants.UserFlags as Record<string, number>); const UserFlags = Constants.UserFlags as Record<string, number>;
const badges: Record<string, ProfileBadge> = { const badges: Record<string, ProfileBadge> = {
active_developer: { id: "active_developer", description: "Active Developer", icon: "6bdc42827a38498929a4920da12695d9", link: "https://support-dev.discord.com/hc/en-us/articles/10113997751447" }, active_developer: { id: "active_developer", description: "Active Developer", icon: "6bdc42827a38498929a4920da12695d9", link: "https://support-dev.discord.com/hc/en-us/articles/10113997751447" },
bug_hunter_level_1: { id: "bug_hunter_level_1", description: "Discord Bug Hunter", icon: "2717692c7dca7289b35297368a940dd0", link: "https://support.discord.com/hc/en-us/articles/360046057772-Discord-Bugs" }, bug_hunter_level_1: { id: "bug_hunter_level_1", description: "Discord Bug Hunter", icon: "2717692c7dca7289b35297368a940dd0", link: "https://support.discord.com/hc/en-us/articles/360046057772-Discord-Bugs" },

View file

@ -122,3 +122,22 @@ export function proxyLazy<T = AnyObject>(factory: () => T, attempts = 5): ProxyL
return proxy; return proxy;
} }
/**
* A string which returns the factory result every time its value is accessed.
*
* @param factory Factory returning the string to use as the value
*/
export function lazyString<T extends string>(factory: () => T) {
const descriptor: PropertyDescriptor = {
configurable: true,
enumerable: false,
writable: false,
value: factory
};
return Object.create(String.prototype, {
toString: descriptor,
valueOf: descriptor
});
}

View file

@ -4,7 +4,7 @@
* SPDX-License-Identifier: GPL-3.0-or-later * SPDX-License-Identifier: GPL-3.0-or-later
*/ */
import { makeLazy, proxyLazy } from "@utils/lazy"; import { lazyString, makeLazy, proxyLazy } from "@utils/lazy";
import { LazyComponent, LazyComponentType, SYM_LAZY_COMPONENT_INNER } from "@utils/lazyReact"; import { LazyComponent, LazyComponentType, SYM_LAZY_COMPONENT_INNER } from "@utils/lazyReact";
import { Logger } from "@utils/Logger"; import { Logger } from "@utils/Logger";
import { canonicalizeMatch } from "@utils/patches"; import { canonicalizeMatch } from "@utils/patches";
@ -430,37 +430,53 @@ export function findByFactoryCode<T = AnyObject>(...code: string[] | [...string[
* @returns Unmangled exports as specified in mappers * @returns Unmangled exports as specified in mappers
*/ */
export function mapMangledModule<S extends PropertyKey>(code: string | string[], mappers: Record<S, FilterFn>) { export function mapMangledModule<S extends PropertyKey>(code: string | string[], mappers: Record<S, FilterFn>) {
const result = find<Record<S, any>>(filters.byFactoryCode(...Array.isArray(code) ? code : [code]), exports => {
const mapping = {} as Record<S, any>; const mapping = {} as Record<S, any>;
const setters = {} as Record<S, (innerValue: any) => void>;
for (const newName in mappers) {
// Wrapper to select whether the parent factory filter or child mapper filter failed when the error is thrown
const errorMsgWrapper = lazyString(() => `Webpack mapMangledModule ${callbackCalled ? "mapper" : "factory"} filter matched no module. Filter: ${printFilter(callbackCalled ? mappers[newName] : factoryFilter)}`);
const [proxy, setInnerValue] = proxyInner(errorMsgWrapper, "Webpack find with proxy called on a primitive value.");
mapping[newName] = proxy;
setters[newName] = setInnerValue;
}
const factoryFilter = filters.byFactoryCode(...Array.isArray(code) ? code : [code]);
let callbackCalled = false;
waitFor(factoryFilter, exports => {
callbackCalled = true;
for (const exportKey in exports) {
const exportValue = exports[exportKey];
if (exportValue == null) continue;
outer:
for (const newName in mappers) { for (const newName in mappers) {
const filter = mappers[newName]; const filter = mappers[newName];
if (typeof exports === "object") { if (filter(exportValue)) {
for (const exportKey in exports) { setters[newName](exportValue);
const exportValue = exports[exportKey];
if (exportValue != null && filter(exportValue)) {
mapping[newName] = exportValue;
continue outer;
} }
} }
} }
const [proxy] = proxyInner(`Webpack mapMangledModule mapper filter matched no module. Filter: ${printFilter(filter)}`, "Webpack find with proxy called on a primitive value.");
// Use the proxy to throw errors because no export matched the filter
mapping[newName] = proxy;
}
return mapping;
}, { isIndirect: true }); }, { isIndirect: true });
if (IS_REPORTER) { if (IS_REPORTER) {
webpackSearchHistory.push(["mapMangledModule", [result, code, mappers]]); webpackSearchHistory.push(["mapMangledModule", [mapping, code, mappers]]);
} }
return result; if (callbackCalled) {
for (const innerMap in mapping) {
const innerValue = mapping[innerMap];
if (innerValue[SYM_PROXY_INNER_VALUE] != null) {
mapping[innerMap] = innerValue[SYM_PROXY_INNER_VALUE];
}
}
}
return mapping;
} }
/** /**

View file

@ -129,7 +129,7 @@ export const Clipboard: t.Clipboard = mapMangledModule('queryCommandEnabled("cop
SUPPORTS_COPY: e => typeof e === "boolean" SUPPORTS_COPY: e => typeof e === "boolean"
}); });
export const NavigationRouter: t.NavigationRouter = mapMangledModule("Transitioning to ", { export const NavigationRouter: t.NavigationRouter = mapMangledModule("aTransitioning to ", {
transitionTo: filters.byCode("transitionTo -"), transitionTo: filters.byCode("transitionTo -"),
transitionToGuild: filters.byCode("transitionToGuild -"), transitionToGuild: filters.byCode("transitionToGuild -"),
back: filters.byCode("goBack()"), back: filters.byCode("goBack()"),