From 50b02fc4c3e32dcf26e8f7c81f1240664ef74019 Mon Sep 17 00:00:00 2001 From: Aleksoid Date: Wed, 14 Feb 2024 11:35:29 +1000 Subject: [PATCH] =?UTF-8?q?Updater=20-=20=D1=82=D0=B5=D0=BF=D0=B5=D1=80?= =?UTF-8?q?=D1=8C=20=D0=BF=D1=80=D0=BE=D0=B2=D0=B5=D1=80=D1=8F=D0=B5=D1=82?= =?UTF-8?q?=20=D0=BD=D0=B0=D0=BB=D0=B8=D1=87=D0=B8=D0=B5=20=D0=BD=D0=BE?= =?UTF-8?q?=D0=B2=D0=BE=D0=B9=20=D0=B2=D0=B5=D1=80=D1=81=D0=B8=D0=B8=20?= =?UTF-8?q?=D1=87=D0=B5=D1=80=D0=B5=D0=B7=20github=20api.=20=D0=9D=D0=B5?= =?UTF-8?q?=D0=B1=D0=BE=D0=BB=D1=8C=D1=88=D0=B0=D1=8F=20=D0=BA=D0=BE=D1=80?= =?UTF-8?q?=D1=80=D0=B5=D0=BA=D1=82=D0=B8=D1=80=D0=BE=D0=B2=D0=BA=D0=B0=20?= =?UTF-8?q?CHTTPAsync::Read().?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/DSUtil/HTTPAsync.cpp | 2 + src/apps/mplayerc/UpdateChecker.cpp | 92 ++++++++++++----------------- src/apps/mplayerc/UpdateChecker.h | 18 +++--- 3 files changed, 50 insertions(+), 62 deletions(-) diff --git a/src/DSUtil/HTTPAsync.cpp b/src/DSUtil/HTTPAsync.cpp index 4511ac9b5c..f6e0c0743d 100644 --- a/src/DSUtil/HTTPAsync.cpp +++ b/src/DSUtil/HTTPAsync.cpp @@ -464,6 +464,8 @@ HRESULT CHTTPAsync::ReadInternal(PBYTE pBuffer, DWORD dwSizeToRead, DWORD& dwSiz HRESULT CHTTPAsync::Read(PBYTE pBuffer, DWORD dwSizeToRead, DWORD& dwSizeRead, DWORD dwTimeOut/* = INFINITE*/) { + dwSizeRead = 0; + if (m_http_chunk.use) { HRESULT hr = S_OK; auto begin = pBuffer; diff --git a/src/apps/mplayerc/UpdateChecker.cpp b/src/apps/mplayerc/UpdateChecker.cpp index 37a6edd71d..0c390d802b 100644 --- a/src/apps/mplayerc/UpdateChecker.cpp +++ b/src/apps/mplayerc/UpdateChecker.cpp @@ -1,5 +1,5 @@ /* - * (C) 2013-2023 see Authors.txt + * (C) 2013-2024 see Authors.txt * * This file is part of MPC-BE. * @@ -22,25 +22,14 @@ #include #include "DSUtil/SysVersion.h" #include "DSUtil/text.h" +#include "DSUtil/HTTPAsync.h" +#include "rapidjsonHelper.h" #include "UpdateChecker.h" #include "Version.h" // UpdateChecker -bool UpdateChecker::bUpdating = false; -CCritSec UpdateChecker::csUpdating; -Version UpdateChecker::m_UpdateVersion = { 0, 0, 0, 0 }; -CStringA UpdateChecker::m_UpdateURL; - -UpdateChecker::UpdateChecker() -{ -} - -UpdateChecker::~UpdateChecker(void) -{ -} - bool UpdateChecker::IsTimeToAutoUpdate(int delay, time_t lastcheck) { return (time(nullptr) >= lastcheck + delay * 24 * 3600); @@ -59,41 +48,43 @@ void UpdateChecker::CheckForUpdate(bool autocheck) Update_Status UpdateChecker::CheckNewVersion() { m_UpdateURL.Empty(); - m_UpdateVersion = { 0, 0, 0, 0 }; + m_UpdateVersion = {}; Update_Status updatestatus = UPDATER_ERROR_CONNECT; - CStringA updateinfo; - - HINTERNET hInet = InternetOpenW(L"MPC-BE", 0, nullptr, nullptr, 0); - if (hInet) { - HINTERNET hUrl = InternetOpenUrlW(hInet, L"http://mpc-be.org/version.txt", nullptr, 0, INTERNET_FLAG_TRANSFER_BINARY | INTERNET_FLAG_EXISTING_CONNECT | INTERNET_FLAG_NO_CACHE_WRITE | INTERNET_FLAG_RELOAD, 0); - if (hUrl) { - char buffer[1024] = { 0 }; // limit update file to 1024 bytes - DWORD dwBytesRead = 0; - - if (InternetReadFile(hUrl, (LPVOID)buffer, std::size(buffer), &dwBytesRead) == TRUE && dwBytesRead < std::size(buffer)) { - updateinfo = CStringA(buffer); - } - InternetCloseHandle(hUrl); + CHTTPAsync HTTPAsync; + if (SUCCEEDED(HTTPAsync.Connect(L"https://api.github.com/repos/Aleksoid1978/MPC-BE/releases/latest", 3000))) { + constexpr auto sizeRead = 16 * KILOBYTE; + CStringA data; + DWORD dwSizeRead = 0; + int dataSize = 0; + while (S_OK == HTTPAsync.Read(reinterpret_cast(data.GetBuffer(dataSize + sizeRead) + dataSize), sizeRead, dwSizeRead)) { + data.ReleaseBuffer(dataSize + dwSizeRead); + dataSize = data.GetLength(); + dwSizeRead = 0; } - InternetCloseHandle(hInet); - } - - if (updateinfo.GetLength()) { - updatestatus = UPDATER_ERROR_DATA; - int pos = 0; - CStringA updateversion = updateinfo.Tokenize("\r\n", pos).Trim(); - m_UpdateURL = updateinfo.Tokenize("\r\n", pos).Trim(); - - int n = sscanf_s(updateversion, "%u.%u.%u.%u", &m_UpdateVersion.major, &m_UpdateVersion.minor, &m_UpdateVersion.patch, &m_UpdateVersion.revision); - if (n == 4 && (StartsWith(m_UpdateURL, "http://") || StartsWith(m_UpdateURL, "https://"))) { - if (MPC_VERSION_MAJOR < m_UpdateVersion.major - || MPC_VERSION_MAJOR == m_UpdateVersion.major && MPC_VERSION_MINOR < m_UpdateVersion.minor - || MPC_VERSION_MAJOR == m_UpdateVersion.major && MPC_VERSION_MINOR == m_UpdateVersion.minor && MPC_VERSION_PATCH < m_UpdateVersion.patch - || MPC_VERSION_MAJOR == m_UpdateVersion.major && MPC_VERSION_MINOR == m_UpdateVersion.minor && MPC_VERSION_PATCH == m_UpdateVersion.patch && MPC_VERSION_REV < m_UpdateVersion.revision) { - updatestatus = UPDATER_NEW_VERSION_IS_AVAILABLE; - } else { - updatestatus = UPDATER_NO_NEW_VERSION; + data.ReleaseBuffer(dataSize); + + if (!data.IsEmpty()) { + updatestatus = UPDATER_ERROR_DATA; + rapidjson::Document json; + if (!json.Parse(data.GetString()).HasParseError()) { + CString tag_name; + if (getJsonValue(json, "tag_name", tag_name)) { + int n = swscanf_s(tag_name, L"%u.%u.%u.%u", + &m_UpdateVersion.major, &m_UpdateVersion.minor, &m_UpdateVersion.patch, &m_UpdateVersion.revision); + if (n == 3 || n == 4) { + if (getJsonValue(json, "html_url", m_UpdateURL)) { + if (MPC_VERSION_MAJOR < m_UpdateVersion.major + || MPC_VERSION_MAJOR == m_UpdateVersion.major && MPC_VERSION_MINOR < m_UpdateVersion.minor + || MPC_VERSION_MAJOR == m_UpdateVersion.major && MPC_VERSION_MINOR == m_UpdateVersion.minor && MPC_VERSION_PATCH < m_UpdateVersion.patch + || MPC_VERSION_MAJOR == m_UpdateVersion.major && MPC_VERSION_MINOR == m_UpdateVersion.minor && MPC_VERSION_PATCH == m_UpdateVersion.patch && MPC_VERSION_REV < m_UpdateVersion.revision) { + updatestatus = UPDATER_NEW_VERSION_IS_AVAILABLE; + } else { + updatestatus = UPDATER_NO_NEW_VERSION; + } + } + } + } } } } @@ -106,7 +97,6 @@ UINT UpdateChecker::RunCheckForUpdateThread(LPVOID pParam) bool autocheck = !!pParam; Update_Status updatestatus = CheckNewVersion(); - if (!autocheck || updatestatus == UPDATER_NEW_VERSION_IS_AVAILABLE) { UpdateCheckerDlg dlg(updatestatus, m_UpdateVersion, m_UpdateURL); dlg.DoModal(); @@ -125,8 +115,8 @@ UINT UpdateChecker::RunCheckForUpdateThread(LPVOID pParam) IMPLEMENT_DYNAMIC(UpdateCheckerDlg, CDialog) -UpdateCheckerDlg::UpdateCheckerDlg(Update_Status updateStatus, Version UpdateVersion, LPCSTR UpdateURL, CWnd* pParent) - : CDialog(UpdateCheckerDlg::IDD, pParent), m_updateStatus(updateStatus) +UpdateCheckerDlg::UpdateCheckerDlg(Update_Status updateStatus, Version UpdateVersion, LPCWSTR UpdateURL) + : CDialog(UpdateCheckerDlg::IDD), m_updateStatus(updateStatus) { CString VersionStr; @@ -155,10 +145,6 @@ UpdateCheckerDlg::UpdateCheckerDlg(Update_Status updateStatus, Version UpdateVer } } -UpdateCheckerDlg::~UpdateCheckerDlg() -{ -} - void UpdateCheckerDlg::DoDataExchange(CDataExchange* pDX) { CDialog::DoDataExchange(pDX); diff --git a/src/apps/mplayerc/UpdateChecker.h b/src/apps/mplayerc/UpdateChecker.h index 5ebf13b4e5..a2cb1c42ef 100644 --- a/src/apps/mplayerc/UpdateChecker.h +++ b/src/apps/mplayerc/UpdateChecker.h @@ -1,5 +1,5 @@ /* - * (C) 2013-2017 see Authors.txt + * (C) 2013-2024 see Authors.txt * * This file is part of MPC-BE. * @@ -41,15 +41,15 @@ enum Update_Status class UpdateChecker { - static bool bUpdating; - static CCritSec csUpdating; + static inline bool bUpdating = false; + static inline CCritSec csUpdating; - static Version m_UpdateVersion; - static CStringA m_UpdateURL; + static inline Version m_UpdateVersion = {}; + static inline CString m_UpdateURL; public: - UpdateChecker(); - ~UpdateChecker(void); + UpdateChecker() = default; + ~UpdateChecker() = default; static bool IsTimeToAutoUpdate(int delay, time_t lastcheck); static void CheckForUpdate(bool autocheck = false); @@ -64,8 +64,8 @@ class UpdateCheckerDlg : public CDialog DECLARE_DYNAMIC(UpdateCheckerDlg) public: - UpdateCheckerDlg(Update_Status updateStatus, Version UpdateVersion, LPCSTR UpdateURL, CWnd* pParent = nullptr); - virtual ~UpdateCheckerDlg(); + UpdateCheckerDlg(Update_Status updateStatus, Version UpdateVersion, LPCWSTR UpdateURL); + ~UpdateCheckerDlg() = default; enum { IDD = IDD_UPDATE_DIALOG };