homm3-parsers
Version:
Collection of binary parsers able to parse various file formats related to Heroes of Might and Magic III
47 lines • 4.44 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
const binary_markup_1 = require("binary-markup");
const rgb = binary_markup_1.bytes(3);
const filePointer = (context) => {
const index = context.get('index');
const offsets = context.get('offsets');
context.set('offset', offsets[index]);
return offsets[index];
};
const linePointer = (context) => {
const index = context.get('index');
const offsets = context.get('lineOffsets');
const offset = context.get('offset');
return offset + 32 + offsets[index];
};
const repeatCondition = (context, _el, _index, list) => list.reduce((acc, el) => acc + el.content.length, 0) <
context.get('width');
const codeFragment = binary_markup_1.struct(binary_markup_1.byte `code`, binary_markup_1.byte `length`, binary_markup_1.branch(binary_markup_1.ctx `code`, {
0xff: binary_markup_1.bytes((context) => context.get('length') + 1),
}, binary_markup_1.computed((context) => new Array(context.get('length') + 1).fill(context.get('code')))) `content`);
const segmentFragment = binary_markup_1.struct(binary_markup_1.byte `segment`, binary_markup_1.computed((context) => context.get('segment') >> 5) `code`, binary_markup_1.computed((context) => (context.get('segment') & 0x1f) + 1) `length`, binary_markup_1.branch(binary_markup_1.ctx `code`, {
7: binary_markup_1.bytes(binary_markup_1.ctx `length`),
}, binary_markup_1.computed((context) => new Array(context.get('length')).fill(context.get('code')))) `content`);
const lineReducer = (acc, list) => acc.concat(
//
list.reduce((a, l) => a.concat(l.content), []));
const file = binary_markup_1.struct(binary_markup_1.uint32 `size`, binary_markup_1.uint32 `encoding`, binary_markup_1.uint32 `fullWidth`, binary_markup_1.uint32 `fullHeight`, binary_markup_1.uint32 `width`, binary_markup_1.uint32 `height`, binary_markup_1.int32 `left`, binary_markup_1.int32 `top`, binary_markup_1.branch(binary_markup_1.ctx `encoding`, {
0: binary_markup_1.bytes((context) => context.get('width') * context.get('height')),
1: binary_markup_1.struct(binary_markup_1.array(binary_markup_1.uint32, binary_markup_1.ctx `height`) `lineOffsets`, binary_markup_1.array(
//
binary_markup_1.pointer(linePointer, binary_markup_1.repeatWhile(codeFragment, repeatCondition)), binary_markup_1.ctx `height`) `lines`),
2: binary_markup_1.struct(binary_markup_1.array(binary_markup_1.uint16, binary_markup_1.ctx `height`) `lineOffsets`, binary_markup_1.skip(2), binary_markup_1.array(binary_markup_1.pointer(linePointer, binary_markup_1.repeatWhile(segmentFragment, repeatCondition)), binary_markup_1.ctx `height`) `lines`),
3: binary_markup_1.struct(binary_markup_1.array(binary_markup_1.array(binary_markup_1.uint16, (context) => Math.ceil(context.get('width') / 32)), binary_markup_1.ctx `height`) `lineOffsets`, binary_markup_1.array(binary_markup_1.array(binary_markup_1.repeatWhile(segmentFragment, repeatCondition), (context) => Math.ceil(context.get('width') / 32)) `line`, binary_markup_1.ctx `height`) `lines`),
}) `content`, binary_markup_1.branch(binary_markup_1.ctx `encoding`, {
0: binary_markup_1.computed(binary_markup_1.ctx `content`),
1: binary_markup_1.computed((context) => context.get('content.lines').reduce(lineReducer, [])),
2: binary_markup_1.computed((context) => context.get('content.lines').reduce(lineReducer, [])),
3: binary_markup_1.computed((context) => context.get('content.lines').reduce(
//
(acc, line) => acc.concat(
//
line.reduce(lineReducer, [])), [])),
}) `pixels`);
const block = binary_markup_1.struct(binary_markup_1.uint32 `blockId`, binary_markup_1.uint32 `entriesCount`, binary_markup_1.uint32 `unknown`, binary_markup_1.uint32 `unknown`, binary_markup_1.array(binary_markup_1.string(13), binary_markup_1.ctx `entriesCount`) `filenames`, binary_markup_1.array(binary_markup_1.uint32, binary_markup_1.ctx `entriesCount`) `offsets`, binary_markup_1.array(binary_markup_1.pointer(filePointer, file), binary_markup_1.ctx `entriesCount`) `files`);
exports.defFile = binary_markup_1.struct(binary_markup_1.endian(binary_markup_1.Endian.LE), binary_markup_1.uint32 `type`, binary_markup_1.uint32 `width`, binary_markup_1.uint32 `height`, binary_markup_1.uint32 `blockCount`, binary_markup_1.array(rgb, 256) `palette`, binary_markup_1.array(block, binary_markup_1.ctx `blockCount`) `blocks`);
//# sourceMappingURL=def.bml.js.map