support alternate update servers, and optional changelogs

This commit is contained in:
jacob1 2015-09-26 11:47:51 -04:00
parent cf5ec57ab3
commit 908f60d47a
4 changed files with 109 additions and 59 deletions

View File

@ -70,6 +70,8 @@ void writeUserPreferences(const char * prefData);
Client::Client():
messageOfTheDay(""),
versionCheckRequest(NULL),
alternateVersionCheckRequest(NULL),
usingAltUpdateServer(false),
updateAvailable(false),
authUser(0, "")
{
@ -135,7 +137,6 @@ Client::Client():
void Client::Initialise(std::string proxyString)
{
if(GetPrefBool("version.update", false)==true)
{
SetPref("version.update", false);
@ -175,6 +176,12 @@ void Client::Initialise(std::string proxyString)
delete[] id;
delete[] session;
}
#ifdef UPDATESERVER
// use an alternate update server
alternateVersionCheckRequest = http_async_req_start(NULL, "http://" UPDATESERVER "/Startup.json", NULL, 0, 0);
usingAltUpdateServer = true;
#endif
}
bool Client::IsFirstRun()
@ -737,14 +744,26 @@ void Client::Tick()
{
//Check thumbnail queue
RequestBroker::Ref().FlushThumbQueue();
if (versionCheckRequest)
{
if (CheckUpdate(versionCheckRequest, true))
versionCheckRequest = NULL;
}
if (alternateVersionCheckRequest)
{
if (CheckUpdate(alternateVersionCheckRequest, false))
alternateVersionCheckRequest = NULL;
}
}
bool Client::CheckUpdate(void *updateRequest, bool checkSession)
{
//Check status on version check request
if (versionCheckRequest && http_async_req_status(versionCheckRequest))
if (http_async_req_status(updateRequest))
{
int status;
int dataLength;
char * data = http_async_req_stop(versionCheckRequest, &status, &dataLength);
versionCheckRequest = NULL;
char * data = http_async_req_stop(updateRequest, &status, &dataLength);
if (status != 200)
{
@ -760,10 +779,13 @@ void Client::Tick()
json::Reader::Read(objDocument, dataStream);
//Check session
json::Boolean sessionStatus = objDocument["Session"];
if(!sessionStatus.Value())
if (checkSession)
{
SetAuthUser(User(0, ""));
json::Boolean sessionStatus = objDocument["Session"];
if(!sessionStatus.Value())
{
SetAuthUser(User(0, ""));
}
}
//Notifications from server
@ -779,55 +801,61 @@ void Client::Tick()
//MOTD
json::String messageOfTheDay = objDocument["MessageOfTheDay"];
this->messageOfTheDay = messageOfTheDay.Value();
notifyMessageOfTheDay();
if (!usingAltUpdateServer || !checkSession)
{
json::String messageOfTheDay = objDocument["MessageOfTheDay"];
this->messageOfTheDay = messageOfTheDay.Value();
notifyMessageOfTheDay();
#ifndef IGNORE_UPDATES
//Check for updates
json::Object versions = objDocument["Updates"];
//Check for updates
json::Object versions = objDocument["Updates"];
#if !defined(BETA) && !defined(SNAPSHOT)
json::Object stableVersion = versions["Stable"];
json::Number stableMajor = stableVersion["Major"];
json::Number stableMinor = stableVersion["Minor"];
json::Number stableBuild = stableVersion["Build"];
json::String stableFile = stableVersion["File"];
if (stableMajor.Value()>SAVE_VERSION || (stableMinor.Value()>MINOR_VERSION && stableMajor.Value()==SAVE_VERSION) || stableBuild.Value()>BUILD_NUM)
{
updateAvailable = true;
updateInfo = UpdateInfo(stableMajor.Value(), stableMinor.Value(), stableBuild.Value(), stableFile.Value(), UpdateInfo::Stable);
}
json::Object stableVersion = versions["Stable"];
json::Number stableMajor = stableVersion["Major"];
json::Number stableMinor = stableVersion["Minor"];
json::Number stableBuild = stableVersion["Build"];
json::String stableFile = stableVersion["File"];
json::String changelog = stableVersion["Changelog"];
if (stableMajor.Value()>SAVE_VERSION || (stableMinor.Value()>MINOR_VERSION && stableMajor.Value()==SAVE_VERSION) || stableBuild.Value()>BUILD_NUM)
{
updateAvailable = true;
updateInfo = UpdateInfo(stableMajor.Value(), stableMinor.Value(), stableBuild.Value(), stableFile.Value(), changelog.Value(), UpdateInfo::Stable);
}
#endif
#ifdef BETA
json::Object betaVersion = versions["Beta"];
json::Number betaMajor = betaVersion["Major"];
json::Number betaMinor = betaVersion["Minor"];
json::Number betaBuild = betaVersion["Build"];
json::String betaFile = betaVersion["File"];
if (betaMajor.Value()>SAVE_VERSION || (betaMinor.Value()>MINOR_VERSION && betaMajor.Value()==SAVE_VERSION) || betaBuild.Value()>BUILD_NUM)
{
updateAvailable = true;
updateInfo = UpdateInfo(betaMajor.Value(), betaMinor.Value(), betaBuild.Value(), betaFile.Value(), UpdateInfo::Beta);
}
json::Object betaVersion = versions["Beta"];
json::Number betaMajor = betaVersion["Major"];
json::Number betaMinor = betaVersion["Minor"];
json::Number betaBuild = betaVersion["Build"];
json::String betaFile = betaVersion["File"];
json::String changelog = stableVersion["Changelog"];
if (betaMajor.Value()>SAVE_VERSION || (betaMinor.Value()>MINOR_VERSION && betaMajor.Value()==SAVE_VERSION) || betaBuild.Value()>BUILD_NUM)
{
updateAvailable = true;
updateInfo = UpdateInfo(betaMajor.Value(), betaMinor.Value(), betaBuild.Value(), betaFile.Value(), changelog.Value(), UpdateInfo::Beta);
}
#endif
#ifdef SNAPSHOT
json::Object snapshotVersion = versions["Snapshot"];
json::Number snapshotSnapshot = snapshotVersion["Snapshot"];
json::String snapshotFile = snapshotVersion["File"];
if (snapshotSnapshot.Value() > SNAPSHOT_ID)
{
updateAvailable = true;
updateInfo = UpdateInfo(snapshotSnapshot.Value(), snapshotFile.Value(), UpdateInfo::Snapshot);
}
json::Object snapshotVersion = versions["Snapshot"];
json::Number snapshotSnapshot = snapshotVersion["Snapshot"];
json::String snapshotFile = snapshotVersion["File"];
json::String changelog = stableVersion["Changelog"];
if (snapshotSnapshot.Value() > SNAPSHOT_ID)
{
updateAvailable = true;
updateInfo = UpdateInfo(snapshotSnapshot.Value(), snapshotFile.Value(), changelog.Value(), UpdateInfo::Snapshot);
}
#endif
if(updateAvailable)
{
notifyUpdateAvailable();
}
if(updateAvailable)
{
notifyUpdateAvailable();
}
#endif
}
}
catch (json::Exception &e)
{
@ -836,7 +864,9 @@ void Client::Tick()
free(data);
}
return true;
}
return false;
}
UpdateInfo Client::GetUpdateInfo()

View File

@ -35,14 +35,15 @@ class UpdateInfo
public:
enum BuildType { Stable, Beta, Snapshot };
std::string File;
std::string Changelog;
int Major;
int Minor;
int Build;
int Time;
BuildType Type;
UpdateInfo() : File(""), Major(0), Minor(0), Build(0), Time(0), Type(Stable) {}
UpdateInfo(int major, int minor, int build, std::string file, BuildType type) : File(file), Major(major), Minor(minor), Build(build), Time(0), Type(type) {}
UpdateInfo(int time, std::string file, BuildType type) : File(file), Major(0), Minor(0), Build(0), Time(time), Type(type) {}
UpdateInfo() : File(""), Changelog(""), Major(0), Minor(0), Build(0), Time(0), Type(Stable) {}
UpdateInfo(int major, int minor, int build, std::string file, std::string changelog, BuildType type) : File(file), Changelog(changelog), Major(major), Minor(minor), Build(build), Time(0), Type(type) {}
UpdateInfo(int time, std::string file, std::string changelog, BuildType type) : File(file), Changelog(changelog), Major(0), Minor(0), Build(0), Time(time), Type(type) {}
};
class RequestListener;
@ -53,6 +54,8 @@ private:
std::vector<std::pair<std::string, std::string> > serverNotifications;
void * versionCheckRequest;
void * alternateVersionCheckRequest;
bool usingAltUpdateServer;
bool updateAvailable;
UpdateInfo updateInfo;
@ -165,6 +168,7 @@ public:
}
RequestStatus ParseServerReturn(char *result, int status, bool json);
void Tick();
bool CheckUpdate(void *updateRequest, bool checkSession);
void Shutdown();
//Force flushing preferences to file on disk.

View File

@ -1531,24 +1531,30 @@ void GameController::NotifyUpdateAvailable(Client * sender)
virtual void Action()
{
std::string currentVersion, newVersion;
UpdateInfo info = Client::Ref().GetUpdateInfo();
std::stringstream updateMessage;
updateMessage << "Are you sure you want to run the updater? Please save any changes before updating.\n\nCurrent version:\n ";
#ifdef BETA
currentVersion = MTOS(SAVE_VERSION) "." MTOS(MINOR_VERSION) " Beta, Build " MTOS(BUILD_NUM);
updateMessage << SAVE_VERSION << "." << MINOR_VERSION << " Beta, Build " << BUILD_NUM;
#elif defined(SNAPSHOT)
currentVersion = "Snapshot " MTOS(SNAPSHOT_ID);
updateMessage << "Snapshot " << SNAPSHOT_ID;
#else
currentVersion = MTOS(SAVE_VERSION) "." MTOS(MINOR_VERSION) " Stable, Build " MTOS(BUILD_NUM);
updateMessage << SAVE_VERSION << "." << MINOR_VERSION << " Stable, Build " << BUILD_NUM;
#endif
UpdateInfo info = Client::Ref().GetUpdateInfo();
if(info.Type == UpdateInfo::Beta)
newVersion = format::NumberToString<int>(info.Major) + " " + format::NumberToString<int>(info.Minor) + " Beta, Build " + format::NumberToString<int>(info.Build);
else if(info.Type == UpdateInfo::Snapshot)
newVersion = "Snapshot " + format::NumberToString<int>(info.Time);
updateMessage << "\nNew version:\n ";
if (info.Type == UpdateInfo::Beta)
updateMessage << info.Major << " " << info.Minor << " Beta, Build " << info.Build;
else if (info.Type == UpdateInfo::Snapshot)
updateMessage << "Snapshot " << info.Time;
else if(info.Type == UpdateInfo::Stable)
newVersion = format::NumberToString<int>(info.Major) + " " + format::NumberToString<int>(info.Minor) + " Stable, Build " + format::NumberToString<int>(info.Build);
updateMessage << info.Major << " " << info.Minor << " Stable, Build " << info.Build;
new ConfirmPrompt("Run Updater", "Are you sure you want to run the updater, please save any changes before updating.\n\nCurrent version:\n " + currentVersion + "\nNew version:\n " + newVersion, new UpdateConfirmation(c));
if (info.Changelog.length())
updateMessage << "\n\nChangelog:\n" << info.Changelog;
new ConfirmPrompt("Run Updater", updateMessage.str(), new UpdateConfirmation(c));
}
};

View File

@ -44,7 +44,7 @@ private:
{
free(data);
errorStream << "Server responded with Status " << status;
notifyError("Could not download update");
notifyError("Could not download update: " + errorStream.str());
return false;
}
if (!data)
@ -118,7 +118,11 @@ private:
UpdateActivity::UpdateActivity() {
std::stringstream file;
#ifdef UPDATESERVER
file << "http://" << UPDATESERVER << Client::Ref().GetUpdateInfo().File;
#else
file << "http://" << SERVER << Client::Ref().GetUpdateInfo().File;
#endif
updateDownloadTask = new UpdateDownloadTask(file.str(), this);
updateWindow = new TaskWindow("Downloading update...", updateDownloadTask, true);
}
@ -148,13 +152,19 @@ void UpdateActivity::NotifyError(Task * sender)
virtual void ConfirmCallback(ConfirmPrompt::DialogueResult result) {
if (result == ConfirmPrompt::ResultOkay)
{
#ifndef UPDATESERVER
Platform::OpenURI("http://powdertoy.co.uk/Download.html");
#endif
}
a->Exit();
}
virtual ~ErrorMessageCallback() { }
};
#ifdef UPDATESERVER
new ConfirmPrompt("Autoupdate failed", "Please go online to manually download a newer version.\nError: " + sender->GetError(), new ErrorMessageCallback(this));
#else
new ConfirmPrompt("Autoupdate failed", "Please visit the website to download a newer version.\nError: " + sender->GetError(), new ErrorMessageCallback(this));
#endif
}