Files
blitzkit/scripts/buildAssets.ts
T

206 lines
6.4 KiB
TypeScript
Raw Normal View History

2023-11-27 16:52:38 -06:00
import { Octokit } from '@octokit/rest';
2023-12-04 10:13:14 -06:00
import { config } from 'dotenv';
2023-11-30 12:59:37 -06:00
import { writeFileSync } from 'fs';
2023-12-05 17:59:58 -06:00
import { readFile, readdir } from 'fs/promises';
2023-11-06 23:02:26 -06:00
import { load } from 'js-yaml';
2023-11-27 16:52:38 -06:00
import { argv, env } from 'process';
2023-11-06 23:02:26 -06:00
import { ElementCompact, xml2js } from 'xml-js';
2023-11-16 12:57:46 -06:00
import { parse } from 'yaml';
2023-11-06 23:02:26 -06:00
import { NATION_IDS } from '../src/constants/nations';
2023-12-05 17:59:58 -06:00
import { readDVPL } from '../src/core/blitz/readDVPL';
2023-12-05 17:36:18 -06:00
import {
TankDefinitions,
Tier,
} from '../src/core/blitzkrieg/definitions/tanks';
2023-12-04 10:13:14 -06:00
import commitMultipleFiles, { FileChange } from './commitMultipleFiles';
config();
2023-11-06 23:02:26 -06:00
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 17:59:58 -06:00
const targets = argv
.find((argument) => argument.startsWith('--target'))
?.split('=')[1]
.split(',');
2023-11-27 16:52:38 -06:00
2023-12-05 17:59:58 -06:00
if (!targets) throw new Error('No target(s) specified');
2023-11-16 12:57:46 -06:00
2023-11-27 17:50:02 -06:00
const directoriesOfInterest = {
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-11-27 17:50:02 -06:00
};
2023-12-05 17:59:58 -06:00
console.log(
(
await readDVPL(
`${DATA}/${directoriesOfInterest.strings}/${LANGUAGE}.yaml.dvpl`,
)
).toString(),
);
throw new Error('stop');
2023-11-16 12:57:46 -06:00
2023-11-06 23:02:26 -06:00
const strings = await readFile(
2023-12-05 17:59:58 -06:00
`${DATA}/${directoriesOfInterest.strings}/${LANGUAGE}.yaml.dvpl`,
2023-11-06 23:02:26 -06:00
'utf-8',
).then((data) => load(data) as Record<string, string>);
2023-12-05 17:59:58 -06:00
2023-12-05 17:36:18 -06:00
const tankDefinitions: TankDefinitions = {};
2023-11-27 16:52:38 -06:00
const tankIds: number[] = [];
const images: Record<number, { big: string; small: string }> = {};
2023-11-27 17:50:02 -06:00
const nations = await readdir(
`${TEMP}/${directoriesOfInterest.vehicleDefinitions}`,
);
2023-11-06 23:02:26 -06:00
2023-11-30 12:59:37 -06:00
writeFileSync('test.json', JSON.stringify(strings, null, 2));
2023-11-16 12:57:46 -06:00
Promise.all(
2023-11-06 23:02:26 -06:00
nations
.filter((nation) => nation !== 'common')
.map(async (nation) => {
const listXML = await readFile(
2023-11-27 17:50:02 -06:00
`${TEMP}/${directoriesOfInterest.vehicleDefinitions}/${nation}/list.xml`,
2023-11-06 23:02:26 -06:00
'utf-8',
);
const list = xml2js(listXML, { compact: true }) as ElementCompact;
await Promise.all(
Object.entries<ElementCompact>(list.root).map(
async ([vehicle, vehicleData]) => {
const nationVehicleId = parseInt(list.root[vehicle].id._text);
const id = (nationVehicleId << 8) + (NATION_IDS[nation] << 4) + 1;
2023-11-30 12:21:15 -06:00
const name = strings[vehicleData.userString._text];
2023-11-30 12:59:37 -06:00
const name_short = strings[vehicleData.shortUserString?._text];
2023-11-27 12:26:51 -06:00
const isPremium = 'gold' in vehicleData.price;
const isCollector = vehicleData.sellPrice
2023-11-06 23:02:26 -06:00
? 'gold' in vehicleData.sellPrice
: false;
2023-11-29 12:17:21 -06:00
const tier = parseInt(vehicleData.level._text) as Tier;
2023-11-06 23:02:26 -06:00
const tags = vehicleData.tags._text.split(' ');
const type = tags[0];
2023-11-30 12:59:37 -06:00
if ((name ?? name_short) === undefined) {
2023-12-04 10:13:14 -06:00
console.warn(`Missing name for ${vehicle}`);
2023-11-30 12:59:37 -06:00
}
2023-11-30 12:21:15 -06:00
2023-11-27 16:52:38 -06:00
tankIds.push(id);
2023-12-05 17:36:18 -06:00
tankDefinitions[id] = {
2023-11-06 23:02:26 -06:00
id,
name,
name_short,
nation,
2023-11-27 12:26:51 -06:00
tree_type: isCollector
? 'collector'
: isPremium
2023-12-03 22:09:08 -06:00
? 'premium'
2023-12-04 18:48:52 -06:00
: 'researchable',
2023-11-06 23:02:26 -06:00
tier,
type,
};
2023-11-16 12:57:46 -06:00
// Tank images
2023-11-27 17:50:02 -06:00
const parameterPath = `${TEMP}/${directoriesOfInterest.tankParameters}/${nation}/${vehicle}.yaml`;
2023-11-16 12:57:46 -06:00
const parameterString = await readFile(parameterPath, 'utf-8');
const { smallIconPath, bigIconPath } =
parse(parameterString).resourcesPath;
2023-11-27 16:52:38 -06:00
images[id] = {
2023-11-27 17:50:02 -06:00
small: `${TEMP}/assets/Data/${(smallIconPath as string)
2023-11-27 16:52:38 -06:00
.replace(/~res:\//, '')
.replace(/\..+/, '')}.packed.webp`,
2023-11-27 17:50:02 -06:00
big: `${TEMP}/assets/Data/${(bigIconPath as string)
2023-11-27 16:52:38 -06:00
.replace(/~res:\//, '')
.replace(/\..+/, '')}.packed.webp`,
};
2023-11-06 23:02:26 -06:00
},
),
);
}),
2023-11-27 16:52:38 -06:00
).then(async () => {
2023-12-05 17:36:18 -06:00
if (!publish) return;
2023-11-27 16:52:38 -06:00
2023-12-05 17:36:18 -06:00
const octokit = new Octokit({ auth: env.GH_TOKEN });
// const tankDefinitionsChange: FileChange = {
// content: JSON.stringify(tankDefinitions),
// path: 'definitions/tanks.json',
// encoding: 'utf-8',
// };
// const iconsChange = (
// await Promise.all(
// tankIds.map(async (id) => {
// try {
// const [contentSmall, contentBig] = await Promise.all([
// readFile(images[id].small, { encoding: 'base64' }),
// readFile(images[id].big, { encoding: 'base64' }),
// ]);
2023-12-04 10:13:14 -06:00
2023-12-05 17:36:18 -06:00
// return [
// {
// content: contentBig,
// path: `icons/big/${id}.webp`,
// encoding: 'base64',
// },
// {
// content: contentSmall,
// path: `icons/small/${id}.webp`,
// encoding: 'base64',
// },
// ];
// } catch (error) {
// console.log(error);
// return undefined;
// }
// }),
// )
// ).flat() as FileChange[];
const flags = await readdir(`${TEMP}/${directoriesOfInterest.flags}`);
// const scratchedFlags = await Promise.all(
// flags
// .filter((flag) => flag.startsWith('flag_tutor-tank_'))
// .map(async (flag) => {
// const content = await readFile(
// `${TEMP}/${directoriesOfInterest.flags}/${flag}`,
// { encoding: 'base64' },
// );
// const name = flag.match(/flag_tutor-tank_(.+)\.packed\.webp/)![1];
2023-12-04 19:10:26 -06:00
2023-12-05 17:36:18 -06:00
// return {
// content,
// encoding: 'base64',
// path: `flags/scratched/${name}.webp`,
// } satisfies FileChange;
// }),
// );
const circleFlags = await Promise.all(
flags
.filter((flag) => flag.startsWith('flag_profile-stat_'))
.map(async (flag) => {
const content = await readFile(
`${TEMP}/${directoriesOfInterest.flags}/${flag}`,
{ encoding: 'base64' },
);
const name = flag.match(/flag_profile-stat_(.+)\.packed\.webp/)![1];
2023-11-27 16:52:38 -06:00
2023-12-05 17:36:18 -06:00
return {
content,
encoding: 'base64',
path: `flags/circle/${name}.webp`,
} satisfies FileChange;
}),
);
console.log('Writing files...');
await commitMultipleFiles(
octokit,
'tresabhi',
'blitzkrieg-assets',
'main',
new Date().toString(),
circleFlags,
true,
);
2023-11-27 16:52:38 -06:00
});