UNPKG

rl-loadout-lib

Version:

Load Rocket League assets into three.js

70 lines 2.28 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); class BitBinaryWriter { constructor(bufferSize) { this.currentBit = 0; this.buffer = new Uint8Array(bufferSize); } writeNumber(n, bits) { const a = new Uint16Array([n]); for (let i = 0; i < bits; i++) { if ((a[0] & 1) === 1) { this.buffer[Math.floor(this.currentBit / 8)] |= (1 << (this.currentBit % 8)); } else { this.buffer[Math.floor(this.currentBit / 8)] &= ~(1 << (this.currentBit % 8)); } this.currentBit++; a[0] >>= 1; } } writeBool(b) { this.writeNumber(b ? 1 : 0, 1); } calculateCrc(startByte, endByte) { const a = new Uint8Array([255]); for (let i = startByte; i < endByte; i++) { a[0] = a[0] ^ this.buffer[i]; } return a[0]; } toHex() { const sizeInBytes = Math.floor(this.currentBit / 8) + (this.currentBit % 8 === 0 ? 0 : 1); // @ts-ignore return btoa(String.fromCharCode.apply(null, this.buffer.slice(0, sizeInBytes))); } } exports.BitBinaryWriter = BitBinaryWriter; class BitBinaryReader { constructor(hexString) { this.currentBit = 0; const str = atob(hexString); this.buffer = new Uint8Array(new ArrayBuffer(str.length)); for (let i = 0, strLen = str.length; i < strLen; i++) { this.buffer[i] = str.charCodeAt(i); } } readNumber(bits) { const a = new Uint16Array([0]); for (let i = 0; i < bits; i++) { a[0] |= ((this.buffer[Math.floor(this.currentBit / 8)] >> (this.currentBit % 8)) & 1) << i; this.currentBit++; } return a[0]; } readBool() { return this.readNumber(1) === 1; } calculateCrc(startByte, endByte) { const a = new Uint8Array([255]); for (let i = startByte; i < endByte; i++) { a[0] = a[0] ^ this.buffer[i]; } return a[0]; } verifyCrc(crc, startByte, endByte) { return (crc ^ this.calculateCrc(startByte, endByte)) === 0; } } exports.BitBinaryReader = BitBinaryReader; //# sourceMappingURL=binary.js.map