Skip to content

Commit

Permalink
Use wx for web requests instead of SFML
Browse files Browse the repository at this point in the history
Hopefully this fixes the startup crash on linux with the start page enabled
  • Loading branch information
sirjuddington committed May 27, 2024
1 parent 60f3096 commit bba8542
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 23 deletions.
20 changes: 13 additions & 7 deletions src/Application/SLADEWxApp.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,6 @@
#include "App.h"
#include "Archive/ArchiveManager.h"
#include "General/Console.h"
#include "General/Web.h"
#include "MainEditor/MainEditor.h"
#include "MainEditor/UI/ArchiveManagerPanel.h"
#include "MainEditor/UI/MainWindow.h"
Expand All @@ -46,6 +45,7 @@
#include <wx/filefn.h>
#include <wx/statbmp.h>
#include <wx/url.h>
#include <wx/webrequest.h>
#undef BOOL
#ifdef UPDATEREVISION
#include "gitinfo.h"
Expand Down Expand Up @@ -510,7 +510,7 @@ bool SLADEWxApp::OnInit()

// Bind events
Bind(wxEVT_MENU, &SLADEWxApp::onMenu, this);
Bind(wxEVT_THREAD_WEBGET_COMPLETED, &SLADEWxApp::onVersionCheckCompleted, this);
Bind(wxEVT_WEBREQUEST_STATE, &SLADEWxApp::onVersionCheckCompleted, this);
Bind(wxEVT_ACTIVATE_APP, &SLADEWxApp::onActivate, this);
Bind(wxEVT_QUERY_END_SESSION, &SLADEWxApp::onEndSession, this);

Expand Down Expand Up @@ -561,7 +561,8 @@ void SLADEWxApp::checkForUpdates(bool message_box)
#ifdef __WXMSW__
update_check_message_box = message_box;
log::info(1, "Checking for updates...");
web::getHttpAsync("slade.mancubus.net", "/version_win.txt", this);
auto request = wxWebSession::GetDefault().CreateRequest(this, "https://slade.mancubus.net/version_win.txt");
request.Start();
#endif
}

Expand Down Expand Up @@ -612,10 +613,10 @@ void SLADEWxApp::onMenu(wxCommandEvent& e)
// -----------------------------------------------------------------------------
// Called when the version check thread completes
// -----------------------------------------------------------------------------
void SLADEWxApp::onVersionCheckCompleted(wxThreadEvent& e)
void SLADEWxApp::onVersionCheckCompleted(wxWebRequestEvent& e)
{
// Check failed
if (e.GetString() == "connect_failed")
if (e.GetState() == wxWebRequest::State_Failed || e.GetState() == wxWebRequest::State_Unauthorized)
{
log::error("Version check failed, unable to connect");
if (update_check_message_box)
Expand All @@ -627,11 +628,16 @@ void SLADEWxApp::onVersionCheckCompleted(wxThreadEvent& e)
return;
}

// If not completed, ignore
if (e.GetState() != wxWebRequest::State_Completed)
return;

// Parse version info
app::Version stable, beta;
string bin_stable, installer_stable, bin_beta; // Currently unused but may be useful in the future
Parser parser;
if (parser.parseText(e.GetString().ToStdString()))
auto response_string = e.GetResponse().AsString();
if (parser.parseText(response_string.ToStdString()))
{
// Stable
auto node_stable = parser.parseTreeRoot()->childPTN("stable");
Expand Down Expand Up @@ -686,7 +692,7 @@ void SLADEWxApp::onVersionCheckCompleted(wxThreadEvent& e)
if (stable.major == 0 || beta.major == 0)
{
log::warning("Version check failed, received invalid version info");
log::debug("Received version text:\n\n%s", wxutil::strToView(e.GetString()));
log::debug("Received version text:\n\n%s", wxutil::strToView(response_string));
if (update_check_message_box)
wxMessageBox("Update check failed: received invalid version info.", "Check for Updates");
return;
Expand Down
3 changes: 2 additions & 1 deletion src/Application/SLADEWxApp.h
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#pragma once

class wxWebRequestEvent;
class wxSingleInstanceChecker;
class MainAppFileListener;
class SLADECrashDialog;
Expand All @@ -24,7 +25,7 @@ class SLADEWxApp : public wxApp
void checkForUpdates(bool message_box);

void onMenu(wxCommandEvent& e);
void onVersionCheckCompleted(wxThreadEvent& e);
void onVersionCheckCompleted(wxWebRequestEvent& e);
void onActivate(wxActivateEvent& e);
void onEndSession(wxCloseEvent& e);

Expand Down
38 changes: 23 additions & 15 deletions src/MainEditor/UI/StartPage.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -37,9 +37,8 @@
#include "App.h"
#include "Archive/ArchiveManager.h"
#include "General/SAction.h"
#include "General/Web.h"
#include "Graphics/Icons.h"
#include "Utility/Tokenizer.h"
#include <wx/webrequest.h>

using namespace slade;

Expand Down Expand Up @@ -112,16 +111,22 @@ void SStartPage::init()
}

Bind(
wxEVT_THREAD_WEBGET_COMPLETED,
[&](wxThreadEvent& e)
wxEVT_WEBREQUEST_STATE,
[&](wxWebRequestEvent& e)
{
latest_news_ = e.GetString();
latest_news_.Trim();

if (latest_news_ == "connect_failed" || latest_news_.empty())
switch (e.GetState())
{
case wxWebRequest::State_Failed:
case wxWebRequest::State_Unauthorized:
latest_news_ = "<center>Unable to load latest SLADE news</center>";

load(false);
load(false);
break;
case wxWebRequest::State_Completed:
latest_news_ = e.GetResponse().AsString().Trim();
load(false);
break;
default: break;
}
});

#else
Expand Down Expand Up @@ -177,7 +182,10 @@ void SStartPage::load(bool new_tip)
{
// Get latest news post
if (latest_news_.empty())
web::getHttpAsync("slade.mancubus.net", "/news-latest.php", this);
{
auto request = wxWebSession::GetDefault().CreateRequest(this, "https://slade.mancubus.net/news-latest.php");
request.Start();
}

// Can't do anything without html entry
if (!entry_base_html_)
Expand Down Expand Up @@ -329,7 +337,7 @@ void SStartPage::load(bool new_tip)
}

last_tip_index_ = tipindex;
tip = tips_[tipindex];
tip = tips_[tipindex];
// log::debug(wxString::Format("Tip index %d/%lu", last_tip_index_, (int)tips_.size()));
}

Expand Down Expand Up @@ -482,15 +490,15 @@ void SStartPage::onHTMLLinkClicked(wxEvent& e)
// -----------------------------------------------------------------------------
void SStartPage::onHTMLLinkClicked(wxEvent& e)
{
wxHtmlLinkEvent& ev = (wxHtmlLinkEvent&)e;
wxString href = ev.GetLinkInfo().GetHref();
wxHtmlLinkEvent& ev = (wxHtmlLinkEvent&)e;
wxString href = ev.GetLinkInfo().GetHref();

if (href.StartsWith("http://") || href.StartsWith("https://"))
wxLaunchDefaultBrowser(ev.GetLinkInfo().GetHref());
else if (href.StartsWith("recent://"))
{
// Recent file
wxString rs = href.Mid(9);
wxString rs = href.Mid(9);
unsigned long index = 0;
rs.ToULong(&index);
SActionHandler::setWxIdOffset(index);
Expand Down

0 comments on commit bba8542

Please sign in to comment.