UNPKG

dmclc

Version:

Dolphin Minecraft Launcher Core

86 lines (85 loc) 2.69 kB
import fs from 'fs'; import fsPromises from 'fs/promises'; import StreamZip from 'node-stream-zip'; import path from 'path'; import { downloadAll } from '../../../utils/downloads.js'; export class ModrinthModpack { zipFile; manifest; launcher; unzipDir; constructor(zipFile, manifest, launcher) { this.zipFile = zipFile; this.manifest = manifest; this.launcher = launcher; } async downloadMods(mcdir) { const map = new Map(); for (const i of this.manifest.files) { if (i.env) { if (i.env.client === "unsupported") continue; } const outPath = `${mcdir}/${i.path}`; if (!path.resolve(outPath).startsWith(path.resolve(mcdir))) await this.launcher.error("mod.modpack.invalid"); const dir = path.dirname(outPath); map.set(i.downloads[0], outPath); } return await downloadAll(map, this.launcher); } async getOverrideDirs() { if (this.unzipDir === undefined) { this.unzipDir = await fsPromises.mkdtemp(this.manifest.name); this.zipFile.extract(null, this.unzipDir); } const ret = []; if (fs.existsSync(`${this.unzipDir}/overrides`)) { ret.push(`${this.unzipDir}/overrides`); } if (fs.existsSync(`${this.unzipDir}/client-overrides`)) { ret.push(`${this.unzipDir}/client-overrides`); } return ret; } getName() { return this.manifest.name; } getSummary() { return this.manifest.summary ?? "Loerm ipusm..."; } getVersion() { return this.manifest.versionId; } getLoaders() { const loaders = Object.entries(this.manifest.dependencies); return loaders.filter(v => v[0] !== "minecraft").map(v => { return { name: v[0].replace("-loader", ""), version: v[1] }; }); } getMinecraftVersion() { return this.manifest.dependencies.minecraft; } } export class ModrinthModpackFormat { async readModpack(file, launcher) { const zip = new StreamZip.async({ file }); const index = JSON.parse((await zip.entryData("modrinth.index.json")).toString()); return new ModrinthModpack(zip, index, launcher); } async checkModpack(file, launcher) { const zip = new StreamZip.async({ file }); for (let key in (await zip.entries())) { if (key === "modrinth.index.json") return true; } return false; } }