import fs from "node:fs" import crypto from "node:crypto" import { yauzl_promise } from "./yauzl.promise.js" import got from "got" import { Utils } from "./utils.js" interface IMixins{ name: string data: string } interface IFile{ filename: string hash: string mixins: IMixins[] } interface IHashRes{ [key:string]:{ project_id: string } } interface IPjs{ id:string, client_side:string, server_side:string } export class DeEarth{ movepath: string modspath: string file: IFile[] utils: Utils constructor(modspath:string) { this.utils = new Utils(); this.movepath = "./.rubbish" this.modspath = modspath this.file = [] } async Main(){ if(!fs.existsSync(this.movepath)){ fs.mkdirSync(this.movepath,{recursive:true}) } await this.getFile() const hash = await this.Check_Hashes() const mixins = await this.Check_Mixins() const result = [...new Set(hash.concat(mixins))] result.forEach(async e=>{ await fs.promises.rename(`${this.modspath}/${e}`,`${this.movepath}/${e}`) }) } async Check_Hashes(){ const cmap = new Map() const fmap = new Map() const hashes:string[] = [] const files = this.file.forEach(e=>{ hashes.push(e.hash); cmap.set(e.hash,e.filename) }) const res = await got.post(this.utils.modrinth_url+"/v2/version_files",{ headers:{ "User-Agent": "DeEarth", "Content-Type": "application/json" }, json:{ hashes, algorithm: "sha1" } }).json() const x = Object.keys(res) const arr = [] const fhashes = [] for(let i=0;i() const result = [] //要删除的文件 for(let i=0;i{ try{ const json = JSON.parse(e.data); if(this._isClientMx(file,json)){ result.push(file.filename) } }catch(e){} }) } const _result = [...new Set(result)] return _result; } async getFile():Promise{ const files = this.getDir() const arr = [] for(let i=0;i{ //Get Mixins Info to check if(e.fileName.endsWith(".mixins.json")&&!e.fileName.includes("/")){ mxarr.push({name:e.fileName,data:(await e.ReadEntry).toString()}) } }) arr.push({filename:file,hash:sha1,mixins:mxarr}) } this.file = arr return arr; } private getDir():string[]{ if(!fs.existsSync(this.movepath)){ fs.mkdirSync(this.movepath) } const dirarr = fs.readdirSync(this.modspath).filter(e=>e.endsWith(".jar")).filter(e=>e.concat(this.modspath)); return dirarr } private _isClientMx(file:IFile,mixins:any){ return (!("mixins" in mixins) || mixins.mixins.length === 0)&&(("client" in mixins) && (mixins.client.length !== 0))&&!file.filename.includes("lib") } }