Skip to content

Commit

Permalink
fix(insim): Derive InSimPacketEvents type from packetTypeToClass
Browse files Browse the repository at this point in the history
…object

The `InSimPacketEvents` mapping of packet types to event handlers is now derived from the `packetTypeToClass` mapping, which is the only source of truth for every InSim packet now.

As a side-effect of this, the `IS_NONE` packet class definition was moved into its own file and is exported from `node-insim/packets`.
  • Loading branch information
mkapal committed May 7, 2024
1 parent fff7eb9 commit 66f8350
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 131 deletions.
14 changes: 5 additions & 9 deletions src/InSim.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import defaults from 'lodash/defaults';
import { TypedEmitter } from 'tiny-typed-emitter';

import { InSimError } from './errors';
import type { InSimEvents, InSimPacketEvents } from './InSimEvents';
import type { InSimEvents } from './InSimEvents';
import { unpack } from './lfspack';
import { log as baseLog } from './log';
import type { IS_ISI_Data, SendablePacket } from './packets';
Expand Down Expand Up @@ -215,16 +215,16 @@ export class InSim extends TypedEmitter<InSimEvents> {
return;
}

const packetType = header[1];
const packetType = header[1] as PacketType;

const packetTypeString = PacketType[packetType as PacketType];
const packetTypeString = PacketType[packetType];

if (packetTypeString === undefined) {
log(`Unknown packet type received: ${packetType}`);
return;
}

const PacketClass = packetTypeToClass[packetType as PacketType];
const PacketClass = packetTypeToClass[packetType];

if (PacketClass === undefined) {
log(`Packet handler not found for ${packetTypeString}`);
Expand All @@ -234,11 +234,7 @@ export class InSim extends TypedEmitter<InSimEvents> {
const packetInstance = new PacketClass();
packetInstance.SIZE_MULTIPLIER = this.sizeMultiplier;

this.emit(
packetType as keyof InSimPacketEvents,
packetInstance.unpack(data) as never,
this,
);
this.emit(packetType, packetInstance.unpack(data) as never, this);
}

private handleKeepAlive(packet: IS_TINY) {
Expand Down
120 changes: 5 additions & 115 deletions src/InSimEvents.ts
Original file line number Diff line number Diff line change
@@ -1,121 +1,11 @@
import type { InSim } from './InSim';
import type {
IR_ARP,
IR_ARQ,
IR_ERR,
IR_HOS,
IS_ACR,
IS_AXI,
IS_AXM,
IS_AXO,
IS_BFN,
IS_BTC,
IS_BTT,
IS_CCH,
IS_CIM,
IS_CNL,
IS_CON,
IS_CPP,
IS_CPR,
IS_CRS,
IS_CSC,
IS_FIN,
IS_FLG,
IS_HLV,
IS_III,
IS_ISM,
IS_JRR,
IS_LAP,
IS_MAL,
IS_MCI,
IS_MSO,
IS_NCI,
IS_NCN,
IS_NLP,
IS_NPL,
IS_OBH,
IS_PEN,
IS_PFL,
IS_PIT,
IS_PLA,
IS_PLH,
IS_PLL,
IS_PLP,
IS_PSF,
IS_REO,
IS_RES,
IS_RIP,
IS_RST,
IS_SLC,
IS_SMALL,
IS_SPX,
IS_SSH,
IS_STA,
IS_TINY,
IS_TOC,
IS_UCO,
IS_VER,
IS_VTN,
PacketType,
} from './packets';
import type { packetTypeToClass } from './packets';

export type InSimPacketEvents = {
[PacketType.ISP_VER]: (packet: IS_VER, inSim: InSim) => void;
[PacketType.ISP_TINY]: (packet: IS_TINY, inSim: InSim) => void;
[PacketType.ISP_SMALL]: (packet: IS_SMALL, inSim: InSim) => void;
[PacketType.ISP_STA]: (packet: IS_STA, inSim: InSim) => void;
[PacketType.ISP_CPP]: (packet: IS_CPP, inSim: InSim) => void;
[PacketType.ISP_ISM]: (packet: IS_ISM, inSim: InSim) => void;
[PacketType.ISP_MSO]: (packet: IS_MSO, inSim: InSim) => void;
[PacketType.ISP_III]: (packet: IS_III, inSim: InSim) => void;
[PacketType.ISP_VTN]: (packet: IS_VTN, inSim: InSim) => void;
[PacketType.ISP_RST]: (packet: IS_RST, inSim: InSim) => void;
[PacketType.ISP_NCN]: (packet: IS_NCN, inSim: InSim) => void;
[PacketType.ISP_CNL]: (packet: IS_CNL, inSim: InSim) => void;
[PacketType.ISP_CPR]: (packet: IS_CPR, inSim: InSim) => void;
[PacketType.ISP_NPL]: (packet: IS_NPL, inSim: InSim) => void;
[PacketType.ISP_PLP]: (packet: IS_PLP, inSim: InSim) => void;
[PacketType.ISP_PLL]: (packet: IS_PLL, inSim: InSim) => void;
[PacketType.ISP_LAP]: (packet: IS_LAP, inSim: InSim) => void;
[PacketType.ISP_SPX]: (packet: IS_SPX, inSim: InSim) => void;
[PacketType.ISP_PIT]: (packet: IS_PIT, inSim: InSim) => void;
[PacketType.ISP_PSF]: (packet: IS_PSF, inSim: InSim) => void;
[PacketType.ISP_PLA]: (packet: IS_PLA, inSim: InSim) => void;
[PacketType.ISP_CCH]: (packet: IS_CCH, inSim: InSim) => void;
[PacketType.ISP_PEN]: (packet: IS_PEN, inSim: InSim) => void;
[PacketType.ISP_TOC]: (packet: IS_TOC, inSim: InSim) => void;
[PacketType.ISP_FLG]: (packet: IS_FLG, inSim: InSim) => void;
[PacketType.ISP_PFL]: (packet: IS_PFL, inSim: InSim) => void;
[PacketType.ISP_FIN]: (packet: IS_FIN, inSim: InSim) => void;
[PacketType.ISP_RES]: (packet: IS_RES, inSim: InSim) => void;
[PacketType.ISP_REO]: (packet: IS_REO, inSim: InSim) => void;
[PacketType.ISP_NLP]: (packet: IS_NLP, inSim: InSim) => void;
[PacketType.ISP_MCI]: (packet: IS_MCI, inSim: InSim) => void;
[PacketType.ISP_CRS]: (packet: IS_CRS, inSim: InSim) => void;
[PacketType.ISP_BFN]: (packet: IS_BFN, inSim: InSim) => void;
[PacketType.ISP_AXI]: (packet: IS_AXI, inSim: InSim) => void;
[PacketType.ISP_AXO]: (packet: IS_AXO, inSim: InSim) => void;
[PacketType.ISP_BTC]: (packet: IS_BTC, inSim: InSim) => void;
[PacketType.ISP_BTT]: (packet: IS_BTT, inSim: InSim) => void;
[PacketType.ISP_RIP]: (packet: IS_RIP, inSim: InSim) => void;
[PacketType.ISP_SSH]: (packet: IS_SSH, inSim: InSim) => void;
[PacketType.ISP_CON]: (packet: IS_CON, inSim: InSim) => void;
[PacketType.ISP_OBH]: (packet: IS_OBH, inSim: InSim) => void;
[PacketType.ISP_HLV]: (packet: IS_HLV, inSim: InSim) => void;
[PacketType.ISP_AXM]: (packet: IS_AXM, inSim: InSim) => void;
[PacketType.ISP_ACR]: (packet: IS_ACR, inSim: InSim) => void;
[PacketType.ISP_NCI]: (packet: IS_NCI, inSim: InSim) => void;
[PacketType.ISP_JRR]: (packet: IS_JRR, inSim: InSim) => void;
[PacketType.ISP_UCO]: (packet: IS_UCO, inSim: InSim) => void;
[PacketType.ISP_SLC]: (packet: IS_SLC, inSim: InSim) => void;
[PacketType.ISP_CSC]: (packet: IS_CSC, inSim: InSim) => void;
[PacketType.ISP_CIM]: (packet: IS_CIM, inSim: InSim) => void;
[PacketType.ISP_MAL]: (packet: IS_MAL, inSim: InSim) => void;
[PacketType.ISP_PLH]: (packet: IS_PLH, inSim: InSim) => void;
[PacketType.IRP_ARQ]: (packet: IR_ARQ, inSim: InSim) => void;
[PacketType.IRP_ARP]: (packet: IR_ARP, inSim: InSim) => void;
[PacketType.IRP_HOS]: (packet: IR_HOS, inSim: InSim) => void;
[PacketType.IRP_ERR]: (packet: IR_ERR, inSim: InSim) => void;
[k in keyof typeof packetTypeToClass]: (
packet: (typeof packetTypeToClass)[k]['prototype'],
inSim: InSim,
) => void;
};

export type InSimEvents = InSimPacketEvents & {
Expand Down
8 changes: 8 additions & 0 deletions src/packets/IS_NONE.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import { Packet } from './base';
import { PacketType } from './enums';

export class IS_NONE extends Packet {
readonly Size = 4;
readonly Type = PacketType.ISP_NONE;
readonly ReqI = 0;
}
11 changes: 4 additions & 7 deletions src/packets/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import { Packet } from './base';
import { PacketType } from './enums';
import { IR_ARP } from './IR_ARP';
import { IR_ARQ } from './IR_ARQ';
Expand Down Expand Up @@ -42,6 +41,7 @@ import { IS_MTC } from './IS_MTC';
import { IS_NCI } from './IS_NCI';
import { IS_NCN } from './IS_NCN';
import { IS_NLP } from './IS_NLP';
import { IS_NONE } from './IS_NONE';
import { IS_NPL } from './IS_NPL';
import { IS_OBH } from './IS_OBH';
import { IS_OCO } from './IS_OCO';
Expand Down Expand Up @@ -73,12 +73,8 @@ import { IS_UCO } from './IS_UCO';
import { IS_VER } from './IS_VER';
import { IS_VTN } from './IS_VTN';

export const packetTypeToClass: Record<PacketType, new () => Packet> = {
[PacketType.ISP_NONE]: class A extends Packet {
ReqI = 0;
Size = 0;
Type = 0;
},
export const packetTypeToClass = {
[PacketType.ISP_NONE]: IS_NONE,
[PacketType.ISP_ISI]: IS_ISI,
[PacketType.ISP_VER]: IS_VER,
[PacketType.ISP_TINY]: IS_TINY,
Expand Down Expand Up @@ -278,6 +274,7 @@ export { IS_MTC } from './IS_MTC';
export { IS_NCI } from './IS_NCI';
export { IS_NCN } from './IS_NCN';
export { IS_NLP } from './IS_NLP';
export { IS_NONE } from './IS_NONE';
export { IS_NPL } from './IS_NPL';
export { IS_OBH } from './IS_OBH';
export type { IS_OCO_Data } from './IS_OCO';
Expand Down

0 comments on commit 66f8350

Please sign in to comment.