static int ValidateOptions(AnyOption& options, String& file) { for( int i = 0; i < options.getArgc(); ++i ) { file = options.getArgv(i); break; } // If no file was passed then we have nothing to do. if( file.empty() ) { options.printAutoUsage(); return EXIT_FAILURE; } // Check that the file is a DLL. String extension = PathGetFileExtension(file); if( StringCompareInsensitive(extension, "dll") != 0 ) { LogError("The file is not a DLL"); return EXIT_FAILURE; } // Check if the file exists. if( !FileExists(file) ) { LogError("The file could not be found"); return EXIT_FAILURE; } return EXIT_SUCCESS; }
static Serializer* GetSerializerForStream(const Stream& stream) { const String& ext = PathGetFileExtension(stream.path); if(ext == "json") return SerializerCreateJSON(AllocatorGetHeap(), 0); else if(ext == "bin") return SerializerCreateBinary(AllocatorGetHeap(), 0); return SerializerCreateJSON(AllocatorGetHeap(), 0); }
NAMESPACE_RESOURCES_BEGIN //-----------------------------------// void ResourceTaskRun(Task* task) { ResourceLoadOptions* options = (ResourceLoadOptions*) task->userdata; Stream* stream = options->stream; Resource* resource = options->resource; const Path& path = resource->getPath(); ResourceManager* res = GetResourceManager(); ResourceLoader* loader = res->findLoader( PathGetFileExtension(path) ); bool decoded = loader->decode(*options); if( !decoded ) { resource->setStatus( ResourceStatus::Error ); LogWarn("Error decoding resource '%s'", path.CString()); goto cleanup; } resource->setStatus( ResourceStatus::Loaded ); LogInfo("Loaded resource '%s'", path.CString()); if( options->sendLoadEvent ) { ResourceEvent event; event.resource = resource; res->resourceEvents.push_back(event); } cleanup: res->numResourcesQueuedLoad.decrement(); res->resourceFinishLoad->wakeOne(); if( !options->keepStreamOpen ) Deallocate(stream); Deallocate(options); }
bool ResourceManager::validateResource( const Path& path ) { if( path.empty() ) return false; const Path& extension = PathGetFileExtension(path); if( extension.empty() ) { LogWarn( "Resource '%s' has an invalid extension", path.c_str() ); return false; } if( !findLoader(extension) ) { LogWarn("No resource loader found for resource '%s'", path.c_str()); return false; } return true; }
ResourceHandle ResourceManager::loadResource(ResourceLoadOptions& options) { if( !archive ) return ResourceHandle(HandleInvalid); Path fileExt = PathGetFileExtension(options.name); // If the file has no extension, search for one with the same // name but with known resource loadable file extensions. if(fileExt.empty() && !findResource(options)) { LogError("Could not find matching file for '%s'", options.name.c_str()); return ResourceHandle(HandleInvalid); } // Check if the resource is already loaded. ResourceHandle handle = getResource(options.name); if( handle ) return handle; if( !validateResource(options.name) ) return ResourceHandle(HandleInvalid); Resource* resource = prepareResource(options); if( !resource ) return ResourceHandle(HandleInvalid); handle = ResourceHandleCreate(resource); if(handle == HandleInvalid) return ResourceHandle(HandleInvalid); // Register the decoded resource in the map. Path base = PathGetFile(options.name); auto key = MurmurHash64(base.c_str(), base.size(), 0); resources.set(key, handle); decodeResource(options); return handle; }
Resource* ResourceManager::prepareResource( ResourceLoadOptions& options ) { const Path& path = options.name; Stream* stream = archive->openFile(path, GetResourcesAllocator()); if( !stream ) { LogWarn("Resource was not found: '%s'", path.c_str()); return nullptr; } const Path& file = PathGetFile(path); // Get the available resource loader and prepare the resource. ResourceLoader* loader = findLoader( PathGetFileExtension(file) ); if( !loader ) { LogWarn("No resource loader found for resource '%s'", file.c_str()); return nullptr; } options.stream = stream; Resource* resource = loader->prepare(options); if( !resource ) { LogError("Error preparing resource: '%s'", path.c_str()); return nullptr; } resource->setStatus( ResourceStatus::Loading ); resource->setPath( file ); options.resource = resource; return resource; }
bool ResourceManager::findResource(ResourceLoadOptions& options) { Path& path = options.name; for(auto it = resourceLoaders.begin(); it != resourceLoaders.end(); it++) { auto ext = PathGetFileExtension(options.name); auto loader = it->value.get(); if( loader->getResourceGroup() != options.group ) continue; Path newPath = StringFormat("%s.%s", path.c_str(), ext.c_str()); if (archive->existsFile(newPath)) { path = PathNormalize(newPath); return true; } } return false; }