resource.cpp

Engine/source/core/resource.cpp

More...

Detailed Description

  1
  2//-----------------------------------------------------------------------------
  3// Copyright (c) 2012 GarageGames, LLC
  4//
  5// Permission is hereby granted, free of charge, to any person obtaining a copy
  6// of this software and associated documentation files (the "Software"), to
  7// deal in the Software without restriction, including without limitation the
  8// rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
  9// sell copies of the Software, and to permit persons to whom the Software is
 10// furnished to do so, subject to the following conditions:
 11//
 12// The above copyright notice and this permission notice shall be included in
 13// all copies or substantial portions of the Software.
 14//
 15// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 16// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 17// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 18// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 19// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
 20// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
 21// IN THE SOFTWARE.
 22//-----------------------------------------------------------------------------
 23
 24#include "core/resourceManager.h"
 25#include "core/volume.h"
 26
 27#include "console/console.h"
 28
 29
 30FreeListChunker<ResourceHolderBase> ResourceHolderBase::smHolderFactory;
 31
 32ResourceBase::Header ResourceBase::smBlank;
 33
 34
 35U32   ResourceBase::Header::getChecksum() const
 36{
 37   Torque::FS::FileNodeRef fileRef = Torque::FS::GetFileNode( mPath );
 38
 39   if ( fileRef == NULL )
 40   {
 41      Con::errorf("ResourceBase::getChecksum could not access file: [%s]", mPath.getFullPath().c_str() );
 42      return 0;
 43   }
 44
 45   return fileRef->getChecksum();
 46}
 47
 48void ResourceBase::Header::destroySelf()
 49{
 50   if (this == &smBlank)
 51      return;
 52      
 53   if( mNotifyUnload )
 54      mNotifyUnload( getPath(), getResource() );
 55
 56   if ( mResource != NULL )
 57   {
 58      mResource->~ResourceHolderBase();
 59      ResourceHolderBase::smHolderFactory.free( mResource );
 60   }
 61
 62   ResourceManager::get().remove( this );
 63   delete this;
 64}
 65
 66void  ResourceBase::assign(const ResourceBase &inResource, void* resource)
 67{
 68   mResourceHeader = inResource.mResourceHeader;
 69
 70   if ( mResourceHeader == NULL || mResourceHeader.getPointer() == &(ResourceBase::smBlank) )
 71      return;
 72
 73   if (mResourceHeader->getSignature())
 74   {
 75      AssertFatal(inResource.mResourceHeader->getSignature() == getSignature(),"Resource::assign: mis-matching signature");
 76   }
 77   else
 78   {
 79      mResourceHeader->mSignature = getSignature();
 80
 81      const Torque::Path   path = mResourceHeader->getPath();
 82
 83      if (resource == NULL)
 84      {
 85         if ( !getStaticLoadSignal().trigger(path, &resource) && (resource != NULL) )
 86         {
 87            mResourceHeader->mResource = createHolder(resource);
 88            mResourceHeader->mNotifyUnload = _getNotifyUnloadFn();
 89            _triggerPostLoadSignal();
 90            return;
 91         }
 92
 93         resource = create(path);
 94      }
 95
 96      if (resource)
 97      {
 98         mResourceHeader->mResource = createHolder(resource);
 99         mResourceHeader->mNotifyUnload = _getNotifyUnloadFn();
100         _triggerPostLoadSignal();
101      }
102      else
103      {
104         // Failed to create...delete signature so we can attempt to successfully create resource later
105         Con::warnf("Failed to create resource: [%s]", path.getFullPath().c_str() );
106
107         mResourceHeader->mSignature = 0;
108      }
109   }
110}
111
112