resource.cpp 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  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 "core/resourceManager.h"
  23. #include "core/volume.h"
  24. #include "console/console.h"
  25. FreeListChunker<ResourceHolderBase> ResourceHolderBase::smHolderFactory;
  26. ResourceBase::Header ResourceBase::smBlank;
  27. U32 ResourceBase::Header::getChecksum() const
  28. {
  29. Torque::FS::FileNodeRef fileRef = Torque::FS::GetFileNode( mPath );
  30. if ( fileRef == NULL )
  31. {
  32. Con::errorf("ResourceBase::getChecksum could not access file: [%s]", mPath.getFullPath().c_str() );
  33. return 0;
  34. }
  35. return fileRef->getChecksum();
  36. }
  37. void ResourceBase::Header::destroySelf()
  38. {
  39. if (this == &smBlank)
  40. return;
  41. if( mNotifyUnload )
  42. mNotifyUnload( getPath(), getResource() );
  43. if ( mResource != NULL )
  44. {
  45. mResource->~ResourceHolderBase();
  46. ResourceHolderBase::smHolderFactory.free( mResource );
  47. }
  48. ResourceManager::get().remove( this );
  49. delete this;
  50. }
  51. void ResourceBase::assign(const ResourceBase &inResource, void* resource)
  52. {
  53. mResourceHeader = inResource.mResourceHeader;
  54. if ( mResourceHeader == NULL || mResourceHeader.getPointer() == &(ResourceBase::smBlank) )
  55. return;
  56. if (mResourceHeader->getSignature())
  57. {
  58. AssertFatal(inResource.mResourceHeader->getSignature() == getSignature(),"Resource::assign: mis-matching signature");
  59. }
  60. else
  61. {
  62. mResourceHeader->mSignature = getSignature();
  63. const Torque::Path path = mResourceHeader->getPath();
  64. if (resource == NULL)
  65. {
  66. if ( !getStaticLoadSignal().trigger(path, &resource) && (resource != NULL) )
  67. {
  68. mResourceHeader->mResource = createHolder(resource);
  69. mResourceHeader->mNotifyUnload = _getNotifyUnloadFn();
  70. _triggerPostLoadSignal();
  71. return;
  72. }
  73. resource = create(path);
  74. }
  75. if (resource)
  76. {
  77. mResourceHeader->mResource = createHolder(resource);
  78. mResourceHeader->mNotifyUnload = _getNotifyUnloadFn();
  79. _triggerPostLoadSignal();
  80. }
  81. else
  82. {
  83. // Failed to create...delete signature so we can attempt to successfully create resource later
  84. Con::warnf("Failed to create resource: [%s]", path.getFullPath().c_str() );
  85. mResourceHeader->mSignature = 0;
  86. }
  87. }
  88. }