From 4c2f6bea25c9fa8489e8899694eb87fdf890c951 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tr=C3=A8sAbhi?= <43380238+tresabhi@users.noreply.github.com> Date: Sat, 9 Dec 2023 15:56:25 -0600 Subject: [PATCH] feat: uhh --- .vscode/settings.json | 4 + scripts/buildAssets.ts | 66 ++++++++++- .../core/blitz/SCPGStream.ts | 104 ++++++++++-------- src/core/blitz/readBase64DVPL.ts | 4 +- src/core/blitz/readDVPL.ts | 4 +- src/core/blitz/readDVPLFile.ts | 6 + src/core/blitz/readStringDVPL.ts | 4 +- 7 files changed, 136 insertions(+), 56 deletions(-) rename scripts/buildTest.ts => src/core/blitz/SCPGStream.ts (90%) create mode 100644 src/core/blitz/readDVPLFile.ts diff --git a/.vscode/settings.json b/.vscode/settings.json index 0dbe16fe9..aa8b0487b 100644 --- a/.vscode/settings.json +++ b/.vscode/settings.json @@ -3,6 +3,7 @@ "AABBOX", "Abhi", "accountinfo", + "alphatest", "bepaint", "BINORMAL", "blitzstars", @@ -12,6 +13,7 @@ "CUBETEXCOORD", "customperiod", "deburr", + "decalmask", "diacriticless", "dvpl", "DVPLCRC", @@ -28,6 +30,7 @@ "Leaderboard", "leaderboards", "MURICA", + "normalmap", "Oopsy", "outdir", "outfile", @@ -45,6 +48,7 @@ "searchtanks", "SKLLD", "Smri", + "Specularity", "startcase", "statsfull", "steamapps", diff --git a/scripts/buildAssets.ts b/scripts/buildAssets.ts index 0d960f3f8..3f895b53a 100644 --- a/scripts/buildAssets.ts +++ b/scripts/buildAssets.ts @@ -1,8 +1,10 @@ import { config } from 'dotenv'; -import { readdir } from 'fs/promises'; +import { existsSync } from 'fs'; +import { mkdir, readdir, rm } from 'fs/promises'; import { argv } from 'process'; import { TankType } from '../src/components/Tanks'; import { NATION_IDS } from '../src/constants/nations'; +import { SCPGStream } from '../src/core/blitz/SCPGStream'; import { readBase64DVPL } from '../src/core/blitz/readBase64DVPL'; import { readXMLDVPL } from '../src/core/blitz/readXMLDVPL'; import { readYAMLDVPL } from '../src/core/blitz/readYAMLDVPL'; @@ -43,6 +45,7 @@ interface TankParameters { resourcesPath: { smallIconPath: string; bigIconPath: string; + blitzModelPath: string; }; } @@ -62,6 +65,7 @@ const DOI = { smallIcons: 'Gfx/UI/BattleScreenHUD/SmallTankIcons', bigIcons: 'Gfx/UI/BigTankIcons', flags: 'Gfx/Lobby/flags', + '3d': '3d', }; const allTargets = argv.includes('--all-targets'); @@ -269,3 +273,63 @@ if (allTargets || targets?.includes('circleFlags')) { true, ); } + +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( + [nations[0]].map(async (nation) => { + const tanks = await readXMLDVPL( + `${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; + + // if (id !== 817) continue; + console.log(`Building model ${id} @ ${nation}/${tankIndex}`); + + const parameters = await readYAMLDVPL( + `${DATA}/${DOI.tankParameters}/${nation}/${tankIndex}.yaml.dvpl`, + ); + const sc2Path = parameters.resourcesPath.blitzModelPath; + // const scgPath = parameters.resourcesPath.blitzModelPath.replace( + // /\.sc2$/, + // '.scg', + // ); + const sc2Stream = await SCPGStream.fromDVPLFile( + `${DATA}/${DOI['3d']}/${sc2Path}.dvpl`, + ); + // const scgStream = await SCPGStream.fromDVPLFile( + // `${DATA}/${DOI['3d']}/${scgPath}.dvpl`, + // ); + const sc2Data = sc2Stream.consumeSC2(); + // const scgData = scgStream.consumeSCG(); + + // writeFile(`dist/assets/models/${id}.json`, JSON.stringify(scgData)); + } + }), + ); + + // console.log('Committing tank icons...'); + // await commitMultipleFiles( + // 'tresabhi', + // 'blitzkrieg-assets', + // 'main', + // 'tank icons', + // changes, + // true, + // ); +} diff --git a/scripts/buildTest.ts b/src/core/blitz/SCPGStream.ts similarity index 90% rename from scripts/buildTest.ts rename to src/core/blitz/SCPGStream.ts index ddaf255f0..7b7fccb26 100644 --- a/scripts/buildTest.ts +++ b/src/core/blitz/SCPGStream.ts @@ -1,6 +1,6 @@ -import { writeFileSync } from 'fs'; -import { readFile } from 'fs/promises'; import { range } from 'lodash'; +import { readDVPL } from './readDVPL'; +import { readDVPLFile } from './readDVPLFile'; enum KAType { NONE, @@ -55,6 +55,31 @@ enum VertexType { CUBETEXCOORD3, } +interface SCPGBuffer { + type: 'Buffer'; + data: number[]; +} + +interface PolygonGroupRaw { + '##name': 'PolygonGroup'; + '#id': Buffer; + cubeTextureCoordCount: number; + indexCount: number; + indexFormat: 0 | 1; + indices: Buffer; + packing: 0 | 1; + primitiveCount: number; + rhi_primitiveType: 1 | 2 | 10; + textureCoordCount: number; + vertexCount: number; + vertexFormat: number; + vertices: Buffer; +} + +interface PolygonGroup extends Omit { + indices: number[]; +} + const VECTOR_SIZES = [ [ // vector 1; basically array of 1 number @@ -90,27 +115,7 @@ const VECTOR_SIZES = [ ], ]; -interface PolygonGroupRaw { - '##name': 'PolygonGroup'; - '#id': Buffer; - cubeTextureCoordCount: number; - indexCount: number; - indexFormat: 0 | 1; - indices: Buffer; - packing: 0 | 1; - primitiveCount: number; - rhi_primitiveType: 1 | 2 | 10; - textureCoordCount: number; - vertexCount: number; - vertexFormat: number; - vertices: Buffer; -} - -interface PolygonGroup extends Omit { - indices: number[]; -} - -class SCPGStream { +export class SCPGStream { public index = 0; constructor(public buffer: Buffer) {} @@ -165,7 +170,10 @@ class SCPGStream { return this.consume(2).readUInt16LE(); } consumeUInt32() { - return this.consume(4).readUInt32LE(); + const buffer = this.consume(4); + const value = buffer.readUInt32LE(); + + return value; } consumeUInt64() { return this.consume(8).readBigUInt64LE(); @@ -210,6 +218,7 @@ class SCPGStream { const verticesStream = new SCPGStream(polygonGroupRaw.vertices); const vertexFormat = polygonGroupRaw.vertexFormat .toString(2) + .padStart(8 * 8, '0') .split('') .map(parseInt) .map((bit, index) => (bit ? index : null)) @@ -247,36 +256,31 @@ class SCPGStream { } consumeSC2Header() { - return { + const header = { name: this.consumeAscii(4), version: this.consumeUInt32(), nodeCount: this.consumeUInt32(), }; + const version = this.consumeKA(); + const descriptor = this.consumeSC2Descriptor(header.version); + + return { header, version, descriptor }; } - consumeSC2Descriptor() { + consumeSC2Descriptor(version: number) { const size = this.consumeUInt32(); const fileType = this.consumeUInt32(); - this.skip(8); // other felids that we don't care about for some reason + // other felids that we don't care about for some reason lol + this.skip(size - 4); return { size, fileType }; } consumeSC2() { - const header = this.consumeSC2Header(); - const versionTags = this.consumeKA(); - const descriptor = this.consumeSC2Descriptor(); - const data = this.consumeKA(); + console.log(this.consumeSC2Header()); - writeFileSync( - 'test.json', - JSON.stringify( - { header, versionTags, descriptor, data }, - (key, value) => (typeof value === 'bigint' ? value.toString() : value), - 2, - ), - ); + console.log(this.read(32).toString('ascii')); - // console.log(data); + return this.consumeKA(); } consumeKAHeader() { @@ -319,11 +323,9 @@ class SCPGStream { } case KAType.KEYED_ARCHIVE: { - this.skip(4); // the length + this.skip(4); // UInt32 length of the nested archive in bytes - const value = this.consumeKA(stringTable); - - return value; + return this.consumeKA(stringTable); } case KAType.UINT64: @@ -372,6 +374,8 @@ class SCPGStream { const header = this.consumeKAHeader(); const pairs: Record = {}; + console.log(header); + if (header.version === 1) { for (let index = 0; index < header.count; index++) { const { name, value } = this.consumeKAV1Pair(); @@ -421,8 +425,12 @@ class SCPGStream { return pairs as Type; } + + static fromDVPL(buffer: Buffer) { + return new SCPGStream(readDVPL(buffer)); + } + + static async fromDVPLFile(file: string) { + return new SCPGStream(await readDVPLFile(file)); + } } - -const stream = new SCPGStream(await readFile('test.sc2')); - -stream.consumeSC2(); diff --git a/src/core/blitz/readBase64DVPL.ts b/src/core/blitz/readBase64DVPL.ts index 7615eb869..4e50b9042 100644 --- a/src/core/blitz/readBase64DVPL.ts +++ b/src/core/blitz/readBase64DVPL.ts @@ -1,6 +1,6 @@ -import { readDVPL } from './readDVPL'; +import { readDVPLFile } from './readDVPLFile'; export async function readBase64DVPL(file: string) { - const buffer = await readDVPL(file); + const buffer = await readDVPLFile(file); return buffer.toString('base64'); } diff --git a/src/core/blitz/readDVPL.ts b/src/core/blitz/readDVPL.ts index fff0c81d0..f27473acf 100644 --- a/src/core/blitz/readDVPL.ts +++ b/src/core/blitz/readDVPL.ts @@ -1,5 +1,4 @@ import { crc32 } from 'crc'; -import { readFile } from 'fs/promises'; import { decodeBlock } from 'lz4'; /** @@ -7,8 +6,7 @@ import { decodeBlock } from 'lz4'; * * https://github.com/Maddoxkkm/dvpl_converter/ */ -export async function readDVPL(file: string) { - const buffer = await readFile(file); +export function readDVPL(buffer: Buffer) { const footerBuffer = buffer.subarray(buffer.length - 20, buffer.length); if ( diff --git a/src/core/blitz/readDVPLFile.ts b/src/core/blitz/readDVPLFile.ts new file mode 100644 index 000000000..ef83c77b2 --- /dev/null +++ b/src/core/blitz/readDVPLFile.ts @@ -0,0 +1,6 @@ +import { readFile } from 'fs/promises'; +import { readDVPL } from './readDVPL'; + +export async function readDVPLFile(file: string) { + return readDVPL(await readFile(file)); +} diff --git a/src/core/blitz/readStringDVPL.ts b/src/core/blitz/readStringDVPL.ts index d55639d44..46c799be9 100644 --- a/src/core/blitz/readStringDVPL.ts +++ b/src/core/blitz/readStringDVPL.ts @@ -1,6 +1,6 @@ -import { readDVPL } from './readDVPL'; +import { readDVPLFile } from './readDVPLFile'; export async function readStringDVPL(file: string) { - const buffer = await readDVPL(file); + const buffer = await readDVPLFile(file); return buffer.toString(); }