From 62b5b5fa269d4e6032aa4f47bc5a132a52209ae1 Mon Sep 17 00:00:00 2001 From: "753.network" Date: Thu, 6 Jun 2024 00:38:41 -0400 Subject: [PATCH] Add support for modloader selection Add support for automatically listing common packages (e.g. modloaders) to the newly created communities. For safety, include a known set of valid values rather than allowing arbitrary inputs. --- games/src/models.ts | 1 + games/src/schema/autolistPackages.ts | 24 ++++++++++++++++++++++++ games/src/schema/validator.ts | 12 ++++++++++++ games/src/scripts/add.ts | 9 ++++++++- 4 files changed, 45 insertions(+), 1 deletion(-) create mode 100644 games/src/schema/autolistPackages.ts diff --git a/games/src/models.ts b/games/src/models.ts index f066b85..2a834a0 100644 --- a/games/src/models.ts +++ b/games/src/models.ts @@ -61,6 +61,7 @@ export interface ThunderstoreCommunityDefinition { }; discordUrl?: string; wikiUrl?: string; + autolistPackageIds?: string[]; } export interface GameDefinition { diff --git a/games/src/schema/autolistPackages.ts b/games/src/schema/autolistPackages.ts new file mode 100644 index 0000000..3732703 --- /dev/null +++ b/games/src/schema/autolistPackages.ts @@ -0,0 +1,24 @@ +export const AUTOLIST_PACKAGE_CHOICES = [ + { + value: "BepInEx-BepInExPack", + name: "BepInEx 5 (use this for Unity mono games)", + }, + { + value: "BepInEx-BepInExPack_IL2CPP", + name: "BepInEx 6 IL2CPP (use this for Unity IL2CPP games)", + }, + { + value: "Thunderstore-unreal_shimloader", + name: "Unreal Engine shimloader", + }, + { + value: "LavaGang-MelonLoader", + name: "MelonLoader", + }, +]; + +const AUTOLIST_PACKAGE_IDS = AUTOLIST_PACKAGE_CHOICES.map((x) => x.value); + +export function isAutolistPackageValid(autolistPackageId: string): boolean { + return AUTOLIST_PACKAGE_IDS.indexOf(autolistPackageId) > -1; +} diff --git a/games/src/schema/validator.ts b/games/src/schema/validator.ts index a147467..fdc9b63 100644 --- a/games/src/schema/validator.ts +++ b/games/src/schema/validator.ts @@ -1,4 +1,5 @@ import { z } from "zod"; +import { isAutolistPackageValid } from "./autolistPackages"; const slug = z.string().regex(new RegExp(/^[a-z0-9](-?[a-z0-9])*$/)); @@ -20,6 +21,7 @@ const communitySchema = z.strictObject({ ), wikiUrl: z.string().optional(), discordUrl: z.string().optional(), + autolistPackageIds: z.array(z.string()).optional(), }); export type CommunitySchemaType = z.infer; @@ -96,5 +98,15 @@ export function validateSchemaJson(schemaJson: any): SchemaType { } }); + Object.entries(parsed.communities).forEach(([key, community]) => { + (community.autolistPackageIds ?? []).forEach((packageId) => { + if (!isAutolistPackageValid(packageId)) { + throw new Error( + `Invalid autolist package ID "${packageId}" defined for community "${key}"` + ); + } + }); + }); + return parsed; } diff --git a/games/src/scripts/add.ts b/games/src/scripts/add.ts index a7ee3a2..b2f4b96 100644 --- a/games/src/scripts/add.ts +++ b/games/src/scripts/add.ts @@ -2,8 +2,9 @@ import { GameDefinition } from "../models"; import { v4 as uuid } from "uuid"; import fs from "fs"; import * as yaml from "js-yaml"; -import { input } from "@inquirer/prompts"; +import { input, checkbox } from "@inquirer/prompts"; import _ from "lodash"; +import { AUTOLIST_PACKAGE_CHOICES } from "../schema/autolistPackages"; const displayName = await input({ message: "Display name for the community", @@ -23,6 +24,11 @@ const wikiUrl = await input({ default: "", }); +const autolistPackageIds = await checkbox({ + message: "Automatically list package", + choices: AUTOLIST_PACKAGE_CHOICES, +}); + const game: GameDefinition = { uuid: uuid(), label: identifier, @@ -68,6 +74,7 @@ const game: GameDefinition = { }, wikiUrl: wikiUrl || undefined, discordUrl: discordUrl || undefined, + autolistPackageIds: autolistPackageIds || undefined, }, };