Files
blitzkit/packages/scripts/src/buildAssets/consumableProvisionIcons.ts
T

225 lines
6.6 KiB
TypeScript
Raw Normal View History

2025-10-23 21:22:21 -05:00
import sharp from "sharp";
import { AssetUploader } from "../core/github/assetUploader";
2026-04-07 22:35:17 +02:00
import { parsePackedSpriteRect } from "../core/blitz/parsePackedSpriteRect";
2025-11-26 13:18:44 -06:00
import { vfs } from "./constants";
2025-10-23 21:22:21 -05:00
import { ConsumablesCommon, ProvisionsCommon } from "./definitions";
2024-01-17 11:34:27 -06:00
interface Mappings {
Header: { version: number };
StyleSheets: {
selectors: string[];
2025-10-23 21:22:21 -05:00
properties: { "bg-sprite": string };
2024-01-17 11:34:27 -06:00
}[];
}
2024-08-14 14:04:06 -05:00
const listItemsPattern = /<items path="(.+)\.xml"\/>/g;
2026-04-07 23:15:26 +02:00
async function extractPackedIcon(
texture: sharp.Sharp,
sizes: number[],
context: string,
) {
const [left, top, width, height] = sizes;
const bounds = [left, top, width, height];
if (!bounds.every(Number.isInteger)) {
throw new Error(
`Invalid RIFF sprite bounds for ${context}: ${bounds.join(" ")}`,
);
}
const metadata = await texture.metadata();
if (metadata.width === undefined || metadata.height === undefined) {
throw new Error(`Failed to read image dimensions for ${context}`);
}
if (
left < 0 ||
top < 0 ||
width <= 0 ||
height <= 0 ||
left + width > metadata.width ||
top + height > metadata.height
) {
throw new Error(
`Out-of-bounds RIFF sprite for ${context}: ${bounds.join(" ")} in ${metadata.width}x${metadata.height}`,
);
}
return await texture.clone().extract({ left, top, width, height }).toBuffer();
}
2024-08-27 14:40:43 -05:00
export async function consumableProvisionIcons() {
2025-10-23 21:22:21 -05:00
console.log("Building consumable and provision icons...");
2024-08-14 14:04:06 -05:00
2025-10-23 21:22:21 -05:00
using uploader = new AssetUploader("consumable and provision icons");
2024-08-14 14:04:06 -05:00
const styles = [
2026-03-27 02:03:15 +01:00
"UI/Styles/Lobby/Inventory/InventoryNormalStyles.yaml",
2025-10-23 21:22:21 -05:00
"UI/Styles/Lobby/Inventory/Event/InventoryBigStyles.yaml",
"UI/Styles/Lobby/Inventory/Event/InventoryNormalStyles.yaml",
"UI/Screens3/Lobby/Inventory/Equipment/EquipmentItemImage.style.yaml",
"UI/Screens/Battle/Styles/BattleEquipmentStyles.yaml",
2024-08-14 14:04:06 -05:00
];
const styleSheets = await Promise.all(
styles.map(async (path) =>
2026-04-01 21:40:15 -05:00
Object.values((await vfs.yaml<Mappings>(`Data/${path}`)).StyleSheets),
),
2024-08-14 14:04:06 -05:00
).then((array) => array.flat());
2024-01-17 11:34:27 -06:00
2024-08-14 14:04:06 -05:00
for (const match of (
2026-04-01 21:40:15 -05:00
await vfs.text(`Data/XML/item_defs/vehicles/common/consumables/list.xml`)
2024-08-14 14:04:06 -05:00
).matchAll(listItemsPattern)) {
2025-11-26 13:18:44 -06:00
const consumablesCommon = await vfs.xml<{
2024-08-14 14:04:06 -05:00
root: ConsumablesCommon;
2025-11-26 13:18:44 -06:00
}>(`Data/XML/item_defs/vehicles/common/consumables/${match[1]}.xml`);
2024-01-17 11:34:27 -06:00
2025-10-23 21:22:21 -05:00
for (const consumable of Object.values(consumablesCommon.root)) {
const styleSheet = styleSheets.find((styleSheet) =>
styleSheet.selectors.some((selector) =>
2026-04-01 21:40:15 -05:00
selector.includes(`${consumable.icon} `),
),
2025-10-23 21:22:21 -05:00
);
if (!styleSheet) {
console.warn(
2026-04-01 21:40:15 -05:00
`No style sheet found for consumable ${consumable.icon}; skipping...`,
2024-01-17 11:34:27 -06:00
);
2025-10-23 21:22:21 -05:00
continue;
}
2024-01-17 11:34:27 -06:00
2025-10-23 21:22:21 -05:00
const configPath = styleSheet.properties["bg-sprite"]
.replace("~res:/", "")
.replace(".psd", "")
.replace(".txt", "");
2024-01-17 11:34:27 -06:00
2026-04-01 21:40:15 -05:00
if (await vfs.resolve(`Data/${configPath}.packed.webp`)) {
2026-04-07 22:35:17 +02:00
const packedBuffer = await vfs.file(`Data/${configPath}.packed.webp`);
const consumablesTexture = sharp(packedBuffer);
const sizes = parsePackedSpriteRect(packedBuffer);
let content: Buffer;
if (sizes) {
2026-04-07 23:15:26 +02:00
content = await extractPackedIcon(
consumablesTexture,
sizes,
`consumable ${consumable.icon}`,
);
2026-04-07 22:35:17 +02:00
} else {
2026-04-07 23:15:26 +02:00
content = await consumablesTexture.trim({ threshold: 80 }).toBuffer();
2026-04-07 22:35:17 +02:00
}
2024-08-14 14:04:06 -05:00
2025-10-23 21:22:21 -05:00
await uploader.add({
path: `icons/consumables/${consumable.id}.webp`,
content,
});
} else {
const consumablesTexture = sharp(
2025-11-26 13:18:44 -06:00
await vfs.file(
`${`Data/${configPath}`
2025-10-23 21:22:21 -05:00
.split("/")
.slice(0, -1)
2026-04-01 21:40:15 -05:00
.join("/")}/texture0.packed.webp`,
),
2025-10-23 21:22:21 -05:00
);
2025-11-26 13:18:44 -06:00
const sizes = (await vfs.text(`Data/${configPath}.txt`))
2025-10-23 21:22:21 -05:00
.split("\n")[4]
.split(" ")
.map(Number);
const content = await consumablesTexture
.clone()
.extract({
left: sizes[0],
top: sizes[1],
width: sizes[2],
height: sizes[3],
})
.toBuffer();
2024-08-14 14:04:06 -05:00
2025-10-23 21:22:21 -05:00
await uploader.add({
path: `icons/consumables/${consumable.id}.webp`,
content,
});
}
}
2024-08-14 14:04:06 -05:00
}
for (const match of (
2026-04-01 21:40:15 -05:00
await vfs.text(`Data/XML/item_defs/vehicles/common/provisions/list.xml`)
2024-08-14 14:04:06 -05:00
).matchAll(listItemsPattern)) {
2025-11-26 13:18:44 -06:00
const provisionsCommon = await vfs.xml<{
2024-08-14 14:04:06 -05:00
root: ProvisionsCommon;
2025-11-26 13:18:44 -06:00
}>(`Data/XML/item_defs/vehicles/common/provisions/${match[1]}.xml`);
2025-10-23 21:22:21 -05:00
for (const provision of Object.values(provisionsCommon.root)) {
const styleSheet = styleSheets.find((styleSheet) =>
styleSheet.selectors.some((selector) =>
2026-04-01 21:40:15 -05:00
selector.includes(`${provision.icon} `),
),
2025-10-23 21:22:21 -05:00
);
if (!styleSheet) {
console.warn(
2026-04-01 21:40:15 -05:00
`No style sheet found for provision ${provision.icon}; skipping...`,
2024-02-12 19:24:34 -06:00
);
2025-10-23 21:22:21 -05:00
continue;
}
2024-01-17 11:34:27 -06:00
2025-10-23 21:22:21 -05:00
const configPath = styleSheet.properties["bg-sprite"]
.replace("~res:/", "")
.replace(".psd", "")
.replace(".txt", "");
2024-01-17 11:34:27 -06:00
2026-04-01 21:40:15 -05:00
if (await vfs.resolve(`Data/${configPath}.packed.webp`)) {
2026-04-07 22:35:17 +02:00
const packedBuffer = await vfs.file(`Data/${configPath}.packed.webp`);
const provisionsTexture = sharp(packedBuffer);
const sizes = parsePackedSpriteRect(packedBuffer);
let content: Buffer;
if (sizes) {
2026-04-07 23:15:26 +02:00
content = await extractPackedIcon(
provisionsTexture,
sizes,
`provision ${provision.icon}`,
);
2026-04-07 22:35:17 +02:00
} else {
2026-04-07 23:15:26 +02:00
content = await provisionsTexture.trim({ threshold: 80 }).toBuffer();
2026-04-07 22:35:17 +02:00
}
2024-01-17 11:34:27 -06:00
2025-10-23 21:22:21 -05:00
await uploader.add({
path: `icons/provisions/${provision.id}.webp`,
content,
});
} else {
const provisionsTexture = sharp(
2025-11-26 13:18:44 -06:00
await vfs.file(
`${`Data/${configPath}`
2025-10-23 21:22:21 -05:00
.split("/")
.slice(0, -1)
2026-04-01 21:40:15 -05:00
.join("/")}/texture0.packed.webp`,
),
2025-10-23 21:22:21 -05:00
);
2025-11-26 13:18:44 -06:00
const sizes = (await vfs.text(`Data/${configPath}.txt`))
2025-10-23 21:22:21 -05:00
.split("\n")[4]
.split(" ")
.map(Number);
const content = await provisionsTexture
.clone()
.extract({
left: sizes[0],
top: sizes[1],
width: sizes[2],
height: sizes[3],
})
.toBuffer();
2024-01-25 13:51:30 -06:00
2025-10-23 21:22:21 -05:00
await uploader.add({
path: `icons/provisions/${provision.id}.webp`,
content,
});
}
}
2024-08-14 14:04:06 -05:00
}
2024-01-25 13:51:30 -06:00
2025-10-23 21:22:21 -05:00
await uploader.flush();
2024-01-17 11:34:27 -06:00
}