feat: build assets

This commit is contained in:
TrèsAbhi committed 2023-12-05 19:14:53 -06:00
1 parent aa78bc4045
commit 9df47777d0
12 files changed
+328 -214

No files matched your search

+269 -172
View File
@@ -1,33 +1,50 @@
import { Octokit } from '@octokit/rest';
import { config } from 'dotenv';
import { writeFileSync } from 'fs';
import { readFile, readdir } from 'fs/promises';
import { load } from 'js-yaml';
import { readdir } from 'fs/promises';
import { argv, env } from 'process';
import { ElementCompact, xml2js } from 'xml-js';
import { parse } from 'yaml';
import { TankType } from '../src/components/Tanks';
import { NATION_IDS } from '../src/constants/nations';
import { readDVPL } from '../src/core/blitz/readDVPL';
import { readXMLDVPL } from '../src/core/blitz/readXMLDVPL';
import { readYAMLDVPL } from '../src/core/blitz/readYAMLDVPL';
import commitMultipleFiles from '../src/core/blitzkrieg/commitMultipleFiles';
import {
TankDefinitions,
Tier,
} from '../src/core/blitzkrieg/definitions/tanks';
import commitMultipleFiles, { FileChange } from './commitMultipleFiles';
config();
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;
};
}
interface Strings {
[key: string]: string;
}
type BlitzTankType = 'AT-SPG' | 'lightTank' | 'mediumTank' | 'heavyTank';
const blitzTankTypeToBlitzkrieg: Record<BlitzTankType, TankType> = {
'AT-SPG': 'tank_destroyer',
lightTank: 'light',
mediumTank: 'medium',
heavyTank: 'heavy',
};
const DATA =
'C:/Program Files (x86)/Steam/steamapps/common/World of Tanks Blitz/Data';
const LANGUAGE = 'en';
const targets = argv
.find((argument) => argument.startsWith('--target'))
?.split('=')[1]
.split(',');
if (!targets) throw new Error('No target(s) specified');
const directoriesOfInterest = {
const DOI = {
vehicleDefinitions: 'XML/item_defs/vehicles',
strings: 'Strings',
tankParameters: '3d/Tanks/Parameters',
@@ -36,171 +53,251 @@ const directoriesOfInterest = {
flags: 'Gfx/Lobby/flags',
};
console.log(
(
await readDVPL(
`${DATA}/${directoriesOfInterest.strings}/${LANGUAGE}.yaml.dvpl`,
)
).toString(),
);
throw new Error('stop');
const octokit = new Octokit({ auth: env.GH_TOKEN });
const targets = argv
.find((argument) => argument.startsWith('--target'))
?.split('=')[1]
.split(',');
const strings = await readFile(
`${DATA}/${directoriesOfInterest.strings}/${LANGUAGE}.yaml.dvpl`,
'utf-8',
).then((data) => load(data) as Record<string, string>);
if (!targets) throw new Error('No target(s) specified');
const tankDefinitions: TankDefinitions = {};
const tankIds: number[] = [];
const images: Record<number, { big: string; small: string }> = {};
const nations = await readdir(
`${TEMP}/${directoriesOfInterest.vehicleDefinitions}`,
);
if (targets.includes('tankDefinitions')) {
console.log('Building tank definitions...');
writeFileSync('test.json', JSON.stringify(strings, null, 2));
Promise.all(
nations
.filter((nation) => nation !== 'common')
.map(async (nation) => {
const listXML = await readFile(
`${TEMP}/${directoriesOfInterest.vehicleDefinitions}/${nation}/list.xml`,
'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;
const name = strings[vehicleData.userString._text];
const name_short = strings[vehicleData.shortUserString?._text];
const isPremium = 'gold' in vehicleData.price;
const isCollector = vehicleData.sellPrice
? 'gold' in vehicleData.sellPrice
: false;
const tier = parseInt(vehicleData.level._text) as Tier;
const tags = vehicleData.tags._text.split(' ');
const type = tags[0];
if ((name ?? name_short) === undefined) {
console.warn(`Missing name for ${vehicle}`);
}
tankIds.push(id);
tankDefinitions[id] = {
id,
name,
name_short,
nation,
tree_type: isCollector
? 'collector'
: isPremium
? 'premium'
: 'researchable',
tier,
type,
};
// Tank images
const parameterPath = `${TEMP}/${directoriesOfInterest.tankParameters}/${nation}/${vehicle}.yaml`;
const parameterString = await readFile(parameterPath, 'utf-8');
const { smallIconPath, bigIconPath } =
parse(parameterString).resourcesPath;
images[id] = {
small: `${TEMP}/assets/Data/${(smallIconPath as string)
.replace(/~res:\//, '')
.replace(/\..+/, '')}.packed.webp`,
big: `${TEMP}/assets/Data/${(bigIconPath as string)
.replace(/~res:\//, '')
.replace(/\..+/, '')}.packed.webp`,
};
},
),
);
}),
).then(async () => {
if (!publish) return;
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' }),
// ]);
// 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];
// 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];
return {
content,
encoding: 'base64',
path: `flags/circle/${name}.webp`,
} satisfies FileChange;
}),
const tankDefinitions: TankDefinitions = {};
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`,
);
console.log('Writing files...');
await Promise.all(
nations.map(async (nation) => {
const tanks = await readXMLDVPL<VehicleDefinitionList>(
`${DATA}/${DOI.vehicleDefinitions}/${nation}/list.xml.dvpl`,
);
for (const tankIndex in tanks) {
const tank = tanks[tankIndex];
const nationVehicleId = tank.id;
const id = (nationVehicleId << 8) + (NATION_IDS[nation] << 4) + 1;
const name = strings[tank.userString];
const name_short = tank.shortUserString
? strings[tank.shortUserString] === name
? undefined
: strings[tank.shortUserString]
: undefined;
const isPremium =
typeof tank.price === 'number' ? false : 'gold' in tank.price;
const isCollector = tank.sellPrice ? 'gold' in tank.sellPrice : false;
const tier = tank.level as Tier;
const tags = tank.tags.split(' ');
const type = blitzTankTypeToBlitzkrieg[tags[0] as BlitzTankType];
if ((name ?? name_short) === undefined) {
console.warn(`Missing name for ${tankIndex}`);
}
tankDefinitions[id] = {
id,
name,
name_short,
nation,
tree_type: isCollector
? 'collector'
: isPremium
? 'premium'
: 'researchable',
tier,
type,
};
}
}),
);
console.log('Committing tank definitions...');
await commitMultipleFiles(
octokit,
'tresabhi',
'blitzkrieg-assets',
'main',
new Date().toString(),
circleFlags,
'tank definitions',
[
{
content: JSON.stringify(tankDefinitions),
encoding: 'utf-8',
path: 'definitions/tanks.json',
},
],
true,
);
});
}
// console.log(
// (
// await readDVPL(
// `${DATA}/${DOI.strings}/${LANGUAGE}.yaml.dvpl`,
// )
// ).toString(),
// );
// const strings = await readFile(
// `${DATA}/${DOI.strings}/${LANGUAGE}.yaml.dvpl`,
// 'utf-8',
// ).then((data) => load(data) as Record<string, string>);
// const tankDefinitions: TankDefinitions = {};
// const tankIds: number[] = [];
// const images: Record<number, { big: string; small: string }> = {};
// const nations = await readdir(
// `${TEMP}/${DOI.vehicleDefinitions}`,
// );
// writeFileSync('test.json', JSON.stringify(strings, null, 2));
// Promise.all(
// nations
// .filter((nation) => nation !== 'common')
// .map(async (nation) => {
// const listXML = await readFile(
// `${TEMP}/${DOI.vehicleDefinitions}/${nation}/list.xml`,
// '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;
// const name = strings[vehicleData.userString._text];
// const name_short = strings[vehicleData.shortUserString?._text];
// const isPremium = 'gold' in vehicleData.price;
// const isCollector = vehicleData.sellPrice
// ? 'gold' in vehicleData.sellPrice
// : false;
// const tier = parseInt(vehicleData.level._text) as Tier;
// const tags = vehicleData.tags._text.split(' ');
// const type = tags[0];
// if ((name ?? name_short) === undefined) {
// console.warn(`Missing name for ${vehicle}`);
// }
// tankIds.push(id);
// tankDefinitions[id] = {
// id,
// name,
// name_short,
// nation,
// tree_type: isCollector
// ? 'collector'
// : isPremium
// ? 'premium'
// : 'researchable',
// tier,
// type,
// };
// // Tank images
// const parameterPath = `${TEMP}/${DOI.tankParameters}/${nation}/${vehicle}.yaml`;
// const parameterString = await readFile(parameterPath, 'utf-8');
// const { smallIconPath, bigIconPath } =
// parse(parameterString).resourcesPath;
// images[id] = {
// small: `${TEMP}/assets/Data/${(smallIconPath as string)
// .replace(/~res:\//, '')
// .replace(/\..+/, '')}.packed.webp`,
// big: `${TEMP}/assets/Data/${(bigIconPath as string)
// .replace(/~res:\//, '')
// .replace(/\..+/, '')}.packed.webp`,
// };
// },
// ),
// );
// }),
// ).then(async () => {
// if (!publish) return;
// 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' }),
// // ]);
// // 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}/${DOI.flags}`);
// // const scratchedFlags = await Promise.all(
// // flags
// // .filter((flag) => flag.startsWith('flag_tutor-tank_'))
// // .map(async (flag) => {
// // const content = await readFile(
// // `${TEMP}/${DOI.flags}/${flag}`,
// // { encoding: 'base64' },
// // );
// // const name = flag.match(/flag_tutor-tank_(.+)\.packed\.webp/)![1];
// // 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}/${DOI.flags}/${flag}`,
// { encoding: 'base64' },
// );
// const name = flag.match(/flag_profile-stat_(.+)\.packed\.webp/)![1];
// 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,
// );
// });
+1 -1
View File
@@ -6,8 +6,8 @@ import {
RatingsNeighbors,
RatingsPlayer,
} from '../src/commands/ratings';
import commitMultipleFiles from '../src/core/blitzkrieg/commitMultipleFiles.js';
import { patientFetchJSON } from '../src/core/blitzkrieg/patientFetchJSON';
import commitMultipleFiles from './commitMultipleFiles.js';
/*
* Central North American Time (UTC-5): use 0 5 * * *.
-108
View File
@@ -1,108 +0,0 @@
import { Octokit } from '@octokit/rest';
const TIME_PER_BLOB = 2 ** 4 * 1000;
export interface FileChange {
path: string;
content: string;
encoding: 'utf-8' | 'base64';
}
export default async function commitMultipleFiles(
octokit: Octokit,
owner: string,
repo: string,
branch: string,
message: string,
changes: FileChange[],
logBlobCreation = false,
) {
// Get the latest commit SHA on the specified branch
const latestCommitSha = (
await octokit.git.getRef({
owner,
repo,
ref: `heads/${branch}`,
})
).data.object.sha;
// Get the tree SHA for the latest commit
const treeSha = (
await octokit.git.getCommit({
owner,
repo,
commit_sha: latestCommitSha,
})
).data.tree.sha;
const blobs: {
sha: string;
path: string;
mode: '100644';
type: 'blob';
}[] = [];
for (const changeIndexString in changes) {
const changeIndex = parseInt(changeIndexString);
const change = changes[changeIndex];
let done = false;
while (!done) {
try {
const { sha } = (
await octokit.git.createBlob({
owner,
repo,
content: change.content,
encoding: change.encoding,
})
).data;
blobs.push({ sha, path: change.path, mode: '100644', type: 'blob' });
done = true;
} catch (error) {
console.warn(
`Failed blob ${changeIndex + 1} / ${
changes.length
}; retrying in ${TIME_PER_BLOB}ms...`,
);
await new Promise((resolve) => setTimeout(resolve, TIME_PER_BLOB));
}
}
if (logBlobCreation) {
console.log(
`Created blob ${changeIndex + 1} / ${changes.length} (${(
(100 * (changeIndex + 1)) /
changes.length
).toFixed(2)}%)`,
);
}
}
// Create a new tree with the new blobs
const { data: treeData } = await octokit.git.createTree({
owner,
repo,
base_tree: treeSha,
tree: blobs,
});
// Create a new commit with the new tree
const { data: newCommitData } = await octokit.git.createCommit({
owner,
repo,
message,
tree: treeData.sha,
parents: [latestCommitSha],
});
// Update the branch reference to point to the new commit
await octokit.git.updateRef({
owner,
repo,
ref: `heads/${branch}`,
sha: newCommitData.sha,
});
}