|
|
|
@ -1,5 +1,6 @@
|
|
|
|
|
#include <STDInclude.hpp>
|
|
|
|
|
#include <Utils/InfoString.hpp>
|
|
|
|
|
#include <Utils/WebIO.hpp>
|
|
|
|
|
|
|
|
|
|
#include "Discovery.hpp"
|
|
|
|
|
#include "Events.hpp"
|
|
|
|
@ -10,6 +11,10 @@
|
|
|
|
|
#include "Toast.hpp"
|
|
|
|
|
#include "UIFeeder.hpp"
|
|
|
|
|
|
|
|
|
|
#include <rapidjson/document.h>
|
|
|
|
|
#include <rapidjson/prettywriter.h>
|
|
|
|
|
#include <rapidjson/stringbuffer.h>
|
|
|
|
|
|
|
|
|
|
namespace Components
|
|
|
|
|
{
|
|
|
|
|
bool ServerList::SortAsc = true;
|
|
|
|
@ -24,6 +29,8 @@ namespace Components
|
|
|
|
|
|
|
|
|
|
std::vector<unsigned int> ServerList::VisibleList;
|
|
|
|
|
|
|
|
|
|
bool ServerList::UseMasterServer = false;
|
|
|
|
|
|
|
|
|
|
Dvar::Var ServerList::UIServerSelected;
|
|
|
|
|
Dvar::Var ServerList::UIServerSelectedMap;
|
|
|
|
|
Dvar::Var ServerList::NETServerQueryLimit;
|
|
|
|
@ -271,6 +278,69 @@ namespace Components
|
|
|
|
|
SortList();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void ServerList::ParseNewMasterServerResponse(const std::string& servers)
|
|
|
|
|
{
|
|
|
|
|
std::lock_guard _(RefreshContainer.mutex);
|
|
|
|
|
|
|
|
|
|
rapidjson::Document doc{};
|
|
|
|
|
const rapidjson::ParseResult result = doc.Parse(servers);
|
|
|
|
|
if (!result || !doc.IsObject())
|
|
|
|
|
{
|
|
|
|
|
UseMasterServer = false;
|
|
|
|
|
Logger::Print("Unable to parse JSON response. Using the Node System\n");
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (!doc.HasMember("servers"))
|
|
|
|
|
{
|
|
|
|
|
UseMasterServer = false;
|
|
|
|
|
Logger::Print("Unable to parse JSON response: we were unable to find any server. Using the Node System\n");
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const rapidjson::Value& list = doc["servers"];
|
|
|
|
|
if (!list.IsArray() || list.Empty())
|
|
|
|
|
{
|
|
|
|
|
UseMasterServer = false;
|
|
|
|
|
Logger::Print("Unable to parse JSON response: we were unable to find any server. Using the Node System\n");
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Logger::Print("Response from the master server contains {} servers\n", list.Size());
|
|
|
|
|
|
|
|
|
|
std::size_t count = 0;
|
|
|
|
|
|
|
|
|
|
for (const auto& entry : list.GetArray())
|
|
|
|
|
{
|
|
|
|
|
if (!entry.HasMember("ip") || !entry.HasMember("port"))
|
|
|
|
|
{
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (!entry["ip"].IsString() || !entry["port"].IsInt())
|
|
|
|
|
{
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Using VA because it's faster
|
|
|
|
|
Network::Address server(Utils::String::VA("%s:%u", entry["ip"].GetString(), entry["port"].GetInt()));
|
|
|
|
|
server.setType(Game::NA_IP); // Just making sure...
|
|
|
|
|
|
|
|
|
|
InsertRequest(server);
|
|
|
|
|
++count;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (!count)
|
|
|
|
|
{
|
|
|
|
|
UseMasterServer = false;
|
|
|
|
|
Logger::Print("Despite receiving what looked like a valid response from the master server, we got {} servers. Using the Node System\n", count);
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
UseMasterServer = true;
|
|
|
|
|
Logger::Print("Response from the master server was successfully parsed. We got {} servers\n", count);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void ServerList::Refresh([[maybe_unused]] const UIScript::Token& token, [[maybe_unused]] const Game::uiInfo_s* info)
|
|
|
|
|
{
|
|
|
|
|
Dvar::Var("ui_serverSelected").set(false);
|
|
|
|
@ -294,29 +364,30 @@ namespace Components
|
|
|
|
|
#ifdef IW4_USE_MASTER_SERVER
|
|
|
|
|
else if (IsOnlineList())
|
|
|
|
|
{
|
|
|
|
|
const auto masterPort = (*Game::com_masterPort)->current.integer;
|
|
|
|
|
const auto masterPort = (*Game::com_masterPort)->current.unsignedInt;
|
|
|
|
|
const auto* masterServerName = (*Game::com_masterServerName)->current.string;
|
|
|
|
|
|
|
|
|
|
// Check if our dvars can properly convert to a address
|
|
|
|
|
Game::netadr_t masterServerAddr;
|
|
|
|
|
if (!GetMasterServer(masterServerName, masterPort, masterServerAddr))
|
|
|
|
|
RefreshContainer.awatingList = true;
|
|
|
|
|
RefreshContainer.awaitTime = Game::Sys_Milliseconds();
|
|
|
|
|
|
|
|
|
|
Toast::Show("cardicon_headshot", "Server Browser", "Fetching servers...", 3000);
|
|
|
|
|
|
|
|
|
|
const auto* url = "http://iw4x.plutools.pw/v1/servers/iw4x";
|
|
|
|
|
const auto reply = Utils::WebIO("IW4x", url).setTimeout(5000)->get();
|
|
|
|
|
if (reply.empty())
|
|
|
|
|
{
|
|
|
|
|
Logger::Print("Could not resolve address for {}:{}", masterServerName, masterPort);
|
|
|
|
|
Toast::Show("cardicon_headshot", "^1Error", std::format("Could not resolve address for {}:{}", masterServerName, masterPort), 5000);
|
|
|
|
|
Logger::Print("Response from {} was empty or the request timed out. Using the Node System\n", url);
|
|
|
|
|
Toast::Show("cardicon_headshot", "^1Error", std::format("Could not get a response from {}. Using the Node System", url), 5000);
|
|
|
|
|
UseMasterServer = false;
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Toast::Show("cardicon_headshot", "Server Browser", "Fetching servers...", 3000);
|
|
|
|
|
RefreshContainer.awatingList = false;
|
|
|
|
|
|
|
|
|
|
UseMasterServer = true;
|
|
|
|
|
ParseNewMasterServerResponse(reply);
|
|
|
|
|
|
|
|
|
|
RefreshContainer.awatingList = true;
|
|
|
|
|
RefreshContainer.awaitTime = Game::Sys_Milliseconds();
|
|
|
|
|
// TODO: Figure out what to do with this. Leave it to avoid breaking other code
|
|
|
|
|
RefreshContainer.host = Network::Address(std::format("{}:{}", masterServerName, masterPort));
|
|
|
|
|
|
|
|
|
|
Logger::Print("Sending server list request to master\n");
|
|
|
|
|
Network::SendCommand(RefreshContainer.host, "getservers", std::format("IW4 {} full empty", PROTOCOL));
|
|
|
|
|
}
|
|
|
|
|
#endif
|
|
|
|
|
else if (IsFavouriteList())
|
|
|
|
@ -729,7 +800,9 @@ namespace Components
|
|
|
|
|
if (Game::Sys_Milliseconds() - RefreshContainer.awaitTime > 5000)
|
|
|
|
|
{
|
|
|
|
|
RefreshContainer.awatingList = false;
|
|
|
|
|
Logger::Print("We haven't received a response from the master within {} seconds!\n", (Game::Sys_Milliseconds() - RefreshContainer.awaitTime) / 1000);
|
|
|
|
|
|
|
|
|
|
UseMasterServer = false;
|
|
|
|
|
Node::Synchronize();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|