Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Prevent return of dangling Vector/QAngle to VScript #321

Open
wants to merge 11 commits into
base: develop
Choose a base branch
from
7 changes: 6 additions & 1 deletion sp/src/game/server/BaseAnimatingOverlay.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,10 @@
#include "saverestore_utlvector.h"
#include "dt_utlvector_send.h"

#ifdef MAPBASE_VSCRIPT
#include "mapbase/vscript_funcs_shared.h"
#endif

// memdbgon must be the last include file in a .cpp file!!!
#include "tier0/memdbgon.h"

Expand Down Expand Up @@ -471,7 +475,8 @@ void CAnimationLayer::DispatchAnimEvents( CBaseAnimating *eventHandler, CBaseAni
}

#ifdef MAPBASE_VSCRIPT
if (eventHandler->m_ScriptScope.IsInitialized() && eventHandler->ScriptHookHandleAnimEvent( &event ) == false)
scriptanimevent_t wrapper( event );
if (eventHandler->m_ScriptScope.IsInitialized() && !eventHandler->ScriptHookHandleAnimEvent( wrapper ))
continue;
#endif

Expand Down
7 changes: 4 additions & 3 deletions sp/src/game/server/baseanimating.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1261,7 +1261,8 @@ void CBaseAnimating::DispatchAnimEvents ( CBaseAnimating *eventHandler )
}

#ifdef MAPBASE_VSCRIPT
if (eventHandler->ScriptHookHandleAnimEvent( &event ) == false)
scriptanimevent_t wrapper( event );
if (!eventHandler->ScriptHookHandleAnimEvent( wrapper ))
continue;
#endif

Expand Down Expand Up @@ -1299,11 +1300,11 @@ void CBaseAnimating::DispatchAnimEvents ( CBaseAnimating *eventHandler )
//-----------------------------------------------------------------------------
// Purpose:
//-----------------------------------------------------------------------------
bool CBaseAnimating::ScriptHookHandleAnimEvent( animevent_t *pEvent )
bool CBaseAnimating::ScriptHookHandleAnimEvent( scriptanimevent_t &event )
{
if (m_ScriptScope.IsInitialized() && g_Hook_HandleAnimEvent.CanRunInScope(m_ScriptScope))
{
HSCRIPT hEvent = g_pScriptVM->RegisterInstance( reinterpret_cast<scriptanimevent_t*>(pEvent) );
HSCRIPT hEvent = g_pScriptVM->RegisterInstance( &event );

// event
ScriptVariant_t args[] = { hEvent };
Expand Down
5 changes: 4 additions & 1 deletion sp/src/game/server/baseanimating.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,9 @@
#include "datacache/idatacache.h"
#include "tier0/threadtools.h"

#ifdef MAPBASE_VSCRIPT
struct scriptanimevent_t;
#endif

struct animevent_t;
struct matrix3x4_t;
Expand Down Expand Up @@ -146,7 +149,7 @@ class CBaseAnimating : public CBaseEntity
virtual void DispatchAnimEvents ( CBaseAnimating *eventHandler ); // Handle events that have happend since last time called up until X seconds into the future
virtual void HandleAnimEvent( animevent_t *pEvent );
#ifdef MAPBASE_VSCRIPT
bool ScriptHookHandleAnimEvent( animevent_t *pEvent );
bool ScriptHookHandleAnimEvent( scriptanimevent_t &event );
#endif

int LookupPoseParameter( CStudioHdr *pStudioHdr, const char *szName );
Expand Down
15 changes: 9 additions & 6 deletions sp/src/game/server/hl2/proto_sniper.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2645,16 +2645,19 @@ Vector CProtoSniper::DesiredBodyTarget( CBaseEntity *pTarget )
{
// By default, aim for the center
Vector vecTarget = pTarget->WorldSpaceCenter();
const Vector vecBulletOrigin = GetBulletOrigin();

#ifdef MAPBASE_VSCRIPT
if (m_ScriptScope.IsInitialized() && g_Hook_GetActualShootPosition.CanRunInScope(m_ScriptScope))
if ( m_ScriptScope.IsInitialized() && g_Hook_GetActualShootPosition.CanRunInScope( m_ScriptScope ) )
{
ScriptVariant_t functionReturn;
ScriptVariant_t args[] = { GetBulletOrigin(), ToHScript( pTarget ) };
if (g_Hook_GetActualShootPosition.Call( m_ScriptScope, &functionReturn, args ))
ScriptVariant_t args[] = { vecBulletOrigin, ToHScript( pTarget ) };
if ( g_Hook_GetActualShootPosition.Call( m_ScriptScope, &functionReturn, args ) )
{
if (functionReturn.m_type == FIELD_VECTOR && functionReturn.m_pVector->LengthSqr() != 0.0f)
if ( functionReturn.m_type == FIELD_VECTOR && functionReturn.m_pVector->LengthSqr() != 0.0f )
{
return *functionReturn.m_pVector;
}
}
}
#endif
Expand Down Expand Up @@ -2682,12 +2685,12 @@ Vector CProtoSniper::DesiredBodyTarget( CBaseEntity *pTarget )
{
if( flTimeSinceLastMiss > 0.0f && flTimeSinceLastMiss < 4.0f && hl2_episodic.GetBool() )
{
vecTarget = pTarget->BodyTarget( GetBulletOrigin(), false );
vecTarget = pTarget->BodyTarget( vecBulletOrigin, false );
}
else
{
// Shoot zombies in the headcrab
vecTarget = pTarget->HeadTarget( GetBulletOrigin() );
vecTarget = pTarget->HeadTarget( vecBulletOrigin );
}
}
else if( pTarget->Classify() == CLASS_ANTLION )
Expand Down
3 changes: 2 additions & 1 deletion sp/src/game/shared/mapbase/vscript_consts_shared.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -346,7 +346,8 @@ void RegisterSharedScriptConstants()
ScriptRegisterConstant( g_pScriptVM, ROPE_NO_GRAVITY, "Disable gravity on this rope. (for use in rope flags)" );
ScriptRegisterConstant( g_pScriptVM, ROPE_NUMFLAGS, "The number of rope flags recognized by the game." );

ScriptRegisterConstantNamed( g_pScriptVM, Vector( ROPE_GRAVITY ), "ROPE_GRAVITY", "Default rope gravity vector." );
static Vector vecRopeGravity( ROPE_GRAVITY );
ScriptRegisterConstantNamed( g_pScriptVM, vecRopeGravity, "ROPE_GRAVITY", "Default rope gravity vector." );

//
// Sounds
Expand Down
26 changes: 18 additions & 8 deletions sp/src/game/shared/mapbase/vscript_funcs_shared.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -535,7 +535,7 @@ bool CAnimEventTInstanceHelper::Get( void *p, const char *pszKey, ScriptVariant_
{
DevWarning( "VScript animevent_t.%s: animevent_t metamethod members are deprecated! Use 'script_help animevent_t' to see the correct functions.\n", pszKey );

animevent_t *ani = ((animevent_t *)p);
animevent_t *ani = &((scriptanimevent_t *)p)->event;
if (FStrEq( pszKey, "event" ))
variant = ani->event;
else if (FStrEq( pszKey, "options" ))
Expand All @@ -558,18 +558,28 @@ bool CAnimEventTInstanceHelper::Set( void *p, const char *pszKey, ScriptVariant_
{
DevWarning( "VScript animevent_t.%s: animevent_t metamethod members are deprecated! Use 'script_help animevent_t' to see the correct functions.\n", pszKey );

animevent_t *ani = ((animevent_t *)p);
scriptanimevent_t *script_ani = ((scriptanimevent_t *)p);
animevent_t *ani = &script_ani->event;
if (FStrEq( pszKey, "event" ))
ani->event = variant;
{
return variant.AssignTo( &ani->event );
}
else if (FStrEq( pszKey, "options" ))
ani->options = variant;
{
char *szOptions;
if (!variant.AssignTo( &szOptions ))
{
return false;
}
script_ani->SetOptions( szOptions );
}
else if (FStrEq( pszKey, "cycle" ))
ani->cycle = variant;
return variant.AssignTo( &ani->cycle );
else if (FStrEq( pszKey, "eventtime" ))
ani->eventtime = variant;
return variant.AssignTo( &ani->eventtime );
else if (FStrEq( pszKey, "type" ))
ani->type = variant;
else if (FStrEq( pszKey, "source" ))
return variant.AssignTo( &ani->type );
else if (FStrEq( pszKey, "source" ) && variant.m_type == FIELD_HSCRIPT)
{
CBaseEntity *pEnt = ToEnt( variant.m_hScript );
if (pEnt)
Expand Down
43 changes: 30 additions & 13 deletions sp/src/game/shared/mapbase/vscript_funcs_shared.h
Original file line number Diff line number Diff line change
Expand Up @@ -172,30 +172,47 @@ class CScriptGameTrace : public CGameTrace
//-----------------------------------------------------------------------------
// Exposes animevent_t to VScript
//-----------------------------------------------------------------------------
struct scriptanimevent_t : public animevent_t
struct scriptanimevent_t
{
int GetEvent() { return event; }
void SetEvent( int nEvent ) { event = nEvent; }
friend class CAnimEventTInstanceHelper;

const char *GetOptions() { return options; }
void SetOptions( const char *pOptions ) { options = pOptions; }
public:
scriptanimevent_t( animevent_t &event ) : event( event ), options( NULL ) { }
~scriptanimevent_t( ) { delete[] options; }

int GetEvent() { return event.event; }
void SetEvent( int nEvent ) { event.event = nEvent; }

float GetCycle() { return cycle; }
void SetCycle( float flCycle ) { cycle = flCycle; }
const char *GetOptions() { return event.options; }
void SetOptions( const char *pOptions )
{
size_t len = strlen( pOptions );
delete[] options;
z33ky marked this conversation as resolved.
Show resolved Hide resolved
event.options = options = new char[len + 1];
memcpy( options, pOptions, len + 1 );
}

float GetEventTime() { return eventtime; }
void SetEventTime( float flEventTime ) { eventtime = flEventTime; }
float GetCycle() { return event.cycle; }
void SetCycle( float flCycle ) { event.cycle = flCycle; }

int GetType() { return type; }
void SetType( int nType ) { eventtime = type; }
float GetEventTime() { return event.eventtime; }
void SetEventTime( float flEventTime ) { event.eventtime = flEventTime; }

HSCRIPT GetSource() { return ToHScript( pSource ); }
int GetType() { return event.type; }
void SetType( int nType ) { event.type = nType; }

HSCRIPT GetSource() { return ToHScript( event.pSource ); }
void SetSource( HSCRIPT hSource )
{
CBaseEntity *pEnt = ToEnt( hSource );
if (pEnt)
pSource = pEnt->GetBaseAnimating();
event.pSource = pEnt->GetBaseAnimating();
}

private:
animevent_t &event;
// storage for ScriptVariant_t string, which may be temporary
char *options;
};

class CAnimEventTInstanceHelper : public IScriptInstanceHelper
Expand Down
Loading
Loading