Skip to content
This repository has been archived by the owner on Nov 3, 2024. It is now read-only.

Commit

Permalink
fix: deserialize msgs inside a try catch
Browse files Browse the repository at this point in the history
  • Loading branch information
leia-uwu committed Oct 1, 2024
1 parent 42c4553 commit 16367b7
Show file tree
Hide file tree
Showing 3 changed files with 76 additions and 20 deletions.
82 changes: 68 additions & 14 deletions server/src/game/game.ts
Original file line number Diff line number Diff line change
@@ -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";
Expand Down Expand Up @@ -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;
}

Expand All @@ -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;

Expand All @@ -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;
}
Expand Down
3 changes: 2 additions & 1 deletion server/src/game/gameModeManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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"),
Expand Down
11 changes: 6 additions & 5 deletions server/src/game/objects/player.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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();
Expand Down Expand Up @@ -1982,15 +1983,15 @@ 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 {
// team dead
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 {
Expand Down

0 comments on commit 16367b7

Please sign in to comment.