UNPKG

node-insim

Version:

An InSim library for NodeJS with TypeScript support

85 lines (84 loc) 2.79 kB
import { __decorate } from "tslib"; import { byte } from '../decorators'; import { InSimError } from '../errors'; import { copyBuffer, pack, unpack } from '../lfspack'; import { SendablePacket } from './base'; import { PacketType } from './enums'; /** * Mods ALlowed - variable size */ export class IS_MAL extends SendablePacket { constructor(data) { super(); /** 8 + NumM * 4 */ this.Size = 8; this.Type = PacketType.ISP_MAL; /** 0 unless this is a reply to a {@link TINY_MAL} request */ this.ReqI = 0; /** Number of mods in this packet */ this.NumM = 0; /** Unique id of the connection that updated the list */ this.UCID = 0; /** Zero (for now) */ this.Flags = 0; this.Sp2 = 0; this.Sp3 = 0; /** SkinID of each mod in compressed format, 0 to {@link MAX_MODS} (NumM) */ this.SkinID = []; this.skinIdOffset = 8; this.skinIdSize = 4; this.initialize(data); } unpack(buffer) { super.unpack(buffer); this.SkinID = []; for (let i = 0; i < this.NumM; i++) { const start = this.skinIdOffset + this.skinIdSize * i; const skinIdBuffer = copyBuffer(buffer.slice(start, start + this.skinIdSize)); const data = unpack('C', skinIdBuffer.buffer); if (data) { this.SkinID.push(data[0]); } } return this; } pack() { if (this.SkinID.length > IS_MAL.MAX_MODS) { throw new RangeError(`IS_MAL - Too many SkinIDs set (max is ${IS_MAL.MAX_MODS}`); } this.NumM = this.SkinID.length; this.Size = this.skinIdOffset + this.SkinID.length * this.skinIdSize; const dataBuffer = super.pack(); const objectInfoBufferOrNull = this.SkinID.map((skinId) => pack('C', [skinId])); if (objectInfoBufferOrNull.some((buffer) => buffer === null)) { throw new InSimError('IS_MAL - Could not pack all SkinIDs'); } const objectInfoBuffer = objectInfoBufferOrNull.reduce((acc, objectInfo) => new Uint8Array([...acc, ...objectInfo]), new Uint8Array()); return new Uint8Array([...dataBuffer, ...objectInfoBuffer]); } } IS_MAL.MAX_MODS = 120; __decorate([ byte() ], IS_MAL.prototype, "Size", void 0); __decorate([ byte() ], IS_MAL.prototype, "Type", void 0); __decorate([ byte() ], IS_MAL.prototype, "ReqI", void 0); __decorate([ byte() ], IS_MAL.prototype, "NumM", void 0); __decorate([ byte() ], IS_MAL.prototype, "UCID", void 0); __decorate([ byte() ], IS_MAL.prototype, "Flags", void 0); __decorate([ byte() ], IS_MAL.prototype, "Sp2", void 0); __decorate([ byte() ], IS_MAL.prototype, "Sp3", void 0);