Files
DeEarthX-V2/src/ml_install/neoforge.ts
2025-07-09 10:20:20 +08:00

76 lines
2.3 KiB
TypeScript
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

import AdmZip from "adm-zip";
import fsExtra from "fs-extra";
import gotx from "got";
import { xfastdownload } from "../utils/utils.js";
import { LOGGER } from "../utils/logger.js";
import cp from "child_process";
const got = gotx.extend({
prefixUrl: "https://bmclapi2.bangbang93.com",
headers: { "User-Agent": "DeEarthX V2" },
});
interface mcinfoX {
libraries: Array<any>;
downloads: any;
}
export default async function install(
minecraft: string,
neoforge: string,
path: string
) {
const mcinfo = (await gotx
.get(`version/${minecraft}/json`)
.json()) as mcinfoX; //获取Minecraft版本JSON
const installer = (
await got.get(`neoforge/version/${neoforge}/download/installer.jar`)
).rawBody;
fsExtra.outputFileSync(path + `/installer.jar`, installer);
const zip = new AdmZip(path + `/installer.jar`).getEntries();
for (let i = 0; i < zip.length; i++) {
const e = zip[i];
if (e.entryName == "version.json") {
const fvdata = JSON.parse(e.getData().toString("utf-8")).libraries;
for (let c = 0; c < fvdata.length; c++) {
//下载依赖1
const t = fvdata[c].downloads.artifact;
await xfastdownload(
`maven${new URL(t.url).pathname}`,
`${path}/libraries/${t.path}`,
16
);
}
} else if (e.entryName == "install_profile.json") {
const json = JSON.parse(e.getData().toString("utf-8"));
const fvdata = json.libraries;
for (let c = 0; c < fvdata.length; c++) {
//下载依赖2
const t = fvdata[c].downloads.artifact;
await xfastdownload(
`maven${new URL(t.url).pathname}`,
`${path}/libraries/${t.path}`,
16
);
}
}
}
for (let d = 0; d < mcinfo.libraries.length; d++) {
const g = mcinfo.libraries[d].downloads.artifact;
await xfastdownload(
`maven${new URL(g.url).pathname}`,
`${path}/libraries/${g.path}`,
16
);
}
await xfastdownload(
`version/${minecraft}/server`,
`${path}/libraries/net/minecraft/server/${minecraft}/server-${minecraft}.jar`,
1
);
LOGGER.info("下载NeoForge完成");
cp.execSync(`java -jar ${path}/installer.jar --installServer`, {
cwd: path,
stdio: "ignore",
});
LOGGER.info("安装NeoForge完成");
}