Files
blitzkit/scripts/buildAssets/consumableIcons.ts
T

93 lines
2.8 KiB
TypeScript
Raw Normal View History

2024-01-17 11:34:27 -06:00
import sharp from 'sharp';
import { readDVPLFile } from '../../src/core/blitz/readDVPLFile';
import { readStringDVPL } from '../../src/core/blitz/readStringDVPL';
import { readXMLDVPL } from '../../src/core/blitz/readXMLDVPL';
import { readYAMLDVPL } from '../../src/core/blitz/readYAMLDVPL';
2024-01-20 21:35:36 -06:00
import { commitAssets } from '../../src/core/blitzkrieg/commitAssets';
import { FileChange } from '../../src/core/blitzkrieg/commitMultipleFiles';
2024-01-17 11:34:27 -06:00
import { DATA, POI } from './constants';
2024-01-25 13:10:20 -06:00
import { ConsumablesProvisionsCommon } from './definitions';
2024-01-17 11:34:27 -06:00
interface Mappings {
Header: { version: number };
StyleSheets: {
selectors: string[];
properties: { 'bg-sprite': string };
}[];
}
2024-01-25 13:10:20 -06:00
export async function consumableIcons(production: boolean) {
2024-01-17 11:34:27 -06:00
console.log('Building consumable icons...');
const changes: FileChange[] = [];
const texture = sharp(
await readDVPLFile(
`${DATA}/${POI.consumableIcons}/texture0.packed.webp.dvpl`,
),
);
const equipmentItemImageMappings = await readYAMLDVPL<Mappings>(
`${DATA}/${POI.equipmentItemImageMappings}.dvpl`,
);
2024-01-25 13:10:20 -06:00
const consumablesCommon = await readXMLDVPL<{
root: ConsumablesProvisionsCommon;
}>(`${DATA}/${POI.consumablesCommon}.dvpl`);
2024-01-17 11:34:27 -06:00
const styleSheets = Object.values(equipmentItemImageMappings.StyleSheets);
2024-01-20 21:35:36 -06:00
await Promise.all(
2024-01-17 11:34:27 -06:00
Object.values(consumablesCommon.root).map(async (consumable) => {
const styleSheet = styleSheets.find((styleSheet) =>
styleSheet.selectors.includes(
`.equipment_item.${consumable.icon} * .equipment-img`,
),
);
if (!styleSheet) {
throw new Error(
`No style sheet found for consumable ${consumable.icon}`,
);
}
const configPath = styleSheet.properties['bg-sprite']
.replace('~res:/', '')
.replace('.psd', '.txt');
if (configPath.startsWith('Gfx/Shared')) {
const imagePath = `${configPath}.packed.webp.dvpl`;
const content = (await readDVPLFile(`${DATA}/${imagePath}`)).toString(
'base64',
);
changes.push({
path: `icons/consumables/${consumable.id}.webp`,
encoding: 'base64',
content,
});
} else {
const sizes = (await readStringDVPL(`${DATA}/${configPath}.dvpl`))
.split('\n')[4]
.split(' ')
.map(Number);
const content = (
await texture
.clone()
.extract({
left: sizes[0],
top: sizes[1],
width: sizes[2],
height: sizes[3],
})
.toBuffer()
).toString('base64');
changes.push({
path: `icons/consumables/${consumable.id}.webp`,
encoding: 'base64',
content,
});
}
}),
);
2024-01-21 00:01:32 -06:00
await commitAssets('consumable icons', changes, production);
2024-01-17 11:34:27 -06:00
}