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.

249 lines
8.9 KiB

  1. //========= Copyright Valve Corporation, All rights reserved. ============//
  2. //
  3. // Purpose:
  4. //
  5. // $NoKeywords: $
  6. //=============================================================================//
  7. #if !defined( MOD_LOADER_H )
  8. #define MOD_LOADER_H
  9. #ifdef _WIN32
  10. #pragma once
  11. #endif
  12. struct model_t;
  13. class IMaterial;
  14. class IFileList;
  15. class IModelLoadCallback;
  16. #include "utlmemory.h"
  17. //-----------------------------------------------------------------------------
  18. // Purpose:
  19. //-----------------------------------------------------------------------------
  20. abstract_class IModelLoader
  21. {
  22. public:
  23. enum REFERENCETYPE
  24. {
  25. // The name is allocated, but nothing else is in memory or being referenced
  26. FMODELLOADER_NOTLOADEDORREFERENCED = 0,
  27. // The model has been loaded into memory
  28. FMODELLOADER_LOADED = (1<<0),
  29. // The model is being referenced by the server code
  30. FMODELLOADER_SERVER = (1<<1),
  31. // The model is being referenced by the client code
  32. FMODELLOADER_CLIENT = (1<<2),
  33. // The model is being referenced in the client .dll
  34. FMODELLOADER_CLIENTDLL = (1<<3),
  35. // The model is being referenced by static props
  36. FMODELLOADER_STATICPROP = (1<<4),
  37. // The model is a detail prop
  38. FMODELLOADER_DETAILPROP = (1<<5),
  39. // The model is dynamically loaded
  40. FMODELLOADER_DYNSERVER = (1<<6),
  41. FMODELLOADER_DYNCLIENT = (1<<7),
  42. FMODELLOADER_DYNAMIC = FMODELLOADER_DYNSERVER | FMODELLOADER_DYNCLIENT,
  43. FMODELLOADER_REFERENCEMASK = (FMODELLOADER_SERVER | FMODELLOADER_CLIENT | FMODELLOADER_CLIENTDLL | FMODELLOADER_STATICPROP | FMODELLOADER_DETAILPROP | FMODELLOADER_DYNAMIC ),
  44. // The model was touched by the preload method
  45. FMODELLOADER_TOUCHED_BY_PRELOAD = (1<<15),
  46. // The model was loaded by the preload method, a postload fixup is required
  47. FMODELLOADER_LOADED_BY_PRELOAD = (1<<16),
  48. // The model touched its materials as part of its load
  49. FMODELLOADER_TOUCHED_MATERIALS = (1<<17),
  50. };
  51. enum ReloadType_t
  52. {
  53. RELOAD_LOD_CHANGED = 0,
  54. RELOAD_EVERYTHING,
  55. RELOAD_REFRESH_MODELS,
  56. };
  57. // Start up modelloader subsystem
  58. virtual void Init( void ) = 0;
  59. virtual void Shutdown( void ) = 0;
  60. virtual int GetCount( void ) = 0;
  61. virtual model_t *GetModelForIndex( int i ) = 0;
  62. // Look up name for model
  63. virtual const char *GetName( const model_t *model ) = 0;
  64. // Check for extra data, reload studio model if needed
  65. virtual void *GetExtraData( model_t *model ) = 0;
  66. // Get disk size for model
  67. virtual int GetModelFileSize( const char *name ) = 0;
  68. // Finds the model, and loads it if it isn't already present. Updates reference flags
  69. virtual model_t *GetModelForName( const char *name, REFERENCETYPE referencetype ) = 0;
  70. virtual model_t *ReferenceModel( const char *name, REFERENCETYPE referencetype ) = 0;
  71. // Unmasks the referencetype field for the model
  72. virtual void UnreferenceModel( model_t *model, REFERENCETYPE referencetype ) = 0;
  73. // Unmasks the specified reference type across all models
  74. virtual void UnreferenceAllModels( REFERENCETYPE referencetype ) = 0;
  75. // Set all models to last loaded on server count -1
  76. virtual void ResetModelServerCounts() = 0;
  77. // For any models with referencetype blank, frees all memory associated with the model
  78. // and frees up the models slot
  79. virtual void UnloadUnreferencedModels( void ) = 0;
  80. virtual void PurgeUnusedModels( void ) = 0;
  81. virtual void UnloadModel( model_t *pModel ) = 0;
  82. // On the client only, there is some information that is computed at the time we are just
  83. // about to render the map the first time. If we don't change/unload the map, then we
  84. // shouldn't have to recompute it each time we reconnect to the same map
  85. virtual bool Map_GetRenderInfoAllocated( void ) = 0;
  86. virtual void Map_SetRenderInfoAllocated( bool allocated ) = 0;
  87. // Load all the displacements for rendering. Set bRestoring to true if we're recovering from an alt+tab.
  88. virtual void Map_LoadDisplacements( model_t *model, bool bRestoring ) = 0;
  89. // Print which models are in the cache/known
  90. virtual void Print( void ) = 0;
  91. // Validate version/header of a .bsp file
  92. virtual bool Map_IsValid( char const *mapname, bool bQuiet = false ) = 0;
  93. // Recomputes surface flags
  94. virtual void RecomputeSurfaceFlags( model_t *mod ) = 0;
  95. // Reloads all models
  96. virtual void Studio_ReloadModels( ReloadType_t reloadType ) = 0;
  97. // Is a model loaded?
  98. virtual bool IsLoaded( const model_t *mod ) = 0;
  99. virtual bool LastLoadedMapHasHDRLighting( void ) = 0;
  100. // See CL_HandlePureServerWhitelist for what this is for.
  101. virtual void ReloadFilesInList( IFileList *pFilesToReload ) = 0;
  102. virtual const char *GetActiveMapName( void ) = 0;
  103. // Called by app system once per frame to poll and update dynamic models
  104. virtual void UpdateDynamicModels() = 0;
  105. // Called by server and client engine code to flush unreferenced dynamic models
  106. virtual void FlushDynamicModels() = 0;
  107. // Called by server and client engine code to flush unreferenced dynamic models
  108. virtual void ForceUnloadNonClientDynamicModels() = 0;
  109. // Called by client code to load dynamic models, instead of GetModelForName.
  110. virtual model_t *GetDynamicModel( const char *name, bool bClientOnly ) = 0;
  111. // Called by client code to query dynamic model state
  112. virtual bool IsDynamicModelLoading( model_t *pModel, bool bClientOnly ) = 0;
  113. // Called by client code to refcount dynamic models
  114. virtual void AddRefDynamicModel( model_t *pModel, bool bClientSideRef ) = 0;
  115. virtual void ReleaseDynamicModel( model_t *pModel, bool bClientSideRef ) = 0;
  116. // Called by client code
  117. virtual bool RegisterModelLoadCallback( model_t *pModel, bool bClientOnly, IModelLoadCallback *pCallback, bool bCallImmediatelyIfLoaded = true ) = 0;
  118. // Called by client code or IModelLoadCallback destructor
  119. virtual void UnregisterModelLoadCallback( model_t *pModel, bool bClientOnly, IModelLoadCallback *pCallback ) = 0;
  120. virtual void Client_OnServerModelStateChanged( model_t *pModel, bool bServerLoaded ) = 0;
  121. };
  122. extern IModelLoader *modelloader;
  123. //-----------------------------------------------------------------------------
  124. // Purpose: Loads the lump to temporary memory and automatically cleans up the
  125. // memory when it goes out of scope.
  126. //-----------------------------------------------------------------------------
  127. class CMapLoadHelper
  128. {
  129. public:
  130. CMapLoadHelper( int lumpToLoad );
  131. ~CMapLoadHelper( void );
  132. // Get raw memory pointer
  133. byte *LumpBase( void );
  134. int LumpSize( void );
  135. int LumpOffset( void );
  136. int LumpVersion() const;
  137. const char *GetMapName( void );
  138. char *GetLoadName( void );
  139. struct worldbrushdata_t *GetMap( void );
  140. // Global setup/shutdown
  141. static void Init( model_t *pMapModel, const char *pLoadname );
  142. static void InitFromMemory( model_t *pMapModel, const void *pData, int nDataSize );
  143. static void Shutdown( void );
  144. static int GetRefCount( void );
  145. // Free the lighting lump (increases free memory during loading on 360)
  146. static void FreeLightingLump();
  147. // Returns the size of a particular lump without loading it
  148. static int LumpSize( int lumpId );
  149. static int LumpOffset( int lumpId );
  150. // Loads one element in a lump.
  151. void LoadLumpElement( int nElemIndex, int nElemSize, void *pData );
  152. void LoadLumpData( int offset, int size, void *pData );
  153. private:
  154. int m_nLumpSize;
  155. int m_nLumpOffset;
  156. int m_nLumpVersion;
  157. byte *m_pRawData;
  158. byte *m_pData;
  159. byte *m_pUncompressedData;
  160. // Handling for lump files
  161. int m_nLumpID;
  162. char m_szLumpFilename[MAX_PATH];
  163. };
  164. //-----------------------------------------------------------------------------
  165. // Recomputes translucency for the model...
  166. //-----------------------------------------------------------------------------
  167. void Mod_RecomputeTranslucency( model_t* mod, int nSkin, int nBody, void /*IClientRenderable*/ *pClientRenderable, float fInstanceAlphaModulate );
  168. //-----------------------------------------------------------------------------
  169. // game lumps
  170. //-----------------------------------------------------------------------------
  171. int Mod_GameLumpSize( int lumpId );
  172. int Mod_GameLumpVersion( int lumpId );
  173. bool Mod_LoadGameLump( int lumpId, void* pBuffer, int size );
  174. // returns the material count...
  175. int Mod_GetMaterialCount( model_t* mod );
  176. // returns the first n materials.
  177. int Mod_GetModelMaterials( model_t* mod, int count, IMaterial** ppMaterial );
  178. bool Mod_MarkWaterSurfaces( model_t *pModel );
  179. void Mod_SetMaterialVarFlag( model_t *pModel, unsigned int flag, bool on );
  180. //-----------------------------------------------------------------------------
  181. // Hooks the cache notify into the MDL cache system
  182. //-----------------------------------------------------------------------------
  183. void ConnectMDLCacheNotify( );
  184. void DisconnectMDLCacheNotify( );
  185. //-----------------------------------------------------------------------------
  186. // Initialize studiomdl state
  187. //-----------------------------------------------------------------------------
  188. void InitStudioModelState( model_t *pModel );
  189. extern bool g_bLoadedMapHasBakedPropLighting;
  190. extern bool g_bBakedPropLightingNoSeparateHDR;
  191. #endif // MOD_LOADER_H