Update Companion

This commit is contained in:
thororen1234 2025-02-11 13:56:19 -05:00
parent f5cad98f78
commit c4c5077ccf
No known key found for this signature in database
4 changed files with 71 additions and 34 deletions

View file

@ -14,8 +14,9 @@ import { reporterData } from "debug/reporterData";
import { Settings } from "Vencord";
import { logger, PORT, settings } from ".";
import { Recieve, Send } from "./types";
import { extractModule, extractOrThrow, findModuleId, mkRegexFind, parseNode, toggleEnabled, } from "./util";
import { Recieve } from "./types";
import { FullOutgoingMessage, OutgoingMessage } from "./types/send";
import { extractModule, extractOrThrow, findModuleId, getModulePatchedBy, mkRegexFind, parseNode, toggleEnabled, } from "./util";
export function stopWs() {
socket?.close(1000, "Plugin Stopped");
@ -29,7 +30,7 @@ export function initWs(isManual = false) {
let hasErrored = false;
const ws = socket = new WebSocket(`ws://localhost:${PORT}`);
function replyData(data: Send.OutgoingMessage) {
function replyData(data: OutgoingMessage) {
ws.send(JSON.stringify(data));
}
@ -122,20 +123,22 @@ export function initWs(isManual = false) {
* @param error the error to reply with. if there is no error, the reply is a sucess
*/
function reply(error?: string) {
const data = { nonce: d.nonce, ok: !error } as Record<string, unknown>;
if (error) data.error = error;
ws.send(JSON.stringify(data));
const toSend = { nonce: d.nonce, ok: !error } as Record<string, unknown>;
if (error) toSend.error = error;
logger.debug("Replying with:", toSend);
ws.send(JSON.stringify(toSend));
}
function replyData(data: Send.OutgoingMessage) {
const toSend: Send.FullOutgoingMessage = {
function replyData(data: OutgoingMessage) {
const toSend: FullOutgoingMessage = {
...data,
nonce: d.nonce
};
// data.nonce = d.nonce;
logger.debug(`Replying with data: ${toSend}`);
ws.send(JSON.stringify(toSend));
}
logger.debug(`Received Message: ${d.type}`, "\n", d.data);
switch (d.type) {
case "disable": {
const m = d.data;
@ -146,6 +149,7 @@ export function initWs(isManual = false) {
}
case "rawId": {
const m = d.data;
logger.warn("Deprecated rawId message received, use extract instead");
replyData({
type: "rawId",
ok: true,
@ -166,7 +170,8 @@ export function initWs(isManual = false) {
data: {
patched: extractOrThrow(m.idOrSearch),
source: extractModule(m.idOrSearch, false),
moduleNumber: m.idOrSearch
moduleNumber: m.idOrSearch,
patchedBy: getModulePatchedBy(m.idOrSearch, true)
},
});
break;
@ -186,7 +191,8 @@ export function initWs(isManual = false) {
data: {
patched: p,
source: p2,
moduleNumber: moduleId
moduleNumber: moduleId,
patchedBy: getModulePatchedBy(moduleId, true)
},
});
break;
@ -217,6 +223,7 @@ export function initWs(isManual = false) {
data: {
module: extractModule(m.idOrSearch, m.usePatched ?? undefined),
moduleNumber: m.idOrSearch,
patchedBy: getModulePatchedBy(m.idOrSearch, m.usePatched ?? undefined)
},
});
@ -234,7 +241,8 @@ export function initWs(isManual = false) {
ok: true,
data: {
module: extractModule(moduleId, m.usePatched ?? undefined),
moduleNumber: moduleId
moduleNumber: moduleId,
patchedBy: getModulePatchedBy(moduleId, m.usePatched ?? undefined)
},
});
break;
@ -283,7 +291,8 @@ export function initWs(isManual = false) {
data: {
module: foundFind,
find: true,
moduleNumber: +findModuleId([foundFind])
moduleNumber: +findModuleId([foundFind]),
patchedBy: getModulePatchedBy(foundFind)
},
});
} catch (err) {

View file

@ -73,7 +73,13 @@ export type DisablePlugin = {
};
};
/**
* @deprecated use {@link ExtractModule} instead
*/
export type RawId = {
/**
* @deprecated use {@link ExtractModule} instead
*/
type: "rawId";
data: {
id: number;

View file

@ -23,6 +23,14 @@ export type Nonce = {
};
export type ModuleResult = {
moduleNumber: number;
/**
* a list of plugins that patched this module, if it was patched, otherwise an empty array
*
* if the module was patched, but the returned module is the original, they array will still be empty
*
* if {@link ExtractModule.data|ExtractModule.data.find} is true, this will be a list of what patched the entire module (not just the part that was found)
*/
patchedBy: string[];
};
// #region valid payloads
@ -56,7 +64,13 @@ export type ModuleList = {
modules: string[];
};
};
/**
* @deprecated use extractModule with usePatched instead
*/
export type RawId = {
/**
* @deprecated use extractModule with usePatched instead
*/
type: "rawId";
data: string;
};

View file

@ -9,29 +9,22 @@ import { Settings } from "@api/Settings";
import { canonicalizeMatch } from "@utils/patches";
import { CodeFilter, stringMatches, wreq } from "@webpack";
import { Toasts } from "@webpack/common";
import { AnyWebpackRequire } from "webpack";
import { settings as companionSettings } from ".";
import { Recieve } from "./types";
import { logger, settings as companionSettings } from ".";
import { FindNode } from "./types/recieve";
const SYM_PATCHED_SOURCE = Symbol("WebpackPatcher.patchedSource");
function getFactoryPatchedSource(id: PropertyKey, webpackRequire = wreq as AnyWebpackRequire) {
return webpackRequire.m[id]?.[SYM_PATCHED_SOURCE];
}
const { WebpackPatcher: { getFactoryPatchedBy, getFactoryPatchedSource } } = require("Vencord") as typeof import("Vencord");
/**
* extracts the patched module, if there is no patched module, throws an error
* @param id module id
*/
export function extractOrThrow(id: number): string {
const module = wreq.m[id];
export function extractOrThrow(id: PropertyKey): string {
const patchedSource = getFactoryPatchedSource(id);
if (!patchedSource)
throw new Error("No patched module found for module id " + id);
throw new Error(`No patched module found for module id ${String(id)}`);
return patchedSource;
}
/**
* attempts to extract the module, throws if not found
*
@ -40,16 +33,31 @@ export function extractOrThrow(id: number): string {
* @param id module id
* @param patched return the patched module
*/
export function extractModule(id: number, patched = companionSettings.store.usePatchedModule): string {
const module = wreq.m[id];
if (!module)
throw new Error("No module found for module id:" + id);
const original = module.toString();
const patchedSource = getFactoryPatchedSource(id);
return patched ? patchedSource ?? original : original;
export function extractModule(id: PropertyKey, patched = companionSettings.store.usePatchedModule): string {
if (patched) {
try {
return extractOrThrow(id);
} catch (e) {
logger.debug(e);
}
}
return extractUnpatchedModule(id);
}
function extractUnpatchedModule(id: PropertyKey): string {
if (!wreq.m[id]) {
throw new Error(`Module not found for id: ${String(id)}`);
}
return `// Webpack Module ${String(id)} - Patched by\n0,${wreq.m[id]}\n//# sourceURL=WebpackModule${String(id)}`;
}
export function parseNode(node: Recieve.FindNode): any {
/**
*
* @param usePatched if false, always returns `[]`, otherwise uses the same setting as {@link extractModule}
*/
export function getModulePatchedBy(id: PropertyKey, usePatched = companionSettings.store.usePatchedModule): string[] {
return [...usePatched && getFactoryPatchedBy(id) || []];
}
export function parseNode(node: FindNode): any {
switch (node.type) {
case "string":
return node.value;