Files
blitzkit/scripts/buildAssets.ts
T

422 lines
13 KiB
TypeScript
Raw Normal View History

2023-12-04 10:13:14 -06:00
import { config } from 'dotenv';
2023-12-09 15:56:25 -06:00
import { existsSync } from 'fs';
import { mkdir, readdir, rm } from 'fs/promises';
2023-12-06 10:17:42 -06:00
import { argv } from 'process';
2023-12-05 19:14:53 -06:00
import { TankType } from '../src/components/Tanks';
2023-11-06 23:02:26 -06:00
import { NATION_IDS } from '../src/constants/nations';
2023-12-09 15:56:25 -06:00
import { SCPGStream } from '../src/core/blitz/SCPGStream';
2023-12-06 10:17:42 -06:00
import { readBase64DVPL } from '../src/core/blitz/readBase64DVPL';
2023-12-05 19:14:53 -06:00
import { readXMLDVPL } from '../src/core/blitz/readXMLDVPL';
import { readYAMLDVPL } from '../src/core/blitz/readYAMLDVPL';
2023-12-12 13:40:58 -06:00
import { toUniqueId } from '../src/core/blitz/toUniqueId';
2023-12-06 10:17:42 -06:00
import commitMultipleFiles, {
FileChange,
} from '../src/core/blitzkrieg/commitMultipleFiles';
2023-12-12 13:40:58 -06:00
import { BlitzkriegGunDefinitions } from '../src/core/blitzkrieg/definitions/guns';
2023-12-05 17:36:18 -06:00
import {
2023-12-12 13:40:58 -06:00
BlitzkriegTankDefinitions,
2023-12-05 17:36:18 -06:00
Tier,
} from '../src/core/blitzkrieg/definitions/tanks';
2023-12-12 13:40:58 -06:00
import { BlitzkriegTurretDefinitions } from '../src/core/blitzkrieg/definitions/turrets';
2023-12-04 10:13:14 -06:00
config();
2023-11-06 23:02:26 -06:00
2023-12-12 13:40:58 -06:00
// WARNING! MOST OF THESE TYPES ARE NOT EXHAUSTIVE!
2023-12-05 19:14:53 -06:00
interface VehicleDefinitionList {
[key: string]: {
id: number;
userString: string;
shortUserString?: string;
description: string;
price: number | { gold: ''; '#text': number };
sellPrice?: { gold: ''; '#text': number };
enrichmentPermanentCost: number;
notInShop?: boolean;
tags: string;
level: number;
combatRole?: Record<string, string>;
configurationModes: string;
};
}
2023-12-12 13:40:58 -06:00
interface TurretDefinitionsList {
nextAvailableId: number;
ids: Record<string, number>;
}
interface GunDefinitionsList {
nextAvailableId: number;
ids: Record<string, number>;
shared: {
[key: string]: {
userString: string;
tags: string;
level: number;
pitchLimits: string;
2023-12-12 14:14:26 -06:00
burst?: { count: number; rate: number };
clip?: { count: number; rate: number };
2023-12-12 13:40:58 -06:00
};
};
}
interface VehicleDefinitions {
turrets0: {
[key: string]: {
userString: number;
level: number;
yawLimits: string | string[];
guns: {
[key: string]: {
maxAmmo: number;
pitchLimits?: string | string[];
};
};
};
};
}
2023-12-05 19:14:53 -06:00
interface Strings {
[key: string]: string;
}
type BlitzTankType = 'AT-SPG' | 'lightTank' | 'mediumTank' | 'heavyTank';
2023-12-06 10:17:42 -06:00
interface TankParameters {
resourcesPath: {
smallIconPath: string;
bigIconPath: string;
2023-12-09 15:56:25 -06:00
blitzModelPath: string;
2023-12-06 10:17:42 -06:00
};
}
2023-12-05 19:14:53 -06:00
const blitzTankTypeToBlitzkrieg: Record<BlitzTankType, TankType> = {
'AT-SPG': 'tank_destroyer',
lightTank: 'light',
mediumTank: 'medium',
heavyTank: 'heavy',
};
2023-12-05 17:59:58 -06:00
const DATA =
'C:/Program Files (x86)/Steam/steamapps/common/World of Tanks Blitz/Data';
2023-11-27 17:50:02 -06:00
const LANGUAGE = 'en';
2023-12-05 19:14:53 -06:00
const DOI = {
2023-12-05 17:59:58 -06:00
vehicleDefinitions: 'XML/item_defs/vehicles',
strings: 'Strings',
tankParameters: '3d/Tanks/Parameters',
smallIcons: 'Gfx/UI/BattleScreenHUD/SmallTankIcons',
bigIcons: 'Gfx/UI/BigTankIcons',
flags: 'Gfx/Lobby/flags',
2023-12-09 15:56:25 -06:00
'3d': '3d',
2023-11-27 17:50:02 -06:00
};
2023-12-06 10:17:42 -06:00
const allTargets = argv.includes('--all-targets');
2023-12-05 19:14:53 -06:00
const targets = argv
.find((argument) => argument.startsWith('--target'))
?.split('=')[1]
.split(',');
2023-11-16 12:57:46 -06:00
2023-12-06 10:19:13 -06:00
if (!targets && !allTargets) throw new Error('No target(s) specified');
2023-12-05 17:59:58 -06:00
2023-12-12 13:40:58 -06:00
if (allTargets || targets?.includes('definitions')) {
2023-12-05 19:14:53 -06:00
console.log('Building tank definitions...');
2023-11-06 23:02:26 -06:00
2023-12-12 13:40:58 -06:00
const tankDefinitions: BlitzkriegTankDefinitions = {};
const turretDefinitions: BlitzkriegTurretDefinitions = {};
const gunDefinitions: BlitzkriegGunDefinitions = {};
2023-12-05 19:14:53 -06:00
const nations = await readdir(`${DATA}/${DOI.vehicleDefinitions}`).then(
(nations) => nations.filter((nation) => nation !== 'common'),
);
const strings = await readYAMLDVPL<Strings>(
`${DATA}/${DOI.strings}/${LANGUAGE}.yaml.dvpl`,
2023-12-05 17:36:18 -06:00
);
2023-12-05 19:14:53 -06:00
await Promise.all(
nations.map(async (nation) => {
2023-12-12 13:40:58 -06:00
const tankList = await readXMLDVPL<{ root: VehicleDefinitionList }>(
2023-12-05 19:14:53 -06:00
`${DATA}/${DOI.vehicleDefinitions}/${nation}/list.xml.dvpl`,
);
2023-12-12 13:40:58 -06:00
const turretList = await readXMLDVPL<{
root: TurretDefinitionsList;
}>(
`${DATA}/${DOI.vehicleDefinitions}/${nation}/components/turrets.xml.dvpl`,
);
const gunList = await readXMLDVPL<{
root: GunDefinitionsList;
}>(
`${DATA}/${DOI.vehicleDefinitions}/${nation}/components/guns.xml.dvpl`,
);
2023-12-05 19:14:53 -06:00
2023-12-12 13:40:58 -06:00
for (const tankKey in tankList.root) {
const tank = tankList.root[tankKey];
const tankDefinition = await readXMLDVPL<{ root: VehicleDefinitions }>(
`${DATA}/${DOI.vehicleDefinitions}/${nation}/${tankKey}.xml.dvpl`,
);
const tankId = toUniqueId(nation, tank.id);
const tankTags = tank.tags.split(' ');
const tankTurrets = Object.keys(tankDefinition.root.turrets0).map(
(turretKey) => {
const turret = tankDefinition.root.turrets0[turretKey];
const turretId = toUniqueId(nation, turretList.root.ids[turretKey]);
2023-12-12 14:14:26 -06:00
const turretGuns = Object.keys(turret.guns).map((gunKey) => {
const turretGunEntry = turret.guns[gunKey];
const gunId = toUniqueId(nation, gunList.root.ids[gunKey]);
const gun = gunList.root.shared[gunKey];
const pitchLimitsRaw =
turretGunEntry.pitchLimits ?? gun.pitchLimits;
gunDefinitions[gunId] = {
id: gunId,
pitch: (typeof pitchLimitsRaw === 'string'
? pitchLimitsRaw
: pitchLimitsRaw.at(-1)!
)
.split(' ')
.map(Number) as [number, number],
name: strings[gun.userString] ?? gunKey.replaceAll('_', ' '),
tier: gun.level,
};
return gunId;
});
2023-12-05 19:14:53 -06:00
2023-12-12 13:40:58 -06:00
turretDefinitions[turretId] = {
id: turretId,
name:
strings[turret.userString] ??
turretKey
.replaceAll('_', ' ')
.replace(/^(Turret ([0-9] )?)+/, ''),
tier: turret.level,
yaw: (typeof turret.yawLimits === 'string'
? turret.yawLimits
2023-12-12 14:14:26 -06:00
: turret.yawLimits.at(-1)!
2023-12-12 13:40:58 -06:00
)
.split(' ')
.map(Number) as [number, number],
guns: turretGuns,
};
2023-12-05 19:14:53 -06:00
2023-12-12 13:40:58 -06:00
return turretId;
},
);
tankDefinitions[tankId] = {
id: tankId,
name:
(tank.shortUserString
? strings[tank.shortUserString]
: undefined) ??
strings[tank.userString] ??
tankKey.replaceAll('_', ' '),
2023-12-05 19:14:53 -06:00
nation,
2023-12-12 13:40:58 -06:00
tree_type: (tank.sellPrice ? 'gold' in tank.sellPrice : false)
2023-12-05 19:14:53 -06:00
? 'collector'
2023-12-12 13:40:58 -06:00
: (typeof tank.price === 'number' ? false : 'gold' in tank.price)
2023-12-05 19:14:53 -06:00
? 'premium'
: 'researchable',
2023-12-12 13:40:58 -06:00
tier: tank.level as Tier,
type: blitzTankTypeToBlitzkrieg[tankTags[0] as BlitzTankType],
testing: tankTags.includes('testTank') ? true : undefined,
turrets: tankTurrets,
2023-12-05 19:14:53 -06:00
};
}
}),
);
2023-12-12 13:40:58 -06:00
// rmSync('dist/definitions', { force: true, recursive: true });
// mkdirSync('dist/definitions', { recursive: true });
// writeFile(
// 'dist/definitions/tanks.json',
// JSON.stringify(tankDefinitions, null, 2),
// );
// writeFile(
// 'dist/definitions/turrets.json',
// JSON.stringify(turretDefinitions, null, 2),
// );
// writeFile(
// 'dist/definitions/guns.json',
// JSON.stringify(gunDefinitions, null, 2),
// );
// console.log('Committing tank definitions...');
// await commitMultipleFiles(
// 'tresabhi',
// 'blitzkrieg-assets',
// 'main',
// 'tank definitions',
// [
// {
// content: JSON.stringify(tankDefinitions),
// encoding: 'utf-8',
// path: 'definitions/tanks.json',
// },
// ],
// true,
// );
2023-12-05 19:14:53 -06:00
}
2023-12-06 10:17:42 -06:00
if (
allTargets ||
2023-12-06 10:19:13 -06:00
targets?.includes('bigTankIcons') ||
targets?.includes('smallTankIcons')
2023-12-06 10:17:42 -06:00
) {
console.log('Building tank icons...');
2023-12-05 19:14:53 -06:00
2023-12-06 10:17:42 -06:00
const changes: FileChange[] = [];
const nations = await readdir(`${DATA}/${DOI.vehicleDefinitions}`).then(
(nations) => nations.filter((nation) => nation !== 'common'),
);
2023-12-05 19:14:53 -06:00
2023-12-06 10:17:42 -06:00
await Promise.all(
nations.map(async (nation) => {
2023-12-12 13:40:58 -06:00
const tanks = await readXMLDVPL<{ root: VehicleDefinitionList }>(
2023-12-06 10:17:42 -06:00
`${DATA}/${DOI.vehicleDefinitions}/${nation}/list.xml.dvpl`,
);
2023-12-05 19:14:53 -06:00
2023-12-12 13:40:58 -06:00
for (const tankIndex in tanks.root) {
const tank = tanks.root[tankIndex];
2023-12-06 10:17:42 -06:00
const nationVehicleId = tank.id;
const id = (nationVehicleId << 8) + (NATION_IDS[nation] << 4) + 1;
const parameters = await readYAMLDVPL<TankParameters>(
`${DATA}/${DOI.tankParameters}/${nation}/${tankIndex}.yaml.dvpl`,
);
const small = `${DATA}/${parameters.resourcesPath.smallIconPath
.replace(/~res:\//, '')
.replace(/\..+/, '')}.packed.webp.dvpl`;
const big = `${DATA}/${parameters.resourcesPath.bigIconPath
.replace(/~res:\//, '')
.replace(/\..+/, '')}.packed.webp.dvpl`;
2023-12-05 19:14:53 -06:00
2023-12-06 10:19:13 -06:00
if (allTargets || targets?.includes('bigTankIcons')) {
2023-12-06 10:17:42 -06:00
changes.push({
content: await readBase64DVPL(big),
encoding: 'base64',
path: `icons/big/${id}.webp`,
});
}
2023-12-06 10:19:13 -06:00
if (allTargets || targets?.includes('smallTankIcons')) {
2023-12-06 10:17:42 -06:00
changes.push({
content: await readBase64DVPL(small),
encoding: 'base64',
path: `icons/small/${id}.webp`,
});
}
}
}),
);
2023-12-05 19:14:53 -06:00
2023-12-06 10:17:42 -06:00
console.log('Committing tank icons...');
await commitMultipleFiles(
'tresabhi',
'blitzkrieg-assets',
'main',
'tank icons',
changes,
true,
);
}
2023-12-05 19:14:53 -06:00
2023-12-06 10:19:13 -06:00
if (allTargets || targets?.includes('scratchedFlags')) {
2023-12-06 10:17:42 -06:00
console.log('Building scratched flags...');
2023-12-05 19:14:53 -06:00
2023-12-06 10:17:42 -06:00
const flags = await readdir(`${DATA}/${DOI.flags}`);
2023-12-05 19:14:53 -06:00
2023-12-06 10:17:42 -06:00
console.log('Committing scratched flags...');
commitMultipleFiles(
'tresabhi',
'blitzkrieg-assets',
'main',
'scratched flags',
await Promise.all(
flags
.filter(
(flag) =>
flag.startsWith('flag_tutor-tank_') &&
!flag.endsWith('@2x.packed.webp.dvpl'),
)
.map(async (flag) => {
const content = await readBase64DVPL(`${DATA}/${DOI.flags}/${flag}`);
const name = flag.match(/flag_tutor-tank_(.+)\.packed\.webp/)![1];
2023-12-05 19:14:53 -06:00
2023-12-06 10:17:42 -06:00
return {
content,
encoding: 'base64',
path: `flags/scratched/${name}.webp`,
} satisfies FileChange;
}),
),
true,
);
}
2023-12-05 19:14:53 -06:00
2023-12-06 10:19:13 -06:00
if (allTargets || targets?.includes('circleFlags')) {
2023-12-06 10:17:42 -06:00
const flags = await readdir(`${DATA}/${DOI.flags}`);
2023-12-05 19:14:53 -06:00
2023-12-06 10:17:42 -06:00
commitMultipleFiles(
'tresabhi',
'blitzkrieg-assets',
'main',
'circle flags',
await Promise.all(
flags
.filter(
(flag) =>
flag.startsWith('flag_profile-stat_') &&
!flag.endsWith('@2x.packed.webp.dvpl'),
)
.map(async (flag) => {
const content = await readBase64DVPL(`${DATA}/${DOI.flags}/${flag}`);
const name = flag.match(/flag_profile-stat_(.+)\.packed\.webp/)![1];
2023-12-05 19:14:53 -06:00
2023-12-06 10:17:42 -06:00
return {
content,
encoding: 'base64',
path: `flags/circle/${name}.webp`,
} satisfies FileChange;
}),
),
true,
);
}
2023-12-09 15:56:25 -06:00
if (allTargets || targets?.includes('tankModels')) {
console.log('Building tank models...');
if (existsSync('dist/assets/models')) {
await rm('dist/assets/models', { recursive: true });
}
await mkdir('dist/assets/models', { recursive: true });
const changes: FileChange[] = [];
const nations = await readdir(`${DATA}/${DOI.vehicleDefinitions}`).then(
(nations) => nations.filter((nation) => nation !== 'common'),
);
await Promise.all(
2023-12-09 19:05:46 -06:00
nations.map(async (nation) => {
2023-12-12 13:40:58 -06:00
const tanks = await readXMLDVPL<{ root: VehicleDefinitionList }>(
2023-12-09 15:56:25 -06:00
`${DATA}/${DOI.vehicleDefinitions}/${nation}/list.xml.dvpl`,
);
2023-12-12 13:40:58 -06:00
for (const tankIndex in tanks.root) {
const tank = tanks.root[tankIndex];
2023-12-09 15:56:25 -06:00
const nationVehicleId = tank.id;
const id = (nationVehicleId << 8) + (NATION_IDS[nation] << 4) + 1;
2023-12-12 12:01:50 -06:00
// if (id !== 897) continue;
2023-12-10 17:03:01 -06:00
console.log(`Building model ${id} @ ${nation}/${tankIndex}`);
2023-12-09 15:56:25 -06:00
const parameters = await readYAMLDVPL<TankParameters>(
`${DATA}/${DOI.tankParameters}/${nation}/${tankIndex}.yaml.dvpl`,
);
const sc2Path = parameters.resourcesPath.blitzModelPath;
2023-12-10 17:03:01 -06:00
const scgPath = parameters.resourcesPath.blitzModelPath.replace(
/\.sc2$/,
'.scg',
);
2023-12-09 15:56:25 -06:00
const sc2Stream = await SCPGStream.fromDVPLFile(
`${DATA}/${DOI['3d']}/${sc2Path}.dvpl`,
);
2023-12-10 17:03:01 -06:00
const scgStream = await SCPGStream.fromDVPLFile(
`${DATA}/${DOI['3d']}/${scgPath}.dvpl`,
);
2023-12-12 12:01:50 -06:00
2023-12-10 17:14:05 -06:00
const sc2 = sc2Stream.consumeSC2();
const scg = scgStream.consumeSCG();
2023-12-09 15:56:25 -06:00
}
}),
);
}