feat: removed sub values

This commit is contained in:
TrèsAbhi committed 2023-12-08 18:45:57 -06:00
1 parent a0434b6ed2
commit f1855a569a
1 file changed
+72 -90
+72 -90
View File
@@ -89,22 +89,20 @@ const VECTOR_SIZES = [
], ],
]; ];
type KAResolvedValue<Type> = { buffer: Buffer; value: Type };
interface PolygonGroupRaw { interface PolygonGroupRaw {
'##name': KAResolvedValue<'PolygonGroup'>; '##name': 'PolygonGroup';
'#id': KAResolvedValue<Buffer>; '#id': Buffer;
cubeTextureCoordCount: KAResolvedValue<number>; cubeTextureCoordCount: number;
indexCount: KAResolvedValue<number>; indexCount: number;
indexFormat: KAResolvedValue<0 | 1>; indexFormat: 0 | 1;
indices: KAResolvedValue<Buffer>; indices: Buffer;
packing: KAResolvedValue<0 | 1>; packing: 0 | 1;
primitiveCount: KAResolvedValue<number>; primitiveCount: number;
rhi_primitiveType: KAResolvedValue<1 | 2 | 10>; rhi_primitiveType: 1 | 2 | 10;
textureCoordCount: KAResolvedValue<number>; textureCoordCount: number;
vertexCount: KAResolvedValue<number>; vertexCount: number;
vertexFormat: KAResolvedValue<number>; vertexFormat: number;
vertices: KAResolvedValue<Buffer>; vertices: Buffer;
} }
interface PolygonGroup extends Omit<PolygonGroupRaw, 'indices'> { interface PolygonGroup extends Omit<PolygonGroupRaw, 'indices'> {
@@ -137,65 +135,54 @@ class SCPGStream {
} }
consumeByteArray(size: number) { consumeByteArray(size: number) {
const buffer = this.consume(size); return this.consume(size);
return { buffer, value: buffer };
} }
consumeAscii(size: number) { consumeAscii(size: number) {
const buffer = this.consume(size); return this.consume(size).toString('ascii');
return { buffer, value: buffer.toString('ascii') };
} }
consumeUtf16(size: number) { consumeUtf16(size: number) {
const buffer = this.consume(size); return this.consume(size).toString('utf16le');
return { buffer, value: buffer.toString('utf16le') };
} }
consumeInt8() { consumeInt8() {
const buffer = this.consume(1); return this.consume(1).readInt8();
return { buffer, value: buffer.readInt8() };
} }
consumeInt16() { consumeInt16() {
const buffer = this.consume(2); return this.consume(2).readInt16LE();
return { buffer, value: buffer.readInt16LE() };
} }
consumeInt32() { consumeInt32() {
const buffer = this.consume(4); return this.consume(4).readInt32LE();
return { buffer, value: buffer.readInt32LE() };
} }
consumeInt64() { consumeInt64() {
const buffer = this.consume(8); return this.consume(8).readBigInt64LE();
return { buffer, value: buffer.readBigInt64LE() };
} }
consumeUInt8() { consumeUInt8() {
const buffer = this.consume(1); return this.consume(1).readUInt8();
return { buffer, value: buffer.readUInt8() };
} }
consumeUInt16() { consumeUInt16() {
const buffer = this.consume(2); return this.consume(2).readUInt16LE();
return { buffer, value: buffer.readUInt16LE() };
} }
consumeUInt32() { consumeUInt32() {
const buffer = this.consume(4); return this.consume(4).readUInt32LE();
return { buffer, value: buffer.readUInt32LE() };
} }
consumeUInt64() { consumeUInt64() {
const buffer = this.consume(8); return this.consume(8).readBigUInt64LE();
return { buffer, value: buffer.readBigUInt64LE() };
} }
consumeFloat() { consumeFloat() {
const buffer = this.consume(4); return this.consume(4).readFloatLE();
return { buffer, value: buffer.readFloatLE() };
} }
consumeBoolean() { consumeBoolean() {
const buffer = this.consume(1); return this.consume(1).readUInt8() === 1;
return { buffer, value: buffer.readUInt8() === 1 };
} }
consumeSCGHeader() { consumeSCGHeader() {
return { return {
name: this.consumeAscii(4).value, name: this.consumeAscii(4),
version: this.consumeUInt32().value, version: this.consumeUInt32(),
nodeCount: this.consumeUInt32().value, nodeCount: this.consumeUInt32(),
nodeCount2: this.consumeUInt32().value, nodeCount2: this.consumeUInt32(),
}; };
} }
consumeSCG() { consumeSCG() {
@@ -208,38 +195,33 @@ class SCPGStream {
} }
const polygonGroups = polygonGroupsRaw.map((polygonGroupRaw) => { const polygonGroups = polygonGroupsRaw.map((polygonGroupRaw) => {
const indicesStream = new SCPGStream(polygonGroupRaw.indices.value); const indicesStream = new SCPGStream(polygonGroupRaw.indices);
const indices: number[] = []; const indices: number[] = [];
for (let index = 0; index < polygonGroupRaw.indexCount.value; index++) { for (let index = 0; index < polygonGroupRaw.indexCount; index++) {
indices.push( indices.push(
polygonGroupRaw.indexFormat.value polygonGroupRaw.indexFormat
? indicesStream.consumeUInt32().value ? indicesStream.consumeUInt32()
: indicesStream.consumeUInt16().value, : indicesStream.consumeUInt16(),
); );
} }
const verticesStream = new SCPGStream(polygonGroupRaw.vertices.buffer); const verticesStream = new SCPGStream(polygonGroupRaw.vertices);
const vertexFormat = BigInt( const vertexFormat = polygonGroupRaw.vertexFormat
'0x' + polygonGroupRaw.vertexFormat.buffer.toString('hex'),
)
.toString(2) .toString(2)
.padStart(polygonGroupRaw.vertexFormat.buffer.length * 8, '0')
.split('') .split('')
.map(parseInt) .map(parseInt)
.map((bit, index) => (bit ? index : null)) .map((bit, index) => (bit ? index : null))
.filter((index) => index !== null) as VertexType[]; .filter((index) => index !== null) as VertexType[];
const vertices: { type: VertexType; value: number[] }[] = []; const vertices: { type: VertexType; value: number[] }[] = [];
for (let index = 0; index < polygonGroupRaw.vertexCount.value; index++) { for (let index = 0; index < polygonGroupRaw.vertexCount; index++) {
vertexFormat.forEach((type) => { vertexFormat.forEach((type) => {
const resolved = VECTOR_SIZES.some((types, size) => { const resolved = VECTOR_SIZES.some((types, size) => {
if (types.includes(type)) { if (types.includes(type)) {
vertices.push({ vertices.push({
type, type,
value: range(size + 1).map( value: range(size + 1).map(() => verticesStream.consumeFloat()),
() => verticesStream.consumeFloat().value,
),
}); });
return true; return true;
@@ -265,14 +247,14 @@ class SCPGStream {
consumeSC2Header() { consumeSC2Header() {
return { return {
name: this.consumeAscii(4).value, name: this.consumeAscii(4),
version: this.consumeUInt32().value, version: this.consumeUInt32(),
nodeCount: this.consumeUInt32().value, nodeCount: this.consumeUInt32(),
}; };
} }
consumeSC2Descriptor() { consumeSC2Descriptor() {
const size = this.consumeUInt32().value; const size = this.consumeUInt32();
const fileType = this.consumeUInt32().value; const fileType = this.consumeUInt32();
this.skip(8); // other felids that we don't care about for some reason this.skip(8); // other felids that we don't care about for some reason
@@ -288,41 +270,41 @@ class SCPGStream {
consumeKAHeader() { consumeKAHeader() {
return { return {
name: this.consumeAscii(2).value, name: this.consumeAscii(2),
version: this.consumeUInt16().value, version: this.consumeUInt16(),
count: this.consumeUInt32().value, count: this.consumeUInt32(),
}; };
} }
consumeKAValue<Type>( consumeKAValue<Type>(
type: KAType = this.consumeUInt8().value, type: KAType = this.consumeUInt8(),
stringTable?: Record<number, string>, stringTable?: Record<number, string>,
): KAResolvedValue<Type> { ): Type {
switch (type) { switch (type) {
case KAType.INT32: case KAType.INT32:
return this.consumeInt32() as KAResolvedValue<Type>; return this.consumeInt32() as Type;
case KAType.STRING: { case KAType.STRING: {
const length = this.consumeUInt32(); const length = this.consumeUInt32();
return this.consumeAscii(length.value) as KAResolvedValue<Type>; return this.consumeAscii(length) as Type;
} }
case KAType.BYTE_ARRAY: { case KAType.BYTE_ARRAY: {
const length = this.consumeUInt32(); const length = this.consumeUInt32();
return this.consumeByteArray(length.value) as KAResolvedValue<Type>; return this.consumeByteArray(length) as Type;
} }
case KAType.FLOAT: case KAType.FLOAT:
return this.consumeFloat() as KAResolvedValue<Type>; return this.consumeFloat() as Type;
case KAType.ARRAY: { case KAType.ARRAY: {
const length = this.consumeUInt32(); const length = this.consumeUInt32();
const value = []; const value = [];
for (let index = 0; index < length.value; index++) { for (let index = 0; index < length; index++) {
value.push(this.consumeKAValue(undefined, stringTable)); value.push(this.consumeKAValue(undefined, stringTable));
} }
return { value } as KAResolvedValue<Type>; return value as Type;
} }
case KAType.KEYED_ARCHIVE: { case KAType.KEYED_ARCHIVE: {
@@ -331,17 +313,17 @@ class SCPGStream {
const value = this.consumeKA<Type>(stringTable); const value = this.consumeKA<Type>(stringTable);
return { value } as KAResolvedValue<Type>; return value;
} }
case KAType.UINT64: case KAType.UINT64:
return this.consumeUInt64() as KAResolvedValue<Type>; return this.consumeUInt64() as Type;
case KAType.UINT32: case KAType.UINT32:
return this.consumeUInt32() as KAResolvedValue<Type>; return this.consumeUInt32() as Type;
case KAType.BOOLEAN: case KAType.BOOLEAN:
return this.consumeBoolean() as KAResolvedValue<Type>; return this.consumeBoolean() as Type;
default: default:
throw new TypeError( throw new TypeError(
@@ -350,8 +332,8 @@ class SCPGStream {
} }
} }
consumeKAV1Pair(stringTable?: Record<number, string>) { consumeKAV1Pair(stringTable?: Record<number, string>) {
const name = this.consumeKAValue(undefined, stringTable).value as string; const name = this.consumeKAValue<string>(undefined, stringTable);
const value = this.consumeKAValue(undefined, stringTable).value; const value = this.consumeKAValue(undefined, stringTable);
return { name, value }; return { name, value };
} }
@@ -372,14 +354,14 @@ class SCPGStream {
const strings: string[] = []; const strings: string[] = [];
const stringTable: Record<number, string> = {}; const stringTable: Record<number, string> = {};
for (let index = 0; index < header.count; index++) { for (let index = 0; index < header.count; index++) {
const length = this.consumeUInt16().value; const length = this.consumeUInt16();
const string = this.consumeAscii(length).value; const string = this.consumeAscii(length);
strings.push(string); strings.push(string);
} }
for (let index = 0; index < header.count; index++) { for (let index = 0; index < header.count; index++) {
const id = this.consumeUInt32().value; const id = this.consumeUInt32();
stringTable[id] = strings[index]; stringTable[id] = strings[index];
console.log(` kav2 string: ${strings[index]}`, id); console.log(` kav2 string: ${strings[index]}`, id);
@@ -387,9 +369,9 @@ class SCPGStream {
console.log('\n NODE TIME BABY'); console.log('\n NODE TIME BABY');
const nodeCount = this.consumeUInt32().value; const nodeCount = this.consumeUInt32();
for (let index = 0; index < nodeCount; index++) { for (let index = 0; index < nodeCount; index++) {
const keyId = this.consumeUInt32().value; const keyId = this.consumeUInt32();
const value = this.consumeKAValue(undefined, stringTable); const value = this.consumeKAValue(undefined, stringTable);
console.log(` ${keyId}`, value); console.log(` ${keyId}`, value);
@@ -398,16 +380,16 @@ class SCPGStream {
if (!stringTable) throw new Error('No string table provided'); if (!stringTable) throw new Error('No string table provided');
for (let index = 0; index < header.count; index++) { for (let index = 0; index < header.count; index++) {
const keyIndex = this.consumeUInt32().value; const keyIndex = this.consumeUInt32();
const key = stringTable[keyIndex]; const key = stringTable[keyIndex];
const valueType = this.consumeUInt8().value; const valueType = this.consumeUInt8();
if (valueType === KAType.STRING) { if (valueType === KAType.STRING) {
const valueIndex = this.consumeUInt32().value; const valueIndex = this.consumeUInt32();
const value = stringTable[valueIndex]; const value = stringTable[valueIndex];
pairs[key] = value; pairs[key] = value;
} else { } else {
const { value } = this.consumeKAValue(valueType, stringTable); const value = this.consumeKAValue(valueType, stringTable);
pairs[key] = value; pairs[key] = value;
} }