From e23f898c5f65e32e3160df289bc9059e88a95e30 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tr=C3=A8sAbhi?= <43380238+tresabhi@users.noreply.github.com> Date: Mon, 27 Nov 2023 16:52:38 -0600 Subject: [PATCH] feat: assets writing --- scripts/buildRatingsLeaderboard.ts | 4 +- scripts/buildTankopedia.ts | 117 ++++++++++++++++++++++------- scripts/commitMultipleFiles.ts | 10 ++- 3 files changed, 98 insertions(+), 33 deletions(-) diff --git a/scripts/buildRatingsLeaderboard.ts b/scripts/buildRatingsLeaderboard.ts index cedf7e465..b69df4cc1 100644 --- a/scripts/buildRatingsLeaderboard.ts +++ b/scripts/buildRatingsLeaderboard.ts @@ -132,8 +132,8 @@ if (publish) { 'main', new Date().toString(), [ - { path: infoPath, content: infoJSON }, - { path: leaderboardPath, content: leaderboardJSON }, + { path: infoPath, content: infoJSON, encoding: 'utf-8' }, + { path: leaderboardPath, content: leaderboardJSON, encoding: 'utf-8' }, ], ); } else { diff --git a/scripts/buildTankopedia.ts b/scripts/buildTankopedia.ts index daa7d3cec..28cbd490e 100644 --- a/scripts/buildTankopedia.ts +++ b/scripts/buildTankopedia.ts @@ -1,15 +1,21 @@ +import { Octokit } from '@octokit/rest'; +import decompress from 'decompress'; import { cp, mkdir, readFile, readdir, rm, writeFile } from 'fs/promises'; import { load } from 'js-yaml'; -import { argv } from 'process'; +import { argv, env } from 'process'; import { ElementCompact, xml2js } from 'xml-js'; import { parse } from 'yaml'; import { TreeTypeString } from '../src/components/Tanks'; import { NATION_IDS } from '../src/constants/nations'; +import commitMultipleFiles, { FileChange } from './commitMultipleFiles'; -const VEHICLE_DEFS = 'assets/Data/XML/item_defs/vehicles'; -const APK_FILE = argv +const publish = argv.includes('--publish'); +const apkFile = argv .find((argument) => argument.startsWith('--file')) ?.split('=')[1]; +const noRewrite = argv.includes('--no-rewrite'); + +const VEHICLE_DEFS = 'assets/Data/XML/item_defs/vehicles'; const STRINGS = 'assets/Data/Strings'; const LANGUAGE = 'en'; const TANK_PARAMETERS = 'assets/Data/3d/Tanks/Parameters'; @@ -23,17 +29,20 @@ const DIRECTORIES_OF_INTEREST = [ BIG_ICONS, ]; -if (!APK_FILE) throw new Error('No file specified'); +if (!apkFile) throw new Error('No file specified'); -// console.log('Removing old Blitz files...'); -// await rm('temp/blitz', { recursive: true, force: true }); +console.log('Removing old tankopedia files...'); await rm('dist/tankopedia', { recursive: true, force: true }); +if (!noRewrite) { + console.log('Removing old temp files...'); + await rm('temp/blitz', { recursive: true, force: true }); -// console.log('Extracting Blitz files...'); -// await decompress(APK_FILE, 'temp/blitz', { -// filter: (file) => -// DIRECTORIES_OF_INTEREST.some((dir) => file.path.startsWith(dir)), -// }); + console.log('Extracting Blitz files...'); + await decompress(apkFile, 'temp/blitz', { + filter: (file) => + DIRECTORIES_OF_INTEREST.some((dir) => file.path.startsWith(dir)), + }); +} console.log('Creating new tankopedia folder'); await mkdir('dist/tankopedia'); @@ -53,6 +62,8 @@ const strings = await readFile( 'utf-8', ).then((data) => load(data) as Record); const tankopedia: Record = {}; +const tankIds: number[] = []; +const images: Record = {}; const nations = await readdir(`temp/blitz/${VEHICLE_DEFS}`); Promise.all( @@ -82,6 +93,7 @@ Promise.all( const tags = vehicleData.tags._text.split(' '); const type = tags[0]; + tankIds.push(id); tankopedia[id] = { id, name, @@ -101,26 +113,73 @@ Promise.all( const parameterString = await readFile(parameterPath, 'utf-8'); const { smallIconPath, bigIconPath } = parse(parameterString).resourcesPath; + images[id] = { + small: `temp/blitz/assets/Data/${(smallIconPath as string) + .replace(/~res:\//, '') + .replace(/\..+/, '')}.packed.webp`, + big: `temp/blitz/assets/Data/${(bigIconPath as string) + .replace(/~res:\//, '') + .replace(/\..+/, '')}.packed.webp`, + }; - cp( - `temp/blitz/assets/Data/${(smallIconPath as string) - .replace(/~res:\//, '') - .replace(/\..+/, '')}.packed.webp`, - `dist/tankopedia/icons/small/${id}.webp`, - ); - cp( - `temp/blitz/assets/Data/${(bigIconPath as string) - .replace(/~res:\//, '') - .replace(/\..+/, '')}.packed.webp`, - `dist/tankopedia/icons/big/${id}.webp`, - ); + if (!publish) { + cp(images[id].small, `dist/tankopedia/icons/small/${id}.webp`); + cp(images[id].big, `dist/tankopedia/icons/big/${id}.webp`); + } }, ), ); }), -).then(() => - writeFile( - 'dist/tankopedia/tankopedia.json', - JSON.stringify(tankopedia, null, 2), - ), -); +).then(async () => { + if (publish) { + const octokit = new Octokit({ auth: env.GH_TOKEN }); + + // console.log('Writing tankopedia.json'); + // commitMultipleFiles( + // octokit, + // 'tresabhi', + // 'blitzkrieg-assets', + // 'main', + // new Date().toString(), + // [ + // { + // content: JSON.stringify(tankopedia), + // path: 'tankopedia.json', + // encoding: 'utf-8', + // }, + // ], + // ); + + console.log('Writing big icons...'); + await commitMultipleFiles( + octokit, + 'tresabhi', + 'blitzkrieg-assets', + 'main', + new Date().toString(), + (await Promise.all( + tankIds.map(async (id) => { + try { + const content = await readFile(images[id].big, { + encoding: 'base64', + }); + + return { + content, + path: `icons/big/${id}.webp`, + encoding: 'base64', + }; + } catch (error) { + console.log(error); + return undefined; + } + }), + )) as FileChange[], + ); + } else { + writeFile( + 'dist/tankopedia/tankopedia.json', + JSON.stringify(tankopedia, null, 2), + ); + } +}); diff --git a/scripts/commitMultipleFiles.ts b/scripts/commitMultipleFiles.ts index f00083554..fe1b73a25 100644 --- a/scripts/commitMultipleFiles.ts +++ b/scripts/commitMultipleFiles.ts @@ -1,12 +1,18 @@ import { Octokit } from '@octokit/rest'; +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: { path: string; content: string }[], + changes: FileChange[], ) { // Get the latest commit SHA on the specified branch const { data: refData } = await octokit.git.getRef({ @@ -31,7 +37,7 @@ export default async function commitMultipleFiles( owner, repo, content: change.content, - encoding: 'utf-8', + encoding: change.encoding, }); return {