Files
blitzkit/scripts/buildTest.ts
T

406 lines
9.6 KiB
TypeScript
Raw Normal View History

2023-12-07 22:13:54 -06:00
import { readFile } from 'fs/promises';
2023-12-07 22:41:49 -06:00
import { range } from 'lodash';
2023-12-07 22:13:54 -06:00
2023-12-07 22:41:49 -06:00
enum KAType {
2023-12-07 22:13:54 -06:00
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,
2023-12-08 18:14:33 -06:00
UNKNOWN1,
2023-12-07 22:13:54 -06:00
ARRAY,
}
2023-12-07 22:41:49 -06:00
enum VertexType {
VERTEX,
NORMAL,
COLOR,
TEXCOORD0,
TEXCOORD1,
TEXCOORD2,
TEXCOORD3,
TANGENT,
BINORMAL,
HARD_JOINTINDEX,
PIVOT4,
FLEXIBILITY,
ANGLE_SIN_COS,
JOINTINDEX,
JOINTWEIGHT,
CUBETEXCOORD0,
CUBETEXCOORD1,
CUBETEXCOORD2,
CUBETEXCOORD3,
}
2023-12-07 22:13:54 -06:00
2023-12-07 22:41:49 -06:00
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,
2023-12-07 23:36:46 -06:00
// VertexType.TANGENT, MOVED TO 4
2023-12-07 22:41:49 -06:00
VertexType.BINORMAL,
VertexType.CUBETEXCOORD0,
VertexType.CUBETEXCOORD1,
VertexType.CUBETEXCOORD2,
VertexType.CUBETEXCOORD3,
],
[
// vector 4; 4d vector or something more complex
2023-12-07 23:36:46 -06:00
VertexType.TANGENT, // UNCONFIRMED
2023-12-07 22:41:49 -06:00
VertexType.PIVOT4,
VertexType.JOINTINDEX,
VertexType.JOINTWEIGHT,
],
];
2023-12-07 22:13:54 -06:00
interface PolygonGroupRaw {
2023-12-08 18:45:57 -06:00
'##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;
2023-12-07 22:13:54 -06:00
}
interface PolygonGroup extends Omit<PolygonGroupRaw, 'indices'> {
indices: number[];
}
class SCPGStream {
public index = 0;
constructor(public buffer: Buffer) {}
skip(size: number) {
this.index += size;
}
2023-12-08 18:32:11 -06:00
read(size: number) {
return this.buffer.subarray(this.index, this.index + size);
}
2023-12-07 22:13:54 -06:00
consume(size: number) {
2023-12-08 18:32:11 -06:00
const subarray = this.read(size);
2023-12-07 22:13:54 -06:00
this.index += size;
return subarray;
}
2023-12-08 18:32:11 -06:00
readRemaining() {
return this.read(Number.POSITIVE_INFINITY);
}
2023-12-07 22:13:54 -06:00
consumeRemaining() {
return this.consume(Number.POSITIVE_INFINITY);
}
consumeByteArray(size: number) {
2023-12-08 18:45:57 -06:00
return this.consume(size);
2023-12-07 22:13:54 -06:00
}
2023-12-08 18:45:57 -06:00
2023-12-07 22:13:54 -06:00
consumeAscii(size: number) {
2023-12-08 18:45:57 -06:00
return this.consume(size).toString('ascii');
2023-12-07 22:13:54 -06:00
}
2023-12-08 18:14:33 -06:00
consumeUtf16(size: number) {
2023-12-08 18:45:57 -06:00
return this.consume(size).toString('utf16le');
2023-12-08 18:14:33 -06:00
}
2023-12-08 18:45:57 -06:00
2023-12-07 22:13:54 -06:00
consumeInt8() {
2023-12-08 18:45:57 -06:00
return this.consume(1).readInt8();
2023-12-07 22:13:54 -06:00
}
consumeInt16() {
2023-12-08 18:45:57 -06:00
return this.consume(2).readInt16LE();
2023-12-07 22:13:54 -06:00
}
consumeInt32() {
2023-12-08 18:45:57 -06:00
return this.consume(4).readInt32LE();
2023-12-07 22:13:54 -06:00
}
2023-12-08 18:14:33 -06:00
consumeInt64() {
2023-12-08 18:45:57 -06:00
return this.consume(8).readBigInt64LE();
2023-12-08 18:14:33 -06:00
}
2023-12-07 22:13:54 -06:00
consumeUInt8() {
2023-12-08 18:45:57 -06:00
return this.consume(1).readUInt8();
2023-12-07 22:13:54 -06:00
}
consumeUInt16() {
2023-12-08 18:45:57 -06:00
return this.consume(2).readUInt16LE();
2023-12-07 22:13:54 -06:00
}
consumeUInt32() {
2023-12-08 18:45:57 -06:00
return this.consume(4).readUInt32LE();
2023-12-07 22:13:54 -06:00
}
2023-12-08 18:14:33 -06:00
consumeUInt64() {
2023-12-08 18:45:57 -06:00
return this.consume(8).readBigUInt64LE();
2023-12-08 18:14:33 -06:00
}
2023-12-07 23:36:46 -06:00
consumeFloat() {
2023-12-08 18:45:57 -06:00
return this.consume(4).readFloatLE();
2023-12-07 23:36:46 -06:00
}
2023-12-07 22:13:54 -06:00
2023-12-08 18:32:11 -06:00
consumeBoolean() {
2023-12-08 18:45:57 -06:00
return this.consume(1).readUInt8() === 1;
2023-12-08 18:32:11 -06:00
}
2023-12-07 22:13:54 -06:00
consumeSCGHeader() {
return {
2023-12-08 18:45:57 -06:00
name: this.consumeAscii(4),
version: this.consumeUInt32(),
nodeCount: this.consumeUInt32(),
nodeCount2: this.consumeUInt32(),
2023-12-07 22:13:54 -06:00
};
}
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) => {
2023-12-08 18:45:57 -06:00
const indicesStream = new SCPGStream(polygonGroupRaw.indices);
2023-12-07 22:13:54 -06:00
const indices: number[] = [];
2023-12-08 18:45:57 -06:00
for (let index = 0; index < polygonGroupRaw.indexCount; index++) {
2023-12-07 22:13:54 -06:00
indices.push(
2023-12-08 18:45:57 -06:00
polygonGroupRaw.indexFormat
? indicesStream.consumeUInt32()
: indicesStream.consumeUInt16(),
2023-12-07 22:13:54 -06:00
);
}
2023-12-08 18:45:57 -06:00
const verticesStream = new SCPGStream(polygonGroupRaw.vertices);
const vertexFormat = polygonGroupRaw.vertexFormat
2023-12-07 22:13:54 -06:00
.toString(2)
.split('')
.map(parseInt)
2023-12-07 22:41:49 -06:00
.map((bit, index) => (bit ? index : null))
.filter((index) => index !== null) as VertexType[];
2023-12-07 22:13:54 -06:00
const vertices: { type: VertexType; value: number[] }[] = [];
2023-12-08 18:45:57 -06:00
for (let index = 0; index < polygonGroupRaw.vertexCount; index++) {
2023-12-07 22:13:54 -06:00
vertexFormat.forEach((type) => {
2023-12-07 22:41:49 -06:00
const resolved = VECTOR_SIZES.some((types, size) => {
if (types.includes(type)) {
vertices.push({
type,
2023-12-08 18:45:57 -06:00
value: range(size + 1).map(() => verticesStream.consumeFloat()),
2023-12-07 22:41:49 -06:00
});
return true;
}
});
if (!resolved) {
2023-12-07 22:13:54 -06:00
throw new TypeError(`Unknown vertex type: ${type}`);
}
});
}
return { vertices, indices };
});
2023-12-08 11:36:54 -06:00
return polygonGroups.map((group) => ({
vertices: group.vertices
.filter(({ type }) => type === VertexType.VERTEX)
.map(({ value }) => value),
indices: group.indices,
}));
}
2023-12-08 11:06:56 -06:00
2023-12-08 11:36:54 -06:00
consumeSC2Header() {
return {
2023-12-08 18:45:57 -06:00
name: this.consumeAscii(4),
version: this.consumeUInt32(),
nodeCount: this.consumeUInt32(),
2023-12-08 11:36:54 -06:00
};
}
consumeSC2Descriptor() {
2023-12-08 18:45:57 -06:00
const size = this.consumeUInt32();
const fileType = this.consumeUInt32();
2023-12-08 11:06:56 -06:00
2023-12-08 11:36:54 -06:00
this.skip(8); // other felids that we don't care about for some reason
2023-12-08 11:06:56 -06:00
2023-12-08 18:14:33 -06:00
return { size, fileType };
2023-12-08 11:36:54 -06:00
}
consumeSC2() {
const header = this.consumeSC2Header();
const versionTags = this.consumeKA();
const descriptor = this.consumeSC2Descriptor();
2023-12-08 18:14:33 -06:00
this.consumeKA();
2023-12-07 22:13:54 -06:00
}
consumeKAHeader() {
return {
2023-12-08 18:45:57 -06:00
name: this.consumeAscii(2),
version: this.consumeUInt16(),
count: this.consumeUInt32(),
2023-12-07 22:13:54 -06:00
};
}
2023-12-08 18:14:33 -06:00
consumeKAValue<Type>(
2023-12-08 18:45:57 -06:00
type: KAType = this.consumeUInt8(),
2023-12-08 18:14:33 -06:00
stringTable?: Record<number, string>,
2023-12-08 18:45:57 -06:00
): Type {
2023-12-07 22:13:54 -06:00
switch (type) {
2023-12-07 22:41:49 -06:00
case KAType.INT32:
2023-12-08 18:45:57 -06:00
return this.consumeInt32() as Type;
2023-12-07 22:13:54 -06:00
2023-12-07 22:41:49 -06:00
case KAType.STRING: {
2023-12-07 22:13:54 -06:00
const length = this.consumeUInt32();
2023-12-08 18:45:57 -06:00
return this.consumeAscii(length) as Type;
2023-12-07 22:13:54 -06:00
}
2023-12-07 22:41:49 -06:00
case KAType.BYTE_ARRAY: {
2023-12-07 22:13:54 -06:00
const length = this.consumeUInt32();
2023-12-08 18:45:57 -06:00
return this.consumeByteArray(length) as Type;
2023-12-07 22:13:54 -06:00
}
2023-12-08 18:14:33 -06:00
case KAType.FLOAT:
2023-12-08 18:45:57 -06:00
return this.consumeFloat() as Type;
2023-12-08 18:14:33 -06:00
case KAType.ARRAY: {
const length = this.consumeUInt32();
const value = [];
2023-12-08 18:45:57 -06:00
for (let index = 0; index < length; index++) {
2023-12-08 18:14:33 -06:00
value.push(this.consumeKAValue(undefined, stringTable));
}
2023-12-08 18:45:57 -06:00
return value as Type;
2023-12-08 18:14:33 -06:00
}
case KAType.KEYED_ARCHIVE: {
// the length
this.skip(4);
const value = this.consumeKA<Type>(stringTable);
2023-12-08 18:45:57 -06:00
return value;
2023-12-08 18:14:33 -06:00
}
case KAType.UINT64:
2023-12-08 18:45:57 -06:00
return this.consumeUInt64() as Type;
2023-12-08 18:14:33 -06:00
2023-12-08 18:32:11 -06:00
case KAType.UINT32:
2023-12-08 18:45:57 -06:00
return this.consumeUInt32() as Type;
2023-12-08 18:32:11 -06:00
case KAType.BOOLEAN:
2023-12-08 18:45:57 -06:00
return this.consumeBoolean() as Type;
2023-12-08 18:32:11 -06:00
2023-12-07 22:13:54 -06:00
default:
2023-12-08 18:14:33 -06:00
throw new TypeError(
`Unknown KA type: ${type} (${KAType[type] ?? 'unknown type'})`,
);
2023-12-07 22:13:54 -06:00
}
}
2023-12-08 18:14:33 -06:00
consumeKAV1Pair(stringTable?: Record<number, string>) {
2023-12-08 18:45:57 -06:00
const name = this.consumeKAValue<string>(undefined, stringTable);
const value = this.consumeKAValue(undefined, stringTable);
2023-12-07 22:13:54 -06:00
return { name, value };
}
2023-12-08 18:14:33 -06:00
consumeKA<Type>(stringTable?: Record<number, string>) {
2023-12-07 22:13:54 -06:00
const header = this.consumeKAHeader();
const pairs: Record<string, any> = {};
2023-12-08 18:14:33 -06:00
console.log(
`starting ka version ${header.version} with ${header.count} pairs`,
);
2023-12-08 11:36:54 -06:00
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) {
2023-12-08 18:14:33 -06:00
const strings: string[] = [];
const stringTable: Record<number, string> = {};
2023-12-08 11:36:54 -06:00
for (let index = 0; index < header.count; index++) {
2023-12-08 18:45:57 -06:00
const length = this.consumeUInt16();
const string = this.consumeAscii(length);
2023-12-08 11:36:54 -06:00
2023-12-08 18:14:33 -06:00
strings.push(string);
2023-12-08 11:36:54 -06:00
}
2023-12-08 18:14:33 -06:00
for (let index = 0; index < header.count; index++) {
2023-12-08 18:45:57 -06:00
const id = this.consumeUInt32();
2023-12-08 18:14:33 -06:00
stringTable[id] = strings[index];
console.log(` kav2 string: ${strings[index]}`, id);
}
console.log('\n NODE TIME BABY');
2023-12-08 18:45:57 -06:00
const nodeCount = this.consumeUInt32();
2023-12-08 18:14:33 -06:00
for (let index = 0; index < nodeCount; index++) {
2023-12-08 18:45:57 -06:00
const keyId = this.consumeUInt32();
2023-12-08 18:14:33 -06:00
const value = this.consumeKAValue(undefined, stringTable);
console.log(` ${keyId}`, value);
}
} else if (header.version === 258) {
if (!stringTable) throw new Error('No string table provided');
for (let index = 0; index < header.count; index++) {
2023-12-08 18:45:57 -06:00
const keyIndex = this.consumeUInt32();
2023-12-08 18:14:33 -06:00
const key = stringTable[keyIndex];
2023-12-08 18:45:57 -06:00
const valueType = this.consumeUInt8();
2023-12-08 18:14:33 -06:00
if (valueType === KAType.STRING) {
2023-12-08 18:45:57 -06:00
const valueIndex = this.consumeUInt32();
2023-12-08 18:14:33 -06:00
const value = stringTable[valueIndex];
pairs[key] = value;
} else {
2023-12-08 18:45:57 -06:00
const value = this.consumeKAValue(valueType, stringTable);
2023-12-08 18:14:33 -06:00
pairs[key] = value;
}
console.log(' ', keyIndex, key, pairs[key]);
}
} else throw new SyntaxError(`Unhandled KA version: ${header.version}`);
2023-12-07 22:13:54 -06:00
return pairs as Type;
}
}
2023-12-08 11:36:54 -06:00
const stream = new SCPGStream(await readFile('test.sc2'));
2023-12-07 22:13:54 -06:00
2023-12-08 11:36:54 -06:00
stream.consumeSC2();