2017-02-11 12:40:19 +00:00
|
|
|
/*****************************************************************************
|
2019-03-17 08:16:15 +01:00
|
|
|
* Copyright (c) 2014-2019 OpenRCT2 developers
|
2017-02-11 12:40:19 +00:00
|
|
|
*
|
2018-06-15 14:07:34 +02:00
|
|
|
* For a complete list of all authors, please refer to contributors.md
|
|
|
|
|
* Interested in contributing? Visit https://github.com/OpenRCT2/OpenRCT2
|
2017-02-11 12:40:19 +00:00
|
|
|
*
|
2018-06-15 14:07:34 +02:00
|
|
|
* OpenRCT2 is licensed under the GNU General Public License version 3.
|
2017-02-11 12:40:19 +00:00
|
|
|
*****************************************************************************/
|
|
|
|
|
|
2018-06-22 23:02:47 +02:00
|
|
|
#include "ServerList.h"
|
|
|
|
|
|
|
|
|
|
#include "../Context.h"
|
|
|
|
|
#include "../PlatformEnvironment.h"
|
2019-05-05 13:15:46 +00:00
|
|
|
#include "../config/Config.h"
|
2017-02-11 12:40:19 +00:00
|
|
|
#include "../core/FileStream.hpp"
|
2019-05-05 13:15:46 +00:00
|
|
|
#include "../core/Json.hpp"
|
2018-01-26 13:13:51 +00:00
|
|
|
#include "../core/Memory.hpp"
|
2017-02-11 12:40:19 +00:00
|
|
|
#include "../core/Path.hpp"
|
|
|
|
|
#include "../core/String.hpp"
|
2019-05-05 13:15:46 +00:00
|
|
|
#include "../network/Http.h"
|
2018-01-26 13:13:51 +00:00
|
|
|
#include "../platform/platform.h"
|
2019-05-05 13:15:46 +00:00
|
|
|
#include "UdpSocket.h"
|
|
|
|
|
#include "network.h"
|
|
|
|
|
|
|
|
|
|
#include <algorithm>
|
|
|
|
|
#include <numeric>
|
2017-02-11 12:40:19 +00:00
|
|
|
|
2018-02-07 13:01:36 +00:00
|
|
|
using namespace OpenRCT2;
|
2019-05-05 13:15:46 +00:00
|
|
|
using namespace OpenRCT2::Network;
|
2018-02-07 13:01:36 +00:00
|
|
|
|
2019-05-05 13:15:46 +00:00
|
|
|
int32_t ServerListEntry::CompareTo(const ServerListEntry& other) const
|
|
|
|
|
{
|
|
|
|
|
const auto& a = *this;
|
|
|
|
|
const auto& b = other;
|
|
|
|
|
|
|
|
|
|
// Order by favourite
|
|
|
|
|
if (a.favourite != b.favourite)
|
|
|
|
|
{
|
|
|
|
|
return a.favourite ? -1 : 1;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Then by version
|
|
|
|
|
bool serverACompatible = a.version == network_get_version();
|
|
|
|
|
bool serverBCompatible = b.version == network_get_version();
|
|
|
|
|
if (serverACompatible != serverBCompatible)
|
|
|
|
|
{
|
|
|
|
|
return serverACompatible ? 1 : -1;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Then by password protection
|
|
|
|
|
if (a.requiresPassword != b.requiresPassword)
|
|
|
|
|
{
|
|
|
|
|
return a.requiresPassword ? -1 : 1;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Then by name
|
|
|
|
|
return String::Compare(a.name, b.name, true);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bool ServerListEntry::IsVersionValid() const
|
|
|
|
|
{
|
|
|
|
|
return version.empty() || version == network_get_version();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
std::optional<ServerListEntry> ServerListEntry::FromJson(const json_t* server)
|
|
|
|
|
{
|
|
|
|
|
auto port = json_object_get(server, "port");
|
|
|
|
|
auto name = json_object_get(server, "name");
|
|
|
|
|
auto description = json_object_get(server, "description");
|
|
|
|
|
auto requiresPassword = json_object_get(server, "requiresPassword");
|
|
|
|
|
auto version = json_object_get(server, "version");
|
|
|
|
|
auto players = json_object_get(server, "players");
|
|
|
|
|
auto maxPlayers = json_object_get(server, "maxPlayers");
|
|
|
|
|
auto ip = json_object_get(server, "ip");
|
|
|
|
|
auto ip4 = json_object_get(ip, "v4");
|
|
|
|
|
auto addressIp = json_array_get(ip4, 0);
|
|
|
|
|
|
|
|
|
|
if (name == nullptr || version == nullptr)
|
|
|
|
|
{
|
|
|
|
|
log_verbose("Cowardly refusing to add server without name or version specified.");
|
|
|
|
|
return {};
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
ServerListEntry entry;
|
|
|
|
|
entry.address = String::StdFormat("%s:%d", json_string_value(addressIp), (int32_t)json_integer_value(port));
|
|
|
|
|
entry.name = (name == nullptr ? "" : json_string_value(name));
|
|
|
|
|
entry.description = (description == nullptr ? "" : json_string_value(description));
|
|
|
|
|
entry.version = json_string_value(version);
|
|
|
|
|
entry.requiresPassword = json_is_true(requiresPassword);
|
|
|
|
|
entry.players = (uint8_t)json_integer_value(players);
|
|
|
|
|
entry.maxplayers = (uint8_t)json_integer_value(maxPlayers);
|
|
|
|
|
return entry;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void ServerList::Sort()
|
|
|
|
|
{
|
|
|
|
|
std::sort(_serverEntries.begin(), _serverEntries.end(), [](const ServerListEntry& a, const ServerListEntry& b) {
|
|
|
|
|
return a.CompareTo(b) > 0;
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
ServerListEntry& ServerList::GetServer(size_t index)
|
|
|
|
|
{
|
|
|
|
|
return _serverEntries[index];
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
size_t ServerList::GetCount() const
|
|
|
|
|
{
|
|
|
|
|
return _serverEntries.size();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void ServerList::Add(const ServerListEntry& entry)
|
|
|
|
|
{
|
|
|
|
|
_serverEntries.push_back(entry);
|
|
|
|
|
Sort();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void ServerList::AddRange(const std::vector<ServerListEntry>& entries)
|
|
|
|
|
{
|
|
|
|
|
_serverEntries.insert(_serverEntries.end(), entries.begin(), entries.end());
|
|
|
|
|
Sort();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
std::vector<ServerListEntry> ServerList::ReadFavourites()
|
2017-02-11 12:40:19 +00:00
|
|
|
{
|
2018-02-01 18:49:14 +01:00
|
|
|
log_verbose("server_list_read(...)");
|
2019-05-05 13:15:46 +00:00
|
|
|
std::vector<ServerListEntry> entries;
|
2018-02-01 18:49:14 +01:00
|
|
|
try
|
2017-02-11 12:40:19 +00:00
|
|
|
{
|
2018-02-07 13:01:36 +00:00
|
|
|
auto env = GetContext()->GetPlatformEnvironment();
|
|
|
|
|
auto path = env->GetFilePath(PATHID::NETWORK_SERVERS);
|
2018-09-18 12:50:34 +01:00
|
|
|
if (platform_file_exists(path.c_str()))
|
2017-02-11 12:40:19 +00:00
|
|
|
{
|
2018-09-18 12:50:34 +01:00
|
|
|
auto fs = FileStream(path, FILE_MODE_OPEN);
|
|
|
|
|
auto numEntries = fs.ReadValue<uint32_t>();
|
|
|
|
|
for (size_t i = 0; i < numEntries; i++)
|
|
|
|
|
{
|
2019-05-05 13:15:46 +00:00
|
|
|
ServerListEntry serverInfo;
|
2018-10-04 01:42:22 -06:00
|
|
|
serverInfo.address = fs.ReadStdString();
|
|
|
|
|
serverInfo.name = fs.ReadStdString();
|
2018-09-18 12:50:34 +01:00
|
|
|
serverInfo.requiresPassword = false;
|
2018-10-04 01:42:22 -06:00
|
|
|
serverInfo.description = fs.ReadStdString();
|
|
|
|
|
serverInfo.version = "";
|
2018-09-18 12:50:34 +01:00
|
|
|
serverInfo.favourite = true;
|
|
|
|
|
serverInfo.players = 0;
|
|
|
|
|
serverInfo.maxplayers = 0;
|
|
|
|
|
entries.push_back(std::move(serverInfo));
|
|
|
|
|
}
|
2017-02-11 12:40:19 +00:00
|
|
|
}
|
2018-02-01 18:49:14 +01:00
|
|
|
}
|
2018-06-22 23:02:47 +02:00
|
|
|
catch (const std::exception& e)
|
2018-02-01 18:49:14 +01:00
|
|
|
{
|
2018-02-07 13:01:36 +00:00
|
|
|
log_error("Unable to read server list: %s", e.what());
|
2019-05-05 13:15:46 +00:00
|
|
|
entries = std::vector<ServerListEntry>();
|
2017-02-11 12:40:19 +00:00
|
|
|
}
|
2018-01-26 13:13:51 +00:00
|
|
|
return entries;
|
2018-02-01 18:49:14 +01:00
|
|
|
}
|
|
|
|
|
|
2019-05-05 13:15:46 +00:00
|
|
|
void ServerList::ReadAndAddFavourites()
|
|
|
|
|
{
|
|
|
|
|
_serverEntries.erase(
|
|
|
|
|
std::remove_if(
|
|
|
|
|
_serverEntries.begin(), _serverEntries.end(), [](const ServerListEntry& entry) { return entry.favourite; }),
|
|
|
|
|
_serverEntries.end());
|
|
|
|
|
auto entries = ReadFavourites();
|
|
|
|
|
AddRange(entries);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void ServerList::WriteFavourites()
|
|
|
|
|
{
|
|
|
|
|
// Save just favourite servers
|
|
|
|
|
std::vector<ServerListEntry> favouriteServers;
|
|
|
|
|
std::copy_if(
|
|
|
|
|
_serverEntries.begin(), _serverEntries.end(), std::back_inserter(favouriteServers),
|
|
|
|
|
[](const ServerListEntry& entry) { return entry.favourite; });
|
|
|
|
|
WriteFavourites(favouriteServers);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bool ServerList::WriteFavourites(const std::vector<ServerListEntry>& entries)
|
2018-02-01 18:49:14 +01:00
|
|
|
{
|
2018-01-26 13:13:51 +00:00
|
|
|
log_verbose("server_list_write(%d, 0x%p)", entries.size(), entries.data());
|
2018-02-01 18:49:14 +01:00
|
|
|
|
|
|
|
|
utf8 path[MAX_PATH];
|
|
|
|
|
platform_get_user_directory(path, nullptr, sizeof(path));
|
|
|
|
|
Path::Append(path, sizeof(path), "servers.cfg");
|
|
|
|
|
|
|
|
|
|
try
|
2017-02-11 12:40:19 +00:00
|
|
|
{
|
2018-02-01 18:49:14 +01:00
|
|
|
auto fs = FileStream(path, FILE_MODE_WRITE);
|
2018-06-20 17:28:51 +02:00
|
|
|
fs.WriteValue<uint32_t>((uint32_t)entries.size());
|
2018-06-22 23:02:47 +02:00
|
|
|
for (const auto& entry : entries)
|
2017-02-11 12:40:19 +00:00
|
|
|
{
|
2018-01-26 13:13:51 +00:00
|
|
|
fs.WriteString(entry.address);
|
|
|
|
|
fs.WriteString(entry.name);
|
|
|
|
|
fs.WriteString(entry.description);
|
2017-02-11 12:40:19 +00:00
|
|
|
}
|
2018-02-01 18:49:14 +01:00
|
|
|
return true;
|
|
|
|
|
}
|
2018-06-22 23:02:47 +02:00
|
|
|
catch (const std::exception& e)
|
2018-02-01 18:49:14 +01:00
|
|
|
{
|
2018-02-07 13:01:36 +00:00
|
|
|
log_error("Unable to write server list: %s", e.what());
|
2018-02-01 18:49:14 +01:00
|
|
|
return false;
|
2017-02-11 12:40:19 +00:00
|
|
|
}
|
|
|
|
|
}
|
2019-05-05 13:15:46 +00:00
|
|
|
|
|
|
|
|
std::future<std::vector<ServerListEntry>> ServerList::FetchLocalServerListAsync()
|
|
|
|
|
{
|
|
|
|
|
return std::async([] {
|
|
|
|
|
constexpr auto RECV_DELAY_MS = 10;
|
|
|
|
|
constexpr auto RECV_WAIT_MS = 2000;
|
|
|
|
|
|
|
|
|
|
std::vector<ServerListEntry> entries;
|
|
|
|
|
std::string msg = "Are you an OpenRCT2 server?";
|
|
|
|
|
auto udpSocket = CreateUdpSocket();
|
|
|
|
|
auto len = udpSocket->SendData("192.168.1.255", 11754, msg.data(), msg.size());
|
|
|
|
|
if (len != msg.size())
|
|
|
|
|
{
|
|
|
|
|
throw std::runtime_error("Unable to broadcast server query.");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
char buffer[1024]{};
|
|
|
|
|
size_t recievedLen{};
|
|
|
|
|
std::unique_ptr<INetworkEndpoint> endpoint;
|
|
|
|
|
for (int i = 0; i < (RECV_WAIT_MS / RECV_DELAY_MS); i++)
|
|
|
|
|
{
|
|
|
|
|
auto p = udpSocket->ReceiveData(buffer, sizeof(buffer) - 1, &recievedLen, &endpoint);
|
|
|
|
|
if (p == NETWORK_READPACKET_SUCCESS)
|
|
|
|
|
{
|
|
|
|
|
auto sender = endpoint->GetHostname();
|
|
|
|
|
std::printf(">> Recieved packet from %s\n", sender.c_str());
|
|
|
|
|
auto jinfo = Json::FromString(std::string_view(buffer));
|
|
|
|
|
|
|
|
|
|
auto ip4 = json_array();
|
|
|
|
|
json_array_append_new(ip4, json_string(sender.c_str()));
|
|
|
|
|
auto ip = json_object();
|
|
|
|
|
json_object_set_new(ip, "v4", ip4);
|
|
|
|
|
json_object_set_new(jinfo, "ip", ip);
|
|
|
|
|
|
|
|
|
|
auto entry = ServerListEntry::FromJson(jinfo);
|
|
|
|
|
if (entry.has_value())
|
|
|
|
|
{
|
|
|
|
|
entries.push_back(entry.value());
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
json_decref(jinfo);
|
|
|
|
|
}
|
|
|
|
|
platform_sleep(RECV_DELAY_MS);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return entries;
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
std::future<std::vector<ServerListEntry>> ServerList::FetchOnlineServerListAsync()
|
|
|
|
|
{
|
|
|
|
|
#ifdef DISABLE_HTTP
|
|
|
|
|
return std::async(std::launch::deferred, [] { return std::vector<ServerListEntry>(); });
|
|
|
|
|
#else
|
|
|
|
|
auto p = std::make_shared<std::promise<std::vector<ServerListEntry>>>();
|
|
|
|
|
auto f = p->get_future();
|
|
|
|
|
|
|
|
|
|
std::string masterServerUrl = OPENRCT2_MASTER_SERVER_URL;
|
|
|
|
|
if (!gConfigNetwork.master_server_url.empty())
|
|
|
|
|
{
|
|
|
|
|
masterServerUrl = gConfigNetwork.master_server_url;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Http::Request request;
|
|
|
|
|
request.url = masterServerUrl;
|
|
|
|
|
request.method = Http::Method::GET;
|
|
|
|
|
request.header["Accept"] = "application/json";
|
|
|
|
|
Http::DoAsync(request, [p](Http::Response& response) -> void {
|
|
|
|
|
json_t* root{};
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
if (response.status != Http::Status::OK)
|
|
|
|
|
{
|
|
|
|
|
throw MasterServerException(STR_SERVER_LIST_NO_CONNECTION);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
root = Json::FromString(response.body);
|
|
|
|
|
auto jsonStatus = json_object_get(root, "status");
|
|
|
|
|
if (!json_is_number(jsonStatus))
|
|
|
|
|
{
|
|
|
|
|
throw MasterServerException(STR_SERVER_LIST_INVALID_RESPONSE_JSON_NUMBER);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
auto status = (int32_t)json_integer_value(jsonStatus);
|
|
|
|
|
if (status != 200)
|
|
|
|
|
{
|
|
|
|
|
throw MasterServerException(STR_SERVER_LIST_MASTER_SERVER_FAILED);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
auto jServers = json_object_get(root, "servers");
|
|
|
|
|
if (!json_is_array(jServers))
|
|
|
|
|
{
|
|
|
|
|
throw MasterServerException(STR_SERVER_LIST_INVALID_RESPONSE_JSON_ARRAY);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
std::vector<ServerListEntry> entries;
|
|
|
|
|
auto count = json_array_size(jServers);
|
|
|
|
|
for (size_t i = 0; i < count; i++)
|
|
|
|
|
{
|
|
|
|
|
auto jServer = json_array_get(jServers, i);
|
|
|
|
|
if (json_is_object(jServer))
|
|
|
|
|
{
|
|
|
|
|
auto entry = ServerListEntry::FromJson(jServer);
|
|
|
|
|
if (entry.has_value())
|
|
|
|
|
{
|
|
|
|
|
entries.push_back(entry.value());
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
p->set_value(entries);
|
|
|
|
|
}
|
|
|
|
|
catch (...)
|
|
|
|
|
{
|
|
|
|
|
p->set_exception(std::current_exception());
|
|
|
|
|
}
|
|
|
|
|
json_decref(root);
|
|
|
|
|
});
|
|
|
|
|
return f;
|
|
|
|
|
#endif
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
uint32_t ServerList::GetTotalPlayerCount() const
|
|
|
|
|
{
|
|
|
|
|
return std::accumulate(_serverEntries.begin(), _serverEntries.end(), 0, [](uint32_t acc, const ServerListEntry& entry) {
|
|
|
|
|
return acc + entry.players;
|
|
|
|
|
});
|
|
|
|
|
}
|