-
Notifications
You must be signed in to change notification settings - Fork 2
Add Update Check #41
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Add Update Check #41
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,130 @@ | ||
| #include "ZUpdateCheck.h" | ||
|
|
||
| #include <Logging.h> | ||
|
|
||
| #include <nlohmann/json.hpp> | ||
| #include <ixwebsocket/IXHttpClient.h> | ||
|
|
||
| #include <BuildInfo.h> | ||
|
|
||
| #define TAG "[ZUpdateCheck] " | ||
|
|
||
| using json = nlohmann::json; | ||
|
|
||
| ZUpdateCheck::~ZUpdateCheck() | ||
| { | ||
| if (m_UpdateCheckThread.joinable()) | ||
| { | ||
| m_UpdateCheckThread.join(); | ||
| } | ||
| } | ||
|
|
||
| void ZUpdateCheck::CheckUpdatesAsync() | ||
|
shadow578 marked this conversation as resolved.
|
||
| { | ||
| std::lock_guard s_Lock(m_ResultMutex); | ||
| if (m_eResult != EResult::None) | ||
| { | ||
| Logger::Debug(TAG "Update check already performed, skipping start of another check."); | ||
| return; | ||
| } | ||
|
|
||
| if (m_UpdateCheckThread.joinable()) | ||
| { | ||
| Logger::Error(TAG "Update check thread already running, but state does not match!"); | ||
| return; | ||
| } | ||
|
|
||
| m_UpdateCheckThread = std::thread(&ZUpdateCheck::CheckUpdatesInternal, this); | ||
| m_eResult = EResult::Checking; | ||
| } | ||
|
|
||
| void ZUpdateCheck::CheckUpdatesInternal() | ||
| { | ||
| Logger::Info(TAG "Starting update check for {}/{}...", BuildInfo::c_sRepoOwner, BuildInfo::c_sRepoName); | ||
|
|
||
| std::string s_sUrl = "https://api.github.com/repos/" | ||
| + BuildInfo::c_sRepoOwner | ||
| + "/" | ||
| + BuildInfo::c_sRepoName | ||
| + "/releases/latest"; | ||
| Logger::Debug(TAG "Update check URL: {}", s_sUrl); | ||
|
|
||
| ix::HttpClient s_Client; | ||
| ix::HttpRequestArgsPtr s_pRequest = s_Client.createRequest(); | ||
| const auto s_pResponse = s_Client.get(s_sUrl, s_pRequest); | ||
| if (!s_pResponse) | ||
| { | ||
| Logger::Error(TAG "Failed to perform update check HTTP request!"); | ||
|
|
||
| { | ||
| std::lock_guard s_Lock(m_ResultMutex); | ||
| m_eResult = EResult::Failed; | ||
| } | ||
| return; | ||
| } | ||
|
|
||
| if (s_pResponse->statusCode < 200 || s_pResponse->statusCode >= 300) | ||
| { | ||
| Logger::Error(TAG "update check failed: {} {}", s_pResponse->statusCode, s_pResponse->body); | ||
|
|
||
| { | ||
| std::lock_guard s_Lock(m_ResultMutex); | ||
| m_eResult = EResult::Failed; | ||
| } | ||
| return; | ||
| } | ||
|
|
||
| json s_ResponseJson; | ||
| try | ||
| { | ||
| s_ResponseJson = json::parse(s_pResponse->body); | ||
| } | ||
| catch (const json::exception& e) | ||
| { | ||
| Logger::Error(TAG "Failed to parse update check response as JSON: {}", e.what()); | ||
| { | ||
| std::lock_guard s_Lock(m_ResultMutex); | ||
| m_eResult = EResult::Failed; | ||
| } | ||
| return; | ||
| } | ||
|
|
||
| const auto s_sName = s_ResponseJson.value("name", ""); | ||
| const auto s_sTagName = s_ResponseJson.value("tag_name", ""); | ||
| const auto s_bIsPrerelease = s_ResponseJson.value("prerelease", false); | ||
| const auto s_bIsDraft = s_ResponseJson.value("draft", false); | ||
|
|
||
| if (s_sName.empty() || s_sTagName.empty()) | ||
| { | ||
| Logger::Error(TAG "Invalid response from update check: missing name or tag_name field!"); | ||
| { | ||
| std::lock_guard s_Lock(m_ResultMutex); | ||
| m_eResult = EResult::Failed; | ||
| } | ||
| return; | ||
| } | ||
|
|
||
| Logger::Debug(TAG "Got latest release: name={}, tag_name={} (build={}), prerelease={}, draft={}", s_sName, s_sTagName, BuildInfo::c_sBuildTag, s_bIsPrerelease, s_bIsDraft); | ||
|
|
||
| auto s_bUpdateAvailable = false; | ||
| if (!s_bIsPrerelease && !s_bIsDraft) | ||
| { | ||
| s_bUpdateAvailable = s_sTagName != BuildInfo::c_sBuildTag; | ||
| } | ||
|
|
||
| { | ||
| std::lock_guard s_Lock(m_ResultMutex); | ||
| m_sLatestTag = s_sTagName; | ||
| m_sLatestVersionName = s_sName; | ||
| m_eResult = s_bUpdateAvailable ? EResult::UpdateAvailable : EResult::UpToDate; | ||
| } | ||
| } | ||
|
|
||
| std::string ZUpdateCheck::GetUpdateUrl() const | ||
| { | ||
| return "https://github.com/" | ||
| + BuildInfo::c_sRepoOwner | ||
| + "/" | ||
| + BuildInfo::c_sRepoName | ||
| + "/releases/latest"; | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,58 @@ | ||
| #pragma once | ||
| #include <thread> | ||
| #include <mutex> | ||
|
shadow578 marked this conversation as resolved.
|
||
| #include <string> | ||
|
|
||
| class ZUpdateCheck | ||
| { | ||
| public: | ||
| enum class EResult | ||
|
shadow578 marked this conversation as resolved.
|
||
| { | ||
| None, | ||
| Checking, | ||
| UpToDate, | ||
| UpdateAvailable, | ||
| Failed | ||
| }; | ||
|
|
||
| ~ZUpdateCheck(); | ||
|
|
||
| /** | ||
| * Start an asynchronous check for updates. | ||
| * @note this function will only perform the check once, even when called multiple times. | ||
| */ | ||
| void CheckUpdatesAsync(); | ||
|
|
||
| /** | ||
| * Get the result of the update check. | ||
| */ | ||
| EResult GetResult() const | ||
| { | ||
| std::lock_guard s_Lock(m_ResultMutex); | ||
| return m_eResult; | ||
| } | ||
|
|
||
| /** | ||
| * Get the display name of the latest version as displayed on Github releases. | ||
| */ | ||
|
shadow578 marked this conversation as resolved.
|
||
| std::string GetLatestVersion() const | ||
| { | ||
| std::lock_guard s_Lock(m_ResultMutex); | ||
| return m_sLatestVersionName; | ||
| } | ||
|
|
||
| /** | ||
| * Get the update URL that a user can visit to download the latest version. | ||
| */ | ||
| std::string GetUpdateUrl() const; | ||
|
|
||
| private: | ||
| std::thread m_UpdateCheckThread; | ||
|
|
||
| mutable std::recursive_mutex m_ResultMutex; | ||
| EResult m_eResult = EResult::None; | ||
| std::string m_sLatestTag; | ||
| std::string m_sLatestVersionName; | ||
|
|
||
| void CheckUpdatesInternal(); | ||
| }; | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.