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.

158 lines
5.4 KiB

  1. //========= Copyright Valve Corporation, All rights reserved. ============//
  2. //
  3. // Purpose:
  4. //
  5. //===========================================================================//
  6. #ifndef QUEUEDLOADER_H
  7. #define QUEUEDLOADER_H
  8. #ifdef _WIN32
  9. #pragma once
  10. #endif
  11. #include "tier0/platform.h"
  12. #include "appframework/IAppSystem.h"
  13. class CFunctor;
  14. enum LoaderError_t
  15. {
  16. LOADERERROR_NONE = 0,
  17. LOADERERROR_FILEOPEN = -1,
  18. LOADERERROR_READING = -2,
  19. };
  20. enum LoaderPriority_t
  21. {
  22. LOADERPRIORITY_ANYTIME = 0, // low priority, job can finish during gameplay
  23. LOADERPRIORITY_BEFOREPLAY = 1, // job must complete before load ends
  24. LOADERPRIORITY_DURINGPRELOAD = 2, // job must be complete during preload phase
  25. };
  26. typedef void ( *QueuedLoaderCallback_t )( void *pContext, void *pContext2, const void *pData, int nSize, LoaderError_t loaderError );
  27. typedef void ( *DynamicResourceCallback_t )( const char *pFilename, void *pContext, void *pContext2 );
  28. struct LoaderJob_t
  29. {
  30. LoaderJob_t()
  31. {
  32. memset( this, 0, sizeof( *this ) );
  33. }
  34. const char *m_pFilename; // path to resource
  35. const char *m_pPathID; // optional, can be NULL
  36. QueuedLoaderCallback_t m_pCallback; // called at i/o delivery
  37. void *m_pContext; // caller provided data
  38. void *m_pContext2; // caller provided data
  39. void *m_pTargetData; // optional, caller provided target buffer
  40. int m_nBytesToRead; // optional read clamp, otherwise 0
  41. unsigned int m_nStartOffset; // optional start offset, otherwise 0
  42. LoaderPriority_t m_Priority; // data must arrive by specified interval
  43. bool m_bPersistTargetData; // caller wants ownership of i/o buffer
  44. };
  45. enum ResourcePreload_t
  46. {
  47. RESOURCEPRELOAD_UNKNOWN,
  48. RESOURCEPRELOAD_SOUND,
  49. RESOURCEPRELOAD_MATERIAL,
  50. RESOURCEPRELOAD_MODEL,
  51. RESOURCEPRELOAD_CUBEMAP,
  52. RESOURCEPRELOAD_STATICPROPLIGHTING,
  53. RESOURCEPRELOAD_ANONYMOUS,
  54. RESOURCEPRELOAD_COUNT
  55. };
  56. abstract_class IResourcePreload
  57. {
  58. public:
  59. // Called during preload phase for ALL the resources expected by the level.
  60. // Caller should not do i/o but generate AddJob() requests. Resources that already exist
  61. // and are not referenced by this function would be candidates for purge.
  62. virtual bool CreateResource( const char *pName ) = 0;
  63. // Sent as an event hint during preload, that creation has completed, AddJob() i/o is about to commence.
  64. // Caller should purge any unreferenced resources before the AddJobs are performed.
  65. // "Must Complete" data will be guaranteed finished, at preload conclusion, before the normal load phase commences.
  66. virtual void PurgeUnreferencedResources() = 0;
  67. // Sent as an event hint that gameplay rendering is imminent.
  68. // Low priority jobs may still be in async flight.
  69. virtual void OnEndMapLoading( bool bAbort ) = 0;
  70. virtual void PurgeAll() = 0;
  71. };
  72. // Default implementation
  73. class CResourcePreload : public IResourcePreload
  74. {
  75. void PurgeUnreferencedResources() {}
  76. void OnEndMapLoading( bool bAbort ) {}
  77. void PurgeAll() {}
  78. };
  79. // UI can install progress notification
  80. abstract_class ILoaderProgress
  81. {
  82. public:
  83. // implementation must ignore UpdateProgress() if not scoped by Begin/End
  84. virtual void BeginProgress() = 0;
  85. virtual void EndProgress() = 0;
  86. virtual void UpdateProgress( float progress ) = 0;
  87. };
  88. // spew detail
  89. #define LOADER_DETAIL_NONE 0
  90. #define LOADER_DETAIL_TIMING (1<<0)
  91. #define LOADER_DETAIL_COMPLETIONS (1<<1)
  92. #define LOADER_DETAIL_LATECOMPLETIONS (1<<2)
  93. #define LOADER_DETAIL_PURGES (1<<3)
  94. #define QUEUEDLOADER_INTERFACE_VERSION "QueuedLoaderVersion004"
  95. abstract_class IQueuedLoader : public IAppSystem
  96. {
  97. public:
  98. virtual void InstallLoader( ResourcePreload_t type, IResourcePreload *pLoader ) = 0;
  99. virtual void InstallProgress( ILoaderProgress *pProgress ) = 0;
  100. // Set bOptimizeReload if you want appropriate data (such as static prop lighting)
  101. // to persist - rather than being purged and reloaded - when going from map A to map A.
  102. virtual bool BeginMapLoading( const char *pMapName, bool bLoadForHDR, bool bOptimizeMapReload ) = 0;
  103. virtual void EndMapLoading( bool bAbort ) = 0;
  104. virtual bool AddJob( const LoaderJob_t *pLoaderJob ) = 0;
  105. // injects a resource into the map's reslist, rejected if not understood
  106. virtual void AddMapResource( const char *pFilename ) = 0;
  107. // dynamically load a map resource
  108. virtual void DynamicLoadMapResource( const char *pFilename, DynamicResourceCallback_t pCallback, void *pContext, void *pContext2 ) = 0;
  109. virtual void QueueDynamicLoadFunctor( CFunctor* pFunctor ) = 0;
  110. virtual bool CompleteDynamicLoad() = 0;
  111. virtual void QueueCleanupDynamicLoadFunctor( CFunctor* pFunctor ) = 0;
  112. virtual bool CleanupDynamicLoad() = 0;
  113. // callback is asynchronous
  114. virtual bool ClaimAnonymousJob( const char *pFilename, QueuedLoaderCallback_t pCallback, void *pContext, void *pContext2 = NULL ) = 0;
  115. // provides data if loaded, caller owns data
  116. virtual bool ClaimAnonymousJob( const char *pFilename, void **pData, int *pDataSize, LoaderError_t *pError = NULL ) = 0;
  117. virtual bool IsMapLoading() const = 0;
  118. virtual bool IsSameMapLoading() const = 0;
  119. virtual bool IsFinished() const = 0;
  120. // callers can expect that jobs are not immediately started when batching
  121. virtual bool IsBatching() const = 0;
  122. virtual bool IsDynamic() const = 0;
  123. // callers can conditionalize operational spew
  124. virtual int GetSpewDetail() const = 0;
  125. virtual void PurgeAll() = 0;
  126. };
  127. extern IQueuedLoader *g_pQueuedLoader;
  128. #endif // QUEUEDLOADER_H