From c422bf2fe99129e5fe972bf1dd3daed4e52b0fce Mon Sep 17 00:00:00 2001 From: TheLostTree <65834918+TheLostTree@users.noreply.github.com> Date: Thu, 28 Jul 2022 18:40:59 -0700 Subject: [PATCH] better typed packetIds --- src/data/packetIds.json | 8 ++++---- src/server/kcp/Packet.ts | 15 ++++++++++----- 2 files changed, 14 insertions(+), 9 deletions(-) diff --git a/src/data/packetIds.json b/src/data/packetIds.json index 4ae5e47..1bca59c 100644 --- a/src/data/packetIds.json +++ b/src/data/packetIds.json @@ -1,6 +1,6 @@ { - "101": "DebugNotify", - "5": "PlayerGetTokenCsReq", - "48": "PlayerGetTokenScRsp", - "22": "PlayerKeepAliveNotify" + "DebugNotify": "101", + "PlayerGetTokenCsReq": "5", + "PlayerGetTokenScRsp": "48", + "PlayerKeepAliveNotify": "22" } \ No newline at end of file diff --git a/src/server/kcp/Packet.ts b/src/server/kcp/Packet.ts index 503fe37..8534d36 100644 --- a/src/server/kcp/Packet.ts +++ b/src/server/kcp/Packet.ts @@ -2,16 +2,21 @@ import Logger, { VerboseLevel } from "../../util/Logger"; import protobuf from 'protobufjs'; import { resolve } from 'path'; import _packetIds from '../../data/packetIds.json'; -const packetIds = _packetIds as { [key: string]: string }; -const switchedPacketIds: { [key: string]: number } = (function () { - const obj: { [key: string]: number } = {}; - Object.keys(packetIds).forEach((key) => { - obj[packetIds[key]] = Number(key); + +export type PacketTypes = keyof typeof _packetIds; +const switchedPacketIds = _packetIds as { [key in PacketTypes]: string }; +const packetIds: { [key: string]: PacketTypes } = (function () { + const obj: { [key: string]: PacketTypes } = {}; + + Object.keys(switchedPacketIds).forEach((key) => { + obj[switchedPacketIds[key as PacketTypes]] = key as PacketTypes; }); return obj; })(); + + const c = new Logger("Packet") export default class Packet {