From 47e35aa836e9412e08591e774dfcc1268b610b43 Mon Sep 17 00:00:00 2001 From: Kir_Antipov Date: Fri, 10 Mar 2023 11:29:12 +0000 Subject: [PATCH] Made an interface for custom Forge payload --- .../forge/forge-metadata-custom-payload.ts | 123 ++++++++++++++++++ 1 file changed, 123 insertions(+) create mode 100644 src/loaders/forge/forge-metadata-custom-payload.ts diff --git a/src/loaders/forge/forge-metadata-custom-payload.ts b/src/loaders/forge/forge-metadata-custom-payload.ts new file mode 100644 index 0000000..e81600c --- /dev/null +++ b/src/loaders/forge/forge-metadata-custom-payload.ts @@ -0,0 +1,123 @@ +import { ACTION_NAME } from "@/action"; +import { Dependency, createDependency } from "@/dependencies"; +import { LoaderType } from "@/loaders/loader-type"; +import { PlatformType } from "@/platforms"; +import { PartialRecord } from "@/utils/types"; +import { deprecate } from "node:util"; +import { RawForgeMetadata } from "./raw-forge-metadata"; +import { asString } from "@/utils/string-utils"; + +// TODO: Remove the deprecated stuff in v4.0. + +/** + * Custom payload for Forge metadata. + */ +export type ForgeMetadataCustomPayload = { + /** + * A list of supported mod loaders. + */ + loaders?: string[]; + + /** + * A list of mod dependencies. + */ + dependencies?: string[]; +} +& PartialRecord; + +/** + * Gets the custom payload from the Forge metadata. + * + * @param metadata - The raw Forge metadata. + * + * @returns The custom payload attached to the given metadata. + */ +export function getForgeMetadataCustomPayload(metadata: RawForgeMetadata): ForgeMetadataCustomPayload { + return containsLegacyCustomPayloadDefinition(metadata) + ? getLegacyForgeMetadataCustomPayload(metadata) + : (metadata?.[ACTION_NAME] || {}); +} + +/** + * Checks if the metadata contains a legacy custom payload definition. + * + * @param metadata - The raw Forge metadata. + * + * @returns A boolean indicating if the legacy custom payload definition is present. + */ +function containsLegacyCustomPayloadDefinition(metadata: RawForgeMetadata): boolean { + return !!metadata?.custom?.[ACTION_NAME] || !!metadata?.custom?.projects || !!metadata?.projects; +} + +/** + * Gets the legacy custom payload from the Forge metadata. + * + * @param metadata - The raw Forge metadata. + * + * @returns The custom payload object. + */ +function _getLegacyForgeMetadataCustomPayload(metadata: RawForgeMetadata): ForgeMetadataCustomPayload { + const legacyPayload = { ...metadata?.projects, ...metadata?.custom?.projects, ...metadata?.custom?.[ACTION_NAME] }; + const basePayload = metadata?.[ACTION_NAME]; + return { ...legacyPayload, ...basePayload }; +} + +/** + * Gets the legacy custom payload from the Forge metadata. + * + * @param metadata - The raw Forge metadata. + * + * @returns The custom payload object. + * + * @deprecated + * + * Use top-level `mc-publish` field in your mod metadata. + */ +const getLegacyForgeMetadataCustomPayload = deprecate( + _getLegacyForgeMetadataCustomPayload, + "Use top-level `mc-publish` field in your mods.toml.", +); + +/** + * A list of default mod loaders associated with the Forge loader. + */ +const DEFAULT_LOADERS = [LoaderType.FORGE] as const; + +/** + * Gets an array of supported mod loaders from the custom payload attached to the Forge metadata. + * + * @param payload - The custom payload object. + * + * @returns An array of supported mod loaders. + */ +export function getLoadersFromForgeMetadataCustomPayload(payload: ForgeMetadataCustomPayload): string[] { + return payload?.loaders || [...DEFAULT_LOADERS]; +} + +/** + * Gets the dependencies from the custom payload attached to the Forge metadata. + * + * @param payload - The custom payload object. + * + * @returns An array of dependencies included into the custom payload. + */ +export function getDependenciesFromForgeMetadataCustomPayload(payload: ForgeMetadataCustomPayload): Dependency[] { + if (!Array.isArray(payload?.dependencies)) { + return []; + } + + return payload?.dependencies?.map(x => createDependency(x)).filter(x => x) || []; +} + +/** + * Gets the project ID from the custom payload attached to the Forge metadata based on the given platform. + * + * @param payload - The custom payload object. + * @param platform - The platform for which the project ID is required. + * + * @returns The project ID as a string, or `undefined` if not found. + */ +export function getProjectIdFromForgeMetadataCustomPayload(payload: ForgeMetadataCustomPayload, platform: PlatformType): string | undefined { + const id = payload?.[platform]; + return id ? asString(id) : undefined; +}