@@ -257,9 +257,6 @@ void Downloader::downloadToBuffer(const std::string& srcUrl, const std::string&
257257 CC_ASSERT (buffer && " must not be nill" );
258258 CC_ASSERT (data && " must not be nill" );
259259
260- std::weak_ptr<Downloader> ptr = shared_from_this ();
261- std::shared_ptr<Downloader> shared = ptr.lock ();
262-
263260 CC_ASSERT (_downloaderImpl && " Cannot instanciate more than one instance of DownloaderImpl" );
264261
265262 // ASYNC methods must use the _downloaderImpl
@@ -283,18 +280,23 @@ void Downloader::downloadToBuffer(const std::string& srcUrl, const std::string&
283280 }
284281 else
285282 {
286- Director::getInstance ()->getScheduler ()-> performFunctionInCocosThread ([=]{
287- if (!ptr. expired ())
288- {
289- std::shared_ptr<Downloader> downloader = ptr.lock ();
290-
291- auto successCB = downloader-> getSuccessCallback ();
292- if (successCB != nullptr )
283+ if ( std::this_thread::get_id () != Director::getInstance ()->getCocos2dThreadId ())
284+ {
285+ std::weak_ptr<Downloader> ptr = shared_from_this ();
286+ std::shared_ptr<Downloader> shared = ptr.lock ();
287+
288+ Director::getInstance ()-> getScheduler ()-> performFunctionInCocosThread ([=]{
289+ if (!ptr. expired () )
293290 {
294- successCB (data->url , " " , data->customId );
291+ std::shared_ptr<Downloader> downloader = ptr.lock ();
292+ reportDownloadFinished (data->url , " " , data->customId );
295293 }
296- }
297- });
294+ });
295+ }
296+ else
297+ {
298+ reportDownloadFinished (data->url , " " , data->customId );
299+ }
298300 }
299301}
300302
@@ -324,11 +326,6 @@ void Downloader::downloadSync(const std::string& srcUrl, const std::string& stor
324326void Downloader::download (const std::string& srcUrl, const std::string& customId, FILE* fp, ProgressData* data)
325327{
326328 CC_ASSERT (data && " data must not be nill" );
327-
328- std::weak_ptr<Downloader> ptr = shared_from_this ();
329- std::shared_ptr<Downloader> shared = ptr.lock ();
330-
331-
332329 CC_ASSERT (_downloaderImpl && " Cannot instanciate more than one instance of DownloaderImpl" );
333330
334331 // ASYNC methods must use the _downloaderImpl
@@ -347,6 +344,7 @@ void Downloader::download(const std::string& srcUrl, const std::string& customId
347344
348345 if (res != 0 )
349346 {
347+ // XXX: If this is called from a different thread, will it crash?
350348 _fileUtils->removeFile (data->path + data->name + TEMP_EXT);
351349 std::string msg = StringUtils::format (" Unable to download file: [curl error]%s" , _downloaderImpl->getStrError ().c_str ());
352350 this ->notifyError (msg, customId, res);
@@ -358,19 +356,24 @@ void Downloader::download(const std::string& srcUrl, const std::string& customId
358356 if (res == 0 )
359357 {
360358 _fileUtils->renameFile (data->path , data->name + TEMP_EXT, data->name );
361-
362- Director::getInstance ()->getScheduler ()-> performFunctionInCocosThread ([=]{
363- if (!ptr. expired ())
364- {
365- std::shared_ptr<Downloader> downloader = ptr.lock ();
366-
367- auto successCB = downloader-> getSuccessCallback ();
368- if (successCB != nullptr )
359+
360+ if ( std::this_thread::get_id () != Director::getInstance ()->getCocos2dThreadId ())
361+ {
362+ std::weak_ptr<Downloader> ptr = shared_from_this ();
363+ std::shared_ptr<Downloader> shared = ptr.lock ();
364+
365+ Director::getInstance ()-> getScheduler ()-> performFunctionInCocosThread ([=]{
366+ if (!ptr. expired () )
369367 {
370- successCB (data->url , data->path + data->name , data->customId );
368+ std::shared_ptr<Downloader> downloader = ptr.lock ();
369+ reportDownloadFinished (data->url , data->path + data->name , data->customId );
371370 }
372- }
373- });
371+ });
372+ }
373+ else
374+ {
375+ reportDownloadFinished (data->url , data->path + data->name , data->customId );
376+ }
374377 }
375378}
376379
@@ -510,19 +513,23 @@ size_t Downloader::bufferWriteFunc(void *ptr, size_t size, size_t nmemb, void *u
510513 else return 0 ;
511514}
512515
513- void Downloader::reportDownloadProgressFinished ( double totalToDownload, double nowDownloaded , const ProgressData* data )
516+ void Downloader::reportDownloadFinished ( const std::string& url, const std::string& path , const std::string& customid )
514517{
515- if (_onProgress != nullptr )
518+ if (_onSuccess != nullptr )
516519 {
517- _onProgress (totalToDownload, nowDownloaded, data-> url , data-> customId );
520+ _onSuccess (url, path, customid );
518521 }
519- if (_onSuccess != nullptr )
522+ }
523+ void Downloader::reportProgressFinished (double totalToDownload, double nowDownloaded, const ProgressData* data)
524+ {
525+ if (_onProgress != nullptr )
520526 {
521- _onSuccess (data-> url , data-> path + data->name , data->customId );
527+ _onProgress (totalToDownload, nowDownloaded, data->url , data->customId );
522528 }
529+ reportDownloadFinished (data->url , data->path + data->name , data->customId );
523530}
524531
525- void Downloader::reportDownloadProgressInProgress (double totalToDownload, double nowDownloaded, const ProgressData* data)
532+ void Downloader::reportProgressInProgress (double totalToDownload, double nowDownloaded, const ProgressData* data)
526533{
527534 if (_onProgress != nullptr )
528535 {
@@ -554,13 +561,13 @@ int Downloader::batchDownloadProgressFunc(void *userdata, double totalToDownload
554561 Director::getInstance ()->getScheduler ()->performFunctionInCocosThread ([=]{
555562 if (!_this.expired ())
556563 {
557- this ->reportDownloadProgressFinished (totalToDownload, nowDownloaded, ©Data);
564+ this ->reportProgressFinished (totalToDownload, nowDownloaded, ©Data);
558565 }
559566 });
560567 }
561568 else
562569 {
563- reportDownloadProgressFinished (totalToDownload, nowDownloaded, ptr);
570+ reportProgressFinished (totalToDownload, nowDownloaded, ptr);
564571 }
565572 }
566573 else
@@ -572,13 +579,13 @@ int Downloader::batchDownloadProgressFunc(void *userdata, double totalToDownload
572579 Director::getInstance ()->getScheduler ()->performFunctionInCocosThread ([=]{
573580 if (!_this.expired ())
574581 {
575- reportDownloadProgressInProgress (totalToDownload, nowDownloaded, ©Data);
582+ reportProgressInProgress (totalToDownload, nowDownloaded, ©Data);
576583 }
577584 });
578585 }
579586 else
580587 {
581- reportDownloadProgressInProgress (totalToDownload, nowDownloaded, ptr);
588+ reportProgressInProgress (totalToDownload, nowDownloaded, ptr);
582589 }
583590 }
584591 }
0 commit comments