UNPKG

node-insim

Version:

An InSim library for NodeJS with TypeScript support

35 lines (34 loc) 1.34 kB
import { InSimError } from '../../errors'; import { pack } from '../../lfspack'; import { Packet } from './Packet'; import { SendableStruct } from './SendableStruct'; export class SendablePacket extends Packet { /** @ignore */ pack(propertyFormatOverrides) { const propertyNames = this.getValidPropertyNames(); const format = `<${this.getFormat(propertyFormatOverrides)}`; const values = []; propertyNames.forEach((propertyName) => { const propertyValue = this[propertyName]; if (propertyName === 'Size') { values.push(propertyValue / this.SIZE_MULTIPLIER); return; } // Spread all values of structs in packet properties if (propertyValue instanceof SendableStruct) { const struct = propertyValue; const structValues = struct .getValidPropertyNames() .map((structPropName) => struct[structPropName]); values.push(...structValues); return; } return values.push(propertyValue); }); const packedData = pack(format, values); if (!packedData) { throw new InSimError('Could not pack values into a packet'); } return packedData; } }