const void * ResCache::Load(const Resource & r) { // Note: Change Post-printing // // Graphic resources are loaded differently, since the tool that created them (Ipac) // added some data to grab any sequence of frames - not just the whole thing. // // Added an optional<int> &len in case we wanted to return the // length of the resource in bytes. int size = m_file->VGetResourceSize(r); char *buffer = Allocate(size); if (buffer==NULL) { return NULL; // ResCache is out of memory! } memset(buffer,0,sizeof(buffer)); // Create a new resource and add it to the lru list and map ResHandle *handle = SAFE_NEW ResHandle(r, buffer); m_lru.push_front(handle); m_resources[r.m_name] = handle; m_file->VGetResource(r, buffer); return buffer; }
//************************************ // Method: VCreateHandle // FullName: Resource::VCreateHandle // Access: virtual public // Returns: ResHandle * // Qualifier: // Parameter: const char * buffer // Parameter: unsigned int size // Parameter: ResCache * pResCache //************************************ ResHandle *Resource::VCreateHandle(const char *buffer, unsigned int size, ResCache *pResCache) { return DEBUG_CLIENTBLOCK ResHandle(*this, (char*)buffer, size, pResCache); }
ResHandle* ResCache::Load(Resource* resource, IResourceFile *file) { // Create a new resource and add it to the lru list and map IResourceLoader* loader; ResHandle* handle; for (ResourceLoaders::iterator it = m_resourceLoaders.begin(); it != m_resourceLoaders.end(); ++it) { IResourceLoader* testLoader = *it; if (WildcardMatch(testLoader->VGetPattern().c_str(), resource->m_name.c_str())) { loader = testLoader; break; } } if (!loader) { //GCC_ASSERT(loader && _T("Default resource loader not found!")); return handle; // Resource not loaded! } int rawSize = m_file->GetRawResourceSize(*resource); if (rawSize < 0) { //GCC_ASSERT(rawSize > 0 && "Resource size returned -1 - Resource not found"); return nullptr; } int allocSize = rawSize + ((loader->VAddNullZero()) ? (1) : (0)); char* rawBuffer; if (loader->VUseRawFile()) { rawBuffer = Allocate(allocSize); } else { rawBuffer = NEW char[allocSize]; } memset(rawBuffer, 0, allocSize); if (rawBuffer == NULL || m_file->GetRawResource(*resource, rawBuffer) == 0) { // resource cache out of memory return nullptr; } char* buffer = NULL; unsigned int size = 0; if (loader->VUseRawFile()) { buffer = rawBuffer; handle = NEW ResHandle(*resource, buffer, rawSize, this); } else { size = loader->VGetLoadedResourceSize(rawBuffer, rawSize); buffer = Allocate(size); if (rawBuffer == NULL || buffer == NULL) { // resource cache out of memory return nullptr; } handle = NEW ResHandle(*resource, buffer, size, this); bool success = loader->VLoadResource(rawBuffer, rawSize, handle); if (loader->VDiscardRawBufferAfterLoad()) { SAFE_DELETE_ARRAY(rawBuffer); } if (!success) { // resource cache out of memory return nullptr; } } if (handle) { m_lruResources.push_front(handle); m_resources[resource->m_name] = handle; } //JB_ASSERT(loader && _T("Default resource loader not found!")); return handle; // ResCache is out of memory! }
// // ResCache::Load - Chapter 8, page 228-230 // shared_ptr<ResHandle> ResCache::Load(Resource *r) { // Create a new resource and add it to the lru list and map shared_ptr<IResourceLoader> loader; shared_ptr<ResHandle> handle; for (ResourceLoaders::iterator it = m_resourceLoaders.begin(); it != m_resourceLoaders.end(); ++it) { shared_ptr<IResourceLoader> testLoader = *it; if (WildcardMatch(testLoader->VGetPattern().c_str(), r->m_name.c_str())) { loader = testLoader; break; } } if (!loader) { GCC_ASSERT(loader && _T("Default resource loader not found!")); return handle; // Resource not loaded! } int rawSize = m_file->VGetRawResourceSize(*r); if (rawSize < 0) { GCC_ASSERT(rawSize > 0 && "Resource size returned -1 - Resource not found"); return shared_ptr<ResHandle>(); } int allocSize = rawSize + ((loader->VAddNullZero()) ? (1) : (0)); char *rawBuffer = loader->VUseRawFile() ? Allocate(allocSize) : GCC_NEW char[allocSize]; memset(rawBuffer, 0, allocSize); if (rawBuffer==NULL || m_file->VGetRawResource(*r, rawBuffer)==0) { // resource cache out of memory return shared_ptr<ResHandle>(); } char *buffer = NULL; unsigned int size = 0; if (loader->VUseRawFile()) { buffer = rawBuffer; handle = shared_ptr<ResHandle>(GCC_NEW ResHandle(*r, buffer, rawSize, this)); } else { size = loader->VGetLoadedResourceSize(rawBuffer, rawSize); buffer = Allocate(size); if (rawBuffer==NULL || buffer==NULL) { // resource cache out of memory return shared_ptr<ResHandle>(); } handle = shared_ptr<ResHandle>(GCC_NEW ResHandle(*r, buffer, size, this)); bool success = loader->VLoadResource(rawBuffer, rawSize, handle); // [mrmike] - This was added after the chapter went to copy edit. It is used for those // resoruces that are converted to a useable format upon load, such as a compressed // file. If the raw buffer from the resource file isn't needed, it shouldn't take up // any additional memory, so we release it. // if (loader->VDiscardRawBufferAfterLoad()) { SAFE_DELETE_ARRAY(rawBuffer); } if (!success) { // resource cache out of memory return shared_ptr<ResHandle>(); } } if (handle) { m_lru.push_front(handle); m_resources[r->m_name] = handle; } GCC_ASSERT(loader && _T("Default resource loader not found!")); return handle; // ResCache is out of memory! }