UNPKG

xcom2charpool

Version:

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

61 lines (60 loc) 2.5 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.ArrayProperty = void 0; const ArrayValue_1 = require("./ArrayValue"); const BaseCodec_1 = require("../../BaseCodec"); const CodecError_1 = require("../../Errors/CodecError"); /** * Codec for UE ArrayProperty values with element dispatch via the registry. */ class ArrayProperty extends BaseCodec_1.BaseCodec { constructor() { super(...arguments); this.type = 'ArrayProperty'; } read(reader, length, ctx) { if (length < 4) { throw new CodecError_1.CodecError(`ArrayProperty payload is too small (${length}). Expected at least 4 bytes for count.`, this.fullPath(ctx)); } const propName = this.propName(ctx); const element = ctx.registry.getArray(propName); return this.readSized(reader, length, ctx, (payload) => { const count = payload.uint32(); if (count > 0 && payload.position === payload.length) { return new ArrayValue_1.ArrayValue(count, new Uint8Array(0)); } // Unknown array: сохраняем сырой payload (после count) как bytes для round-trip. if (!element) { const rest = payload.bytes(payload.length - payload.position).slice(); return new ArrayValue_1.ArrayValue(count, rest); } const items = []; for (let i = 0; i < count; i++) { items.push(element.read(payload, this.childContext(`[${i}]`, ctx))); } return items; }); } write(writer, value, ctx) { const start = writer.position; if (value instanceof ArrayValue_1.ArrayValue) { writer.uint32(value.count); writer.bytes(value.payload); return writer.position - start; } const propName = this.propName(ctx); const element = ctx.registry.getArray(propName); if (!element) { throw new CodecError_1.CodecError(`No element codec for array ${propName}`, this.fullPath(ctx)); } writer.uint32(value.length); for (let i = 0; i < value.length; i++) { element.write(writer, value[i], this.childContext(`[${i}]`, ctx)); } return writer.position - start; } isSupported(value) { return Array.isArray(value) || value instanceof ArrayValue_1.ArrayValue; } } exports.ArrayProperty = ArrayProperty;