xcom2charpool
Version:
Library for reading, manipulating, and managing XCOM 2 character pool binary files, supporting both browser and Node.js environments.
67 lines (66 loc) • 2.53 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.ObjectProperty = void 0;
const BaseCodec_1 = require("../BaseCodec");
const CodecError_1 = require("../Errors/CodecError");
/**
* Codec for UE object property maps, reading/writing named property lists.
*/
class ObjectProperty extends BaseCodec_1.BaseCodec {
read(reader, length, ctx) {
const result = {};
let terminated = false;
while (reader.position < reader.length) {
const prop = this.readProperty(reader, ctx);
if (!prop) {
terminated = true;
break;
}
result[prop[0]] = prop[1];
}
if (!terminated) {
throw new CodecError_1.CodecError(`ObjectProperty payload is malformed: Missing terminator None`, this.fullPath(ctx));
}
return result;
}
write(writer, value, ctx) {
for (const [name, v] of Object.entries(value)) {
this.writeProperty(writer, name, v, this.childContext(name, ctx));
}
writer.string('None').padding();
}
readProperty(reader, ctx) {
const name = reader.string();
reader.padding();
if (name === 'None')
return;
const type = reader.string();
reader.padding();
const length = reader.uint32();
reader.padding();
const codec = ctx.registry.get(type, name);
const propertyCtx = this.childContext(name, ctx);
if (!codec) {
throw new CodecError_1.CodecError(`Cannot find codec ${type}`, this.fullPath(propertyCtx));
}
return [name, codec.read(reader, length, propertyCtx)];
}
writeProperty(writer, name, value, ctx) {
var _a, _b;
const codec = ctx.registry.resolveByValue(value);
if (!codec) {
const kind = value === null
? 'null'
: Array.isArray(value)
? 'array'
: typeof value === 'object'
? ((_b = (_a = value.constructor) === null || _a === void 0 ? void 0 : _a.name) !== null && _b !== void 0 ? _b : 'object')
: typeof value;
throw new CodecError_1.CodecError(`Cannot find codec for value ${kind}`, this.fullPath(ctx));
}
writer.string(name).padding();
writer.string(codec.type).padding();
writer.withLength(() => codec.write(writer, value, ctx));
}
}
exports.ObjectProperty = ObjectProperty;