feat: uhh

This commit is contained in:
TrèsAbhi committed 2023-12-09 15:56:25 -06:00
1 parent fdf77a43cb
commit 4c2f6bea25
7 files changed
+136 -56

No files matched your search

+65 -1
View File
@@ -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<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;
// if (id !== 817) continue;
console.log(`Building model ${id} @ ${nation}/${tankIndex}`);
const parameters = await readYAMLDVPL<TankParameters>(
`${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,
// );
}
-428
View File
@@ -1,428 +0,0 @@
import { writeFileSync } from 'fs';
import { readFile } from 'fs/promises';
import { range } from 'lodash';
enum KAType {
NONE,
BOOLEAN,
INT32,
FLOAT,
STRING,
WIDE_STRING,
BYTE_ARRAY,
UINT32,
KEYED_ARCHIVE,
INT64,
UINT64,
VECTOR2,
VECTOR3,
VECTOR4,
MATRIX2,
MATRIX3,
MATRIX4,
COLOR,
FASTNAME,
AABBOX3,
FILEPATH,
FLOAT64,
INT8,
UINT8,
INT16,
UINT16,
UNKNOWN1,
ARRAY,
}
enum VertexType {
VERTEX,
NORMAL,
COLOR,
TEXCOORD0,
TEXCOORD1,
TEXCOORD2,
TEXCOORD3,
TANGENT,
BINORMAL,
HARD_JOINTINDEX,
PIVOT4,
FLEXIBILITY,
ANGLE_SIN_COS,
JOINTINDEX,
JOINTWEIGHT,
CUBETEXCOORD0,
CUBETEXCOORD1,
CUBETEXCOORD2,
CUBETEXCOORD3,
}
const VECTOR_SIZES = [
[
// vector 1; basically array of 1 number
VertexType.COLOR,
VertexType.FLEXIBILITY,
VertexType.HARD_JOINTINDEX,
],
[
// vector 2; 2d vector
VertexType.TEXCOORD0,
VertexType.TEXCOORD1,
VertexType.TEXCOORD2,
VertexType.TEXCOORD3,
VertexType.ANGLE_SIN_COS,
],
[
// vector 3; 3d vector
VertexType.VERTEX,
VertexType.NORMAL,
// VertexType.TANGENT, MOVED TO 4
VertexType.BINORMAL,
VertexType.CUBETEXCOORD0,
VertexType.CUBETEXCOORD1,
VertexType.CUBETEXCOORD2,
VertexType.CUBETEXCOORD3,
],
[
// vector 4; 4d vector or something more complex
VertexType.TANGENT, // UNCONFIRMED
VertexType.PIVOT4,
VertexType.JOINTINDEX,
VertexType.JOINTWEIGHT,
],
];
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<PolygonGroupRaw, 'indices'> {
indices: number[];
}
class SCPGStream {
public index = 0;
constructor(public buffer: Buffer) {}
skip(size: number) {
this.index += size;
}
read(size: number) {
return this.buffer.subarray(this.index, this.index + size);
}
consume(size: number) {
const subarray = this.read(size);
this.index += size;
return subarray;
}
readRemaining() {
return this.read(Number.POSITIVE_INFINITY);
}
consumeRemaining() {
return this.consume(Number.POSITIVE_INFINITY);
}
consumeByteArray(size: number) {
return this.consume(size);
}
consumeAscii(size: number) {
return this.consume(size).toString('ascii');
}
consumeUtf16(size: number) {
return this.consume(size).toString('utf16le');
}
consumeInt8() {
return this.consume(1).readInt8();
}
consumeInt16() {
return this.consume(2).readInt16LE();
}
consumeInt32() {
return this.consume(4).readInt32LE();
}
consumeInt64() {
return this.consume(8).readBigInt64LE();
}
consumeUInt8() {
return this.consume(1).readUInt8();
}
consumeUInt16() {
return this.consume(2).readUInt16LE();
}
consumeUInt32() {
return this.consume(4).readUInt32LE();
}
consumeUInt64() {
return this.consume(8).readBigUInt64LE();
}
consumeFloat() {
return this.consume(4).readFloatLE();
}
consumeBoolean() {
return this.consume(1).readUInt8() === 1;
}
consumeSCGHeader() {
return {
name: this.consumeAscii(4),
version: this.consumeUInt32(),
nodeCount: this.consumeUInt32(),
nodeCount2: this.consumeUInt32(),
};
}
consumeSCG() {
const header = this.consumeSCGHeader();
const polygonGroupsRaw: PolygonGroupRaw[] = [];
for (let index = 0; index < header.nodeCount; index++) {
const polygonGroupRaw = this.consumeKA<PolygonGroupRaw>();
polygonGroupsRaw.push(polygonGroupRaw);
}
const polygonGroups = polygonGroupsRaw.map((polygonGroupRaw) => {
const indicesStream = new SCPGStream(polygonGroupRaw.indices);
const indices: number[] = [];
for (let index = 0; index < polygonGroupRaw.indexCount; index++) {
indices.push(
polygonGroupRaw.indexFormat
? indicesStream.consumeUInt32()
: indicesStream.consumeUInt16(),
);
}
const verticesStream = new SCPGStream(polygonGroupRaw.vertices);
const vertexFormat = polygonGroupRaw.vertexFormat
.toString(2)
.split('')
.map(parseInt)
.map((bit, index) => (bit ? index : null))
.filter((index) => index !== null) as VertexType[];
const vertices: { type: VertexType; value: number[] }[] = [];
for (let index = 0; index < polygonGroupRaw.vertexCount; index++) {
vertexFormat.forEach((type) => {
const resolved = VECTOR_SIZES.some((types, size) => {
if (types.includes(type)) {
vertices.push({
type,
value: range(size + 1).map(() => verticesStream.consumeFloat()),
});
return true;
}
});
if (!resolved) {
throw new TypeError(`Unknown vertex type: ${type}`);
}
});
}
return { vertices, indices };
});
return polygonGroups.map((group) => ({
vertices: group.vertices
.filter(({ type }) => type === VertexType.VERTEX)
.map(({ value }) => value),
indices: group.indices,
}));
}
consumeSC2Header() {
return {
name: this.consumeAscii(4),
version: this.consumeUInt32(),
nodeCount: this.consumeUInt32(),
};
}
consumeSC2Descriptor() {
const size = this.consumeUInt32();
const fileType = this.consumeUInt32();
this.skip(8); // other felids that we don't care about for some reason
return { size, fileType };
}
consumeSC2() {
const header = this.consumeSC2Header();
const versionTags = this.consumeKA();
const descriptor = this.consumeSC2Descriptor();
const data = this.consumeKA();
writeFileSync(
'test.json',
JSON.stringify(
{ header, versionTags, descriptor, data },
(key, value) => (typeof value === 'bigint' ? value.toString() : value),
2,
),
);
// console.log(data);
}
consumeKAHeader() {
return {
name: this.consumeAscii(2),
version: this.consumeUInt16(),
count: this.consumeUInt32(),
};
}
consumeKAValue<Type>(
type: KAType = this.consumeUInt8(),
stringTable?: Record<number, string>,
): Type {
switch (type) {
case KAType.INT32:
return this.consumeInt32() as Type;
case KAType.STRING: {
const length = this.consumeUInt32();
return this.consumeAscii(length) as Type;
}
case KAType.BYTE_ARRAY: {
const length = this.consumeUInt32();
return this.consumeByteArray(length) as Type;
}
case KAType.FLOAT:
return this.consumeFloat() as Type;
case KAType.ARRAY: {
const length = this.consumeUInt32();
const value = [];
for (let index = 0; index < length; index++) {
value.push(this.consumeKAValue(undefined, stringTable));
}
return value as Type;
}
case KAType.KEYED_ARCHIVE: {
this.skip(4); // the length
const value = this.consumeKA<Type>(stringTable);
return value;
}
case KAType.UINT64:
return this.consumeUInt64() as Type;
case KAType.UINT32:
return this.consumeUInt32() as Type;
case KAType.BOOLEAN:
return this.consumeBoolean() as Type;
case KAType.AABBOX3: {
const minimum = range(3).map(() => this.consumeFloat());
const maximum = range(3).map(() => this.consumeFloat());
return { minimum, maximum } as Type;
}
case KAType.VECTOR4:
return range(4).map(() => this.consumeFloat()) as Type;
case KAType.VECTOR3:
return range(3).map(() => this.consumeFloat()) as Type;
case KAType.FASTNAME: {
if (!stringTable) throw new Error('No string table provided');
const index = this.consumeUInt32();
return stringTable[index] as Type;
}
default:
throw new TypeError(
`Unknown KA type: ${type} (${KAType[type] ?? 'unknown type'})`,
);
}
}
consumeKAV1Pair(stringTable?: Record<number, string>) {
const name = this.consumeKAValue<string>(undefined, stringTable);
const value = this.consumeKAValue(undefined, stringTable);
return { name, value };
}
consumeKA<Type>(stringTable?: Record<number, string>) {
const header = this.consumeKAHeader();
const pairs: Record<string, any> = {};
if (header.version === 1) {
for (let index = 0; index < header.count; index++) {
const { name, value } = this.consumeKAV1Pair();
pairs[name] = value;
}
} else if (header.version === 2) {
const strings: string[] = [];
const stringTable: Record<number, string> = {};
for (let index = 0; index < header.count; index++) {
const length = this.consumeUInt16();
const string = this.consumeAscii(length);
strings.push(string);
}
for (let index = 0; index < header.count; index++) {
const id = this.consumeUInt32();
stringTable[id] = strings[index];
}
const nodeCount = this.consumeUInt32();
for (let index = 0; index < nodeCount; index++) {
const keyId = this.consumeUInt32();
const value = this.consumeKAValue(undefined, stringTable);
const key = stringTable[keyId];
pairs[key] = value;
}
} else if (header.version === 258) {
if (!stringTable) throw new Error('No string table provided');
for (let index = 0; index < header.count; index++) {
const keyIndex = this.consumeUInt32();
const key = stringTable[keyIndex];
const valueType = this.consumeUInt8();
if (valueType === KAType.STRING) {
const valueIndex = this.consumeUInt32();
const value = stringTable[valueIndex];
pairs[key] = value;
} else {
const value = this.consumeKAValue(valueType, stringTable);
pairs[key] = value;
}
}
} else throw new SyntaxError(`Unhandled KA version: ${header.version}`);
return pairs as Type;
}
}
const stream = new SCPGStream(await readFile('test.sc2'));
stream.consumeSC2();