Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
60 changes: 40 additions & 20 deletions source/application.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -217,8 +217,8 @@ bool Application::OnInit() {
}
}
if (g_settings.getInteger(Config::USE_UPDATER) == 1) {
// UpdateChecker updater;
// updater.connect(g_gui.root);
UpdateChecker updater;
updater.connect(g_gui.root);
}
#endif

Expand Down Expand Up @@ -424,28 +424,48 @@ void MainFrame::OnIdle(wxIdleEvent& event) {
void MainFrame::OnUpdateReceived(wxCommandEvent& event) {
std::string data = *(std::string*)event.GetClientData();
delete (std::string*)event.GetClientData();
size_t first_colon = data.find(':');
size_t second_colon = data.find(':', first_colon + 1);

if (first_colon == std::string::npos || second_colon == std::string::npos) {
return;
}
try {
json::mValue val;
if (!json::read(data, val)) {
return;
}

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

if (update == "yes") {
int ret = g_gui.PopupDialog(
"Update Notice",
wxString("There is a newd update available (") << wxstr(verstr) << "). Do you want to go to the website and download it?",
wxYES | wxNO,
"I don't want any update notices",
Config::AUTOCHECK_FOR_UPDATES
);
if (ret == wxID_YES) {
::wxLaunchDefaultBrowser(wxstr(url), wxBROWSER_NEW_WINDOW);
std::string tag = obj["tag_name"].get_str();
if (!tag.empty() && (tag[0] == 'v' || tag[0] == 'V')) {
tag = tag.substr(1);
}

// Parse version
int major = 0, minor = 0, patch = 0;
sscanf(tag.c_str(), "%d.%d.%d", &major, &minor, &patch);

int remoteVersion = MAKE_VERSION_ID(major, minor, patch);
int localVersion = __RME_VERSION_ID__;

if (remoteVersion > localVersion) {
std::string url = obj["html_url"].get_str();
std::string name = obj["name"].get_str();

int ret = g_gui.PopupDialog(
"Update Notice",
wxString("There is a new update available (") << wxstr(name) << "). Do you want to go to the download page?",
wxYES | wxNO,
"I don't want any update notices",
Config::AUTOCHECK_FOR_UPDATES
);

if (ret == wxID_YES) {
::wxLaunchDefaultBrowser(wxstr(url), wxBROWSER_NEW_WINDOW);
}
}
} catch (...) {
// Error parsing JSON, silently ignore
}
}
#endif
Expand Down
6 changes: 6 additions & 0 deletions source/definitions.h
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@
)

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

// #define __PRERELEASE__ 1

Expand All @@ -56,6 +57,11 @@
#define __W_RME_VERSION__ (wxString() << __RME_VERSION_MAJOR__ << "." << __RME_VERSION_MINOR__ << "." << __RME_SUBVERSION__)
#endif
// OS
#if defined(_WIN32) || defined(WIN32) || defined(__WXMSW__)
#ifndef __WINDOWS__
#define __WINDOWS__ 1
#endif
#endif

#define OTGZ_SUPPORT 1
#define ASSETS_NAME "Tibia"
Expand Down
51 changes: 14 additions & 37 deletions source/updater.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,69 +19,46 @@

#ifdef _USE_UPDATER_

#include <wx/url.h>
#include <wx/sstream.h>
#include <wx/protocol/http.h>
#include <cpr/cpr.h>

#include "json.h"

#include "updater.h"

const wxEventType EVT_UPDATE_CHECK_FINISHED = wxNewEventType();

UpdateChecker::UpdateChecker() {
////
}

UpdateChecker::~UpdateChecker() {
////
}

void UpdateChecker::connect(wxEvtHandler* receiver) {
wxString address = "http://www.remeresmapeditor.com/update.php";
address << "?os="
<<
#ifdef __WINDOWS__
"windows";
#elif __LINUX__
"linux";
#else
"unknown";
#endif
address << "&verid=" << __RME_VERSION_ID__;
#ifdef __EXPERIMENTAL__
address << "&beta";
#endif
wxURL* url = newd wxURL(address);
UpdateConnectionThread* connection = newd UpdateConnectionThread(receiver, url);
UpdateConnectionThread* connection = newd UpdateConnectionThread(receiver);
connection->Execute();
}

UpdateConnectionThread::UpdateConnectionThread(wxEvtHandler* receiver, wxURL* url) :
receiver(receiver),
url(url) {
////
UpdateConnectionThread::UpdateConnectionThread(wxEvtHandler* receiver) :
receiver(receiver) {
}

UpdateConnectionThread::~UpdateConnectionThread() {
////
}

wxThread::ExitCode UpdateConnectionThread::Entry() {
wxInputStream* input = url->GetInputStream();
if (!input) {
delete input;
delete url;
return 0;
}
cpr::Response response = cpr::Get(
cpr::Url { __UPDATE_URL__ },
cpr::Header { { "User-Agent", "OTAcademy-RME-Updater" } },
cpr::Timeout { 10000 }
);

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

delete input;
delete url;
wxCommandEvent event(EVT_UPDATE_CHECK_FINISHED);
event.SetClientData(newd std::string(data));
event.SetClientData(newd std::string(response.text));
if (receiver) {
receiver->AddPendingEvent(event);
}
Expand Down
5 changes: 1 addition & 4 deletions source/updater.h
Original file line number Diff line number Diff line change
Expand Up @@ -31,17 +31,14 @@ extern const wxEventType EVT_UPDATE_CHECK_FINISHED;
(wxObject*)nullptr \
),

class wxURL;

class UpdateConnectionThread : public DetachedThread {
public:
UpdateConnectionThread(wxEvtHandler* receiver, wxURL* url);
UpdateConnectionThread(wxEvtHandler* receiver);
virtual ~UpdateConnectionThread();

protected:
virtual ExitCode Entry();
wxEvtHandler* receiver;
wxURL* url;
};

class UpdateChecker {
Expand Down