xcom2charpool
Version:
Library for reading, manipulating, and managing XCOM 2 character pool binary files, supporting both browser and Node.js environments.
48 lines (47 loc) • 1.56 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.BaseCodec = void 0;
const CodecError_1 = require("./Errors/CodecError");
/**
* Base class for codecs providing path-aware helpers and error wrapping.
*/
class BaseCodec {
childContext(name, ctx) {
return Object.assign(Object.assign({}, ctx), { path: [...ctx.path, name] });
}
propName(ctx) {
return ctx.path.length > 0 ? ctx.path[ctx.path.length - 1] : '';
}
fullPath(ctx) {
if (!ctx.path.length)
return '<root>';
let out = ctx.path[0];
for (let i = 1; i < ctx.path.length; i++) {
const seg = ctx.path[i];
out += seg.startsWith('[') ? seg : `.${seg}`;
}
return out;
}
readSized(reader, length, ctx, fn) {
const payload = reader.subarray(length);
let val;
try {
val = fn(payload);
}
catch (e) {
if (e instanceof CodecError_1.CodecError)
throw e;
if (e instanceof Error) {
throw new CodecError_1.CodecError(e.message, this.fullPath(ctx), e);
}
else {
throw new CodecError_1.CodecError(`${e}`, this.fullPath(ctx));
}
}
if (payload.position !== payload.length) {
throw new CodecError_1.CodecError(`Payload size mismatch: ${payload.length - payload.position} bytes remaining`, this.fullPath(ctx));
}
return val;
}
}
exports.BaseCodec = BaseCodec;