UNPKG

xcom2charpool

Version:

Library for reading, manipulating, and managing XCOM 2 character pool binary files, supporting both browser and Node.js environments.

54 lines (53 loc) 2.33 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.CharacterPoolFile = void 0; const ObjectProperty_1 = require("../Properties/ObjectProperty"); const ArrayProperty_1 = require("../Properties/Array/ArrayProperty"); const CodecError_1 = require("../Errors/CodecError"); const Registry_1 = require("../Registry"); const StructArrayElement_1 = require("../Properties/Struct/StructArrayElement"); const BaseCodec_1 = require("../BaseCodec"); const CharacterPoolSchema_1 = require("../Schema/CharacterPoolSchema"); /** * Core file codec for vanilla CharacterPool binaries with schema validation and registry setup. */ class CharacterPoolFile extends BaseCodec_1.BaseCodec { get registry() { return this.ctx.registry; } constructor() { super(); this.obj = new ObjectProperty_1.ObjectProperty(); this.arr = new ArrayProperty_1.ArrayProperty(); this.magic = 0xffffffff; this.ctx = { registry: new Registry_1.CodecRegistry(), path: [], }; this.ctx.registry.registerArray('CharacterPool', new StructArrayElement_1.StructArrayElement('CharacterPoolDataElement')); } read(reader) { const magic = reader.uint32(); if (magic !== this.magic) { throw new CodecError_1.CodecError(`Bad magic: ${magic.toString(16).padStart(8, '0')}`, this.fullPath(this.ctx)); } const Props = this.obj.read(reader, 0, this.childContext('Props', this.ctx)); const remaining = reader.length - reader.position; const CharacterPool = this.arr.read(reader, remaining, this.childContext('CharacterPool', this.ctx)); if (reader.position !== reader.length) throw new CodecError_1.CodecError('Trailing bytes', '<root>'); const data = { Props, CharacterPool }; this.schema().parse(data); return data; } write(writer, file) { const data = this.schema().parse(file); writer.uint32(this.magic); this.obj.write(writer, data.Props, this.childContext('Props', this.ctx)); this.arr.write(writer, data.CharacterPool, this.childContext('CharacterPool', this.ctx)); } schema() { return CharacterPoolSchema_1.CharacterPoolSchema; } } exports.CharacterPoolFile = CharacterPoolFile;