resourceManager.cpp 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246
  1. //-----------------------------------------------------------------------------
  2. // Copyright (c) 2012 GarageGames, LLC
  3. //
  4. // Permission is hereby granted, free of charge, to any person obtaining a copy
  5. // of this software and associated documentation files (the "Software"), to
  6. // deal in the Software without restriction, including without limitation the
  7. // rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
  8. // sell copies of the Software, and to permit persons to whom the Software is
  9. // furnished to do so, subject to the following conditions:
  10. //
  11. // The above copyright notice and this permission notice shall be included in
  12. // all copies or substantial portions of the Software.
  13. //
  14. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  17. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  19. // FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
  20. // IN THE SOFTWARE.
  21. //-----------------------------------------------------------------------------
  22. #include "platform/platform.h"
  23. #include "core/resourceManager.h"
  24. #include "core/volume.h"
  25. #include "console/console.h"
  26. #include "core/util/autoPtr.h"
  27. #include "console/engineAPI.h"
  28. static AutoPtr< ResourceManager > smInstance;
  29. ResourceManager::ResourceManager()
  30. : mIterSigFilter( U32_MAX )
  31. {
  32. }
  33. ResourceManager::~ResourceManager()
  34. {
  35. // TODO: Dump resources that have not been released?
  36. }
  37. ResourceManager &ResourceManager::get()
  38. {
  39. if ( smInstance.isNull() )
  40. smInstance = new ResourceManager;
  41. return *smInstance;
  42. }
  43. ResourceBase ResourceManager::load(const Torque::Path &path)
  44. {
  45. #ifdef TORQUE_DEBUG_RES_MANAGER
  46. Con::printf( "ResourceManager::load : [%s]", path.getFullPath().c_str() );
  47. #endif
  48. ResourceHeaderMap::Iterator iter = mResourceHeaderMap.findOrInsert( path.getFullPath() );
  49. ResourceHeaderMap::Pair &pair = *iter;
  50. if ( pair.value == NULL )
  51. {
  52. pair.value = new ResourceBase::Header;
  53. // TODO: This can fail if the file doesn't exist
  54. // at all which is possible.
  55. //
  56. // The problem is the templated design in ResourceManager
  57. // keeps me from checking to see if the resource load failed
  58. // before adding a notification.
  59. //
  60. // IMO the resource manager is overly templateized and
  61. // we should refactor it so that its not so.
  62. //
  63. FS::AddChangeNotification( path, this, &ResourceManager::notifiedFileChanged );
  64. }
  65. ResourceBase::Header *header = pair.value;
  66. if (header->getSignature() == 0)
  67. header->mPath = path;
  68. return ResourceBase( header );
  69. }
  70. ResourceBase ResourceManager::find(const Torque::Path &path)
  71. {
  72. #ifdef TORQUE_DEBUG_RES_MANAGER
  73. Con::printf( "ResourceManager::find : [%s]", path.getFullPath().c_str() );
  74. #endif
  75. ResourceHeaderMap::Iterator iter = mResourceHeaderMap.find( path.getFullPath() );
  76. if ( iter == mResourceHeaderMap.end() )
  77. return ResourceBase();
  78. ResourceHeaderMap::Pair &pair = *iter;
  79. ResourceBase::Header *header = pair.value;
  80. return ResourceBase(header);
  81. }
  82. #ifdef TORQUE_DEBUG
  83. void ResourceManager::dumpToConsole()
  84. {
  85. const U32 numResources = mResourceHeaderMap.size();
  86. if ( numResources == 0 )
  87. {
  88. Con::printf( "ResourceManager is not managing any resources" );
  89. return;
  90. }
  91. Con::printf( "ResourceManager is managing %d resources:", numResources );
  92. Con::printf( " [ref count/signature/path]" );
  93. ResourceHeaderMap::Iterator iter;
  94. for( iter = mResourceHeaderMap.begin(); iter != mResourceHeaderMap.end(); ++iter )
  95. {
  96. ResourceBase::Header *header = (*iter).value;
  97. char fourCC[ 5 ];
  98. *( ( U32* ) fourCC ) = header->getSignature();
  99. fourCC[ 4 ] = 0;
  100. Con::printf( " %3d %s [%s] ", header->getRefCount(), fourCC, (*iter).key.c_str() );
  101. }
  102. }
  103. #endif
  104. bool ResourceManager::remove( ResourceBase::Header* header )
  105. {
  106. const Path &path = header->getPath();
  107. #ifdef TORQUE_DEBUG_RES_MANAGER
  108. Con::printf( "ResourceManager::remove : [%s]", path.getFullPath().c_str() );
  109. #endif
  110. ResourceHeaderMap::Iterator iter = mResourceHeaderMap.find( path.getFullPath() );
  111. if ( iter != mResourceHeaderMap.end() && iter->value == header )
  112. {
  113. AssertISV( header && (header->getRefCount() == 0), "ResourceManager error: trying to remove resource which is still in use." );
  114. mResourceHeaderMap.erase( iter );
  115. }
  116. else
  117. {
  118. iter = mPrevResourceHeaderMap.find( path.getFullPath() );
  119. if ( iter == mPrevResourceHeaderMap.end() || iter->value != header )
  120. {
  121. Con::errorf( "ResourceManager::remove : Trying to remove non-existent resource [%s]", path.getFullPath().c_str() );
  122. return false;
  123. }
  124. AssertISV( header && (header->getRefCount() == 0), "ResourceManager error: trying to remove resource which is still in use." );
  125. mPrevResourceHeaderMap.erase( iter );
  126. }
  127. FS::RemoveChangeNotification( path, this, &ResourceManager::notifiedFileChanged );
  128. return true;
  129. }
  130. void ResourceManager::notifiedFileChanged( const Torque::Path &path )
  131. {
  132. reloadResource( path, true );
  133. }
  134. void ResourceManager::reloadResource( const Torque::Path &path, bool showMessage )
  135. {
  136. if ( showMessage )
  137. Con::warnf( "[ResourceManager::notifiedFileChanged] : File changed [%s]", path.getFullPath().c_str() );
  138. ResourceHeaderMap::Iterator iter = mResourceHeaderMap.find( path.getFullPath() );
  139. if ( iter != mResourceHeaderMap.end() )
  140. {
  141. ResourceBase::Header *header = (*iter).value;
  142. mResourceHeaderMap.erase( iter );
  143. // Move the resource into the previous resource map.
  144. iter = mPrevResourceHeaderMap.findOrInsert( path );
  145. iter->value = header;
  146. }
  147. // Now notify users of the resource change so they
  148. // can release and reload.
  149. mChangeSignal.trigger( path );
  150. }
  151. ResourceBase ResourceManager::startResourceList( ResourceBase::Signature inSignature )
  152. {
  153. mIter = mResourceHeaderMap.begin();
  154. mIterSigFilter = inSignature;
  155. return nextResource();
  156. }
  157. ResourceBase ResourceManager::nextResource()
  158. {
  159. ResourceBase::Header *header = NULL;
  160. while( mIter != mResourceHeaderMap.end() )
  161. {
  162. header = (*mIter).value;
  163. ++mIter;
  164. if ( mIterSigFilter == U32_MAX )
  165. return ResourceBase(header);
  166. if ( header->getSignature() == mIterSigFilter )
  167. return ResourceBase(header);
  168. }
  169. return ResourceBase();
  170. }
  171. ConsoleFunctionGroupBegin(ResourceManagerFunctions, "Resource management functions.");
  172. #ifdef TORQUE_DEBUG
  173. ConsoleFunction(resourceDump, void, 1, 1, "()"
  174. "@brief List the currently managed resources\n\n"
  175. "Currently used by editors only, internal\n"
  176. "@ingroup Editors\n"
  177. "@internal")
  178. {
  179. ResourceManager::get().dumpToConsole();
  180. }
  181. #endif
  182. DefineEngineFunction( reloadResource, void, ( const char* path ),,
  183. "Force the resource at specified input path to be reloaded\n"
  184. "@param path Path to the resource to be reloaded\n\n"
  185. "@tsexample\n"
  186. "reloadResource( \"art/shapes/box.dts\" );\n"
  187. "@endtsexample\n\n"
  188. "@note Currently used by editors only\n"
  189. "@ingroup Editors\n"
  190. "@internal")
  191. {
  192. ResourceManager::get().reloadResource( path );
  193. }
  194. ConsoleFunctionGroupEnd( ResourceManagerFunctions );