beerpsi-x/patchers/_parser.ts
2024-10-27 02:44:51 +07:00

59 lines
1.4 KiB
TypeScript

import vm from "node:vm";
import * as cheerio from "npm:cheerio";
import { BemaniPatch, BemaniPatcherArgs } from "./_types.ts";
export function bytesToHex(bytes: number[]) {
return bytes.map((b) => b.toString(16).padStart(2, "0").toUpperCase()).join(
"",
);
}
export function parsePatcherHtml(filename: string, contents: string) {
const $ = cheerio.load(contents);
let script = "";
for (const element of $("script").get()) {
const code = $(element).html();
if (!code?.includes("new Patcher")) {
continue;
}
script += code;
script += "\n";
}
if (script.length === 0) {
console.warn(
`Failed to find any BemaniPatcher patches in ${filename}.`,
);
return [];
}
const patchers: BemaniPatcherArgs[] = [];
const context = {
window: {
addEventListener: (_type: string, listener: () => unknown) =>
listener(),
},
Patcher: class {
constructor(
fname: string,
description: string,
patches: BemaniPatch[],
) {
patchers.push({ fname, description, patches });
}
},
PatchContainer: class {
constructor(_patchers: unknown[]) {}
},
};
vm.createContext(context);
vm.runInContext(script, context);
return patchers;
}