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

@@ -1,3 +1,15 @@
import toml from 'smol-toml' import got from 'got';
const t = `\nmodLoader = "javafml"\nloaderVersion = "[4,)"\n\nlicense = "Supplementaries Team License v.1.2"\nissueTrackerURL = "https://github.com/MehVahdJukaar/supplementaries/issues"\n\n[[mixins]]\n config = "supplementaries.mixins.json"\n[[mixins]]\n config = "supplementaries-common.mixins.json"\n\n[[mods]]\n modId = "supplementaries"\n version = "1.20-3.1.20"\n displayName = "Supplementaries"\n logoFile = "icon.png"\n authors = "MehVahdJukaar, Plantkillable"\n credits = "Put a book named 'credi…1.20.1,1.20.2)"\n ordering = "NONE"\n side = "CLIENT"\n\n[[dependencies.supplementaries]]\n modId = "moonlight"\n mandatory = true\n versionRange = "[1.20-2.13.65,]"\n ordering = "NONE"\n side = "BOTH"\n\n[[dependencies.supplementaries]]\n modId = "quark"\n mandatory = false\n versionRange = "[1.19-3.4-404,]"\n ordering = "AFTER"\n side = "BOTH"\n\n["lithium:options"]\n mixin.block.moving_block_shapes = false\n\n["canary:options"]\n mixin.block.moving_block_shapes = false\n\n` const res = await got.post('https://api.modrinth.com/v2/version_files', {
console.log(toml.parse(t)) headers: {
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:91.0) Gecko/20100101 Firefox/91.0',
},
json: {
hashes: [
"ea0f38408102e4d2efd53c2cc11b88b711996b48d8922f76ea6abf731219c5bd1efe39ddf9cce77c54d49a62ff10fb685c00d2e4c524ab99d20f6296677ab2c4",
"925a5c4899affa4098d997dfa4a4cb52c636d539e94bc489d1fa034218cb96819a70eb8b01647a39316a59fcfe223c1a8c05ed2e2ae5f4c1e75fa48f6af1c961"
],
algorithm: 'sha512',
}
}).json()
console.log(res)

View File

@@ -54,6 +54,7 @@ async function main(modpack_path: string) {
yauzl.open(modpack_path, { lazyEntries: true }, (err, zipfile) => { yauzl.open(modpack_path, { lazyEntries: true }, (err, zipfile) => {
zipfile.readEntry(); //首次读取 zipfile.readEntry(); //首次读取
zipfile.on("entry", async (entry) => { zipfile.on("entry", async (entry) => {
const name: string = entry.fileName; const name: string = entry.fileName;
if (/\/$/.test(name)) { if (/\/$/.test(name)) {
} else if (name.includes("overrides/")) { } else if (name.includes("overrides/")) {

View File

@@ -7,6 +7,7 @@
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 crypto from "crypto";
import toml from 'smol-toml' import toml from 'smol-toml'
import path from 'path'; import path from 'path';
import pMap from "p-map"; import pMap from "p-map";
@@ -34,107 +35,6 @@ export async function DeEarthMain(modspath: string, movepath: any) {
} }
multibar.stop() 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) { export async function DeEarth(modpath: string, movepath: string) {
let mrurl = "https://api.modrinth.com" let mrurl = "https://api.modrinth.com"
@@ -142,6 +42,19 @@ export async function DeEarth(modpath: string, movepath: string) {
mrurl = "https://mod.mcimirror.top/modrinth" 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) const zipinfo = ZipInfo(modpath)
let modid:string = "" let modid:string = ""
if(zipinfo){ if(zipinfo){
@@ -152,7 +65,12 @@ export async function DeEarth(modpath: string, movepath: string) {
} }
try { //Modrinth 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"){ if(body.client_side == "required" && body.server_side !== "required"){
fs.renameSync(modpath, `${movepath}/${path.basename(modpath)}`) fs.renameSync(modpath, `${movepath}/${path.basename(modpath)}`)
} }