Team Fortress 2 Source Code as on 22/4/2020
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

222 lines
6.1 KiB

  1. //========= Copyright Valve Corporation, All rights reserved. ============//
  2. //
  3. // Purpose:
  4. //
  5. //=============================================================================//
  6. #include "cbase.h"
  7. // memdbgon must be the last include file in a .cpp file!!!
  8. #include "tier0/memdbgon.h"
  9. //-----------------------------------------------------------------------------
  10. // Phys pointer association
  11. //-----------------------------------------------------------------------------
  12. static CUtlMap<void *, void *> s_VPhysPtrMap( 0, 0, DefLessFunc(void *) );
  13. CVPhysPtrSaveRestoreOps g_VPhysPtrSaveRestoreOps;
  14. CVPhysPtrUtlVectorSaveRestoreOps g_VPhysPtrUtlVectorSaveRestoreOps;
  15. //-----------------------------------------------------------------------------
  16. // Phys pointer association
  17. //-----------------------------------------------------------------------------
  18. static void AddPtrAssociation( void *pOldValue, void *pNewValue )
  19. {
  20. s_VPhysPtrMap.Insert( pOldValue, pNewValue );
  21. }
  22. //-----------------------------------------------------------------------------
  23. // Purpose: Save/load part of CPhysicsEnvironment
  24. //-----------------------------------------------------------------------------
  25. static bool NoPhysSaveFunc( const physsaveparams_t &params, void * )
  26. {
  27. AssertMsg( 0, "Physics cannot save the specified type" );
  28. return false;
  29. }
  30. bool CPhysicsEnvironment::Save( const physsaveparams_t &params )
  31. {
  32. const PhysInterfaceId_t type = params.type;
  33. Assert( type >= 0 && type < PIID_NUM_TYPES );
  34. static PhysSaveFunc_t saveFuncs[PIID_NUM_TYPES] =
  35. {
  36. NoPhysSaveFunc,
  37. (PhysSaveFunc_t)SavePhysicsObject,
  38. (PhysSaveFunc_t)SavePhysicsFluidController,
  39. (PhysSaveFunc_t)SavePhysicsSpring,
  40. (PhysSaveFunc_t)SavePhysicsConstraintGroup,
  41. (PhysSaveFunc_t)SavePhysicsConstraint,
  42. (PhysSaveFunc_t)SavePhysicsShadowController,
  43. (PhysSaveFunc_t)SavePhysicsPlayerController,
  44. (PhysSaveFunc_t)SavePhysicsMotionController,
  45. (PhysSaveFunc_t)SavePhysicsVehicleController,
  46. };
  47. if ( type >= 0 && type < PIID_NUM_TYPES )
  48. {
  49. params.pSave->WriteInt( (int *)&params.pObject );
  50. return (*saveFuncs[type])( params, params.pObject );
  51. }
  52. return false;
  53. }
  54. static bool NoPhysRestoreFunc( const physrestoreparams_t &params, void ** )
  55. {
  56. AssertMsg( 0, "Physics cannot save the specified type" );
  57. return false;
  58. }
  59. CVPhysPtrSaveRestoreOps::CVPhysPtrSaveRestoreOps()
  60. {
  61. }
  62. void CPhysicsEnvironment::PreRestore( const physprerestoreparams_t &params )
  63. {
  64. g_VPhysPtrSaveRestoreOps.PreRestore();
  65. for ( int i = 0; i < params.recreatedObjectCount; i++ )
  66. {
  67. AddPtrAssociation( params.recreatedObjectList[i].pOldObject, params.recreatedObjectList[i].pNewObject );
  68. }
  69. }
  70. bool CPhysicsEnvironment::Restore( const physrestoreparams_t &params )
  71. {
  72. const PhysInterfaceId_t type = params.type;
  73. Assert( type >= 0 && type < PIID_NUM_TYPES );
  74. static PhysRestoreFunc_t restoreFuncs[PIID_NUM_TYPES] =
  75. {
  76. NoPhysRestoreFunc,
  77. (PhysRestoreFunc_t)RestorePhysicsObject,
  78. (PhysRestoreFunc_t)RestorePhysicsFluidController,
  79. (PhysRestoreFunc_t)RestorePhysicsSpring,
  80. (PhysRestoreFunc_t)RestorePhysicsConstraintGroup,
  81. (PhysRestoreFunc_t)RestorePhysicsConstraint,
  82. (PhysRestoreFunc_t)RestorePhysicsShadowController,
  83. (PhysRestoreFunc_t)RestorePhysicsPlayerController,
  84. (PhysRestoreFunc_t)RestorePhysicsMotionController,
  85. (PhysRestoreFunc_t)RestorePhysicsVehicleController,
  86. };
  87. if ( type >= 0 && type < PIID_NUM_TYPES )
  88. {
  89. void *pOldObject;
  90. params.pRestore->ReadInt( (int *)&pOldObject );
  91. if ( (*restoreFuncs[type])( params, params.ppObject ) )
  92. {
  93. AddPtrAssociation( pOldObject, *params.ppObject );
  94. if ( type == PIID_IPHYSICSOBJECT )
  95. {
  96. m_objects.AddToTail( (IPhysicsObject *)(*params.ppObject) );
  97. }
  98. return true;
  99. }
  100. }
  101. return false;
  102. }
  103. void CPhysicsEnvironment::PostRestore()
  104. {
  105. g_VPhysPtrSaveRestoreOps.PostRestore();
  106. }
  107. //-----------------------------------------------------------------------------
  108. // Purpose: Fixes up pointers beteween vphysics objects
  109. //-----------------------------------------------------------------------------
  110. void CVPhysPtrSaveRestoreOps::Save( const SaveRestoreFieldInfo_t &fieldInfo, ISave *pSave )
  111. {
  112. int *pField = (int *)fieldInfo.pField;
  113. int nObjects = fieldInfo.pTypeDesc->fieldSize;
  114. for ( int i = 0; i < nObjects; i++ )
  115. {
  116. pSave->WriteInt( pField );
  117. ++pField;
  118. }
  119. }
  120. //-------------------------------------
  121. void CVPhysPtrSaveRestoreOps::PreRestore()
  122. {
  123. Assert( s_VPhysPtrMap.Count() == 0 );
  124. }
  125. //-------------------------------------
  126. void CVPhysPtrSaveRestoreOps::Restore( const SaveRestoreFieldInfo_t &fieldInfo, IRestore *pRestore )
  127. {
  128. void **ppField = (void **)fieldInfo.pField;
  129. int nObjects = fieldInfo.pTypeDesc->fieldSize;
  130. for ( int i = 0; i < nObjects; i++ )
  131. {
  132. pRestore->ReadInt( (int *)ppField );
  133. int iNewVal = s_VPhysPtrMap.Find( *ppField );
  134. if ( iNewVal != s_VPhysPtrMap.InvalidIndex() )
  135. {
  136. *ppField = s_VPhysPtrMap[iNewVal];
  137. }
  138. else
  139. {
  140. *ppField = NULL;
  141. }
  142. ++ppField;
  143. }
  144. }
  145. //-------------------------------------
  146. void CVPhysPtrSaveRestoreOps::PostRestore()
  147. {
  148. s_VPhysPtrMap.RemoveAll();
  149. PostRestorePhysicsObject();
  150. PostRestorePhysicsConstraintGroup();
  151. }
  152. //-----------------------------------------------------------------------------
  153. void CVPhysPtrUtlVectorSaveRestoreOps::Save( const SaveRestoreFieldInfo_t &fieldInfo, ISave *pSave )
  154. {
  155. Assert( fieldInfo.pTypeDesc->fieldSize == 1 );
  156. VPhysPtrVector *pUtlVector = (VPhysPtrVector*)fieldInfo.pField;
  157. int nObjects = pUtlVector->Count();
  158. pSave->WriteInt( &nObjects );
  159. for ( int i = 0; i < nObjects; i++ )
  160. {
  161. pSave->WriteInt( &pUtlVector->Element(i) );
  162. }
  163. }
  164. void CVPhysPtrUtlVectorSaveRestoreOps::Restore( const SaveRestoreFieldInfo_t &fieldInfo, IRestore *pRestore )
  165. {
  166. Assert( fieldInfo.pTypeDesc->fieldSize == 1 );
  167. VPhysPtrVector *pUtlVector = (VPhysPtrVector*)fieldInfo.pField;
  168. int nObjects;
  169. pRestore->ReadInt( &nObjects );
  170. pUtlVector->AddMultipleToTail( nObjects );
  171. for ( int i = 0; i < nObjects; i++ )
  172. {
  173. void **ppElem = (void**)(&pUtlVector->Element(i));
  174. pRestore->ReadInt( (int*)ppElem );
  175. int iNewVal = s_VPhysPtrMap.Find( *ppElem );
  176. if ( iNewVal != s_VPhysPtrMap.InvalidIndex() )
  177. {
  178. *ppElem = s_VPhysPtrMap[iNewVal];
  179. }
  180. else
  181. {
  182. *ppElem = NULL;
  183. }
  184. }
  185. }