From 16367b791fe1a3eef0f54bdb44811e285ce74404 Mon Sep 17 00:00:00 2001 From: leia uwu Date: Tue, 1 Oct 2024 19:59:49 -0300 Subject: [PATCH] fix: deserialize msgs inside a try catch --- server/src/game/game.ts | 82 +++++++++++++++++++++++++----- server/src/game/gameModeManager.ts | 3 +- server/src/game/objects/player.ts | 11 ++-- 3 files changed, 76 insertions(+), 20 deletions(-) diff --git a/server/src/game/game.ts b/server/src/game/game.ts index eb8afc5f..874f7c30 100644 --- a/server/src/game/game.ts +++ b/server/src/game/game.ts @@ -1,5 +1,7 @@ +import { GameObjectDefs } from "../../../shared/defs/gameObjectDefs"; import { GameConfig, TeamMode } from "../../../shared/gameConfig"; import * as net from "../../../shared/net/net"; +import { assert } from "../../../shared/utils/util"; import { Config } from "../config"; import { Logger } from "../utils/logger"; import type { ServerGameConfig } from "./gameManager"; @@ -196,17 +198,74 @@ export class Game { ); } - handleMsg(buff: ArrayBuffer | Buffer, socketId: string): void { + deserializeMsg(buff: ArrayBuffer): { + type: net.MsgType; + msg: net.AbstractMsg | undefined; + } { const msgStream = new net.MsgStream(buff); - const type = msgStream.deserializeMsgType(); const stream = msgStream.stream; + const type = msgStream.deserializeMsgType(); + + let msg: + | net.JoinMsg + | net.InputMsg + | net.EmoteMsg + | net.DropItemMsg + | net.PerkModeRoleSelectMsg + | undefined = undefined; + + switch (type) { + case net.MsgType.Join: { + msg = new net.JoinMsg(); + msg.deserialize(stream); + break; + } + case net.MsgType.Input: { + msg = new net.InputMsg(); + msg.deserialize(stream); + break; + } + case net.MsgType.Emote: + msg = new net.EmoteMsg(); + msg.deserialize(stream); + break; + case net.MsgType.DropItem: + msg = new net.DropItemMsg(); + msg.deserialize(stream); + break; + case net.MsgType.PerkModeRoleSelect: + msg = new net.PerkModeRoleSelectMsg(); + msg.deserialize(stream); + break; + } + + return { + type, + msg, + }; + } + + handleMsg(buff: ArrayBuffer | Buffer, socketId: string): void { + if (!(buff instanceof ArrayBuffer)) return; + const player = this.playerBarn.socketIdToPlayer.get(socketId); + let msg: net.AbstractMsg | undefined = undefined; + let type = net.MsgType.None; + + try { + const deserialized = this.deserializeMsg(buff); + msg = deserialized.msg; + type = deserialized.type; + } catch (err) { + this.logger.warn("Failed to deserialize msg: "); + console.error(err); + return; + } + if (type === net.MsgType.Join && !player) { - const joinMsg = new net.JoinMsg(); - joinMsg.deserialize(stream); - this.playerBarn.addPlayer(socketId, joinMsg); + this.playerBarn.addPlayer(socketId, msg as net.JoinMsg); return; } @@ -217,14 +276,11 @@ export class Game { switch (type) { case net.MsgType.Input: { - const inputMsg = new net.InputMsg(); - inputMsg.deserialize(stream); - player.handleInput(inputMsg); + player.handleInput(msg as net.InputMsg); break; } case net.MsgType.Emote: { - const emoteMsg = new net.EmoteMsg(); - emoteMsg.deserialize(stream); + const emoteMsg = msg as net.EmoteMsg; if (player.dead) break; @@ -234,14 +290,12 @@ export class Game { break; } case net.MsgType.DropItem: { - const dropMsg = new net.DropItemMsg(); - dropMsg.deserialize(stream); + const dropMsg = msg as net.DropItemMsg; player.dropItem(dropMsg); break; } case net.MsgType.Spectate: { - const spectateMsg = new net.SpectateMsg(); - spectateMsg.deserialize(stream); + const spectateMsg = msg as net.SpectateMsg; player.spectate(spectateMsg); break; } diff --git a/server/src/game/gameModeManager.ts b/server/src/game/gameModeManager.ts index f487f952..3cf00a79 100644 --- a/server/src/game/gameModeManager.ts +++ b/server/src/game/gameModeManager.ts @@ -287,7 +287,8 @@ export class GameModeManager { return; } - const allDeadOrDisconnected = group.checkAllDead(player); + const allDeadOrDisconnected = + group.checkAllDeadOrDisconnected(player); const allDowned = group.checkAllDowned(player); const groupHasSelfRevive = group.livingPlayers.find((p) => p.hasPerk("self_revive"), diff --git a/server/src/game/objects/player.ts b/server/src/game/objects/player.ts index 10ae404a..bae5c619 100644 --- a/server/src/game/objects/player.ts +++ b/server/src/game/objects/player.ts @@ -168,7 +168,7 @@ export class PlayerBarn { this.players.push(player); this.livingPlayers.push(player); if (!this.game.modeManager.isSolo) { - this.livingPlayers = this.livingPlayers.sort((a, b) => a.teamId - b.teamId); + this.livingPlayers.sort((a, b) => a.teamId - b.teamId); } this.aliveCountDirty = true; this.game.pluginManager.emit("playerJoin", player); @@ -230,8 +230,9 @@ export class PlayerBarn { this.groupsByHash.delete(player.group.hash); } } - if (this.game.isTeamMode) - this.livingPlayers = this.livingPlayers.sort((a, b) => a.teamId - b.teamId); + if (this.game.isTeamMode) { + this.livingPlayers.sort((a, b) => a.teamId - b.teamId); + } this.game.checkGameOver(); this.game.updateData(); @@ -1982,7 +1983,7 @@ export class Player extends BaseGameObject { ? this.killedBy : this.game.playerBarn.randomPlayer(); } else if (this.group) { - if (!this.group.checkAllDead(this)) { + if (!this.group.checkAllDeadOrDisconnected(this)) { // team alive player = this.group.randomPlayer(this); } else { @@ -1990,7 +1991,7 @@ export class Player extends BaseGameObject { if ( this.killedBy && this.killedBy != this && - this.group.checkAllDead(this) // only spectate player's killer if all the players teammates are dead, otherwise spec teammates + this.group.checkAllDeadOrDisconnected(this) // only spectate player's killer if all the players teammates are dead, otherwise spec teammates ) { player = this.killedBy; } else {