@@ -758,8 +758,6 @@ int CModelInfoSA::GetRefCount ()
758758
759759void CModelInfoSA::RemoveRef ( bool bRemoveExtraGTARef )
760760{
761- assert ( m_dwReferences > 0 );
762-
763761 // Decrement the references
764762 if ( m_dwReferences > 0 )
765763 m_dwReferences--;
@@ -1383,4 +1381,50 @@ void CModelInfoSA::ResetSupportedUpgrades ( void )
13831381eModelInfoType CModelInfoSA::GetModelType (void )
13841382{
13851383 return ((eModelInfoType (*)(void ))m_pInterface->VFTBL ->GetModelType )();
1386- }
1384+ }
1385+
1386+ // ////////////////////////////////////////////////////////////////////////////////////////
1387+ //
1388+ // CModelInfoSA::ForceUnload
1389+ //
1390+ // 1. Unload the dff from memory
1391+ // 2. Unload the associated txd from memory
1392+ // 3. Cross fingers
1393+ //
1394+ // Why do we do this?
1395+ // Player model adds (seemingly) unnecessary refs
1396+ // (Will crash if anything is actually using the model or txd)
1397+ //
1398+ // Won't work if there is a custom model replacement
1399+ //
1400+ // Returns true if model was unloaded
1401+ //
1402+ // ////////////////////////////////////////////////////////////////////////////////////////
1403+ bool CModelInfoSA::ForceUnload ( void )
1404+ {
1405+ CBaseModelInfoSAInterface* pModelInfoSAInterface = GetInterface ();
1406+
1407+ // Need to have at least one ref to delete pRwObject
1408+ if ( pModelInfoSAInterface->usNumberOfRefs == 0 && pModelInfoSAInterface->pRwObject != NULL )
1409+ {
1410+ pModelInfoSAInterface->usNumberOfRefs ++;
1411+ }
1412+
1413+ // Keep removing refs from the model until is it gone
1414+ uint uiLimit = 100 ;
1415+ while ( pModelInfoSAInterface->usNumberOfRefs > 0 && uiLimit-- )
1416+ {
1417+ RemoveRef ();
1418+ }
1419+
1420+ // Did it work?
1421+ if ( pModelInfoSAInterface->usNumberOfRefs > 0 || pModelInfoSAInterface->pRwObject != NULL )
1422+ return false ;
1423+
1424+ // If success, then remove txd
1425+ ushort usTxdId = GetTextureDictionaryID ();
1426+ if ( usTxdId )
1427+ pGame->GetRenderWare ()->TxdForceUnload ( usTxdId, true );
1428+
1429+ return true ;
1430+ }
0 commit comments