support regex finds and bit more work on reporter gui

This commit is contained in:
sadan 2024-08-20 16:03:02 -04:00
parent be739d8774
commit 4a5d480acc
No known key found for this signature in database
7 changed files with 74 additions and 15 deletions

View file

@ -24,7 +24,7 @@ import { canonicalizeMatch, canonicalizeReplace } from "@utils/patches";
import definePlugin, { OptionType, ReporterTestable } from "@utils/types";
import { filters, findAll, search, wreq } from "@webpack";
import { extractModule, extractOrThrow, FindData, findModuleId, parseNode, PatchData, SendData } from "./util";
import { extractModule, extractOrThrow, FindData, findModuleId, FindType, mkRegexFind, parseNode, PatchData, SendData } from "./util";
const PORT = 8485;
const NAV_ID = "dev-companion-reconnect";
@ -149,7 +149,11 @@ function initWs(isManual = false) {
break;
}
case "search": {
const moduleId = +findModuleId([idOrSearch.toString()]);
let moduleId;
if (data.findType === FindType.STRING)
moduleId = +findModuleId([idOrSearch.toString()]);
else
moduleId = +findModuleId(mkRegexFind(idOrSearch));
const p = extractOrThrow(moduleId);
const p2 = extractModule(moduleId, false);
console.log(p, p2, "done");
@ -188,7 +192,11 @@ function initWs(isManual = false) {
break;
}
case "search": {
const moduleId = +findModuleId([idOrSearch.toString()]);
let moduleId;
if (data.findType === FindType.STRING)
moduleId = +findModuleId([idOrSearch.toString()]);
else
moduleId = +findModuleId(mkRegexFind(idOrSearch));
replyData({
type: "extract",
ok: true,
@ -259,7 +267,13 @@ function initWs(isManual = false) {
case "testPatch": {
const { find, replacement } = data as PatchData;
const candidates = search(find);
let candidates;
if (data.findType === FindType.STRING)
candidates = search(find.toString());
else
candidates = search(...mkRegexFind(find));
// const candidates = search(find);
const keys = Object.keys(candidates);
if (keys.length !== 1)
return reply("Expected exactly one 'find' matches, found " + keys.length);
@ -368,3 +382,4 @@ export default definePlugin({
socket = void 0;
}
});

View file

@ -4,6 +4,7 @@
* SPDX-License-Identifier: GPL-3.0-or-later
*/
import { canonicalizeMatch } from "@utils/patches";
import { CodeFilter, stringMatches, wreq } from "@webpack";
import { settings } from ".";
@ -21,6 +22,10 @@ export interface RegexNode {
flags: string;
};
}
export enum FindType {
STRING,
REGEX
}
export interface FunctionNode {
type: "function";
value: string;
@ -93,4 +98,9 @@ export function findModuleId(find: CodeFilter) {
}
return matches[0];
}
export function mkRegexFind(idOrSearch: string): RegExp[] {
const regex = idOrSearch.substring(1, idOrSearch.lastIndexOf("/"));
const flags = idOrSearch.substring(idOrSearch.lastIndexOf("/") + 1);
return [canonicalizeMatch(RegExp(regex, flags))];
}