Skip to content

Commit 7593348

Browse files
authored
feat: Update version checker (#39)
* feat: update the updater version checker
1 parent 0866af2 commit 7593348

File tree

4 files changed

+61
-61
lines changed

4 files changed

+61
-61
lines changed

source/application.cpp

Lines changed: 40 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -217,8 +217,8 @@ bool Application::OnInit() {
217217
}
218218
}
219219
if (g_settings.getInteger(Config::USE_UPDATER) == 1) {
220-
// UpdateChecker updater;
221-
// updater.connect(g_gui.root);
220+
UpdateChecker updater;
221+
updater.connect(g_gui.root);
222222
}
223223
#endif
224224

@@ -424,28 +424,48 @@ void MainFrame::OnIdle(wxIdleEvent& event) {
424424
void MainFrame::OnUpdateReceived(wxCommandEvent& event) {
425425
std::string data = *(std::string*)event.GetClientData();
426426
delete (std::string*)event.GetClientData();
427-
size_t first_colon = data.find(':');
428-
size_t second_colon = data.find(':', first_colon + 1);
429427

430-
if (first_colon == std::string::npos || second_colon == std::string::npos) {
431-
return;
432-
}
428+
try {
429+
json::mValue val;
430+
if (!json::read(data, val)) {
431+
return;
432+
}
433433

434-
std::string update = data.substr(0, first_colon);
435-
std::string verstr = data.substr(first_colon + 1, second_colon - first_colon - 1);
436-
std::string url = (second_colon == data.size() ? "" : data.substr(second_colon + 1));
434+
json::mObject& obj = val.get_obj();
435+
if (obj.count("tag_name") == 0) {
436+
return;
437+
}
437438

438-
if (update == "yes") {
439-
int ret = g_gui.PopupDialog(
440-
"Update Notice",
441-
wxString("There is a newd update available (") << wxstr(verstr) << "). Do you want to go to the website and download it?",
442-
wxYES | wxNO,
443-
"I don't want any update notices",
444-
Config::AUTOCHECK_FOR_UPDATES
445-
);
446-
if (ret == wxID_YES) {
447-
::wxLaunchDefaultBrowser(wxstr(url), wxBROWSER_NEW_WINDOW);
439+
std::string tag = obj["tag_name"].get_str();
440+
if (!tag.empty() && (tag[0] == 'v' || tag[0] == 'V')) {
441+
tag = tag.substr(1);
442+
}
443+
444+
// Parse version
445+
int major = 0, minor = 0, patch = 0;
446+
sscanf(tag.c_str(), "%d.%d.%d", &major, &minor, &patch);
447+
448+
int remoteVersion = MAKE_VERSION_ID(major, minor, patch);
449+
int localVersion = __RME_VERSION_ID__;
450+
451+
if (remoteVersion > localVersion) {
452+
std::string url = obj["html_url"].get_str();
453+
std::string name = obj["name"].get_str();
454+
455+
int ret = g_gui.PopupDialog(
456+
"Update Notice",
457+
wxString("There is a new update available (") << wxstr(name) << "). Do you want to go to the download page?",
458+
wxYES | wxNO,
459+
"I don't want any update notices",
460+
Config::AUTOCHECK_FOR_UPDATES
461+
);
462+
463+
if (ret == wxID_YES) {
464+
::wxLaunchDefaultBrowser(wxstr(url), wxBROWSER_NEW_WINDOW);
465+
}
448466
}
467+
} catch (...) {
468+
// Error parsing JSON, silently ignore
449469
}
450470
}
451471
#endif

source/definitions.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@
3939
)
4040

4141
#define __SITE_URL__ "https://github.com/OTAcademy/RME"
42+
#define __UPDATE_URL__ "https://api.github.com/repos/OTAcademy/RME/releases/latest"
4243

4344
// #define __PRERELEASE__ 1
4445

@@ -56,6 +57,11 @@
5657
#define __W_RME_VERSION__ (wxString() << __RME_VERSION_MAJOR__ << "." << __RME_VERSION_MINOR__ << "." << __RME_SUBVERSION__)
5758
#endif
5859
// OS
60+
#if defined(_WIN32) || defined(WIN32) || defined(__WXMSW__)
61+
#ifndef __WINDOWS__
62+
#define __WINDOWS__ 1
63+
#endif
64+
#endif
5965

6066
#define OTGZ_SUPPORT 1
6167
#define ASSETS_NAME "Tibia"

source/updater.cpp

Lines changed: 14 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -19,69 +19,46 @@
1919

2020
#ifdef _USE_UPDATER_
2121

22-
#include <wx/url.h>
22+
#include <wx/sstream.h>
23+
#include <wx/protocol/http.h>
24+
#include <cpr/cpr.h>
2325

2426
#include "json.h"
25-
2627
#include "updater.h"
2728

2829
const wxEventType EVT_UPDATE_CHECK_FINISHED = wxNewEventType();
2930

3031
UpdateChecker::UpdateChecker() {
31-
////
3232
}
3333

3434
UpdateChecker::~UpdateChecker() {
35-
////
3635
}
3736

3837
void UpdateChecker::connect(wxEvtHandler* receiver) {
39-
wxString address = "http://www.remeresmapeditor.com/update.php";
40-
address << "?os="
41-
<<
42-
#ifdef __WINDOWS__
43-
"windows";
44-
#elif __LINUX__
45-
"linux";
46-
#else
47-
"unknown";
48-
#endif
49-
address << "&verid=" << __RME_VERSION_ID__;
50-
#ifdef __EXPERIMENTAL__
51-
address << "&beta";
52-
#endif
53-
wxURL* url = newd wxURL(address);
54-
UpdateConnectionThread* connection = newd UpdateConnectionThread(receiver, url);
38+
UpdateConnectionThread* connection = newd UpdateConnectionThread(receiver);
5539
connection->Execute();
5640
}
5741

58-
UpdateConnectionThread::UpdateConnectionThread(wxEvtHandler* receiver, wxURL* url) :
59-
receiver(receiver),
60-
url(url) {
61-
////
42+
UpdateConnectionThread::UpdateConnectionThread(wxEvtHandler* receiver) :
43+
receiver(receiver) {
6244
}
6345

6446
UpdateConnectionThread::~UpdateConnectionThread() {
65-
////
6647
}
6748

6849
wxThread::ExitCode UpdateConnectionThread::Entry() {
69-
wxInputStream* input = url->GetInputStream();
70-
if (!input) {
71-
delete input;
72-
delete url;
73-
return 0;
74-
}
50+
cpr::Response response = cpr::Get(
51+
cpr::Url { __UPDATE_URL__ },
52+
cpr::Header { { "User-Agent", "OTAcademy-RME-Updater" } },
53+
cpr::Timeout { 10000 }
54+
);
7555

76-
std::string data;
77-
while (!input->Eof()) {
78-
data += input->GetC();
56+
if (response.status_code != 200 || response.text.empty()) {
57+
return 0;
7958
}
8059

81-
delete input;
82-
delete url;
8360
wxCommandEvent event(EVT_UPDATE_CHECK_FINISHED);
84-
event.SetClientData(newd std::string(data));
61+
event.SetClientData(newd std::string(response.text));
8562
if (receiver) {
8663
receiver->AddPendingEvent(event);
8764
}

source/updater.h

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -31,17 +31,14 @@ extern const wxEventType EVT_UPDATE_CHECK_FINISHED;
3131
(wxObject*)nullptr \
3232
),
3333

34-
class wxURL;
35-
3634
class UpdateConnectionThread : public DetachedThread {
3735
public:
38-
UpdateConnectionThread(wxEvtHandler* receiver, wxURL* url);
36+
UpdateConnectionThread(wxEvtHandler* receiver);
3937
virtual ~UpdateConnectionThread();
4038

4139
protected:
4240
virtual ExitCode Entry();
4341
wxEvtHandler* receiver;
44-
wxURL* url;
4542
};
4643

4744
class UpdateChecker {

0 commit comments

Comments
 (0)