Files
blitzkit/scripts/buildAssets/consumableProvisionIcons.ts
T

164 lines
4.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-04-23 09:57:59 -05:00
import { commitAssets } from '../../src/core/blitzkit/commitAssets';
import { FileChange } from '../../src/core/blitzkit/commitMultipleFiles';
2024-02-20 11:21:01 -06:00
import { DATA, POI } from './constants';
2024-02-13 19:46:34 -06:00
import { ConsumablesCommon, ProvisionsCommon } 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:51:30 -06:00
export async function consumableProvisionIcons(production: boolean) {
console.log('Building consumable and provision icons...');
2024-01-17 11:34:27 -06:00
const changes: FileChange[] = [];
2024-01-25 13:51:30 -06:00
const consumablesTexture = sharp(
2024-01-17 11:34:27 -06:00
await readDVPLFile(
`${DATA}/${POI.consumableIcons}/texture0.packed.webp.dvpl`,
),
);
2024-01-25 13:51:30 -06:00
const provisionsTexture = sharp(
await readDVPLFile(
`${DATA}/${POI.provisionIcons}/texture0.packed.webp.dvpl`,
),
);
2024-01-17 11:34:27 -06:00
const equipmentItemImageMappings = await readYAMLDVPL<Mappings>(
`${DATA}/${POI.equipmentItemImageMappings}.dvpl`,
);
2024-01-25 13:10:20 -06:00
const consumablesCommon = await readXMLDVPL<{
2024-02-13 19:46:34 -06:00
root: ConsumablesCommon;
2024-01-25 13:10:20 -06:00
}>(`${DATA}/${POI.consumablesCommon}.dvpl`);
2024-01-25 13:51:30 -06:00
const provisionsCommon = await readXMLDVPL<{
2024-02-13 19:46:34 -06:00
root: ProvisionsCommon;
2024-01-25 13:51:30 -06:00
}>(`${DATA}/${POI.provisionsCommon}.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) {
2024-01-25 13:51:30 -06:00
console.warn(
`No style sheet found for consumable ${consumable.icon}; skipping...`,
2024-01-17 11:34:27 -06:00
);
2024-01-25 13:51:30 -06:00
return;
2024-01-17 11:34:27 -06:00
}
const configPath = styleSheet.properties['bg-sprite']
.replace('~res:/', '')
2024-01-25 13:51:30 -06:00
.replace('.psd', '')
.replace('.txt', '');
2024-01-17 11:34:27 -06:00
if (configPath.startsWith('Gfx/Shared')) {
2024-02-12 19:24:34 -06:00
const image = sharp(
await readDVPLFile(`${DATA}/${configPath}.packed.webp.dvpl`),
);
2024-02-17 10:36:00 -06:00
const content = (
await image.trim({ threshold: 100 }).toBuffer()
).toString('base64');
2024-01-17 11:34:27 -06:00
changes.push({
path: `icons/consumables/${consumable.id}.webp`,
encoding: 'base64',
content,
});
} else {
2024-01-25 13:51:30 -06:00
const sizes = (await readStringDVPL(`${DATA}/${configPath}.txt.dvpl`))
2024-01-17 11:34:27 -06:00
.split('\n')[4]
.split(' ')
.map(Number);
const content = (
2024-01-25 13:51:30 -06:00
await consumablesTexture
2024-01-17 11:34:27 -06:00
.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-25 13:51:30 -06:00
await Promise.all(
Object.values(provisionsCommon.root).map(async (provision) => {
const styleSheet = styleSheets.find((styleSheet) =>
styleSheet.selectors.includes(
`.equipment_item.${provision.icon} * .equipment-img`,
),
);
if (!styleSheet) {
console.warn(
`No style sheet found for provision ${provision.icon}; skipping...`,
);
return;
}
const configPath = styleSheet.properties['bg-sprite']
.replace('~res:/', '')
.replace('.psd', '')
.replace('.txt', '');
if (configPath.startsWith('Gfx/Shared')) {
2024-02-12 19:24:34 -06:00
const image = sharp(
await readDVPLFile(`${DATA}/${configPath}.packed.webp.dvpl`),
);
2024-02-17 10:36:00 -06:00
const content = (
await image.trim({ threshold: 100 }).toBuffer()
).toString('base64');
2024-01-25 13:51:30 -06:00
changes.push({
path: `icons/provisions/${provision.id}.webp`,
encoding: 'base64',
content,
});
} else {
const sizes = (await readStringDVPL(`${DATA}/${configPath}.txt.dvpl`))
.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()
).toString('base64');
changes.push({
path: `icons/provisions/${provision.id}.webp`,
encoding: 'base64',
content,
});
}
}),
);
await commitAssets('consumable and provision icons', changes, production);
2024-01-17 11:34:27 -06:00
}