Skip to content

Commit

Permalink
Fixed EngineSound.emit_sound from crashing on OrangeBox engine.
Browse files Browse the repository at this point in the history
  • Loading branch information
invincibleqc committed Jun 11, 2014
1 parent d09384f commit 03d9e12
Showing 1 changed file with 7 additions and 4 deletions.
11 changes: 7 additions & 4 deletions src/core/modules/engines/orangebox/engines_wrap_python.h
Expand Up @@ -71,13 +71,16 @@ inline void IEngineSound_EmitSound(IEngineSound* pEngineSound, IRecipientFilter&
float flVolume, float flAttenuation, int iFlags = 0, int iPitch = PITCH_NORM, const Vector *pOrigin = NULL, const Vector *pDirection = NULL,
tuple origins = tuple(), bool bUpdatePositions = true, float soundtime = 0.0f, int speakerentity = -1)
{
CUtlVector< Vector > pUtlVecOrigins;
for(int i=0; i < len(origins); i++)
CUtlVector< Vector > *pUtlVecOrigins = NULL;
if (len(origins) > 0)
{
pUtlVecOrigins.AddToTail(extract<Vector>(origins[i]));
for(int i=0; i < len(origins); i++)
{
pUtlVecOrigins->AddToTail(extract<Vector>(origins[i]));
}
}

pEngineSound->EmitSound(filter, iEntIndex, iChannel, pSample, flVolume, flAttenuation, iFlags, iPitch, 0, pOrigin, pDirection, &pUtlVecOrigins, bUpdatePositions, soundtime, speakerentity);
pEngineSound->EmitSound(filter, iEntIndex, iChannel, pSample, flVolume, flAttenuation, iFlags, iPitch, 0, pOrigin, pDirection, pUtlVecOrigins, bUpdatePositions, soundtime, speakerentity);
}

// Visitor function
Expand Down

4 comments on commit 03d9e12

@jordanbriere
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Test code:

from engines.sound import Attenuations
from engines.sound import Channels
from engines.sound import EngineSound
from engines.sound import PitchTypes
from engines.sound import SoundFlags
from engines.sound import VOL_NORM
from events import Event
from filters.recipients import RecipientFilter
from players.entity import PlayerEntity
from players.helpers import index_from_userid

@Event
def player_jump(game_event):
    player = PlayerEntity(index_from_userid(game_event.get_int('userid')))
    EngineSound.emit_sound(RecipientFilter(), player.index, Channels.AUTO,
        'player\death3.wav', VOL_NORM, Attenuations.NORMAL,
            SoundFlags.NOFLAGS, PitchTypes.NORMAL)

@Ayuto
Copy link
Member

@Ayuto Ayuto commented on 03d9e12 Jun 11, 2014

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This doesn't fix the real issue. The problem is that pUtlVecOrigins is never initialized. Also, the length check is not necessary. :P

Actually, it should also crash on CS:GO.

@jordanbriere
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Isn't AddToTail supposed to grow itself? And the length check is necessary as we want to explicitly pass NULL unless we have valid data.

However, the following seems to work just fine:

inline void IEngineSound_EmitSound(IEngineSound* pEngineSound, IRecipientFilter& filter, int iEntIndex, int iChannel, const char *pSample, 
        float flVolume, float flAttenuation, int iFlags = 0, int iPitch = PITCH_NORM, const Vector *pOrigin = NULL, const Vector *pDirection = NULL,
        tuple origins = tuple(), bool bUpdatePositions = true, float soundtime = 0.0f, int speakerentity = -1)
{
    CUtlVector<Vector> *pUtlVecOrigins = NULL;
    CUtlVector<Vector> vecOrigins;
    if (len(origins) > 0)
    {
        pUtlVecOrigins = &vecOrigins;
        for(int i=0; i < len(origins); i++)
        {
            vecOrigins.AddToTail(extract<Vector>(origins[i]));
        }
    }

    pEngineSound->EmitSound(filter, iEntIndex, iChannel, pSample, flVolume, flAttenuation, iFlags, iPitch, 0, pOrigin, pDirection, pUtlVecOrigins, bUpdatePositions, soundtime, speakerentity);
}

While using the following code:

from engines.sound import Attenuations
from engines.sound import Channels
from engines.sound import EngineSound
from engines.sound import PitchTypes
from engines.sound import SoundFlags
from engines.sound import VOL_NORM
from events import Event
from filters.recipients import RecipientFilter
from players.entity import PlayerEntity
from players.helpers import index_from_userid
from mathlib import Vector

@Event
def player_jump(game_event):
    player = PlayerEntity(index_from_userid(game_event.get_int('userid')))
    EngineSound.emit_sound(RecipientFilter(), 0, Channels.AUTO,
        'player\death3.wav', VOL_NORM, Attenuations.NORMAL,
            SoundFlags.NOFLAGS, PitchTypes.NORMAL, Vector(0, 0, 0), Vector(0, 0, 0), (player.m_vecOrigin,))

@Ayuto
Copy link
Member

@Ayuto Ayuto commented on 03d9e12 Jun 11, 2014

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, that should work. The reason why your previous code would crash in the loop is because it would try to call AddToTail() on a NULL pointer and member variables are used in that call.

Please sign in to comment.