Advertisement
Guest User

Untitled

a guest
Feb 17th, 2018
66
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 9.55 KB | None | 0 0
  1. #ifndef IMDLCACHE_H
  2. #define IMDLCACHE_H
  3.  
  4. #ifdef _WIN32
  5. #pragma once
  6. #endif
  7.  
  8. //-----------------------------------------------------------------------------
  9. // Forward declarations
  10. //-----------------------------------------------------------------------------
  11. struct studiohdr_t;
  12. struct studiohwdata_t;
  13. struct vcollide_t;
  14. struct virtualmodel_t;
  15. struct vertexFileHeader_t;
  16.  
  17. namespace OptimizedModel
  18. {
  19. struct FileHeader_t;
  20. }
  21.  
  22.  
  23. //-----------------------------------------------------------------------------
  24. // Reference to a loaded studiomdl
  25. //-----------------------------------------------------------------------------
  26. typedef unsigned short MDLHandle_t;
  27.  
  28. enum
  29. {
  30. MDLHANDLE_INVALID = (MDLHandle_t)~0
  31. };
  32.  
  33.  
  34. //-----------------------------------------------------------------------------
  35. // Cache data types
  36. //-----------------------------------------------------------------------------
  37. enum MDLCacheDataType_t
  38. {
  39. // Callbacks to get called when data is loaded or unloaded for these:
  40. MDLCACHE_STUDIOHDR = 0,
  41. MDLCACHE_STUDIOHWDATA,
  42. MDLCACHE_VCOLLIDE,
  43.  
  44. // Callbacks NOT called when data is loaded or unloaded for these:
  45. MDLCACHE_ANIMBLOCK,
  46. MDLCACHE_VIRTUALMODEL,
  47. MDLCACHE_VERTEXES,
  48. MDLCACHE_DECODEDANIMBLOCK,
  49. };
  50.  
  51. class IMDLCacheNotify
  52. {
  53. public:
  54. // Called right after the data is loaded
  55. virtual void OnDataLoaded(MDLCacheDataType_t type, MDLHandle_t handle) = 0;
  56.  
  57. // Called right before the data is unloaded
  58. virtual void OnDataUnloaded(MDLCacheDataType_t type, MDLHandle_t handle) = 0;
  59. };
  60.  
  61.  
  62. //-----------------------------------------------------------------------------
  63. // Flags for flushing
  64. //-----------------------------------------------------------------------------
  65. enum MDLCacheFlush_t
  66. {
  67. MDLCACHE_FLUSH_STUDIOHDR = 0x01,
  68. MDLCACHE_FLUSH_STUDIOHWDATA = 0x02,
  69. MDLCACHE_FLUSH_VCOLLIDE = 0x04,
  70. MDLCACHE_FLUSH_ANIMBLOCK = 0x08,
  71. MDLCACHE_FLUSH_VIRTUALMODEL = 0x10,
  72. MDLCACHE_FLUSH_AUTOPLAY = 0x20,
  73. MDLCACHE_FLUSH_VERTEXES = 0x40,
  74.  
  75. MDLCACHE_FLUSH_IGNORELOCK = 0x80000000,
  76. MDLCACHE_FLUSH_ALL = 0xFFFFFFFF
  77. };
  78.  
  79. /*
  80. #define MDLCACHE_INTERFACE_VERSION_4 "MDLCache004"
  81. namespace MDLCacheV4
  82. {
  83. abstract_class IMDLCache : public IAppSystem
  84. {
  85. public:
  86. // Used to install callbacks for when data is loaded + unloaded
  87. virtual void SetCacheNotify( IMDLCacheNotify *pNotify ) = 0;
  88. // NOTE: This assumes the "GAME" path if you don't use
  89. // the UNC method of specifying files. This will also increment
  90. // the reference count of the MDL
  91. virtual MDLHandle_t FindMDL( const char *pMDLRelativePath ) = 0;
  92. // Reference counting
  93. virtual int AddRef( MDLHandle_t handle ) = 0;
  94. virtual int Release( MDLHandle_t handle ) = 0;
  95. // Gets at the various data associated with a MDL
  96. virtual studiohdr_t *GetStudioHdr( MDLHandle_t handle ) = 0;
  97. virtual studiohwdata_t *GetHardwareData( MDLHandle_t handle ) = 0;
  98. virtual vcollide_t *GetVCollide( MDLHandle_t handle ) = 0;
  99. virtual unsigned char *GetAnimBlock( MDLHandle_t handle, int nBlock ) = 0;
  100. virtual virtualmodel_t *GetVirtualModel( MDLHandle_t handle ) = 0;
  101. virtual int GetAutoplayList( MDLHandle_t handle, unsigned short **pOut ) = 0;
  102. virtual vertexFileHeader_t *GetVertexData( MDLHandle_t handle ) = 0;
  103. // Brings all data associated with an MDL into memory
  104. virtual void TouchAllData( MDLHandle_t handle ) = 0;
  105. // Gets/sets user data associated with the MDL
  106. virtual void SetUserData( MDLHandle_t handle, void* pData ) = 0;
  107. virtual void *GetUserData( MDLHandle_t handle ) = 0;
  108. // Is this MDL using the error model?
  109. virtual bool IsErrorModel( MDLHandle_t handle ) = 0;
  110. // Flushes the cache, force a full discard
  111. virtual void Flush( int nFlushFlags = MDLCACHE_FLUSH_ALL ) = 0;
  112. // Flushes a particular model out of memory
  113. virtual void Flush( MDLHandle_t handle, int nFlushFlags = MDLCACHE_FLUSH_ALL ) = 0;
  114. // Returns the name of the model (its relative path)
  115. virtual const char *GetModelName( MDLHandle_t handle ) = 0;
  116. // faster access when you already have the studiohdr
  117. virtual virtualmodel_t *GetVirtualModelFast( const studiohdr_t *pStudioHdr, MDLHandle_t handle ) = 0;
  118. // all cache entries that subsequently allocated or successfully checked
  119. // are considered "locked" and will not be freed when additional memory is needed
  120. virtual void BeginLock() = 0;
  121. // reset all protected blocks to normal
  122. virtual void EndLock() = 0;
  123. // returns a pointer to a counter that is incremented every time the cache has been out of the locked state (EVIL)
  124. virtual int *GetFrameUnlockCounterPtr() = 0;
  125. // Finish all pending async operations
  126. virtual void FinishPendingLoads() = 0;
  127. };
  128. }
  129. */
  130.  
  131. //-----------------------------------------------------------------------------
  132. // The main MDL cacher
  133. //-----------------------------------------------------------------------------
  134. #define MDLCACHE_INTERFACE_VERSION "MDLCache004"
  135.  
  136. class IMDLCache
  137. {
  138. public:
  139. // Used to install callbacks for when data is loaded + unloaded
  140. // Returns the prior notify
  141. virtual void SetCacheNotify(IMDLCacheNotify *pNotify) = 0;
  142.  
  143. // NOTE: This assumes the "GAME" path if you don't use
  144. // the UNC method of specifying files. This will also increment
  145. // the reference count of the MDL
  146. virtual MDLHandle_t FindMDL(const char *pMDLRelativePath) = 0;
  147.  
  148. // Reference counting
  149. virtual int AddRef(MDLHandle_t handle) = 0;
  150. virtual int Release(MDLHandle_t handle) = 0;
  151. virtual int GetRef(MDLHandle_t handle) = 0;
  152.  
  153. // Gets at the various data associated with a MDL
  154. virtual studiohdr_t *GetStudioHdr(MDLHandle_t handle) = 0;
  155. virtual studiohwdata_t *GetHardwareData(MDLHandle_t handle) = 0;
  156. virtual vcollide_t *GetVCollide(MDLHandle_t handle) = 0;
  157. virtual unsigned char *GetAnimBlock(MDLHandle_t handle, int nBlock) = 0;
  158. virtual virtualmodel_t *GetVirtualModel(MDLHandle_t handle) = 0;
  159. virtual int GetAutoplayList(MDLHandle_t handle, unsigned short **pOut) = 0;
  160. virtual vertexFileHeader_t *GetVertexData(MDLHandle_t handle) = 0;
  161.  
  162. // Brings all data associated with an MDL into memory
  163. virtual void TouchAllData(MDLHandle_t handle) = 0;
  164.  
  165. // Gets/sets user data associated with the MDL
  166. virtual void SetUserData(MDLHandle_t handle, void* pData) = 0;
  167. virtual void *GetUserData(MDLHandle_t handle) = 0;
  168.  
  169. // Is this MDL using the error model?
  170. virtual bool IsErrorModel(MDLHandle_t handle) = 0;
  171.  
  172. // Flushes the cache, force a full discard
  173. virtual void Flush(MDLCacheFlush_t nFlushFlags = MDLCACHE_FLUSH_ALL) = 0;
  174.  
  175. // Flushes a particular model out of memory
  176. virtual void Flush(MDLHandle_t handle, int nFlushFlags = MDLCACHE_FLUSH_ALL) = 0;
  177.  
  178. // Returns the name of the model (its relative path)
  179. virtual const char *GetModelName(MDLHandle_t handle) = 0;
  180.  
  181. // faster access when you already have the studiohdr
  182. virtual virtualmodel_t *GetVirtualModelFast(const studiohdr_t *pStudioHdr, MDLHandle_t handle) = 0;
  183.  
  184. // all cache entries that subsequently allocated or successfully checked
  185. // are considered "locked" and will not be freed when additional memory is needed
  186. virtual void BeginLock() = 0;
  187.  
  188. // reset all protected blocks to normal
  189. virtual void EndLock() = 0;
  190.  
  191. // returns a pointer to a counter that is incremented every time the cache has been out of the locked state (EVIL)
  192. virtual int *GetFrameUnlockCounterPtrOLD() = 0;
  193.  
  194. // Finish all pending async operations
  195. virtual void FinishPendingLoads() = 0;
  196.  
  197. virtual vcollide_t *GetVCollideEx(MDLHandle_t handle, bool synchronousLoad = true) = 0;
  198. virtual bool GetVCollideSize(MDLHandle_t handle, int *pVCollideSize) = 0;
  199.  
  200. virtual bool GetAsyncLoad(MDLCacheDataType_t type) = 0;
  201. virtual bool SetAsyncLoad(MDLCacheDataType_t type, bool bAsync) = 0;
  202.  
  203. virtual void BeginMapLoad() = 0;
  204. virtual void EndMapLoad() = 0;
  205. virtual void MarkAsLoaded(MDLHandle_t handle) = 0;
  206.  
  207. virtual void InitPreloadData(bool rebuild) = 0;
  208. virtual void ShutdownPreloadData() = 0;
  209.  
  210. virtual bool IsDataLoaded(MDLHandle_t handle, MDLCacheDataType_t type) = 0;
  211.  
  212. virtual int *GetFrameUnlockCounterPtr(MDLCacheDataType_t type) = 0;
  213.  
  214. virtual studiohdr_t *LockStudioHdr(MDLHandle_t handle) = 0;
  215. virtual void UnlockStudioHdr(MDLHandle_t handle) = 0;
  216.  
  217. virtual bool PreloadModel(MDLHandle_t handle) = 0;
  218.  
  219. // Hammer uses this. If a model has an error loading in GetStudioHdr, then it is flagged
  220. // as an error model and any further attempts to load it will just get the error model.
  221. // That is, until you call this function. Then it will load the correct model.
  222. virtual void ResetErrorModelStatus(MDLHandle_t handle) = 0;
  223.  
  224. virtual void MarkFrame() = 0;
  225. };
  226.  
  227.  
  228. //-----------------------------------------------------------------------------
  229. // Critical section helper code
  230. //-----------------------------------------------------------------------------
  231. class CMDLCacheCriticalSection
  232. {
  233. public:
  234. CMDLCacheCriticalSection(IMDLCache *pCache) : m_pCache(pCache)
  235. {
  236. m_pCache->BeginLock();
  237. }
  238.  
  239. ~CMDLCacheCriticalSection()
  240. {
  241. m_pCache->EndLock();
  242. }
  243.  
  244. private:
  245. IMDLCache *m_pCache;
  246. };
  247.  
  248. #define MDCACHE_FINE_GRAINED 1
  249.  
  250. #if defined(MDCACHE_FINE_GRAINED)
  251. #define MDLCACHE_CRITICAL_SECTION_( pCache ) CMDLCacheCriticalSection cacheCriticalSection(pCache)
  252. #define MDLCACHE_COARSE_LOCK_( pCache ) ((void)(0))
  253. #elif defined(MDLCACHE_LEVEL_LOCKED)
  254. #define MDLCACHE_CRITICAL_SECTION_( pCache ) ((void)(0))
  255. #define MDLCACHE_COARSE_LOCK_( pCache ) ((void)(0))
  256. #else
  257. #define MDLCACHE_CRITICAL_SECTION_( pCache ) ((void)(0))
  258. #define MDLCACHE_COARSE_LOCK_( pCache ) CMDLCacheCriticalSection cacheCriticalSection(pCache)
  259. #endif
  260. #define MDLCACHE_CRITICAL_SECTION() MDLCACHE_CRITICAL_SECTION_(mdlcache)
  261. #define MDLCACHE_COARSE_LOCK() MDLCACHE_COARSE_LOCK_(mdlcache)
  262.  
  263. #endif // IMDLCACHE_H#pragma once
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement