//-------------------------------------------------------------------------------------------------------------- // Starts a new download if there are queued requests void DownloadManager::StartNewDownload() { if ( m_activeRequest || !m_queuedRequests.Count() ) return; while ( !m_activeRequest && m_queuedRequests.Count() ) { // Remove one request from the queue and make it active m_activeRequest = m_queuedRequests[0]; m_queuedRequests.Remove( 0 ); if ( g_pFileSystem->FileExists( m_activeRequest->gamePath ) ) { ConDColorMsg( DownloadColor, "Skipping existing file %s%s.\n", m_activeRequest->baseURL, m_activeRequest->gamePath ); m_activeRequest->shouldStop = true; m_activeRequest->threadDone = true; m_completedRequests.AddToTail( m_activeRequest ); m_activeRequest = NULL; } } if ( !m_activeRequest ) return; if ( g_pFileSystem->FileExists( m_activeRequest->gamePath ) ) { m_activeRequest->shouldStop = true; m_activeRequest->threadDone = true; m_completedRequests.AddToTail( m_activeRequest ); m_activeRequest = NULL; return; // don't download existing files } if ( m_activeRequest->bAsHTTP ) { // Check cache for partial match TheDownloadCache->GetCachedData( m_activeRequest ); //TODO: ContinueLoadingProgressBar( "Http", m_totalRequests - m_queuedRequests.Count(), 0.0f ); //TODO: SetLoadingProgressBarStatusText( "#GameUI_VerifyingAndDownloading" ); //TODO: SetSecondaryProgressBarText( m_activeRequest->gamePath ); //TODO: SetSecondaryProgressBar( 0.0f ); UpdateProgressBar(); ConDColorMsg( DownloadColor, "Downloading %s%s.\n", m_activeRequest->baseURL, m_activeRequest->gamePath ); m_lastPercent = 0; // Start the thread DWORD threadID; VCRHook_CreateThread(NULL, 0, DownloadThread, m_activeRequest, 0, &threadID ); } else { UpdateProgressBar(); ConDColorMsg( DownloadColor, "Downloading %s.\n", m_activeRequest->gamePath ); m_lastPercent = 0; m_activeRequest->nRequestID = cl.m_NetChannel->RequestFile( m_activeRequest->gamePath ); } }
//-------------------------------------------------------------------------------------------------------------- void DownloadManager::UpdateProgressBar() { if ( !m_activeRequest ) { return; } wchar_t filenameBuf[MAX_OSPATH]; float progress = 0.0f; if ( m_activeRequest->bAsHTTP ) { int overallPercent = (m_totalRequests - m_queuedRequests.Count() - 1) * 100 / m_totalRequests; int filePercent = 0; if ( m_activeRequest->nBytesTotal > 0 ) { filePercent = ( m_activeRequest->nBytesCurrent * 100 / m_activeRequest->nBytesTotal ); } progress = (overallPercent + filePercent * 1.0f / m_totalRequests) * 0.01f; } else { int received, total; cl.m_NetChannel->GetStreamProgress( FLOW_INCOMING, &received, &total ); progress = (float)(received)/(float)(total); } _snwprintf( filenameBuf, 256, L"Downloading %hs", m_activeRequest->gamePath ); EngineVGui()->UpdateCustomProgressBar( progress, filenameBuf ); }
//-------------------------------------------------------------------------------------------------------------- void DownloadManager::Reset() { // ask the active request to bail if ( m_activeRequest ) { ConDColorMsg( DownloadColor, "Aborting download of %s\n", m_activeRequest->gamePath ); if ( m_activeRequest->nBytesTotal && m_activeRequest->nBytesCurrent ) { // Persist partial data to cache TheDownloadCache->PersistToCache( m_activeRequest ); } m_activeRequest->shouldStop = true; m_completedRequests.AddToTail( m_activeRequest ); m_activeRequest = NULL; //TODO: StopLoadingProgressBar(); } // clear out any queued requests for ( int i=0; i<m_queuedRequests.Count(); ++i ) { ConDColorMsg( DownloadColor, "Discarding queued download of %s\n", m_queuedRequests[i]->gamePath ); delete m_queuedRequests[i]; } m_queuedRequests.RemoveAll(); if ( TheDownloadCache ) { delete TheDownloadCache; TheDownloadCache = NULL; } m_lastPercent = 0; m_totalRequests = 0; }
//-------------------------------------------------------------------------------------------------------------- // Check download requests that have been completed to see if their threads have exited void DownloadManager::PruneCompletedRequests() { for ( int i=m_completedRequests.Count()-1; i>=0; --i ) { if ( m_completedRequests[i]->threadDone || !m_completedRequests[i]->bAsHTTP ) { if ( m_completedRequests[i]->cacheData ) { delete[] m_completedRequests[i]->cacheData; } delete m_completedRequests[i]; m_completedRequests.Remove( i ); } } }
//-------------------------------------------------------------------------------------------------------------- // Checks download status, and updates progress bar void DownloadManager::CheckActiveDownload() { if ( !m_activeRequest ) return; if ( !m_activeRequest->bAsHTTP ) { UpdateProgressBar(); return; } // check active request for completion / error / progress update switch ( m_activeRequest->status ) { case HTTP_DONE: ConDColorMsg( DownloadCompleteColor, "Download finished!\n" ); UpdateProgressBar(); if ( m_activeRequest->nBytesTotal ) { // Persist complete data to disk, and remove cache entry //TODO: SetSecondaryProgressBarText( m_activeRequest->gamePath ); TheDownloadCache->PersistToDisk( m_activeRequest ); m_activeRequest->shouldStop = true; m_completedRequests.AddToTail( m_activeRequest ); m_activeRequest = NULL; if ( !m_queuedRequests.Count() ) { //TODO: StopLoadingProgressBar(); //TODO: Cbuf_AddText("retry\n"); } } break; case HTTP_ERROR: ConDColorMsg( DownloadErrorColor, "Error downloading %s%s\n", m_activeRequest->baseURL, m_activeRequest->gamePath ); UpdateProgressBar(); // try to download the next file m_activeRequest->shouldStop = true; m_completedRequests.AddToTail( m_activeRequest ); m_activeRequest = NULL; if ( !m_queuedRequests.Count() ) { //TODO: StopLoadingProgressBar(); //TODO: Cbuf_AddText("retry\n"); } break; case HTTP_FETCH: UpdateProgressBar(); // Update progress bar //TODO: SetSecondaryProgressBarText( m_activeRequest->gamePath ); if ( m_activeRequest->nBytesTotal ) { int percent = ( m_activeRequest->nBytesCurrent * 100 / m_activeRequest->nBytesTotal ); if ( percent != m_lastPercent ) { /* ConDColorMsg( DownloadColor, "Downloading %s%s: %3.3d%% - %d of %d bytes\n", m_activeRequest->baseURL, m_activeRequest->gamePath, percent, m_activeRequest->nBytesCurrent, m_activeRequest->nBytesTotal ); */ m_lastPercent = percent; //TODO: SetSecondaryProgressBar( m_lastPercent * 0.01f ); } } break; } }