From c59789180bbf236a1d0e611d82438239df771502 Mon Sep 17 00:00:00 2001 From: TheLostTree <65834918+TheLostTree@users.noreply.github.com> Date: Fri, 5 Aug 2022 01:28:37 -0700 Subject: [PATCH] remove dynamic runtime generated root --- src/server/kcp/Packet.ts | 44 ++++++++-------------------------------- src/util/ProtoFactory.ts | 2 +- 2 files changed, 9 insertions(+), 37 deletions(-) diff --git a/src/server/kcp/Packet.ts b/src/server/kcp/Packet.ts index 45ecb72..1dd5c65 100644 --- a/src/server/kcp/Packet.ts +++ b/src/server/kcp/Packet.ts @@ -1,11 +1,15 @@ import Logger, { VerboseLevel } from "../../util/Logger"; import protobuf, { Root } from 'protobufjs'; import { resolve } from 'path'; +import ProtoFactory from "../../util/ProtoFactory"; const c = new Logger("Packet") + + + + export default class Packet { public readonly cmdid: number; public readonly data: Buffer; - private static root: Root = Packet.getRoot(); public body: {} = {}; public constructor(public readonly rawData: Buffer, public readonly protoName: string = "") { @@ -15,9 +19,10 @@ export default class Packet { this.cmdid = this.rawData.readUInt16BE(4); this.protoName = this.protoName || CmdID[this.cmdid]; - if (this.protoName) { + + if(this.protoName){ try { - const Message = Packet.root.lookupTypeOrEnum(this.protoName); + const Message = ProtoFactory.getType(this.protoName as PacketName); this.body = Message.decode(this.data); } catch (e) { c.warn(`Failed to decode ${this.protoName}`); @@ -37,37 +42,6 @@ export default class Packet { return str.startsWith("01234567") && str.endsWith("89abcdef"); } - public static encode(name: PacketName, body: {}, customCmdId?: number): Packet | null { - try { - const cmdid = CmdID[name]; - const Message = Packet.root.lookupTypeOrEnum(name); - - const data = Buffer.from(Message.encode(body).finish()); - const packet = Buffer.allocUnsafe(16 + data.length); - packet.writeUInt32BE(0x1234567); - packet.writeUint16BE(customCmdId || cmdid, 4); - packet.writeUint16BE(0, 6); - packet.writeUint32BE(data.length, 8); - data.copy(packet, 12); - packet.writeUint32BE(0x89abcdef, 12 + data.length); - - return new Packet(packet, name); - } catch (e) { - c.error(e as Error); - return null; - } - } - - private static getRoot(): Root { - try { - // Combined proto file with all definitions - return protobuf.loadSync(resolve(__dirname, `../../data/proto/StarRail.proto`)); - } catch (e) { - c.error("Failed to load proto root! Server will not be able to function properly. Please check your data/ folder."); - c.error(e as Error, false); - process.exit(1); - } - } public static fromEncodedBuffer(data: Buffer, name: PacketName): Buffer { const cmdid = CmdID[name]; @@ -80,8 +54,6 @@ export default class Packet { packet.writeUint32BE(0x89abcdef, 12 + data.length); return packet; } - - } export type PacketName = keyof typeof CmdID; diff --git a/src/util/ProtoFactory.ts b/src/util/ProtoFactory.ts index 2fbb9b0..5e0e0b0 100644 --- a/src/util/ProtoFactory.ts +++ b/src/util/ProtoFactory.ts @@ -8,7 +8,7 @@ const c = new Logger("ProtoFactory"); export class MessageType { "encode": (arg0: T) => protobufjs.Writer; "fromPartial": (arg0: object) => T; - // "decode": (input: protobufjs.Reader | Uint8Array, length?: number)=> T; + "decode": (input: protobufjs.Reader | Uint8Array, length?: number)=> T; // "fromJSON": (object: any)=>T; // "toJSON": (message: T)=> unknown //you can add more fields here from the generated types