UNPKG

dmclc

Version:

Dolphin Minecraft Launcher Core

69 lines (68 loc) 2.64 kB
import fs from 'fs'; import fsPromises from 'fs/promises'; import { download } from '../../utils/downloads.js'; import { ContentType } from '../download/ContentService.js'; import { ModJarInfo } from '../mod.js'; export class ModManager { version; launcher; constructor(version, launcher) { this.version = version; this.launcher = launcher; } async saveInfo() { } async findMods() { const moddir = `${this.version.versionLaunchWorkDir}/mods`; if (!fs.existsSync(moddir) && fs.statSync(moddir).isDirectory()) return []; const val = []; for (const mod of await fsPromises.readdir(moddir)) { const modJar = `${moddir}/${mod}`; if ((await fsPromises.stat(modJar)).isFile() && mod.endsWith(".jar")) { val.push(await ModJarInfo.of(modJar, this.launcher, this.version.extras.loaders.map(v => v.name))); } } return val; } /** * Check mod dependencies. You should warn your users that the result is not always correct. * @returns All mod loading issues. */ async checkMods() { if (this.version.extras.loaders.length === 0) return []; let moddir = `${this.version.versionLaunchWorkDir}/mods`; if (!fs.existsSync(moddir) && fs.statSync(moddir).isDirectory()) return []; const loader = this.launcher.loaders.get(this.version.extras.loaders[0].name); return loader?.checkMods((await this.findMods()).map(v => v.manifests).flat(), this.version.extras.version, this.version.extras.loaders[0].version) ?? []; } async searchMod(name, skip, limit) { const result = []; for (const service of this.launcher.contentServices.values()) { result.push(...await service.searchContent(name, skip, limit, ContentType.MOD, service.getDefaultSortField(), this.version)); } return result; } /** * @throws RequestError * @param content content * @returns true if success */ async installContent(content) { return this.installContentVersion((await content.listVersions(this.version))[0]); } /** * @throws RequestError * @param version content version * @returns true if success */ async installContentVersion(version) { const url = await version.getVersionFileURL(); const moddir = `${this.version.versionLaunchWorkDir}/mods`; if (!await download(url, `${moddir}/${await version.getVersionFileName()}`, this.launcher)) { return false; } return true; } }