Skip to content

Commit

Permalink
Change the capitalisation of classes 🏛️.
Browse files Browse the repository at this point in the history
  • Loading branch information
gmarty committed May 28, 2024
1 parent d09eacf commit 4d67b70
Show file tree
Hide file tree
Showing 21 changed files with 187 additions and 19 deletions.
110 changes: 110 additions & 0 deletions src/lib/parser/Parser.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,110 @@
class Parser {
#view;
#ptr = 0;
#characters = {};

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

getUint8() {
return this.#view.getUint8(this.#ptr++);
}

getUint16() {
const val = this.#view.getUint16(this.#ptr, true);
this.#ptr += 2;
return val;
}

getChar() {
const charCode = this.getUint8();

if (charCode === 0x00) {
return 0x00;
}

const char = String.fromCodePoint(charCode);

if (typeof this.#characters[char] !== 'undefined') {
return this.#characters[char];
}

return char;
}

getString() {
let str = '';
let charCode;
do {
charCode = this.getUint8();

if (charCode === 0x00) {
break;
}

const flag = (charCode & 0x80) !== 0;
charCode &= 0x7f;

if (charCode < 8) {
switch (charCode) {
case 1:
// Line break
str += '<01>\n';
break;
case 2:
// Do not clear the text with the next `printEgo` call?
str += '<02>';
break;
case 3:
// New line
str += '<03>\n';
break;
case 4:
// Used only in the address sign.
str += `<Var[${this.getUint8()}]>`;
break;
case 0:
case 5:
case 6:
case 7:
throw new Error(`Unknown escape code ${charCode}.`);
default:
break;
}

continue;
}

let char = String.fromCodePoint(charCode);

if (typeof this.#characters[char] === 'string') {
char = this.#characters[char];
}

str += char;

if (flag) {
str += ' ';
}
} while (true);

return str;
}

peekUint8() {
return this.#view.getUint8(this.#ptr);
}

// Return the position of the next byte to read.
get pointer() {
return this.#ptr;
}

get length() {
return this.#view.byteLength;
}
}

export default Parser;
2 changes: 1 addition & 1 deletion src/lib/parser/parseGlobdata.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import Parser from './parser.js';
import Parser from './Parser.js';

const parseGlobdata = (arrayBuffer, i = 0, offset = 0) => {
const parser = new Parser(arrayBuffer);
Expand Down
2 changes: 1 addition & 1 deletion src/lib/parser/parsePalette.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import Parser from './parser.js';
import Parser from './Parser.js';

// Generic palette parser used in rooms and title screens.
const parsePalette = (arrayBuffer, offset = 0) => {
Expand Down
2 changes: 1 addition & 1 deletion src/lib/parser/parsePreps.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import Parser from './parser.js';
import Parser from './Parser.js';

const assert = console.assert;

Expand Down
2 changes: 1 addition & 1 deletion src/lib/parser/parseRoomGfx.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import Parser from './parser.js';
import Parser from './Parser.js';

const assert = console.assert;

Expand Down
2 changes: 1 addition & 1 deletion src/lib/parser/parseRooms.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import Parser from './parser.js';
import Parser from './Parser.js';
import parseRoomHeader from './room/parseRoomHeader.js';
import parseRoomNametable from './room/parseRoomNametable.js';
import parsePalette from './parsePalette.js';
Expand Down
2 changes: 1 addition & 1 deletion src/lib/parser/parseScript.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import parseScriptCode from './parseScriptCode.js';
import Parser from './parser.js';
import Parser from './Parser.js';
const assert = console.assert;

const parseScript = (arrayBuffer, i, offset = 0, characters = {}) => {
Expand Down
2 changes: 1 addition & 1 deletion src/lib/parser/parseTitles.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import Parser from './parser.js';
import Parser from './Parser.js';
import parsePalette from './parsePalette.js';

const assert = console.assert;
Expand Down
2 changes: 1 addition & 1 deletion src/lib/parser/room/parseRoomAttributes.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import Parser from '../parser.js';
import Parser from '../Parser.js';

const parseRoomAttributes = (arrayBuffer, offset = 0, width = 0) => {
const parser = new Parser(arrayBuffer);
Expand Down
2 changes: 1 addition & 1 deletion src/lib/parser/room/parseRoomBoxes.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import Parser from '../parser.js';
import Parser from '../Parser.js';

const assert = console.assert;

Expand Down
2 changes: 1 addition & 1 deletion src/lib/parser/room/parseRoomHeader.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import Parser from '../parser.js';
import Parser from '../Parser.js';

const assert = console.assert;

Expand Down
2 changes: 1 addition & 1 deletion src/lib/parser/room/parseRoomMatrix.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import Parser from '../parser.js';
import Parser from '../Parser.js';

const assert = console.assert;

Expand Down
2 changes: 1 addition & 1 deletion src/lib/parser/room/parseRoomNametable.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import Parser from '../parser.js';
import Parser from '../Parser.js';

const assert = console.assert;

Expand Down
58 changes: 58 additions & 0 deletions src/lib/serialiser/Serialiser.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
const maxByteLength = 16384; // The largest resource is sprdata.

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

constructor(characters = {}) {
const buffer = new ArrayBuffer(0, { maxByteLength });
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.#resize(this.#ptr + 1);
this.#view.setUint8(this.#ptr++, value);
}

setUint16(value = 0x00) {
this.#resize(this.#ptr + 2);
this.#view.setUint16(this.#ptr, value, true);
this.#ptr += 2;
}

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));
}
}

// Firefox doesn't support ArrayBuffer#resize.
#resize(newByteLength) {
if (ArrayBuffer.prototype.resize) {
this.#view.buffer.resize(newByteLength);
return;
}

const newArrayBuffer = new ArrayBuffer(newByteLength);
const view = new DataView(newArrayBuffer);
for (let i = 0; i < this.#view.buffer.byteLength; i++) {
view.setUint8(i, this.#view.getUint8(i));
}
this.#view = view;
}

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

export default Serialiser;
2 changes: 1 addition & 1 deletion src/lib/serialiser/room/serialiseRoomAttributes.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import Serialiser from '../serialiser.js';
import Serialiser from '../Serialiser.js';
import { compress } from '../serialiserUtils.js';

const serialiseRoomAttributes = (attributes = [], width = 0) => {
Expand Down
2 changes: 1 addition & 1 deletion src/lib/serialiser/room/serialiseRoomBoxes.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import Serialiser from '../serialiser.js';
import Serialiser from '../Serialiser.js';

const serialiseRoomBoxes = (boxes = []) => {
const serialiser = new Serialiser();
Expand Down
2 changes: 1 addition & 1 deletion src/lib/serialiser/room/serialiseRoomHeader.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import Serialiser from '../serialiser.js';
import Serialiser from '../Serialiser.js';

const serialiseRoomHeader = (header) => {
const serialiser = new Serialiser();
Expand Down
2 changes: 1 addition & 1 deletion src/lib/serialiser/room/serialiseRoomMatrix.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import Serialiser from '../serialiser.js';
import Serialiser from '../Serialiser.js';

const serialiseRoomMatrix = (matrix = []) => {
const serialiser = new Serialiser();
Expand Down
2 changes: 1 addition & 1 deletion src/lib/serialiser/room/serialiseRoomNametable.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import Serialiser from '../serialiser.js';
import Serialiser from '../Serialiser.js';
import { compress } from '../serialiserUtils.js';

const serialiseRoomNametable = (nametable = []) => {
Expand Down
2 changes: 1 addition & 1 deletion src/lib/serialiser/serialisePalette.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import Serialiser from './serialiser.js';
import Serialiser from './Serialiser.js';

const serialisePalette = (palette = []) => {
const serialiser = new Serialiser();
Expand Down
2 changes: 1 addition & 1 deletion src/lib/serialiser/serialisePreps.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import Serialiser from './serialiser.js';
import Serialiser from './Serialiser.js';

const serialisePreps = (preps = [], characters = {}) => {
const serialiser = new Serialiser(characters);
Expand Down

0 comments on commit 4d67b70

Please sign in to comment.