UNPKG

furnace-module-interface

Version:

An interface for loading, processing and saving furnace modules in an object-oriented manner. Fully typed via TypeScript.

241 lines (240 loc) 8.88 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.BufferWriteHelper = exports.BufferReadHelper = void 0; class BufferReadHelper { constructor(buffer) { this.buffer = buffer; this.buffer = buffer; this.position = 0; } skipBytes(offset) { this.position = this.getPosition(offset); } getPosition(offset) { let pos = this.position; if (offset) { if (pos + offset < 0 || pos + offset > this.buffer.byteLength) { throw new RangeError("Buffer position is out of range!"); } pos += offset; } return pos; } setPosition(position) { if (position < 0 || position > this.buffer.byteLength) { throw new RangeError("Buffer position is out of range!"); } this.position = position; } getASCII(length, position) { const pos = position !== null && position !== void 0 ? position : this.position; if (pos + length > this.buffer.length) { throw new RangeError("String length is too large for buffer!"); } this.setPosition(pos + length); return this.buffer.toString("ascii", pos, pos + length); } readString(encoding, position) { const end = this.findByte(0, position); const pos = position !== null && position !== void 0 ? position : this.position; this.setPosition(end + 1); return this.buffer.toString(encoding, pos, end); } findByte(byte, position) { let pos = position !== null && position !== void 0 ? position : this.position; while (pos < this.buffer.length) { if (this.buffer.readUInt8(pos) === byte) { return pos; } pos++; } throw new RangeError("Could not find the requested byte before end of buffer!"); } readBuffer(length, position) { const pos = position !== null && position !== void 0 ? position : this.position; if (pos + length > this.buffer.length) { throw new RangeError("Array length is too large for buffer!"); } if (!position) { this.setPosition(this.position + length); } return this.buffer.subarray(pos, pos + length); } readInt8(position) { const pos = position !== null && position !== void 0 ? position : this.position; if (!position) { this.setPosition(this.position + 1); } return this.buffer.readInt8(pos); } readUInt8(position) { const pos = position !== null && position !== void 0 ? position : this.position; if (!position) { this.setPosition(this.position + 1); } return this.buffer.readUInt8(pos); } readInt16LE(position) { const pos = position !== null && position !== void 0 ? position : this.position; if (!position) { this.setPosition(this.position + 2); } return this.buffer.readInt16LE(pos); } readUInt16LE(position) { const pos = position !== null && position !== void 0 ? position : this.position; if (!position) { this.setPosition(this.position + 2); } return this.buffer.readUInt16LE(pos); } readInt32LE(position) { const pos = position !== null && position !== void 0 ? position : this.position; if (!position) { this.setPosition(this.position + 4); } return this.buffer.readInt32LE(pos); } readUInt32LE(position) { const pos = position !== null && position !== void 0 ? position : this.position; if (!position) { this.setPosition(this.position + 4); } return this.buffer.readUInt32LE(pos); } readFloat32LE(position) { const pos = position !== null && position !== void 0 ? position : this.position; if (!position) { this.setPosition(this.position + 4); } return this.buffer.readFloatLE(pos); } } exports.BufferReadHelper = BufferReadHelper; const BUFFER_ALLOC_BASE = 4096; class BufferWriteHelper { constructor() { this.buffer = Buffer.alloc(BUFFER_ALLOC_BASE); this.position = 0; } getTrimmedBuffer() { return this.buffer.subarray(0, this.position); } expandBuffer(until) { if (this.buffer.byteLength > until) { return; } if (until < 0 || until > Number.MAX_VALUE) { throw new RangeError("Buffer position " + until + " is invalid!"); } let newmax = this.buffer.byteLength << 1; while (newmax < until) { newmax <<= 1; } if (newmax < 0 || newmax > Number.MAX_VALUE) { throw new RangeError("Buffer size " + newmax + " is invalid!"); } this.buffer = Buffer.concat([this.buffer, Buffer.alloc(newmax - this.buffer.byteLength),]); } skipBytes(offset) { this.position = this.getPosition(offset); this.expandBuffer(this.position); } getPosition(offset) { let pos = this.position; if (offset) { if (pos + offset < 0 || pos + offset > this.buffer.byteLength) { this.expandBuffer(pos + offset); } pos += offset; } return pos; } setPosition(position) { if (position < 0 || position >= this.buffer.byteLength) { this.expandBuffer(position); } this.position = position; } writeString(str, encoding, nullTerminated, position) { const pos = position !== null && position !== void 0 ? position : this.position; this.expandBuffer(pos + (str.length * 2)); let bytes = this.buffer.write(str, pos, encoding); if (nullTerminated) { this.writeInt8(0, pos + bytes); bytes++; } if (!position) { this.setPosition(this.position + bytes); } return bytes; } writeBuffer(buf, start, length, position) { const pos = position !== null && position !== void 0 ? position : this.position; if (!position) { this.setPosition(this.position + length); } this.expandBuffer(pos + length - 1); for (let i = 0; i < length; i++) { this.buffer.writeUint8(buf.readUint8(start + i), pos + i); } return length; } writeInt8(value, position) { const pos = position !== null && position !== void 0 ? position : this.position; if (!position) { this.setPosition(this.position + 1); } this.expandBuffer(pos); return this.buffer.writeInt8(value, pos); } writeUInt8(value, position) { const pos = position !== null && position !== void 0 ? position : this.position; if (!position) { this.setPosition(this.position + 1); } this.expandBuffer(pos); return this.buffer.writeUInt8(value, pos); } writeInt16LE(value, position) { const pos = position !== null && position !== void 0 ? position : this.position; if (!position) { this.setPosition(this.position + 2); } this.expandBuffer(pos + 1); return this.buffer.writeInt16LE(value, pos); } writeUInt16LE(value, position) { const pos = position !== null && position !== void 0 ? position : this.position; if (!position) { this.setPosition(this.position + 2); } this.expandBuffer(pos + 1); return this.buffer.writeUInt16LE(value, pos); } writeInt32LE(value, position) { const pos = position !== null && position !== void 0 ? position : this.position; if (!position) { this.setPosition(this.position + 4); } this.expandBuffer(pos + 3); return this.buffer.writeInt32LE(value, pos); } writeUInt32LE(value, position) { const pos = position !== null && position !== void 0 ? position : this.position; if (!position) { this.setPosition(this.position + 4); } this.expandBuffer(pos + 3); return this.buffer.writeUInt32LE(value, pos); } writeFloat32LE(value, position) { const pos = position !== null && position !== void 0 ? position : this.position; if (!position) { this.setPosition(this.position + 4); } this.expandBuffer(pos + 3); return this.buffer.writeFloatLE(value, pos); } } exports.BufferWriteHelper = BufferWriteHelper;