/
updatehelper.cpp
63 lines (53 loc) · 1.86 KB
/
updatehelper.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
/* This file is part of the Springlobby (GPL v2 or later), see COPYING */
#include "updatehelper.h"
#include <time.h>
#include <cerrno>
#include "httpfile.h"
#include "utils/conversion.h"
#include "utils/slconfig.h"
#include "utils/version.h"
SLCONFIG("/General/LastUpdateCheck", 0L, "Last time springlobby checked for an update");
SLCONFIG("/General/UpdateChannel", "release", "update channel to use (release or develop)");
#define VERSION_CHECK_INTERVAL 1 * 60 * 60
#define BASE_URL "https://springlobby.springrts.com/dl/"
static bool isReleaseChannel()
{
const wxString channel = cfg().ReadString(_T("/General/UpdateChannel"));
return channel != _T("develop"); //typos will make use release channel
}
std::string GetDownloadUrl(const std::string& version)
{
if (isReleaseChannel()) {
return stdprintf( BASE_URL "stable/springlobby-%s-win32.zip", version.c_str());
}
return stdprintf( BASE_URL "develop/springlobby-%s-win32.zip", version.c_str());
}
std::string GetLatestVersionUrl()
{
if (isReleaseChannel()) {
return std::string(BASE_URL "stable/version.txt");
}
return std::string(BASE_URL "develop/version-develop.txt");
}
static time_t GetTime()
{
time_t now = time(nullptr);
if (static_cast<time_t>(-1) == now) {
std::string msg = "time() broke: ";
msg += strerror(errno);
throw std::runtime_error(msg.c_str());
}
return now;
}
std::string GetLatestVersion(bool use_cached)
{
static std::string latest_version = GetSpringlobbyVersion();
const time_t now = GetTime();
const time_t last_check_time = cfg().ReadLong(_T("/General/LastUpdateCheck"));
if (latest_version.empty() || !use_cached || now > (last_check_time + VERSION_CHECK_INTERVAL)) {
latest_version = GetHttpFile(GetLatestVersionUrl());
latest_version = STD_STRING(TowxString(latest_version).Trim().Trim(false));
cfg().Write(_T("/General/LastUpdateCheck"), (long)now);
}
return latest_version;
}