From ecaec6d942ed4e5eb733c1110823443c162fb72f Mon Sep 17 00:00:00 2001 From: beerpsi Date: Sun, 27 Oct 2024 03:17:55 +0700 Subject: [PATCH] use a proper logger --- .gitignore | 1 + README.md | 4 ++-- patchers/b2mpatch.ts | 41 ++++++++++++++++++++++++++++++++--------- patchers/b2spatch.ts | 7 +++++-- 4 files changed, 40 insertions(+), 13 deletions(-) diff --git a/.gitignore b/.gitignore index 65d44d2..65f7a48 100644 --- a/.gitignore +++ b/.gitignore @@ -1 +1,2 @@ .ruff_cache +*.log diff --git a/README.md b/README.md index 647f397..7a7720c 100644 --- a/README.md +++ b/README.md @@ -9,10 +9,10 @@ disk image. ## Patchers - `patchers/b2spatch.ts`: Convert from BemaniPatcher to Spice2x JSON - - Use Deno: `deno run -WNE b2spatch.ts ...` + - Use Deno: `deno run -A b2spatch.ts ...` - You will need to rename the resulting JSON manually so that they match the `{gameCode}-{timestamp}_{entrypoint}` format expected by Spice2x. -- `patchers/b2mpatch.ts`: Convert from BemaniPatcher to [mempatch-hook](https://github.com/djhackersdev/bemanitools/blob/master/doc/tools/mempatch-hook.md) +- `patchers/b2mpatch.ts`: Convert from BemaniPatcher to [mempatch-hook](https://github.com/djhackersdev/bemanitools/blob/master/doc/tools/mempatch-hook.md)/[mempatcher](https://github.com/aixxe/mempatcher) - Use Deno: `deno run -A b2mpatch.ts ...` - You need to setup a folder of executables or DLLs you want to patch. They can be named the patcher's description, or the patcher's filename. diff --git a/patchers/b2mpatch.ts b/patchers/b2mpatch.ts index f34bc2e..df8b3e8 100644 --- a/patchers/b2mpatch.ts +++ b/patchers/b2mpatch.ts @@ -1,11 +1,15 @@ +import { Buffer } from "node:buffer"; import { existsSync, readFileSync, writeFileSync } from "node:fs"; import process from "node:process"; import path from "node:path"; +import { CreateLogger } from "npm:mei-logger"; import PE from "npm:pe-parser"; import type { PEFile } from "npm:pe-parser/parser"; import sanitize from "npm:sanitize-filename"; import { bytesToHex, parsePatcherHtml } from "./_parser.ts"; +const logger = CreateLogger("b2mpatch"); + function fileOffsetToRVA(pe: PEFile, offset: number) { for (const section of pe.sections) { if ( @@ -45,32 +49,46 @@ async function main() { executableDir, sanitize(patcher.description) + path.extname(patcher.fname), ); + let useFileOffsets = false; if (!existsSync(exePath)) { - console.warn( + logger.warn( `Target file ${exePath} does not exist, falling back to ${patcher.fname}.`, ); exePath = path.join(executableDir, patcher.fname); } if (!existsSync(exePath)) { - console.warn(`Target file ${exePath} does not exist, skipping.`); - continue; + logger.warn(`Target file ${exePath} does not exist, using file offsets instead (requires gh:aixxe/mempatcher).`); + useFileOffsets = true; + } + + let data: Buffer | null = null; + let pe: PEFile | null = null; + + if (!useFileOffsets) { + data = readFileSync(exePath); + pe = await PE.Parse(data); } - const data = readFileSync(exePath); - const pe = await PE.Parse(data); let mempatch = `# ${patcher.fname} ${patcher.description}\n\n`; for (const patch of patcher.patches) { if (patch.type !== undefined && patch.type !== "union") { - console.warn( + logger.warn( `Unsupported BemaniPatcher patch type ${patch.type}`, patch, ); continue; } + if (patch.type === "union" && !data) { + logger.warn( + `Union patch requires file data, skipping.`, patch, + ); + continue; + } + mempatch += `# ${patch.name}\n`; if (patch.tooltip) { @@ -90,7 +108,7 @@ async function main() { const up = p as { name: string; patch: number[] }; on = bytesToHex(up.patch); - off = data + off = data! .subarray( patch.offset, patch.offset + up.patch.length, @@ -111,8 +129,13 @@ async function main() { on = bytesToHex(sp.on); off = bytesToHex(sp.off); - offset = fileOffsetToRVA(pe, sp.offset).toString(16) - .toUpperCase(); + + if (useFileOffsets) { + offset = "F+" + sp.offset.toString(16).toUpperCase(); + } else { + offset = fileOffsetToRVA(pe, sp.offset).toString(16) + .toUpperCase(); + } } if (patch.type === "union" && on === off) { diff --git a/patchers/b2spatch.ts b/patchers/b2spatch.ts index 74ef5ae..3314886 100644 --- a/patchers/b2spatch.ts +++ b/patchers/b2spatch.ts @@ -8,6 +8,7 @@ import { readFileSync, writeFileSync } from "node:fs"; import path from "node:path"; import process from "node:process"; +import { CreateLogger } from "npm:mei-logger"; import { bytesToHex, parsePatcherHtml } from "./_parser.ts"; import type { BemaniPatch, @@ -19,6 +20,8 @@ import type { SpiceUnionPatch, } from "./_types.ts"; +const logger = CreateLogger("b2spatch"); + function convertToSpicePatch( bmPatch: BemaniPatch, gameCode: string, @@ -69,7 +72,7 @@ function convertToSpicePatch( // Technically you can match BemaniPatcher's DynamicPatch type to // Spice's signature patch, but one DynamicPatch can have multiple signatures, // while Spice only supports one signature per patch. - console.warn( + logger.warn( `Unsupported BemaniPatcher patch type ${bmPatch.type}`, bmPatch, ); @@ -90,7 +93,7 @@ function convertToSpicePatch( async function main() { if (process.argv.length < 4) { console.log( - "Usage: deno run -WNE b2spatch.ts [output dir]", + "Usage: deno run -A b2spatch.ts [output dir]", ); process.exit(1); }