Commit 4be2dac6 authored by gerstrong@gmail.com's avatar gerstrong@gmail.com

Tweaked and polished the Downloader a bit

parent 4abcb497
......@@ -158,7 +158,7 @@ GsProgressBar::processRender(const GsRect<float> &RectDispCoordFloat)
float progressWidth = 0.0;
if(mProgressToRender < 0.0 || mProgressToRender > 1000.0)
if(mProgressToRender < 0.0f || mProgressToRender > 1000.0f)
{
progressWidth = displayRect.w;
}
......@@ -182,7 +182,7 @@ GsProgressBar::processRender(const GsRect<float> &RectDispCoordFloat)
std::stringstream ss;
if( mProgressToRender == 0.0 ) // Less than not doing
if( mProgressToRender == 0.0f ) // Exactly 0 percent
{
ss << "0 %";
......@@ -199,7 +199,7 @@ GsProgressBar::processRender(const GsRect<float> &RectDispCoordFloat)
SDL_FillRect(pBlitsurface, &bgSDLRect, bgColor);
}
}
else if( mProgressToRender < 0.0 ) // Less than not doing
else if( mProgressToRender < 0.0f ) // Less than not doing
{
const auto progressColor = SDL_MapRGBA( pBlitsurface->format, 0, 255, 128, 255 );
......@@ -208,19 +208,34 @@ GsProgressBar::processRender(const GsRect<float> &RectDispCoordFloat)
SDL_FillRect(pBlitsurface, &bgSDLRect, bgColor);
SDL_FillRect(pBlitsurface, &progressSDLRect, progressColor);
}
else if( mProgressToRender >= 1000.0 ) // Finished
else if( mProgressToRender >= 1000.0f ) // Finished
{
SDL_FillRect(pBlitsurface, &bgSDLRect, bgColor);
if(!mBad)
if(!mUserAbort)
{
const auto progressColor = SDL_MapRGBA( pBlitsurface->format, 0, 255, mFlashBlue, 255 );
ss << "Completed!";
Uint32 progressColor;
if(mErrorCode == 0)
{
progressColor = SDL_MapRGBA(
pBlitsurface->format,
0, 255, Uint8(mFlashBlue), 255 );
ss << "Completed!";
}
else
{
progressColor = SDL_MapRGBA(
pBlitsurface->format,
255, 0, Uint8(mFlashBlue), 255 );
ss << "Error!";
}
SDL_FillRect(pBlitsurface, &progressSDLRect, progressColor);
}
}
else
{
const auto progressColor = SDL_MapRGBA( pBlitsurface->format, 255, 0, mFlashBlue, 255 );
const auto progressColor = SDL_MapRGBA( pBlitsurface->format, 255, 128, Uint8(mFlashBlue), 255 );
ss << "Aborted!";
SDL_FillRect(pBlitsurface, &progressSDLRect, progressColor);
}
......
......@@ -11,15 +11,17 @@ class GsProgressBar : public GsControl
{
public:
GsProgressBar(const int &progress) :
mProgress(progress) {}
GsProgressBar(const int &progress,
const int &errorCode) :
mProgress(progress),
mErrorCode(errorCode) {}
void processLogic() override;
void processRender(const GsRect<float> &RectDispCoordFloat) override;
const bool finished() const
bool finished() const
{
return mFinished;
}
......@@ -29,9 +31,9 @@ public:
mDoFancyAnimation = value;
}
void setBad(const bool value)
void setUserAbort(const bool value)
{
mBad = value;
mUserAbort = value;
}
private:
......@@ -49,6 +51,7 @@ private:
const Uint32 color2);
const int &mProgress;
const int &mErrorCode;
float mProgressToRender = 0.0f; // Progress to render. It might be behind "mProgress"
// when a nice animation takes place
......@@ -68,7 +71,7 @@ private:
bool mDoFancyAnimation = false;
// Bad will render the control red and show different message
bool mBad = false;
bool mUserAbort = false;
};
#endif // GSPROGRESSBAR_H
......@@ -326,10 +326,10 @@ bool CGameLauncher::scanSubDirectories(const std::string& path,
}
permil = lastPermil;
mGameScanner.setPermilage(permil);
mGameScanner.setPermilage(int(permil));
}
mGameScanner.setPermilage(endPermil);
mGameScanner.setPermilage(int(endPermil));
return gamesDetected;
}
......@@ -645,7 +645,7 @@ void CGameLauncher::pumpEvent(const CEvent *evPtr)
{
mCancelDownload = true;
mpDloadCancel->enable(false);
mpDloadProgressCtrl->setBad(true);
mpDloadProgressCtrl->setUserAbort(true);
}
else if( dynamic_cast<const OpenSettingsMenuEvent*>(evPtr) )
{
......
......@@ -184,6 +184,7 @@ private:
int m_start_level;
int mDownloadProgress = 0;
int mDownloadErrorCode = 0;
bool mCancelDownload = false;
ThreadPoolItem* mpGameDownloadThread = nullptr;
......@@ -202,6 +203,7 @@ private:
void putLabels();
int m_DownloadProgress = 0;
int m_DownloadProgressError = 0;
bool m_DownloadCancel = false;
ThreadPoolItem *mp_Thread = nullptr;
......
......@@ -50,6 +50,9 @@ void GameEngine::render()
return;
}
if(!mpGameMode)
return;
// Render the game mode object
mpGameMode->render();
......
......@@ -19,7 +19,9 @@ void CGameLauncher::verifyGameStore()
m_DownloadProgress = 0;
m_DownloadCancel = false;
GameDownloader gameDownloader(m_DownloadProgress, m_DownloadCancel);
GameDownloader gameDownloader(m_DownloadProgress,
m_DownloadProgressError,
m_DownloadCancel);
std::vector< std::string > missingList;
......@@ -58,7 +60,10 @@ void CGameLauncher::verifyGameStore()
// Try to download the catalogue file, in the background
if(!mp_Thread)
{
GameDownloader *pCatalogueDownloader = new GameDownloader(m_DownloadProgress, m_DownloadCancel);
GameDownloader *pCatalogueDownloader =
new GameDownloader(m_DownloadProgress,
m_DownloadProgressError,
m_DownloadCancel);
pCatalogueDownloader->setupDownloadCatalogue(true);
mp_Thread = threadPool->start(pCatalogueDownloader, "Loading catalogue file in the background");
}
......@@ -96,6 +101,7 @@ void CGameLauncher::pullGame(const int selection)
mpDloadProgressCtrl->enableFancyAnimation(true);
mpGameDownloadThread = threadPool->start(new GameDownloader(mDownloadProgress,
mDownloadErrorCode,
mCancelDownload,
gameFileName,
gameName),
......@@ -109,7 +115,7 @@ void CGameLauncher::ponderDownloadDialog()
int sel = mpGSSelList->getSelection();
if(mLastStoreSelection != sel)
{
auto &gameEntry = mGameCatalogue[sel];
auto &gameEntry = mGameCatalogue[size_t(sel)];
mpDDescriptionText->setText(gameEntry.mDescription);
......@@ -154,18 +160,17 @@ void CGameLauncher::setupDownloadDialog()
mFinishedDownload = 0;
mDownloadProgress = 0;
mDownloading = false;
mDownloadErrorCode = 0;
int progress = 0;
bool cancel = false;
GameDownloader gameDownloader(progress, cancel);
GameDownloader gameDownloader(progress, mDownloadErrorCode, cancel);
std::vector< std::string > missingList;
gameDownloader.checkForMissingGames( missingList );
mpGSSelList = new CGUITextSelectionList();
assert( !missingList.empty() );
if(!missingList.empty())
{
// Create the selection list
......@@ -205,7 +210,8 @@ void CGameLauncher::setupDownloadDialog()
// Progress Bar
mpDloadProgressCtrl = std::dynamic_pointer_cast<GsProgressBar>(
mpGameStoreDialog->addControl(new GsProgressBar(mDownloadProgress),
mpGameStoreDialog->addControl(new GsProgressBar(mDownloadProgress,
mDownloadErrorCode),
GsRect<float>(0.1f, 0.8f, 0.8f, 0.05f)) );
// Bottom Controls
......
......@@ -96,7 +96,8 @@ int downloadFile(const std::string &filename, int &progress,
{
progressPtr = &progress;
const std::string urlString = "http://downloads.sourceforge.net/project/clonekeenplus/Downloads/" + filename;
const std::string urlString = "http://downloads.sourceforge.net/project/clonekeenplus/Downloads/" + filename;
const std::string outputPathTemp = JoinPaths(downloadDirPath, "temp" + filename);
const std::string outputPath = JoinPaths(downloadDirPath, filename);
CURLcode res = CURLE_OK;
......@@ -122,7 +123,7 @@ int downloadFile(const std::string &filename, int &progress,
/* pass the struct pointer into the progress function */
curl_easy_setopt(curl, CURLOPT_PROGRESSDATA, &prog);
FILE *fp = OpenGameFile(outputPath, "wb");
FILE *fp = OpenGameFile(outputPathTemp, "wb");
if(fp != nullptr)
{
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, write_data);
......@@ -148,7 +149,7 @@ int downloadFile(const std::string &filename, int &progress,
curl_easy_setopt(curl, CURLOPT_NOPROGRESS, 0L);
res = curl_easy_perform(curl);
gLogging.ftextOut( FONTCOLORS::GREEN, "Finished downloading from \"%s\", destination: \"%s\"", urlString.c_str(), outputPath.c_str());
gLogging.ftextOut( FONTCOLORS::GREEN, "Finished downloading from \"%s\", destination: \"%s\"", urlString.c_str(), outputPathTemp.c_str());
progress = 1000;
}
......@@ -157,12 +158,10 @@ int downloadFile(const std::string &filename, int &progress,
/* always cleanup */
curl_easy_cleanup(curl);
gLogging.ftextOut( FONTCOLORS::RED, "Error creating path \"%s\" for writing", outputPath.c_str());
gLogging.ftextOut( FONTCOLORS::RED, "Error creating path \"%s\" for writing", outputPathTemp.c_str());
return 1;
}
// output any error to the central CG Log
if(res != CURLE_OK)
{
......@@ -173,10 +172,20 @@ int downloadFile(const std::string &filename, int &progress,
curl_easy_cleanup(curl);
fclose(fp);
// If all went well, the temp file will become the real one
if(res == CURLE_OK)
{
remove(outputPath.c_str());
Rename(outputPathTemp, outputPath);
}
else
{
remove(GetFullFileName(outputPathTemp).c_str());
}
}
return (int)res;
}
#define TRACE_NODE(x) gLogging << #x"=" << x;
......@@ -315,18 +324,18 @@ bool GameDownloader::loadCatalogue(const std::string &catalogueFile)
{
return false;
}
return true;
}
bool GameDownloader::downloadCatalogue()
{
pCancelDownload = &mCancelDownload;
pCancelDownload = &mCancelDownload;
const int res = downloadFile(mCatalogFName, mProgress, "");
if(res==0)
{
return true;
}
return false;
}
......@@ -426,6 +435,7 @@ int GameDownloader::handle()
if(mDownloadCatalogue)
{
downloadCatalogue();
mProgressError = res;
return res;
}
......@@ -501,6 +511,7 @@ int GameDownloader::handle()
}
mProgress = 1000;
mProgressError = res;
return res;
}
......
......@@ -26,16 +26,20 @@ class GameDownloader : public Action
public:
GameDownloader(int &progress,
int &errorCode,
bool &cancelDownload) :
mProgress(progress),
mProgressError(errorCode),
mCancelDownload(cancelDownload){}
GameDownloader(int &progress,
int &errorCode,
bool &cancelDownload,
const std::string &gameFileName,
const std::string &gameName) :
mProgress(progress),
mProgressError(errorCode),
mCancelDownload(cancelDownload),
mGameFileName(gameFileName),
mGameName(gameName) {}
......@@ -85,7 +89,7 @@ public:
}
int handle();
int handle() override;
/**
* @brief hasCatalog tells user if a catalog was detected
......@@ -104,6 +108,7 @@ public:
private:
int &mProgress;
int &mProgressError;
bool &mCancelDownload;
bool mCataFound = false;
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment