From 4d67b70dbad9c678ddaafb2344b68cdccc2f20ca Mon Sep 17 00:00:00 2001 From: edo999 Date: Tue, 28 May 2024 21:37:00 +0100 Subject: [PATCH] =?UTF-8?q?Change=20the=20capitalisation=20of=20classes=20?= =?UTF-8?q?=F0=9F=8F=9B=EF=B8=8F.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/lib/parser/Parser.js | 110 ++++++++++++++++++ src/lib/parser/parseGlobdata.js | 2 +- src/lib/parser/parsePalette.js | 2 +- src/lib/parser/parsePreps.js | 2 +- src/lib/parser/parseRoomGfx.js | 2 +- src/lib/parser/parseRooms.js | 2 +- src/lib/parser/parseScript.js | 2 +- src/lib/parser/parseTitles.js | 2 +- src/lib/parser/room/parseRoomAttributes.js | 2 +- src/lib/parser/room/parseRoomBoxes.js | 2 +- src/lib/parser/room/parseRoomHeader.js | 2 +- src/lib/parser/room/parseRoomMatrix.js | 2 +- src/lib/parser/room/parseRoomNametable.js | 2 +- src/lib/serialiser/Serialiser.js | 58 +++++++++ .../room/serialiseRoomAttributes.js | 2 +- src/lib/serialiser/room/serialiseRoomBoxes.js | 2 +- .../serialiser/room/serialiseRoomHeader.js | 2 +- .../serialiser/room/serialiseRoomMatrix.js | 2 +- .../serialiser/room/serialiseRoomNametable.js | 2 +- src/lib/serialiser/serialisePalette.js | 2 +- src/lib/serialiser/serialisePreps.js | 2 +- 21 files changed, 187 insertions(+), 19 deletions(-) create mode 100644 src/lib/parser/Parser.js create mode 100644 src/lib/serialiser/Serialiser.js diff --git a/src/lib/parser/Parser.js b/src/lib/parser/Parser.js new file mode 100644 index 0000000..ba039c2 --- /dev/null +++ b/src/lib/parser/Parser.js @@ -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 += ``; + 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; diff --git a/src/lib/parser/parseGlobdata.js b/src/lib/parser/parseGlobdata.js index d678883..45c028b 100644 --- a/src/lib/parser/parseGlobdata.js +++ b/src/lib/parser/parseGlobdata.js @@ -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); diff --git a/src/lib/parser/parsePalette.js b/src/lib/parser/parsePalette.js index 59c9536..e1c62d1 100644 --- a/src/lib/parser/parsePalette.js +++ b/src/lib/parser/parsePalette.js @@ -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) => { diff --git a/src/lib/parser/parsePreps.js b/src/lib/parser/parsePreps.js index 943a195..b991df9 100644 --- a/src/lib/parser/parsePreps.js +++ b/src/lib/parser/parsePreps.js @@ -1,4 +1,4 @@ -import Parser from './parser.js'; +import Parser from './Parser.js'; const assert = console.assert; diff --git a/src/lib/parser/parseRoomGfx.js b/src/lib/parser/parseRoomGfx.js index 52f26b6..94a7f6c 100644 --- a/src/lib/parser/parseRoomGfx.js +++ b/src/lib/parser/parseRoomGfx.js @@ -1,4 +1,4 @@ -import Parser from './parser.js'; +import Parser from './Parser.js'; const assert = console.assert; diff --git a/src/lib/parser/parseRooms.js b/src/lib/parser/parseRooms.js index b130127..67c0aa8 100644 --- a/src/lib/parser/parseRooms.js +++ b/src/lib/parser/parseRooms.js @@ -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'; diff --git a/src/lib/parser/parseScript.js b/src/lib/parser/parseScript.js index 9630dde..e58a194 100644 --- a/src/lib/parser/parseScript.js +++ b/src/lib/parser/parseScript.js @@ -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 = {}) => { diff --git a/src/lib/parser/parseTitles.js b/src/lib/parser/parseTitles.js index 7012f82..75860cb 100644 --- a/src/lib/parser/parseTitles.js +++ b/src/lib/parser/parseTitles.js @@ -1,4 +1,4 @@ -import Parser from './parser.js'; +import Parser from './Parser.js'; import parsePalette from './parsePalette.js'; const assert = console.assert; diff --git a/src/lib/parser/room/parseRoomAttributes.js b/src/lib/parser/room/parseRoomAttributes.js index c674232..59a6a69 100644 --- a/src/lib/parser/room/parseRoomAttributes.js +++ b/src/lib/parser/room/parseRoomAttributes.js @@ -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); diff --git a/src/lib/parser/room/parseRoomBoxes.js b/src/lib/parser/room/parseRoomBoxes.js index c22acb0..630581a 100644 --- a/src/lib/parser/room/parseRoomBoxes.js +++ b/src/lib/parser/room/parseRoomBoxes.js @@ -1,4 +1,4 @@ -import Parser from '../parser.js'; +import Parser from '../Parser.js'; const assert = console.assert; diff --git a/src/lib/parser/room/parseRoomHeader.js b/src/lib/parser/room/parseRoomHeader.js index 42f0c94..f026da8 100644 --- a/src/lib/parser/room/parseRoomHeader.js +++ b/src/lib/parser/room/parseRoomHeader.js @@ -1,4 +1,4 @@ -import Parser from '../parser.js'; +import Parser from '../Parser.js'; const assert = console.assert; diff --git a/src/lib/parser/room/parseRoomMatrix.js b/src/lib/parser/room/parseRoomMatrix.js index b1d59c7..636333d 100644 --- a/src/lib/parser/room/parseRoomMatrix.js +++ b/src/lib/parser/room/parseRoomMatrix.js @@ -1,4 +1,4 @@ -import Parser from '../parser.js'; +import Parser from '../Parser.js'; const assert = console.assert; diff --git a/src/lib/parser/room/parseRoomNametable.js b/src/lib/parser/room/parseRoomNametable.js index 28b73c1..7715d09 100644 --- a/src/lib/parser/room/parseRoomNametable.js +++ b/src/lib/parser/room/parseRoomNametable.js @@ -1,4 +1,4 @@ -import Parser from '../parser.js'; +import Parser from '../Parser.js'; const assert = console.assert; diff --git a/src/lib/serialiser/Serialiser.js b/src/lib/serialiser/Serialiser.js new file mode 100644 index 0000000..f679716 --- /dev/null +++ b/src/lib/serialiser/Serialiser.js @@ -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; diff --git a/src/lib/serialiser/room/serialiseRoomAttributes.js b/src/lib/serialiser/room/serialiseRoomAttributes.js index 2bf4d94..63f4085 100644 --- a/src/lib/serialiser/room/serialiseRoomAttributes.js +++ b/src/lib/serialiser/room/serialiseRoomAttributes.js @@ -1,4 +1,4 @@ -import Serialiser from '../serialiser.js'; +import Serialiser from '../Serialiser.js'; import { compress } from '../serialiserUtils.js'; const serialiseRoomAttributes = (attributes = [], width = 0) => { diff --git a/src/lib/serialiser/room/serialiseRoomBoxes.js b/src/lib/serialiser/room/serialiseRoomBoxes.js index 2cb8169..cd4b356 100644 --- a/src/lib/serialiser/room/serialiseRoomBoxes.js +++ b/src/lib/serialiser/room/serialiseRoomBoxes.js @@ -1,4 +1,4 @@ -import Serialiser from '../serialiser.js'; +import Serialiser from '../Serialiser.js'; const serialiseRoomBoxes = (boxes = []) => { const serialiser = new Serialiser(); diff --git a/src/lib/serialiser/room/serialiseRoomHeader.js b/src/lib/serialiser/room/serialiseRoomHeader.js index aabea8f..63c64aa 100644 --- a/src/lib/serialiser/room/serialiseRoomHeader.js +++ b/src/lib/serialiser/room/serialiseRoomHeader.js @@ -1,4 +1,4 @@ -import Serialiser from '../serialiser.js'; +import Serialiser from '../Serialiser.js'; const serialiseRoomHeader = (header) => { const serialiser = new Serialiser(); diff --git a/src/lib/serialiser/room/serialiseRoomMatrix.js b/src/lib/serialiser/room/serialiseRoomMatrix.js index 4c3be5d..747e4f5 100644 --- a/src/lib/serialiser/room/serialiseRoomMatrix.js +++ b/src/lib/serialiser/room/serialiseRoomMatrix.js @@ -1,4 +1,4 @@ -import Serialiser from '../serialiser.js'; +import Serialiser from '../Serialiser.js'; const serialiseRoomMatrix = (matrix = []) => { const serialiser = new Serialiser(); diff --git a/src/lib/serialiser/room/serialiseRoomNametable.js b/src/lib/serialiser/room/serialiseRoomNametable.js index ad974c7..3a7c787 100644 --- a/src/lib/serialiser/room/serialiseRoomNametable.js +++ b/src/lib/serialiser/room/serialiseRoomNametable.js @@ -1,4 +1,4 @@ -import Serialiser from '../serialiser.js'; +import Serialiser from '../Serialiser.js'; import { compress } from '../serialiserUtils.js'; const serialiseRoomNametable = (nametable = []) => { diff --git a/src/lib/serialiser/serialisePalette.js b/src/lib/serialiser/serialisePalette.js index 74c28e6..929e0aa 100644 --- a/src/lib/serialiser/serialisePalette.js +++ b/src/lib/serialiser/serialisePalette.js @@ -1,4 +1,4 @@ -import Serialiser from './serialiser.js'; +import Serialiser from './Serialiser.js'; const serialisePalette = (palette = []) => { const serialiser = new Serialiser(); diff --git a/src/lib/serialiser/serialisePreps.js b/src/lib/serialiser/serialisePreps.js index a6449a4..0287d98 100644 --- a/src/lib/serialiser/serialisePreps.js +++ b/src/lib/serialiser/serialisePreps.js @@ -1,4 +1,4 @@ -import Serialiser from './serialiser.js'; +import Serialiser from './Serialiser.js'; const serialisePreps = (preps = [], characters = {}) => { const serialiser = new Serialiser(characters);