1
0
mirror of https://github.com/blupi-games/planetblupi synced 2024-12-30 10:15:36 +01:00

Show a message on the main screen if an update is available

This commit is contained in:
Mathieu Schroeter 2017-08-13 16:36:35 +02:00
parent 4ba3056984
commit 77262bf3a8
5 changed files with 13074 additions and 1 deletions

View File

@ -22,9 +22,15 @@
#include <argagg/argagg.hpp>
#include <curl/curl.h>
#include <iostream>
#include <iterator>
#include <sstream>
#include <stdio.h>
#include <stdlib.h>
#include <string>
#include <thread>
#include <vector>
#include "json.hpp"
#include "blupi.h"
#include "button.h"
@ -74,6 +80,25 @@ struct url_data {
size_t size;
};
template <typename Out>
void split (const std::string & s, char delim, Out result)
{
std::stringstream ss;
ss.str (s);
std::string item;
while (std::getline (ss, item, delim))
{
*(result++) = item;
}
}
std::vector<std::string> split (const std::string & s, char delim)
{
std::vector<std::string> elems;
split (s, delim, std::back_inserter (elems));
return elems;
}
/**
* \brief Read an integer from a string.
*
@ -389,6 +414,28 @@ static void HandleEvent (const SDL_Event & event)
break;
}
case EV_CHECKUPDATE:
{
std::string * data = static_cast<std::string *> (event.user.data1);
using json = nlohmann::json;
json jsonData = json::parse (*data);
/* Check if the remote version is newer */
std::vector<std::string> list = split (jsonData["version"], '.');
std::vector<unsigned int> version (3);
std::transform (
list.begin (), list.end (), version.begin (),
[](const std::string & s) -> unsigned int { return std::stoi (s); });
if (
PB_VERSION_INT (version[0], version[1], version[2]) >
PLANETBLUPI_VERSION_INT)
g_pEvent->SetUpdateVersion (jsonData["version"]);
delete data;
}
case EV_MUSIC_STOP:
if (g_pSound->IsStoppedOnDemand ())
break;

View File

@ -56,10 +56,13 @@ typedef Sint32 LPARAM;
#define PB_STRINGIFY(s) #s
#define PB_TOSTRING(s) PB_STRINGIFY (s)
#define PB_VERSION_INT(a, b, c) (a << 16 | b << 8 | c)
#define PB_VERSION_DOT(a, b, c) a##.##b##.##c
#define PB_VERSION(a, b, c) PB_VERSION_DOT (a, b, c)
#define PLANETBLUPI_VERSION \
PB_VERSION (PB_VERSION_MAJOR, PB_VERSION_MINOR, PB_VERSION_PATCH)
#define PLANETBLUPI_VERSION_INT \
PB_VERSION_INT (PB_VERSION_MAJOR, PB_VERSION_MINOR, PB_VERSION_PATCH)
#define PLANETBLUPI_VERSION_STR PB_TOSTRING (PLANETBLUPI_VERSION)
#ifdef _WIN32

View File

@ -1590,6 +1590,8 @@ CEvent::CEvent ()
m_Lang = m_Languages.begin () + 3;
else
m_Lang = m_Languages.begin ();
m_updateBlinking = 0;
}
// Destructeur.
@ -1870,6 +1872,11 @@ void AddCheatCode (char * pDst, char * pSrc)
pDst[j] = 0;
}
void CEvent::SetUpdateVersion (const std::string & version)
{
this->m_updateVersion = version;
}
// Dessine un texte multi-lignes centré.
void CEvent::DrawTextCenter (const char * text, Sint32 x, Sint32 y, Sint32 font)
@ -1945,6 +1952,17 @@ bool CEvent::DrawButtons ()
res, sizeof (res), "%s %u.%u.%u", gettext ("Version"), PB_VERSION_MAJOR,
PB_VERSION_MINOR, PB_VERSION_PATCH);
DrawText (m_pPixmap, pos, res, FONTLITTLE);
if (!this->m_updateVersion.empty () && this->m_updateBlinking++ % 80 < 40)
{
pos.x = 70;
pos.y = 465;
snprintf (
res, sizeof (res),
gettext ("New version available for download on www.blupi.org (v%s)"),
this->m_updateVersion.c_str ());
DrawText (m_pPixmap, pos, res, FONTLITTLE);
}
}
if (m_phase == EV_PHASE_SETUP || m_phase == EV_PHASE_SETUPp)

View File

@ -129,8 +129,8 @@ public:
void IntroStep ();
Uint8 GetWindowScale ();
void SetUpdateVersion (const std::string & version);
public:
static void PushUserEvent (Sint32 code, void * data = nullptr);
protected:
@ -247,6 +247,8 @@ protected:
Uint16 m_keymod;
POINT m_debugPos;
Sint32 m_introTime;
Sint32 m_updateBlinking;
std::string m_updateVersion;
};
/////////////////////////////////////////////////////////////////////////////

13003
src/json.hpp Normal file

File diff suppressed because it is too large Load Diff