@bedrock-apis/nbt
Version:
Solid NBT package, with multiple formats
343 lines (321 loc) • 9.93 kB
JavaScript
import { UTF8_BUFFER_HELPER, UTF8_DECODER, UTF8_ENCODER, readVarInt32, readVarInt64, writeVarInt32, writeVarInt64 } from "./shared-D4Le05PG.js";
import { Byte, Double, Float, Int, Short, TagType } from "@bedrock-apis/nbt-core";
//#region main/readers/general.ts
var GeneralReader = class {
constructor(littleEndian, textEncoder) {
this.littleEndian = littleEndian;
this.textEncoder = textEncoder;
}
static {
this.prototype.readType = this.prototype[TagType.Byte];
this.prototype.readArrayLength = this.prototype[TagType.Int];
this.prototype.readStringLength = this.prototype[TagType.Short];
}
readType(_) {
return 0;
}
readArrayLength(_) {
return 0;
}
readStringLength(_) {
return 0;
}
/**
* TagType.byte
*/
1(cursor) {
return cursor.view.getUint8(cursor.pointer++);
}
/**
* TagType.Short
*/
2(cursor) {
const _ = cursor.view.getInt16(cursor.pointer, this.littleEndian);
return cursor.pointer += 2, _;
}
/**
* TagType.Int
*/
3(cursor) {
const _ = cursor.view.getInt32(cursor.pointer, this.littleEndian);
return cursor.pointer += 4, _;
}
/**
* TagType.Long
*/
4(cursor) {
const _ = cursor.view.getBigInt64(cursor.pointer, this.littleEndian);
return cursor.pointer += 8, _;
}
/**
* TagType.Float
*/
5(cursor) {
const _ = cursor.view.getFloat32(cursor.pointer, this.littleEndian);
return cursor.pointer += 4, _;
}
/**
* TagType.Double
*/
6(cursor) {
const _ = cursor.view.getFloat64(cursor.pointer, this.littleEndian);
return cursor.pointer += 8, _;
}
/**
* TagType.ByteArray
*/
7(cursor) {
const length = this.readArrayLength(cursor);
return cursor.buffer.subarray(cursor.pointer, cursor.pointer += length);
}
/**
* TagType.String
*/
8(cursor) {
const length = this.readStringLength(cursor);
return this.textEncoder.decode(cursor.buffer.subarray(cursor.pointer, cursor.pointer += length));
}
11(cursor) {
const length = this.readArrayLength(cursor);
const _ = new Int32Array(length);
const func = this["3"].bind(this, cursor);
for (let i = 0; i < length; i++) _[i] = func();
return _;
}
12(cursor) {
const length = this.readArrayLength(cursor);
const _ = new BigInt64Array(length);
const func = this["4"].bind(this, cursor);
for (let i = 0; i < length; i++) _[i] = func();
return _;
}
9(cursor) {
const type = this.readType(cursor);
const length = this.readArrayLength(cursor);
if (type === 0 || length === 0) return [];
if (!(type in this)) throw new SyntaxError("Unexpected NBT token type: " + type);
const _ = new Array(length);
const func = this[type].bind(this, cursor);
for (let i = 0; i < length; i++) _[i] = func();
return _;
}
10(cursor) {
const _ = {};
let type;
while ((type = this.readType(cursor)) !== 0) _[this["8"](cursor)] = this[type](cursor);
return _;
}
};
var GeneralVariantReader = class extends GeneralReader {
static {
this.prototype[4] = readVarInt64;
this.prototype.readArrayLength = this.prototype.readStringLength = this.prototype[3] = readVarInt32;
}
};
const NBT_FORMAT_READER = new GeneralReader(true, UTF8_DECODER);
const NBT_NETWORK_VARIANT_FORMAT_READER = new GeneralVariantReader(true, UTF8_DECODER);
const NBT_BIG_ENDIAN_FORMAT_READER = new GeneralReader(false, UTF8_DECODER);
const NBT_BIG_ENDIAN_NETWORK_VARIANT_FORMAT_READER = new GeneralVariantReader(false, UTF8_DECODER);
//#endregion
//#region main/readers/typed-reader.ts
var TypedReader = class {
static 1(cursor, format) {
return new Byte(format[1](cursor));
}
static 2(cursor, format) {
return new Short(format[2](cursor));
}
static 3(cursor, format) {
return new Int(format[3](cursor));
}
static 5(cursor, format) {
return new Float(format[5](cursor));
}
static 6(cursor, format) {
return new Double(format[6](cursor));
}
static 4(cursor, format) {
return format[4](cursor);
}
static 7(cursor, format) {
return format[7](cursor);
}
static 8(cursor, format) {
return format[8](cursor);
}
static 11(cursor, format) {
return format[11](cursor);
}
static 12(cursor, format) {
return format[12](cursor);
}
static 10(cursor, format) {
const _ = {};
let type;
while ((type = format.readType(cursor)) !== 0) _[format["8"](cursor)] = this[type](cursor, format);
return _;
}
static 9(cursor, format) {
const type = format.readType(cursor);
const length = format.readArrayLength(cursor);
if (type === 0 || length === 0) return [];
if (!(type in this)) throw new SyntaxError("Unexpected NBT token type: " + type);
const _ = new Array(length);
const func = this[type].bind(this, cursor, format);
for (let i = 0; i < length; i++) _[i] = func();
return _;
}
static parseRootSync(cursor, format) {
const type = format.readType(cursor);
let _ = format[8](cursor);
return this[type](cursor, format);
}
static parseSync(cursor, format) {
const type = format.readType(cursor);
return this[type](cursor, format);
}
static parseExplicitSync(cursor, type, format) {
return this[type](cursor, format);
}
};
//#endregion
//#region main/readers/index.ts
function parseRootSync(cursor, format = NBT_FORMAT_READER) {
const _ = format.readType(cursor);
return format[8](cursor), format[_](cursor);
}
function parseExplicitSync(cursor, type, format = NBT_FORMAT_READER) {
return format[type](cursor);
}
function parseSync(cursor, format = NBT_FORMAT_READER) {
return format[format.readType(cursor)](cursor);
}
//#endregion
//#region main/writers/general.ts
const { keys } = Object;
var GeneralWriter = class {
constructor(littleEndian, textEncoder) {
this.littleEndian = littleEndian;
this.textEncoder = textEncoder;
}
static {
this.prototype.writeType = this.prototype[TagType.Byte];
this.prototype.writeArrayLength = this.prototype[TagType.Int];
this.prototype.writeStringLength = this.prototype[TagType.Short];
}
writeType(_cursor, _) {}
writeStringLength(_cursor, _) {}
writeArrayLength(_cursor, _) {}
1(cursor, _) {
cursor.view.setUint8(cursor.pointer++, _);
}
2(cursor, _) {
cursor.view.setUint16(cursor.pointer, _, this.littleEndian);
cursor.pointer += 2;
}
3(cursor, _) {
cursor.view.setUint32(cursor.pointer, _, this.littleEndian);
cursor.pointer += 4;
}
4(cursor, _) {
cursor.view.setBigUint64(cursor.pointer, _, this.littleEndian);
cursor.pointer += 8;
}
5(cursor, _) {
cursor.view.setFloat32(cursor.pointer, _, this.littleEndian);
cursor.pointer += 4;
}
6(cursor, _) {
cursor.view.setFloat64(cursor.pointer, _, this.littleEndian);
cursor.pointer += 8;
}
7(cursor, _) {
this.writeArrayLength(cursor, _.length);
cursor.buffer.set(_, cursor.pointer);
cursor.pointer += _.byteLength;
}
8(cursor, _) {
if (_.length == 0) return void this.writeStringLength(cursor, 0);
const { written } = this.textEncoder.encodeInto(_, UTF8_BUFFER_HELPER);
this.writeStringLength(cursor, written);
cursor.buffer.set(UTF8_BUFFER_HELPER.subarray(0, written), cursor.pointer);
cursor.pointer += written;
}
9(cursor, _) {
if (_.length === 0) {
this.writeType(cursor, TagType.EndOfCompound);
this.writeArrayLength(cursor, 0);
return;
}
const type = this.determinateType(_[0]);
const writer = this[type].bind(this, cursor);
const length = _.length;
this.writeType(cursor, type);
this.writeArrayLength(cursor, length);
for (let i = 0; i < length; i++) writer(_[i].valueOf());
}
10(cursor, _) {
const k = keys(_);
for (let i = 0; i < k.length; i++) {
const key = k[i];
const value = _[key];
const type = this.determinateType(value);
if (type === 0) continue;
this.writeType(cursor, type);
this["8"](cursor, key);
this[type](cursor, value.valueOf());
}
this.writeType(cursor, 0);
}
11(cursor, _) {
const length = _.length;
this.writeArrayLength(cursor, length);
const writer = this[3].bind(this, cursor);
for (let i = 0; i < length; i++) writer(_[i]);
}
12(cursor, _) {
const length = _.length;
this.writeArrayLength(cursor, length);
const writer = this[4].bind(this, cursor);
for (let i = 0; i < length; i++) writer(_[i]);
}
determinateType(_) {
switch (typeof _) {
case "bigint": return 5;
case "number": return 4;
case "boolean": return 1;
case "string": return 8;
case "object": return _.internalTagType ?? 10;
}
return 0;
}
};
var GeneralVariantWriter = class extends GeneralWriter {
static {
this.prototype[4] = writeVarInt64;
this.prototype.writeArrayLength = this.prototype.writeStringLength = this.prototype[3] = writeVarInt32;
}
};
const NBT_FORMAT_WRITER = new GeneralWriter(true, UTF8_ENCODER);
const NBT_NETWORK_VARIANT_FORMAT_WRITER = new GeneralVariantWriter(true, UTF8_ENCODER);
const NBT_BIG_ENDIAN_FORMAT_WRITER = new GeneralWriter(false, UTF8_ENCODER);
const NBT_BIG_ENDIAN_NETWORK_VARIANT_FORMAT_WRITER = new GeneralVariantWriter(false, UTF8_ENCODER);
//#endregion
//#region main/writers/index.ts
function writeRootSync(cursor, value, format = NBT_FORMAT_WRITER, root = "") {
const type = format.determinateType(value);
format.writeType(cursor, type);
format[8](cursor, root);
format[type](cursor, value.valueOf());
}
function writeExplicitSync(cursor, value, type, format = NBT_FORMAT_WRITER) {
if (type === 0) return;
format[type](cursor, value);
}
function writeSync(cursor, value, format = NBT_FORMAT_WRITER) {
const type = format.determinateType(value);
format.writeType(cursor, type);
format[type](cursor, value.valueOf());
}
//#endregion
export { GeneralReader, GeneralVariantReader, GeneralVariantWriter, GeneralWriter, NBT_BIG_ENDIAN_FORMAT_READER, NBT_BIG_ENDIAN_FORMAT_WRITER, NBT_BIG_ENDIAN_NETWORK_VARIANT_FORMAT_READER, NBT_BIG_ENDIAN_NETWORK_VARIANT_FORMAT_WRITER, NBT_FORMAT_READER, NBT_FORMAT_WRITER, NBT_NETWORK_VARIANT_FORMAT_READER, NBT_NETWORK_VARIANT_FORMAT_WRITER, TypedReader, parseExplicitSync, parseRootSync, parseSync, writeExplicitSync, writeRootSync, writeSync };