102 lines
2.8 KiB
JavaScript
102 lines
2.8 KiB
JavaScript
const fs = require("fs");
|
|
const path = require("path");
|
|
const AdmZip = require("adm-zip"); // 用于解压 .jar 文件
|
|
|
|
// 创建文件夹(如果不存在)
|
|
function createDirIfNotExist(dirPath) {
|
|
if (!fs.existsSync(dirPath)) {
|
|
fs.mkdirSync(dirPath, { recursive: true });
|
|
}
|
|
}
|
|
|
|
/*// 写入 pack.mcmeta 文件
|
|
function writePackMcmeta() {
|
|
const packMcmetaPath = "./recipes/pack.mcmeta";
|
|
const packContent = {
|
|
pack: {
|
|
description: "通用了部分整合包模组的物品",
|
|
pack_format: 9,
|
|
},
|
|
};
|
|
|
|
if (!fs.existsSync(packMcmetaPath)) {
|
|
fs.writeFileSync(
|
|
packMcmetaPath,
|
|
JSON.stringify(packContent, null, 2),
|
|
"utf8"
|
|
);
|
|
console.log("创建并写入 pack.mcmeta 文件");
|
|
}
|
|
}*/
|
|
|
|
// 获取 .jar 文件列表
|
|
function getJarFiles(dirPath) {
|
|
const files = fs.readdirSync(dirPath);
|
|
return files.filter((file) => file.endsWith(".jar"));
|
|
}
|
|
|
|
// 解压 .jar 文件中的特定目录
|
|
function extractRecipesFromJar(jarPath, dir) {
|
|
const zip = new AdmZip(jarPath);
|
|
const recipesPath = `data/${dir}/recipes/`;
|
|
const entries = zip.getEntries();
|
|
|
|
// 查找并解压 recipes 文件夹中的内容
|
|
entries.forEach((entry) => {
|
|
if (/^data\/[^\/]+\/recipes\/.*$/.test(entry.entryName)) {
|
|
const targetFile = path.join(__dirname, entry.entryName);
|
|
const targetPath = path.dirname(targetFile)
|
|
if (entry.isDirectory){
|
|
createDirIfNotExist(targetFile);
|
|
} else {
|
|
createDirIfNotExist(targetPath);
|
|
fs.writeFileSync(targetFile, entry.getData());
|
|
}
|
|
// console.log(`解压文件到 ${targetFile}`);
|
|
}
|
|
});
|
|
}
|
|
|
|
// 处理 mods 目录下的所有 .jar 文件
|
|
function processMods() {
|
|
const modsDir = "./mods";
|
|
// const recipesDir = "./recipes";
|
|
if (fs.existsSync("./data")) {
|
|
fs.rmSync("./data", { recursive: true, force: true });
|
|
}
|
|
// createDirIfNotExist(recipesDir); // 确保 ./recipes 文件夹存在
|
|
// writePackMcmeta(); // 确保 ./recipes/pack.mcmeta 文件存在
|
|
|
|
const jarFiles = getJarFiles(modsDir);
|
|
|
|
jarFiles.forEach((jarFile) => {
|
|
const jarPath = path.join(modsDir, jarFile);
|
|
console.log(`正在读取 ${jarPath}`);
|
|
|
|
const zip = new AdmZip(jarPath);
|
|
const dataEntry = zip.getEntry("data/");
|
|
// console.log(zip.getEntries().map((v) => v.entryName).filter((v) => /data\/[a-zA-Z]+\/recipes\/.*/.test(v)));
|
|
if (!dataEntry) {
|
|
console.log(`未找到 data 目录: ${jarPath}`);
|
|
return;
|
|
}
|
|
|
|
// 获取 /data 文件夹内的所有子文件夹目录
|
|
const dirList = [];
|
|
const entries = zip.getEntries();
|
|
entries.forEach((entry) => {
|
|
const match = entry.entryName.match(/^data\/([^\/]+)/);
|
|
if (match && !dirList.includes(match[1])) {
|
|
dirList.push(match[1]);
|
|
}
|
|
});
|
|
|
|
// 遍历并解压每个目录下的 recipes 文件夹
|
|
dirList.forEach((dir) => {
|
|
extractRecipesFromJar(jarPath, dir);
|
|
});
|
|
});
|
|
}
|
|
|
|
processMods();
|