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

View File

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

View File

@ -1531,24 +1531,30 @@ void GameController::NotifyUpdateAvailable(Client * sender)
virtual void Action() 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 #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) #elif defined(SNAPSHOT)
currentVersion = "Snapshot " MTOS(SNAPSHOT_ID); updateMessage << "Snapshot " << SNAPSHOT_ID;
#else #else
currentVersion = MTOS(SAVE_VERSION) "." MTOS(MINOR_VERSION) " Stable, Build " MTOS(BUILD_NUM); updateMessage << SAVE_VERSION << "." << MINOR_VERSION << " Stable, Build " << BUILD_NUM;
#endif #endif
UpdateInfo info = Client::Ref().GetUpdateInfo(); updateMessage << "\nNew version:\n ";
if(info.Type == UpdateInfo::Beta) if (info.Type == UpdateInfo::Beta)
newVersion = format::NumberToString<int>(info.Major) + " " + format::NumberToString<int>(info.Minor) + " Beta, Build " + format::NumberToString<int>(info.Build); updateMessage << info.Major << " " << info.Minor << " Beta, Build " << info.Build;
else if(info.Type == UpdateInfo::Snapshot) else if (info.Type == UpdateInfo::Snapshot)
newVersion = "Snapshot " + format::NumberToString<int>(info.Time); updateMessage << "Snapshot " << info.Time;
else if(info.Type == UpdateInfo::Stable) 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); free(data);
errorStream << "Server responded with Status " << status; errorStream << "Server responded with Status " << status;
notifyError("Could not download update"); notifyError("Could not download update: " + errorStream.str());
return false; return false;
} }
if (!data) if (!data)
@ -118,7 +118,11 @@ private:
UpdateActivity::UpdateActivity() { UpdateActivity::UpdateActivity() {
std::stringstream file; std::stringstream file;
#ifdef UPDATESERVER
file << "http://" << UPDATESERVER << Client::Ref().GetUpdateInfo().File;
#else
file << "http://" << SERVER << Client::Ref().GetUpdateInfo().File; file << "http://" << SERVER << Client::Ref().GetUpdateInfo().File;
#endif
updateDownloadTask = new UpdateDownloadTask(file.str(), this); updateDownloadTask = new UpdateDownloadTask(file.str(), this);
updateWindow = new TaskWindow("Downloading update...", updateDownloadTask, true); updateWindow = new TaskWindow("Downloading update...", updateDownloadTask, true);
} }
@ -148,13 +152,19 @@ void UpdateActivity::NotifyError(Task * sender)
virtual void ConfirmCallback(ConfirmPrompt::DialogueResult result) { virtual void ConfirmCallback(ConfirmPrompt::DialogueResult result) {
if (result == ConfirmPrompt::ResultOkay) if (result == ConfirmPrompt::ResultOkay)
{ {
#ifndef UPDATESERVER
Platform::OpenURI("http://powdertoy.co.uk/Download.html"); Platform::OpenURI("http://powdertoy.co.uk/Download.html");
#endif
} }
a->Exit(); a->Exit();
} }
virtual ~ErrorMessageCallback() { } 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)); new ConfirmPrompt("Autoupdate failed", "Please visit the website to download a newer version.\nError: " + sender->GetError(), new ErrorMessageCallback(this));
#endif
} }