////////////////////////////////////////////////////////////////////////// // internalCreateResource BcBool CsCore::internalCreateResource( const BcName& Name, const BcName& Type, CsResourceRef<>& Handle ) { // Generate a unique name for the resource. BcName UniqueName = Name.isValid() ? Name.getUnique() : Type.getUnique(); // Allocate resource with a unique name. Handle = allocResource( UniqueName, Type, NULL ); // Put into create list. if( Handle.isValid() ) { BcScopedLock< BcMutex > Lock( ContainerLock_ ); CreateResources_.push_back( Handle ); } return Handle.isValid(); }
////////////////////////////////////////////////////////////////////////// // internalRequestResource BcBool CsCore::internalRequestResource( const BcName& Package, const BcName& Name, const BcName& Type, CsResourceRef<>& Handle ) { // Find package CsPackage* pPackage = findPackage( Package ); if( pPackage || Package == BcName::NONE ) { // If we can't find resource, throw an error. if( internalFindResource( Package, Name, Type, Handle ) == BcFalse ) { BcPrintf( "CsCore::requestResource: Resource not availible \"%s.%s:%s\" requested.\n", (*Package).c_str(), (*Name).c_str(), (*Type).c_str() ); } } else { BcPrintf( "CsCore::requestResource: Invalid package \"%s\" requested.\n", (*Package).c_str() ); } return Handle.isValid(); }
////////////////////////////////////////////////////////////////////////// // internalRequestResource BcBool CsCore::internalRequestResource( const BcName& Name, const BcName& Type, CsResourceRef<>& Handle ) { #if PSY_SERVER // Attempt to import on request if need be. BcPath FileName = getResourceFullName( Name, Type ); if( shouldImportResource( FileName, BcFalse ) ) { if( internalImportResource( FileName, Handle, NULL, BcFalse ) ) { return Handle.isValid(); } } #endif // Try to find resource, if we can't, allocate a new one and put into create list. if( internalFindResource( Name, Type, Handle ) == BcFalse ) { // Only request if we have a name. if( Name.isValid() ) { // Create a file reader for resource (using full name!) CsFile* pFile = createFileReader( getResourceFullName( Name, Type ) ); // Allocate resource. Handle = allocResource( Name, Type, pFile ); if( Handle.isValid() ) { // Call default initialiser. Handle->initialise(); // Acquire (callback from load will release). Handle->acquire(); // Trigger a file load. if( pFile->load( CsFileReadyDelegate::bind< CsResource, &CsResource::delegateFileReady >( (CsResource*)Handle ), CsFileChunkDelegate::bind< CsResource, &CsResource::delegateFileChunkReady >( (CsResource*)Handle ) ) ) { // Put into create list. if( Handle.isValid() ) { BcScopedLock< BcMutex > Lock( ContainerLock_ ); CreateResources_.push_back( Handle ); } } else { BcPrintf( "CsCore::requestResource: Failed to load %s (%s).\n", (*Name).c_str(), pFile->getName().c_str() ); // Release (callback from load won't happen on failure). Handle->release(); Handle = NULL; } } else { BcPrintf( "CsCore::requestResource: Failed to create %s (%s).\n", (*Name).c_str(), pFile->getName().c_str() ); Handle = NULL; } } else { BcPrintf( "CsCore::requestResource: Resource name invalid.\n" ); Handle = NULL; } } return Handle.isValid(); }
////////////////////////////////////////////////////////////////////////// // internalImportObject BcBool CsCore::internalImportObject( const Json::Value& Object, CsResourceRef<>& Handle, CsDependancyList* pDependancyList, BcBool ForceImport ) { BcScopedLock< BcMutex > Lock( ContainerLock_ ); // If we pass in a string value, call into the importResource that takes a file name. // If we pass in an object value, load it from that. if( Object.type() == Json::stringValue ) { // We don't pass the dependancy list in if it's a file. return internalImportResource( Object.asString(), Handle, NULL, ForceImport ); } else if( Object.type() == Json::objectValue ) { Json::Value::Members Members = Object.getMemberNames(); if( Members.size() == 1 ) { const std::string& Type = Members[ 0 ]; const Json::Value& Resource = Object[ Type ]; // If the resource is a string, it's an alias - so we need to load the appropriate file. // If the resource is an object, then do appropriate loading. if( Resource.type() == Json::stringValue ) { return internalImportResource( Resource.asString(), Handle, pDependancyList, ForceImport ); } else if( Resource.type() == Json::objectValue ) { // Check the resource has a name, and it's a string. if( Resource.isMember( "name" ) && Resource[ "name" ].type() == Json::stringValue ) { // Import resource by string. const std::string& Name = Resource[ "name" ].asString(); // Check name is a valid length. // TODO: Check it only contains valid characters. if( Name.length() > 0 ) { // Create a file writer for resource (using full name!) CsFile* pFile = createFileWriter( getResourceFullName( Name, Type ) ); if( pFile != NULL ) { // Add name as first string. pFile->addString( Name.c_str() ); // Allocate resource. Handle = allocResource( Name, Type, pFile ); if( Handle.isValid() ) { // Import the resource immediately (blocking operation). if( Handle->import( Resource, *pDependancyList ) ) { BcBool Success = pFile->save(); BcAssertMsg( Success, "Failed to save out \"%s\"", getResourceFullName( Name, Type ).c_str() ); if( Success ) { // Now we need to request the resource, this will delete the imported // one and replace it with a valid loaded resource. internalRequestResource( Name, Type, Handle ); } } else { // Failed to import, so set handle to NULL to release it. Handle = NULL; } } else { delete pFile; BcPrintf( "CsCore: Can not allocate resource type %s.\n", Type.c_str() ); } } else { BcPrintf( "CsCore: Can not allocate file writer.\n" ); } } else { BcPrintf( "CsCore: Json object does not contain valid name.\n" ); } } else { BcPrintf( "CsCore: Json object does not contain name.\n" ); } } else { BcPrintf( "CsCore: Json object is neither an alias or valid resource.\n" ); } } else { BcPrintf( "CsCore: Json object contains more than 1 element.\n" ); } } else { BcPrintf( "CsCore: Invalid Json object.\n" ); } return Handle.isValid(); }