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

178 lines
5.6 KiB
TypeScript
Raw Normal View History

2024-08-14 14:04:06 -05:00
import { existsSync } from 'fs';
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';
2024-09-07 21:45:00 -05:00
import { readXMLDVPL } from '../core/blitz/readXMLDVPL';
import { readYAMLDVPL } from '../core/blitz/readYAMLDVPL';
import { commitAssets } from '../core/github/commitAssets';
import { FileChange } from '../core/github/commitMultipleFiles';
2024-09-29 22:42:15 -05:00
import { DATA } from './constants';
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-08-14 14:04:06 -05:00
const listItemsPattern = /<items path="(.+)\.xml"\/>/g;
2024-08-27 14:40:43 -05:00
export async function consumableProvisionIcons() {
2024-01-25 13:51:30 -06:00
console.log('Building consumable and provision icons...');
2024-01-17 11:34:27 -06:00
const changes: FileChange[] = [];
2024-08-14 14:04:06 -05:00
const styles = [
2024-08-25 21:32:35 -05:00
'UI/Styles/Lobby/Inventory/Event/InventoryBigStyles.yaml',
'UI/Styles/Lobby/Inventory/Event/InventoryNormalStyles.yaml',
2024-08-14 14:04:06 -05:00
'UI/Styles/Lobby/Inventory/InventoryNormalStyles.yaml',
'UI/Screens3/Lobby/Inventory/Equipment/EquipmentItemImage.style.yaml',
'UI/Screens/Battle/Styles/BattleEquipmentStyles.yaml',
];
const styleSheets = await Promise.all(
styles.map(async (path) =>
Object.values(
2025-01-14 19:22:41 -06:00
(await readYAMLDVPL<Mappings>(`${DATA}/${path}`)).StyleSheets,
2024-08-14 14:04:06 -05:00
),
2024-01-17 11:34:27 -06:00
),
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 (
await readStringDVPL(
2025-01-14 19:22:41 -06:00
`${DATA}/XML/item_defs/vehicles/common/consumables/list.xml`,
2024-08-14 14:04:06 -05:00
)
).matchAll(listItemsPattern)) {
const consumablesCommon = await readXMLDVPL<{
root: ConsumablesCommon;
2025-01-14 19:22:41 -06:00
}>(`${DATA}/XML/item_defs/vehicles/common/consumables/${match[1]}.xml`);
2024-01-17 11:34:27 -06:00
2024-08-14 14:04:06 -05:00
await Promise.all(
Object.values(consumablesCommon.root).map(async (consumable) => {
const styleSheet = styleSheets.find((styleSheet) =>
styleSheet.selectors.some((selector) =>
2024-08-25 21:32:35 -05:00
selector.includes(`${consumable.icon} `),
2024-08-14 14:04:06 -05:00
),
2024-01-17 11:34:27 -06:00
);
2024-08-14 14:04:06 -05:00
if (!styleSheet) {
console.warn(
`No style sheet found for consumable ${consumable.icon}; skipping...`,
);
return;
}
2024-01-17 11:34:27 -06:00
2024-08-14 14:04:06 -05:00
const configPath = styleSheet.properties['bg-sprite']
.replace('~res:/', '')
.replace('.psd', '')
.replace('.txt', '');
2025-01-14 19:22:41 -06:00
if (existsSync(`${DATA}/${configPath}.packed.webp`)) {
2024-08-14 14:04:06 -05:00
const image = sharp(
2025-01-14 19:22:41 -06:00
await readDVPLFile(`${DATA}/${configPath}.packed.webp`),
2024-08-14 14:04:06 -05:00
);
2024-10-21 18:32:52 -05:00
const content = await image.trim({ threshold: 100 }).toBuffer();
2024-08-14 14:04:06 -05:00
changes.push({
path: `icons/consumables/${consumable.id}.webp`,
content,
});
} else {
const consumablesTexture = sharp(
await readDVPLFile(
2025-01-14 19:22:41 -06:00
`${`${DATA}/${configPath}`.split('/').slice(0, -1).join('/')}/texture0.packed.webp`,
2024-08-14 14:04:06 -05:00
),
);
2025-01-14 19:22:41 -06:00
const sizes = (await readStringDVPL(`${DATA}/${configPath}.txt`))
2024-08-14 14:04:06 -05:00
.split('\n')[4]
.split(' ')
.map(Number);
2024-10-21 18:32:52 -05:00
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
changes.push({
path: `icons/consumables/${consumable.id}.webp`,
content,
});
}
}),
);
}
for (const match of (
await readStringDVPL(
2025-01-14 19:22:41 -06:00
`${DATA}/XML/item_defs/vehicles/common/provisions/list.xml`,
2024-08-14 14:04:06 -05:00
)
).matchAll(listItemsPattern)) {
const provisionsCommon = await readXMLDVPL<{
root: ProvisionsCommon;
2025-01-14 19:22:41 -06:00
}>(`${DATA}/XML/item_defs/vehicles/common/provisions/${match[1]}.xml`);
2024-08-14 14:04:06 -05:00
await Promise.all(
Object.values(provisionsCommon.root).map(async (provision) => {
const styleSheet = styleSheets.find((styleSheet) =>
styleSheet.selectors.some((selector) =>
2024-08-25 21:32:35 -05:00
selector.includes(`${provision.icon} `),
2024-08-14 14:04:06 -05:00
),
2024-02-12 19:24:34 -06:00
);
2024-01-17 11:34:27 -06:00
2024-08-14 14:04:06 -05:00
if (!styleSheet) {
console.warn(
`No style sheet found for provision ${provision.icon}; skipping...`,
);
return;
}
2024-01-17 11:34:27 -06:00
2024-08-14 14:04:06 -05:00
const configPath = styleSheet.properties['bg-sprite']
.replace('~res:/', '')
.replace('.psd', '')
.replace('.txt', '');
2024-01-17 11:34:27 -06:00
2025-01-14 19:22:41 -06:00
if (existsSync(`${DATA}/${configPath}.packed.webp`)) {
2024-08-14 14:04:06 -05:00
const image = sharp(
2025-01-14 19:22:41 -06:00
await readDVPLFile(`${DATA}/${configPath}.packed.webp`),
2024-08-14 14:04:06 -05:00
);
2024-10-21 18:32:52 -05:00
const content = await image.trim({ threshold: 100 }).toBuffer();
2024-01-25 13:51:30 -06:00
2024-08-14 14:04:06 -05:00
changes.push({
path: `icons/provisions/${provision.id}.webp`,
content,
});
} else {
const provisionsTexture = sharp(
await readDVPLFile(
2025-01-14 19:22:41 -06:00
`${`${DATA}/${configPath}`.split('/').slice(0, -1).join('/')}/texture0.packed.webp`,
2024-08-14 14:04:06 -05:00
),
);
2025-01-14 19:22:41 -06:00
const sizes = (await readStringDVPL(`${DATA}/${configPath}.txt`))
2024-08-14 14:04:06 -05:00
.split('\n')[4]
.split(' ')
.map(Number);
2024-10-21 18:32:52 -05:00
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
2024-08-14 14:04:06 -05:00
changes.push({
path: `icons/provisions/${provision.id}.webp`,
content,
});
}
}),
);
}
2024-01-25 13:51:30 -06:00
2024-08-27 14:40:43 -05:00
await commitAssets('consumable and provision icons', changes);
2024-01-17 11:34:27 -06:00
}