Skip to content

Fix scene loading memory errors #130

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

Merged
merged 2 commits into from
Jul 2, 2021
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 25 additions & 12 deletions sp/src/game/server/sceneentity.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3735,7 +3735,7 @@ CChoreoScene *CSceneEntity::LoadScene( const char *filename, IChoreoEventCallbac
Q_FixSlashes( loadfile );

// binary compiled vcd
void *pBuffer;
void *pBuffer = NULL;
#ifdef MAPBASE
//
// Raw scene file support
Expand All @@ -3760,12 +3760,13 @@ CChoreoScene *CSceneEntity::LoadScene( const char *filename, IChoreoEventCallbac
{
g_TokenProcessor.SetBuffer((char*)pBuffer);
pScene = ChoreoLoadScene( loadfile, NULL, &g_TokenProcessor, LocalScene_Printf );
g_TokenProcessor.SetBuffer(NULL);
}
// Okay, it's definitely missing.
else
{
MissingSceneWarning( loadfile );
return NULL;
pScene = NULL;
}

if (pScene)
Expand Down Expand Up @@ -4283,6 +4284,7 @@ CBaseEntity *CSceneEntity::FindNamedEntity( const char *name, CBaseEntity *pActo
#ifdef MAPBASE
const char *GetFirstSoundInScene(const char *pszScene)
{
const char *soundName;
SceneCachedData_t sceneData;
if ( scenefilecache->GetSceneCachedData( pszScene, &sceneData ) )
{
Expand All @@ -4292,30 +4294,35 @@ const char *GetFirstSoundInScene(const char *pszScene)
short stringId = scenefilecache->GetSceneCachedSound( sceneData.sceneId, 0 );

// Trust that it's been precached
return scenefilecache->GetSceneString( stringId );
soundName = scenefilecache->GetSceneString( stringId );
}
}
else
{
void *pBuffer = NULL;
if (filesystem->ReadFileEx( pszScene, "MOD", &pBuffer, false, true ))
if (filesystem->ReadFileEx( pszScene, "MOD", &pBuffer, true ))
{
g_TokenProcessor.SetBuffer((char*)pBuffer);
CChoreoScene *pScene = ChoreoLoadScene( pszScene, NULL, &g_TokenProcessor, LocalScene_Printf );
g_TokenProcessor.SetBuffer(NULL);
if (pScene)
{
for (int i = 0; i < pScene->GetNumEvents(); i++)
{
CChoreoEvent *pEvent = pScene->GetEvent(i);

if (pEvent->GetType() == CChoreoEvent::SPEAK)
return pEvent->GetParameters();
{
soundName = pEvent->GetParameters();
break;
}
}
}
}
FreeSceneFileMemory( pBuffer );
}

return NULL;
return soundName;
}

const char *GetFirstSoundInScene(CChoreoScene *scene)
Expand Down Expand Up @@ -4483,6 +4490,8 @@ bool CSceneEntity::ScriptLoadSceneFromString(const char* pszFilename, const char
PrecacheScene(pScene);
}

g_TokenProcessor.SetBuffer(NULL);

if (pScene != NULL)
{
// release prior scene if present
Expand Down Expand Up @@ -5284,12 +5293,12 @@ int GetSceneSpeechCount( char const *pszScene )
else
{
void *pBuffer = NULL;
if (filesystem->ReadFileEx( pszScene, "MOD", &pBuffer, false, true ))
int iNumSounds = 0;
if (filesystem->ReadFileEx( pszScene, "MOD", &pBuffer, true ))
{
int iNumSounds = 0;

g_TokenProcessor.SetBuffer((char*)pBuffer);
CChoreoScene *pScene = ChoreoLoadScene( pszScene, NULL, &g_TokenProcessor, LocalScene_Printf );
g_TokenProcessor.SetBuffer(NULL);
if (pScene)
{
for (int i = 0; i < pScene->GetNumEvents(); i++)
Expand All @@ -5300,9 +5309,11 @@ int GetSceneSpeechCount( char const *pszScene )
iNumSounds++;
}
}

return iNumSounds;
}

FreeSceneFileMemory( pBuffer );

return iNumSounds;
}
#endif
return 0;
Expand Down Expand Up @@ -5359,15 +5370,17 @@ void PrecacheInstancedScene( char const *pszScene )

// Attempt to precache manually
void *pBuffer = NULL;
if (filesystem->ReadFileEx( loadfile, "MOD", &pBuffer, false, true ))
if (filesystem->ReadFileEx( loadfile, "MOD", &pBuffer, true ))
{
g_TokenProcessor.SetBuffer((char*)pBuffer);
CChoreoScene *pScene = ChoreoLoadScene( loadfile, NULL, &g_TokenProcessor, LocalScene_Printf );
if (pScene)
{
PrecacheChoreoScene(pScene);
}
g_TokenProcessor.SetBuffer(NULL);
}
FreeSceneFileMemory( pBuffer );
#else
// Scenes are sloppy and don't always exist.
// A scene that is not in the pre-built cache image, but on disk, is a true error.
Expand Down