From a63e0b697d7b0f87f25fdae4f90468a83454ecdd Mon Sep 17 00:00:00 2001 From: Kir_Antipov Date: Tue, 7 Dec 2021 21:52:26 +0300 Subject: [PATCH] Made ModMetadata implementation for Fabric --- src/metadata/fabric/fabric-mod-metadata.ts | 47 ++++++++++++++++++++++ 1 file changed, 47 insertions(+) create mode 100644 src/metadata/fabric/fabric-mod-metadata.ts diff --git a/src/metadata/fabric/fabric-mod-metadata.ts b/src/metadata/fabric/fabric-mod-metadata.ts new file mode 100644 index 0000000..e1c8d73 --- /dev/null +++ b/src/metadata/fabric/fabric-mod-metadata.ts @@ -0,0 +1,47 @@ +import ModConfig from "../../metadata/mod-config"; +import ModConfigDependency from "../../metadata/mod-config-dependency"; +import Dependency from "../../metadata/dependency"; +import DependencyKind from "../../metadata/dependency-kind"; +import PublisherTarget from "../../publishing/publisher-target"; + +function getDependenciesByKind(config: any, kind: DependencyKind): Dependency[] { + const kindName = DependencyKind.toString(kind).toLowerCase(); + const dependencies = new Array(); + for (const [id, value] of Object.entries(config[kindName] || {})) { + if (typeof value === "string") { + dependencies.push(Dependency.create({ id, kind, version: value })); + } else { + dependencies.push(new ModConfigDependency({ ...value, id, kind })); + } + } + return dependencies; +} + +export default class FabricModMetadata 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); + this.id = String(this.config.id ?? ""); + this.name = String(this.config.name ?? this.id); + this.version = String(this.config.version ?? "*"); + this.loaders = ["fabric"]; + this.dependencies = DependencyKind.getValues().flatMap(x => getDependenciesByKind(this.config, x)); + } + + getProjectId(project: PublisherTarget): string | undefined { + const projectId = super.getProjectId(project); + if (projectId) { + return projectId; + } + + const projectName = PublisherTarget.toString(project).toLowerCase(); + const custom = this.config.custom; + const modManagerProjectId = custom?.modmanager?.[projectName]?.id ?? custom?.modmanager?.[projectName]; + return modManagerProjectId === undefined ? modManagerProjectId : String(modManagerProjectId); + } +}