AEVersionCheck.cpp 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. // Copyright (c) 2014-2015, THUNDERBEAST GAMES LLC All rights reserved
  2. // Please see LICENSE.md in repository root for license information
  3. // https://github.com/AtomicGameEngine/AtomicGameEngine
  4. #include <rapidjson/document.h>
  5. #include <rapidjson/filestream.h>
  6. #include <rapidjson/prettywriter.h>
  7. #include "AtomicEditor.h"
  8. #include <Atomic/Core/Context.h>
  9. #include <Atomic/IO/Log.h>
  10. #include "AEEditor.h"
  11. #include "AEEvents.h"
  12. #include "AEVersion.h"
  13. #include "AEVersionCheck.h"
  14. namespace AtomicEditor
  15. {
  16. VersionCheck::VersionCheck(Context* context) :
  17. Object(context)
  18. {
  19. SubscribeToEvent(E_EDITORSHUTDOWN, HANDLER(VersionCheck, HandleEditorShutdown));
  20. progressModal_ = new ProgressModal(context_, "Checking for Updates", "Checking for updates, please wait...");
  21. }
  22. VersionCheck::~VersionCheck()
  23. {
  24. }
  25. void VersionCheck::DoVersionCheck()
  26. {
  27. progressModal_->Show();
  28. CurlManager* cm = GetSubsystem<CurlManager>();
  29. versionRequest_ = cm->MakeRequest("https://store.atomicgameengine.com/versions/atomic_editor.html");
  30. SubscribeToEvent(versionRequest_, E_CURLCOMPLETE, HANDLER(VersionCheck, HandleCurlComplete));
  31. }
  32. void VersionCheck::HandleCurlComplete(StringHash eventType, VariantMap& eventData)
  33. {
  34. progressModal_->Hide();
  35. Editor* editor = GetSubsystem<Editor>();
  36. bool valid = true;
  37. int major;
  38. int minor;
  39. int patch;
  40. if (versionRequest_->GetError().Length())
  41. {
  42. valid = false;
  43. }
  44. else
  45. {
  46. rapidjson::Document document;
  47. String json = versionRequest_->GetResponse();
  48. if (document.Parse<0>(json.CString()).HasParseError())
  49. {
  50. valid = false;
  51. }
  52. else
  53. {
  54. const rapidjson::Value::Member* jmajor = document.FindMember("major");
  55. if (jmajor && jmajor->value.IsInt())
  56. {
  57. major = jmajor->value.GetInt();
  58. }
  59. else
  60. valid = false;
  61. const rapidjson::Value::Member* jminor = document.FindMember("minor");
  62. if (jminor && jminor->value.IsInt())
  63. {
  64. minor = jminor->value.GetInt();
  65. }
  66. else
  67. valid = false;
  68. const rapidjson::Value::Member* jpatch = document.FindMember("patch");
  69. if (jpatch && jpatch->value.IsInt())
  70. {
  71. patch = jpatch->value.GetInt();
  72. }
  73. else
  74. valid = false;
  75. }
  76. }
  77. if (!valid)
  78. {
  79. String errorMessage;
  80. errorMessage.AppendWithFormat("There was an error contacting the version server\n\n%s", versionRequest_->GetError().CString());
  81. editor->PostModalError("Error Contacting Version Server", errorMessage);
  82. }
  83. else
  84. {
  85. bool newer = false;
  86. if (ATOMIC_EDITOR_VERSION_MAJOR < major)
  87. newer = true;
  88. else if (ATOMIC_EDITOR_VERSION_MINOR < minor)
  89. newer = true;
  90. else if (ATOMIC_EDITOR_VERSION_PATCH < patch)
  91. newer = true;
  92. if (newer)
  93. {
  94. editor->PostModalInfo("New Version Available", "There is a newer version of the Atomic Editor available");
  95. }
  96. else
  97. {
  98. editor->PostModalInfo("Up to date", "The Atomic Editor is up to date");
  99. }
  100. }
  101. versionRequest_ = 0;
  102. }
  103. void VersionCheck::HandleEditorShutdown(StringHash eventType, VariantMap& eventData)
  104. {
  105. context_->RemoveSubsystem(GetType());
  106. }
  107. /*
  108. {
  109. "major" : 0,
  110. "minor" : 2,
  111. "patch" : 0
  112. }
  113. */
  114. }