UNPKG

node-insim

Version:

An InSim library for NodeJS with TypeScript support

89 lines (88 loc) 2.96 kB
import { __decorate } from "tslib"; import { byte, getFormat, stringNull } from '../decorators'; import { InSimError } from '../errors'; import { unpack } from '../lfspack'; import { Packet } from './base'; import { PacketType } from './enums'; /** * MSg Out - system messages and user messages - variable size * * NOTE: Typing "/o MESSAGE" into LFS will send an IS_MSO with {@link UserType} = {@link MSO_O} */ export class IS_MSO extends Packet { constructor() { super(...arguments); this.Size = IS_MSO.FIXED_DATA_SIZE; this.Type = PacketType.ISP_MSO; this.ReqI = 0; this.Zero = 0; /** Connection's unique id (0 = host) */ this.UCID = 0; /** Player's unique id (if zero, use UCID) */ this.PLID = 0; /** Set if typed by a user (see {@link UserType}) */ this.UserType = 0; /** First character of the actual text (after player name) */ this.TextStart = 0; /** 4, 8, 12... 128 characters - last byte is zero */ this.Msg = ''; } unpack(buffer) { const data = unpack(`<${getFormat(this, 'Size')}`, buffer.buffer); if (!data || data.length === 0) { throw new InSimError('IS_MSO - Unpacked no data from buffer'); } const size = data[0] * this.SIZE_MULTIPLIER; const msgLength = size - IS_MSO.FIXED_DATA_SIZE; super.unpack(buffer, { Msg: `${msgLength}s`, }); const playerNameUnpacked = unpack(`<${this.TextStart}s`, buffer.buffer, IS_MSO.FIXED_DATA_SIZE); if (playerNameUnpacked !== null && Array.isArray(playerNameUnpacked[0]) && playerNameUnpacked[0].length === 2) { const [, playerName] = playerNameUnpacked[0]; this.TextStart = playerName.length; } return this; } } IS_MSO.FIXED_DATA_SIZE = 8; __decorate([ byte() ], IS_MSO.prototype, "Size", void 0); __decorate([ byte() ], IS_MSO.prototype, "Type", void 0); __decorate([ byte() ], IS_MSO.prototype, "ReqI", void 0); __decorate([ byte() ], IS_MSO.prototype, "Zero", void 0); __decorate([ byte() ], IS_MSO.prototype, "UCID", void 0); __decorate([ byte() ], IS_MSO.prototype, "PLID", void 0); __decorate([ byte() ], IS_MSO.prototype, "UserType", void 0); __decorate([ byte() ], IS_MSO.prototype, "TextStart", void 0); __decorate([ stringNull(128) ], IS_MSO.prototype, "Msg", void 0); export var UserType; (function (UserType) { /** System message */ UserType[UserType["MSO_SYSTEM"] = 0] = "MSO_SYSTEM"; /** Normal visible user message */ UserType[UserType["MSO_USER"] = 1] = "MSO_USER"; /** hidden message starting with special prefix (see {@link IS_ISI}) */ UserType[UserType["MSO_PREFIX"] = 2] = "MSO_PREFIX"; /** Hidden message typed on local pc with /o command */ UserType[UserType["MSO_O"] = 3] = "MSO_O"; })(UserType || (UserType = {}));