Skip to content

Commit

Permalink
Merge pull request #804 from openmultiplayer/amir/weap-validity-check
Browse files Browse the repository at this point in the history
Weapon validity check in foot, passenger, and vehicle sync, and Give/TakeDamage events
  • Loading branch information
AmyrAhmady authored Dec 26, 2023
2 parents 267c1ac + 5624dd5 commit 9483d4b
Show file tree
Hide file tree
Showing 3 changed files with 57 additions and 3 deletions.
14 changes: 14 additions & 0 deletions SDK/include/utils.hpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
#pragma once
#include "player.hpp"

#define STRINGIFY(s) _STRINGIFY(s)
#define _STRINGIFY(s) #s
Expand All @@ -14,3 +15,16 @@ inline StringView trim(StringView view)
const size_t end = view.find_last_not_of(whitespace);
return view.substr(start, end - start + 1);
}

inline bool IsWeaponForTakenDamageValid(int weapon)
{
auto slot = WeaponSlotData(weapon).slot();
if (slot == INVALID_WEAPON_SLOT)
{
if (weapon < 49 || weapon > 54)
{
return false;
}
}
return true;
}
6 changes: 6 additions & 0 deletions Server/Components/Actors/actors_main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@

#include "actor.hpp"
#include <Server/Components/Fixes/fixes.hpp>
#include <utils.hpp>

class ActorsComponent final : public IActorsComponent, public PlayerConnectEventHandler, public PlayerUpdateEventHandler, public PoolEventHandler<IPlayer>
{
Expand Down Expand Up @@ -41,6 +42,11 @@ class ActorsComponent final : public IActorsComponent, public PlayerConnectEvent
return false;
}

if (!IsWeaponForTakenDamageValid(onPlayerDamageActorRPC.WeaponID))
{
return false;
}

if (onPlayerDamageActorRPC.Bodypart < BodyPart_Torso || onPlayerDamageActorRPC.Bodypart > BodyPart_Head)
{
return false;
Expand Down
40 changes: 37 additions & 3 deletions Server/Source/player_pool.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@

#include "player_impl.hpp"
#include <Server/Components/Console/console.hpp>
#include <utils.hpp>

struct PlayerPool final : public IPlayerPool, public NetworkEventHandler, public PlayerUpdateEventHandler, public CoreEventHandler
{
Expand Down Expand Up @@ -213,7 +214,13 @@ struct PlayerPool final : public IPlayerPool, public NetworkEventHandler, public
{
return false;
}
if (!from->areWeaponsAllowed() && 0 < onPlayerGiveTakeDamageRPC.WeaponID && onPlayerGiveTakeDamageRPC.WeaponID <= 47)

if (!IsWeaponForTakenDamageValid(onPlayerGiveTakeDamageRPC.WeaponID))
{
return false;
}

if (!from->areWeaponsAllowed() && 0 < onPlayerGiveTakeDamageRPC.WeaponID && onPlayerGiveTakeDamageRPC.WeaponID <= 54)
{
// They were shooting and shouldn't be.
return false;
Expand Down Expand Up @@ -247,7 +254,14 @@ struct PlayerPool final : public IPlayerPool, public NetworkEventHandler, public
{
return false;
}
if (!peer.areWeaponsAllowed() && 0 < onPlayerGiveTakeDamageRPC.WeaponID && onPlayerGiveTakeDamageRPC.WeaponID <= 47)

auto slot = WeaponSlotData(onPlayerGiveTakeDamageRPC.WeaponID).slot();
if (slot == INVALID_WEAPON_SLOT)
{
return false;
}

if (!peer.areWeaponsAllowed() && (0 < onPlayerGiveTakeDamageRPC.WeaponID && onPlayerGiveTakeDamageRPC.WeaponID <= 46))
{
// They were shooting and shouldn't be.
return false;
Expand Down Expand Up @@ -657,6 +671,12 @@ struct PlayerPool final : public IPlayerPool, public NetworkEventHandler, public

Player& player = static_cast<Player&>(peer);

uint8_t slot = WeaponSlotData(footSync.Weapon).slot();
if (slot == INVALID_WEAPON_SLOT)
{
return false;
}

footSync.PlayerID = player.poolID;
footSync.Rotation *= player.rotTransform_;

Expand Down Expand Up @@ -1091,6 +1111,13 @@ struct PlayerPool final : public IPlayerPool, public NetworkEventHandler, public
{
return false;
}

uint8_t slot = WeaponSlotData(vehicleSync.WeaponID).slot();
if (slot == INVALID_WEAPON_SLOT)
{
return false;
}

IVehicle& vehicle = *vehiclePtr;
Player& player = static_cast<Player&>(peer);
player.pos_ = vehicleSync.Position;
Expand Down Expand Up @@ -1335,9 +1362,16 @@ struct PlayerPool final : public IPlayerPool, public NetworkEventHandler, public
{
return false;
}
IVehicle& vehicle = *vehiclePtr;

uint8_t slot = WeaponSlotData(passengerSync.WeaponID).slot();
if (slot == INVALID_WEAPON_SLOT)
{
return false;
}

IVehicle& vehicle = *vehiclePtr;
Player& player = static_cast<Player&>(peer);

if (vehicle.isRespawning())
return false;

Expand Down

0 comments on commit 9483d4b

Please sign in to comment.