From e8d9eb6916dda81357414286198c760d3ba68093 Mon Sep 17 00:00:00 2001 From: Hornwitser Date: Thu, 8 Feb 2024 21:09:56 +0100 Subject: [PATCH] Convert prototype icons to array during export It's possible to set the .icons to an arbitrary key value mapping instead of an array and the game will still accept it. Convert the icons to an array if it's an object mapping to handle this case. Note that this doesn't preserve the order the game reads the keys in because JavaScript reorders numeric key indexes on objects differently to how the game orders them. --- CHANGELOG.md | 1 + packages/host/src/export.ts | 15 ++++++++++++++- 2 files changed, 15 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 80b4f549..ea1ac02f 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -13,6 +13,7 @@ - Fixed Research Sync applying progress of a previous techology level to the level on the controller when it's the active research during instance startup. - Fixed data export with a mod using shorthand notation for the default value of a color-setting causing the ModPack data to be set to invalid data. - Fixed per user per instance player stats not being deleted when the instance it's for is deleted. +- Fixed mods setting the item prototype icons property to an object mapping instead of an array breaking the data export. ## Version 2.0.0-alpha.16 diff --git a/packages/host/src/export.ts b/packages/host/src/export.ts index 337a89f5..530f8fc9 100644 --- a/packages/host/src/export.ts +++ b/packages/host/src/export.ts @@ -245,6 +245,18 @@ async function loadLayeredIcon( return icon; } +function fixIcons(item: ItemPrototype) { + const icons = item.icons; + if (typeof icons === "object" && !(icons instanceof Array) && icons !== null) { + // It's possible to specify icons as an object with arbitrary keys + // and the game will still accept it, cast the value to array if + // this is the case. + item = { ...item }; + item.icons = Object.values(icons); + } + return item; +} + const itemTypes = new Set([ "item", "ammo", @@ -277,7 +289,8 @@ const itemTypes = new Set([ function filterItems(prototypes: Prototypes): ItemPrototype[] { return Object.entries(prototypes) .filter(([type, _]) => itemTypes.has(type)) - .flatMap(([_, typePrototypes]) => Object.values(typePrototypes)) as ItemPrototype[] + .flatMap(([_, typePrototypes]) => Object.values(typePrototypes) as ItemPrototype[]) + .map(fixIcons) ; }