115 lines
3.5 KiB
TypeScript
115 lines
3.5 KiB
TypeScript
import inquirer from "inquirer";
|
||
import yauzl from "yauzl";
|
||
import process from "node:process";
|
||
import fs from "node:fs"
|
||
import fse from "fs-extra";
|
||
import { join, basename, dirname } from "node:path";
|
||
import { platform, what_platform } from "./platform/index.js";
|
||
import { isDevelopment, readzipentry } from "./utils/utils.js";
|
||
import fabric from "./ml_install/fabric.js"
|
||
import forge from "./ml_install/forge.js"
|
||
import neoforge from "./ml_install/neoforge.js"
|
||
import { DeEarthMain } from "./utils/DeEarth.js";
|
||
import { LOGGER } from "./utils/logger.js";
|
||
import { fileURLToPath } from "node:url";
|
||
interface Answers {
|
||
modpack_path: string | undefined;
|
||
}
|
||
let unzip_path:string = ""
|
||
if(isDevelopment){
|
||
unzip_path = join("./","instance/")
|
||
}else{
|
||
unzip_path = join(getCurrnetDir().replace("dist",""),"instance")
|
||
}
|
||
let zipnamew: string = ""
|
||
const argv = process.argv.slice(2)[0];
|
||
|
||
if (!argv) {
|
||
const answer: Answers = await inquirer.prompt([
|
||
{ type: "input", name: "modpack_path", message: "请输入整合包路径" },
|
||
]);
|
||
if (answer.modpack_path) {
|
||
initdir()
|
||
await main(answer.modpack_path);
|
||
}
|
||
} else {
|
||
initdir()
|
||
await main(argv);
|
||
}
|
||
|
||
function initdir(){
|
||
if(!fs.existsSync(unzip_path)){
|
||
fse.ensureDirSync(join(unzip_path,"rubbish"))
|
||
}
|
||
}
|
||
|
||
async function main(modpack_path: string) {
|
||
const zipname= basename(modpack_path)
|
||
.replace(".zip", "")
|
||
.replace(".mrpack", "");
|
||
zipnamew=zipname
|
||
let dud_files: Array<string> = [];
|
||
let pack_info: object;
|
||
//unzip
|
||
yauzl.open(modpack_path, { lazyEntries: true }, (err, zipfile) => {
|
||
zipfile.readEntry(); //首次读取
|
||
zipfile.on("entry", async (entry) => {
|
||
if (/\/$/.test(entry.fileName)) {
|
||
} else if (entry.fileName.includes("overrides/")) {
|
||
const zipfilex = join(unzip_path, zipname,entry.fileName.replace("overrides/",""))
|
||
if(!fs.existsSync(zipfilex)){
|
||
zipfile.openReadStream(entry,(err,stream)=>{ //读取overrides文件夹下的所有文件和文件夹
|
||
const dir = dirname(zipfilex)
|
||
fse.ensureDirSync(dir);
|
||
console.log(zipfilex)
|
||
stream.pipe(fse.createWriteStream(zipfilex))
|
||
})
|
||
//console.log(entry.fileName)
|
||
}
|
||
} else {
|
||
const name: string = entry.fileName;
|
||
dud_files.push(name);
|
||
if (name.endsWith(".json")) {
|
||
//console.log((await readzipentry(zipfile, entry)))
|
||
pack_info = JSON.parse(
|
||
(await readzipentry(zipfile, entry)).toString()
|
||
);
|
||
}
|
||
}
|
||
zipfile.readEntry();
|
||
});
|
||
zipfile.on("end", async () => {
|
||
//zip
|
||
const dirx = join(unzip_path,zipname)
|
||
const plat = platform(what_platform(dud_files))
|
||
const info = await plat.getinfo(pack_info);
|
||
await plat.downloadfile(pack_info,dirx)
|
||
await DeEarthMain(join(dirx,"mods"),join(unzip_path,"rubbish"))
|
||
await install(info.loader,info.minecraft,info.loader_version,dirx)
|
||
LOGGER.info("DeEarthX已将服务端制作完成!");
|
||
zipfile.close();
|
||
});
|
||
});
|
||
}
|
||
|
||
async function install(type: string,minecraft:string,loaderver:string,path:string){
|
||
switch(type){
|
||
case "fabric":
|
||
await fabric(minecraft,loaderver,path)
|
||
break;
|
||
case "fabric-loader":
|
||
await fabric(minecraft,loaderver,path)
|
||
break;
|
||
case "forge":
|
||
await forge(minecraft,loaderver,path)
|
||
break;
|
||
case "neoforge":
|
||
await neoforge(minecraft,loaderver,path)
|
||
break;
|
||
}
|
||
}
|
||
|
||
function getCurrnetDir () {
|
||
const url = new URL(".", import.meta.url);
|
||
return fileURLToPath(url);
|
||
} |