Skip to content

Commit

Permalink
Add a simple serialiser 🏗️.
Browse files Browse the repository at this point in the history
  • Loading branch information
gmarty committed Apr 27, 2024
1 parent 072f3af commit 2b95210
Show file tree
Hide file tree
Showing 12 changed files with 99 additions and 9 deletions.
2 changes: 1 addition & 1 deletion index.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import {
isJapaneseVersion,
getResFromCrc32,
} from './src/lib/getResFromCrc32.js';
import parseRom from './src/lib/parseRom.js';
import parseRom from './src/lib/parser/parseRom.js';
import { stringify } from './src/lib/cliUtils.js';

const options = {
Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
},
"scripts": {
"test": "node --test",
"coverage": "node --experimental-test-coverage --test",
"dev": "parcel",
"start": "parcel",
"build": "parcel build --public-url /scumm-nes"
Expand Down
2 changes: 1 addition & 1 deletion src/App.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ const App = () => {
const navigate = useNavigate();

const onFile = async (rom, res) => {
const { default: parseRom } = await import('./lib/parseRom');
const { default: parseRom } = await import('./lib/parser/parseRom');
setRom(rom);
setRes(res);
setResources(parseRom(rom, res));
Expand Down
File renamed without changes.
4 changes: 2 additions & 2 deletions src/lib/parseRom.js → src/lib/parser/parseRom.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import parseRooms from '../lib/parseRooms.js';
import parseRoomGfx from '../lib/parseRoomGfx.js';
import parseRooms from './parseRooms.js';
import parseRoomGfx from './parseRoomGfx.js';
import parsePreps from './parsePreps.js';

const parseRom = (arrayBuffer, res) => {
Expand Down
File renamed without changes.
File renamed without changes.
6 changes: 3 additions & 3 deletions src/lib/parser.js → src/lib/parser/parser.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ class Parser {
#ptr = 0;
#characters = {};

constructor(arrayBuffer, characters = {}) {
this.#view = new DataView(arrayBuffer);
constructor(buffer, characters = {}) {
this.#view = new DataView(buffer);
this.#characters = characters;
}

Expand All @@ -25,7 +25,7 @@ class Parser {
return 0x00;
}

const char = String.fromCharCode(charCode);
const char = String.fromCodePoint(charCode);

if (typeof this.#characters[char] !== 'undefined') {
return this.#characters[char];
Expand Down
14 changes: 14 additions & 0 deletions src/lib/serialiser/serialisePreps.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import Serialiser from './serialiser.js';

const serialisePreps = (preps, characters) => {
const serialiser = new Serialiser(characters);

for (let i = 0; i < preps.length; i++) {
serialiser.setString(preps[i]);
serialiser.setUint8(); // 0x00 is the default value.
}

return serialiser.buffer;
};

export default serialisePreps;
37 changes: 37 additions & 0 deletions src/lib/serialiser/serialiser.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
const MAX_BYTE_LENGTH = 12288; // The largest resource is sprdata.

class Serialiser {
#view;
#ptr = 0;
#characters = {};

constructor(characters = {}) {
const buffer = new ArrayBuffer(0, { maxByteLength: MAX_BYTE_LENGTH });
this.#view = new DataView(buffer);
// Swap keys for values.
this.#characters = Object.fromEntries(
Object.entries(characters).map(([key, value]) => [value, key]),
);
}

setUint8(value = 0x00) {
this.#view.buffer.resize(this.#ptr + 1);
this.#view.setUint8(this.#ptr++, value);
}

setString(str) {
// See https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/codePointAt#looping_with_codepointat
for (let codePoint of str) {
if (this.#characters[codePoint] !== undefined) {
codePoint = this.#characters[codePoint];
}
this.setUint8(codePoint.codePointAt(0));
}
}

get buffer() {
return this.#view.buffer;
}
}

export default Serialiser;
4 changes: 2 additions & 2 deletions test/parsePreps.test.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { describe, it } from 'node:test';
import assert from 'node:assert/strict';
import parsePreps from '../src/lib/parsePreps.js';
import parsePreps from '../src/lib/parser/parsePreps.js';

describe('parsePreps', () => {
it('should set the offset value in metadata.', () => {
Expand Down Expand Up @@ -39,7 +39,7 @@ describe('parsePreps', () => {
view.setUint8(4, 'c'.codePointAt(0));
view.setUint8(5, 0x00);

const characters = { a: '0', b: 1, c: '2' };
const characters = { a: '0', b: '1', c: '2' };
const { preps } = parsePreps(buffer, 0, 0, characters);
assert.deepEqual(preps, ['0', '1', '2']);
});
Expand Down
38 changes: 38 additions & 0 deletions test/serialisePreps.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
import { describe, it } from 'node:test';
import assert from 'node:assert/strict';
import serialisePreps from '../src/lib/serialiser/serialisePreps.js';

describe('serialisePreps', () => {
it('should return an instance of ArrayBuffer.', () => {
const preps = ['a', 'b', 'c'];
const buffer = serialisePreps(preps);
assert.ok(buffer instanceof ArrayBuffer);
});

it('should serialise prepositions.', () => {
const preps = ['a', 'b', 'c'];
const buffer = serialisePreps(preps);
const view = new DataView(buffer);

assert.equal(view.getUint8(0), 0x61);
assert.equal(view.getUint8(1), 0x00);
assert.equal(view.getUint8(2), 0x62);
assert.equal(view.getUint8(3), 0x00);
assert.equal(view.getUint8(4), 0x63);
assert.equal(view.getUint8(5), 0x00);
});

it('should serialise characters according to the character mapping.', () => {
const preps = ['0', '1', '2'];
const characters = { a: '0', b: '1', c: '2' };
const buffer = serialisePreps(preps, characters);

const view = new DataView(buffer);
assert.equal(view.getUint8(0), 0x61);
assert.equal(view.getUint8(1), 0x00);
assert.equal(view.getUint8(2), 0x62);
assert.equal(view.getUint8(3), 0x00);
assert.equal(view.getUint8(4), 0x63);
assert.equal(view.getUint8(5), 0x00);
});
});

0 comments on commit 2b95210

Please sign in to comment.