slight change to encode func

This commit is contained in:
TheLostTree 2022-07-28 18:41:51 -07:00
parent c422bf2fe9
commit 400bb016b8

View File

@ -4,13 +4,13 @@ import { resolve } from 'path';
import _packetIds from '../../data/packetIds.json'; import _packetIds from '../../data/packetIds.json';
export type PacketTypes = keyof typeof _packetIds; export type PacketType = keyof typeof _packetIds;
const switchedPacketIds = _packetIds as { [key in PacketTypes]: string }; const switchedPacketIds = _packetIds as { [key in PacketType]: string };
const packetIds: { [key: string]: PacketTypes } = (function () { const packetIds: { [key: string]: PacketType } = (function () {
const obj: { [key: string]: PacketTypes } = {}; const obj: { [key: string]: PacketType } = {};
Object.keys(switchedPacketIds).forEach((key) => { Object.keys(switchedPacketIds).forEach((key) => {
obj[switchedPacketIds[key as PacketTypes]] = key as PacketTypes; obj[switchedPacketIds[key as PacketType]] = key as PacketType;
}); });
return obj; return obj;
@ -54,9 +54,9 @@ export default class Packet {
return str.startsWith("01234567") && str.endsWith("89abcdef"); return str.startsWith("01234567") && str.endsWith("89abcdef");
} }
public static encode(name: string, body: {}, customCmdId?: number): Packet | null { public static encode(name: PacketType, body: {}, customCmdId?: number): Packet | null {
try { try {
const cmdid = switchedPacketIds[name]; const cmdid = Number(switchedPacketIds[name]);
const root = protobuf.loadSync(resolve(__dirname, `../../data/proto/${name}.proto`)); const root = protobuf.loadSync(resolve(__dirname, `../../data/proto/${name}.proto`));
const Message = root.lookupTypeOrEnum(name); const Message = root.lookupTypeOrEnum(name);