Files
blitzkit/scripts/buildTankopedia.ts
T

186 lines
5.9 KiB
TypeScript
Raw Normal View History

2023-11-27 16:52:38 -06:00
import { Octokit } from '@octokit/rest';
import decompress from 'decompress';
2023-11-16 12:57:46 -06:00
import { cp, mkdir, readFile, readdir, rm, writeFile } 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-27 12:26:51 -06:00
import { TreeTypeString } from '../src/components/Tanks';
2023-11-06 23:02:26 -06:00
import { NATION_IDS } from '../src/constants/nations';
2023-11-27 16:52:38 -06:00
import commitMultipleFiles, { FileChange } from './commitMultipleFiles';
2023-11-06 23:02:26 -06:00
2023-11-27 17:50:02 -06:00
const LANGUAGE = 'en';
const TEMP = 'temp/blitz';
2023-11-27 16:52:38 -06:00
const publish = argv.includes('--publish');
const apkFile = argv
2023-11-16 12:57:46 -06:00
.find((argument) => argument.startsWith('--file'))
?.split('=')[1];
2023-11-27 16:52:38 -06:00
const noRewrite = argv.includes('--no-rewrite');
if (!apkFile) throw new Error('No file specified');
2023-11-16 12:57:46 -06:00
2023-11-27 17:50:02 -06:00
const directoriesOfInterest = {
vehicleDefinitions: 'assets/Data/XML/item_defs/vehicles',
strings: 'assets/Data/Strings',
tankParameters: 'assets/Data/3d/Tanks/Parameters',
smallIcons: 'assets/Data/Gfx/UI/BattleScreenHUD/SmallTankIcons',
bigIcons: 'assets/Data/Gfx/UI/BigTankIcons',
};
const directoriesOfInterestArray = Object.values(directoriesOfInterest);
2023-11-27 16:52:38 -06:00
console.log('Removing old tankopedia files...');
2023-11-16 12:57:46 -06:00
await rm('dist/tankopedia', { recursive: true, force: true });
2023-11-27 16:52:38 -06:00
if (!noRewrite) {
console.log('Removing old temp files...');
2023-11-27 17:50:02 -06:00
await rm(TEMP, { recursive: true, force: true });
2023-11-16 12:57:46 -06:00
2023-11-27 16:52:38 -06:00
console.log('Extracting Blitz files...');
2023-11-27 17:50:02 -06:00
await decompress(apkFile, TEMP, {
2023-11-27 16:52:38 -06:00
filter: (file) =>
2023-11-27 17:50:02 -06:00
directoriesOfInterestArray.some((dir) => file.path.startsWith(dir)),
2023-11-27 16:52:38 -06:00
});
}
2023-11-16 12:57:46 -06:00
console.log('Creating new tankopedia folder');
await mkdir('dist/tankopedia');
2023-11-06 23:02:26 -06:00
interface BlitzkriegTankopedia {
id: number;
nation: string;
name: string;
name_short?: string;
2023-11-27 12:26:51 -06:00
tree_type: TreeTypeString;
2023-11-06 23:02:26 -06:00
tier: number;
type: string;
}
const strings = await readFile(
2023-11-27 17:50:02 -06:00
`${TEMP}/${directoriesOfInterest.strings}/${LANGUAGE}.yaml`,
2023-11-06 23:02:26 -06:00
'utf-8',
).then((data) => load(data) as Record<string, string>);
const tankopedia: Record<number, BlitzkriegTankopedia> = {};
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-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]) => {
2023-11-16 12:57:46 -06:00
// Tankopedia
2023-11-06 23:02:26 -06:00
const nationVehicleId = parseInt(list.root[vehicle].id._text);
const id = (nationVehicleId << 8) + (NATION_IDS[nation] << 4) + 1;
// TODO: use userString property instead
const name = strings[`#${nation}_vehicles:${vehicle}`];
const name_short = strings[`#${nation}_vehicles:${vehicle}_short`];
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;
const tier = parseInt(vehicleData.level._text);
const tags = vehicleData.tags._text.split(' ');
const type = tags[0];
2023-11-27 16:52:38 -06:00
tankIds.push(id);
2023-11-06 23:02:26 -06:00
tankopedia[id] = {
id,
name,
name_short,
nation,
2023-11-27 12:26:51 -06:00
tree_type: isCollector
? 'collector'
: isPremium
? 'premium'
: 'tech-tree',
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-16 12:57:46 -06:00
2023-11-27 16:52:38 -06:00
if (!publish) {
cp(images[id].small, `dist/tankopedia/icons/small/${id}.webp`);
cp(images[id].big, `dist/tankopedia/icons/big/${id}.webp`);
}
2023-11-06 23:02:26 -06:00
},
),
);
}),
2023-11-27 16:52:38 -06:00
).then(async () => {
if (publish) {
const octokit = new Octokit({ auth: env.GH_TOKEN });
2023-11-27 17:50:02 -06:00
const tankopediaChange: FileChange = {
content: JSON.stringify(tankopedia),
path: 'tankopedia.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-11-27 16:52:38 -06:00
2023-11-27 17:50:02 -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[];
2023-11-27 16:52:38 -06:00
2023-11-27 17:50:02 -06:00
console.log('Writing files...');
2023-11-27 16:52:38 -06:00
await commitMultipleFiles(
octokit,
'tresabhi',
'blitzkrieg-assets',
'main',
new Date().toString(),
2023-11-27 17:50:02 -06:00
iconsChange,
true,
2023-11-27 16:52:38 -06:00
);
} else {
writeFile(
'dist/tankopedia/tankopedia.json',
JSON.stringify(tankopedia, null, 2),
);
}
});