Skip to content

Commit

Permalink
Fix crash by incorrectly deleting recipfilter + let IsNnpc and IsPlay…
Browse files Browse the repository at this point in the history
…er return false on null safely
  • Loading branch information
luttje committed Aug 24, 2024
1 parent 622d472 commit 2893797
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 45 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -469,6 +469,7 @@ ENTITY_META.OBBMins = ENTITY_META.GetOBBMins
ENTITY_META.LocalToWorld = ENTITY_META.EntityToWorldSpace
ENTITY_META.SkinCount = ENTITY_META.GetSkinCount
ENTITY_META.Alive = ENTITY_META.IsAlive
ENTITY_META.IsNPC = ENTITY_META.IsNpc
ENTITY_META.Widget = false
ENTITY_META.AddFlags = ENTITY_META.AddFlag
ENTITY_META.RemoveFlags = ENTITY_META.RemoveFlag
Expand Down Expand Up @@ -636,7 +637,7 @@ WEAPON_META.Ammo1 = WEAPON_META.GetPrimaryAmmoCount
WEAPON_META.Ammo2 = WEAPON_META.GetSecondaryAmmoCount

local PLAYER_META = FindMetaTable("Player")
PLAYER_META.GetShootPos = ENTITY_META.GetWeaponShootPosition
PLAYER_META.GetShootPos = PLAYER_META.GetWeaponShootPosition
PLAYER_META.UserID = PLAYER_META.GetUserId
PLAYER_META.AccountID = PLAYER_META.GetAccountId
PLAYER_META.SteamID = PLAYER_META.GetSteamId
Expand Down
39 changes: 27 additions & 12 deletions src/game/shared/lbaseentity_shared.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -387,13 +387,13 @@ LUA_BINDING_BEGIN( Entity, EmitSound, "class", "Emit sound." )
int nSoundFlags = LUA_BINDING_ARGUMENT_WITH_DEFAULT( luaL_optnumber, 7, 0, "soundFlags" );
int nDSP = LUA_BINDING_ARGUMENT_WITH_DEFAULT( luaL_optnumber, 8, 0, "dsp" );

CRecipientFilter *filter = nullptr;
CRecipientFilter filter;

if ( lua_isrecipientfilter( L, 9 ) )
filter = &LUA_BINDING_ARGUMENT_NILLABLE( luaL_checkrecipientfilter, 9, "filter" );
filter = LUA_BINDING_ARGUMENT_NILLABLE( luaL_checkrecipientfilter, 9, "filter" );
else
{
filter = new CPASAttenuationFilter( pEntity, soundLevel );
filter = CPASAttenuationFilter( pEntity, soundLevel );
}

float duration = 0;
Expand All @@ -411,14 +411,12 @@ LUA_BINDING_BEGIN( Entity, EmitSound, "class", "Emit sound." )
params.m_flSoundTime = 0;
params.m_nSpeakerEntity = iEntIndex;
params.m_pflSoundDuration = &duration;
params.m_bWarnOnDirectWaveReference = true;
params.m_bWarnOnDirectWaveReference = false;

pEntity->EmitSound( *filter, iEntIndex, params );
pEntity->EmitSound( filter, iEntIndex, params );

lua_pushnumber( L, duration );

delete[] filter;

return 1;
}
LUA_BINDING_END( "number", "The sound duration." )
Expand Down Expand Up @@ -1029,7 +1027,7 @@ LUA_BINDING_BEGIN( Entity, GetRefTable, "class", "Get reference table." )
// have a nil reference table returned.
lua_CBaseEntity *pEntity = LUA_BINDING_ARGUMENT( lua_toentity, 1, "entity" );

if (pEntity == NULL)
if ( pEntity == NULL )
{
lua_pushnil( L );
}
Expand Down Expand Up @@ -1396,9 +1394,15 @@ LUA_BINDING_BEGIN( Entity, IsMarkedForDeletion, "class", "Is marked for deletion
}
LUA_BINDING_END( "boolean", "True if marked for deletion, false otherwise." )

LUA_BINDING_BEGIN( Entity, IsNPC, "class", "Is NPC." )
LUA_BINDING_BEGIN( Entity, IsNpc, "class", "Is NPC." )
{
lua_CBaseEntity *pEntity = LUA_BINDING_ARGUMENT( luaL_checkentity, 1, "entity" );
lua_CBaseEntity *pEntity = LUA_BINDING_ARGUMENT( lua_toentity, 1, "entity" );

if ( !pEntity )
{
lua_pushboolean( L, false );
return 1;
}

lua_pushboolean( L, pEntity->IsNPC() );
return 1;
Expand All @@ -1407,7 +1411,13 @@ LUA_BINDING_END( "boolean", "True if NPC, false otherwise." )

LUA_BINDING_BEGIN( Entity, IsPlayer, "class", "Is player." )
{
lua_CBaseEntity *pEntity = LUA_BINDING_ARGUMENT( luaL_checkentity, 1, "entity" );
lua_CBaseEntity *pEntity = LUA_BINDING_ARGUMENT( lua_toentity, 1, "entity" );

if ( !pEntity )
{
lua_pushboolean( L, false );
return 1;
}

lua_pushboolean( L, pEntity->IsPlayer() );
return 1;
Expand Down Expand Up @@ -2815,7 +2825,7 @@ LUA_BINDING_BEGIN( Entity, SetNetworkDataValue, "class", "Sets a data table vari
{
int newValueInt = LUA_BINDING_ARGUMENT( luaL_checknumber, 4, "value" );
lua_pushinteger( L, entity->m_LuaVariables_int[slot] );
lua_pushnumber( L, newValueInt );
lua_pushinteger( L, newValueInt );
#ifdef CLIENT_DLL
entity->m_LuaVariables_int[slot] = newValueInt;
#else
Expand All @@ -2828,6 +2838,11 @@ LUA_BINDING_BEGIN( Entity, SetNetworkDataValue, "class", "Sets a data table vari
float newValueFloat = LUA_BINDING_ARGUMENT( luaL_checknumber, 4, "value" );
lua_pushnumber( L, entity->m_LuaVariables_float[slot] );
lua_pushnumber( L, newValueFloat );
if ( !IsFinite( newValueFloat ) )
{
newValueFloat = FLT_MAX;
Assert( IsFinite( newValueFloat ) );
}
#ifdef CLIENT_DLL
entity->m_LuaVariables_float[slot] = newValueFloat;
#else
Expand Down
41 changes: 9 additions & 32 deletions src/game/shared/lbaseplayer_shared.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1549,22 +1549,12 @@ LUA_BINDING_END( "integer", "Team of the local player." )

LUA_BINDING_BEGIN( Players, GetAllBots, "library", "Get all bots." )
{
CUtlVector< CBasePlayer * > curPlayers;
lua_newtable( L );

for ( int i = 1; i <= gpGlobals->maxClients; i++ )
{
CBasePlayer *pPlayer = UTIL_PlayerByIndex( i );
if ( pPlayer && ( pPlayer->GetFlags() & FL_FAKECLIENT ) )
{
curPlayers.AddToTail( pPlayer );
}
}

lua_newtable( L );

for ( int i = 0; i < curPlayers.Count(); i++ )
{
CBaseEntity::PushLuaInstanceSafe( L, curPlayers[i] );
CBaseEntity::PushLuaInstanceSafe( L, pPlayer );
lua_rawseti( L, -2, i + 1 );
}

Expand All @@ -1574,50 +1564,37 @@ LUA_BINDING_END( "table", "All bots." )

LUA_BINDING_BEGIN( Players, GetAllHumans, "library", "Get all humans." )
{
CUtlVector< CBasePlayer * > curPlayers;
lua_newtable( L );

for ( int i = 1; i <= gpGlobals->maxClients; i++ )
{
CBasePlayer *pPlayer = UTIL_PlayerByIndex( i );

if ( pPlayer && !( pPlayer->GetFlags() & FL_FAKECLIENT ) )
{
curPlayers.AddToTail( pPlayer );
CBaseEntity::PushLuaInstanceSafe( L, pPlayer );
lua_rawseti( L, -2, i + 1 );
}
}

lua_newtable( L );

for ( int i = 0; i < curPlayers.Count(); i++ )
{
CBaseEntity::PushLuaInstanceSafe( L, curPlayers[i] );
lua_rawseti( L, -2, i + 1 );
}

return 1;
}
LUA_BINDING_END( "table", "All humans." )

LUA_BINDING_BEGIN( Players, GetAll, "library", "Get all players." )
{
CUtlVector< CBasePlayer * > curPlayers;
lua_newtable( L );

for ( int i = 1; i <= gpGlobals->maxClients; i++ )
{
CBasePlayer *pPlayer = UTIL_PlayerByIndex( i );
if ( pPlayer )
{
curPlayers.AddToTail( pPlayer );
CBaseEntity::PushLuaInstanceSafe( L, pPlayer );
lua_rawseti( L, -2, i + 1 );
}
}

lua_newtable( L );

for ( int i = 0; i < curPlayers.Count(); i++ )
{
CBaseEntity::PushLuaInstanceSafe( L, curPlayers[i] );
lua_rawseti( L, -2, i + 1 );
}

return 1;
}
LUA_BINDING_END( "table", "All players." )
Expand Down

0 comments on commit 2893797

Please sign in to comment.