mc-anvil
Version:
A Typescript library for reading Minecraft Anvil format files and Minecraft NBT format files in the browser.
184 lines • 5.46 kB
JavaScript
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.BinaryParser = void 0;
class BinaryParser {
constructor(data) {
this._getByte = () => 0;
this._getShort = () => 0;
this._getUShort = () => 0;
this._getInt = () => 0;
this._getUInt = () => 0;
this._getFloat = () => 0;
this._getDouble = () => 0;
this._getInt64 = () => BigInt(0);
this._getInt64LE = () => BigInt(0);
this.view = new DataView(data);
this.position = 0;
this.bindReaders();
}
buffer() {
return this.view.buffer;
}
bindReaders() {
this._getByte = this.view.getUint8.bind(this.view);
this._getShort = this.view.getInt16.bind(this.view);
this._getUShort = this.view.getUint16.bind(this.view);
this._getInt = this.view.getInt32.bind(this.view);
this._getUInt = this.view.getUint32.bind(this.view);
this._getFloat = this.view.getFloat32.bind(this.view);
this._getDouble = this.view.getFloat64.bind(this.view);
this._getInt64 = (byteOffset) => this.view.getBigInt64(byteOffset, true);
this._getInt64LE = (byteOffset) => this.view.getBigInt64(byteOffset);
}
seek(position) {
this.position = position;
}
currentPosition() {
return this.position;
}
remainingLength() {
return this.view.byteLength - this.position;
}
getValue(readFunc, positionIncrement) {
const retValue = readFunc(this.position);
this.position += positionIncrement;
return retValue;
}
getBigValue(readFunc, positionIncrement) {
const retValue = readFunc(this.position);
this.position += positionIncrement;
return retValue;
}
getByte() {
return this.getValue(this._getByte, 1);
}
setByte(value) {
this.view.setUint8(this.position++, value);
}
getShort() {
return this.getValue(this._getShort, 2);
}
setShort(value) {
this.view.setInt16(this.position, value);
this.position += 2;
}
getUShort() {
return this.getValue(this._getUShort, 2);
}
setUShort(value) {
this.view.setUint16(this.position, value);
this.position += 2;
}
getInt() {
return this.getValue(this._getInt, 4);
}
setInt(value) {
this.view.setInt32(this.position, value);
this.position += 4;
}
getUInt() {
return this.getValue(this._getUInt, 4);
}
setUInt(value) {
this.view.setUint32(this.position, value);
this.position += 4;
}
getFloat() {
return this.getValue(this._getFloat, 4);
}
setFloat(value) {
this.view.setFloat32(this.position, value);
this.position += 4;
}
getDouble() {
return this.getValue(this._getDouble, 8);
}
setDouble(value) {
this.view.setFloat64(this.position, value);
this.position += 8;
}
getNByteInteger(n) {
const b = [];
for (let i = 0; i < n; ++i)
b[i] = this.view.getUint8(this.position + i);
let value = 0;
for (let i = 0; i < b.length; ++i)
value = (value * 256) + b[i];
this.position += n;
return value;
}
setNByteInteger(value, n) {
for (let i = n - 1; i >= 0; --i)
this.setByte(Math.floor(value / Math.pow(256, i)) % 256);
}
getUInt64() {
const v = this.view.getBigUint64(this.position);
this.position += 8;
return v;
}
setUInt64(value) {
this.view.setBigUint64(this.position, value);
this.position += 8;
}
getInt64() {
return this.getBigValue(this._getInt64, 8);
}
setInt64(value) {
this.view.setBigInt64(this.position, value);
this.position += 8;
}
getUInt64LE() {
const v = this.view.getBigUint64(this.position, true);
this.position += 8;
return v;
}
setUInt64LE(value) {
this.view.setBigUint64(this.position, value, true);
this.position += 8;
}
getInt64LE() {
return this.getBigValue(this._getInt64LE, 8);
}
setInt64LE(value) {
this.view.setBigUint64(this.position, value, true);
this.position += 8;
}
getString(len) {
let s = "", c;
while ((c = this.view.getUint8(this.position++)) != 0) {
s += String.fromCharCode(c);
if (len && s.length == len)
break;
}
return s;
}
setString(value) {
for (let i = 0; i < value.length; ++i)
this.setByte(value.charCodeAt(i));
this.setByte(0);
}
getFixedLengthString(len) {
let s = "";
for (let i = 0; i < len; i++) {
const c = this.view.getUint8(this.position++);
if (c > 0)
s += String.fromCharCode(c);
}
return s;
}
setFixedLengthString(value) {
for (let i = 0; i < value.length; ++i)
this.setByte(value.charCodeAt(i));
}
getFixedLengthTrimmedString(len) {
let s = "";
for (let i = 0; i < len; i++) {
const c = this.view.getUint8(this.position++);
if (c > 32)
s += String.fromCharCode(c);
}
return s;
}
}
exports.BinaryParser = BinaryParser;
//# sourceMappingURL=binary.js.map