2023-03-10 18:25:49 -05:00
|
|
|
/*
|
|
|
|
* Vencord, a modification for Discord's desktop app
|
|
|
|
* Copyright (c) 2023 Vendicated and contributors
|
|
|
|
*
|
|
|
|
* This program is free software: you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation, either version 3 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with this program. If not, see <https://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
|
2023-03-19 03:53:00 -04:00
|
|
|
import { addContextMenuPatch, NavContextMenuPatchCallback, removeContextMenuPatch } from "@api/ContextMenu";
|
2023-03-10 18:25:49 -05:00
|
|
|
import { showNotification } from "@api/Notifications";
|
2023-03-21 02:07:16 -04:00
|
|
|
import { definePluginSettings } from "@api/settings";
|
2023-03-10 18:25:49 -05:00
|
|
|
import { Devs } from "@utils/constants";
|
|
|
|
import Logger from "@utils/Logger";
|
|
|
|
import { canonicalizeMatch, canonicalizeReplace } from "@utils/patches";
|
2023-03-21 02:07:16 -04:00
|
|
|
import definePlugin, { OptionType } from "@utils/types";
|
2023-03-10 18:25:49 -05:00
|
|
|
import { filters, findAll, search } from "@webpack";
|
|
|
|
import { Menu } from "@webpack/common";
|
|
|
|
|
|
|
|
const PORT = 8485;
|
|
|
|
const NAV_ID = "dev-companion-reconnect";
|
|
|
|
|
|
|
|
const logger = new Logger("DevCompanion");
|
|
|
|
|
|
|
|
let socket: WebSocket | undefined;
|
|
|
|
|
|
|
|
type Node = StringNode | RegexNode | FunctionNode;
|
|
|
|
|
|
|
|
interface StringNode {
|
|
|
|
type: "string";
|
|
|
|
value: string;
|
|
|
|
}
|
|
|
|
|
|
|
|
interface RegexNode {
|
|
|
|
type: "regex";
|
|
|
|
value: {
|
|
|
|
pattern: string;
|
|
|
|
flags: string;
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
interface FunctionNode {
|
|
|
|
type: "function";
|
|
|
|
value: string;
|
|
|
|
}
|
|
|
|
|
|
|
|
interface PatchData {
|
|
|
|
find: string;
|
|
|
|
replacement: {
|
|
|
|
match: StringNode | RegexNode;
|
|
|
|
replace: StringNode | FunctionNode;
|
|
|
|
}[];
|
|
|
|
}
|
|
|
|
|
|
|
|
interface FindData {
|
|
|
|
type: string;
|
|
|
|
args: Array<StringNode | FunctionNode>;
|
|
|
|
}
|
|
|
|
|
2023-03-21 02:07:16 -04:00
|
|
|
const settings = definePluginSettings({
|
|
|
|
notifyOnAutoConnect: {
|
2023-03-21 02:30:09 -04:00
|
|
|
description: "Whether to notify when Dev Companion has automatically connected.",
|
2023-03-21 02:07:16 -04:00
|
|
|
type: OptionType.BOOLEAN,
|
|
|
|
default: true
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2023-03-10 18:25:49 -05:00
|
|
|
function parseNode(node: Node) {
|
|
|
|
switch (node.type) {
|
|
|
|
case "string":
|
|
|
|
return node.value;
|
|
|
|
case "regex":
|
|
|
|
return new RegExp(node.value.pattern, node.value.flags);
|
|
|
|
case "function":
|
|
|
|
// We LOVE remote code execution
|
|
|
|
// Safety: This comes from localhost only, which actually means we have less permissions than the source,
|
|
|
|
// since we're running in the browser sandbox, whereas the sender has host access
|
|
|
|
return (0, eval)(node.value);
|
|
|
|
default:
|
|
|
|
throw new Error("Unknown Node Type " + (node as any).type);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
function initWs(isManual = false) {
|
|
|
|
let wasConnected = isManual;
|
|
|
|
let hasErrored = false;
|
|
|
|
const ws = socket = new WebSocket(`ws://localhost:${PORT}`);
|
|
|
|
|
|
|
|
ws.addEventListener("open", () => {
|
|
|
|
wasConnected = true;
|
|
|
|
|
|
|
|
logger.info("Connected to WebSocket");
|
|
|
|
|
2023-03-21 02:07:16 -04:00
|
|
|
(settings.store.notifyOnAutoConnect || isManual) && showNotification({
|
2023-03-10 18:25:49 -05:00
|
|
|
title: "Dev Companion Connected",
|
2023-04-13 13:15:36 -04:00
|
|
|
body: "Connected to WebSocket",
|
|
|
|
noPersist: true
|
2023-03-10 18:25:49 -05:00
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
ws.addEventListener("error", e => {
|
|
|
|
if (!wasConnected) return;
|
|
|
|
|
|
|
|
hasErrored = true;
|
|
|
|
|
|
|
|
logger.error("Dev Companion Error:", e);
|
|
|
|
|
|
|
|
showNotification({
|
|
|
|
title: "Dev Companion Error",
|
|
|
|
body: (e as ErrorEvent).message || "No Error Message",
|
2023-03-31 20:47:49 -04:00
|
|
|
color: "var(--status-danger, red)",
|
|
|
|
noPersist: true,
|
2023-03-10 18:25:49 -05:00
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
ws.addEventListener("close", e => {
|
2023-03-11 08:18:32 -05:00
|
|
|
if (!wasConnected || hasErrored) return;
|
2023-03-10 18:25:49 -05:00
|
|
|
|
|
|
|
logger.info("Dev Companion Disconnected:", e.code, e.reason);
|
|
|
|
|
|
|
|
showNotification({
|
|
|
|
title: "Dev Companion Disconnected",
|
|
|
|
body: e.reason || "No Reason provided",
|
2023-03-31 20:47:49 -04:00
|
|
|
color: "var(--status-danger, red)",
|
|
|
|
noPersist: true,
|
2023-03-10 18:25:49 -05:00
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
ws.addEventListener("message", e => {
|
|
|
|
try {
|
|
|
|
var { nonce, type, data } = JSON.parse(e.data);
|
|
|
|
} catch (err) {
|
|
|
|
logger.error("Invalid JSON:", err, "\n" + e.data);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
function reply(error?: string) {
|
|
|
|
const data = { nonce, ok: !error } as Record<string, unknown>;
|
|
|
|
if (error) data.error = error;
|
|
|
|
|
|
|
|
ws.send(JSON.stringify(data));
|
|
|
|
}
|
|
|
|
|
|
|
|
logger.info("Received Message:", type, "\n", data);
|
|
|
|
|
|
|
|
switch (type) {
|
|
|
|
case "testPatch": {
|
|
|
|
const { find, replacement } = data as PatchData;
|
|
|
|
|
|
|
|
const candidates = search(find);
|
|
|
|
const keys = Object.keys(candidates);
|
|
|
|
if (keys.length !== 1)
|
|
|
|
return reply("Expected exactly one 'find' matches, found " + keys.length);
|
|
|
|
|
2023-03-25 20:27:01 -04:00
|
|
|
const mod = candidates[keys[0]];
|
2023-04-04 09:26:53 -04:00
|
|
|
let src = String(mod.original ?? mod).replaceAll("\n", "");
|
|
|
|
|
|
|
|
if (src.startsWith("function(")) {
|
|
|
|
src = "0," + src;
|
|
|
|
}
|
2023-03-10 18:25:49 -05:00
|
|
|
|
|
|
|
let i = 0;
|
|
|
|
|
|
|
|
for (const { match, replace } of replacement) {
|
|
|
|
i++;
|
|
|
|
|
|
|
|
try {
|
|
|
|
const matcher = canonicalizeMatch(parseNode(match));
|
|
|
|
const replacement = canonicalizeReplace(parseNode(replace), "PlaceHolderPluginName");
|
|
|
|
|
|
|
|
const newSource = src.replace(matcher, replacement as string);
|
|
|
|
|
|
|
|
if (src === newSource) throw "Had no effect";
|
|
|
|
Function(newSource);
|
|
|
|
|
|
|
|
src = newSource;
|
|
|
|
} catch (err) {
|
|
|
|
return reply(`Replacement ${i} failed: ${err}`);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
reply();
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case "testFind": {
|
|
|
|
const { type, args } = data as FindData;
|
|
|
|
try {
|
|
|
|
var parsedArgs = args.map(parseNode);
|
|
|
|
} catch (err) {
|
|
|
|
return reply("Failed to parse args: " + err);
|
|
|
|
}
|
|
|
|
|
|
|
|
try {
|
|
|
|
let results: any[];
|
|
|
|
switch (type.replace("find", "").replace("Lazy", "")) {
|
|
|
|
case "":
|
|
|
|
results = findAll(parsedArgs[0]);
|
|
|
|
break;
|
|
|
|
case "ByProps":
|
|
|
|
results = findAll(filters.byProps(...parsedArgs));
|
|
|
|
break;
|
|
|
|
case "Store":
|
|
|
|
results = findAll(filters.byStoreName(parsedArgs[0]));
|
|
|
|
break;
|
|
|
|
case "ByCode":
|
|
|
|
results = findAll(filters.byCode(...parsedArgs));
|
|
|
|
break;
|
|
|
|
case "ModuleId":
|
|
|
|
results = Object.keys(search(parsedArgs[0]));
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
return reply("Unknown Find Type " + type);
|
|
|
|
}
|
|
|
|
|
2023-03-11 08:18:32 -05:00
|
|
|
const uniqueResultsCount = new Set(results).size;
|
|
|
|
if (uniqueResultsCount === 0) throw "No results";
|
|
|
|
if (uniqueResultsCount > 1) throw "Found more than one result! Make this filter more specific";
|
2023-03-10 18:25:49 -05:00
|
|
|
} catch (err) {
|
|
|
|
return reply("Failed to find: " + err);
|
|
|
|
}
|
|
|
|
|
|
|
|
reply();
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
default:
|
|
|
|
reply("Unknown Type " + type);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2023-04-14 20:47:03 -04:00
|
|
|
const contextMenuPatch: NavContextMenuPatchCallback = children => () => {
|
|
|
|
children.unshift(
|
2023-03-19 03:53:00 -04:00
|
|
|
<Menu.MenuItem
|
|
|
|
id={NAV_ID}
|
|
|
|
label="Reconnect Dev Companion"
|
|
|
|
action={() => {
|
|
|
|
socket?.close(1000, "Reconnecting");
|
|
|
|
initWs(true);
|
|
|
|
}}
|
|
|
|
/>
|
|
|
|
);
|
|
|
|
};
|
|
|
|
|
2023-03-10 18:25:49 -05:00
|
|
|
export default definePlugin({
|
|
|
|
name: "DevCompanion",
|
|
|
|
description: "Dev Companion Plugin",
|
|
|
|
authors: [Devs.Ven],
|
2023-03-21 02:07:16 -04:00
|
|
|
settings,
|
2023-03-10 18:25:49 -05:00
|
|
|
|
|
|
|
start() {
|
|
|
|
initWs();
|
2023-03-19 03:53:00 -04:00
|
|
|
addContextMenuPatch("user-settings-cog", contextMenuPatch);
|
2023-03-10 18:25:49 -05:00
|
|
|
},
|
|
|
|
|
|
|
|
stop() {
|
|
|
|
socket?.close(1000, "Plugin Stopped");
|
|
|
|
socket = void 0;
|
2023-03-19 03:53:00 -04:00
|
|
|
removeContextMenuPatch("user-settings-cog", contextMenuPatch);
|
2023-03-10 18:25:49 -05:00
|
|
|
}
|
|
|
|
});
|