feat:修复TOML 重构DeEarth(未)

This commit is contained in:
Tianpao
2025-07-24 02:40:20 +08:00
parent 40492b8bd0
commit 5ddcaf9b45
6 changed files with 351 additions and 94 deletions

20
package-lock.json generated
View File

@@ -18,7 +18,7 @@
"inquirer": "^12.6.3", "inquirer": "^12.6.3",
"p-map": "^7.0.3", "p-map": "^7.0.3",
"p-retry": "^6.2.1", "p-retry": "^6.2.1",
"toml": "^3.0.0", "smol-toml": "^1.4.1",
"yauzl": "^3.2.0" "yauzl": "^3.2.0"
}, },
"devDependencies": { "devDependencies": {
@@ -5482,6 +5482,18 @@
"node": ">=8" "node": ">=8"
} }
}, },
"node_modules/smol-toml": {
"version": "1.4.1",
"resolved": "https://registry.npmmirror.com/smol-toml/-/smol-toml-1.4.1.tgz",
"integrity": "sha512-CxdwHXyYTONGHThDbq5XdwbFsuY4wlClRGejfE2NtwUtiHYsP1QtNsHb/hnj31jKYSchztJsaA8pSQoVzkfCFg==",
"license": "BSD-3-Clause",
"engines": {
"node": ">= 18"
},
"funding": {
"url": "https://github.com/sponsors/cyyynthia"
}
},
"node_modules/sort-keys": { "node_modules/sort-keys": {
"version": "1.1.2", "version": "1.1.2",
"resolved": "https://registry.npmmirror.com/sort-keys/-/sort-keys-1.1.2.tgz", "resolved": "https://registry.npmmirror.com/sort-keys/-/sort-keys-1.1.2.tgz",
@@ -5838,12 +5850,6 @@
"node": ">=8.0" "node": ">=8.0"
} }
}, },
"node_modules/toml": {
"version": "3.0.0",
"resolved": "https://registry.npmmirror.com/toml/-/toml-3.0.0.tgz",
"integrity": "sha512-y/mWCZinnvxjTKYhJ+pYxwD0mRLVvOtdS2Awbgxln6iEnt4rk0yBxeSBHkGJcPucRiG0e55mwWp+g/05rsrd6w==",
"license": "MIT"
},
"node_modules/trim-repeated": { "node_modules/trim-repeated": {
"version": "1.0.0", "version": "1.0.0",
"resolved": "https://registry.npmmirror.com/trim-repeated/-/trim-repeated-1.0.0.tgz", "resolved": "https://registry.npmmirror.com/trim-repeated/-/trim-repeated-1.0.0.tgz",

View File

@@ -24,7 +24,7 @@
"inquirer": "^12.6.3", "inquirer": "^12.6.3",
"p-map": "^7.0.3", "p-map": "^7.0.3",
"p-retry": "^6.2.1", "p-retry": "^6.2.1",
"toml": "^3.0.0", "smol-toml": "^1.4.1",
"yauzl": "^3.2.0" "yauzl": "^3.2.0"
}, },
"devDependencies": { "devDependencies": {

167
src/main copy.ts Normal file
View File

@@ -0,0 +1,167 @@
import inquirer from "inquirer";
import yauzl, { Entry, ZipFile } from "yauzl";
import process, { emit } from "node:process";
import fs from "node:fs";
import fse from "fs-extra";
import { exec } from "node:child_process";
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 | undefined = undefined;
let entry_arr: Entry[] = [];
//unzip
const zipfile: ZipFile = await new Promise((resolve, reject) => {
yauzl.open(modpack_path, { lazyEntries: true }, (err, zipfile) => {
if (err) {
reject(err);
}
resolve(zipfile);
});
});
zipfile.readEntry(); //首次读取
zipfile.on("entry", (entry) => {
entry_arr.push(entry);
zipfile.readEntry();
});
await new Promise((resolve) => zipfile.on("end", async () => resolve));
for (let i = 0; i < entry_arr.length; i++) {
const entry = entry_arr[i];
const name: string = entry.fileName;
if (/\/$/.test(name)) {
continue;
} else if (name.includes("overrides/")) {
const zipfilex = join(
unzip_path,
zipname,
name.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));
});
}
} else if (name.endsWith(".json") || name.includes("mcbbs.packmeta")) {
dud_files.push(name);
if (
name.includes("manifest.json") ||
name.includes("modrinth.index.json")
) {
pack_info = JSON.parse((await readzipentry(zipfile, entry)).toString());
}
}
}
//zipfile.on("end", async () => {
//zip
//try {
const dirx = join(unzip_path, zipname);
const plat = platform(what_platform(dud_files));
if (typeof pack_info !== "object") throw new Error("未找到manifest.json");
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);
fs.writeFileSync(
join(dirx, "eula.txt"),
"#By changing the setting below to TRUE you are indicating your agreement to our EULA (https://aka.ms/MinecraftEULA).\n#This serverpack created by DeEarthX(QQ_Group:559349662)\neula=true"
);
LOGGER.info("DeEarthX已将服务端制作完成");
zipfile.close();
//} catch (e) {
//LOGGER.error(e);
//}
//});
//});
//})
}
async function install(
type: string,
minecraft: string,
loaderver: string,
path: string
) {
if (await checkJava()) {
LOGGER.error(
"未安装Java或系统环境变量不存在Java已跳过自动安装模组加载器服务端"
);
return;
}
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 checkJava() {
return new Promise((resolve) => {
exec("java -version", (err) => {
if (err) {
resolve(false);
} else {
resolve(true);
}
});
});
}
function getCurrnetDir() {
const url = new URL(".", import.meta.url);
return fileURLToPath(url);
}

View File

@@ -71,12 +71,14 @@ async function main(modpack_path: string) {
stream.pipe(fse.createWriteStream(zipfilex)); stream.pipe(fse.createWriteStream(zipfilex));
}); });
} }
} else if (name.endsWith(".json")||name.includes("mcbbs.packmeta")) { } else if (name.endsWith(".json")||name.endsWith("mcbbs.packmeta")) {
dud_files.push(name); dud_files.push(name);
//console.log((await readzipentry(zipfile, entry))) //console.log((await readzipentry(zipfile, entry)))
pack_info = JSON.parse( if(name.endsWith("modrinth.index.json")|| name.endsWith("manifest.json")){
(await readzipentry(zipfile, entry)).toString() pack_info = JSON.parse(
); (await readzipentry(zipfile, entry)).toString()
);
}
} }
zipfile.readEntry(); zipfile.readEntry();
}); });

View File

@@ -7,15 +7,15 @@
import AdmZip from "adm-zip"; import AdmZip from "adm-zip";
import got from "got"; import got from "got";
import fs from "fs"; import fs from "fs";
import toml from 'toml'; import toml from 'smol-toml'
import path from 'path'; import path from 'path';
import pMap from "p-map"; import pMap from "p-map";
import { LOGGER } from "./logger.js"; import { LOGGER } from "./logger.js";
import { MultiBar } from "cli-progress"; import { MultiBar } from "cli-progress";
export async function DeEarthMain(modspath: string, movepath: any) { export async function DeEarthMain(modspath: string, movepath: any) {
if(!fs.existsSync(movepath)){ if (!fs.existsSync(movepath)) {
fs.mkdirSync(movepath) fs.mkdirSync(movepath)
} }
LOGGER.info(`DeEarth V1.0.0`) LOGGER.info(`DeEarth V1.0.0`)
LOGGER.info(`如有无法筛选的mods请前往 https://dearth.0771010.xyz/ 提交未成功筛选的模组的modid`) LOGGER.info(`如有无法筛选的mods请前往 https://dearth.0771010.xyz/ 提交未成功筛选的模组的modid`)
@@ -33,103 +33,185 @@ fs.mkdirSync(movepath)
} }
multibar.stop() multibar.stop()
} }
/*
export async function DeEarth(modpath: string, movepath: string) { export async function DeEarth(modpath: string, movepath: string) {
try{ try {
const zip = new AdmZip(modpath).getEntries(); const zip = new AdmZip(modpath).getEntries();
//for (let i = 0; i < zip.length; i++) { //for (let i = 0; i < zip.length; i++) {
//const e = zip[i] //const e = zip[i]
try { //Modrinth try { //Modrinth
for (let i = 0; i < zip.length; i++) {
const e = zip[i]
if (isForge(e.entryName)) { //Forge,NeoForge
const modid = toml.parse(e.getData().toString('utf-8')).mods[0].modId
//const body = await got.get(`https://api.modrinth.com/v2/project/${modid}`, { headers: { "User-Agent": "DeEarth" } }).json()
const body = JSON.parse(await FastGot(`https://api.modrinth.com/v2/project/${modid}`))
if (body.client_side == "required" && body.server_side !== "required") {
fs.renameSync(modpath, `${movepath}/${path.basename(modpath)}`)
}
} else if (e.entryName == "fabric.mod.json") { //Fabric
const modid = JSON.parse(e.getData().toString('utf-8')).id
//const body = await got.get(`https://api.modrinth.com/v2/project/${modid}`, { headers: { "User-Agent": "DeEarth" } }).json()
const body = JSON.parse(await FastGot(`https://api.modrinth.com/v2/project/${modid}`))
if (body.client_side == "required" && body.server_side !== "required") {
fs.renameSync(modpath, `${movepath}/${path.basename(modpath)}`)
}
}
}
} catch (error) { //mods.toml或fabric.mod.json判断
try{//DeEarthPublic
for (let i = 0; i < zip.length; i++) { for (let i = 0; i < zip.length; i++) {
const e = zip[i] const e = zip[i]
if (isForge(e.entryName)) { //Forge,Neoforge if (isForge(e.entryName)) { //Forge,NeoForge
const modid = toml.parse(e.getData().toString('utf-8')).mods[0].modId const modid = toml.parse(e.getData().toString('utf-8')).mods[0].modId
const body = JSON.parse(await FastGot(`https://dearth.0771010.xyz/api/modid?modid=${modid}`)) //const body = await got.get(`https://api.modrinth.com/v2/project/${modid}`, { headers: { "User-Agent": "DeEarth" } }).json()
if (body.isClient) { const body = JSON.parse(await FastGot(`https://api.modrinth.com/v2/project/${modid}`))
if (body.client_side == "required" && body.server_side !== "required") {
fs.renameSync(modpath, `${movepath}/${path.basename(modpath)}`) fs.renameSync(modpath, `${movepath}/${path.basename(modpath)}`)
} }
} else if (e.entryName == "fabric.mod.json") { //Fabric } else if (e.entryName == "fabric.mod.json") { //Fabric
const modid = JSON.parse(e.getData().toString('utf-8')).id const modid = JSON.parse(e.getData().toString('utf-8')).id
const body = JSON.parse(await FastGot(`https://dearth.0771010.xyz/api/modid?modid=${modid}`)) //const body = await got.get(`https://api.modrinth.com/v2/project/${modid}`, { headers: { "User-Agent": "DeEarth" } }).json()
if (body.isClient) { const body = JSON.parse(await FastGot(`https://api.modrinth.com/v2/project/${modid}`))
if (body.client_side == "required" && body.server_side !== "required") {
fs.renameSync(modpath, `${movepath}/${path.basename(modpath)}`) fs.renameSync(modpath, `${movepath}/${path.basename(modpath)}`)
} }
} }
console.log("Modrinth")
} }
}catch(errorr){ } catch (error) { //mods.toml或fabric.mod.json判断
for (let i = 0; i < zip.length; i++) { for (let i = 0; i < zip.length; i++) {
const e = zip[i] try {
try {
if (isForge(e.entryName)) { //Forge,Neoforge
const tr = toml.parse(e.getData().toString('utf-8'))
const mcside = tr.dependencies[tr.mods[0].modId].find((mod: { modId: string; }) => mod.modId === "minecraft").side
if (mcside == "CLIENT"){ //从Minecraft判断
fs.renameSync(modpath, `${movepath}/${path.basename(modpath)}`)
}
const forgeside = tr.dependencies[tr.mods[0].modId].find((mod: { modId: string; }) => mod.modId === "forge").side
if (forgeside == "CLIENT") { //从Forge判断
fs.renameSync(modpath, `${movepath}/${path.basename(modpath)}`)
}
const neoside = tr.dependencies[tr.mods[0].modId].find((mod: { modId: string; }) => mod.modId === "neoforge").side
if (neoside == "CLIENT") { //从NeoForge判断
fs.renameSync(modpath, `${movepath}/${path.basename(modpath)}`)
}
} else if (e.entryName == "fabric.mod.json") { //Fabric
const fmj = JSON.parse(e.getData().toString('utf-8')).environment
if (fmj == "client") {
fs.renameSync(modpath, `${movepath}/${path.basename(modpath)}`)
}
}
} catch (erro) {//从Mixin判断 但是可能为不准确
for (let i = 0; i < zip.length; i++) {
const e = zip[i] const e = zip[i]
try { if (isForge(e.entryName)) { //Forge,Neoforge
if (isMixinFile(e.entryName)) { const modid = toml.parse(e.getData().toString('utf-8')).mods[0].modId
LOGGER.info(e.entryName) const body = JSON.parse(await FastGot(`https://dearth.0771010.xyz/api/modid?modid=${modid}`))
const resx = JSON.parse(e.getData().toString('utf-8')) if (body.isClient) {
if (e.entryName.includes("common.mixins.json")) { //第一步从common mixins文件判断判断失败后再使用modid.mixins.json进行判断 fs.renameSync(modpath, `${movepath}/${path.basename(modpath)}`)
if (isMixin(resx)) { }
} else if (e.entryName == "fabric.mod.json") { //Fabric
const modid = JSON.parse(e.getData().toString('utf-8')).id
const body = JSON.parse(await FastGot(`https://dearth.0771010.xyz/api/modid?modid=${modid}`))
if (body.isClient) {
fs.renameSync(modpath, `${movepath}/${path.basename(modpath)}`)
}
}
console.log("DeEarth")
} catch (errorr) {
for (let i = 0; i < zip.length; i++) {
const e = zip[i]
try {
if (isForge(e.entryName)) { //Forge,Neoforge
const tr = toml.parse(e.getData().toString('utf-8'))
const mcside = tr.dependencies[tr.mods[0].modId].find((mod: { modId: string; }) => mod.modId === "minecraft").side
if (mcside == "CLIENT") { //从Minecraft判断
fs.renameSync(modpath, `${movepath}/${path.basename(modpath)}`) fs.renameSync(modpath, `${movepath}/${path.basename(modpath)}`)
} }
} else { const forgeside = tr.dependencies[tr.mods[0].modId].find((mod: { modId: string; }) => mod.modId === "forge").side
if (isMixin(resx)) { if (forgeside == "CLIENT") { //从Forge判断
fs.renameSync(modpath, `${movepath}/${path.basename(modpath)}`)
}
const neoside = tr.dependencies[tr.mods[0].modId].find((mod: { modId: string; }) => mod.modId === "neoforge").side
if (neoside == "CLIENT") { //从NeoForge判断
fs.renameSync(modpath, `${movepath}/${path.basename(modpath)}`)
}
} else if (e.entryName == "fabric.mod.json") { //Fabric
const fmj = JSON.parse(e.getData().toString('utf-8')).environment
if (fmj == "client") {
fs.renameSync(modpath, `${movepath}/${path.basename(modpath)}`) fs.renameSync(modpath, `${movepath}/${path.basename(modpath)}`)
} }
} }
} } catch (erro) {//从Mixin判断 但是可能为不准确
} catch (err:any) {//避免有傻逼JSON写注释虽然GSON可以这样 但是这样一点也不人道) for (let i = 0; i < zip.length; i++) {
if (err.errno !== -4058) { const e = zip[i]
LOGGER.error(`大天才JSON写注释了估计模组路径:${modpath},过滤失败`) try {
if (isMixinFile(e.entryName)) {
LOGGER.info(e.entryName)
const resx = JSON.parse(e.getData().toString('utf-8'))
if (e.entryName.includes("common.mixins.json")) { //第一步从common mixins文件判断判断失败后再使用modid.mixins.json进行判断
if (isMixin(resx)) {
fs.renameSync(modpath, `${movepath}/${path.basename(modpath)}`)
}
} else {
if (isMixin(resx)) {
fs.renameSync(modpath, `${movepath}/${path.basename(modpath)}`)
}
}
}
} catch (err: any) {//避免有傻逼JSON写注释虽然GSON可以这样 但是这样一点也不人道)
if (err.errno !== -4058) {
LOGGER.error(`大天才JSON写注释了估计模组路径:${modpath},过滤失败`)
}
}
}
} }
} }
} }
} }
} }
} catch (error) {
LOGGER.error("DeEarth: " + error)
}
}
*/
export async function DeEarth(modpath: string, movepath: string) {
const zipinfo = ZipInfo(modpath)
let modid:string = ""
if(zipinfo.modinfo.type === "forge"){
modid = zipinfo.modinfo.data.mods[0].modId
}else if(zipinfo.modinfo.type === "fabric"){
modid = zipinfo.modinfo.data.id
}
try { //Modrinth
const body = JSON.parse(await FastGot(`https://api.modrinth.com/v2/project/${modid}`))
if(body.client_side == "required" && body.server_side !== "required"){
fs.renameSync(modpath, `${movepath}/${path.basename(modpath)}`)
}
} catch (error) { //DeEarthPublic
try {
if (JSON.parse(await FastGot(`https://dearth.0771010.xyz/api/modid?modid=${modid}`)).isClient) {
fs.renameSync(modpath, `${movepath}/${path.basename(modpath)}`)
}
} catch (error) { //mods.toml或fabric.mod.json判断
try{
if(zipinfo.modinfo.type === "forge"){
const mcside = zipinfo.modinfo.data.dependencies[modid].find((mod: { modId: string; }) => mod.modId === "minecraft").side //Minecraft
if (mcside == "CLIENT") {
fs.renameSync(modpath, `${movepath}/${path.basename(modpath)}`)
}
const forgeside = zipinfo.modinfo.data.dependencies[modid].find((mod: { modId: string; }) => mod.modId === "forge").side //Forge
if (forgeside == "CLIENT") {
fs.renameSync(modpath, `${movepath}/${path.basename(modpath)}`)
}
const neoside = zipinfo.modinfo.data.dependencies[modid].find((mod: { modId: string; }) => mod.modId === "neoforge").side //NeoForge
if (neoside == "CLIENT") {
fs.renameSync(modpath, `${movepath}/${path.basename(modpath)}`)
}
}else if(zipinfo.modinfo.type === "fabric"){ //Fabric
const fmj = zipinfo.modinfo.data.environment
if (fmj == "client") {
fs.renameSync(modpath, `${movepath}/${path.basename(modpath)}`)
}
}
}catch(error){
try{
for (let i = 0; i < zipinfo.mixins.length; i++) {
const e = zipinfo.mixins[i]
const info = JSON.parse(e.info)
if (isMixin(info)) {
fs.renameSync(modpath, `${movepath}/${path.basename(modpath)}`)
}
}
}catch(error){
LOGGER.error(`大天才JSON写注释了估计模组路径:${modpath},过滤失败`)
}
} }
} }
}catch(error){ }
LOGGER.error("DeEarth: "+error)
} }
function ZipInfo(modpath: string) {
interface ZipInfo {
mixins: { filename: string, info: string }[]
modinfo: {type:string,data:any};
}
let zipinfo: ZipInfo = { mixins: [], modinfo: {type: "",data: {}} }
const zip = new AdmZip(modpath).getEntries();
for (let i = 0; i < zip.length; i++) {
const e = zip[i]
if (isMixinFile(e.entryName)) {
zipinfo.mixins.push({ filename: e.entryName, info: e.getData().toString('utf-8') })
} else if (isForge(e.entryName)) {
zipinfo.modinfo.type = "forge"
zipinfo.modinfo.data = toml.parse(e.getData().toString('utf-8'))
} else if (e.entryName.endsWith("fabric.mod.json")) {
zipinfo.modinfo.type = "fabric"
zipinfo.modinfo.data = JSON.parse(e.getData().toString('utf-8'))
}
}
return zipinfo;
} }
async function FastGot(url: string) { async function FastGot(url: string) {
@@ -156,7 +238,7 @@ async function FastGot(url: string) {
}) })
if (fastgot[0] !== undefined) { if (fastgot[0] !== undefined) {
return fastgot[0] return fastgot[0]
}else{ } else {
return "null" return "null"
} }
} }
@@ -164,18 +246,18 @@ async function FastGot(url: string) {
const multibar = new MultiBar({ const multibar = new MultiBar({
format: ' {bar} | {filename} | {value}/{total}', format: ' {bar} | {filename} | {value}/{total}',
noTTYOutput: true, noTTYOutput: true,
notTTYSchedule: 10*1000, notTTYSchedule: 10 * 1000,
}) })
function isMixin(resx: { mixins: {} | null; client: {}; }){ function isMixin(resx: { mixins: {} | null; client: {}; }) {
return resx.mixins == null || Object.keys(resx.mixins).length == 0 && Object.keys(resx.client).length !== 0 return resx.mixins == null || Object.keys(resx.mixins).length == 0 && Object.keys(resx.client).length !== 0
} }
function isForge(name:string):boolean{ function isForge(name: string): boolean {
return name.includes("mods.toml")||name.includes("META-INF") return name.endsWith("mods.toml") || name.endsWith("neoforge.mod.toml")
} }
function isMixinFile(name:string):boolean{ function isMixinFile(name: string): boolean {
return !name.includes("/") && name.endsWith(".json") && !name.endsWith("refmap.json") && !name.endsWith("mod.json") return !name.includes("/") && name.endsWith(".json") && !name.endsWith("refmap.json") && !name.endsWith("mod.json")
} }

View File

@@ -6,7 +6,7 @@ export const LOGGER = {
warn(text: string) { warn(text: string) {
warn(text); warn(text);
}, },
error(text: string | object) { error(text: string | object | unknown) {
error(text); error(text);
}, },
}; };
@@ -27,7 +27,7 @@ function warn(text: string) {
)}` )}`
); );
} }
function error(error: object | string) { function error(error: object | string | unknown) {
if (typeof error === "object") { if (typeof error === "object") {
console.log( console.log(
`[${chalk.blue( `[${chalk.blue(