feat:hash优化筛选模组

This commit is contained in:
Tianpao
2025-09-07 17:42:56 +08:00
parent f65dea29f5
commit 9afbc993ba
3 changed files with 36 additions and 105 deletions

View File

@@ -7,6 +7,7 @@
import AdmZip from "adm-zip";
import got from "got";
import fs from "fs";
import crypto from "crypto";
import toml from 'smol-toml'
import path from 'path';
import pMap from "p-map";
@@ -34,107 +35,6 @@ export async function DeEarthMain(modspath: string, movepath: any) {
}
multibar.stop()
}
/*
export async function DeEarth(modpath: string, movepath: string) {
try {
const zip = new AdmZip(modpath).getEntries();
//for (let i = 0; i < zip.length; i++) {
//const e = zip[i]
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)}`)
}
}
console.log("Modrinth")
}
} catch (error) { //mods.toml或fabric.mod.json判断
for (let i = 0; i < zip.length; i++) {
try {
const e = zip[i]
if (isForge(e.entryName)) { //Forge,Neoforge
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}`))
if (body.isClient) {
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 = 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)}`)
}
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]
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) {
let mrurl = "https://api.modrinth.com"
@@ -142,6 +42,19 @@ export async function DeEarth(modpath: string, movepath: string) {
mrurl = "https://mod.mcimirror.top/modrinth"
}
let pid:string|undefined = undefined
try {
const data = fs.readFileSync(modpath)
const hash = crypto.createHash('sha1').update(data).digest('hex')
const pjson = await got.get(`${mrurl}/v2/version_file/${hash}?algorithm=sha1`,{
headers:{
"User-Agent": "DeEarth"
}
}).json<{project_id:string}>()
pid = pjson.project_id
//console.log("哈希命中成功正在查询Modrinth")
}catch(e){}
const zipinfo = ZipInfo(modpath)
let modid:string = ""
if(zipinfo){
@@ -152,7 +65,12 @@ export async function DeEarth(modpath: string, movepath: string) {
}
try { //Modrinth
const body = JSON.parse(await FastGot(`${mrurl}/v2/project/${modid}`))
let body:any
if (pid){
body = JSON.parse(await FastGot(`${mrurl}/v2/project/${pid}`))
}else{
body = JSON.parse(await FastGot(`${mrurl}/v2/project/${modid}`))
}
if(body.client_side == "required" && body.server_side !== "required"){
fs.renameSync(modpath, `${movepath}/${path.basename(modpath)}`)
}