mirror of
https://github.com/Equicord/Equicord.git
synced 2025-06-18 19:07:08 -04:00
Rewrite WebpackPatcher to support new features (#3157)
This commit is contained in:
parent
fcf8690d26
commit
e8639e2e16
22 changed files with 896 additions and 358 deletions
|
@ -16,7 +16,6 @@
|
|||
* along with this program. If not, see <https://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
export const WEBPACK_CHUNK = "webpackChunkdiscord_app";
|
||||
export const REACT_GLOBAL = "Vencord.Webpack.Common.React";
|
||||
export const VENBOT_USER_ID = "1017176847865352332";
|
||||
export const VENCORD_GUILD_ID = "1015060230222131221";
|
||||
|
|
|
@ -100,6 +100,11 @@ export function pluralise(amount: number, singular: string, plural = singular +
|
|||
return amount === 1 ? `${amount} ${singular}` : `${amount} ${plural}`;
|
||||
}
|
||||
|
||||
export function interpolateIfDefined(strings: TemplateStringsArray, ...args: any[]) {
|
||||
if (args.some(arg => arg == null)) return "";
|
||||
return String.raw({ raw: strings }, ...args);
|
||||
}
|
||||
|
||||
export function tryOrElse<T>(func: () => T, fallback: T): T {
|
||||
try {
|
||||
const res = func();
|
||||
|
|
|
@ -41,16 +41,17 @@ export function canonicalizeMatch<T extends RegExp | string>(match: T): T {
|
|||
}
|
||||
|
||||
const canonSource = partialCanon.replaceAll("\\i", String.raw`(?:[A-Za-z_$][\w$]*)`);
|
||||
return new RegExp(canonSource, match.flags) as T;
|
||||
const canonRegex = new RegExp(canonSource, match.flags);
|
||||
canonRegex.toString = match.toString.bind(match);
|
||||
|
||||
return canonRegex as T;
|
||||
}
|
||||
|
||||
export function canonicalizeReplace<T extends string | ReplaceFn>(replace: T, pluginName: string): T {
|
||||
const self = `Vencord.Plugins.plugins[${JSON.stringify(pluginName)}]`;
|
||||
|
||||
export function canonicalizeReplace<T extends string | ReplaceFn>(replace: T, pluginPath: string): T {
|
||||
if (typeof replace !== "function")
|
||||
return replace.replaceAll("$self", self) as T;
|
||||
return replace.replaceAll("$self", pluginPath) as T;
|
||||
|
||||
return ((...args) => replace(...args).replaceAll("$self", self)) as T;
|
||||
return ((...args) => replace(...args).replaceAll("$self", pluginPath)) as T;
|
||||
}
|
||||
|
||||
export function canonicalizeDescriptor<T>(descriptor: TypedPropertyDescriptor<T>, canonicalize: (value: T) => T) {
|
||||
|
@ -65,12 +66,12 @@ export function canonicalizeDescriptor<T>(descriptor: TypedPropertyDescriptor<T>
|
|||
return descriptor;
|
||||
}
|
||||
|
||||
export function canonicalizeReplacement(replacement: Pick<PatchReplacement, "match" | "replace">, plugin: string) {
|
||||
export function canonicalizeReplacement(replacement: Pick<PatchReplacement, "match" | "replace">, pluginPath: string) {
|
||||
const descriptors = Object.getOwnPropertyDescriptors(replacement);
|
||||
descriptors.match = canonicalizeDescriptor(descriptors.match, canonicalizeMatch);
|
||||
descriptors.replace = canonicalizeDescriptor(
|
||||
descriptors.replace,
|
||||
replace => canonicalizeReplace(replace, plugin),
|
||||
replace => canonicalizeReplace(replace, pluginPath),
|
||||
);
|
||||
Object.defineProperties(replacement, descriptors);
|
||||
}
|
||||
|
|
|
@ -43,6 +43,10 @@ export interface PatchReplacement {
|
|||
replace: string | ReplaceFn;
|
||||
/** A function which returns whether this patch replacement should be applied */
|
||||
predicate?(): boolean;
|
||||
/** The minimum build number for this patch to be applied */
|
||||
fromBuild?: number;
|
||||
/** The maximum build number for this patch to be applied */
|
||||
toBuild?: number;
|
||||
}
|
||||
|
||||
export interface Patch {
|
||||
|
@ -59,6 +63,10 @@ export interface Patch {
|
|||
group?: boolean;
|
||||
/** A function which returns whether this patch should be applied */
|
||||
predicate?(): boolean;
|
||||
/** The minimum build number for this patch to be applied */
|
||||
fromBuild?: number;
|
||||
/** The maximum build number for this patch to be applied */
|
||||
toBuild?: number;
|
||||
}
|
||||
|
||||
export interface PluginAuthor {
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue