const fs = require("fs"); const path = require("path"); const AdmZip = require("adm-zip"); // 用于解压和打包 .jar 文件 const { execSync } = require("child_process"); // 读取文件并解析为 JSON function readJson(filePath) { try { const content = fs.readFileSync(filePath, "utf8"); return JSON.parse(content); } catch (err) { console.error(`无法读取或解析文件: ${filePath}`); return null; } } // 获取 .jar 文件列表 function getJarFiles(dirPath) { const files = fs.readdirSync(dirPath); return files.filter((file) => file.endsWith(".jar")); } // 复制文件 function copyFile(source, destination) { try { fs.copyFileSync(source, destination); } catch (err) { console.error(`复制文件失败: ${source} 到 ${destination}`); } } // 将 zh_cn.json 文件添加到 jar 包 /*function addToJar(jarPath, modId, zhJsonPath) { const zip = new AdmZip(jarPath); // 读取 zh_cn.json 文件内容 const zhJson = readJson(zhJsonPath); if (!zhJson) return; // 将 zh_cn.json 写入到 zip 包的指定位置 const entryPath = `assets/${modId}/lang/zh_cn.json`; zip.addFile( entryPath, Buffer.from(JSON.stringify(zhJson, null, 2), "utf8") ); // 保存新的 .jar 文件 const newJarPath = path.join( "./mods_translated", `[Translated]${path.basename(jarPath)}` ); zip.writeZip(newJarPath); console.log(`${jarPath} 已重新打包到 ${newJarPath}`); }*/ // 将 zh_cn.json 文件添加到 jar 包 function addToJar(jarPath, modId, zhJsonPath) { // 新的 .jar 文件路径 const translatedJarPath = path.join( "./mods_translated", `[Translated]${path.basename(jarPath)}` ); // 复制原始 jar 文件到 mods_translated 目录 copyFile(jarPath, translatedJarPath); // 使用 jar 命令行工具将 zh_cn.json 添加到 jar 包内 const assetsPath = `assets/${modId}/lang/zh_cn.json`; // 生成的命令:jar -cf <新文件路径> <要添加的文件路径> <原始 jar 文件路径> try { if (fs.existsSync(path.join(__dirname, "assets"))) { fs.rmSync(path.join(__dirname, "assets"), { recursive: true, force: true }); } fs.mkdirSync(path.join(__dirname, path.dirname(assetsPath)), { recursive: true }); copyFile(zhJsonPath, assetsPath); execSync( `jar -uf "${translatedJarPath}" assets` ); console.log( `${jarPath} 已重新打包到 ${translatedJarPath},并添加了 ${assetsPath}` ); } catch (err) { console.error(`打包失败: ${err.message}`); } } function removeJsonLineBreak(str) { const regex = /"([^"]*)"/g; return str.replace(regex, (match, p1) => { return `"${p1.replace(/[\r\n]+/g, "")}"`; }); } // 处理 mods 目录下的 .jar 文件 function processMods() { const modsDir = "./mods"; const translatedDir = "./mods_translated"; if (!fs.existsSync(translatedDir)) { fs.mkdirSync(translatedDir, { recursive: true }); } const jarFiles = getJarFiles(modsDir); jarFiles.forEach((jarFile) => { const jarPath = path.join(modsDir, jarFile); console.log(`正在读取 ${jarPath}`); // 解压 jar 包并读取 fabric.mod.json 文件 const zip = new AdmZip(jarPath); const modJsonEntry = zip.getEntry("fabric.mod.json"); if (!modJsonEntry) { console.log(`未找到 fabric.mod.json: ${jarPath}`); return; } const modJsonContent = zip.readAsText(modJsonEntry); const modJson = JSON.parse(removeJsonLineBreak(modJsonContent)); const modId = modJson.id; // 检查是否存在 zh_cn.json 文件 const zhJsonPath = path.join("./lang/zh_cn", `${modId}.json`); if (fs.existsSync(zhJsonPath)) { // 将 zh_cn.json 文件打包进新的 jar 文件 addToJar(jarPath, modId, zhJsonPath); } }); } processMods();