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

View file

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

View file

@ -23,6 +23,14 @@ export type Nonce = {
}; };
export type ModuleResult = { export type ModuleResult = {
moduleNumber: number; 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 // #region valid payloads
@ -56,7 +64,13 @@ export type ModuleList = {
modules: string[]; modules: string[];
}; };
}; };
/**
* @deprecated use extractModule with usePatched instead
*/
export type RawId = { export type RawId = {
/**
* @deprecated use extractModule with usePatched instead
*/
type: "rawId"; type: "rawId";
data: string; data: string;
}; };

View file

@ -9,29 +9,22 @@ import { Settings } from "@api/Settings";
import { canonicalizeMatch } from "@utils/patches"; import { canonicalizeMatch } from "@utils/patches";
import { CodeFilter, stringMatches, wreq } from "@webpack"; import { CodeFilter, stringMatches, wreq } from "@webpack";
import { Toasts } from "@webpack/common"; import { Toasts } from "@webpack/common";
import { AnyWebpackRequire } from "webpack";
import { settings as companionSettings } from "."; import { logger, settings as companionSettings } from ".";
import { Recieve } from "./types"; import { FindNode } from "./types/recieve";
const SYM_PATCHED_SOURCE = Symbol("WebpackPatcher.patchedSource"); const { WebpackPatcher: { getFactoryPatchedBy, getFactoryPatchedSource } } = require("Vencord") as typeof import("Vencord");
function getFactoryPatchedSource(id: PropertyKey, webpackRequire = wreq as AnyWebpackRequire) {
return webpackRequire.m[id]?.[SYM_PATCHED_SOURCE];
}
/** /**
* extracts the patched module, if there is no patched module, throws an error * extracts the patched module, if there is no patched module, throws an error
* @param id module id * @param id module id
*/ */
export function extractOrThrow(id: number): string { export function extractOrThrow(id: PropertyKey): string {
const module = wreq.m[id];
const patchedSource = getFactoryPatchedSource(id); const patchedSource = getFactoryPatchedSource(id);
if (!patchedSource) 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; return patchedSource;
} }
/** /**
* attempts to extract the module, throws if not found * attempts to extract the module, throws if not found
* *
@ -40,16 +33,31 @@ export function extractOrThrow(id: number): string {
* @param id module id * @param id module id
* @param patched return the patched module * @param patched return the patched module
*/ */
export function extractModule(id: number, patched = companionSettings.store.usePatchedModule): string { export function extractModule(id: PropertyKey, patched = companionSettings.store.usePatchedModule): string {
const module = wreq.m[id]; if (patched) {
if (!module) try {
throw new Error("No module found for module id:" + id); return extractOrThrow(id);
const original = module.toString(); } catch (e) {
const patchedSource = getFactoryPatchedSource(id); logger.debug(e);
return patched ? patchedSource ?? original : original; }
}
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) { switch (node.type) {
case "string": case "string":
return node.value; return node.value;