//-----------------------------------------------------------------------------
// Reload particle definitions
//-----------------------------------------------------------------------------
void CClientTools::ReloadParticleDefintions( const char *pFileName, const void *pBufData, int nLen )
{
	MDLCACHE_CRITICAL_SECTION(); // Copying particle attachment control points may end up needing to evaluate skeletons

	//////////////
	// Find any systems that depend on any system in the buffer
	// slow, but necessary if we want live reloads to work - and doesn't happen too often
	CUtlVector<CUtlString> systemNamesToReload;

	g_pParticleSystemMgr->GetParticleSystemsInBuffer( CUtlBuffer(pBufData, nLen, CUtlBuffer::READ_ONLY), &systemNamesToReload );

	CUtlVector<CNewParticleEffect*> toReplaceEffects;
	CUtlVector<CUtlString> toReplaceNames;

	for( CNewParticleEffect *pEffect = ParticleMgr()->FirstNewEffect(); pEffect; pEffect = ParticleMgr()->NextNewEffect(pEffect) )
	{
		for( int i = 0; i < systemNamesToReload.Count(); ++i )
		{
			if ( pEffect->DependsOnSystem( systemNamesToReload[i] ) )
			{
				// only reload a given effect once
				if ( -1 == toReplaceEffects.Find(pEffect) )
				{
					toReplaceNames.AddToTail( pEffect->GetName() );
					toReplaceEffects.AddToTail( pEffect );
				}
			}
		}
	}

	CUtlVector<CNonDrawingParticleSystem*> toReplaceEffectsNonDrawing;
	CUtlVector<CUtlString> toReplaceNamesNonDrawing;
	for( CNonDrawingParticleSystem *i = ParticleMgr()->m_NonDrawingParticleSystems.Head(); i ; i = i->m_pNext )
	{
		for( int j = 0; j < systemNamesToReload.Count(); ++j )
		{
			if ( i->Get()->DependsOnSystem( systemNamesToReload[j] ) )
			{
				if ( -1 == toReplaceEffectsNonDrawing.Find( i ) )
				{
					toReplaceNamesNonDrawing.AddToTail( i->Get()->GetName() );
					toReplaceEffectsNonDrawing.AddToTail( i );
				}
			}
		}
	}

	//////////////
	// Load the data and stomp the old definitions
	g_pParticleSystemMgr->ReadParticleConfigFile( CUtlBuffer(pBufData, nLen, CUtlBuffer::READ_ONLY), true );

	//////////////
	// Now replace all of the systems with their new versions
	Assert( toReplaceEffects.Count() == toReplaceNames.Count() );

	for( int i = 0; i < toReplaceNames.Count(); ++i )
	{
		CNewParticleEffect *pEffect = toReplaceEffects[i];
		pEffect->ReplaceWith( toReplaceNames[i] );
	}

	// update all the non-drawings ones
	for( int i = 0; i < toReplaceNamesNonDrawing.Count(); i++ )
	{
		CNonDrawingParticleSystem *pEffect = toReplaceEffectsNonDrawing[i];
		delete pEffect->m_pSystem;
		pEffect->m_pSystem = g_pParticleSystemMgr->CreateParticleCollection( toReplaceNamesNonDrawing[i] );
	}
}