// 改写 .atlas 里的贴图页名后缀(页名行是形如 `xilian.png` 的独立一行)。 // 逐行处理、只改匹配 `^\S+\.png$` 的行,保留原有行尾与其余内容,避免误伤区域名。 // 用法:node tools/atlas-pages.mjs [后缀替换: .png=.webp] [--dry] import { readFile, writeFile } from "node:fs/promises"; const args = process.argv.slice(2); const file = args[0]; if (!file) { console.error("usage: node tools/atlas-pages.mjs [.png=.webp] [--dry]"); process.exit(2); } const pair = (args.find((a) => a.includes("=")) ?? ".png=.webp").split("="); const dry = args.includes("--dry"); const text = await readFile(file, "utf8"); const lines = text.split("\n"); let changed = 0; const out = lines.map((line) => { const m = line.match(/^(\S+\.png)(\r?)$/); // 注意保留 CRLF 行尾,实测这两个 .atlas 是 CRLF if (!m) return line; changed++; return line.replace(/\.png(\r?)$/, `${pair[1]}$1`); }); if (!changed) { console.log(`${file}: 没有需要改写的页名行`); process.exit(0); } console.log(`${file}: ${changed} 个页名 → ${pair[1]}${dry ? "(试运行,未写入)" : ""}`); if (!dry) await writeFile(file, out.join("\n"), "utf8");