mc-beta-server/server/bufferStuff.js

174 lines
3.1 KiB
JavaScript
Raw Normal View History

2021-08-15 00:21:41 +01:00
/*
===========- bufferStuff.js -===========
Created by Holly (tgpethan) (c) 2021
2021-08-15 11:32:17 +01:00
Licenced under MIT
2021-08-15 00:21:41 +01:00
========================================
*/
2021-08-10 00:40:30 +01:00
module.exports.Writer = class {
2021-08-20 22:42:00 +01:00
constructor(size = 0) {
this.buffer = Buffer.alloc(size);
this.offset = 0;
this.baseSize = size;
2021-08-10 23:21:44 +01:00
}
reset() {
this.buffer = Buffer.alloc(0);
2021-08-10 00:40:30 +01:00
}
writeBuffer(buff = Buffer.alloc(0)) {
this.buffer = Buffer.concat([this.buffer, buff], this.buffer.length + buff.length);
}
writeBool(data = false) {
this.writeByte(data ? 1 : 0);
}
writeByte(data = 0) {
2021-08-20 22:42:00 +01:00
if (this.baseSize == 0) {
const buff = Buffer.alloc(1);
buff.writeInt8(data, 0);
this.writeBuffer(buff);
} else {
this.buffer.writeInt8(data, this.offset);
this.offset += 1;
}
}
2021-08-10 00:40:30 +01:00
2021-08-20 22:42:00 +01:00
writeUByte(data = 0) {
if (this.baseSize == 0) {
const buff = Buffer.alloc(1);
buff.writeUInt8(data, 0);
this.writeBuffer(buff);
} else {
this.buffer.writeUInt8(data, this.offset);
this.offset += 1;
}
2021-08-10 00:40:30 +01:00
}
2021-08-12 04:58:56 +01:00
writeByteArray(data = [0]) {
const buff = Buffer.alloc(data.length);
for (let byte of data) {
buff.writeInt8(byte);
}
this.writeBuffer(buff);
}
2021-08-10 00:40:30 +01:00
writeShort(data = 0) {
const buff = Buffer.alloc(2);
buff.writeIntBE(data, 0, 2);
this.writeBuffer(buff);
}
2021-08-12 04:58:56 +01:00
writeShortArray(data = [0]) {
const buff = Buffer.alloc(data.length * 2);
let offset = 0;
for (let short of data) {
buff.writeIntBE(short, offset, 2);
offset += 2;
}
this.writeBuffer(buff);
}
2021-08-10 00:40:30 +01:00
writeInt(data = 0) {
const buff = Buffer.alloc(4);
buff.writeIntBE(data, 0, 4);
this.writeBuffer(buff);
}
writeLong(data = 0) {
const buff = Buffer.alloc(8);
2021-08-15 00:21:41 +01:00
if (data instanceof BigInt) buff.writeBigInt64BE(data, 0);
else buff.writeBigInt64BE(BigInt(data), 0);
2021-08-10 00:40:30 +01:00
this.writeBuffer(buff);
}
writeFloat(data = 0.0) {
const buff = Buffer.alloc(4);
buff.writeFloatBE(data, 0);
this.writeBuffer(buff);
}
writeDouble(data = 0.0) {
const buff = Buffer.alloc(8);
buff.writeDoubleBE(data, 0);
this.writeBuffer(buff);
}
writeString(string = "") {
this.writeShort(string.length);
for (let i = 0; i < string.length; i++) {
this.writeShort(string.charCodeAt(i));
}
}
}
module.exports.Reader = class {
constructor(buffer = Buffer.alloc(0)) {
this.buffer = buffer;
this.offset = 0;
}
readBool() {
return this.readByte() == 0x01 ? true : false;
}
readByte() {
const data = this.buffer.readInt8(this.offset);
this.offset += 1;
return data;
}
readShort() {
const data = this.buffer.readIntBE(this.offset, 2);
this.offset += 2;
return data;
}
readInt() {
const data = this.buffer.readIntBE(this.offset, 4);
this.offset += 4;
return data;
}
readLong() {
const data = this.buffer.readBigInt64BE(this.offset);
this.offset += 8;
return data;
}
readFloat() {
const data = this.buffer.readFloatBE(this.offset);
this.offset += 4;
return data;
}
readDouble() {
const data = this.buffer.readDoubleBE(this.offset);
this.offset += 8;
return data;
}
readString() {
const length = this.readShort();
let data = "";
for (let i = 0; i < length; i++) {
data += String.fromCharCode(this.readShort());
}
return data;
}
}