From 4ba3056984491e6b4326daccba3b8314a9259ae6 Mon Sep 17 00:00:00 2001 From: Mathieu Schroeter Date: Sun, 13 Aug 2017 16:31:49 +0200 Subject: [PATCH] Add a function to check for updates --- CMakeLists.txt | 2 ++ src/blupi.cxx | 84 +++++++++++++++++++++++++++++++++++++++++++++++--- src/def.h | 1 + 3 files changed, 82 insertions(+), 5 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 78a9f8b..6fb36ca 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -81,6 +81,7 @@ find_package (PkgConfig REQUIRED) pkg_search_module (SDL2 REQUIRED sdl2) pkg_search_module (SDL2_MIXER REQUIRED SDL2_mixer) pkg_search_module (SDL2_IMAGE REQUIRED SDL2_image) +pkg_search_module (CURL REQUIRED libcurl) # Static dependencies for SDL_kitchensink pkg_search_module (PNG REQUIRED libpng) pkg_search_module (AVCODEC REQUIRED libavcodec) @@ -106,6 +107,7 @@ target_link_libraries (planetblupi PUBLIC ${SDL2_STATIC_LIBRARIES} ${SDL2_MIXER_STATIC_LIBRARIES} ${SDL2_IMAGE_STATIC_LIBRARIES} + ${CURL_STATIC_LIBRARIES} SDL_kitchensink ${PNG_STATIC_LIBRARIES} ${AVCODEC_STATIC_LIBRARIES} diff --git a/src/blupi.cxx b/src/blupi.cxx index 4c965f3..a40cb66 100644 --- a/src/blupi.cxx +++ b/src/blupi.cxx @@ -20,6 +20,7 @@ #include #include +#include #include #include #include @@ -42,11 +43,12 @@ SDL_Window * g_window; SDL_Renderer * g_renderer; -CEvent * g_pEvent = nullptr; -CPixmap * g_pPixmap = nullptr; // pixmap principal -CSound * g_pSound = nullptr; // sound principal -CMovie * g_pMovie = nullptr; // movie principal -CDecor * g_pDecor = nullptr; +CEvent * g_pEvent = nullptr; +CPixmap * g_pPixmap = nullptr; // pixmap principal +CSound * g_pSound = nullptr; // sound principal +CMovie * g_pMovie = nullptr; // movie principal +CDecor * g_pDecor = nullptr; +std::thread * g_updateThread = nullptr; bool g_bFullScreen = false; // false si mode de test Sint32 g_speedRate = 1; @@ -66,6 +68,12 @@ bool g_bTermInit = false; // initialisation en cours Uint32 g_lastPhase = 999; static bool g_pause; +struct url_data { + CURLcode status; + char * buffer; + size_t size; +}; + /** * \brief Read an integer from a string. * @@ -414,6 +422,64 @@ static void InitFail (const char * msg) FinishObjects (); } +static size_t +updateCallback (void * ptr, size_t size, size_t nmemb, void * data) +{ + size_t realsize = size * nmemb; + url_data * mem = static_cast (data); + + mem->buffer = + static_cast (realloc (mem->buffer, mem->size + realsize + 1)); + if (mem->buffer) + { + memcpy (&(mem->buffer[mem->size]), ptr, realsize); + mem->size += realsize; + mem->buffer[mem->size] = 0; + } + + return realsize; +} + +static void CheckForUpdates () +{ + url_data chunk; + + chunk.buffer = nullptr; /* we expect realloc(NULL, size) to work */ + chunk.size = 0; /* no data at this point */ + chunk.status = CURLE_FAILED_INIT; + + CURL * curl = curl_easy_init (); + if (!curl) + return; + + curl_easy_setopt (curl, CURLOPT_NOSIGNAL, 1); + curl_easy_setopt (curl, CURLOPT_FAILONERROR, 1); + curl_easy_setopt (curl, CURLOPT_FOLLOWLOCATION, 1); + + curl_easy_setopt (curl, CURLOPT_WRITEFUNCTION, updateCallback); + + curl_easy_setopt (curl, CURLOPT_URL, "http://blupi.org/update/planet"); + curl_easy_setopt (curl, CURLOPT_WRITEDATA, (void *) &chunk); + chunk.status = curl_easy_perform (curl); + + if (chunk.status) + { + const char * err = curl_easy_strerror (chunk.status); + SDL_LogError ( + SDL_LOG_CATEGORY_APPLICATION, "Check for updates, error: %s", err); + } + else + { + std::string * res = new std::string (chunk.buffer, chunk.size); + CEvent::PushUserEvent (EV_CHECKUPDATE, res); + } + + if (chunk.buffer) + free (chunk.buffer); + + curl_easy_cleanup (curl); +} + static int parseArgs (int argc, char * argv[], bool & exit) { argagg::parser argparser{{ @@ -792,6 +858,7 @@ static int DoInit (int argc, char * argv[], bool & exit) } g_pEvent->Create (g_pPixmap, g_pDecor, g_pSound, g_pMovie); + g_updateThread = new std::thread (CheckForUpdates); g_pEvent->SetFullScreen (g_bFullScreen); g_pEvent->ChangePhase (EV_PHASE_INTRO1); @@ -838,5 +905,12 @@ int main (int argc, char * argv[]) SDL_RemoveTimer (updateTimer); FinishObjects (); SDL_Quit (); + + if (g_updateThread) + { + g_updateThread->join (); + delete (g_updateThread); + } + return 0; } diff --git a/src/def.h b/src/def.h index c2894a7..f8d2440 100644 --- a/src/def.h +++ b/src/def.h @@ -388,6 +388,7 @@ enum MouseSprites { #define EV_UPDATE (EV_OFFSET+1) #define EV_WARPMOUSE (EV_OFFSET+2) +#define EV_CHECKUPDATE (EV_OFFSET+3) #define EV_DECOR1 (EV_OFFSET+20) #define EV_DECOR2 (EV_OFFSET+21)