From ddc460484ece08738a43e14e510400176839b75d Mon Sep 17 00:00:00 2001 From: Kir_Antipov Date: Tue, 7 Dec 2021 21:53:53 +0300 Subject: [PATCH] Implemented ModMetadata for Forge --- src/metadata/forge/forge-mod-metadata.ts | 41 ++++++++++++++++++++++++ 1 file changed, 41 insertions(+) create mode 100644 src/metadata/forge/forge-mod-metadata.ts diff --git a/src/metadata/forge/forge-mod-metadata.ts b/src/metadata/forge/forge-mod-metadata.ts new file mode 100644 index 0000000..f8cac50 --- /dev/null +++ b/src/metadata/forge/forge-mod-metadata.ts @@ -0,0 +1,41 @@ +import ModConfig from "../../metadata/mod-config"; +import ModConfigDependency from "../../metadata/mod-config-dependency"; +import Dependency from "../../metadata/dependency"; +import DependencyKind from "../../metadata/dependency-kind"; + +function createDependency(body: any): Dependency { + return new ModConfigDependency({ + ...body, + id: body.modId, + version: body.versionRange, + kind: body.incompatible && DependencyKind.Breaks || body.embedded && DependencyKind.Includes || body.mandatory && DependencyKind.Depends || DependencyKind.Recommends + }); +} + +export default class ForgeModMetadata extends ModConfig { + public readonly id: string; + public readonly name: string; + public readonly version: string; + public readonly loaders: string[]; + public readonly dependencies: Dependency[]; + + constructor(config: Record) { + super(config); + const mods = Array.isArray(this.config.mods) && this.config.mods || []; + const mod = mods[0]; + if (!mod) { + throw new Error("At least one mod should be specified"); + } + + this.id = mod.modId; + this.name = mod.displayName || this.id; + this.version = mod.version || "*"; + this.loaders = ["forge"]; + this.dependencies = Object + .values(this.config.dependencies || {}) + .filter(Array.isArray) + .flatMap(x => x) + .map(createDependency) + .filter((x, i, self) => self.findIndex(y => x.id === y.id && x.kind === y.kind) === i); + } +}