Skip to content

Commit

Permalink
Fix resources map off by 1 ✋🏽.
Browse files Browse the repository at this point in the history
  • Loading branch information
gmarty committed May 17, 2024
1 parent 21ae855 commit 02585e7
Show file tree
Hide file tree
Showing 10 changed files with 25 additions and 26 deletions.
22 changes: 11 additions & 11 deletions src/lib/parser/parseRooms.js
Original file line number Diff line number Diff line change
Expand Up @@ -48,14 +48,14 @@ const parseRooms = (arrayBuffer, i = 0, offset = 0, characters = {}) => {
map.push({
type: 'excdOffs',
from: excdOffs,
to: encdOffs - 1, // Hack
to: encdOffs, // Hack
});
}
if (encdOffs > 0) {
map.push({
type: 'encdOffs',
from: encdOffs,
to: chunkSize - 1, // Hack
to: chunkSize, // Hack
});
}

Expand All @@ -69,7 +69,7 @@ const parseRooms = (arrayBuffer, i = 0, offset = 0, characters = {}) => {
map.push(nametableMap);

assert(
nametableMap.to + 1 === attrOffs,
nametableMap.to === attrOffs,
'Gfx nametable overlaps on attributes table.',
);

Expand All @@ -83,7 +83,7 @@ const parseRooms = (arrayBuffer, i = 0, offset = 0, characters = {}) => {
map.push(attributesMap);

assert(
attributesMap.to + 1 === maskOffs,
attributesMap.to === maskOffs,
'Gfx attributes table overlaps on gfx mask table.',
);

Expand Down Expand Up @@ -124,7 +124,7 @@ const parseRooms = (arrayBuffer, i = 0, offset = 0, characters = {}) => {
map.push({
type: 'masktable',
from: maskOffs,
to: maskOffs + masktableParser.pointer - 1,
to: maskOffs + masktableParser.pointer,
});
} else {
map.push({
Expand All @@ -147,7 +147,7 @@ const parseRooms = (arrayBuffer, i = 0, offset = 0, characters = {}) => {
{
type: 'objectsOffs',
from: 0x1c + objectsNum * 2,
to: 0x1c + objectsNum * 4 - 1,
to: 0x1c + objectsNum * 4,
},
);
}
Expand Down Expand Up @@ -285,8 +285,8 @@ const parseRooms = (arrayBuffer, i = 0, offset = 0, characters = {}) => {
objectImages.push({ tiles });

// @fixme This does not account for object scripts.
if (objectCodeMap.to < objectOffs + objectImagesParser.pointer - 1) {
objectCodeMap.to = objectOffs + objectImagesParser.pointer - 1;
if (objectCodeMap.to < objectOffs + objectImagesParser.pointer) {
objectCodeMap.to = objectOffs + objectImagesParser.pointer;
}

// Calculate the number of object script given the value of object name offset.
Expand Down Expand Up @@ -341,10 +341,10 @@ const parseRooms = (arrayBuffer, i = 0, offset = 0, characters = {}) => {
// Parse matrix.
const { matrixUnks, matrix, matrixMap } = parseRoomMatrix(
arrayBuffer.slice(
boxesMap.to + 1,
boxesMap.to + 1 + boxes.length * (boxes.length + 1),
boxesMap.to,
boxesMap.to + boxes.length * (boxes.length + 1),
),
boxesMap.to + 1,
boxesMap.to,
boxes.length,
);

Expand Down
1 change: 0 additions & 1 deletion src/lib/parser/parser.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,6 @@ class Parser {
}

// Return the position of the next byte to read.
// @todo Return the last read character's instead.
get pointer() {
return this.#ptr;
}
Expand Down
2 changes: 1 addition & 1 deletion src/lib/parser/room/parseRoomAttributes.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ const parseRoomAttributes = (arrayBuffer, offset = 0, width = 0) => {
const attributesMap = {
type: 'attributes',
from: offset,
to: offset + parser.pointer - 1,
to: offset + parser.pointer,
};

return {
Expand Down
2 changes: 1 addition & 1 deletion src/lib/parser/room/parseRoomBoxes.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ const parseRoomBoxes = (arrayBuffer, offset = 0) => {
const boxesMap = {
type: 'boxes',
from: offset,
to: offset + parser.pointer - 1,
to: offset + parser.pointer,
};

return { boxes, boxesMap };
Expand Down
2 changes: 1 addition & 1 deletion src/lib/parser/room/parseRoomMatrix.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ const parseRoomMatrix = (arrayBuffer, offset = 0, boxNum = 1) => {
const matrixMap = {
type: 'matrix',
from: offset,
to: offset + parser.pointer - 1,
to: offset + parser.pointer,
};

return { matrixUnks, matrix, matrixMap };
Expand Down
2 changes: 1 addition & 1 deletion src/lib/parser/room/parseRoomNametable.js
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ const parseRoomNametable = (arrayBuffer, offset = 0, width = 0) => {
const nametableMap = {
type: 'nametable',
from: offset,
to: offset + parser.pointer - 1,
to: offset + parser.pointer,
};

return {
Expand Down
4 changes: 2 additions & 2 deletions test/parseRoomAttributes.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ describe('parseRoomAttributes', () => {

assert.equal(typeof attributesMap, 'object');
assert.equal(attributesMap.from, 0);
assert.equal(attributesMap.to, 1);
assert.equal(attributesMap.to, 2);
});

it('should return a map object with a start offset.', () => {
Expand All @@ -58,7 +58,7 @@ describe('parseRoomAttributes', () => {
);

assert.equal(attributesMap.from, 0xabc);
assert.equal(attributesMap.to, 0xabd);
assert.equal(attributesMap.to, 0xabe);
});

it('should be the inverse of serialiseRoomAttributes.', () => {
Expand Down
6 changes: 3 additions & 3 deletions test/parseRoomBoxes.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,21 +41,21 @@ describe('parseRoomBoxes', () => {

assert.equal(typeof boxesMap, 'object');
assert.equal(boxesMap.from, 0);
assert.equal(boxesMap.to, 8);
assert.equal(boxesMap.to, 9);
});

it('should return a map object with a start offset.', () => {
const { boxesMap } = parseRoomBoxes(roomBoxesEmptyBuffer(1), 0xabc);

assert.equal(boxesMap.from, 0xabc);
assert.equal(boxesMap.to, 0xac4);
assert.equal(boxesMap.to, 0xac5);
});

it('should return a map object with an end offset.', () => {
const { boxesMap } = parseRoomBoxes(roomBoxesEmptyBuffer(5), 0xabc);

assert.equal(boxesMap.from, 0xabc);
assert.equal(boxesMap.to, 0xae4);
assert.equal(boxesMap.to, 0xae5);
});

it('should be the inverse of serialiseRoomBoxes.', () => {
Expand Down
6 changes: 3 additions & 3 deletions test/parseRoomMatrix.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -54,21 +54,21 @@ describe('parseRoomMatrix', () => {

assert.equal(typeof matrixMap, 'object');
assert.equal(matrixMap.from, 0);
assert.equal(matrixMap.to, 1);
assert.equal(matrixMap.to, 2);
});

it('should return a map object with a start offset.', () => {
const { matrixMap } = parseRoomMatrix(roomMatrixEmptyBuffer(1), 0xabc, 1);

assert.equal(matrixMap.from, 0xabc);
assert.equal(matrixMap.to, 0xabd);
assert.equal(matrixMap.to, 0xabe);
});

it('should return a map object with an end offset.', () => {
const { matrixMap } = parseRoomMatrix(roomMatrixEmptyBuffer(5), 0xabc, 5);

assert.equal(matrixMap.from, 0xabc);
assert.equal(matrixMap.to, 0xad9);
assert.equal(matrixMap.to, 0xada);
});

it('should be the inverse of serialiseRoomMatrix.', () => {
Expand Down
4 changes: 2 additions & 2 deletions test/parseRoomNametable.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ describe('parseRoomNametable', () => {

assert.equal(typeof nametableMap, 'object');
assert.equal(nametableMap.from, 0);
assert.equal(nametableMap.to, 16);
assert.equal(nametableMap.to, 17);
});

it('should return a map object with a start offset.', () => {
Expand All @@ -117,7 +117,7 @@ describe('parseRoomNametable', () => {
);

assert.equal(nametableMap.from, 0xabc);
assert.equal(nametableMap.to, 0xacc);
assert.equal(nametableMap.to, 0xacd);
});

it('should be the inverse of serialiseRoomNametable.', () => {
Expand Down

0 comments on commit 02585e7

Please sign in to comment.