resourceManager.cpp 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249
  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. using namespace Torque;
  29. static AutoPtr< ResourceManager > smInstance;
  30. ResourceManager::ResourceManager()
  31. : mIterSigFilter( U32_MAX )
  32. {
  33. }
  34. ResourceManager::~ResourceManager()
  35. {
  36. // TODO: Dump resources that have not been released?
  37. }
  38. ResourceManager &ResourceManager::get()
  39. {
  40. if ( smInstance.isNull() )
  41. smInstance = new ResourceManager;
  42. return *smInstance;
  43. }
  44. ResourceBase ResourceManager::load(const Torque::Path &path)
  45. {
  46. #ifdef TORQUE_DEBUG_RES_MANAGER
  47. Con::printf( "ResourceManager::load : [%s]", path.getFullPath().c_str() );
  48. #endif
  49. ResourceHeaderMap::Iterator iter = mResourceHeaderMap.findOrInsert( path.getFullPath() );
  50. ResourceHeaderMap::Pair &pair = *iter;
  51. if ( pair.value == NULL )
  52. {
  53. pair.value = new ResourceBase::Header;
  54. // TODO: This can fail if the file doesn't exist
  55. // at all which is possible.
  56. //
  57. // The problem is the templated design in ResourceManager
  58. // keeps me from checking to see if the resource load failed
  59. // before adding a notification.
  60. //
  61. // IMO the resource manager is overly templateized and
  62. // we should refactor it so that its not so.
  63. //
  64. FS::AddChangeNotification( path, this, &ResourceManager::notifiedFileChanged );
  65. }
  66. ResourceBase::Header *header = pair.value;
  67. if (header->getSignature() == 0)
  68. header->mPath = path;
  69. return ResourceBase( header );
  70. }
  71. ResourceBase ResourceManager::find(const Torque::Path &path)
  72. {
  73. #ifdef TORQUE_DEBUG_RES_MANAGER
  74. Con::printf( "ResourceManager::find : [%s]", path.getFullPath().c_str() );
  75. #endif
  76. ResourceHeaderMap::Iterator iter = mResourceHeaderMap.find( path.getFullPath() );
  77. if ( iter == mResourceHeaderMap.end() )
  78. return ResourceBase();
  79. ResourceHeaderMap::Pair &pair = *iter;
  80. ResourceBase::Header *header = pair.value;
  81. return ResourceBase(header);
  82. }
  83. #ifdef TORQUE_DEBUG
  84. void ResourceManager::dumpToConsole()
  85. {
  86. const U32 numResources = mResourceHeaderMap.size();
  87. if ( numResources == 0 )
  88. {
  89. Con::printf( "ResourceManager is not managing any resources" );
  90. return;
  91. }
  92. Con::printf( "ResourceManager is managing %d resources:", numResources );
  93. Con::printf( " [ref count/signature/path]" );
  94. ResourceHeaderMap::Iterator iter;
  95. for( iter = mResourceHeaderMap.begin(); iter != mResourceHeaderMap.end(); ++iter )
  96. {
  97. ResourceBase::Header *header = (*iter).value;
  98. char fourCC[ 5 ];
  99. *( ( U32* ) fourCC ) = header->getSignature();
  100. fourCC[ 4 ] = 0;
  101. Con::printf( " %3d %s [%s] ", header->getRefCount(), fourCC, (*iter).key.c_str() );
  102. }
  103. }
  104. #endif
  105. bool ResourceManager::remove( ResourceBase::Header* header )
  106. {
  107. const Path &path = header->getPath();
  108. #ifdef TORQUE_DEBUG_RES_MANAGER
  109. Con::printf( "ResourceManager::remove : [%s]", path.getFullPath().c_str() );
  110. #endif
  111. ResourceHeaderMap::Iterator iter = mResourceHeaderMap.find( path.getFullPath() );
  112. if ( iter != mResourceHeaderMap.end() && iter->value == header )
  113. {
  114. AssertISV( header && (header->getRefCount() == 0), "ResourceManager error: trying to remove resource which is still in use." );
  115. mResourceHeaderMap.erase( iter );
  116. }
  117. else
  118. {
  119. iter = mPrevResourceHeaderMap.find( path.getFullPath() );
  120. if ( iter == mPrevResourceHeaderMap.end() || iter->value != header )
  121. {
  122. Con::errorf( "ResourceManager::remove : Trying to remove non-existent resource [%s]", path.getFullPath().c_str() );
  123. return false;
  124. }
  125. AssertISV( header && (header->getRefCount() == 0), "ResourceManager error: trying to remove resource which is still in use." );
  126. mPrevResourceHeaderMap.erase( iter );
  127. }
  128. FS::RemoveChangeNotification( path, this, &ResourceManager::notifiedFileChanged );
  129. return true;
  130. }
  131. void ResourceManager::notifiedFileChanged( const Torque::Path &path )
  132. {
  133. reloadResource( path, true );
  134. }
  135. void ResourceManager::reloadResource( const Torque::Path &path, bool showMessage )
  136. {
  137. if ( showMessage )
  138. Con::warnf( "[ResourceManager::notifiedFileChanged] : File changed [%s]", path.getFullPath().c_str() );
  139. ResourceHeaderMap::Iterator iter = mResourceHeaderMap.find( path.getFullPath() );
  140. if ( iter != mResourceHeaderMap.end() )
  141. {
  142. ResourceBase::Header *header = (*iter).value;
  143. mResourceHeaderMap.erase( iter );
  144. // Move the resource into the previous resource map.
  145. iter = mPrevResourceHeaderMap.findOrInsert( path );
  146. iter->value = header;
  147. }
  148. // Now notify users of the resource change so they
  149. // can release and reload.
  150. mChangeSignal.trigger( path );
  151. }
  152. ResourceBase ResourceManager::startResourceList( ResourceBase::Signature inSignature )
  153. {
  154. mIter = mResourceHeaderMap.begin();
  155. mIterSigFilter = inSignature;
  156. return nextResource();
  157. }
  158. ResourceBase ResourceManager::nextResource()
  159. {
  160. ResourceBase::Header *header = NULL;
  161. while( mIter != mResourceHeaderMap.end() )
  162. {
  163. header = (*mIter).value;
  164. ++mIter;
  165. if ( mIterSigFilter == U32_MAX )
  166. return ResourceBase(header);
  167. if ( header->getSignature() == mIterSigFilter )
  168. return ResourceBase(header);
  169. }
  170. return ResourceBase();
  171. }
  172. ConsoleFunctionGroupBegin(ResourceManagerFunctions, "Resource management functions.");
  173. DefineEngineFunction(resourceDump, void, (),,
  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. #ifdef TORQUE_DEBUG
  180. ResourceManager::get().dumpToConsole();
  181. #endif
  182. }
  183. DefineEngineFunction( reloadResource, void, ( const char* path ),,
  184. "Force the resource at specified input path to be reloaded\n"
  185. "@param path Path to the resource to be reloaded\n\n"
  186. "@tsexample\n"
  187. "reloadResource( \"art/shapes/box.dts\" );\n"
  188. "@endtsexample\n\n"
  189. "@note Currently used by editors only\n"
  190. "@ingroup Editors\n"
  191. "@internal")
  192. {
  193. ResourceManager::get().reloadResource( path );
  194. }
  195. ConsoleFunctionGroupEnd( ResourceManagerFunctions );