scpg.ts•5.57 kB
import { ReadStream } from './buffer.js';
enum KAType {
NONE = 0,
BOOLEAN = 1,
INT32 = 2,
FLOAT = 3,
STRING = 4,
WIDE_STRING = 5,
BYTE_ARRAY = 6,
UINT32 = 7,
KEYED_ARCHIVE = 8,
INT64 = 9,
UINT64 = 10,
VECTOR2 = 11,
VECTOR3 = 12,
VECTOR4 = 13,
MATRIX2 = 14,
MATRIX3 = 15,
MATRIX4 = 16,
COLOR = 17,
FASTNAME = 18,
AABBOX3 = 19,
FILEPATH = 20,
FLOAT64 = 21,
INT8 = 22,
UINT8 = 23,
INT16 = 24,
UINT16 = 25,
ARRAY = 27,
TRANSFORM = 29,
}
type StringTable = Record<number, string>;
export class ScpgReadStream extends ReadStream {
vectorN(size: number): number[] {
const result: number[] = [];
for (let i = 0; i < size; i++) {
result.push(this.float32());
}
return result;
}
vector2() {
return this.vectorN(2);
}
vector3() {
return this.vectorN(3);
}
vector4() {
return this.vectorN(4);
}
matrixN(size: number): number[][] {
const result: number[][] = [];
for (let i = 0; i < size; i++) {
result.push(this.vectorN(size));
}
return result;
}
matrix2() {
return this.matrixN(2);
}
matrix3() {
return this.matrixN(3);
}
matrix4() {
return this.matrixN(4);
}
kaValue(type: KAType | undefined = undefined, stringTable?: StringTable): unknown {
if (type === undefined) {
type = this.uint8();
}
switch (type) {
case KAType.NONE:
return undefined;
case KAType.FILEPATH:
case KAType.STRING: {
if (stringTable) {
const id = this.uint32();
return stringTable[id];
}
const length = this.uint32();
return this.ascii(length);
}
case KAType.BYTE_ARRAY: {
const length = this.uint32();
return this.byte(length);
}
case KAType.FLOAT:
return this.float32();
case KAType.ARRAY: {
const length = this.uint32();
const value: unknown[] = [];
for (let i = 0; i < length; i++) {
value.push(this.kaValue(undefined, stringTable));
}
return value;
}
case KAType.KEYED_ARCHIVE: {
this.seek(4);
return this.ka(stringTable);
}
case KAType.INT8:
return this.int8();
case KAType.INT16:
return this.int16();
case KAType.INT32:
return this.int32();
case KAType.INT64:
return this.int64();
case KAType.UINT8:
return this.uint8();
case KAType.UINT16:
return this.uint16();
case KAType.UINT32:
return this.uint32();
case KAType.UINT64:
return this.uint64();
case KAType.BOOLEAN:
return this.boolean();
case KAType.VECTOR2:
return this.vector2();
case KAType.VECTOR3:
return this.vector3();
case KAType.VECTOR4:
return this.vector4();
case KAType.COLOR:
return this.vector4();
case KAType.MATRIX2:
return this.matrix2();
case KAType.MATRIX3:
return this.matrix3();
case KAType.MATRIX4:
return this.matrix4();
case KAType.AABBOX3: {
const minimum = this.vector3();
const maximum = this.vector3();
return { minimum, maximum };
}
case KAType.FASTNAME: {
if (!stringTable) throw new Error('No string table provided');
const index = this.uint32();
return stringTable[index];
}
case KAType.TRANSFORM: {
const position = this.vector3();
const scale = this.vector3();
const rotation = this.vector4();
return { position, scale, rotation };
}
default:
throw new TypeError(`Unhandled KA type: ${type}`);
}
}
ka(stringTable?: StringTable): Record<string, unknown> {
this.seek(2); // "KA"
const version = this.uint16();
const pairs: Record<string, unknown> = {};
if (version === 0x0001) {
const count = this.uint32();
for (let i = 0; i < count; i++) {
const name = this.kaValue(undefined, stringTable) as string;
const value = this.kaValue(undefined, stringTable);
pairs[name] = value;
}
} else if (version === 0x0002) {
const count = this.uint32();
const strings: string[] = [];
const localStringTable: StringTable = {};
for (let i = 0; i < count; i++) {
const length = this.uint16();
const str = this.ascii(length);
strings.push(str);
}
for (let i = 0; i < count; i++) {
const id = this.uint32();
localStringTable[id] = strings[i];
}
const nodeCount = this.uint32();
for (let i = 0; i < nodeCount; i++) {
const keyId = this.uint32();
const value = this.kaValue(undefined, localStringTable);
const key = localStringTable[keyId];
pairs[key] = value;
}
} else if (version === 0x0102) {
if (!stringTable) throw new Error('No string table provided');
const count = this.uint32();
for (let i = 0; i < count; i++) {
const keyIndex = this.uint32();
const key = stringTable[keyIndex];
const valueType = this.uint8();
if (valueType === KAType.STRING) {
const valueIndex = this.uint32();
pairs[key] = stringTable[valueIndex];
} else {
pairs[key] = this.kaValue(valueType, stringTable);
}
}
} else if (version === 0xff02) {
return {};
} else {
throw new RangeError(`Unhandled KA version: ${version}`);
}
return pairs;
}
}