KDbool AssetsManager::downLoad ( KDvoid )
{
    // Create a file to save package.
	std::string  sOutFileName = m_sStoragePath + TEMP_PACKAGE_FILE_NAME;
    KDFile*  pFile = kdFopen ( sOutFileName.c_str ( ), "wb" );
    if ( !pFile )
    {
        CCLOG ( "can not create file %s", sOutFileName.c_str ( ) );
        return KD_FALSE;
    }
    
    // Download pacakge
    CURLcode   eRes;
    curl_easy_setopt ( m_pCurl, CURLOPT_URL, m_sPackageUrl.c_str ( ) );
    curl_easy_setopt ( m_pCurl, CURLOPT_WRITEFUNCTION, downLoadPackage );
    curl_easy_setopt ( m_pCurl, CURLOPT_WRITEDATA, pFile );
    curl_easy_setopt ( m_pCurl, CURLOPT_NOPROGRESS, KD_FALSE );
    curl_easy_setopt ( m_pCurl, CURLOPT_PROGRESSFUNCTION, progressFunc );
    eRes = curl_easy_perform ( m_pCurl );
    curl_easy_cleanup ( m_pCurl );
    if ( eRes != 0 )
    {
        CCLOG ( "error when download package" );
        kdFclose ( pFile );
        return KD_FALSE;
    }
    
    CCLOG ( "succeed downloading package %s", m_sPackageUrl.c_str ( ) );
    
    kdFclose ( pFile );
    return KD_TRUE;
}
void  RBaseStream::close()
{
    if( m_file )
    {
        kdFclose( m_file );
        m_file = 0;
    }
    m_is_opened = false;
    if( !m_allocated )
        m_start = m_end = m_current = 0;
}
void  WBaseStream::close()
{
    if( m_is_opened )
        writeBlock();
    if( m_file )
    {
        kdFclose( m_file );
        m_file = 0;
    }
    m_buf = 0;
    m_is_opened = false;
}
Exemple #4
0
/* kdGetImageATX, kdGetImageFromStreamATX: Read and decode an image from a file or stream, returning a decoded image object. */
KD_API KDImageATX KD_APIENTRY kdGetImageATX(const KDchar *pathname, KDint format, KDint flags)
{
    KDFile *file = kdFopen(pathname, "rb");
    if(file == KD_NULL)
    {
        kdSetError(KD_EIO);
        return KD_NULL;
    }
    KDImageATX image = kdGetImageFromStreamATX(file, format, flags);
    kdFclose(file);
    return image;
}
KDvoid TestLocation::onExit ( KDvoid )
{
	if ( m_pFile )
	{
		kdFclose ( m_pFile );
		m_pFile = KD_NULL;
	}

	xmStopGPSLog ( );

	TestBasic::onExit ( );
}
KDvoid TestLocation::onReadFile ( KDfloat fDelta )
{
	static KDchar  szBuffer [ 1024 ];

	if ( m_pFile )
	{
		KDsize  uReadBytes = kdFread ( (KDvoid*) szBuffer, 1, 1024, m_pFile );
		if ( uReadBytes > 0 )
		{
			xmParseNMEA ( KD_FALSE, szBuffer, uReadBytes );
		}
		else
		{
			kdFclose ( m_pFile );
			m_pFile = KD_NULL;
		}
	}
}
KDbool Image::initWithStringShadowStroke (	const KDchar*	szText,
											KDint           nWidth,
											KDint           nHeight,
											TextAlign       eAlignMask,
											const KDchar*	szFontName,
											KDint			nSize,
											KDfloat			fTextTintR,
											KDfloat			fTextTintG,
											KDfloat			fTextTintB,
											KDbool			bShadow,
											KDfloat			fShadowOffsetX,
											KDfloat			fShadowOffsetY,
											KDfloat			fShadowOpacity,
											KDfloat			fShadowBlur,
											KDbool			bStroke,
											KDfloat			fStrokeR,
											KDfloat			fStrokeG,
											KDfloat			fStrokeB,
											KDfloat			fStrokeSize )
{
	KDbool		bRet = KD_FALSE;

    KDFile*		pFile = kdFopen ( FileUtils::getInstance ( )->fullPathForFilename ( szFontName ).c_str( ), "r" );
	if ( pFile )
	{
		kdFclose ( pFile );
		bRet = this->initWithFreeTypeFont ( szText, nWidth, nHeight, eAlignMask, szFontName, nSize );
	}
	#if CC_TARGET_PLATFORM == CC_PLATFORM_ANDROID || CC_TARGET_PLATFORM == CC_PLATFORM_WIN32 || CC_TARGET_PLATFORM == CC_PLATFORM_IOS
	else
	{
		bRet = this->initWithPlatformFont
		(
			szText, nWidth, nHeight, eAlignMask, szFontName, nSize, 
			fTextTintR, fTextTintG, fTextTintB, 
			bShadow, fShadowOffsetX, fShadowOffsetY, fShadowOpacity, fShadowBlur,
			bStroke, fStrokeR, fStrokeG, fStrokeB, fStrokeSize
		);
	}
	#endif

	return bRet;
}
KDbool Image::initWithImageData ( const KDubyte* pData, KDint32 nDataLen )
{
	KDbool	bRet = false;

	do
	{
		CC_BREAK_IF ( !pData || nDataLen <= 0 );

		KDubyte*	pUnpackedData = nullptr;
		KDint		nUnpackedLen = 0;

		// detecgt and unzip the compress file
		if ( ZipUtils::isCCZBuffer ( pData, nDataLen ) )
		{
			nUnpackedLen = ZipUtils::inflateCCZBuffer ( pData, nDataLen, &pUnpackedData );
		}
		else if ( ZipUtils::isGZipBuffer ( pData, nDataLen ) )
		{
			nUnpackedLen = ZipUtils::inflateMemory ( const_cast<KDubyte*> ( pData ), nDataLen, &pUnpackedData );
		}
		else
		{
			pUnpackedData = const_cast<KDubyte*> ( pData );
			nUnpackedLen = nDataLen;
		}

		KDFile*		pFile   = kdFmemopen ( pUnpackedData, nUnpackedLen, "rb" );
		KDoff		uOffset = kdFtell ( pFile );
		KDImageATX	pImage  = KD_NULL;

		do 
		{
			Texture2D::PixelFormat  eFormat = Texture2D::getDefaultAlphaPixelFormat ( );
			
			if ( eFormat == Texture2D::PixelFormat::AUTO )
			{			
				pImage = kdGetImageInfoFromStreamATX ( pFile );
				CC_BREAK_IF ( !pImage );

				KDint  nFormat = kdGetImageIntATX ( pImage, KD_IMAGE_FORMAT_ATX );
				KDint  nBpp    = kdGetImageIntATX ( pImage, KD_IMAGE_BITSPERPIXEL_ATX );
				KDint  nAlpha  = kdGetImageIntATX ( pImage, KD_IMAGE_ALPHA_ATX );
				KDint  nType   = kdGetImageIntATX ( pImage, KD_IMAGE_TYPE );

				m_eFileType = (Format) nType;
		
				kdFreeImageATX ( pImage );
				kdFseek ( pFile, uOffset, KD_SEEK_SET );

				switch ( nFormat )
				{
					case KD_IMAGE_FORMAT_COMPRESSED_ATX :	
						if ( ( nType == KD_IMAGE_TYPE_PVR   && !Configuration::getInstance ( )->supportsPVRTC ( ) ) ||
							 ( nType == KD_IMAGE_TYPE_S3TC  && !Configuration::getInstance ( )->supportsS3TC  ( ) ) ||
							 ( nType == KD_IMAGE_TYPE_ATITC && !Configuration::getInstance ( )->supportsATITC ( ) ) ||
							 ( nType == KD_IMAGE_TYPE_ETC   && !Configuration::getInstance ( )->supportsETC   ( ) ) )
						{
							eFormat = Texture2D::PixelFormat::RGBA8888;
						}
						else
						{
							eFormat = Texture2D::PixelFormat::COMPRESSED;
						}
						break;

					case KD_IMAGE_FORMAT_ALPHA8_ATX:
						eFormat = Texture2D::PixelFormat::A8;
						break;

					case KD_IMAGE_FORMAT_LUMINANCE_ATX:
						eFormat = Texture2D::PixelFormat::I8;
						break;

					case KD_IMAGE_FORMAT_LUMALPHA_ATX:
						eFormat = Texture2D::PixelFormat::AI88;
						break;

					case KD_IMAGE_FORMAT_PALETTED_ATX:
						eFormat = Texture2D::PixelFormat::RGB5A1;
						break;

					case KD_IMAGE_FORMAT_RGB_ATX:
						if ( nBpp == 24 || nBpp == 48 )
						{
							eFormat = Texture2D::PixelFormat::RGB888;
						}
						else
						{
							eFormat = Texture2D::PixelFormat::RGB565;
						}						
						break;

					case KD_IMAGE_FORMAT_RGBA_ATX:
						if ( nBpp == 16 )
						{
							eFormat = ( nAlpha == 4 ) ? Texture2D::PixelFormat::RGBA4444 : Texture2D::PixelFormat::RGB5A1;
						}
						else
						{
							eFormat = Texture2D::PixelFormat::RGBA8888;
						}
						break;

					default:
						break; break;
				}
			}
	
			pImage = kdGetImageFromStreamATX ( pFile, (KDint) eFormat, KD_IMAGE_FLAG_PREMULTIPLIED_ALPHA );
			CC_BREAK_IF ( !pImage );
			
			m_eRenderFormat				= (Texture2D::PixelFormat) kdGetImageIntATX ( pImage, KD_IMAGE_FORMAT_ATX );
			m_nWidth					= kdGetImageIntATX ( pImage, KD_IMAGE_WIDTH_ATX );
			m_nHeight					= kdGetImageIntATX ( pImage, KD_IMAGE_HEIGHT_ATX );
			m_bHasPremultipliedAlpha	= kdGetImageIntATX ( pImage, KD_IMAGE_FLAG_PREMULTIPLIED_ALPHA ) ? KD_TRUE : KD_FALSE;
			m_nDataLen					= kdGetImageIntATX ( pImage, KD_IMAGE_DATASIZE_ATX );
			m_nNumberOfMipmaps			= kdGetImageIntATX ( pImage, KD_IMAGE_LEVELS_ATX );
			m_pData						= (KDubyte*) kdGetImagePointerATX ( pImage, KD_IMAGE_POINTER_BUFFER_ATX );
			m_pImageAtx					= pImage;

			for ( KDint i = 0; i < m_nNumberOfMipmaps; i++ )
			{
				m_aMipmaps [ i ].address = m_pData + kdGetImageLevelIntATX ( pImage, KD_IMAGE_BUFFEROFFSET_ATX, i );
				m_aMipmaps [ i ].len     = kdGetImageLevelIntATX ( pImage, KD_IMAGE_DATASIZE_ATX, i );
			}

			bRet = true;

		} while ( 0 );

		if ( pFile )
		{
			kdFclose ( pFile );
		}
	
		if ( pUnpackedData != pData )
		{
			kdFree ( pUnpackedData );
		}

	} while ( 0 );

	return bRet;
}
void
xmlMemDisplay(KDFile *fp)
{
#ifdef MEM_LIST
    MEMHDR *p;
    unsigned idx;
    int     nb = 0;
#if defined(HAVE_LOCALTIME) && defined(HAVE_STRFTIME)
    time_t currentTime;
    char buf[500];
    struct tm * tstruct;
#endif
#endif
    KDFile *old_fp = fp;

    if (fp == KD_NULL) {
	fp = kdFopen ( ".memorylist", "w" );
	if (fp == KD_NULL)
	    return;
    }

#ifdef MEM_LIST
#if defined(HAVE_LOCALTIME) && defined(HAVE_STRFTIME)
    currentTime = time(KD_NULL);
    tstruct = localtime(&currentTime);
    strftime(buf, sizeof(buf) - 1, "%I:%M:%S %p", tstruct);
    fprintf(fp,"      %s\n\n", buf);
#endif


    fprintf(fp,"      MEMORY ALLOCATED : %lu, MAX was %lu\n",
            debugMemSize, debugMaxMemSize);
    fprintf(fp,"BLOCK  NUMBER   SIZE  TYPE\n");
    idx = 0;
    xmlMutexLock(xmlMemMutex);
    p = memlist;
    while (p) {
	  fprintf(fp,"%-5u  %6lu %6lu ",idx++,p->mh_number,
		  (unsigned long)p->mh_size);
        switch (p->mh_type) {
           case STRDUP_TYPE:fprintf(fp,"strdup()  in ");break;
           case MALLOC_TYPE:fprintf(fp,"kdMalloc()  in ");break;
           case REALLOC_TYPE:fprintf(fp,"kdRealloc() in ");break;
           case MALLOC_ATOMIC_TYPE:fprintf(fp,"atomickdMalloc()  in ");break;
           case REALLOC_ATOMIC_TYPE:fprintf(fp,"atomickdRealloc() in ");break;
           default:
	        fprintf(fp,"Unknown memory block, may be corrupted");
		xmlMutexUnlock(xmlMemMutex);
		if (old_fp == KD_NULL)
		    fclose(fp);
		return;
        }
	if (p->mh_file != KD_NULL) fprintf(fp,"%s(%u)", p->mh_file, p->mh_line);
        if (p->mh_tag != MEMTAG)
	      fprintf(fp,"  INVALID");
        nb++;
	if (nb < 100)
	    xmlMemContentShow(fp, p);
	else
	    fprintf(fp," skip");

        fprintf(fp,"\n");
        p = p->mh_next;
    }
    xmlMutexUnlock(xmlMemMutex);
#else
    kdFprintfKHR ( fp,"Memory list not compiled (MEM_LIST not defined !)\n");
#endif
    if (old_fp == KD_NULL)
	kdFclose ( fp );
}
void
xmlMemDisplayLast(KDFile *fp, long nbBytes)
{
#ifdef MEM_LIST
    MEMHDR *p;
    unsigned idx;
    int     nb = 0;
#endif
    KDFile *old_fp = fp;

    if (nbBytes <= 0)
        return;

    if (fp == KD_NULL) {
	fp = kdFopen(".memorylist", "w");
	if (fp == KD_NULL)
	    return;
    }

#ifdef MEM_LIST
    fprintf(fp,"   Last %li MEMORY ALLOCATED : %lu, MAX was %lu\n",
            nbBytes, debugMemSize, debugMaxMemSize);
    fprintf(fp,"BLOCK  NUMBER   SIZE  TYPE\n");
    idx = 0;
    xmlMutexLock(xmlMemMutex);
    p = memlist;
    while ((p) && (nbBytes > 0)) {
	  fprintf(fp,"%-5u  %6lu %6lu ",idx++,p->mh_number,
		  (unsigned long)p->mh_size);
        switch (p->mh_type) {
           case STRDUP_TYPE:fprintf(fp,"strdup()  in ");break;
           case MALLOC_TYPE:fprintf(fp,"kdMalloc()  in ");break;
           case REALLOC_TYPE:fprintf(fp,"kdRealloc() in ");break;
           case MALLOC_ATOMIC_TYPE:fprintf(fp,"atomickdMalloc()  in ");break;
           case REALLOC_ATOMIC_TYPE:fprintf(fp,"atomickdRealloc() in ");break;
           default:
	        fprintf(fp,"Unknown memory block, may be corrupted");
		xmlMutexUnlock(xmlMemMutex);
		if (old_fp == KD_NULL)
		    fclose(fp);
		return;
        }
	if (p->mh_file != KD_NULL) fprintf(fp,"%s(%u)", p->mh_file, p->mh_line);
        if (p->mh_tag != MEMTAG)
	      fprintf(fp,"  INVALID");
        nb++;
	if (nb < 100)
	    xmlMemContentShow(fp, p);
	else
	    fprintf(fp," skip");

        fprintf(fp,"\n");
	nbBytes -= (unsigned long)p->mh_size;
        p = p->mh_next;
    }
    xmlMutexUnlock(xmlMemMutex);
#else
    kdFprintfKHR ( fp,"Memory list not compiled (MEM_LIST not defined !)\n");
#endif
    if (old_fp == KD_NULL)
	kdFclose ( fp );
}
KDbool AssetsManager::uncompress_ ( KDvoid )
{
    // Open the zip file
	std::string  sOutFileName = m_sStoragePath + TEMP_PACKAGE_FILE_NAME;
    unzFile      pZipFile = unzOpen ( sOutFileName.c_str ( ) );
    if ( !pZipFile )
    {
        CCLOG ( "can not open downloaded zip file %s", sOutFileName.c_str ( ) );
        return KD_FALSE;
    }
    
    // Get info about the zip file
    unz_global_info  tGlobalInfo;
    if ( unzGetGlobalInfo ( pZipFile, &tGlobalInfo ) != UNZ_OK )
    {
        CCLOG ( "can not read file global info of %s", sOutFileName.c_str ( ) );
        unzClose ( pZipFile );
    }
    
    // Buffer to hold data read from the zip file
    KDchar  aReadBuffer [ BUFFER_SIZE ];
    
    CCLOG ( "start uncompressing" );
    
    // Loop to extract all files.
    uLong i;
    for ( i = 0; i < tGlobalInfo.number_entry; ++i )
    {
        // Get info about current file.
        unz_file_info  tFileInfo;
        KDchar  szFileName[MAX_FILENAME];

        if ( unzGetCurrentFileInfo ( pZipFile,
                                   &tFileInfo,
                                   szFileName,
                                   MAX_FILENAME,
                                   NULL,
                                   0,
                                   NULL,
                                   0 ) != UNZ_OK )
        {
            CCLOG ( "can not read file info" );
            unzClose ( pZipFile );
            return KD_FALSE;
        }
        
		std::string  sFullPath = m_sStoragePath + szFileName;
        
        // Check if this entry is a directory or a file.
        const KDsize  uFilenameLength = kdStrlen ( szFileName );
        if ( szFileName [ uFilenameLength - 1 ] == '/' )
        {
            // Entry is a direcotry, so create it.
            // If the directory exists, it will failed scilently.
            if ( !createDirectory ( sFullPath.c_str ( ) ) )
            {
                CCLOG ( "can not create directory %s", sFullPath.c_str ( ) );
                unzClose ( pZipFile );
                return KD_FALSE;
            }
        }
        else
        {
            // Entry is a file, so extract it.
            
            // Open current file.
            if ( unzOpenCurrentFile ( pZipFile ) != UNZ_OK )
            {
                CCLOG ( "can not open file %s", szFileName );
                unzClose ( pZipFile );
                return KD_FALSE;
            }
            
            // Create a file to store current file.
            KDFile*  pOut = kdFopen ( sFullPath.c_str ( ), "wb" );
            if ( !pOut )
            {
                CCLOG ( "can not open destination file %s", sFullPath.c_str ( ) );
                unzCloseCurrentFile ( pZipFile );
                unzClose ( pZipFile );
                return KD_FALSE;
            }
            
            // Write current file content to destinate file.
            KDint  nError = UNZ_OK;
            do
            {
                nError = unzReadCurrentFile ( pZipFile, aReadBuffer, BUFFER_SIZE );
                if ( nError < 0 )
                {
                    CCLOG ( "can not read zip file %s, error code is %d", szFileName, nError );
                    unzCloseCurrentFile ( pZipFile );
                    unzClose( pZipFile );
                    return KD_FALSE;
                }
                
                if ( nError > 0 )
                {
                    kdFwrite ( aReadBuffer, nError, 1, pOut );
                }

            } while ( nError > 0 );
            
            kdFclose ( pOut );
        }
        
        unzCloseCurrentFile ( pZipFile );
        
        // Goto next entry listed in the zip file.
        if ( ( i + 1 ) < tGlobalInfo.number_entry )
        {
            if ( unzGoToNextFile ( pZipFile ) != UNZ_OK )
            {
                CCLOG ( "can not read next file" );
                unzClose ( pZipFile );
                return KD_FALSE;
            }
        }
    }
    
    CCLOG ( "end uncompressing" );
    
    return KD_TRUE;
}