2015-02-12 03:01:02 +01:00
|
|
|
/*****************************************************************************
|
2020-07-21 15:04:34 +02:00
|
|
|
* Copyright (c) 2014-2020 OpenRCT2 developers
|
2015-02-12 03:01:02 +01: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
|
2015-02-12 03:01:02 +01:00
|
|
|
*
|
2018-06-15 14:07:34 +02:00
|
|
|
* OpenRCT2 is licensed under the GNU General Public License version 3.
|
2015-02-12 03:01:02 +01:00
|
|
|
*****************************************************************************/
|
|
|
|
|
2018-06-22 23:02:47 +02:00
|
|
|
#include "network.h"
|
|
|
|
|
2017-06-11 19:19:15 +02:00
|
|
|
#include "../Context.h"
|
2018-11-19 23:10:54 +01:00
|
|
|
#include "../Game.h"
|
2019-05-11 21:31:34 +02:00
|
|
|
#include "../GameStateSnapshots.h"
|
2017-02-08 13:53:00 +01:00
|
|
|
#include "../OpenRCT2.h"
|
2017-06-06 18:55:13 +02:00
|
|
|
#include "../PlatformEnvironment.h"
|
2019-02-21 10:34:30 +01:00
|
|
|
#include "../actions/LoadOrQuitAction.hpp"
|
2019-05-12 19:54:41 +02:00
|
|
|
#include "../actions/NetworkModifyGroupAction.hpp"
|
2019-06-09 17:54:12 +02:00
|
|
|
#include "../actions/PeepPickupAction.hpp"
|
2018-06-22 23:02:47 +02:00
|
|
|
#include "../core/Guard.hpp"
|
2020-07-02 17:03:31 +02:00
|
|
|
#include "../platform/Platform2.h"
|
2019-07-25 23:59:26 +02:00
|
|
|
#include "../scripting/ScriptEngine.h"
|
2017-06-11 19:19:15 +02:00
|
|
|
#include "../ui/UiContext.h"
|
|
|
|
#include "../ui/WindowManager.h"
|
2017-12-13 13:02:24 +01:00
|
|
|
#include "../util/SawyerCoding.h"
|
2018-03-19 23:34:37 +01:00
|
|
|
#include "../world/Location.hpp"
|
2015-08-29 03:04:54 +02:00
|
|
|
|
2018-08-12 13:50:40 +02:00
|
|
|
#include <algorithm>
|
2018-11-21 23:16:04 +01:00
|
|
|
#include <iterator>
|
2018-06-22 23:02:47 +02:00
|
|
|
#include <stdexcept>
|
2015-08-18 09:30:55 +02:00
|
|
|
|
2018-02-20 21:06:35 +01:00
|
|
|
// This string specifies which version of network stream current build uses.
|
|
|
|
// It is used for making sure only compatible builds get connected, even within
|
|
|
|
// single OpenRCT2 version.
|
2020-07-24 22:50:40 +02:00
|
|
|
#define NETWORK_STREAM_VERSION "23"
|
2018-02-20 21:06:35 +01:00
|
|
|
#define NETWORK_STREAM_ID OPENRCT2_VERSION "-" NETWORK_STREAM_VERSION
|
|
|
|
|
2019-02-28 20:28:58 +01:00
|
|
|
static Peep* _pickup_peep = nullptr;
|
2018-06-20 17:28:51 +02:00
|
|
|
static int32_t _pickup_peep_old_x = LOCATION_NULL;
|
2016-10-17 22:26:19 +02:00
|
|
|
|
2019-05-11 21:31:34 +02:00
|
|
|
// General chunk size is 63 KiB, this can not be any larger because the packet size is encoded
|
|
|
|
// with uint16_t and needs some spare room for other data in the packet.
|
|
|
|
static constexpr uint32_t CHUNK_SIZE = 1024 * 63;
|
|
|
|
|
2015-02-12 03:01:02 +01:00
|
|
|
#ifndef DISABLE_NETWORK
|
|
|
|
|
2018-07-21 16:17:06 +02:00
|
|
|
# include "../Cheats.h"
|
|
|
|
# include "../ParkImporter.h"
|
2018-11-19 23:10:54 +01:00
|
|
|
# include "../Version.h"
|
2018-07-21 16:17:06 +02:00
|
|
|
# include "../actions/GameAction.h"
|
|
|
|
# include "../config/Config.h"
|
|
|
|
# include "../core/Console.hpp"
|
|
|
|
# include "../core/FileStream.hpp"
|
|
|
|
# include "../core/Json.hpp"
|
|
|
|
# include "../core/MemoryStream.h"
|
2018-11-19 23:10:54 +01:00
|
|
|
# include "../core/Nullable.hpp"
|
2018-07-21 16:17:06 +02:00
|
|
|
# include "../core/Path.hpp"
|
|
|
|
# include "../core/String.hpp"
|
|
|
|
# include "../interface/Chat.h"
|
|
|
|
# include "../interface/Window.h"
|
|
|
|
# include "../localisation/Date.h"
|
|
|
|
# include "../localisation/Localisation.h"
|
|
|
|
# include "../object/ObjectManager.h"
|
|
|
|
# include "../object/ObjectRepository.h"
|
|
|
|
# include "../rct2/S6Exporter.h"
|
|
|
|
# include "../scenario/Scenario.h"
|
|
|
|
# include "../util/Util.h"
|
|
|
|
# include "../world/Park.h"
|
|
|
|
# include "NetworkAction.h"
|
2018-11-19 23:10:54 +01:00
|
|
|
# include "NetworkConnection.h"
|
|
|
|
# include "NetworkGroup.h"
|
|
|
|
# include "NetworkKey.h"
|
|
|
|
# include "NetworkPacket.h"
|
|
|
|
# include "NetworkPlayer.h"
|
|
|
|
# include "NetworkServerAdvertiser.h"
|
|
|
|
# include "NetworkUser.h"
|
2019-05-05 18:54:16 +02:00
|
|
|
# include "Socket.h"
|
2018-07-21 16:17:06 +02:00
|
|
|
|
|
|
|
# include <algorithm>
|
2018-11-19 23:10:54 +01:00
|
|
|
# include <array>
|
2018-07-21 16:17:06 +02:00
|
|
|
# include <cerrno>
|
|
|
|
# include <cmath>
|
2018-11-19 23:10:54 +01:00
|
|
|
# include <fstream>
|
|
|
|
# include <functional>
|
|
|
|
# include <list>
|
|
|
|
# include <map>
|
|
|
|
# include <memory>
|
2018-07-21 16:17:06 +02:00
|
|
|
# include <set>
|
|
|
|
# include <string>
|
2018-11-19 23:10:54 +01:00
|
|
|
# include <vector>
|
2018-07-21 16:17:06 +02:00
|
|
|
|
2019-09-23 17:14:57 +02:00
|
|
|
# if defined(_WIN32)
|
|
|
|
# pragma comment(lib, "Ws2_32.lib")
|
|
|
|
# endif
|
2015-02-12 03:01:02 +01:00
|
|
|
|
2017-06-11 13:53:37 +02:00
|
|
|
using namespace OpenRCT2;
|
|
|
|
|
2018-06-22 23:02:47 +02:00
|
|
|
enum
|
|
|
|
{
|
2017-06-06 23:24:18 +02:00
|
|
|
SERVER_EVENT_PLAYER_JOINED,
|
|
|
|
SERVER_EVENT_PLAYER_DISCONNECTED,
|
2016-02-27 14:12:04 +01:00
|
|
|
};
|
|
|
|
|
2018-11-19 23:10:54 +01:00
|
|
|
enum
|
|
|
|
{
|
|
|
|
NETWORK_TICK_FLAG_CHECKSUMS = 1 << 0,
|
|
|
|
};
|
|
|
|
|
|
|
|
static void network_chat_show_connected_message();
|
|
|
|
static void network_chat_show_server_greeting();
|
2018-06-22 23:02:47 +02:00
|
|
|
static void network_get_keys_directory(utf8* buffer, size_t bufferSize);
|
2019-02-23 15:54:17 +01:00
|
|
|
static void network_get_private_key_path(utf8* buffer, size_t bufferSize, const std::string& playerName);
|
|
|
|
static void network_get_public_key_path(utf8* buffer, size_t bufferSize, const std::string& playerName, const utf8* hash);
|
2016-02-26 10:22:14 +01:00
|
|
|
|
2018-11-19 23:10:54 +01:00
|
|
|
class Network
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
Network();
|
2018-12-17 12:58:12 +01:00
|
|
|
|
2018-11-19 23:10:54 +01:00
|
|
|
void SetEnvironment(const std::shared_ptr<OpenRCT2::IPlatformEnvironment>& env);
|
|
|
|
bool Init();
|
2019-02-05 06:57:09 +01:00
|
|
|
void Reconnect();
|
2018-11-19 23:10:54 +01:00
|
|
|
void Close();
|
2019-02-23 15:54:17 +01:00
|
|
|
bool BeginClient(const std::string& host, uint16_t port);
|
|
|
|
bool BeginServer(uint16_t port, const std::string& address);
|
2018-11-19 23:10:54 +01:00
|
|
|
int32_t GetMode();
|
|
|
|
int32_t GetStatus();
|
|
|
|
int32_t GetAuthStatus();
|
|
|
|
uint32_t GetServerTick();
|
|
|
|
uint8_t GetPlayerID();
|
|
|
|
void Update();
|
|
|
|
void Flush();
|
2018-12-09 20:39:44 +01:00
|
|
|
void ProcessPending();
|
|
|
|
void ProcessPlayerList();
|
2019-03-24 23:16:57 +01:00
|
|
|
void ProcessPlayerInfo();
|
2019-05-15 19:55:16 +02:00
|
|
|
void ProcessDisconnectedClients();
|
2018-11-19 23:10:54 +01:00
|
|
|
std::vector<std::unique_ptr<NetworkPlayer>>::iterator GetPlayerIteratorByID(uint8_t id);
|
|
|
|
NetworkPlayer* GetPlayerByID(uint8_t id);
|
2020-04-27 20:54:29 +02:00
|
|
|
NetworkConnection* GetPlayerConnection(uint8_t id);
|
2018-11-19 23:10:54 +01:00
|
|
|
std::vector<std::unique_ptr<NetworkGroup>>::iterator GetGroupIteratorByID(uint8_t id);
|
|
|
|
NetworkGroup* GetGroupByID(uint8_t id);
|
|
|
|
static const char* FormatChat(NetworkPlayer* fromplayer, const char* text);
|
|
|
|
void SendPacketToClients(NetworkPacket& packet, bool front = false, bool gameCmd = false);
|
|
|
|
bool CheckSRAND(uint32_t tick, uint32_t srand0);
|
2019-02-08 01:32:05 +01:00
|
|
|
bool IsDesynchronised();
|
2019-05-11 21:31:34 +02:00
|
|
|
bool CheckDesynchronizaton();
|
|
|
|
void RequestStateSnapshot();
|
|
|
|
NetworkServerState_t GetServerState() const;
|
2018-11-19 23:10:54 +01:00
|
|
|
void KickPlayer(int32_t playerId);
|
|
|
|
void SetPassword(const char* password);
|
2019-05-15 19:55:16 +02:00
|
|
|
void ServerClientDisconnected();
|
2018-11-19 23:10:54 +01:00
|
|
|
NetworkGroup* AddGroup();
|
|
|
|
void RemoveGroup(uint8_t id);
|
|
|
|
uint8_t GetDefaultGroup();
|
|
|
|
uint8_t GetGroupIDByHash(const std::string& keyhash);
|
|
|
|
void SetDefaultGroup(uint8_t id);
|
|
|
|
void SaveGroups();
|
|
|
|
void LoadGroups();
|
|
|
|
|
|
|
|
std::string BeginLog(const std::string& directory, const std::string& midName, const std::string& filenameFormat);
|
|
|
|
void AppendLog(std::ostream& fs, const std::string& s);
|
|
|
|
|
|
|
|
void BeginChatLog();
|
|
|
|
void AppendChatLog(const std::string& s);
|
|
|
|
void CloseChatLog();
|
|
|
|
|
|
|
|
void BeginServerLog();
|
|
|
|
void AppendServerLog(const std::string& s);
|
|
|
|
void CloseServerLog();
|
|
|
|
|
2019-05-11 21:31:34 +02:00
|
|
|
void Client_Send_RequestGameState(uint32_t tick);
|
|
|
|
|
2018-11-19 23:10:54 +01:00
|
|
|
void Client_Send_TOKEN();
|
2019-02-23 15:54:17 +01:00
|
|
|
void Client_Send_AUTH(
|
2019-03-09 13:58:23 +01:00
|
|
|
const std::string& name, const std::string& password, const std::string& pubkey, const std::vector<uint8_t>& signature);
|
2018-11-19 23:10:54 +01:00
|
|
|
void Server_Send_AUTH(NetworkConnection& connection);
|
|
|
|
void Server_Send_TOKEN(NetworkConnection& connection);
|
|
|
|
void Server_Send_MAP(NetworkConnection* connection = nullptr);
|
|
|
|
void Client_Send_CHAT(const char* text);
|
2020-04-28 23:02:14 +02:00
|
|
|
void Server_Send_CHAT(const char* text, const std::vector<uint8_t>& playerIds = {});
|
2018-11-19 23:10:54 +01:00
|
|
|
void Client_Send_GAME_ACTION(const GameAction* action);
|
|
|
|
void Server_Send_GAME_ACTION(const GameAction* action);
|
|
|
|
void Server_Send_TICK();
|
2019-03-24 23:16:57 +01:00
|
|
|
void Server_Send_PLAYERINFO(int32_t playerId);
|
2018-11-19 23:10:54 +01:00
|
|
|
void Server_Send_PLAYERLIST();
|
|
|
|
void Client_Send_PING();
|
|
|
|
void Server_Send_PING();
|
|
|
|
void Server_Send_PINGLIST();
|
|
|
|
void Server_Send_SETDISCONNECTMSG(NetworkConnection& connection, const char* msg);
|
|
|
|
void Server_Send_GAMEINFO(NetworkConnection& connection);
|
|
|
|
void Server_Send_SHOWERROR(NetworkConnection& connection, rct_string_id title, rct_string_id message);
|
|
|
|
void Server_Send_GROUPLIST(NetworkConnection& connection);
|
|
|
|
void Server_Send_EVENT_PLAYER_JOINED(const char* playerName);
|
|
|
|
void Server_Send_EVENT_PLAYER_DISCONNECTED(const char* playerName, const char* reason);
|
|
|
|
void Client_Send_GAMEINFO();
|
2020-07-24 23:00:07 +02:00
|
|
|
void Client_Send_REQUESTMAP(const std::vector<std::string>& objects);
|
2020-07-24 22:36:45 +02:00
|
|
|
void Server_Send_OBJECTS_LIST(NetworkConnection& connection, const std::vector<const ObjectRepositoryItem*>& objects) const;
|
2020-02-22 23:18:22 +01:00
|
|
|
void Server_Send_SCRIPTS(NetworkConnection& connection) const;
|
2020-07-24 22:49:05 +02:00
|
|
|
void Client_Send_HEARTBEAT(NetworkConnection& connection) const;
|
2018-11-19 23:10:54 +01:00
|
|
|
|
2019-01-29 16:35:19 +01:00
|
|
|
NetworkStats_t GetStats() const;
|
2019-05-05 04:18:50 +02:00
|
|
|
json_t* GetServerInfoAsJson() const;
|
2019-01-29 16:35:19 +01:00
|
|
|
|
2018-11-19 23:10:54 +01:00
|
|
|
std::vector<std::unique_ptr<NetworkPlayer>> player_list;
|
|
|
|
std::vector<std::unique_ptr<NetworkGroup>> group_list;
|
|
|
|
NetworkKey _key;
|
|
|
|
std::vector<uint8_t> _challenge;
|
|
|
|
std::map<uint32_t, GameAction::Callback_t> _gameActionCallbacks;
|
|
|
|
NetworkUserManager _userManager;
|
|
|
|
std::string ServerName;
|
|
|
|
std::string ServerDescription;
|
|
|
|
std::string ServerGreeting;
|
|
|
|
std::string ServerProviderName;
|
|
|
|
std::string ServerProviderEmail;
|
|
|
|
std::string ServerProviderWebsite;
|
|
|
|
|
|
|
|
private:
|
2019-03-01 09:25:16 +01:00
|
|
|
void DecayCooldown(NetworkPlayer* player);
|
2018-11-19 23:10:54 +01:00
|
|
|
void CloseConnection();
|
|
|
|
|
|
|
|
bool ProcessConnection(NetworkConnection& connection);
|
|
|
|
void ProcessPacket(NetworkConnection& connection, NetworkPacket& packet);
|
2018-12-17 12:58:12 +01:00
|
|
|
void AddClient(std::unique_ptr<ITcpSocket>&& socket);
|
2019-05-15 19:55:16 +02:00
|
|
|
void ServerClientDisconnected(std::unique_ptr<NetworkConnection>& connection);
|
2019-01-29 16:35:19 +01:00
|
|
|
|
2019-05-15 19:55:16 +02:00
|
|
|
void RemovePlayer(std::unique_ptr<NetworkConnection>& connection);
|
2019-02-23 15:54:17 +01:00
|
|
|
NetworkPlayer* AddPlayer(const std::string& name, const std::string& keyhash);
|
2018-11-19 23:10:54 +01:00
|
|
|
std::string MakePlayerNameUnique(const std::string& name);
|
|
|
|
|
2019-02-23 15:54:17 +01:00
|
|
|
std::string GetMasterServerUrl();
|
2018-11-19 23:10:54 +01:00
|
|
|
std::string GenerateAdvertiseKey();
|
|
|
|
void SetupDefaultGroups();
|
|
|
|
|
|
|
|
bool LoadMap(IStream* stream);
|
|
|
|
bool SaveMap(IStream* stream, const std::vector<const ObjectRepositoryItem*>& objects) const;
|
|
|
|
|
2018-12-09 20:39:44 +01:00
|
|
|
struct PlayerListUpdate
|
|
|
|
{
|
|
|
|
std::vector<NetworkPlayer> players;
|
|
|
|
};
|
|
|
|
|
2019-03-12 21:07:48 +01:00
|
|
|
struct ServerTickData_t
|
|
|
|
{
|
|
|
|
uint32_t srand0;
|
|
|
|
uint32_t tick;
|
|
|
|
std::string spriteHash;
|
|
|
|
};
|
|
|
|
|
|
|
|
std::map<uint32_t, ServerTickData_t> _serverTickData;
|
2019-05-15 19:55:16 +02:00
|
|
|
std::map<uint32_t, PlayerListUpdate> _pendingPlayerLists;
|
2019-03-24 23:16:57 +01:00
|
|
|
std::multimap<uint32_t, NetworkPlayer> _pendingPlayerInfo;
|
2019-05-15 19:55:16 +02:00
|
|
|
bool _playerListInvalidated = false;
|
2018-11-19 23:10:54 +01:00
|
|
|
int32_t mode = NETWORK_MODE_NONE;
|
|
|
|
int32_t status = NETWORK_STATUS_NONE;
|
|
|
|
bool _closeLock = false;
|
|
|
|
bool _requireClose = false;
|
2019-02-05 06:57:09 +01:00
|
|
|
bool _requireReconnect = false;
|
2018-11-19 23:10:54 +01:00
|
|
|
bool wsa_initialized = false;
|
2019-03-12 14:24:48 +01:00
|
|
|
bool _clientMapLoaded = false;
|
2018-12-17 12:58:12 +01:00
|
|
|
std::unique_ptr<ITcpSocket> _listenSocket;
|
|
|
|
std::unique_ptr<NetworkConnection> _serverConnection;
|
|
|
|
std::unique_ptr<INetworkServerAdvertiser> _advertiser;
|
2018-11-19 23:10:54 +01:00
|
|
|
uint16_t listening_port = 0;
|
|
|
|
SOCKET_STATUS _lastConnectStatus = SOCKET_STATUS_CLOSED;
|
|
|
|
uint32_t last_ping_sent_time = 0;
|
|
|
|
uint8_t player_id = 0;
|
|
|
|
std::list<std::unique_ptr<NetworkConnection>> client_connection_list;
|
|
|
|
std::vector<uint8_t> chunk_buffer;
|
2019-02-05 06:57:09 +01:00
|
|
|
std::string _host;
|
|
|
|
uint16_t _port = 0;
|
2018-11-19 23:10:54 +01:00
|
|
|
std::string _password;
|
2019-05-11 21:31:34 +02:00
|
|
|
NetworkServerState_t _serverState;
|
|
|
|
MemoryStream _serverGameState;
|
2018-11-19 23:10:54 +01:00
|
|
|
uint32_t server_connect_time = 0;
|
|
|
|
uint8_t default_group = 0;
|
|
|
|
uint32_t _actionId;
|
2019-03-01 09:25:16 +01:00
|
|
|
uint32_t _lastUpdateTime = 0;
|
|
|
|
uint32_t _currentDeltaTime = 0;
|
2020-07-24 22:49:05 +02:00
|
|
|
uint32_t _lastSentHeartbeat = 0;
|
2018-11-19 23:10:54 +01:00
|
|
|
std::string _chatLogPath;
|
|
|
|
std::string _chatLogFilenameFormat = "%Y%m%d-%H%M%S.txt";
|
|
|
|
std::string _serverLogPath;
|
|
|
|
std::string _serverLogFilenameFormat = "%Y%m%d-%H%M%S.txt";
|
|
|
|
std::shared_ptr<OpenRCT2::IPlatformEnvironment> _env;
|
2020-07-24 22:36:45 +02:00
|
|
|
std::vector<std::string> _missingObjects;
|
2018-11-19 23:10:54 +01:00
|
|
|
|
|
|
|
void UpdateServer();
|
|
|
|
void UpdateClient();
|
|
|
|
|
|
|
|
private:
|
|
|
|
std::vector<void (Network::*)(NetworkConnection& connection, NetworkPacket& packet)> client_command_handlers;
|
|
|
|
std::vector<void (Network::*)(NetworkConnection& connection, NetworkPacket& packet)> server_command_handlers;
|
2019-05-11 21:31:34 +02:00
|
|
|
void Server_Handle_REQUEST_GAMESTATE(NetworkConnection& connection, NetworkPacket& packet);
|
2020-07-24 22:49:05 +02:00
|
|
|
void Server_Handle_HEARTBEAT(NetworkConnection& connection, NetworkPacket& packet);
|
2018-11-19 23:10:54 +01:00
|
|
|
void Client_Handle_AUTH(NetworkConnection& connection, NetworkPacket& packet);
|
|
|
|
void Server_Handle_AUTH(NetworkConnection& connection, NetworkPacket& packet);
|
|
|
|
void Server_Client_Joined(const char* name, const std::string& keyhash, NetworkConnection& connection);
|
|
|
|
void Client_Handle_MAP(NetworkConnection& connection, NetworkPacket& packet);
|
|
|
|
void Client_Handle_CHAT(NetworkConnection& connection, NetworkPacket& packet);
|
|
|
|
void Server_Handle_CHAT(NetworkConnection& connection, NetworkPacket& packet);
|
|
|
|
void Client_Handle_GAME_ACTION(NetworkConnection& connection, NetworkPacket& packet);
|
|
|
|
void Server_Handle_GAME_ACTION(NetworkConnection& connection, NetworkPacket& packet);
|
|
|
|
void Client_Handle_TICK(NetworkConnection& connection, NetworkPacket& packet);
|
2019-03-24 23:16:57 +01:00
|
|
|
void Client_Handle_PLAYERINFO(NetworkConnection& connection, NetworkPacket& packet);
|
2018-11-19 23:10:54 +01:00
|
|
|
void Client_Handle_PLAYERLIST(NetworkConnection& connection, NetworkPacket& packet);
|
|
|
|
void Client_Handle_PING(NetworkConnection& connection, NetworkPacket& packet);
|
|
|
|
void Server_Handle_PING(NetworkConnection& connection, NetworkPacket& packet);
|
|
|
|
void Client_Handle_PINGLIST(NetworkConnection& connection, NetworkPacket& packet);
|
|
|
|
void Client_Handle_SETDISCONNECTMSG(NetworkConnection& connection, NetworkPacket& packet);
|
|
|
|
void Client_Handle_GAMEINFO(NetworkConnection& connection, NetworkPacket& packet);
|
|
|
|
void Server_Handle_GAMEINFO(NetworkConnection& connection, NetworkPacket& packet);
|
|
|
|
void Client_Handle_SHOWERROR(NetworkConnection& connection, NetworkPacket& packet);
|
|
|
|
void Client_Handle_GROUPLIST(NetworkConnection& connection, NetworkPacket& packet);
|
|
|
|
void Client_Handle_EVENT(NetworkConnection& connection, NetworkPacket& packet);
|
|
|
|
void Client_Handle_TOKEN(NetworkConnection& connection, NetworkPacket& packet);
|
|
|
|
void Server_Handle_TOKEN(NetworkConnection& connection, NetworkPacket& packet);
|
2020-07-24 20:59:22 +02:00
|
|
|
void Client_Handle_OBJECTS_LIST(NetworkConnection& connection, NetworkPacket& packet);
|
2020-02-22 23:18:22 +01:00
|
|
|
void Client_Handle_SCRIPTS(NetworkConnection& connection, NetworkPacket& packet);
|
2019-05-11 21:31:34 +02:00
|
|
|
void Client_Handle_GAMESTATE(NetworkConnection& connection, NetworkPacket& packet);
|
2020-07-24 20:59:22 +02:00
|
|
|
void Server_Handle_MAPREQUEST(NetworkConnection& connection, NetworkPacket& packet);
|
2018-11-19 23:10:54 +01:00
|
|
|
|
|
|
|
uint8_t* save_for_network(size_t& out_size, const std::vector<const ObjectRepositoryItem*>& objects) const;
|
|
|
|
|
|
|
|
std::ofstream _chat_log_fs;
|
|
|
|
std::ofstream _server_log_fs;
|
|
|
|
};
|
|
|
|
|
|
|
|
static Network gNetwork;
|
|
|
|
|
2015-07-10 21:53:41 +02:00
|
|
|
Network::Network()
|
2015-02-12 03:01:02 +01:00
|
|
|
{
|
2017-06-06 23:24:18 +02:00
|
|
|
wsa_initialized = false;
|
|
|
|
mode = NETWORK_MODE_NONE;
|
|
|
|
status = NETWORK_STATUS_NONE;
|
|
|
|
last_ping_sent_time = 0;
|
2017-07-17 21:27:21 +02:00
|
|
|
_actionId = 0;
|
2017-10-03 00:00:32 +02:00
|
|
|
client_command_handlers.resize(NETWORK_COMMAND_MAX, nullptr);
|
2017-06-06 23:24:18 +02:00
|
|
|
client_command_handlers[NETWORK_COMMAND_AUTH] = &Network::Client_Handle_AUTH;
|
|
|
|
client_command_handlers[NETWORK_COMMAND_MAP] = &Network::Client_Handle_MAP;
|
|
|
|
client_command_handlers[NETWORK_COMMAND_CHAT] = &Network::Client_Handle_CHAT;
|
2017-03-23 20:49:19 +01:00
|
|
|
client_command_handlers[NETWORK_COMMAND_GAME_ACTION] = &Network::Client_Handle_GAME_ACTION;
|
2017-06-06 23:24:18 +02:00
|
|
|
client_command_handlers[NETWORK_COMMAND_TICK] = &Network::Client_Handle_TICK;
|
|
|
|
client_command_handlers[NETWORK_COMMAND_PLAYERLIST] = &Network::Client_Handle_PLAYERLIST;
|
2019-03-24 23:16:57 +01:00
|
|
|
client_command_handlers[NETWORK_COMMAND_PLAYERINFO] = &Network::Client_Handle_PLAYERINFO;
|
2017-06-06 23:24:18 +02:00
|
|
|
client_command_handlers[NETWORK_COMMAND_PING] = &Network::Client_Handle_PING;
|
|
|
|
client_command_handlers[NETWORK_COMMAND_PINGLIST] = &Network::Client_Handle_PINGLIST;
|
|
|
|
client_command_handlers[NETWORK_COMMAND_SETDISCONNECTMSG] = &Network::Client_Handle_SETDISCONNECTMSG;
|
|
|
|
client_command_handlers[NETWORK_COMMAND_SHOWERROR] = &Network::Client_Handle_SHOWERROR;
|
|
|
|
client_command_handlers[NETWORK_COMMAND_GROUPLIST] = &Network::Client_Handle_GROUPLIST;
|
|
|
|
client_command_handlers[NETWORK_COMMAND_EVENT] = &Network::Client_Handle_EVENT;
|
|
|
|
client_command_handlers[NETWORK_COMMAND_GAMEINFO] = &Network::Client_Handle_GAMEINFO;
|
|
|
|
client_command_handlers[NETWORK_COMMAND_TOKEN] = &Network::Client_Handle_TOKEN;
|
2020-07-24 20:59:22 +02:00
|
|
|
client_command_handlers[NETWORK_COMMAND_OBJECTS_LIST] = &Network::Client_Handle_OBJECTS_LIST;
|
2020-02-22 23:18:22 +01:00
|
|
|
client_command_handlers[NETWORK_COMMAND_SCRIPTS] = &Network::Client_Handle_SCRIPTS;
|
2019-05-11 21:31:34 +02:00
|
|
|
client_command_handlers[NETWORK_COMMAND_GAMESTATE] = &Network::Client_Handle_GAMESTATE;
|
2017-10-03 00:00:32 +02:00
|
|
|
server_command_handlers.resize(NETWORK_COMMAND_MAX, nullptr);
|
2017-06-06 23:24:18 +02:00
|
|
|
server_command_handlers[NETWORK_COMMAND_AUTH] = &Network::Server_Handle_AUTH;
|
|
|
|
server_command_handlers[NETWORK_COMMAND_CHAT] = &Network::Server_Handle_CHAT;
|
2017-03-23 20:49:19 +01:00
|
|
|
server_command_handlers[NETWORK_COMMAND_GAME_ACTION] = &Network::Server_Handle_GAME_ACTION;
|
2017-06-06 23:24:18 +02:00
|
|
|
server_command_handlers[NETWORK_COMMAND_PING] = &Network::Server_Handle_PING;
|
|
|
|
server_command_handlers[NETWORK_COMMAND_GAMEINFO] = &Network::Server_Handle_GAMEINFO;
|
|
|
|
server_command_handlers[NETWORK_COMMAND_TOKEN] = &Network::Server_Handle_TOKEN;
|
2020-07-24 20:59:22 +02:00
|
|
|
server_command_handlers[NETWORK_COMMAND_MAP_REQUEST] = &Network::Server_Handle_MAPREQUEST;
|
2019-05-11 21:31:34 +02:00
|
|
|
server_command_handlers[NETWORK_COMMAND_REQUEST_GAMESTATE] = &Network::Server_Handle_REQUEST_GAMESTATE;
|
2020-07-24 22:49:05 +02:00
|
|
|
server_command_handlers[NETWORK_COMMAND_HEARTBEAT] = &Network::Server_Handle_HEARTBEAT;
|
2018-01-04 00:01:43 +01:00
|
|
|
|
|
|
|
_chat_log_fs << std::unitbuf;
|
|
|
|
_server_log_fs << std::unitbuf;
|
2015-07-10 21:53:41 +02:00
|
|
|
}
|
|
|
|
|
2018-04-27 19:47:57 +02:00
|
|
|
void Network::SetEnvironment(const std::shared_ptr<IPlatformEnvironment>& env)
|
2017-06-06 18:55:13 +02:00
|
|
|
{
|
2017-06-06 23:24:18 +02:00
|
|
|
_env = env;
|
2017-06-06 18:55:13 +02:00
|
|
|
}
|
|
|
|
|
2015-07-10 21:53:41 +02:00
|
|
|
bool Network::Init()
|
|
|
|
{
|
2018-06-22 23:02:47 +02:00
|
|
|
if (!InitialiseWSA())
|
|
|
|
{
|
2017-06-06 23:24:18 +02:00
|
|
|
return false;
|
|
|
|
}
|
2016-01-20 23:45:09 +01:00
|
|
|
|
2017-06-06 23:24:18 +02:00
|
|
|
status = NETWORK_STATUS_READY;
|
2016-05-30 23:25:43 +02:00
|
|
|
|
2017-06-06 23:24:18 +02:00
|
|
|
ServerName = std::string();
|
|
|
|
ServerDescription = std::string();
|
|
|
|
ServerGreeting = std::string();
|
|
|
|
ServerProviderName = std::string();
|
|
|
|
ServerProviderEmail = std::string();
|
|
|
|
ServerProviderWebsite = std::string();
|
|
|
|
return true;
|
2015-02-12 03:01:02 +01:00
|
|
|
}
|
|
|
|
|
2019-02-05 06:57:09 +01:00
|
|
|
void Network::Reconnect()
|
|
|
|
{
|
|
|
|
if (status != NETWORK_STATUS_NONE)
|
|
|
|
{
|
|
|
|
Close();
|
|
|
|
}
|
|
|
|
if (_requireClose)
|
|
|
|
{
|
|
|
|
_requireReconnect = true;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
BeginClient(_host, _port);
|
|
|
|
}
|
|
|
|
|
2015-07-10 21:53:41 +02:00
|
|
|
void Network::Close()
|
2015-02-12 03:01:02 +01:00
|
|
|
{
|
2018-03-23 19:29:45 +01:00
|
|
|
if (status != NETWORK_STATUS_NONE)
|
2017-06-06 23:24:18 +02:00
|
|
|
{
|
2018-03-23 19:29:45 +01:00
|
|
|
// HACK Because Close() is closed all over the place, it sometimes gets called inside an Update
|
|
|
|
// call. This then causes disposed data to be accessed. Therefore, save closing until the
|
|
|
|
// end of the update loop.
|
|
|
|
if (_closeLock)
|
|
|
|
{
|
|
|
|
_requireClose = true;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
CloseChatLog();
|
|
|
|
CloseServerLog();
|
|
|
|
CloseConnection();
|
|
|
|
|
|
|
|
client_connection_list.clear();
|
2019-08-20 23:51:12 +02:00
|
|
|
GameActions::ClearQueue();
|
2019-10-05 13:09:21 +02:00
|
|
|
GameActions::ResumeQueue();
|
2018-03-23 19:29:45 +01:00
|
|
|
player_list.clear();
|
|
|
|
group_list.clear();
|
2019-05-15 19:55:16 +02:00
|
|
|
_serverTickData.clear();
|
|
|
|
_pendingPlayerLists.clear();
|
2019-03-24 23:16:57 +01:00
|
|
|
_pendingPlayerInfo.clear();
|
2018-03-23 19:29:45 +01:00
|
|
|
|
|
|
|
gfx_invalidate_screen();
|
|
|
|
|
|
|
|
_requireClose = false;
|
2017-06-06 23:24:18 +02:00
|
|
|
}
|
2018-03-23 19:29:45 +01:00
|
|
|
}
|
2017-06-06 23:24:18 +02:00
|
|
|
|
2019-03-01 09:25:16 +01:00
|
|
|
void Network::DecayCooldown(NetworkPlayer* player)
|
|
|
|
{
|
|
|
|
if (player == nullptr)
|
|
|
|
return; // No valid connection yet.
|
|
|
|
|
|
|
|
for (auto it = std::begin(player->CooldownTime); it != std::end(player->CooldownTime);)
|
|
|
|
{
|
|
|
|
it->second -= _currentDeltaTime;
|
|
|
|
if (it->second <= 0)
|
|
|
|
it = player->CooldownTime.erase(it);
|
|
|
|
else
|
|
|
|
it++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-03-23 19:29:45 +01:00
|
|
|
void Network::CloseConnection()
|
|
|
|
{
|
|
|
|
if (mode == NETWORK_MODE_CLIENT)
|
|
|
|
{
|
2018-12-17 12:58:12 +01:00
|
|
|
_serverConnection.reset();
|
2018-03-23 19:29:45 +01:00
|
|
|
}
|
|
|
|
else if (mode == NETWORK_MODE_SERVER)
|
|
|
|
{
|
2018-12-17 12:58:12 +01:00
|
|
|
_listenSocket.reset();
|
|
|
|
_advertiser.reset();
|
2017-06-06 23:24:18 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
mode = NETWORK_MODE_NONE;
|
|
|
|
status = NETWORK_STATUS_NONE;
|
|
|
|
_lastConnectStatus = SOCKET_STATUS_CLOSED;
|
|
|
|
|
|
|
|
DisposeWSA();
|
2015-02-12 03:01:02 +01:00
|
|
|
}
|
|
|
|
|
2019-02-23 15:54:17 +01:00
|
|
|
bool Network::BeginClient(const std::string& host, uint16_t port)
|
2015-02-12 12:30:57 +01:00
|
|
|
{
|
2018-06-22 23:02:47 +02:00
|
|
|
if (GetMode() != NETWORK_MODE_NONE)
|
|
|
|
{
|
2017-06-06 23:24:18 +02:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
Close();
|
|
|
|
if (!Init())
|
|
|
|
return false;
|
|
|
|
|
|
|
|
mode = NETWORK_MODE_CLIENT;
|
|
|
|
|
2019-05-12 13:48:56 +02:00
|
|
|
log_info("Connecting to %s:%u", host.c_str(), port);
|
2019-02-05 06:57:09 +01:00
|
|
|
_host = host;
|
|
|
|
_port = port;
|
2017-07-15 11:24:08 +02:00
|
|
|
|
2018-12-17 12:58:12 +01:00
|
|
|
_serverConnection = std::make_unique<NetworkConnection>();
|
|
|
|
_serverConnection->Socket = CreateTcpSocket();
|
|
|
|
_serverConnection->Socket->ConnectAsync(host, port);
|
2019-05-11 21:31:34 +02:00
|
|
|
_serverState.gamestateSnapshotsEnabled = false;
|
|
|
|
|
2017-06-06 23:24:18 +02:00
|
|
|
status = NETWORK_STATUS_CONNECTING;
|
|
|
|
_lastConnectStatus = SOCKET_STATUS_CLOSED;
|
2019-03-12 14:24:48 +01:00
|
|
|
_clientMapLoaded = false;
|
2019-03-12 21:07:48 +01:00
|
|
|
_serverTickData.clear();
|
2017-06-06 23:24:18 +02:00
|
|
|
|
|
|
|
BeginChatLog();
|
|
|
|
BeginServerLog();
|
|
|
|
|
2019-10-05 13:09:21 +02:00
|
|
|
// We need to wait for the map load before we execute any actions.
|
|
|
|
// If the client has the title screen running then theres a potential
|
|
|
|
// risk of tick collision with the server map and title screen map.
|
|
|
|
GameActions::SuspendQueue();
|
|
|
|
|
2017-06-06 23:24:18 +02:00
|
|
|
utf8 keyPath[MAX_PATH];
|
|
|
|
network_get_private_key_path(keyPath, sizeof(keyPath), gConfigNetwork.player_name);
|
2020-07-02 17:03:31 +02:00
|
|
|
if (!Platform::FileExists(keyPath))
|
2018-06-22 23:02:47 +02:00
|
|
|
{
|
2017-06-06 23:24:18 +02:00
|
|
|
Console::WriteLine("Generating key... This may take a while");
|
|
|
|
Console::WriteLine("Need to collect enough entropy from the system");
|
|
|
|
_key.Generate();
|
|
|
|
Console::WriteLine("Key generated, saving private bits as %s", keyPath);
|
|
|
|
|
|
|
|
utf8 keysDirectory[MAX_PATH];
|
|
|
|
network_get_keys_directory(keysDirectory, sizeof(keysDirectory));
|
2018-06-22 23:02:47 +02:00
|
|
|
if (!platform_ensure_directory_exists(keysDirectory))
|
|
|
|
{
|
2017-06-06 23:24:18 +02:00
|
|
|
log_error("Unable to create directory %s.", keysDirectory);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
try
|
|
|
|
{
|
|
|
|
auto fs = FileStream(keyPath, FILE_MODE_WRITE);
|
|
|
|
_key.SavePrivate(&fs);
|
|
|
|
}
|
2018-06-22 23:02:47 +02:00
|
|
|
catch (const std::exception&)
|
2017-06-06 23:24:18 +02:00
|
|
|
{
|
|
|
|
log_error("Unable to save private key at %s.", keyPath);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
const std::string hash = _key.PublicKeyHash();
|
2018-06-22 23:02:47 +02:00
|
|
|
const utf8* publicKeyHash = hash.c_str();
|
2017-06-06 23:24:18 +02:00
|
|
|
network_get_public_key_path(keyPath, sizeof(keyPath), gConfigNetwork.player_name, publicKeyHash);
|
|
|
|
Console::WriteLine("Key generated, saving public bits as %s", keyPath);
|
|
|
|
|
|
|
|
try
|
|
|
|
{
|
|
|
|
auto fs = FileStream(keyPath, FILE_MODE_WRITE);
|
|
|
|
_key.SavePublic(&fs);
|
|
|
|
}
|
2018-06-22 23:02:47 +02:00
|
|
|
catch (const std::exception&)
|
2017-06-06 23:24:18 +02:00
|
|
|
{
|
|
|
|
log_error("Unable to save public key at %s.", keyPath);
|
|
|
|
return false;
|
|
|
|
}
|
2018-06-22 23:02:47 +02:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2017-06-06 23:24:18 +02:00
|
|
|
// LoadPrivate returns validity of loaded key
|
|
|
|
bool ok = false;
|
|
|
|
try
|
|
|
|
{
|
|
|
|
log_verbose("Loading key from %s", keyPath);
|
|
|
|
auto fs = FileStream(keyPath, FILE_MODE_OPEN);
|
|
|
|
ok = _key.LoadPrivate(&fs);
|
|
|
|
}
|
2018-06-22 23:02:47 +02:00
|
|
|
catch (const std::exception&)
|
2017-06-06 23:24:18 +02:00
|
|
|
{
|
|
|
|
log_error("Unable to read private key from %s.", keyPath);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Don't store private key in memory when it's not in use.
|
|
|
|
_key.Unload();
|
|
|
|
return ok;
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
2015-02-12 12:30:57 +01:00
|
|
|
}
|
|
|
|
|
2019-02-23 15:54:17 +01:00
|
|
|
bool Network::BeginServer(uint16_t port, const std::string& address)
|
2015-02-12 12:30:57 +01:00
|
|
|
{
|
2017-06-06 23:24:18 +02:00
|
|
|
Close();
|
|
|
|
if (!Init())
|
|
|
|
return false;
|
|
|
|
|
|
|
|
mode = NETWORK_MODE_SERVER;
|
|
|
|
|
|
|
|
_userManager.Load();
|
|
|
|
|
|
|
|
log_verbose("Begin listening for clients");
|
|
|
|
|
2018-12-17 12:58:12 +01:00
|
|
|
_listenSocket = CreateTcpSocket();
|
2017-06-06 23:24:18 +02:00
|
|
|
try
|
|
|
|
{
|
2018-12-17 12:58:12 +01:00
|
|
|
_listenSocket->Listen(address, port);
|
2017-06-06 23:24:18 +02:00
|
|
|
}
|
2018-06-22 23:02:47 +02:00
|
|
|
catch (const std::exception& ex)
|
2017-06-06 23:24:18 +02:00
|
|
|
{
|
2018-01-02 20:23:22 +01:00
|
|
|
Console::Error::WriteLine(ex.what());
|
2017-06-06 23:24:18 +02:00
|
|
|
Close();
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2019-02-23 15:54:17 +01:00
|
|
|
ServerName = gConfigNetwork.server_name;
|
|
|
|
ServerDescription = gConfigNetwork.server_description;
|
|
|
|
ServerGreeting = gConfigNetwork.server_greeting;
|
|
|
|
ServerProviderName = gConfigNetwork.provider_name;
|
|
|
|
ServerProviderEmail = gConfigNetwork.provider_email;
|
|
|
|
ServerProviderWebsite = gConfigNetwork.provider_website;
|
2017-06-06 23:24:18 +02:00
|
|
|
|
2019-05-04 15:28:38 +02:00
|
|
|
CheatsReset();
|
2017-06-06 23:24:18 +02:00
|
|
|
LoadGroups();
|
|
|
|
BeginChatLog();
|
|
|
|
BeginServerLog();
|
|
|
|
|
2018-06-22 23:02:47 +02:00
|
|
|
NetworkPlayer* player = AddPlayer(gConfigNetwork.player_name, "");
|
2017-06-06 23:24:18 +02:00
|
|
|
player->Flags |= NETWORK_PLAYER_FLAG_ISSERVER;
|
|
|
|
player->Group = 0;
|
|
|
|
player_id = player->Id;
|
|
|
|
|
2018-06-22 23:02:47 +02:00
|
|
|
if (network_get_mode() == NETWORK_MODE_SERVER)
|
|
|
|
{
|
2017-06-06 23:24:18 +02:00
|
|
|
// Add SERVER to users.json and save.
|
2018-06-22 23:02:47 +02:00
|
|
|
NetworkUser* networkUser = _userManager.GetOrAddUser(player->KeyHash);
|
2017-06-06 23:24:18 +02:00
|
|
|
networkUser->GroupId = player->Group;
|
|
|
|
networkUser->Name = player->Name;
|
|
|
|
_userManager.Save();
|
|
|
|
}
|
|
|
|
|
|
|
|
printf("Ready for clients...\n");
|
|
|
|
network_chat_show_connected_message();
|
|
|
|
network_chat_show_server_greeting();
|
|
|
|
|
|
|
|
status = NETWORK_STATUS_CONNECTED;
|
|
|
|
listening_port = port;
|
2019-05-11 21:31:34 +02:00
|
|
|
_serverState.gamestateSnapshotsEnabled = gConfigNetwork.desync_debugging;
|
2019-05-05 03:02:20 +02:00
|
|
|
_advertiser = CreateServerAdvertiser(listening_port);
|
2017-06-06 23:24:18 +02:00
|
|
|
|
2020-02-22 23:18:22 +01:00
|
|
|
game_load_scripts();
|
|
|
|
|
2017-06-06 23:24:18 +02:00
|
|
|
return true;
|
2015-02-12 03:01:02 +01:00
|
|
|
}
|
|
|
|
|
2018-06-20 17:28:51 +02:00
|
|
|
int32_t Network::GetMode()
|
2015-02-12 03:01:02 +01:00
|
|
|
{
|
2017-06-06 23:24:18 +02:00
|
|
|
return mode;
|
2015-02-12 03:01:02 +01:00
|
|
|
}
|
|
|
|
|
2018-06-20 17:28:51 +02:00
|
|
|
int32_t Network::GetStatus()
|
2015-10-27 04:43:33 +01:00
|
|
|
{
|
2017-06-06 23:24:18 +02:00
|
|
|
return status;
|
2015-10-27 04:43:33 +01:00
|
|
|
}
|
|
|
|
|
2018-06-20 17:28:51 +02:00
|
|
|
int32_t Network::GetAuthStatus()
|
2015-07-16 19:32:43 +02:00
|
|
|
{
|
2018-06-22 23:02:47 +02:00
|
|
|
if (GetMode() == NETWORK_MODE_CLIENT)
|
|
|
|
{
|
2018-12-17 12:58:12 +01:00
|
|
|
return _serverConnection->AuthStatus;
|
2018-06-22 23:02:47 +02:00
|
|
|
}
|
|
|
|
else if (GetMode() == NETWORK_MODE_SERVER)
|
|
|
|
{
|
2017-06-06 23:24:18 +02:00
|
|
|
return NETWORK_AUTH_OK;
|
|
|
|
}
|
|
|
|
return NETWORK_AUTH_NONE;
|
2015-07-16 19:32:43 +02:00
|
|
|
}
|
|
|
|
|
2018-06-20 17:28:51 +02:00
|
|
|
uint32_t Network::GetServerTick()
|
2015-02-12 12:30:57 +01:00
|
|
|
{
|
2019-05-11 21:31:34 +02:00
|
|
|
return _serverState.tick;
|
2015-07-08 03:21:05 +02:00
|
|
|
}
|
2015-02-12 12:30:57 +01:00
|
|
|
|
2018-06-20 17:28:51 +02:00
|
|
|
uint8_t Network::GetPlayerID()
|
2015-07-24 01:22:03 +02:00
|
|
|
{
|
2017-06-06 23:24:18 +02:00
|
|
|
return player_id;
|
2015-07-24 01:22:03 +02:00
|
|
|
}
|
|
|
|
|
2020-04-27 20:54:29 +02:00
|
|
|
NetworkConnection* Network::GetPlayerConnection(uint8_t id)
|
|
|
|
{
|
|
|
|
auto player = GetPlayerByID(id);
|
|
|
|
if (player != nullptr)
|
|
|
|
{
|
2020-04-30 03:14:33 +02:00
|
|
|
auto clientIt = std::find_if(
|
2020-04-30 18:12:02 +02:00
|
|
|
client_connection_list.begin(), client_connection_list.end(),
|
|
|
|
[player](const auto& conn) -> bool { return conn->Player == player; });
|
2020-04-30 03:14:33 +02:00
|
|
|
return clientIt != client_connection_list.end() ? clientIt->get() : nullptr;
|
2020-04-27 20:54:29 +02:00
|
|
|
}
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
2015-07-10 21:53:41 +02:00
|
|
|
void Network::Update()
|
2015-07-08 03:21:05 +02:00
|
|
|
{
|
2017-06-06 23:24:18 +02:00
|
|
|
_closeLock = true;
|
2017-06-06 21:05:41 +02:00
|
|
|
|
2019-03-01 09:25:16 +01:00
|
|
|
// Update is not necessarily called per game tick, maintain our own delta time
|
|
|
|
uint32_t ticks = platform_get_ticks();
|
|
|
|
_currentDeltaTime = std::max<uint32_t>(ticks - _lastUpdateTime, 1);
|
|
|
|
_lastUpdateTime = ticks;
|
|
|
|
|
2018-06-22 23:02:47 +02:00
|
|
|
switch (GetMode())
|
|
|
|
{
|
|
|
|
case NETWORK_MODE_SERVER:
|
|
|
|
UpdateServer();
|
|
|
|
break;
|
|
|
|
case NETWORK_MODE_CLIENT:
|
|
|
|
UpdateClient();
|
|
|
|
break;
|
2017-06-06 23:24:18 +02:00
|
|
|
}
|
2017-06-06 21:05:41 +02:00
|
|
|
|
2017-06-06 23:24:18 +02:00
|
|
|
// If the Close() was called during the update, close it for real
|
|
|
|
_closeLock = false;
|
2018-06-22 23:02:47 +02:00
|
|
|
if (_requireClose)
|
|
|
|
{
|
2017-06-06 23:24:18 +02:00
|
|
|
Close();
|
2019-02-05 06:57:09 +01:00
|
|
|
if (_requireReconnect)
|
|
|
|
{
|
|
|
|
Reconnect();
|
|
|
|
}
|
2017-06-06 23:24:18 +02:00
|
|
|
}
|
2015-08-16 00:19:15 +02:00
|
|
|
}
|
|
|
|
|
2017-07-15 11:24:08 +02:00
|
|
|
void Network::Flush()
|
|
|
|
{
|
|
|
|
if (GetMode() == NETWORK_MODE_CLIENT)
|
|
|
|
{
|
2018-12-17 12:58:12 +01:00
|
|
|
_serverConnection->SendQueuedPackets();
|
2017-07-15 11:24:08 +02:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
for (auto& it : client_connection_list)
|
|
|
|
{
|
|
|
|
it->SendQueuedPackets();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-08-16 00:19:15 +02:00
|
|
|
void Network::UpdateServer()
|
|
|
|
{
|
2019-05-15 19:55:16 +02:00
|
|
|
for (auto& connection : client_connection_list)
|
2018-06-22 23:02:47 +02:00
|
|
|
{
|
2019-05-15 19:55:16 +02:00
|
|
|
// This can be called multiple times before the connection is removed.
|
|
|
|
if (connection->IsDisconnected)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
if (!ProcessConnection(*connection))
|
2018-06-22 23:02:47 +02:00
|
|
|
{
|
2019-05-15 19:55:16 +02:00
|
|
|
connection->IsDisconnected = true;
|
2018-06-22 23:02:47 +02:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2019-05-15 19:55:16 +02:00
|
|
|
DecayCooldown(connection->Player);
|
2017-06-06 23:24:18 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-06-20 17:28:51 +02:00
|
|
|
uint32_t ticks = platform_get_ticks();
|
2018-06-22 23:02:47 +02:00
|
|
|
if (ticks > last_ping_sent_time + 3000)
|
|
|
|
{
|
2017-06-06 23:24:18 +02:00
|
|
|
Server_Send_PING();
|
|
|
|
Server_Send_PINGLIST();
|
|
|
|
}
|
|
|
|
|
2018-06-22 23:02:47 +02:00
|
|
|
if (_advertiser != nullptr)
|
|
|
|
{
|
2017-06-06 23:24:18 +02:00
|
|
|
_advertiser->Update();
|
|
|
|
}
|
|
|
|
|
2018-12-17 12:58:12 +01:00
|
|
|
std::unique_ptr<ITcpSocket> tcpSocket = _listenSocket->Accept();
|
2018-06-22 23:02:47 +02:00
|
|
|
if (tcpSocket != nullptr)
|
|
|
|
{
|
2018-12-17 12:58:12 +01:00
|
|
|
AddClient(std::move(tcpSocket));
|
2017-06-06 23:24:18 +02:00
|
|
|
}
|
2015-08-16 00:19:15 +02:00
|
|
|
}
|
2015-08-14 17:59:27 +02:00
|
|
|
|
2015-08-16 00:19:15 +02:00
|
|
|
void Network::UpdateClient()
|
|
|
|
{
|
2018-12-17 12:58:12 +01:00
|
|
|
assert(_serverConnection != nullptr);
|
2017-06-06 23:24:18 +02:00
|
|
|
|
2018-06-22 23:02:47 +02:00
|
|
|
switch (status)
|
2017-06-06 23:24:18 +02:00
|
|
|
{
|
2018-06-22 23:02:47 +02:00
|
|
|
case NETWORK_STATUS_CONNECTING:
|
2017-06-06 23:24:18 +02:00
|
|
|
{
|
2018-12-17 12:58:12 +01:00
|
|
|
switch (_serverConnection->Socket->GetStatus())
|
2017-06-06 23:24:18 +02:00
|
|
|
{
|
2018-06-22 23:02:47 +02:00
|
|
|
case SOCKET_STATUS_RESOLVING:
|
|
|
|
{
|
|
|
|
if (_lastConnectStatus != SOCKET_STATUS_RESOLVING)
|
|
|
|
{
|
|
|
|
_lastConnectStatus = SOCKET_STATUS_RESOLVING;
|
|
|
|
char str_resolving[256];
|
|
|
|
format_string(str_resolving, 256, STR_MULTIPLAYER_RESOLVING, nullptr);
|
|
|
|
|
|
|
|
auto intent = Intent(WC_NETWORK_STATUS);
|
|
|
|
intent.putExtra(INTENT_EXTRA_MESSAGE, std::string{ str_resolving });
|
|
|
|
intent.putExtra(INTENT_EXTRA_CALLBACK, []() -> void { gNetwork.Close(); });
|
|
|
|
context_open_intent(&intent);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case SOCKET_STATUS_CONNECTING:
|
|
|
|
{
|
|
|
|
if (_lastConnectStatus != SOCKET_STATUS_CONNECTING)
|
|
|
|
{
|
|
|
|
_lastConnectStatus = SOCKET_STATUS_CONNECTING;
|
|
|
|
char str_connecting[256];
|
|
|
|
format_string(str_connecting, 256, STR_MULTIPLAYER_CONNECTING, nullptr);
|
|
|
|
|
|
|
|
auto intent = Intent(WC_NETWORK_STATUS);
|
|
|
|
intent.putExtra(INTENT_EXTRA_MESSAGE, std::string{ str_connecting });
|
|
|
|
intent.putExtra(INTENT_EXTRA_CALLBACK, []() -> void { gNetwork.Close(); });
|
|
|
|
context_open_intent(&intent);
|
|
|
|
|
|
|
|
server_connect_time = platform_get_ticks();
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case SOCKET_STATUS_CONNECTED:
|
|
|
|
{
|
|
|
|
status = NETWORK_STATUS_CONNECTED;
|
2018-12-17 12:58:12 +01:00
|
|
|
_serverConnection->ResetLastPacketTime();
|
2018-06-22 23:02:47 +02:00
|
|
|
Client_Send_TOKEN();
|
|
|
|
char str_authenticating[256];
|
|
|
|
format_string(str_authenticating, 256, STR_MULTIPLAYER_AUTHENTICATING, nullptr);
|
|
|
|
|
|
|
|
auto intent = Intent(WC_NETWORK_STATUS);
|
|
|
|
intent.putExtra(INTENT_EXTRA_MESSAGE, std::string{ str_authenticating });
|
|
|
|
intent.putExtra(INTENT_EXTRA_CALLBACK, []() -> void { gNetwork.Close(); });
|
|
|
|
context_open_intent(&intent);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
default:
|
|
|
|
{
|
2018-12-17 12:58:12 +01:00
|
|
|
const char* error = _serverConnection->Socket->GetError();
|
2018-06-22 23:02:47 +02:00
|
|
|
if (error != nullptr)
|
|
|
|
{
|
|
|
|
Console::Error::WriteLine(error);
|
|
|
|
}
|
|
|
|
|
|
|
|
Close();
|
|
|
|
context_force_close_window_by_class(WC_NETWORK_STATUS);
|
|
|
|
context_show_error(STR_UNABLE_TO_CONNECT_TO_SERVER, STR_NONE);
|
|
|
|
break;
|
|
|
|
}
|
2017-06-06 23:24:18 +02:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
2018-06-22 23:02:47 +02:00
|
|
|
case NETWORK_STATUS_CONNECTED:
|
2017-06-06 23:24:18 +02:00
|
|
|
{
|
2018-12-17 12:58:12 +01:00
|
|
|
if (!ProcessConnection(*_serverConnection))
|
2017-06-06 23:24:18 +02:00
|
|
|
{
|
2018-06-22 23:02:47 +02:00
|
|
|
// Do not show disconnect message window when password window closed/canceled
|
2018-12-17 12:58:12 +01:00
|
|
|
if (_serverConnection->AuthStatus == NETWORK_AUTH_REQUIREPASSWORD)
|
2018-06-22 23:02:47 +02:00
|
|
|
{
|
|
|
|
context_force_close_window_by_class(WC_NETWORK_STATUS);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
char str_disconnected[256];
|
2017-06-06 23:24:18 +02:00
|
|
|
|
2018-12-17 12:58:12 +01:00
|
|
|
if (_serverConnection->GetLastDisconnectReason())
|
2018-06-22 23:02:47 +02:00
|
|
|
{
|
2018-12-17 12:58:12 +01:00
|
|
|
const char* disconnect_reason = _serverConnection->GetLastDisconnectReason();
|
2018-06-22 23:02:47 +02:00
|
|
|
format_string(str_disconnected, 256, STR_MULTIPLAYER_DISCONNECTED_WITH_REASON, &disconnect_reason);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
format_string(str_disconnected, 256, STR_MULTIPLAYER_DISCONNECTED_NO_REASON, nullptr);
|
|
|
|
}
|
2017-06-06 23:24:18 +02:00
|
|
|
|
2018-06-22 23:02:47 +02:00
|
|
|
auto intent = Intent(WC_NETWORK_STATUS);
|
|
|
|
intent.putExtra(INTENT_EXTRA_MESSAGE, std::string{ str_disconnected });
|
|
|
|
context_open_intent(&intent);
|
2017-06-06 23:24:18 +02:00
|
|
|
}
|
2018-06-22 23:02:47 +02:00
|
|
|
window_close_by_class(WC_MULTIPLAYER);
|
|
|
|
Close();
|
2017-06-06 23:24:18 +02:00
|
|
|
}
|
2020-07-24 22:49:05 +02:00
|
|
|
else
|
|
|
|
{
|
|
|
|
uint32_t ticks = platform_get_ticks();
|
|
|
|
if (ticks - _lastSentHeartbeat >= 3000)
|
|
|
|
{
|
|
|
|
Client_Send_HEARTBEAT(*_serverConnection);
|
|
|
|
_lastSentHeartbeat = ticks;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-06-22 23:02:47 +02:00
|
|
|
break;
|
2017-06-06 23:24:18 +02:00
|
|
|
}
|
|
|
|
}
|
2015-07-08 03:21:05 +02:00
|
|
|
}
|
2015-02-12 12:30:57 +01:00
|
|
|
|
2018-06-20 17:28:51 +02:00
|
|
|
std::vector<std::unique_ptr<NetworkPlayer>>::iterator Network::GetPlayerIteratorByID(uint8_t id)
|
2016-01-20 23:45:09 +01:00
|
|
|
{
|
2018-06-22 23:02:47 +02:00
|
|
|
auto it = std::find_if(player_list.begin(), player_list.end(), [&id](std::unique_ptr<NetworkPlayer> const& player) {
|
|
|
|
return player->Id == id;
|
|
|
|
});
|
|
|
|
if (it != player_list.end())
|
|
|
|
{
|
2017-06-06 23:24:18 +02:00
|
|
|
return it;
|
|
|
|
}
|
|
|
|
return player_list.end();
|
2016-01-20 23:45:09 +01:00
|
|
|
}
|
|
|
|
|
2018-06-20 17:28:51 +02:00
|
|
|
NetworkPlayer* Network::GetPlayerByID(uint8_t id)
|
2016-01-20 23:45:09 +01:00
|
|
|
{
|
2017-06-06 23:24:18 +02:00
|
|
|
auto it = GetPlayerIteratorByID(id);
|
2018-06-22 23:02:47 +02:00
|
|
|
if (it != player_list.end())
|
|
|
|
{
|
2017-06-06 23:24:18 +02:00
|
|
|
return it->get();
|
|
|
|
}
|
|
|
|
return nullptr;
|
2016-01-20 23:45:09 +01:00
|
|
|
}
|
|
|
|
|
2018-06-20 17:28:51 +02:00
|
|
|
std::vector<std::unique_ptr<NetworkGroup>>::iterator Network::GetGroupIteratorByID(uint8_t id)
|
2016-01-20 23:45:09 +01:00
|
|
|
{
|
2018-06-22 23:02:47 +02:00
|
|
|
auto it = std::find_if(
|
|
|
|
group_list.begin(), group_list.end(), [&id](std::unique_ptr<NetworkGroup> const& group) { return group->Id == id; });
|
|
|
|
if (it != group_list.end())
|
|
|
|
{
|
2017-06-06 23:24:18 +02:00
|
|
|
return it;
|
|
|
|
}
|
|
|
|
return group_list.end();
|
2016-01-20 23:45:09 +01:00
|
|
|
}
|
|
|
|
|
2018-06-20 17:28:51 +02:00
|
|
|
NetworkGroup* Network::GetGroupByID(uint8_t id)
|
2016-01-20 23:45:09 +01:00
|
|
|
{
|
2017-06-06 23:24:18 +02:00
|
|
|
auto it = GetGroupIteratorByID(id);
|
2018-06-22 23:02:47 +02:00
|
|
|
if (it != group_list.end())
|
|
|
|
{
|
2017-06-06 23:24:18 +02:00
|
|
|
return it->get();
|
|
|
|
}
|
|
|
|
return nullptr;
|
2015-07-17 02:24:39 +02:00
|
|
|
}
|
|
|
|
|
2015-07-19 01:57:42 +02:00
|
|
|
const char* Network::FormatChat(NetworkPlayer* fromplayer, const char* text)
|
|
|
|
{
|
2017-06-06 23:24:18 +02:00
|
|
|
static char formatted[1024];
|
|
|
|
char* lineCh = formatted;
|
|
|
|
formatted[0] = 0;
|
2018-06-22 23:02:47 +02:00
|
|
|
if (fromplayer)
|
|
|
|
{
|
2017-06-06 23:24:18 +02:00
|
|
|
lineCh = utf8_write_codepoint(lineCh, FORMAT_OUTLINE);
|
|
|
|
lineCh = utf8_write_codepoint(lineCh, FORMAT_BABYBLUE);
|
2020-04-22 17:09:29 +02:00
|
|
|
safe_strcpy(lineCh, static_cast<const char*>(fromplayer->Name.c_str()), sizeof(formatted) - (lineCh - formatted));
|
2017-06-06 23:24:18 +02:00
|
|
|
safe_strcat(lineCh, ": ", sizeof(formatted) - (lineCh - formatted));
|
|
|
|
lineCh = strchr(lineCh, '\0');
|
|
|
|
}
|
|
|
|
lineCh = utf8_write_codepoint(lineCh, FORMAT_OUTLINE);
|
|
|
|
lineCh = utf8_write_codepoint(lineCh, FORMAT_WHITE);
|
|
|
|
char* ptrtext = lineCh;
|
|
|
|
safe_strcpy(lineCh, text, 800);
|
2018-02-15 13:04:25 +01:00
|
|
|
utf8_remove_format_codes(ptrtext, true);
|
2017-06-06 23:24:18 +02:00
|
|
|
return formatted;
|
2015-07-19 01:57:42 +02:00
|
|
|
}
|
|
|
|
|
2017-06-16 07:53:20 +02:00
|
|
|
void Network::SendPacketToClients(NetworkPacket& packet, bool front, bool gameCmd)
|
2015-07-19 01:57:42 +02:00
|
|
|
{
|
2018-06-22 23:02:47 +02:00
|
|
|
for (auto& client_connection : client_connection_list)
|
|
|
|
{
|
2019-05-15 19:55:16 +02:00
|
|
|
if (client_connection->IsDisconnected)
|
|
|
|
{
|
|
|
|
// Client will be removed at the end of the tick, don't bother.
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2018-06-22 23:02:47 +02:00
|
|
|
if (gameCmd)
|
|
|
|
{
|
2017-06-16 07:53:20 +02:00
|
|
|
// If marked as game command we can not send the packet to connections that are not fully connected.
|
|
|
|
// Sending the packet would cause the client to store a command that is behind the tick where he starts,
|
2018-06-22 23:02:47 +02:00
|
|
|
// which would be essentially never executed. The clients do not require commands before the server has not sent the
|
|
|
|
// map data.
|
|
|
|
if (client_connection->Player == nullptr)
|
|
|
|
{
|
2017-06-16 07:53:20 +02:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
}
|
2017-10-03 00:00:32 +02:00
|
|
|
client_connection->QueuePacket(NetworkPacket::Duplicate(packet), front);
|
2017-06-06 23:24:18 +02:00
|
|
|
}
|
2015-07-19 01:57:42 +02:00
|
|
|
}
|
|
|
|
|
2018-06-20 17:28:51 +02:00
|
|
|
bool Network::CheckSRAND(uint32_t tick, uint32_t srand0)
|
2015-07-30 16:34:17 +02:00
|
|
|
{
|
2019-03-12 14:24:48 +01:00
|
|
|
// We have to wait for the map to be loaded first, ticks may match current loaded map.
|
2019-05-10 22:00:38 +02:00
|
|
|
if (!_clientMapLoaded)
|
2019-03-12 14:24:48 +01:00
|
|
|
return true;
|
|
|
|
|
2019-03-12 21:07:48 +01:00
|
|
|
auto itTickData = _serverTickData.find(tick);
|
|
|
|
if (itTickData == std::end(_serverTickData))
|
|
|
|
return true;
|
|
|
|
|
|
|
|
const ServerTickData_t storedTick = itTickData->second;
|
|
|
|
_serverTickData.erase(itTickData);
|
|
|
|
|
|
|
|
if (storedTick.srand0 != srand0)
|
2019-05-11 21:31:34 +02:00
|
|
|
{
|
|
|
|
log_info("Srand0 mismatch, client = %08X, server = %08X", srand0, storedTick.srand0);
|
2019-03-12 21:07:48 +01:00
|
|
|
return false;
|
2019-05-11 21:31:34 +02:00
|
|
|
}
|
2019-03-12 21:07:48 +01:00
|
|
|
|
2019-05-10 22:00:38 +02:00
|
|
|
if (!storedTick.spriteHash.empty())
|
2018-06-22 23:02:47 +02:00
|
|
|
{
|
2018-12-11 03:05:57 +01:00
|
|
|
rct_sprite_checksum checksum = sprite_checksum();
|
2019-03-12 21:07:48 +01:00
|
|
|
std::string clientSpriteHash = checksum.ToString();
|
|
|
|
if (clientSpriteHash != storedTick.spriteHash)
|
2018-06-22 23:02:47 +02:00
|
|
|
{
|
2019-05-11 21:31:34 +02:00
|
|
|
log_info("Sprite hash mismatch, client = %s, server = %s", clientSpriteHash.c_str(), storedTick.spriteHash.c_str());
|
2017-06-06 23:24:18 +02:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
2017-07-15 11:24:08 +02:00
|
|
|
|
2017-06-06 23:24:18 +02:00
|
|
|
return true;
|
2015-07-30 16:34:17 +02:00
|
|
|
}
|
|
|
|
|
2019-02-08 01:32:05 +01:00
|
|
|
bool Network::IsDesynchronised()
|
2019-02-04 06:54:45 +01:00
|
|
|
{
|
2019-05-11 21:31:34 +02:00
|
|
|
return _serverState.state == NETWORK_SERVER_STATE_DESYNCED;
|
2019-02-04 06:54:45 +01:00
|
|
|
}
|
|
|
|
|
2019-05-11 21:31:34 +02:00
|
|
|
bool Network::CheckDesynchronizaton()
|
2017-07-15 11:24:08 +02:00
|
|
|
{
|
|
|
|
// Check synchronisation
|
2019-05-11 21:31:34 +02:00
|
|
|
if (GetMode() == NETWORK_MODE_CLIENT && _serverState.state != NETWORK_SERVER_STATE_DESYNCED
|
|
|
|
&& !CheckSRAND(gCurrentTicks, scenario_rand_state().s0))
|
2018-06-22 23:02:47 +02:00
|
|
|
{
|
2019-05-11 21:31:34 +02:00
|
|
|
_serverState.state = NETWORK_SERVER_STATE_DESYNCED;
|
|
|
|
_serverState.desyncTick = gCurrentTicks;
|
2017-07-15 11:24:08 +02:00
|
|
|
|
|
|
|
char str_desync[256];
|
2018-01-04 06:58:44 +01:00
|
|
|
format_string(str_desync, 256, STR_MULTIPLAYER_DESYNC, nullptr);
|
2017-10-21 19:30:03 +02:00
|
|
|
|
|
|
|
auto intent = Intent(WC_NETWORK_STATUS);
|
2018-06-22 23:02:47 +02:00
|
|
|
intent.putExtra(INTENT_EXTRA_MESSAGE, std::string{ str_desync });
|
2017-10-21 19:30:03 +02:00
|
|
|
context_open_intent(&intent);
|
|
|
|
|
2018-06-22 23:02:47 +02:00
|
|
|
if (!gConfigNetwork.stay_connected)
|
|
|
|
{
|
2017-07-15 11:24:08 +02:00
|
|
|
Close();
|
|
|
|
}
|
2019-05-11 21:31:34 +02:00
|
|
|
|
|
|
|
return true;
|
2017-07-15 11:24:08 +02:00
|
|
|
}
|
2019-05-11 21:31:34 +02:00
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
void Network::RequestStateSnapshot()
|
|
|
|
{
|
|
|
|
log_info("Requesting game state for tick %u", _serverState.desyncTick);
|
|
|
|
|
|
|
|
Client_Send_RequestGameState(_serverState.desyncTick);
|
|
|
|
}
|
|
|
|
|
|
|
|
NetworkServerState_t Network::GetServerState() const
|
|
|
|
{
|
|
|
|
return _serverState;
|
2017-07-15 11:24:08 +02:00
|
|
|
}
|
|
|
|
|
2018-06-20 17:28:51 +02:00
|
|
|
void Network::KickPlayer(int32_t playerId)
|
2015-11-02 01:28:53 +01:00
|
|
|
{
|
2018-06-22 23:02:47 +02:00
|
|
|
for (auto& client_connection : client_connection_list)
|
|
|
|
{
|
|
|
|
if (client_connection->Player->Id == playerId)
|
|
|
|
{
|
2017-06-06 23:24:18 +02:00
|
|
|
// Disconnect the client gracefully
|
2017-10-03 00:00:32 +02:00
|
|
|
client_connection->SetLastDisconnectReason(STR_MULTIPLAYER_KICKED);
|
2017-06-06 23:24:18 +02:00
|
|
|
char str_disconnect_msg[256];
|
2017-08-15 10:07:44 +02:00
|
|
|
format_string(str_disconnect_msg, 256, STR_MULTIPLAYER_KICKED_REASON, nullptr);
|
2017-10-03 00:00:32 +02:00
|
|
|
Server_Send_SETDISCONNECTMSG(*client_connection, str_disconnect_msg);
|
|
|
|
client_connection->Socket->Disconnect();
|
2017-06-06 23:24:18 +02:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2015-11-02 01:28:53 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void Network::SetPassword(const char* password)
|
|
|
|
{
|
2017-06-06 23:24:18 +02:00
|
|
|
_password = password == nullptr ? "" : password;
|
2015-11-04 00:31:09 +01:00
|
|
|
}
|
|
|
|
|
2019-05-15 19:55:16 +02:00
|
|
|
void Network::ServerClientDisconnected()
|
2015-11-04 00:31:09 +01:00
|
|
|
{
|
2018-06-22 23:02:47 +02:00
|
|
|
if (GetMode() == NETWORK_MODE_CLIENT)
|
|
|
|
{
|
2018-12-17 12:58:12 +01:00
|
|
|
_serverConnection->Socket->Disconnect();
|
2017-06-06 23:24:18 +02:00
|
|
|
}
|
2015-11-04 00:31:09 +01:00
|
|
|
}
|
|
|
|
|
2015-11-08 03:12:12 +01:00
|
|
|
std::string Network::GenerateAdvertiseKey()
|
2015-11-04 00:31:09 +01:00
|
|
|
{
|
2017-06-06 23:24:18 +02:00
|
|
|
// Generate a string of 16 random hex characters (64-integer key as a hex formatted string)
|
|
|
|
static char hexChars[] = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f' };
|
|
|
|
char key[17];
|
2018-06-22 23:02:47 +02:00
|
|
|
for (int32_t i = 0; i < 16; i++)
|
|
|
|
{
|
2018-11-21 23:16:04 +01:00
|
|
|
int32_t hexCharIndex = util_rand() % std::size(hexChars);
|
2017-06-06 23:24:18 +02:00
|
|
|
key[i] = hexChars[hexCharIndex];
|
|
|
|
}
|
2018-11-21 23:16:04 +01:00
|
|
|
key[std::size(key) - 1] = 0;
|
2015-11-08 03:12:12 +01:00
|
|
|
|
2017-06-06 23:24:18 +02:00
|
|
|
return key;
|
2015-11-08 03:12:12 +01:00
|
|
|
}
|
2015-11-08 00:56:16 +01:00
|
|
|
|
2019-02-23 15:54:17 +01:00
|
|
|
std::string Network::GetMasterServerUrl()
|
2015-11-08 03:12:12 +01:00
|
|
|
{
|
2019-02-23 15:54:17 +01:00
|
|
|
if (gConfigNetwork.master_server_url.empty())
|
2018-06-22 23:02:47 +02:00
|
|
|
{
|
2017-06-06 23:24:18 +02:00
|
|
|
return OPENRCT2_MASTER_SERVER_URL;
|
2018-06-22 23:02:47 +02:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2017-06-06 23:24:18 +02:00
|
|
|
return gConfigNetwork.master_server_url;
|
|
|
|
}
|
2015-11-08 03:12:12 +01:00
|
|
|
}
|
|
|
|
|
2016-01-22 04:32:51 +01:00
|
|
|
NetworkGroup* Network::AddGroup()
|
|
|
|
{
|
2017-06-06 23:24:18 +02:00
|
|
|
NetworkGroup* addedgroup = nullptr;
|
2018-06-20 17:28:51 +02:00
|
|
|
int32_t newid = -1;
|
2017-06-06 23:24:18 +02:00
|
|
|
// Find first unused group id
|
2018-06-22 23:02:47 +02:00
|
|
|
for (int32_t id = 0; id < 255; id++)
|
|
|
|
{
|
|
|
|
if (std::find_if(
|
2018-07-21 13:51:54 +02:00
|
|
|
group_list.begin(), group_list.end(),
|
2018-06-22 23:02:47 +02:00
|
|
|
[&id](std::unique_ptr<NetworkGroup> const& group) { return group->Id == id; })
|
|
|
|
== group_list.end())
|
|
|
|
{
|
2017-06-06 23:24:18 +02:00
|
|
|
newid = id;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2018-06-22 23:02:47 +02:00
|
|
|
if (newid != -1)
|
|
|
|
{
|
2018-05-12 15:13:48 +02:00
|
|
|
auto group = std::make_unique<NetworkGroup>();
|
2017-06-06 23:24:18 +02:00
|
|
|
group->Id = newid;
|
|
|
|
group->SetName("Group #" + std::to_string(newid));
|
|
|
|
addedgroup = group.get();
|
|
|
|
group_list.push_back(std::move(group));
|
|
|
|
}
|
|
|
|
return addedgroup;
|
2016-01-22 04:32:51 +01:00
|
|
|
}
|
|
|
|
|
2018-06-20 17:28:51 +02:00
|
|
|
void Network::RemoveGroup(uint8_t id)
|
2016-01-22 07:32:40 +01:00
|
|
|
{
|
2017-06-06 23:24:18 +02:00
|
|
|
auto group = GetGroupIteratorByID(id);
|
2018-06-22 23:02:47 +02:00
|
|
|
if (group != group_list.end())
|
|
|
|
{
|
2017-06-06 23:24:18 +02:00
|
|
|
group_list.erase(group);
|
|
|
|
}
|
2016-05-27 00:52:40 +02:00
|
|
|
|
2018-06-22 23:02:47 +02:00
|
|
|
if (GetMode() == NETWORK_MODE_SERVER)
|
|
|
|
{
|
2017-06-06 23:24:18 +02:00
|
|
|
_userManager.UnsetUsersOfGroup(id);
|
|
|
|
_userManager.Save();
|
|
|
|
}
|
2016-01-22 07:32:40 +01:00
|
|
|
}
|
|
|
|
|
2018-06-22 23:02:47 +02:00
|
|
|
uint8_t Network::GetGroupIDByHash(const std::string& keyhash)
|
2016-05-19 10:23:42 +02:00
|
|
|
{
|
2018-06-22 23:02:47 +02:00
|
|
|
const NetworkUser* networkUser = _userManager.GetUserByHash(keyhash);
|
2016-05-27 00:52:40 +02:00
|
|
|
|
2018-06-20 17:28:51 +02:00
|
|
|
uint8_t groupId = GetDefaultGroup();
|
2018-06-22 23:02:47 +02:00
|
|
|
if (networkUser != nullptr && networkUser->GroupId.HasValue())
|
|
|
|
{
|
2018-06-20 17:28:51 +02:00
|
|
|
const uint8_t assignedGroup = networkUser->GroupId.GetValue();
|
2018-06-22 23:02:47 +02:00
|
|
|
if (GetGroupByID(assignedGroup) != nullptr)
|
|
|
|
{
|
2017-06-06 23:24:18 +02:00
|
|
|
groupId = assignedGroup;
|
2018-06-22 23:02:47 +02:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
log_warning(
|
2018-07-21 13:51:54 +02:00
|
|
|
"User %s is assigned to non-existent group %u. Assigning to default group (%u)", keyhash.c_str(), assignedGroup,
|
2018-06-22 23:02:47 +02:00
|
|
|
groupId);
|
2017-06-06 23:24:18 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return groupId;
|
2016-05-19 10:23:42 +02:00
|
|
|
}
|
|
|
|
|
2018-06-20 17:28:51 +02:00
|
|
|
uint8_t Network::GetDefaultGroup()
|
2016-01-20 23:45:09 +01:00
|
|
|
{
|
2017-06-06 23:24:18 +02:00
|
|
|
return default_group;
|
2016-01-20 23:45:09 +01:00
|
|
|
}
|
|
|
|
|
2018-06-20 17:28:51 +02:00
|
|
|
void Network::SetDefaultGroup(uint8_t id)
|
2016-01-20 23:45:09 +01:00
|
|
|
{
|
2018-06-22 23:02:47 +02:00
|
|
|
if (GetGroupByID(id))
|
|
|
|
{
|
2017-06-06 23:24:18 +02:00
|
|
|
default_group = id;
|
|
|
|
}
|
2016-01-20 23:45:09 +01:00
|
|
|
}
|
|
|
|
|
2016-01-24 02:05:53 +01:00
|
|
|
void Network::SaveGroups()
|
|
|
|
{
|
2018-06-22 23:02:47 +02:00
|
|
|
if (GetMode() == NETWORK_MODE_SERVER)
|
|
|
|
{
|
2017-06-06 23:24:18 +02:00
|
|
|
utf8 path[MAX_PATH];
|
2016-01-24 02:05:53 +01:00
|
|
|
|
2017-08-15 10:07:44 +02:00
|
|
|
platform_get_user_directory(path, nullptr, sizeof(path));
|
2017-06-06 23:24:18 +02:00
|
|
|
safe_strcat_path(path, "groups.json", sizeof(path));
|
2016-01-24 02:05:53 +01:00
|
|
|
|
2018-06-22 23:02:47 +02:00
|
|
|
json_t* jsonGroupsCfg = json_object();
|
|
|
|
json_t* jsonGroups = json_array();
|
|
|
|
for (auto& group : group_list)
|
|
|
|
{
|
2017-10-03 00:00:32 +02:00
|
|
|
json_array_append_new(jsonGroups, group->ToJson());
|
2017-06-06 23:24:18 +02:00
|
|
|
}
|
|
|
|
json_object_set_new(jsonGroupsCfg, "default_group", json_integer(default_group));
|
|
|
|
json_object_set_new(jsonGroupsCfg, "groups", jsonGroups);
|
|
|
|
try
|
|
|
|
{
|
|
|
|
Json::WriteToFile(path, jsonGroupsCfg, JSON_INDENT(4) | JSON_PRESERVE_ORDER);
|
|
|
|
}
|
2018-06-22 23:02:47 +02:00
|
|
|
catch (const std::exception& ex)
|
2017-06-06 23:24:18 +02:00
|
|
|
{
|
2018-01-02 20:23:22 +01:00
|
|
|
log_error("Unable to save %s: %s", path, ex.what());
|
2017-06-06 23:24:18 +02:00
|
|
|
}
|
2016-01-24 02:05:53 +01:00
|
|
|
|
2017-06-06 23:24:18 +02:00
|
|
|
json_decref(jsonGroupsCfg);
|
|
|
|
}
|
2016-01-24 02:05:53 +01:00
|
|
|
}
|
|
|
|
|
2016-07-04 18:50:09 +02:00
|
|
|
void Network::SetupDefaultGroups()
|
|
|
|
{
|
2019-02-20 22:30:12 +01:00
|
|
|
// Admin group
|
2018-05-12 15:13:48 +02:00
|
|
|
auto admin = std::make_unique<NetworkGroup>();
|
2017-06-06 23:24:18 +02:00
|
|
|
admin->SetName("Admin");
|
|
|
|
admin->ActionsAllowed.fill(0xFF);
|
|
|
|
admin->Id = 0;
|
|
|
|
group_list.push_back(std::move(admin));
|
2019-02-20 22:30:12 +01:00
|
|
|
|
|
|
|
// Spectator group
|
2018-05-12 15:13:48 +02:00
|
|
|
auto spectator = std::make_unique<NetworkGroup>();
|
2017-06-06 23:24:18 +02:00
|
|
|
spectator->SetName("Spectator");
|
2019-02-20 22:30:12 +01:00
|
|
|
spectator->ToggleActionPermission(NETWORK_PERMISSION_CHAT);
|
2017-06-06 23:24:18 +02:00
|
|
|
spectator->Id = 1;
|
|
|
|
group_list.push_back(std::move(spectator));
|
2019-02-20 22:30:12 +01:00
|
|
|
|
|
|
|
// User group
|
2018-05-12 15:13:48 +02:00
|
|
|
auto user = std::make_unique<NetworkGroup>();
|
2017-06-06 23:24:18 +02:00
|
|
|
user->SetName("User");
|
|
|
|
user->ActionsAllowed.fill(0xFF);
|
2019-02-20 22:30:12 +01:00
|
|
|
user->ToggleActionPermission(NETWORK_PERMISSION_KICK_PLAYER);
|
|
|
|
user->ToggleActionPermission(NETWORK_PERMISSION_MODIFY_GROUPS);
|
|
|
|
user->ToggleActionPermission(NETWORK_PERMISSION_SET_PLAYER_GROUP);
|
|
|
|
user->ToggleActionPermission(NETWORK_PERMISSION_CHEAT);
|
|
|
|
user->ToggleActionPermission(NETWORK_PERMISSION_PASSWORDLESS_LOGIN);
|
|
|
|
user->ToggleActionPermission(NETWORK_PERMISSION_MODIFY_TILE);
|
|
|
|
user->ToggleActionPermission(NETWORK_PERMISSION_EDIT_SCENARIO_OPTIONS);
|
2017-06-06 23:24:18 +02:00
|
|
|
user->Id = 2;
|
|
|
|
group_list.push_back(std::move(user));
|
2019-02-20 22:30:12 +01:00
|
|
|
|
2017-06-06 23:24:18 +02:00
|
|
|
SetDefaultGroup(1);
|
2016-07-04 18:50:09 +02:00
|
|
|
}
|
|
|
|
|
2016-01-24 02:05:53 +01:00
|
|
|
void Network::LoadGroups()
|
|
|
|
{
|
2017-06-06 23:24:18 +02:00
|
|
|
group_list.clear();
|
|
|
|
|
|
|
|
utf8 path[MAX_PATH];
|
|
|
|
|
2017-08-15 10:07:44 +02:00
|
|
|
platform_get_user_directory(path, nullptr, sizeof(path));
|
2017-06-06 23:24:18 +02:00
|
|
|
safe_strcat_path(path, "groups.json", sizeof(path));
|
|
|
|
|
2018-06-22 23:02:47 +02:00
|
|
|
json_t* json = nullptr;
|
2020-07-02 17:03:31 +02:00
|
|
|
if (Platform::FileExists(path))
|
2018-06-22 23:02:47 +02:00
|
|
|
{
|
|
|
|
try
|
|
|
|
{
|
2017-06-06 23:24:18 +02:00
|
|
|
json = Json::ReadFromFile(path);
|
2018-06-22 23:02:47 +02:00
|
|
|
}
|
|
|
|
catch (const std::exception& e)
|
|
|
|
{
|
2018-01-02 20:23:22 +01:00
|
|
|
log_error("Failed to read %s as JSON. Setting default groups. %s", path, e.what());
|
2017-06-06 23:24:18 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-06-22 23:02:47 +02:00
|
|
|
if (json == nullptr)
|
|
|
|
{
|
2017-06-06 23:24:18 +02:00
|
|
|
SetupDefaultGroups();
|
2018-06-22 23:02:47 +02:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
json_t* json_groups = json_object_get(json, "groups");
|
2020-04-22 17:09:29 +02:00
|
|
|
size_t groupCount = json_array_size(json_groups);
|
2018-06-22 23:02:47 +02:00
|
|
|
for (size_t i = 0; i < groupCount; i++)
|
|
|
|
{
|
|
|
|
json_t* jsonGroup = json_array_get(json_groups, i);
|
2017-06-06 23:24:18 +02:00
|
|
|
|
2018-05-12 15:13:48 +02:00
|
|
|
auto newgroup = std::make_unique<NetworkGroup>(NetworkGroup::FromJson(jsonGroup));
|
2017-06-06 23:24:18 +02:00
|
|
|
group_list.push_back(std::move(newgroup));
|
|
|
|
}
|
2018-06-22 23:02:47 +02:00
|
|
|
json_t* jsonDefaultGroup = json_object_get(json, "default_group");
|
2020-04-22 17:09:29 +02:00
|
|
|
default_group = static_cast<uint8_t>(json_integer_value(jsonDefaultGroup));
|
2018-06-22 23:02:47 +02:00
|
|
|
if (GetGroupByID(default_group) == nullptr)
|
|
|
|
{
|
2017-06-06 23:24:18 +02:00
|
|
|
default_group = 0;
|
|
|
|
}
|
|
|
|
json_decref(json);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Host group should always contain all permissions.
|
|
|
|
group_list.at(0)->ActionsAllowed.fill(0xFF);
|
2016-05-19 10:23:42 +02:00
|
|
|
}
|
|
|
|
|
2018-06-22 23:02:47 +02:00
|
|
|
std::string Network::BeginLog(const std::string& directory, const std::string& midName, const std::string& filenameFormat)
|
2016-05-30 15:24:06 +02:00
|
|
|
{
|
2017-06-06 23:24:18 +02:00
|
|
|
utf8 filename[256];
|
|
|
|
time_t timer;
|
|
|
|
time(&timer);
|
|
|
|
auto tmInfo = localtime(&timer);
|
2018-06-22 23:02:47 +02:00
|
|
|
if (strftime(filename, sizeof(filename), filenameFormat.c_str(), tmInfo) == 0)
|
|
|
|
{
|
2017-06-06 23:24:18 +02:00
|
|
|
throw std::runtime_error("strftime failed");
|
|
|
|
}
|
2016-05-30 15:24:06 +02:00
|
|
|
|
2018-01-04 00:01:43 +01:00
|
|
|
platform_ensure_directory_exists(Path::Combine(directory, midName).c_str());
|
2017-11-17 00:02:37 +01:00
|
|
|
return Path::Combine(directory, midName, filename);
|
2016-05-30 15:24:06 +02:00
|
|
|
}
|
|
|
|
|
2018-06-22 23:02:47 +02:00
|
|
|
void Network::AppendLog(std::ostream& fs, const std::string& s)
|
2016-05-30 15:24:06 +02:00
|
|
|
{
|
2018-01-04 00:01:43 +01:00
|
|
|
if (fs.fail())
|
|
|
|
{
|
|
|
|
log_error("bad ostream failed to append log");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
try
|
|
|
|
{
|
2019-05-11 23:40:59 +02:00
|
|
|
utf8 buffer[1024];
|
2018-01-04 00:01:43 +01:00
|
|
|
time_t timer;
|
|
|
|
time(&timer);
|
|
|
|
auto tmInfo = localtime(&timer);
|
|
|
|
if (strftime(buffer, sizeof(buffer), "[%Y/%m/%d %H:%M:%S] ", tmInfo) != 0)
|
2017-06-06 23:24:18 +02:00
|
|
|
{
|
2018-01-04 00:01:43 +01:00
|
|
|
String::Append(buffer, sizeof(buffer), s.c_str());
|
|
|
|
utf8_remove_formatting(buffer, false);
|
|
|
|
String::Append(buffer, sizeof(buffer), PLATFORM_NEWLINE);
|
|
|
|
|
|
|
|
fs.write(buffer, strlen(buffer));
|
2017-06-06 23:24:18 +02:00
|
|
|
}
|
|
|
|
}
|
2018-06-22 23:02:47 +02:00
|
|
|
catch (const std::exception& ex)
|
2018-01-04 00:01:43 +01:00
|
|
|
{
|
2018-01-02 20:23:22 +01:00
|
|
|
log_error("%s", ex.what());
|
2018-01-04 00:01:43 +01:00
|
|
|
}
|
2016-05-30 15:24:06 +02:00
|
|
|
}
|
|
|
|
|
2017-06-06 18:55:13 +02:00
|
|
|
void Network::BeginChatLog()
|
2016-08-10 05:21:55 +02:00
|
|
|
{
|
2017-06-06 23:24:18 +02:00
|
|
|
auto directory = _env->GetDirectoryPath(DIRBASE::USER, DIRID::LOG_CHAT);
|
2017-11-17 00:02:37 +01:00
|
|
|
_chatLogPath = BeginLog(directory, "", _chatLogFilenameFormat);
|
2018-01-04 00:01:43 +01:00
|
|
|
|
2018-07-21 16:17:06 +02:00
|
|
|
# if defined(_WIN32) && !defined(__MINGW32__)
|
2019-07-23 00:11:12 +02:00
|
|
|
auto pathW = String::ToWideChar(_chatLogPath.c_str());
|
2019-07-22 23:13:08 +02:00
|
|
|
_chat_log_fs.open(pathW.c_str(), std::ios::out | std::ios::app);
|
2018-07-21 16:17:06 +02:00
|
|
|
# else
|
2018-01-04 00:01:43 +01:00
|
|
|
_chat_log_fs.open(_chatLogPath, std::ios::out | std::ios::app);
|
2018-07-21 16:17:06 +02:00
|
|
|
# endif
|
2016-08-10 05:21:55 +02:00
|
|
|
}
|
|
|
|
|
2018-06-22 23:02:47 +02:00
|
|
|
void Network::AppendChatLog(const std::string& s)
|
2016-08-10 05:21:55 +02:00
|
|
|
{
|
2018-01-04 00:01:43 +01:00
|
|
|
if (gConfigNetwork.log_chat && _chat_log_fs.is_open())
|
|
|
|
{
|
|
|
|
AppendLog(_chat_log_fs, s);
|
2017-06-06 23:24:18 +02:00
|
|
|
}
|
2016-08-10 05:21:55 +02:00
|
|
|
}
|
|
|
|
|
2016-05-30 15:24:06 +02:00
|
|
|
void Network::CloseChatLog()
|
|
|
|
{
|
2018-01-04 00:01:43 +01:00
|
|
|
_chat_log_fs.close();
|
2016-05-30 15:24:06 +02:00
|
|
|
}
|
|
|
|
|
2017-06-06 18:55:13 +02:00
|
|
|
void Network::BeginServerLog()
|
2016-08-10 05:21:55 +02:00
|
|
|
{
|
2017-06-06 23:24:18 +02:00
|
|
|
auto directory = _env->GetDirectoryPath(DIRBASE::USER, DIRID::LOG_SERVER);
|
2017-11-17 00:02:37 +01:00
|
|
|
_serverLogPath = BeginLog(directory, ServerName, _serverLogFilenameFormat);
|
2016-08-10 05:21:55 +02:00
|
|
|
|
2018-07-21 16:17:06 +02:00
|
|
|
# if defined(_WIN32) && !defined(__MINGW32__)
|
2019-07-23 00:11:12 +02:00
|
|
|
auto pathW = String::ToWideChar(_serverLogPath.c_str());
|
2019-07-22 23:13:08 +02:00
|
|
|
_server_log_fs.open(pathW.c_str(), std::ios::out | std::ios::app | std::ios::binary);
|
2018-07-21 16:17:06 +02:00
|
|
|
# else
|
2019-05-11 23:42:23 +02:00
|
|
|
_server_log_fs.open(_serverLogPath, std::ios::out | std::ios::app | std::ios::binary);
|
2018-07-21 16:17:06 +02:00
|
|
|
# endif
|
2018-01-04 00:01:43 +01:00
|
|
|
|
2017-06-06 23:24:18 +02:00
|
|
|
// Log server start event
|
|
|
|
utf8 logMessage[256];
|
2018-06-22 23:02:47 +02:00
|
|
|
if (GetMode() == NETWORK_MODE_CLIENT)
|
|
|
|
{
|
2017-08-15 10:07:44 +02:00
|
|
|
format_string(logMessage, sizeof(logMessage), STR_LOG_CLIENT_STARTED, nullptr);
|
2018-06-22 23:02:47 +02:00
|
|
|
}
|
|
|
|
else if (GetMode() == NETWORK_MODE_SERVER)
|
|
|
|
{
|
2017-08-15 10:07:44 +02:00
|
|
|
format_string(logMessage, sizeof(logMessage), STR_LOG_SERVER_STARTED, nullptr);
|
2017-06-06 23:24:18 +02:00
|
|
|
}
|
2019-10-21 05:32:13 +02:00
|
|
|
else
|
|
|
|
{
|
|
|
|
logMessage[0] = '\0';
|
2019-12-23 00:39:26 +01:00
|
|
|
Guard::Assert(false, "Unknown network mode!");
|
2019-10-21 05:32:13 +02:00
|
|
|
}
|
2017-06-06 23:24:18 +02:00
|
|
|
AppendServerLog(logMessage);
|
2016-08-10 05:21:55 +02:00
|
|
|
}
|
|
|
|
|
2018-06-22 23:02:47 +02:00
|
|
|
void Network::AppendServerLog(const std::string& s)
|
2016-08-10 05:21:55 +02:00
|
|
|
{
|
2018-01-04 00:01:43 +01:00
|
|
|
if (gConfigNetwork.log_server_actions && _server_log_fs.is_open())
|
|
|
|
{
|
|
|
|
AppendLog(_server_log_fs, s);
|
2017-06-06 23:24:18 +02:00
|
|
|
}
|
2016-08-10 05:21:55 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void Network::CloseServerLog()
|
|
|
|
{
|
2017-06-06 23:24:18 +02:00
|
|
|
// Log server stopped event
|
|
|
|
char logMessage[256];
|
2018-06-22 23:02:47 +02:00
|
|
|
if (GetMode() == NETWORK_MODE_CLIENT)
|
|
|
|
{
|
2017-08-15 10:07:44 +02:00
|
|
|
format_string(logMessage, sizeof(logMessage), STR_LOG_CLIENT_STOPPED, nullptr);
|
2018-06-22 23:02:47 +02:00
|
|
|
}
|
|
|
|
else if (GetMode() == NETWORK_MODE_SERVER)
|
|
|
|
{
|
2017-08-15 10:07:44 +02:00
|
|
|
format_string(logMessage, sizeof(logMessage), STR_LOG_SERVER_STOPPED, nullptr);
|
2017-06-06 23:24:18 +02:00
|
|
|
}
|
2019-10-21 05:32:13 +02:00
|
|
|
else
|
|
|
|
{
|
|
|
|
logMessage[0] = '\0';
|
2019-12-23 00:39:26 +01:00
|
|
|
Guard::Assert(false, "Unknown network mode!");
|
2019-10-21 05:32:13 +02:00
|
|
|
}
|
2017-06-06 23:24:18 +02:00
|
|
|
AppendServerLog(logMessage);
|
2018-01-04 00:01:43 +01:00
|
|
|
_server_log_fs.close();
|
2016-08-10 05:21:55 +02:00
|
|
|
}
|
|
|
|
|
2019-05-11 21:31:34 +02:00
|
|
|
void Network::Client_Send_RequestGameState(uint32_t tick)
|
|
|
|
{
|
|
|
|
if (_serverState.gamestateSnapshotsEnabled == false)
|
|
|
|
{
|
|
|
|
log_verbose("Server does not store a gamestate history");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
log_verbose("Requesting gamestate from server for tick %u", tick);
|
|
|
|
std::unique_ptr<NetworkPacket> packet(NetworkPacket::Allocate());
|
2020-04-22 17:09:29 +02:00
|
|
|
*packet << static_cast<uint32_t>(NETWORK_COMMAND_REQUEST_GAMESTATE) << tick;
|
2019-05-11 21:31:34 +02:00
|
|
|
_serverConnection->QueuePacket(std::move(packet));
|
|
|
|
}
|
|
|
|
|
2016-05-19 10:23:42 +02:00
|
|
|
void Network::Client_Send_TOKEN()
|
|
|
|
{
|
2017-06-06 23:24:18 +02:00
|
|
|
log_verbose("requesting token");
|
|
|
|
std::unique_ptr<NetworkPacket> packet(NetworkPacket::Allocate());
|
2020-04-22 17:09:29 +02:00
|
|
|
*packet << static_cast<uint32_t>(NETWORK_COMMAND_TOKEN);
|
2018-12-17 12:58:12 +01:00
|
|
|
_serverConnection->AuthStatus = NETWORK_AUTH_REQUESTED;
|
|
|
|
_serverConnection->QueuePacket(std::move(packet));
|
2016-05-19 10:23:42 +02:00
|
|
|
}
|
|
|
|
|
2019-02-23 15:54:17 +01:00
|
|
|
void Network::Client_Send_AUTH(
|
2019-03-09 13:58:23 +01:00
|
|
|
const std::string& name, const std::string& password, const std::string& pubkey, const std::vector<uint8_t>& signature)
|
2015-07-10 21:53:41 +02:00
|
|
|
{
|
2017-06-06 23:24:18 +02:00
|
|
|
std::unique_ptr<NetworkPacket> packet(NetworkPacket::Allocate());
|
2020-04-22 17:09:29 +02:00
|
|
|
*packet << static_cast<uint32_t>(NETWORK_COMMAND_AUTH);
|
2018-02-20 21:06:35 +01:00
|
|
|
packet->WriteString(network_get_version().c_str());
|
2019-02-23 15:54:17 +01:00
|
|
|
packet->WriteString(name.c_str());
|
|
|
|
packet->WriteString(password.c_str());
|
|
|
|
packet->WriteString(pubkey.c_str());
|
2020-06-07 23:18:11 +02:00
|
|
|
assert(signature.size() <= static_cast<size_t>(UINT32_MAX));
|
2020-04-22 17:09:29 +02:00
|
|
|
*packet << static_cast<uint32_t>(signature.size());
|
2019-03-09 13:58:23 +01:00
|
|
|
packet->Write(signature.data(), signature.size());
|
2018-12-17 12:58:12 +01:00
|
|
|
_serverConnection->AuthStatus = NETWORK_AUTH_REQUESTED;
|
|
|
|
_serverConnection->QueuePacket(std::move(packet));
|
2015-07-15 08:23:04 +02:00
|
|
|
}
|
|
|
|
|
2020-07-24 23:00:07 +02:00
|
|
|
void Network::Client_Send_REQUESTMAP(const std::vector<std::string>& objects)
|
2016-10-14 07:29:45 +02:00
|
|
|
{
|
2018-06-20 17:28:51 +02:00
|
|
|
log_verbose("client requests %u objects", uint32_t(objects.size()));
|
2017-06-06 23:24:18 +02:00
|
|
|
std::unique_ptr<NetworkPacket> packet(NetworkPacket::Allocate());
|
2020-07-24 20:59:22 +02:00
|
|
|
*packet << static_cast<uint32_t>(NETWORK_COMMAND_MAP_REQUEST) << static_cast<uint32_t>(objects.size());
|
2018-06-22 23:02:47 +02:00
|
|
|
for (const auto& object : objects)
|
2017-06-06 23:24:18 +02:00
|
|
|
{
|
2017-10-03 00:00:32 +02:00
|
|
|
log_verbose("client requests object %s", object.c_str());
|
2020-04-22 17:09:29 +02:00
|
|
|
packet->Write(reinterpret_cast<const uint8_t*>(object.c_str()), 8);
|
2017-06-06 23:24:18 +02:00
|
|
|
}
|
2018-12-17 12:58:12 +01:00
|
|
|
_serverConnection->QueuePacket(std::move(packet));
|
2016-10-14 07:29:45 +02:00
|
|
|
}
|
|
|
|
|
2016-05-19 10:23:42 +02:00
|
|
|
void Network::Server_Send_TOKEN(NetworkConnection& connection)
|
|
|
|
{
|
2017-06-06 23:24:18 +02:00
|
|
|
std::unique_ptr<NetworkPacket> packet(NetworkPacket::Allocate());
|
2020-04-22 17:09:29 +02:00
|
|
|
*packet << static_cast<uint32_t>(NETWORK_COMMAND_TOKEN) << static_cast<uint32_t>(connection.Challenge.size());
|
2017-06-06 23:24:18 +02:00
|
|
|
packet->Write(connection.Challenge.data(), connection.Challenge.size());
|
|
|
|
connection.QueuePacket(std::move(packet));
|
2016-05-19 10:23:42 +02:00
|
|
|
}
|
|
|
|
|
2020-07-24 22:36:45 +02:00
|
|
|
void Network::Server_Send_OBJECTS_LIST(NetworkConnection& connection, const std::vector<const ObjectRepositoryItem*>& objects) const
|
2016-10-14 07:29:45 +02:00
|
|
|
{
|
2017-06-06 23:24:18 +02:00
|
|
|
log_verbose("Server sends objects list with %u items", objects.size());
|
2020-07-24 22:36:45 +02:00
|
|
|
|
|
|
|
for (size_t i = 0; i < objects.size(); ++i)
|
2018-06-22 23:02:47 +02:00
|
|
|
{
|
2020-07-24 22:36:45 +02:00
|
|
|
const auto* object = objects[i];
|
|
|
|
|
|
|
|
std::unique_ptr<NetworkPacket> packet(NetworkPacket::Allocate());
|
|
|
|
*packet << static_cast<uint32_t>(NETWORK_COMMAND_OBJECTS_LIST) << static_cast<uint32_t>(i)
|
|
|
|
<< static_cast<uint32_t>(objects.size());
|
|
|
|
|
2017-10-03 00:00:32 +02:00
|
|
|
log_verbose("Object %.8s (checksum %x)", object->ObjectEntry.name, object->ObjectEntry.checksum);
|
2020-04-22 17:09:29 +02:00
|
|
|
packet->Write(reinterpret_cast<const uint8_t*>(object->ObjectEntry.name), 8);
|
2017-10-03 00:00:32 +02:00
|
|
|
*packet << object->ObjectEntry.checksum << object->ObjectEntry.flags;
|
2020-07-24 22:36:45 +02:00
|
|
|
|
|
|
|
connection.QueuePacket(std::move(packet));
|
2017-06-06 23:24:18 +02:00
|
|
|
}
|
2016-10-14 07:29:45 +02:00
|
|
|
}
|
|
|
|
|
2020-02-22 23:18:22 +01:00
|
|
|
void Network::Server_Send_SCRIPTS(NetworkConnection& connection) const
|
|
|
|
{
|
2020-02-23 13:55:48 +01:00
|
|
|
std::unique_ptr<NetworkPacket> packet(NetworkPacket::Allocate());
|
2020-04-22 23:53:32 +02:00
|
|
|
*packet << static_cast<uint32_t>(NETWORK_COMMAND_SCRIPTS);
|
2020-04-24 16:58:01 +02:00
|
|
|
# ifdef ENABLE_SCRIPTING
|
2020-02-22 23:18:22 +01:00
|
|
|
using namespace OpenRCT2::Scripting;
|
|
|
|
|
|
|
|
auto& scriptEngine = GetContext()->GetScriptEngine();
|
|
|
|
const auto& plugins = scriptEngine.GetPlugins();
|
|
|
|
std::vector<std::shared_ptr<Plugin>> pluginsToSend;
|
|
|
|
for (const auto& plugin : plugins)
|
|
|
|
{
|
|
|
|
const auto& metadata = plugin->GetMetadata();
|
2020-02-24 18:40:36 +01:00
|
|
|
if (metadata.Type == OpenRCT2::Scripting::PluginType::Remote)
|
2020-02-22 23:18:22 +01:00
|
|
|
{
|
|
|
|
pluginsToSend.push_back(plugin);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
log_verbose("Server sends %u scripts", pluginsToSend.size());
|
2020-04-22 23:53:32 +02:00
|
|
|
*packet << static_cast<uint32_t>(pluginsToSend.size());
|
2020-02-22 23:18:22 +01:00
|
|
|
for (const auto& plugin : pluginsToSend)
|
|
|
|
{
|
|
|
|
const auto& metadata = plugin->GetMetadata();
|
|
|
|
log_verbose("Script %s", metadata.Name.c_str());
|
|
|
|
|
|
|
|
const auto& code = plugin->GetCode();
|
2020-04-22 23:53:32 +02:00
|
|
|
*packet << static_cast<uint32_t>(code.size());
|
|
|
|
packet->Write(reinterpret_cast<const uint8_t*>(code.c_str()), code.size());
|
2020-02-22 23:18:22 +01:00
|
|
|
}
|
2020-02-23 13:55:48 +01:00
|
|
|
# else
|
2020-04-22 23:53:32 +02:00
|
|
|
*packet << static_cast<uint32_t>(0);
|
2020-02-23 13:55:48 +01:00
|
|
|
# endif
|
2020-02-22 23:18:22 +01:00
|
|
|
connection.QueuePacket(std::move(packet));
|
|
|
|
}
|
|
|
|
|
2020-07-24 22:49:05 +02:00
|
|
|
void Network::Client_Send_HEARTBEAT(NetworkConnection& connection) const
|
|
|
|
{
|
|
|
|
log_verbose("Sending heartbeat");
|
|
|
|
|
|
|
|
std::unique_ptr<NetworkPacket> packet(NetworkPacket::Allocate());
|
|
|
|
*packet << static_cast<uint32_t>(NETWORK_COMMAND_HEARTBEAT);
|
|
|
|
|
|
|
|
connection.QueuePacket(std::move(packet));
|
|
|
|
}
|
|
|
|
|
2019-01-29 16:35:19 +01:00
|
|
|
NetworkStats_t Network::GetStats() const
|
|
|
|
{
|
|
|
|
NetworkStats_t stats = {};
|
|
|
|
if (mode == NETWORK_MODE_CLIENT)
|
|
|
|
{
|
|
|
|
stats = _serverConnection->Stats;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
for (auto& connection : client_connection_list)
|
|
|
|
{
|
|
|
|
for (size_t n = 0; n < NETWORK_STATISTICS_GROUP_MAX; n++)
|
|
|
|
{
|
|
|
|
stats.bytesReceived[n] += connection->Stats.bytesReceived[n];
|
|
|
|
stats.bytesSent[n] += connection->Stats.bytesSent[n];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return stats;
|
|
|
|
}
|
|
|
|
|
2015-11-02 04:12:14 +01:00
|
|
|
void Network::Server_Send_AUTH(NetworkConnection& connection)
|
|
|
|
{
|
2018-06-20 17:28:51 +02:00
|
|
|
uint8_t new_playerid = 0;
|
2018-06-22 23:02:47 +02:00
|
|
|
if (connection.Player)
|
|
|
|
{
|
2017-06-06 23:24:18 +02:00
|
|
|
new_playerid = connection.Player->Id;
|
|
|
|
}
|
|
|
|
std::unique_ptr<NetworkPacket> packet(NetworkPacket::Allocate());
|
2020-04-22 17:09:29 +02:00
|
|
|
*packet << static_cast<uint32_t>(NETWORK_COMMAND_AUTH) << static_cast<uint32_t>(connection.AuthStatus) << new_playerid;
|
2018-06-22 23:02:47 +02:00
|
|
|
if (connection.AuthStatus == NETWORK_AUTH_BADVERSION)
|
|
|
|
{
|
2018-02-20 21:06:35 +01:00
|
|
|
packet->WriteString(network_get_version().c_str());
|
2017-06-06 23:24:18 +02:00
|
|
|
}
|
|
|
|
connection.QueuePacket(std::move(packet));
|
2018-06-22 23:02:47 +02:00
|
|
|
if (connection.AuthStatus != NETWORK_AUTH_OK && connection.AuthStatus != NETWORK_AUTH_REQUIREPASSWORD)
|
|
|
|
{
|
2017-06-06 23:24:18 +02:00
|
|
|
connection.Socket->Disconnect();
|
|
|
|
}
|
2015-11-02 04:12:14 +01:00
|
|
|
}
|
|
|
|
|
2015-07-31 16:14:44 +02:00
|
|
|
void Network::Server_Send_MAP(NetworkConnection* connection)
|
2015-07-15 08:23:04 +02:00
|
|
|
{
|
2018-06-22 23:02:47 +02:00
|
|
|
std::vector<const ObjectRepositoryItem*> objects;
|
|
|
|
if (connection)
|
|
|
|
{
|
2017-06-06 23:24:18 +02:00
|
|
|
objects = connection->RequestedObjects;
|
2018-06-22 23:02:47 +02:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2017-06-06 23:24:18 +02:00
|
|
|
// This will send all custom objects to connected clients
|
|
|
|
// TODO: fix it so custom objects negotiation is performed even in this case.
|
2018-04-10 14:19:23 +02:00
|
|
|
auto context = GetContext();
|
2018-08-29 00:52:10 +02:00
|
|
|
auto& objManager = context->GetObjectManager();
|
|
|
|
objects = objManager.GetPackableObjects();
|
2017-06-06 23:24:18 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
size_t out_size;
|
2018-06-22 23:02:47 +02:00
|
|
|
uint8_t* header = save_for_network(out_size, objects);
|
|
|
|
if (header == nullptr)
|
|
|
|
{
|
|
|
|
if (connection)
|
|
|
|
{
|
2017-06-06 23:24:18 +02:00
|
|
|
connection->SetLastDisconnectReason(STR_MULTIPLAYER_CONNECTION_CLOSED);
|
|
|
|
connection->Socket->Disconnect();
|
|
|
|
}
|
|
|
|
return;
|
|
|
|
}
|
2020-07-24 22:49:05 +02:00
|
|
|
size_t chunksize = CHUNK_SIZE;
|
2018-06-22 23:02:47 +02:00
|
|
|
for (size_t i = 0; i < out_size; i += chunksize)
|
|
|
|
{
|
2018-06-20 17:11:35 +02:00
|
|
|
size_t datasize = std::min(chunksize, out_size - i);
|
2017-06-06 23:24:18 +02:00
|
|
|
std::unique_ptr<NetworkPacket> packet(NetworkPacket::Allocate());
|
2020-04-22 17:09:29 +02:00
|
|
|
*packet << static_cast<uint32_t>(NETWORK_COMMAND_MAP) << static_cast<uint32_t>(out_size) << static_cast<uint32_t>(i);
|
2017-06-06 23:24:18 +02:00
|
|
|
packet->Write(&header[i], datasize);
|
2018-06-22 23:02:47 +02:00
|
|
|
if (connection)
|
|
|
|
{
|
2017-06-06 23:24:18 +02:00
|
|
|
connection->QueuePacket(std::move(packet));
|
2018-06-22 23:02:47 +02:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2017-06-06 23:24:18 +02:00
|
|
|
SendPacketToClients(*packet);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
free(header);
|
2016-10-13 14:57:40 +02:00
|
|
|
}
|
|
|
|
|
2018-06-22 23:02:47 +02:00
|
|
|
uint8_t* Network::save_for_network(size_t& out_size, const std::vector<const ObjectRepositoryItem*>& objects) const
|
2016-10-13 14:57:40 +02:00
|
|
|
{
|
2018-06-22 23:02:47 +02:00
|
|
|
uint8_t* header = nullptr;
|
2017-06-06 23:24:18 +02:00
|
|
|
out_size = 0;
|
|
|
|
bool RLEState = gUseRLE;
|
|
|
|
gUseRLE = false;
|
|
|
|
|
|
|
|
auto ms = MemoryStream();
|
2018-06-22 23:02:47 +02:00
|
|
|
if (!SaveMap(&ms, objects))
|
|
|
|
{
|
2017-06-06 23:24:18 +02:00
|
|
|
log_warning("Failed to export map.");
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
gUseRLE = RLEState;
|
|
|
|
|
2018-06-22 23:02:47 +02:00
|
|
|
const void* data = ms.GetData();
|
2018-06-20 17:28:51 +02:00
|
|
|
int32_t size = ms.GetLength();
|
2017-06-06 23:24:18 +02:00
|
|
|
|
2020-07-04 14:21:50 +02:00
|
|
|
auto compressed = util_zlib_deflate(static_cast<const uint8_t*>(data), size);
|
|
|
|
if (compressed != std::nullopt)
|
2017-06-06 23:24:18 +02:00
|
|
|
{
|
2020-07-04 14:21:50 +02:00
|
|
|
out_size = compressed->size();
|
2020-04-22 17:09:29 +02:00
|
|
|
header = reinterpret_cast<uint8_t*>(_strdup("open2_sv6_zlib"));
|
|
|
|
size_t header_len = strlen(reinterpret_cast<char*>(header)) + 1; // account for null terminator
|
|
|
|
header = static_cast<uint8_t*>(realloc(header, header_len + out_size));
|
2018-06-22 23:02:47 +02:00
|
|
|
if (header == nullptr)
|
|
|
|
{
|
2017-06-06 23:24:18 +02:00
|
|
|
log_error("Failed to allocate %u bytes.", header_len + out_size);
|
2018-06-22 23:02:47 +02:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2020-07-04 14:21:50 +02:00
|
|
|
std::memcpy(&header[header_len], compressed->data(), out_size);
|
2017-06-06 23:24:18 +02:00
|
|
|
out_size += header_len;
|
|
|
|
log_verbose("Sending map of size %u bytes, compressed to %u bytes", size, out_size);
|
|
|
|
}
|
2018-06-22 23:02:47 +02:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2017-06-06 23:24:18 +02:00
|
|
|
log_warning("Failed to compress the data, falling back to non-compressed sv6.");
|
2020-04-22 17:09:29 +02:00
|
|
|
header = static_cast<uint8_t*>(malloc(size));
|
2018-06-22 23:02:47 +02:00
|
|
|
if (header == nullptr)
|
|
|
|
{
|
2017-06-06 23:24:18 +02:00
|
|
|
log_error("Failed to allocate %u bytes.", size);
|
2018-06-22 23:02:47 +02:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2017-06-06 23:24:18 +02:00
|
|
|
out_size = size;
|
2018-12-15 22:23:31 +01:00
|
|
|
std::memcpy(header, data, size);
|
2017-06-06 23:24:18 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return header;
|
2015-07-10 21:53:41 +02:00
|
|
|
}
|
|
|
|
|
2015-07-15 08:23:04 +02:00
|
|
|
void Network::Client_Send_CHAT(const char* text)
|
2015-07-10 21:53:41 +02:00
|
|
|
{
|
2017-06-06 23:24:18 +02:00
|
|
|
std::unique_ptr<NetworkPacket> packet(NetworkPacket::Allocate());
|
2020-04-22 17:09:29 +02:00
|
|
|
*packet << static_cast<uint32_t>(NETWORK_COMMAND_CHAT);
|
2017-06-06 23:24:18 +02:00
|
|
|
packet->WriteString(text);
|
2018-12-17 12:58:12 +01:00
|
|
|
_serverConnection->QueuePacket(std::move(packet));
|
2015-07-15 08:23:04 +02:00
|
|
|
}
|
|
|
|
|
2020-04-28 23:02:14 +02:00
|
|
|
void Network::Server_Send_CHAT(const char* text, const std::vector<uint8_t>& playerIds)
|
2015-07-15 08:23:04 +02:00
|
|
|
{
|
2017-06-06 23:24:18 +02:00
|
|
|
std::unique_ptr<NetworkPacket> packet(NetworkPacket::Allocate());
|
2020-04-22 17:09:29 +02:00
|
|
|
*packet << static_cast<uint32_t>(NETWORK_COMMAND_CHAT);
|
2017-06-06 23:24:18 +02:00
|
|
|
packet->WriteString(text);
|
2020-04-28 23:02:14 +02:00
|
|
|
|
|
|
|
if (playerIds.empty())
|
|
|
|
{
|
2020-04-30 03:18:32 +02:00
|
|
|
// Empty players / default value means send to all players
|
2020-04-28 23:02:14 +02:00
|
|
|
SendPacketToClients(*packet);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
for (auto playerId : playerIds)
|
|
|
|
{
|
|
|
|
auto conn = GetPlayerConnection(playerId);
|
|
|
|
if (conn != nullptr && !conn->IsDisconnected)
|
|
|
|
{
|
|
|
|
conn->QueuePacket(NetworkPacket::Duplicate(*packet));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2015-07-10 21:53:41 +02:00
|
|
|
}
|
|
|
|
|
2018-06-22 23:02:47 +02:00
|
|
|
void Network::Client_Send_GAME_ACTION(const GameAction* action)
|
2017-03-21 20:05:53 +01:00
|
|
|
{
|
2017-07-21 19:21:18 +02:00
|
|
|
std::unique_ptr<NetworkPacket> packet(NetworkPacket::Allocate());
|
2017-07-15 11:24:08 +02:00
|
|
|
|
2017-07-17 21:27:21 +02:00
|
|
|
uint32_t networkId = 0;
|
|
|
|
networkId = ++_actionId;
|
|
|
|
|
|
|
|
// I know its ugly, want basic functionality for now.
|
2017-07-21 19:54:05 +02:00
|
|
|
const_cast<GameAction*>(action)->SetNetworkId(networkId);
|
2018-06-22 23:02:47 +02:00
|
|
|
if (action->GetCallback())
|
2017-07-17 21:27:21 +02:00
|
|
|
{
|
|
|
|
_gameActionCallbacks.insert(std::make_pair(networkId, action->GetCallback()));
|
|
|
|
}
|
|
|
|
|
2017-07-15 11:24:08 +02:00
|
|
|
DataSerialiser stream(true);
|
|
|
|
action->Serialise(stream);
|
|
|
|
|
2020-04-22 17:09:29 +02:00
|
|
|
*packet << static_cast<uint32_t>(NETWORK_COMMAND_GAME_ACTION) << gCurrentTicks << action->GetType() << stream;
|
2018-12-17 12:58:12 +01:00
|
|
|
_serverConnection->QueuePacket(std::move(packet));
|
2017-03-21 20:05:53 +01:00
|
|
|
}
|
|
|
|
|
2018-06-22 23:02:47 +02:00
|
|
|
void Network::Server_Send_GAME_ACTION(const GameAction* action)
|
2017-03-21 20:05:53 +01:00
|
|
|
{
|
2017-07-21 19:21:18 +02:00
|
|
|
std::unique_ptr<NetworkPacket> packet(NetworkPacket::Allocate());
|
2017-07-15 11:24:08 +02:00
|
|
|
|
|
|
|
DataSerialiser stream(true);
|
|
|
|
action->Serialise(stream);
|
|
|
|
|
2020-04-22 17:09:29 +02:00
|
|
|
*packet << static_cast<uint32_t>(NETWORK_COMMAND_GAME_ACTION) << gCurrentTicks << action->GetType() << stream;
|
2017-07-15 11:24:08 +02:00
|
|
|
|
2017-07-21 19:21:18 +02:00
|
|
|
SendPacketToClients(*packet);
|
2017-03-21 20:05:53 +01:00
|
|
|
}
|
|
|
|
|
2015-07-15 08:23:04 +02:00
|
|
|
void Network::Server_Send_TICK()
|
2015-07-15 06:40:22 +02:00
|
|
|
{
|
2017-06-06 23:24:18 +02:00
|
|
|
std::unique_ptr<NetworkPacket> packet(NetworkPacket::Allocate());
|
2020-04-22 17:09:29 +02:00
|
|
|
*packet << static_cast<uint32_t>(NETWORK_COMMAND_TICK) << gCurrentTicks << scenario_rand_state().s0;
|
2018-06-20 17:28:51 +02:00
|
|
|
uint32_t flags = 0;
|
2017-06-06 23:24:18 +02:00
|
|
|
// Simple counter which limits how often a sprite checksum gets sent.
|
|
|
|
// This can get somewhat expensive, so we don't want to push it every tick in release,
|
|
|
|
// but debug version can check more often.
|
2018-06-20 17:28:51 +02:00
|
|
|
static int32_t checksum_counter = 0;
|
2017-06-06 23:24:18 +02:00
|
|
|
checksum_counter++;
|
2018-06-22 23:02:47 +02:00
|
|
|
if (checksum_counter >= 100)
|
|
|
|
{
|
2017-06-06 23:24:18 +02:00
|
|
|
checksum_counter = 0;
|
|
|
|
flags |= NETWORK_TICK_FLAG_CHECKSUMS;
|
|
|
|
}
|
|
|
|
// Send flags always, so we can understand packet structure on the other end,
|
|
|
|
// and allow for some expansion.
|
|
|
|
*packet << flags;
|
2018-06-22 23:02:47 +02:00
|
|
|
if (flags & NETWORK_TICK_FLAG_CHECKSUMS)
|
|
|
|
{
|
2018-12-11 03:05:57 +01:00
|
|
|
rct_sprite_checksum checksum = sprite_checksum();
|
|
|
|
packet->WriteString(checksum.ToString().c_str());
|
2017-06-06 23:24:18 +02:00
|
|
|
}
|
2019-01-29 16:35:19 +01:00
|
|
|
|
2017-06-06 23:24:18 +02:00
|
|
|
SendPacketToClients(*packet);
|
2015-07-16 19:32:43 +02:00
|
|
|
}
|
|
|
|
|
2019-03-24 23:16:57 +01:00
|
|
|
void Network::Server_Send_PLAYERINFO(int32_t playerId)
|
|
|
|
{
|
|
|
|
std::unique_ptr<NetworkPacket> packet(NetworkPacket::Allocate());
|
2020-04-22 17:09:29 +02:00
|
|
|
*packet << static_cast<uint32_t>(NETWORK_COMMAND_PLAYERINFO) << gCurrentTicks;
|
2019-03-24 23:16:57 +01:00
|
|
|
|
|
|
|
auto* player = GetPlayerByID(playerId);
|
|
|
|
if (player == nullptr)
|
|
|
|
return;
|
|
|
|
|
|
|
|
player->Write(*packet);
|
|
|
|
SendPacketToClients(*packet);
|
|
|
|
}
|
|
|
|
|
2015-07-16 19:32:43 +02:00
|
|
|
void Network::Server_Send_PLAYERLIST()
|
|
|
|
{
|
2017-06-06 23:24:18 +02:00
|
|
|
std::unique_ptr<NetworkPacket> packet(NetworkPacket::Allocate());
|
2020-04-22 17:09:29 +02:00
|
|
|
*packet << static_cast<uint32_t>(NETWORK_COMMAND_PLAYERLIST) << gCurrentTicks << static_cast<uint8_t>(player_list.size());
|
2018-06-22 23:02:47 +02:00
|
|
|
for (auto& player : player_list)
|
|
|
|
{
|
2017-10-03 00:00:32 +02:00
|
|
|
player->Write(*packet);
|
2017-06-06 23:24:18 +02:00
|
|
|
}
|
|
|
|
SendPacketToClients(*packet);
|
2015-07-15 06:40:22 +02:00
|
|
|
}
|
|
|
|
|
2015-07-17 02:24:39 +02:00
|
|
|
void Network::Client_Send_PING()
|
|
|
|
{
|
2017-06-06 23:24:18 +02:00
|
|
|
std::unique_ptr<NetworkPacket> packet(NetworkPacket::Allocate());
|
2020-04-22 17:09:29 +02:00
|
|
|
*packet << static_cast<uint32_t>(NETWORK_COMMAND_PING);
|
2018-12-17 12:58:12 +01:00
|
|
|
_serverConnection->QueuePacket(std::move(packet));
|
2015-07-17 02:24:39 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void Network::Server_Send_PING()
|
|
|
|
{
|
2017-06-06 23:24:18 +02:00
|
|
|
last_ping_sent_time = platform_get_ticks();
|
|
|
|
std::unique_ptr<NetworkPacket> packet(NetworkPacket::Allocate());
|
2020-04-22 17:09:29 +02:00
|
|
|
*packet << static_cast<uint32_t>(NETWORK_COMMAND_PING);
|
2018-06-22 23:02:47 +02:00
|
|
|
for (auto& client_connection : client_connection_list)
|
|
|
|
{
|
2017-10-03 00:00:32 +02:00
|
|
|
client_connection->PingTime = platform_get_ticks();
|
2017-06-06 23:24:18 +02:00
|
|
|
}
|
|
|
|
SendPacketToClients(*packet, true);
|
2015-07-17 02:24:39 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void Network::Server_Send_PINGLIST()
|
|
|
|
{
|
2017-06-06 23:24:18 +02:00
|
|
|
std::unique_ptr<NetworkPacket> packet(NetworkPacket::Allocate());
|
2020-04-22 17:09:29 +02:00
|
|
|
*packet << static_cast<uint32_t>(NETWORK_COMMAND_PINGLIST) << static_cast<uint8_t>(player_list.size());
|
2018-06-22 23:02:47 +02:00
|
|
|
for (auto& player : player_list)
|
|
|
|
{
|
2017-10-03 00:00:32 +02:00
|
|
|
*packet << player->Id << player->Ping;
|
2017-06-06 23:24:18 +02:00
|
|
|
}
|
|
|
|
SendPacketToClients(*packet);
|
2015-07-17 02:24:39 +02:00
|
|
|
}
|
|
|
|
|
2015-11-01 08:50:44 +01:00
|
|
|
void Network::Server_Send_SETDISCONNECTMSG(NetworkConnection& connection, const char* msg)
|
|
|
|
{
|
2017-06-06 23:24:18 +02:00
|
|
|
std::unique_ptr<NetworkPacket> packet(NetworkPacket::Allocate());
|
2020-04-22 17:09:29 +02:00
|
|
|
*packet << static_cast<uint32_t>(NETWORK_COMMAND_SETDISCONNECTMSG);
|
2017-06-06 23:24:18 +02:00
|
|
|
packet->WriteString(msg);
|
|
|
|
connection.QueuePacket(std::move(packet));
|
2015-11-01 08:50:44 +01:00
|
|
|
}
|
|
|
|
|
2019-05-05 04:18:50 +02:00
|
|
|
json_t* Network::GetServerInfoAsJson() const
|
2015-11-04 00:31:09 +01:00
|
|
|
{
|
2017-06-06 23:24:18 +02:00
|
|
|
json_t* obj = json_object();
|
2019-02-23 15:54:17 +01:00
|
|
|
json_object_set_new(obj, "name", json_string(gConfigNetwork.server_name.c_str()));
|
2017-06-06 23:24:18 +02:00
|
|
|
json_object_set_new(obj, "requiresPassword", json_boolean(_password.size() > 0));
|
2018-02-20 21:06:35 +01:00
|
|
|
json_object_set_new(obj, "version", json_string(network_get_version().c_str()));
|
2017-06-06 23:24:18 +02:00
|
|
|
json_object_set_new(obj, "players", json_integer(player_list.size()));
|
|
|
|
json_object_set_new(obj, "maxPlayers", json_integer(gConfigNetwork.maxplayers));
|
2019-02-23 15:54:17 +01:00
|
|
|
json_object_set_new(obj, "description", json_string(gConfigNetwork.server_description.c_str()));
|
|
|
|
json_object_set_new(obj, "greeting", json_string(gConfigNetwork.server_greeting.c_str()));
|
2017-06-06 23:24:18 +02:00
|
|
|
json_object_set_new(obj, "dedicated", json_boolean(gOpenRCT2Headless));
|
2019-05-05 04:18:50 +02:00
|
|
|
return obj;
|
|
|
|
}
|
|
|
|
|
|
|
|
void Network::Server_Send_GAMEINFO(NetworkConnection& connection)
|
|
|
|
{
|
|
|
|
std::unique_ptr<NetworkPacket> packet(NetworkPacket::Allocate());
|
2020-04-22 17:09:29 +02:00
|
|
|
*packet << static_cast<uint32_t>(NETWORK_COMMAND_GAMEINFO);
|
2019-05-05 04:18:50 +02:00
|
|
|
# ifndef DISABLE_HTTP
|
|
|
|
json_t* obj = GetServerInfoAsJson();
|
2017-06-06 23:24:18 +02:00
|
|
|
|
|
|
|
// Provider details
|
|
|
|
json_t* jsonProvider = json_object();
|
2019-02-23 15:54:17 +01:00
|
|
|
json_object_set_new(jsonProvider, "name", json_string(gConfigNetwork.provider_name.c_str()));
|
|
|
|
json_object_set_new(jsonProvider, "email", json_string(gConfigNetwork.provider_email.c_str()));
|
|
|
|
json_object_set_new(jsonProvider, "website", json_string(gConfigNetwork.provider_website.c_str()));
|
2017-06-06 23:24:18 +02:00
|
|
|
json_object_set_new(obj, "provider", jsonProvider);
|
|
|
|
|
|
|
|
packet->WriteString(json_dumps(obj, 0));
|
2019-05-11 21:31:34 +02:00
|
|
|
*packet << _serverState.gamestateSnapshotsEnabled;
|
|
|
|
|
2017-06-06 23:24:18 +02:00
|
|
|
json_decref(obj);
|
2018-07-21 16:17:06 +02:00
|
|
|
# endif
|
2017-06-06 23:24:18 +02:00
|
|
|
connection.QueuePacket(std::move(packet));
|
2015-11-04 00:31:09 +01:00
|
|
|
}
|
|
|
|
|
2016-01-20 23:45:09 +01:00
|
|
|
void Network::Server_Send_SHOWERROR(NetworkConnection& connection, rct_string_id title, rct_string_id message)
|
|
|
|
{
|
2017-06-06 23:24:18 +02:00
|
|
|
std::unique_ptr<NetworkPacket> packet(NetworkPacket::Allocate());
|
2020-04-22 17:09:29 +02:00
|
|
|
*packet << static_cast<uint32_t>(NETWORK_COMMAND_SHOWERROR) << title << message;
|
2017-06-06 23:24:18 +02:00
|
|
|
connection.QueuePacket(std::move(packet));
|
2016-01-20 23:45:09 +01:00
|
|
|
}
|
|
|
|
|
2016-01-22 07:32:40 +01:00
|
|
|
void Network::Server_Send_GROUPLIST(NetworkConnection& connection)
|
2016-01-22 04:32:51 +01:00
|
|
|
{
|
2017-06-06 23:24:18 +02:00
|
|
|
std::unique_ptr<NetworkPacket> packet(NetworkPacket::Allocate());
|
2020-04-22 17:09:29 +02:00
|
|
|
*packet << static_cast<uint32_t>(NETWORK_COMMAND_GROUPLIST) << static_cast<uint8_t>(group_list.size()) << default_group;
|
2018-06-22 23:02:47 +02:00
|
|
|
for (auto& group : group_list)
|
|
|
|
{
|
2017-10-03 00:00:32 +02:00
|
|
|
group->Write(*packet);
|
2017-06-06 23:24:18 +02:00
|
|
|
}
|
|
|
|
connection.QueuePacket(std::move(packet));
|
2016-01-22 04:32:51 +01:00
|
|
|
}
|
|
|
|
|
2018-06-22 23:02:47 +02:00
|
|
|
void Network::Server_Send_EVENT_PLAYER_JOINED(const char* playerName)
|
2016-02-27 14:12:04 +01:00
|
|
|
{
|
2017-06-06 23:24:18 +02:00
|
|
|
std::unique_ptr<NetworkPacket> packet(NetworkPacket::Allocate());
|
2020-04-22 17:09:29 +02:00
|
|
|
*packet << static_cast<uint32_t>(NETWORK_COMMAND_EVENT);
|
|
|
|
*packet << static_cast<uint16_t>(SERVER_EVENT_PLAYER_JOINED);
|
2017-06-06 23:24:18 +02:00
|
|
|
packet->WriteString(playerName);
|
|
|
|
SendPacketToClients(*packet);
|
2016-02-27 14:12:04 +01:00
|
|
|
}
|
|
|
|
|
2018-06-22 23:02:47 +02:00
|
|
|
void Network::Server_Send_EVENT_PLAYER_DISCONNECTED(const char* playerName, const char* reason)
|
2016-02-27 14:12:04 +01:00
|
|
|
{
|
2017-06-06 23:24:18 +02:00
|
|
|
std::unique_ptr<NetworkPacket> packet(NetworkPacket::Allocate());
|
2020-04-22 17:09:29 +02:00
|
|
|
*packet << static_cast<uint32_t>(NETWORK_COMMAND_EVENT);
|
|
|
|
*packet << static_cast<uint16_t>(SERVER_EVENT_PLAYER_DISCONNECTED);
|
2017-06-06 23:24:18 +02:00
|
|
|
packet->WriteString(playerName);
|
|
|
|
packet->WriteString(reason);
|
|
|
|
SendPacketToClients(*packet);
|
2016-02-27 14:12:04 +01:00
|
|
|
}
|
|
|
|
|
2015-07-10 21:53:41 +02:00
|
|
|
bool Network::ProcessConnection(NetworkConnection& connection)
|
2015-07-08 03:21:05 +02:00
|
|
|
{
|
2018-06-20 17:28:51 +02:00
|
|
|
int32_t packetStatus;
|
2018-06-22 23:02:47 +02:00
|
|
|
do
|
|
|
|
{
|
2017-06-06 23:24:18 +02:00
|
|
|
packetStatus = connection.ReadPacket();
|
2018-06-22 23:02:47 +02:00
|
|
|
switch (packetStatus)
|
|
|
|
{
|
|
|
|
case NETWORK_READPACKET_DISCONNECTED:
|
|
|
|
// closed connection or network error
|
|
|
|
if (!connection.GetLastDisconnectReason())
|
|
|
|
{
|
|
|
|
connection.SetLastDisconnectReason(STR_MULTIPLAYER_CONNECTION_CLOSED);
|
|
|
|
}
|
2017-06-06 23:24:18 +02:00
|
|
|
return false;
|
2018-06-22 23:02:47 +02:00
|
|
|
case NETWORK_READPACKET_SUCCESS:
|
|
|
|
// done reading in packet
|
|
|
|
ProcessPacket(connection, connection.InboundPacket);
|
|
|
|
if (connection.Socket == nullptr)
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case NETWORK_READPACKET_MORE_DATA:
|
|
|
|
// more data required to be read
|
|
|
|
break;
|
|
|
|
case NETWORK_READPACKET_NO_DATA:
|
|
|
|
// could not read anything from socket
|
|
|
|
break;
|
2017-06-06 23:24:18 +02:00
|
|
|
}
|
|
|
|
} while (packetStatus == NETWORK_READPACKET_MORE_DATA || packetStatus == NETWORK_READPACKET_SUCCESS);
|
|
|
|
connection.SendQueuedPackets();
|
2018-06-22 23:02:47 +02:00
|
|
|
if (!connection.ReceivedPacketRecently())
|
|
|
|
{
|
|
|
|
if (!connection.GetLastDisconnectReason())
|
|
|
|
{
|
2017-06-06 23:24:18 +02:00
|
|
|
connection.SetLastDisconnectReason(STR_MULTIPLAYER_NO_DATA);
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return true;
|
2015-07-09 04:19:12 +02:00
|
|
|
}
|
|
|
|
|
2015-07-15 08:23:04 +02:00
|
|
|
void Network::ProcessPacket(NetworkConnection& connection, NetworkPacket& packet)
|
2015-02-12 12:30:57 +01:00
|
|
|
{
|
2018-06-20 17:28:51 +02:00
|
|
|
uint32_t command;
|
2017-06-06 23:24:18 +02:00
|
|
|
packet >> command;
|
2018-06-22 23:02:47 +02:00
|
|
|
if (command < NETWORK_COMMAND_MAX)
|
|
|
|
{
|
|
|
|
switch (gNetwork.GetMode())
|
|
|
|
{
|
|
|
|
case NETWORK_MODE_SERVER:
|
|
|
|
if (server_command_handlers[command])
|
|
|
|
{
|
|
|
|
if (connection.AuthStatus == NETWORK_AUTH_OK || !packet.CommandRequiresAuth())
|
|
|
|
{
|
|
|
|
(this->*server_command_handlers[command])(connection, packet);
|
|
|
|
}
|
2017-06-06 23:24:18 +02:00
|
|
|
}
|
2018-06-22 23:02:47 +02:00
|
|
|
break;
|
|
|
|
case NETWORK_MODE_CLIENT:
|
|
|
|
if (client_command_handlers[command])
|
|
|
|
{
|
|
|
|
(this->*client_command_handlers[command])(connection, packet);
|
|
|
|
}
|
|
|
|
break;
|
2017-06-06 23:24:18 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
packet.Clear();
|
2015-07-05 17:19:01 +02:00
|
|
|
}
|
|
|
|
|
2019-05-15 19:55:16 +02:00
|
|
|
// This is called at the end of each game tick, this where things should be processed that affects the game state.
|
2018-12-09 20:39:44 +01:00
|
|
|
void Network::ProcessPending()
|
|
|
|
{
|
2019-05-15 19:55:16 +02:00
|
|
|
if (GetMode() == NETWORK_MODE_SERVER)
|
|
|
|
{
|
|
|
|
ProcessDisconnectedClients();
|
|
|
|
}
|
|
|
|
else if (GetMode() == NETWORK_MODE_CLIENT)
|
|
|
|
{
|
|
|
|
ProcessPlayerInfo();
|
|
|
|
}
|
2018-12-09 20:39:44 +01:00
|
|
|
ProcessPlayerList();
|
|
|
|
}
|
|
|
|
|
2020-04-28 20:33:30 +02:00
|
|
|
static bool ProcessPlayerAuthenticatePluginHooks(
|
|
|
|
const NetworkConnection& connection, const std::string_view& name, const std::string_view& publicKeyHash)
|
|
|
|
{
|
|
|
|
# ifdef ENABLE_SCRIPTING
|
|
|
|
using namespace OpenRCT2::Scripting;
|
|
|
|
|
|
|
|
auto& hookEngine = GetContext()->GetScriptEngine().GetHookEngine();
|
|
|
|
if (hookEngine.HasSubscriptions(OpenRCT2::Scripting::HOOK_TYPE::NETWORK_AUTHENTICATE))
|
|
|
|
{
|
|
|
|
auto ctx = GetContext()->GetScriptEngine().GetContext();
|
|
|
|
|
|
|
|
// Create event args object
|
|
|
|
DukObject eObj(ctx);
|
|
|
|
eObj.Set("name", name);
|
|
|
|
eObj.Set("publicKeyHash", publicKeyHash);
|
|
|
|
eObj.Set("ipAddress", connection.Socket->GetIpAddress());
|
|
|
|
eObj.Set("cancel", false);
|
|
|
|
auto e = eObj.Take();
|
|
|
|
|
|
|
|
// Call the subscriptions
|
|
|
|
hookEngine.Call(OpenRCT2::Scripting::HOOK_TYPE::NETWORK_AUTHENTICATE, e, false);
|
|
|
|
|
|
|
|
// Check if any hook has cancelled the join
|
|
|
|
if (AsOrDefault(e["cancel"], false))
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
# endif
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void ProcessPlayerJoinedPluginHooks(uint8_t playerId)
|
|
|
|
{
|
|
|
|
# ifdef ENABLE_SCRIPTING
|
|
|
|
using namespace OpenRCT2::Scripting;
|
|
|
|
|
|
|
|
auto& hookEngine = GetContext()->GetScriptEngine().GetHookEngine();
|
|
|
|
if (hookEngine.HasSubscriptions(OpenRCT2::Scripting::HOOK_TYPE::NETWORK_JOIN))
|
|
|
|
{
|
|
|
|
auto ctx = GetContext()->GetScriptEngine().GetContext();
|
|
|
|
|
|
|
|
// Create event args object
|
|
|
|
DukObject eObj(ctx);
|
|
|
|
eObj.Set("player", playerId);
|
|
|
|
auto e = eObj.Take();
|
|
|
|
|
|
|
|
// Call the subscriptions
|
|
|
|
hookEngine.Call(OpenRCT2::Scripting::HOOK_TYPE::NETWORK_JOIN, e, false);
|
|
|
|
}
|
|
|
|
# endif
|
|
|
|
}
|
|
|
|
|
|
|
|
static void ProcessPlayerLeftPluginHooks(uint8_t playerId)
|
|
|
|
{
|
|
|
|
# ifdef ENABLE_SCRIPTING
|
|
|
|
using namespace OpenRCT2::Scripting;
|
|
|
|
|
|
|
|
auto& hookEngine = GetContext()->GetScriptEngine().GetHookEngine();
|
|
|
|
if (hookEngine.HasSubscriptions(OpenRCT2::Scripting::HOOK_TYPE::NETWORK_LEAVE))
|
|
|
|
{
|
|
|
|
auto ctx = GetContext()->GetScriptEngine().GetContext();
|
|
|
|
|
|
|
|
// Create event args object
|
|
|
|
DukObject eObj(ctx);
|
|
|
|
eObj.Set("player", playerId);
|
|
|
|
auto e = eObj.Take();
|
|
|
|
|
|
|
|
// Call the subscriptions
|
|
|
|
hookEngine.Call(OpenRCT2::Scripting::HOOK_TYPE::NETWORK_LEAVE, e, false);
|
|
|
|
}
|
|
|
|
# endif
|
|
|
|
}
|
|
|
|
|
2018-12-09 20:39:44 +01:00
|
|
|
void Network::ProcessPlayerList()
|
|
|
|
{
|
2019-05-15 19:55:16 +02:00
|
|
|
if (GetMode() == NETWORK_MODE_SERVER)
|
2018-12-09 20:39:44 +01:00
|
|
|
{
|
2019-05-15 19:55:16 +02:00
|
|
|
// Avoid sending multiple times the player list, we mark the list invalidated on modifications
|
|
|
|
// and then send at the end of the tick the final player list.
|
|
|
|
if (_playerListInvalidated)
|
|
|
|
{
|
|
|
|
_playerListInvalidated = false;
|
|
|
|
Server_Send_PLAYERLIST();
|
|
|
|
}
|
2018-12-09 20:39:44 +01:00
|
|
|
}
|
2019-05-15 19:55:16 +02:00
|
|
|
else
|
|
|
|
{
|
|
|
|
// As client we have to keep things in order so the update is tick bound.
|
|
|
|
// Commands/Actions reference players and so this list needs to be in sync with those.
|
|
|
|
auto itPending = _pendingPlayerLists.begin();
|
|
|
|
while (itPending != _pendingPlayerLists.end())
|
|
|
|
{
|
|
|
|
if (itPending->first > gCurrentTicks)
|
|
|
|
break;
|
2018-12-09 20:39:44 +01:00
|
|
|
|
2019-05-15 19:55:16 +02:00
|
|
|
// List of active players found in the list.
|
|
|
|
std::vector<uint8_t> activePlayerIds;
|
2020-04-28 20:33:30 +02:00
|
|
|
std::vector<uint8_t> newPlayers;
|
|
|
|
std::vector<uint8_t> removedPlayers;
|
2018-12-09 20:39:44 +01:00
|
|
|
|
2019-05-15 19:55:16 +02:00
|
|
|
for (auto&& pendingPlayer : itPending->second.players)
|
|
|
|
{
|
|
|
|
activePlayerIds.push_back(pendingPlayer.Id);
|
2019-03-24 23:16:57 +01:00
|
|
|
|
2019-05-15 19:55:16 +02:00
|
|
|
auto* player = GetPlayerByID(pendingPlayer.Id);
|
|
|
|
if (player == nullptr)
|
|
|
|
{
|
|
|
|
// Add new player.
|
|
|
|
player = AddPlayer("", "");
|
|
|
|
if (player)
|
|
|
|
{
|
|
|
|
*player = pendingPlayer;
|
|
|
|
if (player->Flags & NETWORK_PLAYER_FLAG_ISSERVER)
|
|
|
|
{
|
|
|
|
_serverConnection->Player = player;
|
|
|
|
}
|
|
|
|
}
|
2020-04-28 20:33:30 +02:00
|
|
|
|
|
|
|
newPlayers.push_back(player->Id);
|
2019-05-15 19:55:16 +02:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
// Update.
|
|
|
|
*player = pendingPlayer;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Remove any players that are not in newly received list
|
2020-04-28 20:33:30 +02:00
|
|
|
for (const auto& player : player_list)
|
2018-12-09 20:39:44 +01:00
|
|
|
{
|
2020-04-28 20:33:30 +02:00
|
|
|
if (std::find(activePlayerIds.begin(), activePlayerIds.end(), player->Id) == activePlayerIds.end())
|
2018-12-09 20:39:44 +01:00
|
|
|
{
|
2020-04-28 20:33:30 +02:00
|
|
|
removedPlayers.push_back(player->Id);
|
2018-12-09 20:39:44 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-04-28 20:33:30 +02:00
|
|
|
// Run player removed hooks (must be before players removed from list)
|
|
|
|
for (auto playerId : removedPlayers)
|
|
|
|
{
|
|
|
|
ProcessPlayerLeftPluginHooks(playerId);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Run player joined hooks (must be after players added to list)
|
|
|
|
for (auto playerId : newPlayers)
|
|
|
|
{
|
|
|
|
ProcessPlayerJoinedPluginHooks(playerId);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Now actually remove removed players from player list
|
|
|
|
player_list.erase(
|
|
|
|
std::remove_if(
|
|
|
|
player_list.begin(), player_list.end(),
|
|
|
|
[&removedPlayers](const std::unique_ptr<NetworkPlayer>& player) {
|
|
|
|
return std::find(removedPlayers.begin(), removedPlayers.end(), player->Id) != removedPlayers.end();
|
|
|
|
}),
|
|
|
|
player_list.end());
|
|
|
|
|
2019-05-15 19:55:16 +02:00
|
|
|
_pendingPlayerLists.erase(itPending);
|
|
|
|
itPending = _pendingPlayerLists.begin();
|
2018-12-09 20:39:44 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-03-24 23:16:57 +01:00
|
|
|
void Network::ProcessPlayerInfo()
|
|
|
|
{
|
|
|
|
auto range = _pendingPlayerInfo.equal_range(gCurrentTicks);
|
|
|
|
for (auto it = range.first; it != range.second; it++)
|
|
|
|
{
|
|
|
|
auto* player = GetPlayerByID(it->second.Id);
|
|
|
|
if (player != nullptr)
|
|
|
|
{
|
2019-04-07 13:22:57 +02:00
|
|
|
const NetworkPlayer& networkedInfo = it->second;
|
|
|
|
player->Flags = networkedInfo.Flags;
|
|
|
|
player->Group = networkedInfo.Group;
|
|
|
|
player->LastAction = networkedInfo.LastAction;
|
|
|
|
player->LastActionCoord = networkedInfo.LastActionCoord;
|
|
|
|
player->MoneySpent = networkedInfo.MoneySpent;
|
|
|
|
player->CommandsRan = networkedInfo.CommandsRan;
|
2019-03-24 23:16:57 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
_pendingPlayerInfo.erase(gCurrentTicks);
|
|
|
|
}
|
|
|
|
|
2019-05-15 19:55:16 +02:00
|
|
|
void Network::ProcessDisconnectedClients()
|
|
|
|
{
|
|
|
|
for (auto it = client_connection_list.begin(); it != client_connection_list.end();)
|
|
|
|
{
|
|
|
|
auto& connection = *it;
|
|
|
|
if (connection->IsDisconnected)
|
|
|
|
{
|
|
|
|
ServerClientDisconnected(connection);
|
|
|
|
RemovePlayer(connection);
|
|
|
|
|
|
|
|
it = client_connection_list.erase(it);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
it++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-12-17 12:58:12 +01:00
|
|
|
void Network::AddClient(std::unique_ptr<ITcpSocket>&& socket)
|
2015-07-07 22:52:41 +02:00
|
|
|
{
|
2018-12-17 12:58:12 +01:00
|
|
|
// Log connection info.
|
2017-06-06 23:24:18 +02:00
|
|
|
char addr[128];
|
|
|
|
snprintf(addr, sizeof(addr), "Client joined from %s", socket->GetHostName());
|
|
|
|
AppendServerLog(addr);
|
2018-12-17 12:58:12 +01:00
|
|
|
|
|
|
|
// Store connection
|
|
|
|
auto connection = std::make_unique<NetworkConnection>();
|
|
|
|
connection->Socket = std::move(socket);
|
|
|
|
|
2017-06-06 23:24:18 +02:00
|
|
|
client_connection_list.push_back(std::move(connection));
|
2015-07-07 22:52:41 +02:00
|
|
|
}
|
|
|
|
|
2019-05-15 19:55:16 +02:00
|
|
|
void Network::ServerClientDisconnected(std::unique_ptr<NetworkConnection>& connection)
|
2015-07-07 16:09:21 +02:00
|
|
|
{
|
2017-06-06 23:24:18 +02:00
|
|
|
NetworkPlayer* connection_player = connection->Player;
|
2019-05-15 19:55:16 +02:00
|
|
|
if (connection_player == nullptr)
|
|
|
|
return;
|
2018-06-22 23:02:47 +02:00
|
|
|
|
2019-05-15 19:55:16 +02:00
|
|
|
char text[256];
|
|
|
|
const char* has_disconnected_args[2] = {
|
|
|
|
connection_player->Name.c_str(),
|
|
|
|
connection->GetLastDisconnectReason(),
|
|
|
|
};
|
|
|
|
if (has_disconnected_args[1])
|
|
|
|
{
|
|
|
|
format_string(text, 256, STR_MULTIPLAYER_PLAYER_HAS_DISCONNECTED_WITH_REASON, has_disconnected_args);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
format_string(text, 256, STR_MULTIPLAYER_PLAYER_HAS_DISCONNECTED_NO_REASON, &(has_disconnected_args[0]));
|
|
|
|
}
|
2017-06-06 23:24:18 +02:00
|
|
|
|
2019-05-15 19:55:16 +02:00
|
|
|
chat_history_add(text);
|
|
|
|
Peep* pickup_peep = network_get_pickup_peep(connection_player->Id);
|
|
|
|
if (pickup_peep)
|
|
|
|
{
|
2019-06-09 17:54:12 +02:00
|
|
|
PeepPickupAction pickupAction{ PeepPickupType::Cancel,
|
|
|
|
pickup_peep->sprite_index,
|
|
|
|
{ network_get_pickup_peep_old_x(connection_player->Id), 0, 0 },
|
|
|
|
network_get_current_player_id() };
|
|
|
|
auto res = GameActions::Execute(&pickupAction);
|
2017-06-06 23:24:18 +02:00
|
|
|
}
|
2019-05-15 19:55:16 +02:00
|
|
|
gNetwork.Server_Send_EVENT_PLAYER_DISCONNECTED(
|
2020-04-22 17:09:29 +02:00
|
|
|
const_cast<char*>(connection_player->Name.c_str()), connection->GetLastDisconnectReason());
|
2019-05-15 19:55:16 +02:00
|
|
|
|
|
|
|
// Log player disconnected event
|
|
|
|
AppendServerLog(text);
|
2020-04-28 20:33:30 +02:00
|
|
|
|
|
|
|
ProcessPlayerLeftPluginHooks(connection_player->Id);
|
2019-05-15 19:55:16 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void Network::RemovePlayer(std::unique_ptr<NetworkConnection>& connection)
|
|
|
|
{
|
|
|
|
NetworkPlayer* connection_player = connection->Player;
|
|
|
|
if (connection_player == nullptr)
|
|
|
|
return;
|
|
|
|
|
2018-06-22 23:02:47 +02:00
|
|
|
player_list.erase(
|
|
|
|
std::remove_if(
|
2018-07-21 13:51:54 +02:00
|
|
|
player_list.begin(), player_list.end(),
|
2018-06-22 23:02:47 +02:00
|
|
|
[connection_player](std::unique_ptr<NetworkPlayer>& player) { return player.get() == connection_player; }),
|
|
|
|
player_list.end());
|
2019-05-15 19:55:16 +02:00
|
|
|
|
|
|
|
// Send new player list.
|
|
|
|
_playerListInvalidated = true;
|
2015-02-12 03:01:02 +01:00
|
|
|
}
|
|
|
|
|
2019-02-23 15:54:17 +01:00
|
|
|
NetworkPlayer* Network::AddPlayer(const std::string& name, const std::string& keyhash)
|
2015-07-17 02:24:39 +02:00
|
|
|
{
|
2017-06-06 23:24:18 +02:00
|
|
|
NetworkPlayer* addedplayer = nullptr;
|
2018-06-20 17:28:51 +02:00
|
|
|
int32_t newid = -1;
|
2018-06-22 23:02:47 +02:00
|
|
|
if (GetMode() == NETWORK_MODE_SERVER)
|
|
|
|
{
|
2017-06-06 23:24:18 +02:00
|
|
|
// Find first unused player id
|
2018-06-22 23:02:47 +02:00
|
|
|
for (int32_t id = 0; id < 255; id++)
|
|
|
|
{
|
|
|
|
if (std::find_if(
|
2018-07-21 13:51:54 +02:00
|
|
|
player_list.begin(), player_list.end(),
|
2018-06-22 23:02:47 +02:00
|
|
|
[&id](std::unique_ptr<NetworkPlayer> const& player) { return player->Id == id; })
|
|
|
|
== player_list.end())
|
|
|
|
{
|
2017-06-06 23:24:18 +02:00
|
|
|
newid = id;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2018-06-22 23:02:47 +02:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2017-06-06 23:24:18 +02:00
|
|
|
newid = 0;
|
|
|
|
}
|
2018-06-22 23:02:47 +02:00
|
|
|
if (newid != -1)
|
|
|
|
{
|
2017-06-06 23:24:18 +02:00
|
|
|
std::unique_ptr<NetworkPlayer> player;
|
2018-06-22 23:02:47 +02:00
|
|
|
if (GetMode() == NETWORK_MODE_SERVER)
|
|
|
|
{
|
2017-06-06 23:24:18 +02:00
|
|
|
// Load keys host may have added manually
|
|
|
|
_userManager.Load();
|
|
|
|
|
|
|
|
// Check if the key is registered
|
2018-06-22 23:02:47 +02:00
|
|
|
const NetworkUser* networkUser = _userManager.GetUserByHash(keyhash);
|
2017-06-06 23:24:18 +02:00
|
|
|
|
2017-10-03 00:00:32 +02:00
|
|
|
player = std::make_unique<NetworkPlayer>();
|
2017-06-06 23:24:18 +02:00
|
|
|
player->Id = newid;
|
|
|
|
player->KeyHash = keyhash;
|
2018-06-22 23:02:47 +02:00
|
|
|
if (networkUser == nullptr)
|
|
|
|
{
|
2017-06-06 23:24:18 +02:00
|
|
|
player->Group = GetDefaultGroup();
|
2019-05-10 22:00:38 +02:00
|
|
|
if (!name.empty())
|
2018-06-22 23:02:47 +02:00
|
|
|
{
|
2019-02-23 15:54:17 +01:00
|
|
|
player->SetName(MakePlayerNameUnique(String::Trim(name)));
|
2017-06-06 23:24:18 +02:00
|
|
|
}
|
2018-06-22 23:02:47 +02:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2017-06-06 23:24:18 +02:00
|
|
|
player->Group = networkUser->GroupId.GetValueOrDefault(GetDefaultGroup());
|
|
|
|
player->SetName(networkUser->Name);
|
|
|
|
}
|
2019-05-15 19:55:16 +02:00
|
|
|
|
|
|
|
// Send new player list.
|
|
|
|
_playerListInvalidated = true;
|
2018-06-22 23:02:47 +02:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2017-10-03 00:00:32 +02:00
|
|
|
player = std::make_unique<NetworkPlayer>();
|
2017-06-06 23:24:18 +02:00
|
|
|
player->Id = newid;
|
|
|
|
player->Group = GetDefaultGroup();
|
2017-07-04 09:19:39 +02:00
|
|
|
player->SetName(String::Trim(std::string(name)));
|
2017-06-06 23:24:18 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
addedplayer = player.get();
|
|
|
|
player_list.push_back(std::move(player));
|
|
|
|
}
|
|
|
|
return addedplayer;
|
2015-07-17 02:24:39 +02:00
|
|
|
}
|
|
|
|
|
2018-06-22 23:02:47 +02:00
|
|
|
std::string Network::MakePlayerNameUnique(const std::string& name)
|
2016-05-24 00:20:41 +02:00
|
|
|
{
|
2017-06-06 23:24:18 +02:00
|
|
|
// Note: Player names are case-insensitive
|
|
|
|
|
|
|
|
std::string new_name = name.substr(0, 31);
|
2018-06-20 17:28:51 +02:00
|
|
|
int32_t counter = 1;
|
2017-06-06 23:24:18 +02:00
|
|
|
bool unique;
|
2018-06-22 23:02:47 +02:00
|
|
|
do
|
|
|
|
{
|
2017-06-06 23:24:18 +02:00
|
|
|
unique = true;
|
|
|
|
|
|
|
|
// Check if there is already a player with this name in the server
|
2018-06-22 23:02:47 +02:00
|
|
|
for (const auto& player : player_list)
|
|
|
|
{
|
|
|
|
if (String::Equals(player->Name.c_str(), new_name.c_str(), true))
|
|
|
|
{
|
2017-06-06 23:24:18 +02:00
|
|
|
unique = false;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-06-22 23:02:47 +02:00
|
|
|
if (unique)
|
|
|
|
{
|
2017-06-06 23:24:18 +02:00
|
|
|
// Check if there is already a registered player with this name
|
2018-06-22 23:02:47 +02:00
|
|
|
if (_userManager.GetUserByName(new_name) != nullptr)
|
|
|
|
{
|
2017-06-06 23:24:18 +02:00
|
|
|
unique = false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-06-22 23:02:47 +02:00
|
|
|
if (!unique)
|
|
|
|
{
|
2017-06-06 23:24:18 +02:00
|
|
|
// Increment name counter
|
|
|
|
counter++;
|
|
|
|
new_name = name.substr(0, 31) + " #" + std::to_string(counter);
|
|
|
|
}
|
|
|
|
} while (!unique);
|
|
|
|
return new_name;
|
2016-05-24 00:20:41 +02:00
|
|
|
}
|
|
|
|
|
2016-05-19 10:23:42 +02:00
|
|
|
void Network::Client_Handle_TOKEN(NetworkConnection& connection, NetworkPacket& packet)
|
|
|
|
{
|
2017-06-06 23:24:18 +02:00
|
|
|
utf8 keyPath[MAX_PATH];
|
|
|
|
network_get_private_key_path(keyPath, sizeof(keyPath), gConfigNetwork.player_name);
|
2020-07-02 17:03:31 +02:00
|
|
|
if (!Platform::FileExists(keyPath))
|
2018-06-22 23:02:47 +02:00
|
|
|
{
|
2017-06-06 23:24:18 +02:00
|
|
|
log_error("Key file (%s) was not found. Restart client to re-generate it.", keyPath);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
try
|
|
|
|
{
|
|
|
|
auto fs = FileStream(keyPath, FILE_MODE_OPEN);
|
|
|
|
if (!_key.LoadPrivate(&fs))
|
|
|
|
{
|
2018-01-02 20:23:22 +01:00
|
|
|
throw std::runtime_error("Failed to load private key.");
|
2017-06-06 23:24:18 +02:00
|
|
|
}
|
|
|
|
}
|
2018-06-22 23:02:47 +02:00
|
|
|
catch (const std::exception&)
|
2017-06-06 23:24:18 +02:00
|
|
|
{
|
|
|
|
log_error("Failed to load key %s", keyPath);
|
|
|
|
connection.SetLastDisconnectReason(STR_MULTIPLAYER_VERIFICATION_FAILURE);
|
|
|
|
connection.Socket->Disconnect();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2018-06-20 17:28:51 +02:00
|
|
|
uint32_t challenge_size;
|
2017-06-06 23:24:18 +02:00
|
|
|
packet >> challenge_size;
|
2020-04-22 17:09:29 +02:00
|
|
|
const char* challenge = reinterpret_cast<const char*>(packet.Read(challenge_size));
|
2019-03-09 13:58:23 +01:00
|
|
|
|
|
|
|
std::vector<uint8_t> signature;
|
2017-06-06 23:24:18 +02:00
|
|
|
const std::string pubkey = _key.PublicKeyString();
|
|
|
|
_challenge.resize(challenge_size);
|
2018-12-15 22:23:31 +01:00
|
|
|
std::memcpy(_challenge.data(), challenge, challenge_size);
|
2019-03-09 13:58:23 +01:00
|
|
|
bool ok = _key.Sign(_challenge.data(), _challenge.size(), signature);
|
2018-06-22 23:02:47 +02:00
|
|
|
if (!ok)
|
|
|
|
{
|
2017-06-06 23:24:18 +02:00
|
|
|
log_error("Failed to sign server's challenge.");
|
|
|
|
connection.SetLastDisconnectReason(STR_MULTIPLAYER_VERIFICATION_FAILURE);
|
|
|
|
connection.Socket->Disconnect();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
// Don't keep private key in memory. There's no need and it may get leaked
|
|
|
|
// when process dump gets collected at some point in future.
|
|
|
|
_key.Unload();
|
2017-12-21 21:23:01 +01:00
|
|
|
|
2018-06-22 23:02:47 +02:00
|
|
|
const char* password = String::IsNullOrEmpty(gCustomPassword) ? "" : gCustomPassword;
|
2019-03-09 13:58:23 +01:00
|
|
|
Client_Send_AUTH(gConfigNetwork.player_name.c_str(), password, pubkey.c_str(), signature);
|
2016-05-19 10:23:42 +02:00
|
|
|
}
|
|
|
|
|
2019-05-11 21:31:34 +02:00
|
|
|
void Network::Server_Handle_REQUEST_GAMESTATE(NetworkConnection& connection, NetworkPacket& packet)
|
|
|
|
{
|
|
|
|
uint32_t tick;
|
|
|
|
packet >> tick;
|
|
|
|
|
|
|
|
if (_serverState.gamestateSnapshotsEnabled == false)
|
|
|
|
{
|
|
|
|
// Ignore this if this is off.
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
IGameStateSnapshots* snapshots = GetContext()->GetGameStateSnapshots();
|
|
|
|
|
|
|
|
const GameStateSnapshot_t* snapshot = snapshots->GetLinkedSnapshot(tick);
|
|
|
|
if (snapshot)
|
|
|
|
{
|
|
|
|
MemoryStream snapshotMemory;
|
|
|
|
DataSerialiser ds(true, snapshotMemory);
|
|
|
|
|
|
|
|
snapshots->SerialiseSnapshot(const_cast<GameStateSnapshot_t&>(*snapshot), ds);
|
|
|
|
|
|
|
|
uint32_t bytesSent = 0;
|
2020-04-22 17:09:29 +02:00
|
|
|
uint32_t length = static_cast<uint32_t>(snapshotMemory.GetLength());
|
2019-05-11 21:31:34 +02:00
|
|
|
while (bytesSent < length)
|
|
|
|
{
|
|
|
|
uint32_t dataSize = CHUNK_SIZE;
|
|
|
|
if (bytesSent + dataSize > snapshotMemory.GetLength())
|
|
|
|
{
|
|
|
|
dataSize = snapshotMemory.GetLength() - bytesSent;
|
|
|
|
}
|
|
|
|
|
|
|
|
std::unique_ptr<NetworkPacket> gameStateChunk(NetworkPacket::Allocate());
|
2020-04-22 17:09:29 +02:00
|
|
|
*gameStateChunk << static_cast<uint32_t>(NETWORK_COMMAND_GAMESTATE) << tick << length << bytesSent << dataSize;
|
|
|
|
gameStateChunk->Write(static_cast<const uint8_t*>(snapshotMemory.GetData()) + bytesSent, dataSize);
|
2019-05-11 21:31:34 +02:00
|
|
|
|
|
|
|
connection.QueuePacket(std::move(gameStateChunk));
|
|
|
|
|
|
|
|
bytesSent += dataSize;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-07-24 22:49:05 +02:00
|
|
|
void Network::Server_Handle_HEARTBEAT(NetworkConnection& connection, NetworkPacket& packet)
|
|
|
|
{
|
|
|
|
log_verbose("Client %s heartbeat", connection.Socket->GetHostName());
|
|
|
|
connection.ResetLastPacketTime();
|
|
|
|
}
|
|
|
|
|
2016-01-22 07:32:40 +01:00
|
|
|
void Network::Client_Handle_AUTH(NetworkConnection& connection, NetworkPacket& packet)
|
2015-07-15 06:40:22 +02:00
|
|
|
{
|
2018-06-20 17:28:51 +02:00
|
|
|
uint32_t auth_status;
|
2020-04-22 17:09:29 +02:00
|
|
|
packet >> auth_status >> const_cast<uint8_t&>(player_id);
|
|
|
|
connection.AuthStatus = static_cast<NETWORK_AUTH>(auth_status);
|
2018-06-22 23:02:47 +02:00
|
|
|
switch (connection.AuthStatus)
|
2017-06-06 23:24:18 +02:00
|
|
|
{
|
2018-06-22 23:02:47 +02:00
|
|
|
case NETWORK_AUTH_OK:
|
|
|
|
Client_Send_GAMEINFO();
|
|
|
|
break;
|
|
|
|
case NETWORK_AUTH_BADNAME:
|
|
|
|
connection.SetLastDisconnectReason(STR_MULTIPLAYER_BAD_PLAYER_NAME);
|
|
|
|
connection.Socket->Disconnect();
|
|
|
|
break;
|
|
|
|
case NETWORK_AUTH_BADVERSION:
|
|
|
|
{
|
|
|
|
const char* version = packet.ReadString();
|
|
|
|
connection.SetLastDisconnectReason(STR_MULTIPLAYER_INCORRECT_SOFTWARE_VERSION, &version);
|
|
|
|
connection.Socket->Disconnect();
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case NETWORK_AUTH_BADPASSWORD:
|
|
|
|
connection.SetLastDisconnectReason(STR_MULTIPLAYER_BAD_PASSWORD);
|
|
|
|
connection.Socket->Disconnect();
|
|
|
|
break;
|
|
|
|
case NETWORK_AUTH_VERIFICATIONFAILURE:
|
|
|
|
connection.SetLastDisconnectReason(STR_MULTIPLAYER_VERIFICATION_FAILURE);
|
|
|
|
connection.Socket->Disconnect();
|
|
|
|
break;
|
|
|
|
case NETWORK_AUTH_FULL:
|
|
|
|
connection.SetLastDisconnectReason(STR_MULTIPLAYER_SERVER_FULL);
|
|
|
|
connection.Socket->Disconnect();
|
|
|
|
break;
|
|
|
|
case NETWORK_AUTH_REQUIREPASSWORD:
|
|
|
|
context_open_window_view(WV_NETWORK_PASSWORD);
|
|
|
|
break;
|
|
|
|
case NETWORK_AUTH_UNKNOWN_KEY_DISALLOWED:
|
|
|
|
connection.SetLastDisconnectReason(STR_MULTIPLAYER_UNKNOWN_KEY_DISALLOWED);
|
|
|
|
connection.Socket->Disconnect();
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
connection.SetLastDisconnectReason(STR_MULTIPLAYER_INCORRECT_SOFTWARE_VERSION);
|
|
|
|
connection.Socket->Disconnect();
|
|
|
|
break;
|
2017-06-06 23:24:18 +02:00
|
|
|
}
|
2015-07-15 06:40:22 +02:00
|
|
|
}
|
|
|
|
|
2018-06-22 23:02:47 +02:00
|
|
|
void Network::Server_Client_Joined(const char* name, const std::string& keyhash, NetworkConnection& connection)
|
2016-05-19 10:23:42 +02:00
|
|
|
{
|
2020-04-28 20:33:30 +02:00
|
|
|
auto player = AddPlayer(name, keyhash);
|
2017-06-06 23:24:18 +02:00
|
|
|
connection.Player = player;
|
2020-04-28 20:33:30 +02:00
|
|
|
if (player != nullptr)
|
2018-06-22 23:02:47 +02:00
|
|
|
{
|
2017-06-06 23:24:18 +02:00
|
|
|
char text[256];
|
2020-04-22 17:09:29 +02:00
|
|
|
const char* player_name = static_cast<const char*>(player->Name.c_str());
|
2017-06-06 23:24:18 +02:00
|
|
|
format_string(text, 256, STR_MULTIPLAYER_PLAYER_HAS_JOINED_THE_GAME, &player_name);
|
|
|
|
chat_history_add(text);
|
2017-02-05 16:45:23 +01:00
|
|
|
|
2018-04-10 14:19:23 +02:00
|
|
|
auto context = GetContext();
|
2018-08-29 00:52:10 +02:00
|
|
|
auto& objManager = context->GetObjectManager();
|
|
|
|
auto objects = objManager.GetPackableObjects();
|
2020-07-24 22:36:45 +02:00
|
|
|
Server_Send_OBJECTS_LIST(connection, objects);
|
2020-02-22 23:18:22 +01:00
|
|
|
Server_Send_SCRIPTS(connection);
|
2016-08-10 05:21:55 +02:00
|
|
|
|
2017-06-06 23:24:18 +02:00
|
|
|
// Log player joining event
|
2018-10-15 21:40:22 +02:00
|
|
|
std::string playerNameHash = player->Name + " (" + keyhash + ")";
|
2020-04-22 17:09:29 +02:00
|
|
|
player_name = static_cast<const char*>(playerNameHash.c_str());
|
2018-10-15 21:40:22 +02:00
|
|
|
format_string(text, 256, STR_MULTIPLAYER_PLAYER_HAS_JOINED_THE_GAME, &player_name);
|
2017-06-06 23:24:18 +02:00
|
|
|
AppendServerLog(text);
|
2020-04-28 20:33:30 +02:00
|
|
|
|
|
|
|
ProcessPlayerJoinedPluginHooks(player->Id);
|
2017-06-06 23:24:18 +02:00
|
|
|
}
|
2016-05-19 10:23:42 +02:00
|
|
|
}
|
|
|
|
|
2018-05-31 23:22:46 +02:00
|
|
|
void Network::Server_Handle_TOKEN(NetworkConnection& connection, [[maybe_unused]] NetworkPacket& packet)
|
2016-05-19 10:23:42 +02:00
|
|
|
{
|
2018-06-20 17:28:51 +02:00
|
|
|
uint8_t token_size = 10 + (rand() & 0x7f);
|
2017-06-06 23:24:18 +02:00
|
|
|
connection.Challenge.resize(token_size);
|
2018-06-22 23:02:47 +02:00
|
|
|
for (int32_t i = 0; i < token_size; i++)
|
|
|
|
{
|
2020-04-22 17:09:29 +02:00
|
|
|
connection.Challenge[i] = static_cast<uint8_t>(rand() & 0xff);
|
2017-06-06 23:24:18 +02:00
|
|
|
}
|
|
|
|
Server_Send_TOKEN(connection);
|
2016-05-19 10:23:42 +02:00
|
|
|
}
|
|
|
|
|
2020-07-24 20:59:22 +02:00
|
|
|
void Network::Client_Handle_OBJECTS_LIST(NetworkConnection& connection, NetworkPacket& packet)
|
2016-10-14 07:29:45 +02:00
|
|
|
{
|
2018-08-29 00:52:10 +02:00
|
|
|
auto& repo = GetContext()->GetObjectRepository();
|
2020-07-24 22:36:45 +02:00
|
|
|
|
|
|
|
uint32_t index = 0;
|
|
|
|
uint32_t totalObjects = 0;
|
|
|
|
packet >> index >> totalObjects;
|
|
|
|
|
|
|
|
if (index == 0)
|
|
|
|
{
|
|
|
|
// Start of objects list.
|
|
|
|
_missingObjects.clear();
|
|
|
|
}
|
|
|
|
|
|
|
|
if (totalObjects > OBJECT_ENTRY_COUNT)
|
2017-08-01 13:29:16 +02:00
|
|
|
{
|
|
|
|
connection.SetLastDisconnectReason(STR_MULTIPLAYER_SERVER_INVALID_REQUEST);
|
|
|
|
connection.Socket->Disconnect();
|
|
|
|
log_warning("Server sent invalid amount of objects");
|
|
|
|
return;
|
|
|
|
}
|
2020-07-24 22:36:45 +02:00
|
|
|
|
|
|
|
char objectListMsg[256];
|
|
|
|
uint32_t args[] = {
|
|
|
|
index + 1,
|
|
|
|
totalObjects,
|
|
|
|
};
|
|
|
|
format_string(objectListMsg, 256, STR_MULTIPLAYER_RECEIVING_OBJECTS_LIST, &args);
|
|
|
|
|
|
|
|
auto intent = Intent(WC_NETWORK_STATUS);
|
|
|
|
intent.putExtra(INTENT_EXTRA_MESSAGE, std::string{ objectListMsg });
|
|
|
|
intent.putExtra(INTENT_EXTRA_CALLBACK, []() -> void { gNetwork.Close(); });
|
|
|
|
context_open_intent(&intent);
|
|
|
|
|
|
|
|
const char* name = reinterpret_cast<const char*>(packet.Read(8));
|
|
|
|
// Required, as packet has no null terminators.
|
|
|
|
std::string s(name, name + 8);
|
|
|
|
uint32_t checksum, flags;
|
|
|
|
packet >> checksum >> flags;
|
|
|
|
|
|
|
|
const ObjectRepositoryItem* ori = repo.FindObject(s.c_str());
|
|
|
|
// This could potentially request the object if checksums don't match, but since client
|
|
|
|
// won't replace its version with server-provided one, we don't do that.
|
|
|
|
if (ori == nullptr)
|
|
|
|
{
|
|
|
|
log_verbose("Requesting object %s with checksum %x from server", s.c_str(), checksum);
|
|
|
|
_missingObjects.push_back(s);
|
|
|
|
}
|
|
|
|
else if (ori->ObjectEntry.checksum != checksum || ori->ObjectEntry.flags != flags)
|
|
|
|
{
|
|
|
|
log_warning(
|
|
|
|
"Object %s has different checksum/flags (%x/%x) than server (%x/%x).", s.c_str(), ori->ObjectEntry.checksum,
|
|
|
|
ori->ObjectEntry.flags, checksum, flags);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (_missingObjects.size() == totalObjects)
|
|
|
|
{
|
|
|
|
log_verbose("client received object list, it has %u entries", totalObjects);
|
2020-07-24 23:00:07 +02:00
|
|
|
Client_Send_REQUESTMAP(_missingObjects);
|
2020-07-24 22:36:45 +02:00
|
|
|
_missingObjects.clear();
|
2017-06-06 23:24:18 +02:00
|
|
|
}
|
2016-10-14 07:29:45 +02:00
|
|
|
}
|
|
|
|
|
2020-02-22 23:18:22 +01:00
|
|
|
void Network::Client_Handle_SCRIPTS(NetworkConnection& connection, NetworkPacket& packet)
|
|
|
|
{
|
|
|
|
uint32_t numScripts{};
|
|
|
|
packet >> numScripts;
|
2020-02-23 13:55:48 +01:00
|
|
|
|
2020-04-24 16:58:01 +02:00
|
|
|
# ifdef ENABLE_SCRIPTING
|
2020-02-23 13:55:48 +01:00
|
|
|
auto& scriptEngine = GetContext()->GetScriptEngine();
|
2020-02-22 23:18:22 +01:00
|
|
|
for (uint32_t i = 0; i < numScripts; i++)
|
|
|
|
{
|
|
|
|
uint32_t codeLength{};
|
|
|
|
packet >> codeLength;
|
2020-04-22 23:53:32 +02:00
|
|
|
auto code = std::string_view(reinterpret_cast<const char*>(packet.Read(codeLength)), codeLength);
|
2020-02-22 23:18:22 +01:00
|
|
|
scriptEngine.AddNetworkPlugin(code);
|
|
|
|
}
|
2020-02-23 13:55:48 +01:00
|
|
|
# else
|
|
|
|
if (numScripts > 0)
|
|
|
|
{
|
|
|
|
connection.SetLastDisconnectReason("The server requires plugin support.");
|
|
|
|
Close();
|
|
|
|
}
|
|
|
|
# endif
|
2020-02-22 23:18:22 +01:00
|
|
|
}
|
|
|
|
|
2019-05-11 21:31:34 +02:00
|
|
|
void Network::Client_Handle_GAMESTATE(NetworkConnection& connection, NetworkPacket& packet)
|
|
|
|
{
|
|
|
|
uint32_t tick;
|
|
|
|
uint32_t totalSize;
|
|
|
|
uint32_t offset;
|
|
|
|
uint32_t dataSize;
|
|
|
|
|
|
|
|
packet >> tick >> totalSize >> offset >> dataSize;
|
|
|
|
|
|
|
|
if (offset == 0)
|
|
|
|
{
|
|
|
|
// Reset
|
|
|
|
_serverGameState = MemoryStream();
|
|
|
|
}
|
|
|
|
|
|
|
|
_serverGameState.SetPosition(offset);
|
|
|
|
|
|
|
|
const uint8_t* data = packet.Read(dataSize);
|
|
|
|
_serverGameState.Write(data, dataSize);
|
|
|
|
|
2020-06-07 23:18:11 +02:00
|
|
|
log_verbose(
|
|
|
|
"Received Game State %.02f%%",
|
|
|
|
(static_cast<float>(_serverGameState.GetLength()) / static_cast<float>(totalSize)) * 100.0f);
|
2019-05-11 21:31:34 +02:00
|
|
|
|
|
|
|
if (_serverGameState.GetLength() == totalSize)
|
|
|
|
{
|
|
|
|
_serverGameState.SetPosition(0);
|
|
|
|
DataSerialiser ds(false, _serverGameState);
|
|
|
|
|
|
|
|
IGameStateSnapshots* snapshots = GetContext()->GetGameStateSnapshots();
|
|
|
|
|
|
|
|
GameStateSnapshot_t& serverSnapshot = snapshots->CreateSnapshot();
|
|
|
|
snapshots->SerialiseSnapshot(serverSnapshot, ds);
|
|
|
|
|
|
|
|
const GameStateSnapshot_t* desyncSnapshot = snapshots->GetLinkedSnapshot(tick);
|
|
|
|
if (desyncSnapshot)
|
|
|
|
{
|
|
|
|
GameStateCompareData_t cmpData = snapshots->Compare(serverSnapshot, *desyncSnapshot);
|
|
|
|
|
|
|
|
std::string outputPath = GetContext()->GetPlatformEnvironment()->GetDirectoryPath(
|
|
|
|
DIRBASE::USER, DIRID::LOG_DESYNCS);
|
|
|
|
|
|
|
|
platform_ensure_directory_exists(outputPath.c_str());
|
|
|
|
|
|
|
|
char uniqueFileName[128] = {};
|
|
|
|
snprintf(
|
|
|
|
uniqueFileName, sizeof(uniqueFileName), "desync_%llu_%u.txt",
|
2020-04-22 17:09:29 +02:00
|
|
|
static_cast<long long unsigned>(platform_get_datetime_now_utc()), tick);
|
2019-05-11 21:31:34 +02:00
|
|
|
|
|
|
|
std::string outputFile = Path::Combine(outputPath, uniqueFileName);
|
|
|
|
|
|
|
|
if (snapshots->LogCompareDataToFile(outputFile, cmpData))
|
|
|
|
{
|
|
|
|
log_info("Wrote desync report to '%s'", outputFile.c_str());
|
|
|
|
|
|
|
|
uint8_t args[32]{};
|
2020-04-20 23:55:15 +02:00
|
|
|
Formatter(args).Add<char*>(uniqueFileName);
|
2019-05-11 21:31:34 +02:00
|
|
|
|
|
|
|
char str_desync[1024];
|
|
|
|
format_string(str_desync, sizeof(str_desync), STR_DESYNC_REPORT, args);
|
|
|
|
|
|
|
|
auto intent = Intent(WC_NETWORK_STATUS);
|
|
|
|
intent.putExtra(INTENT_EXTRA_MESSAGE, std::string{ str_desync });
|
|
|
|
context_open_intent(&intent);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-07-24 20:59:22 +02:00
|
|
|
void Network::Server_Handle_MAPREQUEST(NetworkConnection& connection, NetworkPacket& packet)
|
2016-10-14 07:29:45 +02:00
|
|
|
{
|
2018-06-20 17:28:51 +02:00
|
|
|
uint32_t size;
|
2017-06-06 23:24:18 +02:00
|
|
|
packet >> size;
|
2017-08-01 13:29:16 +02:00
|
|
|
if (size > OBJECT_ENTRY_COUNT)
|
|
|
|
{
|
|
|
|
connection.SetLastDisconnectReason(STR_MULTIPLAYER_CLIENT_INVALID_REQUEST);
|
|
|
|
connection.Socket->Disconnect();
|
|
|
|
std::string playerName = "(unknown)";
|
|
|
|
if (connection.Player)
|
|
|
|
{
|
|
|
|
playerName = connection.Player->Name;
|
|
|
|
}
|
|
|
|
std::string text = std::string("Player ") + playerName + std::string(" requested invalid amount of objects");
|
|
|
|
AppendServerLog(text);
|
|
|
|
log_warning(text.c_str());
|
|
|
|
return;
|
|
|
|
}
|
2017-06-06 23:24:18 +02:00
|
|
|
log_verbose("Client requested %u objects", size);
|
2018-08-29 00:52:10 +02:00
|
|
|
auto& repo = GetContext()->GetObjectRepository();
|
2018-06-20 17:28:51 +02:00
|
|
|
for (uint32_t i = 0; i < size; i++)
|
2017-06-06 23:24:18 +02:00
|
|
|
{
|
2020-04-22 17:09:29 +02:00
|
|
|
const char* name = reinterpret_cast<const char*>(packet.Read(8));
|
2017-06-06 23:24:18 +02:00
|
|
|
// This is required, as packet does not have null terminator
|
|
|
|
std::string s(name, name + 8);
|
|
|
|
log_verbose("Client requested object %s", s.c_str());
|
2018-08-29 00:52:10 +02:00
|
|
|
const ObjectRepositoryItem* item = repo.FindObject(s.c_str());
|
2018-06-22 23:02:47 +02:00
|
|
|
if (item == nullptr)
|
|
|
|
{
|
2017-06-06 23:24:18 +02:00
|
|
|
log_warning("Client tried getting non-existent object %s from us.", s.c_str());
|
2018-06-22 23:02:47 +02:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2017-06-06 23:24:18 +02:00
|
|
|
connection.RequestedObjects.push_back(item);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-04-22 17:09:29 +02:00
|
|
|
const char* player_name = static_cast<const char*>(connection.Player->Name.c_str());
|
2017-06-06 23:24:18 +02:00
|
|
|
Server_Send_MAP(&connection);
|
2018-12-09 20:39:44 +01:00
|
|
|
Server_Send_EVENT_PLAYER_JOINED(player_name);
|
2017-06-06 23:24:18 +02:00
|
|
|
Server_Send_GROUPLIST(connection);
|
2016-10-14 07:29:45 +02:00
|
|
|
}
|
|
|
|
|
2016-01-22 07:32:40 +01:00
|
|
|
void Network::Server_Handle_AUTH(NetworkConnection& connection, NetworkPacket& packet)
|
2015-07-15 08:23:04 +02:00
|
|
|
{
|
2018-06-22 23:02:47 +02:00
|
|
|
if (connection.AuthStatus != NETWORK_AUTH_OK)
|
|
|
|
{
|
2017-06-06 23:24:18 +02:00
|
|
|
const char* gameversion = packet.ReadString();
|
|
|
|
const char* name = packet.ReadString();
|
|
|
|
const char* password = packet.ReadString();
|
2018-06-22 23:02:47 +02:00
|
|
|
const char* pubkey = packet.ReadString();
|
2018-06-20 17:28:51 +02:00
|
|
|
uint32_t sigsize;
|
2017-06-06 23:24:18 +02:00
|
|
|
packet >> sigsize;
|
2018-06-22 23:02:47 +02:00
|
|
|
if (pubkey == nullptr)
|
|
|
|
{
|
2017-06-06 23:24:18 +02:00
|
|
|
connection.AuthStatus = NETWORK_AUTH_VERIFICATIONFAILURE;
|
2018-06-22 23:02:47 +02:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2017-06-06 23:24:18 +02:00
|
|
|
try
|
|
|
|
{
|
2019-03-09 13:58:23 +01:00
|
|
|
std::vector<uint8_t> signature;
|
|
|
|
signature.resize(sigsize);
|
|
|
|
|
2020-04-22 17:09:29 +02:00
|
|
|
const uint8_t* signatureData = packet.Read(sigsize);
|
2019-03-09 13:58:23 +01:00
|
|
|
if (signatureData == nullptr)
|
2017-06-06 23:24:18 +02:00
|
|
|
{
|
2018-01-02 20:23:22 +01:00
|
|
|
throw std::runtime_error("Failed to read packet.");
|
2017-06-06 23:24:18 +02:00
|
|
|
}
|
|
|
|
|
2019-03-09 13:58:23 +01:00
|
|
|
std::memcpy(signature.data(), signatureData, sigsize);
|
|
|
|
|
2017-06-06 23:24:18 +02:00
|
|
|
auto ms = MemoryStream(pubkey, strlen(pubkey));
|
|
|
|
if (!connection.Key.LoadPublic(&ms))
|
|
|
|
{
|
2018-01-02 20:23:22 +01:00
|
|
|
throw std::runtime_error("Failed to load public key.");
|
2017-06-06 23:24:18 +02:00
|
|
|
}
|
|
|
|
|
2019-03-09 13:58:23 +01:00
|
|
|
bool verified = connection.Key.Verify(connection.Challenge.data(), connection.Challenge.size(), signature);
|
2017-06-06 23:24:18 +02:00
|
|
|
const std::string hash = connection.Key.PublicKeyHash();
|
|
|
|
if (verified)
|
|
|
|
{
|
|
|
|
log_verbose("Signature verification ok. Hash %s", hash.c_str());
|
|
|
|
if (gConfigNetwork.known_keys_only && _userManager.GetUserByHash(hash) == nullptr)
|
|
|
|
{
|
|
|
|
log_verbose("Hash %s, not known", hash.c_str());
|
|
|
|
connection.AuthStatus = NETWORK_AUTH_UNKNOWN_KEY_DISALLOWED;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
connection.AuthStatus = NETWORK_AUTH_VERIFIED;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
connection.AuthStatus = NETWORK_AUTH_VERIFICATIONFAILURE;
|
|
|
|
log_verbose("Signature verification failed!");
|
|
|
|
}
|
|
|
|
}
|
2018-06-22 23:02:47 +02:00
|
|
|
catch (const std::exception&)
|
2017-06-06 23:24:18 +02:00
|
|
|
{
|
|
|
|
connection.AuthStatus = NETWORK_AUTH_VERIFICATIONFAILURE;
|
|
|
|
log_verbose("Signature verification failed, invalid data!");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
bool passwordless = false;
|
2018-06-22 23:02:47 +02:00
|
|
|
if (connection.AuthStatus == NETWORK_AUTH_VERIFIED)
|
|
|
|
{
|
|
|
|
const NetworkGroup* group = GetGroupByID(GetGroupIDByHash(connection.Key.PublicKeyHash()));
|
2017-06-06 23:24:18 +02:00
|
|
|
passwordless = group->CanPerformCommand(MISC_COMMAND_PASSWORDLESS_LOGIN);
|
|
|
|
}
|
2018-06-22 23:02:47 +02:00
|
|
|
if (!gameversion || network_get_version() != gameversion)
|
|
|
|
{
|
2017-06-06 23:24:18 +02:00
|
|
|
connection.AuthStatus = NETWORK_AUTH_BADVERSION;
|
2018-06-22 23:02:47 +02:00
|
|
|
}
|
|
|
|
else if (!name)
|
|
|
|
{
|
2017-06-06 23:24:18 +02:00
|
|
|
connection.AuthStatus = NETWORK_AUTH_BADNAME;
|
2018-06-22 23:02:47 +02:00
|
|
|
}
|
|
|
|
else if (!passwordless)
|
|
|
|
{
|
|
|
|
if ((!password || strlen(password) == 0) && !_password.empty())
|
|
|
|
{
|
2017-06-06 23:24:18 +02:00
|
|
|
connection.AuthStatus = NETWORK_AUTH_REQUIREPASSWORD;
|
2018-06-22 23:02:47 +02:00
|
|
|
}
|
|
|
|
else if (password && _password != password)
|
|
|
|
{
|
2017-06-06 23:24:18 +02:00
|
|
|
connection.AuthStatus = NETWORK_AUTH_BADPASSWORD;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-04-22 17:09:29 +02:00
|
|
|
if (static_cast<size_t>(gConfigNetwork.maxplayers) <= player_list.size())
|
2018-06-22 23:02:47 +02:00
|
|
|
{
|
2017-06-06 23:24:18 +02:00
|
|
|
connection.AuthStatus = NETWORK_AUTH_FULL;
|
2018-06-22 23:02:47 +02:00
|
|
|
}
|
|
|
|
else if (connection.AuthStatus == NETWORK_AUTH_VERIFIED)
|
|
|
|
{
|
2017-06-06 23:24:18 +02:00
|
|
|
const std::string hash = connection.Key.PublicKeyHash();
|
2020-04-28 20:33:30 +02:00
|
|
|
if (ProcessPlayerAuthenticatePluginHooks(connection, name, hash))
|
|
|
|
{
|
|
|
|
connection.AuthStatus = NETWORK_AUTH_OK;
|
|
|
|
Server_Client_Joined(name, hash, connection);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
connection.AuthStatus = NETWORK_AUTH_VERIFICATIONFAILURE;
|
|
|
|
}
|
2018-06-22 23:02:47 +02:00
|
|
|
}
|
|
|
|
else if (connection.AuthStatus != NETWORK_AUTH_REQUIREPASSWORD)
|
|
|
|
{
|
2017-06-06 23:24:18 +02:00
|
|
|
log_error("Unknown failure (%d) while authenticating client", connection.AuthStatus);
|
|
|
|
}
|
|
|
|
Server_Send_AUTH(connection);
|
|
|
|
}
|
2015-07-15 08:23:04 +02:00
|
|
|
}
|
|
|
|
|
2018-05-31 23:22:46 +02:00
|
|
|
void Network::Client_Handle_MAP([[maybe_unused]] NetworkConnection& connection, NetworkPacket& packet)
|
2015-07-15 06:40:22 +02:00
|
|
|
{
|
2018-06-20 17:28:51 +02:00
|
|
|
uint32_t size, offset;
|
2017-06-06 23:24:18 +02:00
|
|
|
packet >> size >> offset;
|
2020-04-22 17:09:29 +02:00
|
|
|
int32_t chunksize = static_cast<int32_t>(packet.Size - packet.BytesRead);
|
2018-06-22 23:02:47 +02:00
|
|
|
if (chunksize <= 0)
|
|
|
|
{
|
2017-06-06 23:24:18 +02:00
|
|
|
return;
|
|
|
|
}
|
2019-08-20 23:51:12 +02:00
|
|
|
if (offset == 0)
|
|
|
|
{
|
|
|
|
// Start of a new map load, clear the queue now as we have to buffer them
|
|
|
|
// until the map is fully loaded.
|
|
|
|
GameActions::ClearQueue();
|
2019-10-05 13:09:21 +02:00
|
|
|
GameActions::SuspendQueue();
|
|
|
|
|
2019-08-21 00:19:52 +02:00
|
|
|
_serverTickData.clear();
|
|
|
|
_clientMapLoaded = false;
|
2019-08-20 23:51:12 +02:00
|
|
|
}
|
2018-06-22 23:02:47 +02:00
|
|
|
if (size > chunk_buffer.size())
|
|
|
|
{
|
2017-06-06 23:24:18 +02:00
|
|
|
chunk_buffer.resize(size);
|
|
|
|
}
|
|
|
|
char str_downloading_map[256];
|
2018-07-07 14:40:56 +02:00
|
|
|
uint32_t downloading_map_args[2] = {
|
|
|
|
(offset + chunksize) / 1024,
|
|
|
|
size / 1024,
|
|
|
|
};
|
2017-06-06 23:24:18 +02:00
|
|
|
format_string(str_downloading_map, 256, STR_MULTIPLAYER_DOWNLOADING_MAP, downloading_map_args);
|
2017-10-21 19:30:03 +02:00
|
|
|
|
|
|
|
auto intent = Intent(WC_NETWORK_STATUS);
|
2018-06-22 23:02:47 +02:00
|
|
|
intent.putExtra(INTENT_EXTRA_MESSAGE, std::string{ str_downloading_map });
|
|
|
|
intent.putExtra(INTENT_EXTRA_CALLBACK, []() -> void { gNetwork.Close(); });
|
2017-10-21 19:30:03 +02:00
|
|
|
context_open_intent(&intent);
|
|
|
|
|
2020-06-07 23:18:11 +02:00
|
|
|
std::memcpy(&chunk_buffer[offset], const_cast<void*>(static_cast<const void*>(packet.Read(chunksize))), chunksize);
|
2018-06-22 23:02:47 +02:00
|
|
|
if (offset + chunksize == size)
|
|
|
|
{
|
2019-10-05 13:09:21 +02:00
|
|
|
// Allow queue processing of game actions again.
|
|
|
|
GameActions::ResumeQueue();
|
|
|
|
|
2017-10-21 19:30:03 +02:00
|
|
|
context_force_close_window_by_class(WC_NETWORK_STATUS);
|
2017-06-06 23:24:18 +02:00
|
|
|
bool has_to_free = false;
|
2018-06-22 23:02:47 +02:00
|
|
|
uint8_t* data = &chunk_buffer[0];
|
2017-06-06 23:24:18 +02:00
|
|
|
size_t data_size = size;
|
|
|
|
// zlib-compressed
|
2020-04-22 17:09:29 +02:00
|
|
|
if (strcmp("open2_sv6_zlib", reinterpret_cast<char*>(&chunk_buffer[0])) == 0)
|
2017-06-06 23:24:18 +02:00
|
|
|
{
|
|
|
|
log_verbose("Received zlib-compressed sv6 map");
|
|
|
|
has_to_free = true;
|
|
|
|
size_t header_len = strlen("open2_sv6_zlib") + 1;
|
|
|
|
data = util_zlib_inflate(&chunk_buffer[header_len], size - header_len, &data_size);
|
2017-08-15 10:07:44 +02:00
|
|
|
if (data == nullptr)
|
2017-06-06 23:24:18 +02:00
|
|
|
{
|
|
|
|
log_warning("Failed to decompress data sent from server.");
|
|
|
|
Close();
|
|
|
|
return;
|
|
|
|
}
|
2018-06-22 23:02:47 +02:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2017-06-06 23:24:18 +02:00
|
|
|
log_verbose("Assuming received map is in plain sv6 format");
|
|
|
|
}
|
|
|
|
|
|
|
|
auto ms = MemoryStream(data, data_size);
|
|
|
|
if (LoadMap(&ms))
|
|
|
|
{
|
|
|
|
game_load_init();
|
2020-02-22 23:18:22 +01:00
|
|
|
game_load_scripts();
|
2019-05-11 21:31:34 +02:00
|
|
|
_serverState.tick = gCurrentTicks;
|
2017-06-06 23:24:18 +02:00
|
|
|
// window_network_status_open("Loaded new map from network");
|
2019-05-11 21:31:34 +02:00
|
|
|
_serverState.state = NETWORK_SERVER_STATE_OK;
|
2019-03-12 14:24:48 +01:00
|
|
|
_clientMapLoaded = true;
|
2017-06-13 08:05:16 +02:00
|
|
|
gFirstTimeSaving = true;
|
2017-06-06 23:24:18 +02:00
|
|
|
|
|
|
|
// Notify user he is now online and which shortcut key enables chat
|
|
|
|
network_chat_show_connected_message();
|
2017-07-06 03:20:51 +02:00
|
|
|
|
|
|
|
// Fix invalid vehicle sprite sizes, thus preventing visual corruption of sprites
|
|
|
|
fix_invalid_vehicle_sprite_sizes();
|
2017-06-06 23:24:18 +02:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2018-06-22 23:02:47 +02:00
|
|
|
// Something went wrong, game is not loaded. Return to main screen.
|
2019-02-22 10:08:04 +01:00
|
|
|
auto loadOrQuitAction = LoadOrQuitAction(LoadOrQuitModes::OpenSavePrompt, PM_SAVE_BEFORE_QUIT);
|
2019-02-21 10:34:30 +01:00
|
|
|
GameActions::Execute(&loadOrQuitAction);
|
2017-06-06 23:24:18 +02:00
|
|
|
}
|
|
|
|
if (has_to_free)
|
|
|
|
{
|
|
|
|
free(data);
|
|
|
|
}
|
|
|
|
}
|
2015-07-15 06:40:22 +02:00
|
|
|
}
|
|
|
|
|
2018-06-22 23:02:47 +02:00
|
|
|
bool Network::LoadMap(IStream* stream)
|
2017-02-08 19:16:33 +01:00
|
|
|
{
|
2017-06-06 23:24:18 +02:00
|
|
|
bool result = false;
|
|
|
|
try
|
|
|
|
{
|
2018-04-10 14:19:23 +02:00
|
|
|
auto context = GetContext();
|
2018-08-29 00:52:10 +02:00
|
|
|
auto& objManager = context->GetObjectManager();
|
2018-08-29 09:27:35 +02:00
|
|
|
auto importer = ParkImporter::CreateS6(context->GetObjectRepository());
|
2018-05-30 19:21:07 +02:00
|
|
|
auto loadResult = importer->LoadFromStream(stream, false);
|
2018-08-29 00:52:10 +02:00
|
|
|
objManager.LoadObjects(loadResult.RequiredObjects.data(), loadResult.RequiredObjects.size());
|
2017-06-06 23:24:18 +02:00
|
|
|
importer->Import();
|
|
|
|
|
|
|
|
sprite_position_tween_reset();
|
2019-08-16 19:05:49 +02:00
|
|
|
AutoCreateMapAnimations();
|
2017-06-06 23:24:18 +02:00
|
|
|
|
|
|
|
// Read checksum
|
2018-06-20 17:28:51 +02:00
|
|
|
[[maybe_unused]] uint32_t checksum = stream->ReadValue<uint32_t>();
|
2017-06-06 23:24:18 +02:00
|
|
|
|
|
|
|
// Read other data not in normal save files
|
2018-06-20 17:28:51 +02:00
|
|
|
gGamePaused = stream->ReadValue<uint32_t>();
|
|
|
|
_guestGenerationProbability = stream->ReadValue<uint32_t>();
|
|
|
|
_suggestedGuestMaximum = stream->ReadValue<uint32_t>();
|
2020-04-25 01:07:36 +02:00
|
|
|
gCheatsAllowTrackPlaceInvalidHeights = stream->ReadValue<uint8_t>() != 0;
|
2019-06-11 05:10:05 +02:00
|
|
|
gCheatsEnableAllDrawableTrackPieces = stream->ReadValue<uint8_t>() != 0;
|
2018-06-20 17:28:51 +02:00
|
|
|
gCheatsSandboxMode = stream->ReadValue<uint8_t>() != 0;
|
|
|
|
gCheatsDisableClearanceChecks = stream->ReadValue<uint8_t>() != 0;
|
|
|
|
gCheatsDisableSupportLimits = stream->ReadValue<uint8_t>() != 0;
|
|
|
|
gCheatsDisableTrainLengthLimit = stream->ReadValue<uint8_t>() != 0;
|
|
|
|
gCheatsEnableChainLiftOnAllTrack = stream->ReadValue<uint8_t>() != 0;
|
|
|
|
gCheatsShowAllOperatingModes = stream->ReadValue<uint8_t>() != 0;
|
|
|
|
gCheatsShowVehiclesFromOtherTrackTypes = stream->ReadValue<uint8_t>() != 0;
|
|
|
|
gCheatsFastLiftHill = stream->ReadValue<uint8_t>() != 0;
|
|
|
|
gCheatsDisableBrakesFailure = stream->ReadValue<uint8_t>() != 0;
|
|
|
|
gCheatsDisableAllBreakdowns = stream->ReadValue<uint8_t>() != 0;
|
|
|
|
gCheatsBuildInPauseMode = stream->ReadValue<uint8_t>() != 0;
|
|
|
|
gCheatsIgnoreRideIntensity = stream->ReadValue<uint8_t>() != 0;
|
|
|
|
gCheatsDisableVandalism = stream->ReadValue<uint8_t>() != 0;
|
|
|
|
gCheatsDisableLittering = stream->ReadValue<uint8_t>() != 0;
|
|
|
|
gCheatsNeverendingMarketing = stream->ReadValue<uint8_t>() != 0;
|
2018-11-10 14:30:41 +01:00
|
|
|
gCheatsFreezeWeather = stream->ReadValue<uint8_t>() != 0;
|
2018-06-20 17:28:51 +02:00
|
|
|
gCheatsDisablePlantAging = stream->ReadValue<uint8_t>() != 0;
|
|
|
|
gCheatsAllowArbitraryRideTypeChanges = stream->ReadValue<uint8_t>() != 0;
|
|
|
|
gCheatsDisableRideValueAging = stream->ReadValue<uint8_t>() != 0;
|
|
|
|
gConfigGeneral.show_real_names_of_guests = stream->ReadValue<uint8_t>() != 0;
|
|
|
|
gCheatsIgnoreResearchStatus = stream->ReadValue<uint8_t>() != 0;
|
2018-03-24 05:50:59 +01:00
|
|
|
|
2017-06-06 23:24:18 +02:00
|
|
|
gLastAutoSaveUpdate = AUTOSAVE_PAUSE;
|
|
|
|
result = true;
|
|
|
|
}
|
2018-06-22 23:02:47 +02:00
|
|
|
catch (const std::exception&)
|
2017-06-06 23:24:18 +02:00
|
|
|
{
|
|
|
|
}
|
|
|
|
return result;
|
2017-02-08 19:16:33 +01:00
|
|
|
}
|
|
|
|
|
2018-06-22 23:02:47 +02:00
|
|
|
bool Network::SaveMap(IStream* stream, const std::vector<const ObjectRepositoryItem*>& objects) const
|
2017-02-08 19:16:33 +01:00
|
|
|
{
|
2017-06-06 23:24:18 +02:00
|
|
|
bool result = false;
|
2019-08-23 21:05:00 +02:00
|
|
|
map_reorganise_elements();
|
2017-06-06 23:24:18 +02:00
|
|
|
viewport_set_saved_view();
|
|
|
|
try
|
|
|
|
{
|
|
|
|
auto s6exporter = std::make_unique<S6Exporter>();
|
|
|
|
s6exporter->ExportObjectsList = objects;
|
|
|
|
s6exporter->Export();
|
|
|
|
s6exporter->SaveGame(stream);
|
|
|
|
|
|
|
|
// Write other data not in normal save files
|
2018-06-20 17:28:51 +02:00
|
|
|
stream->WriteValue<uint32_t>(gGamePaused);
|
|
|
|
stream->WriteValue<uint32_t>(_guestGenerationProbability);
|
|
|
|
stream->WriteValue<uint32_t>(_suggestedGuestMaximum);
|
2020-04-25 01:07:36 +02:00
|
|
|
stream->WriteValue<uint8_t>(gCheatsAllowTrackPlaceInvalidHeights);
|
2019-06-11 05:10:05 +02:00
|
|
|
stream->WriteValue<uint8_t>(gCheatsEnableAllDrawableTrackPieces);
|
2018-06-20 17:28:51 +02:00
|
|
|
stream->WriteValue<uint8_t>(gCheatsSandboxMode);
|
|
|
|
stream->WriteValue<uint8_t>(gCheatsDisableClearanceChecks);
|
|
|
|
stream->WriteValue<uint8_t>(gCheatsDisableSupportLimits);
|
|
|
|
stream->WriteValue<uint8_t>(gCheatsDisableTrainLengthLimit);
|
|
|
|
stream->WriteValue<uint8_t>(gCheatsEnableChainLiftOnAllTrack);
|
|
|
|
stream->WriteValue<uint8_t>(gCheatsShowAllOperatingModes);
|
|
|
|
stream->WriteValue<uint8_t>(gCheatsShowVehiclesFromOtherTrackTypes);
|
|
|
|
stream->WriteValue<uint8_t>(gCheatsFastLiftHill);
|
|
|
|
stream->WriteValue<uint8_t>(gCheatsDisableBrakesFailure);
|
|
|
|
stream->WriteValue<uint8_t>(gCheatsDisableAllBreakdowns);
|
|
|
|
stream->WriteValue<uint8_t>(gCheatsBuildInPauseMode);
|
|
|
|
stream->WriteValue<uint8_t>(gCheatsIgnoreRideIntensity);
|
|
|
|
stream->WriteValue<uint8_t>(gCheatsDisableVandalism);
|
|
|
|
stream->WriteValue<uint8_t>(gCheatsDisableLittering);
|
|
|
|
stream->WriteValue<uint8_t>(gCheatsNeverendingMarketing);
|
2018-11-10 14:30:41 +01:00
|
|
|
stream->WriteValue<uint8_t>(gCheatsFreezeWeather);
|
2018-06-20 17:28:51 +02:00
|
|
|
stream->WriteValue<uint8_t>(gCheatsDisablePlantAging);
|
|
|
|
stream->WriteValue<uint8_t>(gCheatsAllowArbitraryRideTypeChanges);
|
|
|
|
stream->WriteValue<uint8_t>(gCheatsDisableRideValueAging);
|
|
|
|
stream->WriteValue<uint8_t>(gConfigGeneral.show_real_names_of_guests);
|
|
|
|
stream->WriteValue<uint8_t>(gCheatsIgnoreResearchStatus);
|
2017-06-06 23:24:18 +02:00
|
|
|
|
|
|
|
result = true;
|
|
|
|
}
|
2018-06-22 23:02:47 +02:00
|
|
|
catch (const std::exception&)
|
2017-06-06 23:24:18 +02:00
|
|
|
{
|
|
|
|
}
|
|
|
|
return result;
|
2017-02-08 19:16:33 +01:00
|
|
|
}
|
|
|
|
|
2018-05-31 23:22:46 +02:00
|
|
|
void Network::Client_Handle_CHAT([[maybe_unused]] NetworkConnection& connection, NetworkPacket& packet)
|
2015-07-15 06:40:22 +02:00
|
|
|
{
|
2017-06-06 23:24:18 +02:00
|
|
|
const char* text = packet.ReadString();
|
2018-06-22 23:02:47 +02:00
|
|
|
if (text)
|
|
|
|
{
|
2017-06-06 23:24:18 +02:00
|
|
|
chat_history_add(text);
|
|
|
|
}
|
2015-07-15 06:40:22 +02:00
|
|
|
}
|
|
|
|
|
2020-04-28 18:31:12 +02:00
|
|
|
static bool ProcessChatMessagePluginHooks(uint8_t playerId, std::string& text)
|
2020-04-22 23:53:32 +02:00
|
|
|
{
|
2020-04-24 16:58:01 +02:00
|
|
|
# ifdef ENABLE_SCRIPTING
|
2020-04-22 23:53:32 +02:00
|
|
|
auto& hookEngine = GetContext()->GetScriptEngine().GetHookEngine();
|
|
|
|
if (hookEngine.HasSubscriptions(OpenRCT2::Scripting::HOOK_TYPE::NETWORK_CHAT))
|
|
|
|
{
|
|
|
|
auto ctx = GetContext()->GetScriptEngine().GetContext();
|
|
|
|
|
|
|
|
// Create event args object
|
|
|
|
auto objIdx = duk_push_object(ctx);
|
2020-04-28 18:31:12 +02:00
|
|
|
duk_push_number(ctx, playerId);
|
2020-04-22 23:53:32 +02:00
|
|
|
duk_put_prop_string(ctx, objIdx, "player");
|
|
|
|
duk_push_string(ctx, text.c_str());
|
|
|
|
duk_put_prop_string(ctx, objIdx, "message");
|
|
|
|
auto e = DukValue::take_from_stack(ctx);
|
|
|
|
|
|
|
|
// Call the subscriptions
|
|
|
|
hookEngine.Call(OpenRCT2::Scripting::HOOK_TYPE::NETWORK_CHAT, e, false);
|
|
|
|
|
|
|
|
// Update text from object if subscriptions changed it
|
|
|
|
if (e["message"].type() != DukValue::Type::STRING)
|
|
|
|
{
|
|
|
|
// Subscription set text to non-string, do not relay message
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
text = e["message"].as_string();
|
|
|
|
if (text.empty())
|
|
|
|
{
|
|
|
|
// Subscription set text to empty string, do not relay message
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
# endif
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2016-01-22 07:32:40 +01:00
|
|
|
void Network::Server_Handle_CHAT(NetworkConnection& connection, NetworkPacket& packet)
|
2015-07-15 08:23:04 +02:00
|
|
|
{
|
2020-02-11 00:33:00 +01:00
|
|
|
auto szText = packet.ReadString();
|
|
|
|
if (szText == nullptr || szText[0] == '\0')
|
|
|
|
return;
|
|
|
|
|
2018-06-22 23:02:47 +02:00
|
|
|
if (connection.Player)
|
|
|
|
{
|
2017-06-06 23:24:18 +02:00
|
|
|
NetworkGroup* group = GetGroupByID(connection.Player->Group);
|
2018-06-22 23:02:47 +02:00
|
|
|
if (!group || !group->CanPerformCommand(MISC_COMMAND_CHAT))
|
|
|
|
{
|
2017-06-06 23:24:18 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
2020-02-11 00:33:00 +01:00
|
|
|
|
|
|
|
std::string text = szText;
|
|
|
|
if (connection.Player != nullptr)
|
2018-06-22 23:02:47 +02:00
|
|
|
{
|
2020-04-28 18:31:12 +02:00
|
|
|
if (!ProcessChatMessagePluginHooks(connection.Player->Id, text))
|
2020-02-11 00:33:00 +01:00
|
|
|
{
|
2020-04-22 23:53:32 +02:00
|
|
|
// Message not to be relayed
|
|
|
|
return;
|
2020-02-11 00:33:00 +01:00
|
|
|
}
|
2017-06-06 23:24:18 +02:00
|
|
|
}
|
2020-02-11 00:33:00 +01:00
|
|
|
|
|
|
|
const char* formatted = FormatChat(connection.Player, text.c_str());
|
|
|
|
chat_history_add(formatted);
|
|
|
|
Server_Send_CHAT(formatted);
|
2015-07-15 08:23:04 +02:00
|
|
|
}
|
|
|
|
|
2018-05-31 23:22:46 +02:00
|
|
|
void Network::Client_Handle_GAME_ACTION([[maybe_unused]] NetworkConnection& connection, NetworkPacket& packet)
|
2017-03-23 20:49:19 +01:00
|
|
|
{
|
2018-06-20 17:28:51 +02:00
|
|
|
uint32_t tick;
|
2019-02-07 18:33:46 +01:00
|
|
|
uint32_t actionType;
|
|
|
|
packet >> tick >> actionType;
|
2017-07-15 11:24:08 +02:00
|
|
|
|
2017-07-21 19:21:18 +02:00
|
|
|
MemoryStream stream;
|
|
|
|
size_t size = packet.Size - packet.BytesRead;
|
|
|
|
stream.WriteArray(packet.Read(size), size);
|
|
|
|
stream.SetPosition(0);
|
2017-07-15 11:24:08 +02:00
|
|
|
|
|
|
|
DataSerialiser ds(false, stream);
|
|
|
|
|
2019-02-07 18:33:46 +01:00
|
|
|
GameAction::Ptr action = GameActions::Create(actionType);
|
|
|
|
if (action == nullptr)
|
2017-07-15 11:24:08 +02:00
|
|
|
{
|
2019-02-07 18:33:46 +01:00
|
|
|
log_error("Received unregistered game action type: 0x%08X", actionType);
|
|
|
|
return;
|
2017-07-15 11:24:08 +02:00
|
|
|
}
|
|
|
|
action->Serialise(ds);
|
|
|
|
|
2018-11-20 06:04:42 +01:00
|
|
|
if (player_id == action->GetPlayer().id)
|
2017-07-17 21:27:21 +02:00
|
|
|
{
|
2018-03-24 05:50:59 +01:00
|
|
|
// Only execute callbacks that belong to us,
|
2017-10-02 18:02:50 +02:00
|
|
|
// clients can have identical network ids assigned.
|
|
|
|
auto itr = _gameActionCallbacks.find(action->GetNetworkId());
|
|
|
|
if (itr != _gameActionCallbacks.end())
|
|
|
|
{
|
|
|
|
action->SetCallback(itr->second);
|
|
|
|
_gameActionCallbacks.erase(itr);
|
|
|
|
}
|
2017-07-17 21:27:21 +02:00
|
|
|
}
|
|
|
|
|
2019-08-20 23:51:12 +02:00
|
|
|
GameActions::Enqueue(std::move(action), tick);
|
2017-03-23 20:49:19 +01:00
|
|
|
}
|
2015-07-15 06:40:22 +02:00
|
|
|
|
2017-03-23 20:49:19 +01:00
|
|
|
void Network::Server_Handle_GAME_ACTION(NetworkConnection& connection, NetworkPacket& packet)
|
|
|
|
{
|
2018-06-20 17:28:51 +02:00
|
|
|
uint32_t tick;
|
2019-02-07 18:33:46 +01:00
|
|
|
uint32_t actionType;
|
2017-07-21 19:21:18 +02:00
|
|
|
|
2019-03-01 09:25:16 +01:00
|
|
|
NetworkPlayer* player = connection.Player;
|
|
|
|
if (player == nullptr)
|
2018-06-22 23:02:47 +02:00
|
|
|
{
|
2017-07-21 19:21:18 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2019-02-07 18:33:46 +01:00
|
|
|
packet >> tick >> actionType;
|
2017-07-21 19:21:18 +02:00
|
|
|
|
2019-03-01 09:25:16 +01:00
|
|
|
// Don't let clients send pause or quit
|
|
|
|
if (actionType == GAME_COMMAND_TOGGLE_PAUSE || actionType == GAME_COMMAND_LOAD_OR_QUIT)
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2020-03-01 22:56:49 +01:00
|
|
|
if (actionType != GAME_COMMAND_CUSTOM)
|
2018-06-22 23:02:47 +02:00
|
|
|
{
|
2020-03-01 22:56:49 +01:00
|
|
|
// Check if player's group permission allows command to run
|
|
|
|
NetworkGroup* group = GetGroupByID(connection.Player->Group);
|
|
|
|
if (group == nullptr || group->CanPerformCommand(actionType) == false)
|
|
|
|
{
|
|
|
|
Server_Send_SHOWERROR(connection, STR_CANT_DO_THIS, STR_PERMISSION_DENIED);
|
|
|
|
return;
|
|
|
|
}
|
2017-07-21 19:21:18 +02:00
|
|
|
}
|
|
|
|
|
2019-03-01 09:25:16 +01:00
|
|
|
// Create and enqueue the action.
|
|
|
|
GameAction::Ptr ga = GameActions::Create(actionType);
|
|
|
|
if (ga == nullptr)
|
2018-06-22 23:02:47 +02:00
|
|
|
{
|
2019-03-01 09:25:16 +01:00
|
|
|
log_error(
|
|
|
|
"Received unregistered game action type: 0x%08X from player: (%d) %s", actionType, connection.Player->Id,
|
|
|
|
connection.Player->Name.c_str());
|
|
|
|
return;
|
2017-07-21 19:21:18 +02:00
|
|
|
}
|
2019-03-01 09:25:16 +01:00
|
|
|
|
2019-03-01 11:01:00 +01:00
|
|
|
// Player who is hosting is not affected by cooldowns.
|
|
|
|
if ((player->Flags & NETWORK_PLAYER_FLAG_ISSERVER) == 0)
|
2018-06-22 23:02:47 +02:00
|
|
|
{
|
2019-03-01 11:01:00 +01:00
|
|
|
auto cooldownIt = player->CooldownTime.find(actionType);
|
|
|
|
if (cooldownIt != std::end(player->CooldownTime))
|
2018-06-22 23:02:47 +02:00
|
|
|
{
|
2019-03-01 11:01:00 +01:00
|
|
|
if (cooldownIt->second > 0)
|
|
|
|
{
|
|
|
|
Server_Send_SHOWERROR(connection, STR_CANT_DO_THIS, STR_NETWORK_ACTION_RATE_LIMIT_MESSAGE);
|
|
|
|
return;
|
|
|
|
}
|
2017-07-21 19:21:18 +02:00
|
|
|
}
|
2017-04-10 18:18:26 +02:00
|
|
|
|
2019-03-01 11:01:00 +01:00
|
|
|
uint32_t cooldownTime = ga->GetCooldownTime();
|
|
|
|
if (cooldownTime > 0)
|
|
|
|
{
|
|
|
|
player->CooldownTime[actionType] = cooldownTime;
|
|
|
|
}
|
2017-07-15 11:24:08 +02:00
|
|
|
}
|
2017-04-10 18:18:26 +02:00
|
|
|
|
2017-07-15 11:24:08 +02:00
|
|
|
DataSerialiser stream(false);
|
|
|
|
size_t size = packet.Size - packet.BytesRead;
|
|
|
|
stream.GetStream().WriteArray(packet.Read(size), size);
|
|
|
|
stream.GetStream().SetPosition(0);
|
2017-04-10 18:18:26 +02:00
|
|
|
|
2017-07-15 11:24:08 +02:00
|
|
|
ga->Serialise(stream);
|
|
|
|
// Set player to sender, should be 0 if sent from client.
|
2018-11-20 06:04:42 +01:00
|
|
|
ga->SetPlayer(NetworkPlayerId_t{ connection.Player->Id });
|
2017-04-10 18:18:26 +02:00
|
|
|
|
2019-08-20 23:51:12 +02:00
|
|
|
GameActions::Enqueue(std::move(ga), tick);
|
2017-03-23 20:49:19 +01:00
|
|
|
}
|
2017-04-10 18:18:26 +02:00
|
|
|
|
2018-05-31 23:22:46 +02:00
|
|
|
void Network::Client_Handle_TICK([[maybe_unused]] NetworkConnection& connection, NetworkPacket& packet)
|
2015-07-15 06:40:22 +02:00
|
|
|
{
|
2018-06-20 17:28:51 +02:00
|
|
|
uint32_t srand0;
|
|
|
|
uint32_t flags;
|
2019-05-11 21:31:34 +02:00
|
|
|
uint32_t serverTick;
|
|
|
|
|
|
|
|
packet >> serverTick >> srand0 >> flags;
|
2019-03-12 14:22:31 +01:00
|
|
|
|
2019-03-12 21:07:48 +01:00
|
|
|
ServerTickData_t tickData;
|
|
|
|
tickData.srand0 = srand0;
|
2019-05-11 21:31:34 +02:00
|
|
|
tickData.tick = serverTick;
|
2019-03-12 21:07:48 +01:00
|
|
|
|
2019-03-12 14:22:31 +01:00
|
|
|
if (flags & NETWORK_TICK_FLAG_CHECKSUMS)
|
2018-06-22 23:02:47 +02:00
|
|
|
{
|
2019-03-12 14:22:31 +01:00
|
|
|
const char* text = packet.ReadString();
|
|
|
|
if (text != nullptr)
|
2017-06-06 23:24:18 +02:00
|
|
|
{
|
2019-03-12 21:07:48 +01:00
|
|
|
tickData.spriteHash = text;
|
2017-06-06 23:24:18 +02:00
|
|
|
}
|
|
|
|
}
|
2019-03-12 21:07:48 +01:00
|
|
|
|
|
|
|
// Don't let the history grow too much.
|
|
|
|
while (_serverTickData.size() >= 100)
|
|
|
|
{
|
|
|
|
_serverTickData.erase(_serverTickData.begin());
|
|
|
|
}
|
|
|
|
|
2019-05-11 21:31:34 +02:00
|
|
|
_serverState.tick = serverTick;
|
|
|
|
_serverTickData.emplace(serverTick, tickData);
|
2015-07-15 06:40:22 +02:00
|
|
|
}
|
|
|
|
|
2019-03-24 23:16:57 +01:00
|
|
|
void Network::Client_Handle_PLAYERINFO([[maybe_unused]] NetworkConnection& connection, NetworkPacket& packet)
|
|
|
|
{
|
|
|
|
uint32_t tick;
|
|
|
|
packet >> tick;
|
|
|
|
|
|
|
|
NetworkPlayer playerInfo;
|
|
|
|
playerInfo.Read(packet);
|
|
|
|
|
|
|
|
_pendingPlayerInfo.emplace(tick, playerInfo);
|
|
|
|
}
|
|
|
|
|
2018-05-31 23:22:46 +02:00
|
|
|
void Network::Client_Handle_PLAYERLIST([[maybe_unused]] NetworkConnection& connection, NetworkPacket& packet)
|
2015-07-16 19:32:43 +02:00
|
|
|
{
|
2018-12-09 20:39:44 +01:00
|
|
|
uint32_t tick;
|
2018-06-20 17:28:51 +02:00
|
|
|
uint8_t size;
|
2018-12-09 20:39:44 +01:00
|
|
|
packet >> tick >> size;
|
|
|
|
|
2019-05-15 19:55:16 +02:00
|
|
|
auto& pending = _pendingPlayerLists[tick];
|
|
|
|
pending.players.clear();
|
2018-12-09 20:39:44 +01:00
|
|
|
|
2018-06-22 23:02:47 +02:00
|
|
|
for (uint32_t i = 0; i < size; i++)
|
|
|
|
{
|
2017-06-06 23:24:18 +02:00
|
|
|
NetworkPlayer tempplayer;
|
|
|
|
tempplayer.Read(packet);
|
2018-12-09 20:39:44 +01:00
|
|
|
|
2019-05-15 19:55:16 +02:00
|
|
|
pending.players.push_back(tempplayer);
|
2017-06-06 23:24:18 +02:00
|
|
|
}
|
2015-07-17 02:24:39 +02:00
|
|
|
}
|
|
|
|
|
2018-05-31 23:22:46 +02:00
|
|
|
void Network::Client_Handle_PING([[maybe_unused]] NetworkConnection& connection, [[maybe_unused]] NetworkPacket& packet)
|
2015-07-17 02:24:39 +02:00
|
|
|
{
|
2017-06-06 23:24:18 +02:00
|
|
|
Client_Send_PING();
|
2015-07-17 02:24:39 +02:00
|
|
|
}
|
|
|
|
|
2018-05-31 23:22:46 +02:00
|
|
|
void Network::Server_Handle_PING(NetworkConnection& connection, [[maybe_unused]] NetworkPacket& packet)
|
2015-07-17 02:24:39 +02:00
|
|
|
{
|
2018-06-20 17:28:51 +02:00
|
|
|
int32_t ping = platform_get_ticks() - connection.PingTime;
|
2018-06-22 23:02:47 +02:00
|
|
|
if (ping < 0)
|
|
|
|
{
|
2017-06-06 23:24:18 +02:00
|
|
|
ping = 0;
|
|
|
|
}
|
2018-06-22 23:02:47 +02:00
|
|
|
if (connection.Player)
|
|
|
|
{
|
2017-06-06 23:24:18 +02:00
|
|
|
connection.Player->Ping = ping;
|
|
|
|
window_invalidate_by_number(WC_PLAYER, connection.Player->Id);
|
|
|
|
}
|
2015-07-17 02:24:39 +02:00
|
|
|
}
|
|
|
|
|
2018-05-31 23:22:46 +02:00
|
|
|
void Network::Client_Handle_PINGLIST([[maybe_unused]] NetworkConnection& connection, NetworkPacket& packet)
|
2015-07-17 02:24:39 +02:00
|
|
|
{
|
2018-06-20 17:28:51 +02:00
|
|
|
uint8_t size;
|
2017-06-06 23:24:18 +02:00
|
|
|
packet >> size;
|
2018-06-22 23:02:47 +02:00
|
|
|
for (uint32_t i = 0; i < size; i++)
|
|
|
|
{
|
2018-06-20 17:28:51 +02:00
|
|
|
uint8_t id;
|
|
|
|
uint16_t ping;
|
2017-06-06 23:24:18 +02:00
|
|
|
packet >> id >> ping;
|
|
|
|
NetworkPlayer* player = GetPlayerByID(id);
|
2018-06-22 23:02:47 +02:00
|
|
|
if (player)
|
|
|
|
{
|
2017-06-06 23:24:18 +02:00
|
|
|
player->Ping = ping;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
window_invalidate_by_class(WC_PLAYER);
|
2015-07-16 19:32:43 +02:00
|
|
|
}
|
|
|
|
|
2016-01-22 07:32:40 +01:00
|
|
|
void Network::Client_Handle_SETDISCONNECTMSG(NetworkConnection& connection, NetworkPacket& packet)
|
2015-11-01 08:50:44 +01:00
|
|
|
{
|
2017-06-06 23:24:18 +02:00
|
|
|
static std::string msg;
|
|
|
|
const char* disconnectmsg = packet.ReadString();
|
2018-06-22 23:02:47 +02:00
|
|
|
if (disconnectmsg)
|
|
|
|
{
|
2017-06-06 23:24:18 +02:00
|
|
|
msg = disconnectmsg;
|
|
|
|
connection.SetLastDisconnectReason(msg.c_str());
|
|
|
|
}
|
2015-11-01 08:50:44 +01:00
|
|
|
}
|
|
|
|
|
2018-05-31 23:22:46 +02:00
|
|
|
void Network::Server_Handle_GAMEINFO(NetworkConnection& connection, [[maybe_unused]] NetworkPacket& packet)
|
2015-11-04 00:31:09 +01:00
|
|
|
{
|
2017-06-06 23:24:18 +02:00
|
|
|
Server_Send_GAMEINFO(connection);
|
2015-11-04 00:31:09 +01:00
|
|
|
}
|
|
|
|
|
2018-05-31 23:22:46 +02:00
|
|
|
void Network::Client_Handle_SHOWERROR([[maybe_unused]] NetworkConnection& connection, NetworkPacket& packet)
|
2016-01-20 23:45:09 +01:00
|
|
|
{
|
2017-06-06 23:24:18 +02:00
|
|
|
rct_string_id title, message;
|
|
|
|
packet >> title >> message;
|
2017-09-06 16:09:35 +02:00
|
|
|
context_show_error(title, message);
|
2016-01-22 07:32:40 +01:00
|
|
|
}
|
|
|
|
|
2018-05-31 23:22:46 +02:00
|
|
|
void Network::Client_Handle_GROUPLIST([[maybe_unused]] NetworkConnection& connection, NetworkPacket& packet)
|
2016-01-22 07:32:40 +01:00
|
|
|
{
|
2017-06-06 23:24:18 +02:00
|
|
|
group_list.clear();
|
2018-06-20 17:28:51 +02:00
|
|
|
uint8_t size;
|
2017-06-06 23:24:18 +02:00
|
|
|
packet >> size >> default_group;
|
2018-06-22 23:02:47 +02:00
|
|
|
for (uint32_t i = 0; i < size; i++)
|
|
|
|
{
|
2017-06-06 23:24:18 +02:00
|
|
|
NetworkGroup group;
|
|
|
|
group.Read(packet);
|
2018-05-12 15:13:48 +02:00
|
|
|
auto newgroup = std::make_unique<NetworkGroup>(group);
|
2017-06-06 23:24:18 +02:00
|
|
|
group_list.push_back(std::move(newgroup));
|
|
|
|
}
|
2016-01-20 23:45:09 +01:00
|
|
|
}
|
|
|
|
|
2018-05-31 23:22:46 +02:00
|
|
|
void Network::Client_Handle_EVENT([[maybe_unused]] NetworkConnection& connection, NetworkPacket& packet)
|
2016-02-27 14:12:04 +01:00
|
|
|
{
|
2017-06-06 23:24:18 +02:00
|
|
|
char text[256];
|
2018-06-20 17:28:51 +02:00
|
|
|
uint16_t eventType;
|
2017-06-06 23:24:18 +02:00
|
|
|
packet >> eventType;
|
2018-06-22 23:02:47 +02:00
|
|
|
switch (eventType)
|
2017-06-06 23:24:18 +02:00
|
|
|
{
|
2018-06-22 23:02:47 +02:00
|
|
|
case SERVER_EVENT_PLAYER_JOINED:
|
|
|
|
{
|
|
|
|
const char* playerName = packet.ReadString();
|
|
|
|
format_string(text, 256, STR_MULTIPLAYER_PLAYER_HAS_JOINED_THE_GAME, &playerName);
|
|
|
|
chat_history_add(text);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case SERVER_EVENT_PLAYER_DISCONNECTED:
|
|
|
|
{
|
|
|
|
const char* playerName = packet.ReadString();
|
|
|
|
const char* reason = packet.ReadString();
|
|
|
|
const char* args[] = { playerName, reason };
|
|
|
|
if (str_is_null_or_empty(reason))
|
|
|
|
{
|
|
|
|
format_string(text, 256, STR_MULTIPLAYER_PLAYER_HAS_DISCONNECTED_NO_REASON, args);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
format_string(text, 256, STR_MULTIPLAYER_PLAYER_HAS_DISCONNECTED_WITH_REASON, args);
|
|
|
|
}
|
|
|
|
chat_history_add(text);
|
|
|
|
break;
|
2017-06-06 23:24:18 +02:00
|
|
|
}
|
|
|
|
}
|
2016-02-27 14:12:04 +01:00
|
|
|
}
|
|
|
|
|
2016-05-30 23:25:43 +02:00
|
|
|
void Network::Client_Send_GAMEINFO()
|
|
|
|
{
|
2017-06-06 23:24:18 +02:00
|
|
|
log_verbose("requesting gameinfo");
|
|
|
|
std::unique_ptr<NetworkPacket> packet(NetworkPacket::Allocate());
|
2020-04-22 17:09:29 +02:00
|
|
|
*packet << static_cast<uint32_t>(NETWORK_COMMAND_GAMEINFO);
|
2018-12-17 12:58:12 +01:00
|
|
|
_serverConnection->QueuePacket(std::move(packet));
|
2016-05-30 23:25:43 +02:00
|
|
|
}
|
|
|
|
|
2018-06-22 23:02:47 +02:00
|
|
|
static std::string json_stdstring_value(const json_t* string)
|
2016-05-30 23:25:43 +02:00
|
|
|
{
|
2018-06-22 23:02:47 +02:00
|
|
|
const char* cstr = json_string_value(string);
|
2017-06-06 23:24:18 +02:00
|
|
|
return cstr == nullptr ? std::string() : std::string(cstr);
|
2016-05-30 23:25:43 +02:00
|
|
|
}
|
|
|
|
|
2018-05-31 23:22:46 +02:00
|
|
|
void Network::Client_Handle_GAMEINFO([[maybe_unused]] NetworkConnection& connection, NetworkPacket& packet)
|
2016-05-30 23:25:43 +02:00
|
|
|
{
|
2018-06-22 23:02:47 +02:00
|
|
|
const char* jsonString = packet.ReadString();
|
2019-05-11 21:31:34 +02:00
|
|
|
packet >> _serverState.gamestateSnapshotsEnabled;
|
2016-05-30 23:25:43 +02:00
|
|
|
|
2017-06-06 23:24:18 +02:00
|
|
|
json_error_t error;
|
2018-06-22 23:02:47 +02:00
|
|
|
json_t* root = json_loads(jsonString, 0, &error);
|
2016-05-30 23:25:43 +02:00
|
|
|
|
2017-06-06 23:24:18 +02:00
|
|
|
ServerName = json_stdstring_value(json_object_get(root, "name"));
|
|
|
|
ServerDescription = json_stdstring_value(json_object_get(root, "description"));
|
|
|
|
ServerGreeting = json_stdstring_value(json_object_get(root, "greeting"));
|
2016-05-30 23:25:43 +02:00
|
|
|
|
2018-06-22 23:02:47 +02:00
|
|
|
json_t* jsonProvider = json_object_get(root, "provider");
|
|
|
|
if (jsonProvider != nullptr)
|
|
|
|
{
|
2017-06-06 23:24:18 +02:00
|
|
|
ServerProviderName = json_stdstring_value(json_object_get(jsonProvider, "name"));
|
|
|
|
ServerProviderEmail = json_stdstring_value(json_object_get(jsonProvider, "email"));
|
|
|
|
ServerProviderWebsite = json_stdstring_value(json_object_get(jsonProvider, "website"));
|
|
|
|
}
|
|
|
|
json_decref(root);
|
2016-08-14 06:15:45 +02:00
|
|
|
|
2017-06-06 23:24:18 +02:00
|
|
|
network_chat_show_server_greeting();
|
2016-05-30 23:25:43 +02:00
|
|
|
}
|
|
|
|
|
2018-04-27 19:47:57 +02:00
|
|
|
void network_set_env(const std::shared_ptr<IPlatformEnvironment>& env)
|
2015-07-10 21:53:41 +02:00
|
|
|
{
|
2018-04-20 20:58:59 +02:00
|
|
|
gNetwork.SetEnvironment(env);
|
2015-07-10 21:53:41 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void network_close()
|
|
|
|
{
|
2017-06-06 23:24:18 +02:00
|
|
|
gNetwork.Close();
|
2015-07-10 21:53:41 +02:00
|
|
|
}
|
|
|
|
|
2019-02-05 06:57:09 +01:00
|
|
|
void network_reconnect()
|
|
|
|
{
|
|
|
|
gNetwork.Reconnect();
|
|
|
|
}
|
|
|
|
|
2015-11-04 00:31:09 +01:00
|
|
|
void network_shutdown_client()
|
|
|
|
{
|
2019-05-15 19:55:16 +02:00
|
|
|
gNetwork.ServerClientDisconnected();
|
2015-11-04 00:31:09 +01:00
|
|
|
}
|
|
|
|
|
2019-02-23 15:54:17 +01:00
|
|
|
int32_t network_begin_client(const std::string& host, int32_t port)
|
2015-07-10 21:53:41 +02:00
|
|
|
{
|
2017-06-06 23:24:18 +02:00
|
|
|
return gNetwork.BeginClient(host, port);
|
2015-07-10 21:53:41 +02:00
|
|
|
}
|
|
|
|
|
2019-02-23 15:54:17 +01:00
|
|
|
int32_t network_begin_server(int32_t port, const std::string& address)
|
2015-07-10 21:53:41 +02:00
|
|
|
{
|
2017-06-06 23:24:18 +02:00
|
|
|
return gNetwork.BeginServer(port, address);
|
2015-07-10 21:53:41 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void network_update()
|
|
|
|
{
|
2017-06-06 23:24:18 +02:00
|
|
|
gNetwork.Update();
|
2015-07-10 21:53:41 +02:00
|
|
|
}
|
|
|
|
|
2018-12-09 20:39:44 +01:00
|
|
|
void network_process_pending()
|
2017-06-18 08:03:37 +02:00
|
|
|
{
|
2018-12-09 20:39:44 +01:00
|
|
|
gNetwork.ProcessPending();
|
2017-06-18 08:03:37 +02:00
|
|
|
}
|
|
|
|
|
2017-07-15 11:24:08 +02:00
|
|
|
void network_flush()
|
|
|
|
{
|
|
|
|
gNetwork.Flush();
|
|
|
|
}
|
|
|
|
|
2018-06-20 17:28:51 +02:00
|
|
|
int32_t network_get_mode()
|
2015-07-10 21:53:41 +02:00
|
|
|
{
|
2017-06-06 23:24:18 +02:00
|
|
|
return gNetwork.GetMode();
|
2015-07-10 21:53:41 +02:00
|
|
|
}
|
|
|
|
|
2018-06-20 17:28:51 +02:00
|
|
|
int32_t network_get_status()
|
2015-10-27 04:43:33 +01:00
|
|
|
{
|
2017-06-06 23:24:18 +02:00
|
|
|
return gNetwork.GetStatus();
|
2015-10-27 04:43:33 +01:00
|
|
|
}
|
|
|
|
|
2019-02-04 06:54:45 +01:00
|
|
|
bool network_is_desynchronised()
|
|
|
|
{
|
2019-02-08 01:32:05 +01:00
|
|
|
return gNetwork.IsDesynchronised();
|
2019-02-04 06:54:45 +01:00
|
|
|
}
|
|
|
|
|
2019-05-11 21:31:34 +02:00
|
|
|
bool network_check_desynchronisation()
|
2017-07-15 11:24:08 +02:00
|
|
|
{
|
|
|
|
return gNetwork.CheckDesynchronizaton();
|
|
|
|
}
|
|
|
|
|
2019-05-11 21:31:34 +02:00
|
|
|
void network_request_gamestate_snapshot()
|
|
|
|
{
|
|
|
|
return gNetwork.RequestStateSnapshot();
|
|
|
|
}
|
|
|
|
|
2017-07-15 11:24:08 +02:00
|
|
|
void network_send_tick()
|
|
|
|
{
|
|
|
|
gNetwork.Server_Send_TICK();
|
|
|
|
}
|
|
|
|
|
2018-06-20 17:28:51 +02:00
|
|
|
int32_t network_get_authstatus()
|
2015-07-16 19:32:43 +02:00
|
|
|
{
|
2017-06-06 23:24:18 +02:00
|
|
|
return gNetwork.GetAuthStatus();
|
2015-07-16 19:32:43 +02:00
|
|
|
}
|
|
|
|
|
2018-06-20 17:28:51 +02:00
|
|
|
uint32_t network_get_server_tick()
|
2015-07-10 21:53:41 +02:00
|
|
|
{
|
2017-06-06 23:24:18 +02:00
|
|
|
return gNetwork.GetServerTick();
|
2015-07-10 21:53:41 +02:00
|
|
|
}
|
|
|
|
|
2018-06-20 17:28:51 +02:00
|
|
|
uint8_t network_get_current_player_id()
|
2015-07-24 01:22:03 +02:00
|
|
|
{
|
2017-06-06 23:24:18 +02:00
|
|
|
return gNetwork.GetPlayerID();
|
2015-07-24 01:22:03 +02:00
|
|
|
}
|
|
|
|
|
2018-06-20 17:28:51 +02:00
|
|
|
int32_t network_get_num_players()
|
2015-07-16 19:32:43 +02:00
|
|
|
{
|
2020-04-22 17:09:29 +02:00
|
|
|
return static_cast<int32_t>(gNetwork.player_list.size());
|
2015-07-16 19:32:43 +02:00
|
|
|
}
|
|
|
|
|
2018-06-20 17:28:51 +02:00
|
|
|
const char* network_get_player_name(uint32_t index)
|
2015-07-16 19:32:43 +02:00
|
|
|
{
|
2020-04-22 17:09:29 +02:00
|
|
|
return static_cast<const char*>(gNetwork.player_list[index]->Name.c_str());
|
2015-07-16 19:32:43 +02:00
|
|
|
}
|
|
|
|
|
2018-06-20 17:28:51 +02:00
|
|
|
uint32_t network_get_player_flags(uint32_t index)
|
2015-07-17 02:24:39 +02:00
|
|
|
{
|
2017-06-06 23:24:18 +02:00
|
|
|
return gNetwork.player_list[index]->Flags;
|
2015-07-17 02:24:39 +02:00
|
|
|
}
|
|
|
|
|
2018-06-20 17:28:51 +02:00
|
|
|
int32_t network_get_player_ping(uint32_t index)
|
2015-07-17 02:24:39 +02:00
|
|
|
{
|
2017-06-06 23:24:18 +02:00
|
|
|
return gNetwork.player_list[index]->Ping;
|
2015-07-17 02:24:39 +02:00
|
|
|
}
|
|
|
|
|
2018-06-20 17:28:51 +02:00
|
|
|
int32_t network_get_player_id(uint32_t index)
|
2015-10-16 23:20:16 +02:00
|
|
|
{
|
2017-06-06 23:24:18 +02:00
|
|
|
return gNetwork.player_list[index]->Id;
|
2015-10-16 23:20:16 +02:00
|
|
|
}
|
|
|
|
|
2018-06-20 17:28:51 +02:00
|
|
|
money32 network_get_player_money_spent(uint32_t index)
|
2016-01-20 23:45:09 +01:00
|
|
|
{
|
2017-06-06 23:24:18 +02:00
|
|
|
return gNetwork.player_list[index]->MoneySpent;
|
2016-01-20 23:45:09 +01:00
|
|
|
}
|
|
|
|
|
2020-04-27 20:54:29 +02:00
|
|
|
std::string network_get_player_ip_address(uint32_t id)
|
|
|
|
{
|
|
|
|
auto conn = gNetwork.GetPlayerConnection(id);
|
2020-04-30 03:18:32 +02:00
|
|
|
if (conn != nullptr && conn->Socket != nullptr)
|
2020-04-27 20:54:29 +02:00
|
|
|
{
|
|
|
|
return conn->Socket->GetIpAddress();
|
|
|
|
}
|
|
|
|
return {};
|
|
|
|
}
|
|
|
|
|
|
|
|
std::string network_get_player_public_key_hash(uint32_t id)
|
|
|
|
{
|
|
|
|
auto player = gNetwork.GetPlayerByID(id);
|
|
|
|
if (player != nullptr)
|
|
|
|
{
|
|
|
|
return player->KeyHash;
|
|
|
|
}
|
|
|
|
return {};
|
|
|
|
}
|
|
|
|
|
2018-06-20 17:28:51 +02:00
|
|
|
void network_add_player_money_spent(uint32_t index, money32 cost)
|
2016-01-20 23:45:09 +01:00
|
|
|
{
|
2017-06-06 23:24:18 +02:00
|
|
|
gNetwork.player_list[index]->AddMoneySpent(cost);
|
2016-01-20 23:45:09 +01:00
|
|
|
}
|
|
|
|
|
2018-06-20 17:28:51 +02:00
|
|
|
int32_t network_get_player_last_action(uint32_t index, int32_t time)
|
2016-01-23 20:32:02 +01:00
|
|
|
{
|
2018-06-22 23:02:47 +02:00
|
|
|
if (time && platform_get_ticks() > gNetwork.player_list[index]->LastActionTime + time)
|
|
|
|
{
|
2017-06-06 23:24:18 +02:00
|
|
|
return -999;
|
|
|
|
}
|
|
|
|
return gNetwork.player_list[index]->LastAction;
|
2016-01-23 20:32:02 +01:00
|
|
|
}
|
|
|
|
|
2018-06-20 17:28:51 +02:00
|
|
|
void network_set_player_last_action(uint32_t index, int32_t command)
|
2016-01-23 20:32:02 +01:00
|
|
|
{
|
2017-06-06 23:24:18 +02:00
|
|
|
gNetwork.player_list[index]->LastAction = NetworkActions::FindCommand(command);
|
|
|
|
gNetwork.player_list[index]->LastActionTime = platform_get_ticks();
|
2016-01-23 20:32:02 +01:00
|
|
|
}
|
|
|
|
|
2019-12-19 21:48:20 +01:00
|
|
|
CoordsXYZ network_get_player_last_action_coord(uint32_t index)
|
2016-01-23 22:16:26 +01:00
|
|
|
{
|
2017-06-06 23:24:18 +02:00
|
|
|
return gNetwork.player_list[index]->LastActionCoord;
|
2016-01-23 22:16:26 +01:00
|
|
|
}
|
|
|
|
|
2020-02-29 04:30:50 +01:00
|
|
|
void network_set_player_last_action_coord(uint32_t index, const CoordsXYZ& coord)
|
2016-01-23 22:16:26 +01:00
|
|
|
{
|
2018-06-22 23:02:47 +02:00
|
|
|
if (index < gNetwork.player_list.size())
|
|
|
|
{
|
2017-06-06 23:24:18 +02:00
|
|
|
gNetwork.player_list[index]->LastActionCoord = coord;
|
|
|
|
}
|
2016-01-23 22:16:26 +01:00
|
|
|
}
|
|
|
|
|
2018-06-20 17:28:51 +02:00
|
|
|
uint32_t network_get_player_commands_ran(uint32_t index)
|
2016-01-20 23:45:09 +01:00
|
|
|
{
|
2017-06-06 23:24:18 +02:00
|
|
|
return gNetwork.player_list[index]->CommandsRan;
|
2016-01-20 23:45:09 +01:00
|
|
|
}
|
|
|
|
|
2018-11-20 06:04:42 +01:00
|
|
|
int32_t network_get_player_index(uint32_t id)
|
2016-01-20 23:45:09 +01:00
|
|
|
{
|
2017-06-06 23:24:18 +02:00
|
|
|
auto it = gNetwork.GetPlayerIteratorByID(id);
|
2018-06-22 23:02:47 +02:00
|
|
|
if (it == gNetwork.player_list.end())
|
|
|
|
{
|
2017-06-06 23:24:18 +02:00
|
|
|
return -1;
|
|
|
|
}
|
2020-04-22 17:09:29 +02:00
|
|
|
return static_cast<int32_t>(gNetwork.GetPlayerIteratorByID(id) - gNetwork.player_list.begin());
|
2016-01-20 23:45:09 +01:00
|
|
|
}
|
|
|
|
|
2018-06-20 17:28:51 +02:00
|
|
|
uint8_t network_get_player_group(uint32_t index)
|
2016-01-20 23:45:09 +01:00
|
|
|
{
|
2017-06-06 23:24:18 +02:00
|
|
|
return gNetwork.player_list[index]->Group;
|
2016-01-20 23:45:09 +01:00
|
|
|
}
|
|
|
|
|
2018-06-20 17:28:51 +02:00
|
|
|
void network_set_player_group(uint32_t index, uint32_t groupindex)
|
2016-01-20 23:45:09 +01:00
|
|
|
{
|
2017-06-06 23:24:18 +02:00
|
|
|
gNetwork.player_list[index]->Group = gNetwork.group_list[groupindex]->Id;
|
2016-01-20 23:45:09 +01:00
|
|
|
}
|
|
|
|
|
2018-06-20 17:28:51 +02:00
|
|
|
int32_t network_get_group_index(uint8_t id)
|
2016-01-20 23:45:09 +01:00
|
|
|
{
|
2017-06-06 23:24:18 +02:00
|
|
|
auto it = gNetwork.GetGroupIteratorByID(id);
|
2018-06-22 23:02:47 +02:00
|
|
|
if (it == gNetwork.group_list.end())
|
|
|
|
{
|
2017-06-06 23:24:18 +02:00
|
|
|
return -1;
|
|
|
|
}
|
2020-04-22 17:09:29 +02:00
|
|
|
return static_cast<int32_t>(gNetwork.GetGroupIteratorByID(id) - gNetwork.group_list.begin());
|
2016-01-20 23:45:09 +01:00
|
|
|
}
|
|
|
|
|
2018-06-20 17:28:51 +02:00
|
|
|
uint8_t network_get_group_id(uint32_t index)
|
2016-01-20 23:45:09 +01:00
|
|
|
{
|
2017-06-06 23:24:18 +02:00
|
|
|
return gNetwork.group_list[index]->Id;
|
2016-01-20 23:45:09 +01:00
|
|
|
}
|
|
|
|
|
2018-06-20 17:28:51 +02:00
|
|
|
int32_t network_get_num_groups()
|
2016-01-20 23:45:09 +01:00
|
|
|
{
|
2020-04-22 17:09:29 +02:00
|
|
|
return static_cast<int32_t>(gNetwork.group_list.size());
|
2016-01-20 23:45:09 +01:00
|
|
|
}
|
|
|
|
|
2018-06-20 17:28:51 +02:00
|
|
|
const char* network_get_group_name(uint32_t index)
|
2016-01-20 23:45:09 +01:00
|
|
|
{
|
2017-06-06 23:24:18 +02:00
|
|
|
return gNetwork.group_list[index]->GetName().c_str();
|
2016-01-20 23:45:09 +01:00
|
|
|
}
|
|
|
|
|
2016-02-26 10:22:14 +01:00
|
|
|
void network_chat_show_connected_message()
|
|
|
|
{
|
2017-06-11 19:19:15 +02:00
|
|
|
auto windowManager = GetContext()->GetUiContext()->GetWindowManager();
|
|
|
|
std::string s = windowManager->GetKeyboardShortcutString(41 /* SHORTCUT_OPEN_CHAT_WINDOW */);
|
2018-06-22 23:02:47 +02:00
|
|
|
const char* sptr = s.c_str();
|
2017-06-11 19:19:15 +02:00
|
|
|
|
2017-06-06 23:24:18 +02:00
|
|
|
utf8 buffer[256];
|
2017-06-11 19:19:15 +02:00
|
|
|
format_string(buffer, sizeof(buffer), STR_MULTIPLAYER_CONNECTED_CHAT_HINT, &sptr);
|
|
|
|
|
2017-06-06 23:24:18 +02:00
|
|
|
NetworkPlayer server;
|
|
|
|
server.Name = "Server";
|
2018-06-22 23:02:47 +02:00
|
|
|
const char* formatted = Network::FormatChat(&server, buffer);
|
2017-06-06 23:24:18 +02:00
|
|
|
chat_history_add(formatted);
|
2016-02-26 10:22:14 +01:00
|
|
|
}
|
|
|
|
|
2016-08-15 19:08:23 +02:00
|
|
|
// Display server greeting if one exists
|
|
|
|
void network_chat_show_server_greeting()
|
|
|
|
{
|
2017-06-06 23:24:18 +02:00
|
|
|
const char* greeting = network_get_server_greeting();
|
2018-06-22 23:02:47 +02:00
|
|
|
if (!str_is_null_or_empty(greeting))
|
|
|
|
{
|
2017-06-06 23:24:18 +02:00
|
|
|
static char greeting_formatted[CHAT_INPUT_SIZE];
|
|
|
|
char* lineCh = greeting_formatted;
|
|
|
|
greeting_formatted[0] = 0;
|
|
|
|
lineCh = utf8_write_codepoint(lineCh, FORMAT_OUTLINE);
|
|
|
|
lineCh = utf8_write_codepoint(lineCh, FORMAT_GREEN);
|
|
|
|
char* ptrtext = lineCh;
|
|
|
|
safe_strcpy(lineCh, greeting, CHAT_INPUT_SIZE - 24); // Limit to 1000 characters so we don't overflow the buffer
|
2018-02-15 13:04:25 +01:00
|
|
|
utf8_remove_format_codes(ptrtext, true);
|
2017-06-06 23:24:18 +02:00
|
|
|
chat_history_add(greeting_formatted);
|
|
|
|
}
|
2016-08-15 19:08:23 +02:00
|
|
|
}
|
|
|
|
|
2019-05-01 17:54:12 +02:00
|
|
|
GameActionResult::Ptr network_set_player_group(
|
|
|
|
NetworkPlayerId_t actionPlayerId, NetworkPlayerId_t playerId, uint8_t groupId, bool isExecuting)
|
2018-06-20 17:28:51 +02:00
|
|
|
{
|
2019-05-01 17:54:12 +02:00
|
|
|
NetworkPlayer* player = gNetwork.GetPlayerByID(playerId);
|
|
|
|
|
|
|
|
NetworkGroup* fromgroup = gNetwork.GetGroupByID(actionPlayerId);
|
|
|
|
if (player == nullptr)
|
2018-06-22 23:02:47 +02:00
|
|
|
{
|
2019-05-01 17:54:12 +02:00
|
|
|
return std::make_unique<GameActionResult>(GA_ERROR::INVALID_PARAMETERS, STR_CANT_DO_THIS);
|
2017-06-06 23:24:18 +02:00
|
|
|
}
|
2019-05-01 17:54:12 +02:00
|
|
|
|
|
|
|
if (!gNetwork.GetGroupByID(groupId))
|
2018-06-22 23:02:47 +02:00
|
|
|
{
|
2019-05-01 17:54:12 +02:00
|
|
|
return std::make_unique<GameActionResult>(GA_ERROR::INVALID_PARAMETERS, STR_CANT_DO_THIS);
|
2017-06-06 23:24:18 +02:00
|
|
|
}
|
2019-05-01 17:54:12 +02:00
|
|
|
|
2018-06-22 23:02:47 +02:00
|
|
|
if (player->Flags & NETWORK_PLAYER_FLAG_ISSERVER)
|
|
|
|
{
|
2019-05-01 17:54:12 +02:00
|
|
|
return std::make_unique<GameActionResult>(GA_ERROR::INVALID_PARAMETERS, STR_CANT_CHANGE_GROUP_THAT_THE_HOST_BELONGS_TO);
|
2017-06-06 23:24:18 +02:00
|
|
|
}
|
2019-05-01 17:54:12 +02:00
|
|
|
|
|
|
|
if (groupId == 0 && fromgroup && fromgroup->Id != 0)
|
2018-06-22 23:02:47 +02:00
|
|
|
{
|
2019-05-01 17:54:12 +02:00
|
|
|
return std::make_unique<GameActionResult>(GA_ERROR::INVALID_PARAMETERS, STR_CANT_SET_TO_THIS_GROUP);
|
2017-06-06 23:24:18 +02:00
|
|
|
}
|
2019-05-01 17:54:12 +02:00
|
|
|
|
|
|
|
if (isExecuting)
|
2018-06-22 23:02:47 +02:00
|
|
|
{
|
2019-05-01 17:54:12 +02:00
|
|
|
player->Group = groupId;
|
2017-06-06 23:24:18 +02:00
|
|
|
|
2018-06-22 23:02:47 +02:00
|
|
|
if (network_get_mode() == NETWORK_MODE_SERVER)
|
|
|
|
{
|
2017-06-06 23:24:18 +02:00
|
|
|
// Add or update saved user
|
2018-06-22 23:02:47 +02:00
|
|
|
NetworkUserManager* userManager = &gNetwork._userManager;
|
|
|
|
NetworkUser* networkUser = userManager->GetOrAddUser(player->KeyHash);
|
2019-05-01 17:54:12 +02:00
|
|
|
networkUser->GroupId = groupId;
|
2017-06-06 23:24:18 +02:00
|
|
|
networkUser->Name = player->Name;
|
|
|
|
userManager->Save();
|
|
|
|
}
|
|
|
|
|
2019-05-01 17:54:12 +02:00
|
|
|
window_invalidate_by_number(WC_PLAYER, playerId);
|
2017-06-06 23:24:18 +02:00
|
|
|
|
|
|
|
// Log set player group event
|
2019-05-01 17:54:12 +02:00
|
|
|
NetworkPlayer* game_command_player = gNetwork.GetPlayerByID(actionPlayerId);
|
|
|
|
NetworkGroup* new_player_group = gNetwork.GetGroupByID(groupId);
|
2017-06-06 23:24:18 +02:00
|
|
|
char log_msg[256];
|
2018-07-07 14:40:56 +02:00
|
|
|
const char* args[3] = {
|
|
|
|
player->Name.c_str(),
|
|
|
|
new_player_group->GetName().c_str(),
|
|
|
|
game_command_player->Name.c_str(),
|
|
|
|
};
|
2017-06-06 23:24:18 +02:00
|
|
|
format_string(log_msg, 256, STR_LOG_SET_PLAYER_GROUP, args);
|
|
|
|
network_append_server_log(log_msg);
|
|
|
|
}
|
2019-05-01 17:54:12 +02:00
|
|
|
return std::make_unique<GameActionResult>();
|
2016-01-20 23:45:09 +01:00
|
|
|
}
|
|
|
|
|
2019-05-12 19:54:41 +02:00
|
|
|
GameActionResult::Ptr network_modify_groups(
|
|
|
|
NetworkPlayerId_t actionPlayerId, ModifyGroupType type, uint8_t groupId, const std::string& name, uint32_t permissionIndex,
|
|
|
|
PermissionState permissionState, bool isExecuting)
|
2016-01-22 04:32:51 +01:00
|
|
|
{
|
2019-05-12 19:54:41 +02:00
|
|
|
switch (type)
|
2017-06-06 23:24:18 +02:00
|
|
|
{
|
2019-05-12 19:54:41 +02:00
|
|
|
case ModifyGroupType::AddGroup:
|
|
|
|
{
|
|
|
|
if (isExecuting)
|
2018-06-22 23:02:47 +02:00
|
|
|
{
|
|
|
|
NetworkGroup* newgroup = gNetwork.AddGroup();
|
2019-05-13 20:49:06 +02:00
|
|
|
if (newgroup == nullptr)
|
2018-06-22 23:02:47 +02:00
|
|
|
{
|
2019-05-12 19:54:41 +02:00
|
|
|
return std::make_unique<GameActionResult>(GA_ERROR::UNKNOWN, STR_CANT_DO_THIS);
|
2018-06-22 23:02:47 +02:00
|
|
|
}
|
2017-07-22 11:56:46 +02:00
|
|
|
}
|
2017-06-06 23:24:18 +02:00
|
|
|
}
|
2018-06-22 23:02:47 +02:00
|
|
|
break;
|
2019-05-12 19:54:41 +02:00
|
|
|
case ModifyGroupType::RemoveGroup:
|
|
|
|
{
|
|
|
|
if (groupId == 0)
|
2018-06-22 23:02:47 +02:00
|
|
|
{
|
2019-05-12 19:54:41 +02:00
|
|
|
return std::make_unique<GameActionResult>(GA_ERROR::DISALLOWED, STR_THIS_GROUP_CANNOT_BE_MODIFIED);
|
2017-06-06 23:24:18 +02:00
|
|
|
}
|
2019-05-13 20:53:33 +02:00
|
|
|
for (const auto& it : gNetwork.player_list)
|
2018-06-22 23:02:47 +02:00
|
|
|
{
|
2019-05-13 20:49:06 +02:00
|
|
|
if ((it.get())->Group == groupId)
|
2018-06-22 23:02:47 +02:00
|
|
|
{
|
2019-05-13 20:53:33 +02:00
|
|
|
return std::make_unique<GameActionResult>(
|
|
|
|
GA_ERROR::DISALLOWED, STR_CANT_REMOVE_GROUP_THAT_PLAYERS_BELONG_TO);
|
2018-06-22 23:02:47 +02:00
|
|
|
}
|
2017-07-22 11:56:46 +02:00
|
|
|
}
|
2019-05-12 19:54:41 +02:00
|
|
|
if (isExecuting)
|
2018-06-22 23:02:47 +02:00
|
|
|
{
|
2019-05-12 19:54:41 +02:00
|
|
|
gNetwork.RemoveGroup(groupId);
|
2018-06-22 23:02:47 +02:00
|
|
|
}
|
2017-06-06 23:24:18 +02:00
|
|
|
}
|
2018-06-22 23:02:47 +02:00
|
|
|
break;
|
2019-05-12 19:54:41 +02:00
|
|
|
case ModifyGroupType::SetPermissions:
|
|
|
|
{
|
|
|
|
if (groupId == 0)
|
2018-06-22 23:02:47 +02:00
|
|
|
{ // cant change admin group permissions
|
2019-05-12 19:54:41 +02:00
|
|
|
return std::make_unique<GameActionResult>(GA_ERROR::DISALLOWED, STR_THIS_GROUP_CANNOT_BE_MODIFIED);
|
2017-06-06 23:24:18 +02:00
|
|
|
}
|
2018-06-22 23:02:47 +02:00
|
|
|
NetworkGroup* mygroup = nullptr;
|
2019-05-12 19:54:41 +02:00
|
|
|
NetworkPlayer* player = gNetwork.GetPlayerByID(actionPlayerId);
|
2019-05-13 20:49:06 +02:00
|
|
|
if (player != nullptr && permissionState == PermissionState::Toggle)
|
2018-06-22 23:02:47 +02:00
|
|
|
{
|
|
|
|
mygroup = gNetwork.GetGroupByID(player->Group);
|
2019-05-13 20:49:06 +02:00
|
|
|
if (mygroup == nullptr || !mygroup->CanPerformAction(permissionIndex))
|
2018-06-22 23:02:47 +02:00
|
|
|
{
|
2019-05-12 19:54:41 +02:00
|
|
|
return std::make_unique<GameActionResult>(
|
|
|
|
GA_ERROR::DISALLOWED, STR_CANT_MODIFY_PERMISSION_THAT_YOU_DO_NOT_HAVE_YOURSELF);
|
2018-06-22 23:02:47 +02:00
|
|
|
}
|
|
|
|
}
|
2019-05-12 19:54:41 +02:00
|
|
|
if (isExecuting)
|
2018-06-22 23:02:47 +02:00
|
|
|
{
|
2019-05-12 19:54:41 +02:00
|
|
|
NetworkGroup* group = gNetwork.GetGroupByID(groupId);
|
2019-05-13 20:49:06 +02:00
|
|
|
if (group != nullptr)
|
2018-06-22 23:02:47 +02:00
|
|
|
{
|
2019-05-12 19:54:41 +02:00
|
|
|
if (permissionState != PermissionState::Toggle)
|
2018-06-22 23:02:47 +02:00
|
|
|
{
|
2019-05-13 20:49:06 +02:00
|
|
|
if (mygroup != nullptr)
|
2018-06-22 23:02:47 +02:00
|
|
|
{
|
2019-05-12 19:54:41 +02:00
|
|
|
if (permissionState == PermissionState::SetAll)
|
2018-06-22 23:02:47 +02:00
|
|
|
{
|
|
|
|
group->ActionsAllowed = mygroup->ActionsAllowed;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
group->ActionsAllowed.fill(0x00);
|
|
|
|
}
|
2017-06-06 23:24:18 +02:00
|
|
|
}
|
|
|
|
}
|
2018-06-22 23:02:47 +02:00
|
|
|
else
|
|
|
|
{
|
2019-05-12 19:54:41 +02:00
|
|
|
group->ToggleActionPermission(permissionIndex);
|
2018-06-22 23:02:47 +02:00
|
|
|
}
|
2017-06-06 23:24:18 +02:00
|
|
|
}
|
2018-06-22 23:02:47 +02:00
|
|
|
}
|
2017-06-06 23:24:18 +02:00
|
|
|
}
|
2018-06-22 23:02:47 +02:00
|
|
|
break;
|
2019-05-12 19:54:41 +02:00
|
|
|
case ModifyGroupType::SetName:
|
|
|
|
{
|
|
|
|
NetworkGroup* group = gNetwork.GetGroupByID(groupId);
|
2018-06-22 23:02:47 +02:00
|
|
|
const char* oldName = group->GetName().c_str();
|
2017-06-06 23:24:18 +02:00
|
|
|
|
2019-05-12 19:54:41 +02:00
|
|
|
if (strcmp(oldName, name.c_str()) == 0)
|
2018-06-22 23:02:47 +02:00
|
|
|
{
|
2019-05-12 19:54:41 +02:00
|
|
|
return std::make_unique<GameActionResult>();
|
2018-06-22 23:02:47 +02:00
|
|
|
}
|
2017-06-06 23:24:18 +02:00
|
|
|
|
2019-05-12 19:54:41 +02:00
|
|
|
if (name.empty())
|
2018-06-22 23:02:47 +02:00
|
|
|
{
|
2019-05-12 19:54:41 +02:00
|
|
|
return std::make_unique<GameActionResult>(
|
|
|
|
GA_ERROR::INVALID_PARAMETERS, STR_CANT_RENAME_GROUP, STR_INVALID_GROUP_NAME);
|
2018-06-22 23:02:47 +02:00
|
|
|
}
|
|
|
|
|
2019-05-12 19:54:41 +02:00
|
|
|
if (isExecuting)
|
2018-06-22 23:02:47 +02:00
|
|
|
{
|
2019-05-13 20:49:06 +02:00
|
|
|
if (group != nullptr)
|
2018-06-22 23:02:47 +02:00
|
|
|
{
|
2019-05-12 19:54:41 +02:00
|
|
|
group->SetName(name);
|
2018-06-22 23:02:47 +02:00
|
|
|
}
|
|
|
|
}
|
2017-06-06 23:24:18 +02:00
|
|
|
}
|
2018-06-22 23:02:47 +02:00
|
|
|
break;
|
2019-05-12 19:54:41 +02:00
|
|
|
case ModifyGroupType::SetDefault:
|
|
|
|
{
|
|
|
|
if (groupId == 0)
|
2018-06-22 23:02:47 +02:00
|
|
|
{
|
2019-05-12 19:54:41 +02:00
|
|
|
return std::make_unique<GameActionResult>(GA_ERROR::DISALLOWED, STR_CANT_SET_TO_THIS_GROUP);
|
2018-06-22 23:02:47 +02:00
|
|
|
}
|
2019-05-12 19:54:41 +02:00
|
|
|
if (isExecuting)
|
2018-06-22 23:02:47 +02:00
|
|
|
{
|
2019-05-12 19:54:41 +02:00
|
|
|
gNetwork.SetDefaultGroup(groupId);
|
2017-06-06 23:24:18 +02:00
|
|
|
}
|
|
|
|
}
|
2018-06-22 23:02:47 +02:00
|
|
|
break;
|
2019-05-13 20:53:33 +02:00
|
|
|
default:
|
|
|
|
log_error("Invalid Modify Group Type: %u", static_cast<uint8_t>(type));
|
|
|
|
return std::make_unique<GameActionResult>(GA_ERROR::INVALID_PARAMETERS, STR_NONE);
|
2017-06-06 23:24:18 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
gNetwork.SaveGroups();
|
|
|
|
|
2019-05-12 19:54:41 +02:00
|
|
|
return std::make_unique<GameActionResult>();
|
2016-01-22 04:32:51 +01:00
|
|
|
}
|
|
|
|
|
2019-05-13 20:49:06 +02:00
|
|
|
GameActionResult::Ptr network_kick_player(NetworkPlayerId_t playerId, bool isExecuting)
|
2018-06-20 17:28:51 +02:00
|
|
|
{
|
2019-05-12 19:54:41 +02:00
|
|
|
NetworkPlayer* player = gNetwork.GetPlayerByID(playerId);
|
2018-06-22 23:02:47 +02:00
|
|
|
if (player == nullptr)
|
|
|
|
{
|
2020-02-11 00:33:00 +01:00
|
|
|
// Player might be already removed by the PLAYERLIST command, need to refactor non-game commands executing too
|
|
|
|
// early.
|
2019-05-12 19:54:41 +02:00
|
|
|
return std::make_unique<GameActionResult>(GA_ERROR::UNKNOWN, STR_NONE);
|
2017-06-21 02:00:14 +02:00
|
|
|
}
|
|
|
|
|
2018-06-22 23:02:47 +02:00
|
|
|
if (player && player->Flags & NETWORK_PLAYER_FLAG_ISSERVER)
|
|
|
|
{
|
2019-05-12 19:54:41 +02:00
|
|
|
return std::make_unique<GameActionResult>(GA_ERROR::DISALLOWED, STR_CANT_KICK_THE_HOST);
|
2017-06-06 23:24:18 +02:00
|
|
|
}
|
2017-07-22 11:56:46 +02:00
|
|
|
|
2019-05-12 19:54:41 +02:00
|
|
|
if (isExecuting)
|
2018-06-22 23:02:47 +02:00
|
|
|
{
|
|
|
|
if (gNetwork.GetMode() == NETWORK_MODE_SERVER)
|
|
|
|
{
|
2019-05-12 19:54:41 +02:00
|
|
|
gNetwork.KickPlayer(playerId);
|
2017-06-06 23:24:18 +02:00
|
|
|
|
2018-06-22 23:02:47 +02:00
|
|
|
NetworkUserManager* networkUserManager = &gNetwork._userManager;
|
2017-06-06 23:24:18 +02:00
|
|
|
networkUserManager->Load();
|
|
|
|
networkUserManager->RemoveUser(player->KeyHash);
|
|
|
|
networkUserManager->Save();
|
|
|
|
}
|
|
|
|
}
|
2019-05-12 19:54:41 +02:00
|
|
|
return std::make_unique<GameActionResult>();
|
2016-01-20 23:45:09 +01:00
|
|
|
}
|
|
|
|
|
2018-06-20 17:28:51 +02:00
|
|
|
uint8_t network_get_default_group()
|
2016-01-20 23:45:09 +01:00
|
|
|
{
|
2017-06-06 23:24:18 +02:00
|
|
|
return gNetwork.GetDefaultGroup();
|
2016-01-20 23:45:09 +01:00
|
|
|
}
|
|
|
|
|
2018-06-20 17:28:51 +02:00
|
|
|
int32_t network_get_num_actions()
|
2016-01-22 04:32:51 +01:00
|
|
|
{
|
2020-04-22 17:09:29 +02:00
|
|
|
return static_cast<int32_t>(NetworkActions::Actions.size());
|
2016-01-22 04:32:51 +01:00
|
|
|
}
|
|
|
|
|
2018-06-20 17:28:51 +02:00
|
|
|
rct_string_id network_get_action_name_string_id(uint32_t index)
|
2016-01-22 04:32:51 +01:00
|
|
|
{
|
2017-06-06 23:24:18 +02:00
|
|
|
if (index < NetworkActions::Actions.size())
|
|
|
|
{
|
|
|
|
return NetworkActions::Actions[index].Name;
|
2018-06-22 23:02:47 +02:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2017-06-06 23:24:18 +02:00
|
|
|
return STR_NONE;
|
|
|
|
}
|
2016-01-22 04:32:51 +01:00
|
|
|
}
|
|
|
|
|
2018-06-20 17:28:51 +02:00
|
|
|
int32_t network_can_perform_action(uint32_t groupindex, uint32_t index)
|
2016-01-22 04:32:51 +01:00
|
|
|
{
|
2017-06-06 23:24:18 +02:00
|
|
|
return gNetwork.group_list[groupindex]->CanPerformAction(index);
|
2016-01-22 04:32:51 +01:00
|
|
|
}
|
|
|
|
|
2018-06-20 17:28:51 +02:00
|
|
|
int32_t network_can_perform_command(uint32_t groupindex, int32_t index)
|
2016-03-24 23:59:16 +01:00
|
|
|
{
|
2017-06-06 23:24:18 +02:00
|
|
|
return gNetwork.group_list[groupindex]->CanPerformCommand(index);
|
2016-03-24 23:59:16 +01:00
|
|
|
}
|
|
|
|
|
2019-02-28 20:28:58 +01:00
|
|
|
void network_set_pickup_peep(uint8_t playerid, Peep* peep)
|
2016-10-17 22:26:19 +02:00
|
|
|
{
|
2018-06-22 23:02:47 +02:00
|
|
|
if (gNetwork.GetMode() == NETWORK_MODE_NONE)
|
|
|
|
{
|
2017-07-22 11:56:46 +02:00
|
|
|
_pickup_peep = peep;
|
2018-06-22 23:02:47 +02:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2017-07-22 11:56:46 +02:00
|
|
|
NetworkPlayer* player = gNetwork.GetPlayerByID(playerid);
|
2018-06-22 23:02:47 +02:00
|
|
|
if (player)
|
|
|
|
{
|
2017-07-22 11:56:46 +02:00
|
|
|
player->PickupPeep = peep;
|
|
|
|
}
|
|
|
|
}
|
2016-10-17 22:26:19 +02:00
|
|
|
}
|
|
|
|
|
2019-02-28 20:28:58 +01:00
|
|
|
Peep* network_get_pickup_peep(uint8_t playerid)
|
2016-10-17 22:26:19 +02:00
|
|
|
{
|
2018-06-22 23:02:47 +02:00
|
|
|
if (gNetwork.GetMode() == NETWORK_MODE_NONE)
|
|
|
|
{
|
2017-07-22 11:56:46 +02:00
|
|
|
return _pickup_peep;
|
2018-06-22 23:02:47 +02:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2017-07-22 11:56:46 +02:00
|
|
|
NetworkPlayer* player = gNetwork.GetPlayerByID(playerid);
|
2018-06-22 23:02:47 +02:00
|
|
|
if (player)
|
|
|
|
{
|
2017-07-22 11:56:46 +02:00
|
|
|
return player->PickupPeep;
|
|
|
|
}
|
|
|
|
return nullptr;
|
|
|
|
}
|
2016-10-17 22:26:19 +02:00
|
|
|
}
|
|
|
|
|
2018-06-20 17:28:51 +02:00
|
|
|
void network_set_pickup_peep_old_x(uint8_t playerid, int32_t x)
|
2016-10-17 22:26:19 +02:00
|
|
|
{
|
2018-06-22 23:02:47 +02:00
|
|
|
if (gNetwork.GetMode() == NETWORK_MODE_NONE)
|
|
|
|
{
|
2017-07-22 11:56:46 +02:00
|
|
|
_pickup_peep_old_x = x;
|
2018-06-22 23:02:47 +02:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2017-07-22 11:56:46 +02:00
|
|
|
NetworkPlayer* player = gNetwork.GetPlayerByID(playerid);
|
2018-06-22 23:02:47 +02:00
|
|
|
if (player)
|
|
|
|
{
|
2017-07-22 11:56:46 +02:00
|
|
|
player->PickupPeepOldX = x;
|
|
|
|
}
|
|
|
|
}
|
2016-10-17 22:26:19 +02:00
|
|
|
}
|
|
|
|
|
2018-06-20 17:28:51 +02:00
|
|
|
int32_t network_get_pickup_peep_old_x(uint8_t playerid)
|
2016-10-17 22:26:19 +02:00
|
|
|
{
|
2018-06-22 23:02:47 +02:00
|
|
|
if (gNetwork.GetMode() == NETWORK_MODE_NONE)
|
|
|
|
{
|
2017-07-22 11:56:46 +02:00
|
|
|
return _pickup_peep_old_x;
|
2018-06-22 23:02:47 +02:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2017-07-22 11:56:46 +02:00
|
|
|
NetworkPlayer* player = gNetwork.GetPlayerByID(playerid);
|
2018-06-22 23:02:47 +02:00
|
|
|
if (player)
|
|
|
|
{
|
2017-07-22 11:56:46 +02:00
|
|
|
return player->PickupPeepOldX;
|
|
|
|
}
|
|
|
|
return -1;
|
|
|
|
}
|
2016-10-17 22:26:19 +02:00
|
|
|
}
|
|
|
|
|
2018-06-20 17:28:51 +02:00
|
|
|
int32_t network_get_current_player_group_index()
|
2016-03-24 23:59:16 +01:00
|
|
|
{
|
2017-07-22 11:56:46 +02:00
|
|
|
NetworkPlayer* player = gNetwork.GetPlayerByID(gNetwork.GetPlayerID());
|
2018-06-22 23:02:47 +02:00
|
|
|
if (player)
|
|
|
|
{
|
2017-07-22 11:56:46 +02:00
|
|
|
return network_get_group_index(player->Group);
|
|
|
|
}
|
|
|
|
return -1;
|
2016-03-24 23:59:16 +01:00
|
|
|
}
|
|
|
|
|
2015-07-10 21:53:41 +02:00
|
|
|
void network_send_map()
|
|
|
|
{
|
2017-06-06 23:24:18 +02:00
|
|
|
gNetwork.Server_Send_MAP();
|
2015-07-10 21:53:41 +02:00
|
|
|
}
|
|
|
|
|
2020-04-28 23:02:14 +02:00
|
|
|
void network_send_chat(const char* text, const std::vector<uint8_t>& playerIds)
|
2015-07-10 21:53:41 +02:00
|
|
|
{
|
2018-06-22 23:02:47 +02:00
|
|
|
if (gNetwork.GetMode() == NETWORK_MODE_CLIENT)
|
|
|
|
{
|
2017-06-06 23:24:18 +02:00
|
|
|
gNetwork.Client_Send_CHAT(text);
|
2018-06-22 23:02:47 +02:00
|
|
|
}
|
|
|
|
else if (gNetwork.GetMode() == NETWORK_MODE_SERVER)
|
|
|
|
{
|
2020-04-28 18:31:12 +02:00
|
|
|
std::string message = text;
|
|
|
|
if (ProcessChatMessagePluginHooks(gNetwork.GetPlayerID(), message))
|
|
|
|
{
|
|
|
|
auto player = gNetwork.GetPlayerByID(gNetwork.GetPlayerID());
|
|
|
|
if (player != nullptr)
|
|
|
|
{
|
|
|
|
auto formatted = gNetwork.FormatChat(player, message.c_str());
|
2020-04-28 23:02:14 +02:00
|
|
|
if (playerIds.empty()
|
|
|
|
|| std::find(playerIds.begin(), playerIds.end(), gNetwork.GetPlayerID()) != playerIds.end())
|
|
|
|
{
|
2020-04-30 03:18:32 +02:00
|
|
|
// Server is one of the recipients
|
2020-04-28 23:02:14 +02:00
|
|
|
chat_history_add(formatted);
|
|
|
|
}
|
|
|
|
gNetwork.Server_Send_CHAT(formatted, playerIds);
|
2020-04-28 18:31:12 +02:00
|
|
|
}
|
|
|
|
}
|
2017-06-06 23:24:18 +02:00
|
|
|
}
|
2015-07-10 21:53:41 +02:00
|
|
|
}
|
|
|
|
|
2018-06-22 23:02:47 +02:00
|
|
|
void network_send_game_action(const GameAction* action)
|
2017-03-21 20:05:53 +01:00
|
|
|
{
|
2018-06-22 23:02:47 +02:00
|
|
|
switch (gNetwork.GetMode())
|
|
|
|
{
|
|
|
|
case NETWORK_MODE_SERVER:
|
|
|
|
gNetwork.Server_Send_GAME_ACTION(action);
|
|
|
|
break;
|
|
|
|
case NETWORK_MODE_CLIENT:
|
|
|
|
gNetwork.Client_Send_GAME_ACTION(action);
|
|
|
|
break;
|
2017-07-21 19:21:18 +02:00
|
|
|
}
|
2017-03-21 20:05:53 +01:00
|
|
|
}
|
|
|
|
|
2019-02-23 15:54:17 +01:00
|
|
|
void network_send_password(const std::string& password)
|
2015-11-02 04:12:14 +01:00
|
|
|
{
|
2017-06-06 23:24:18 +02:00
|
|
|
utf8 keyPath[MAX_PATH];
|
|
|
|
network_get_private_key_path(keyPath, sizeof(keyPath), gConfigNetwork.player_name);
|
2020-07-02 17:03:31 +02:00
|
|
|
if (!Platform::FileExists(keyPath))
|
2018-06-22 23:02:47 +02:00
|
|
|
{
|
2017-06-06 23:24:18 +02:00
|
|
|
log_error("Private key %s missing! Restart the game to generate it.", keyPath);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
try
|
|
|
|
{
|
|
|
|
auto fs = FileStream(keyPath, FILE_MODE_OPEN);
|
|
|
|
gNetwork._key.LoadPrivate(&fs);
|
|
|
|
}
|
2018-06-22 23:02:47 +02:00
|
|
|
catch (const std::exception&)
|
2017-06-06 23:24:18 +02:00
|
|
|
{
|
|
|
|
log_error("Error reading private key from %s.", keyPath);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
const std::string pubkey = gNetwork._key.PublicKeyString();
|
2019-03-09 13:58:23 +01:00
|
|
|
|
|
|
|
std::vector<uint8_t> signature;
|
|
|
|
gNetwork._key.Sign(gNetwork._challenge.data(), gNetwork._challenge.size(), signature);
|
2017-06-06 23:24:18 +02:00
|
|
|
// Don't keep private key in memory. There's no need and it may get leaked
|
|
|
|
// when process dump gets collected at some point in future.
|
|
|
|
gNetwork._key.Unload();
|
2019-03-09 13:58:23 +01:00
|
|
|
gNetwork.Client_Send_AUTH(gConfigNetwork.player_name.c_str(), password, pubkey.c_str(), signature);
|
2015-11-02 04:12:14 +01:00
|
|
|
}
|
|
|
|
|
2015-11-02 01:28:53 +01:00
|
|
|
void network_set_password(const char* password)
|
2015-10-16 23:20:16 +02:00
|
|
|
{
|
2017-06-06 23:24:18 +02:00
|
|
|
gNetwork.SetPassword(password);
|
2015-10-16 23:20:16 +02:00
|
|
|
}
|
|
|
|
|
2018-06-22 23:02:47 +02:00
|
|
|
void network_append_chat_log(const utf8* text)
|
2016-05-30 15:24:06 +02:00
|
|
|
{
|
2017-06-06 23:24:18 +02:00
|
|
|
gNetwork.AppendChatLog(text);
|
2016-05-30 15:24:06 +02:00
|
|
|
}
|
|
|
|
|
2018-06-22 23:02:47 +02:00
|
|
|
void network_append_server_log(const utf8* text)
|
2016-08-10 05:21:55 +02:00
|
|
|
{
|
2017-06-06 23:24:18 +02:00
|
|
|
gNetwork.AppendServerLog(text);
|
2016-08-10 05:21:55 +02:00
|
|
|
}
|
|
|
|
|
2018-06-22 23:02:47 +02:00
|
|
|
static void network_get_keys_directory(utf8* buffer, size_t bufferSize)
|
2016-05-25 21:46:59 +02:00
|
|
|
{
|
2017-06-06 23:24:18 +02:00
|
|
|
platform_get_user_directory(buffer, "keys", bufferSize);
|
2016-05-25 21:46:59 +02:00
|
|
|
}
|
|
|
|
|
2019-02-23 15:54:17 +01:00
|
|
|
static void network_get_private_key_path(utf8* buffer, size_t bufferSize, const std::string& playerName)
|
2016-05-25 21:46:59 +02:00
|
|
|
{
|
2017-06-06 23:24:18 +02:00
|
|
|
network_get_keys_directory(buffer, bufferSize);
|
2019-02-23 15:54:17 +01:00
|
|
|
Path::Append(buffer, bufferSize, playerName.c_str());
|
2017-06-06 23:24:18 +02:00
|
|
|
String::Append(buffer, bufferSize, ".privkey");
|
2016-05-25 21:46:59 +02:00
|
|
|
}
|
|
|
|
|
2019-02-23 15:54:17 +01:00
|
|
|
static void network_get_public_key_path(utf8* buffer, size_t bufferSize, const std::string& playerName, const utf8* hash)
|
2016-05-25 21:46:59 +02:00
|
|
|
{
|
2017-06-06 23:24:18 +02:00
|
|
|
network_get_keys_directory(buffer, bufferSize);
|
2019-02-23 15:54:17 +01:00
|
|
|
Path::Append(buffer, bufferSize, playerName.c_str());
|
2017-06-06 23:24:18 +02:00
|
|
|
String::Append(buffer, bufferSize, "-");
|
|
|
|
String::Append(buffer, bufferSize, hash);
|
|
|
|
String::Append(buffer, bufferSize, ".pubkey");
|
2016-05-25 21:46:59 +02:00
|
|
|
}
|
|
|
|
|
2018-06-22 23:02:47 +02:00
|
|
|
const utf8* network_get_server_name()
|
|
|
|
{
|
|
|
|
return gNetwork.ServerName.c_str();
|
|
|
|
}
|
|
|
|
const utf8* network_get_server_description()
|
|
|
|
{
|
|
|
|
return gNetwork.ServerDescription.c_str();
|
|
|
|
}
|
|
|
|
const utf8* network_get_server_greeting()
|
|
|
|
{
|
|
|
|
return gNetwork.ServerGreeting.c_str();
|
|
|
|
}
|
|
|
|
const utf8* network_get_server_provider_name()
|
|
|
|
{
|
|
|
|
return gNetwork.ServerProviderName.c_str();
|
|
|
|
}
|
|
|
|
const utf8* network_get_server_provider_email()
|
|
|
|
{
|
|
|
|
return gNetwork.ServerProviderEmail.c_str();
|
|
|
|
}
|
|
|
|
const utf8* network_get_server_provider_website()
|
|
|
|
{
|
|
|
|
return gNetwork.ServerProviderWebsite.c_str();
|
|
|
|
}
|
2016-05-30 23:25:43 +02:00
|
|
|
|
2018-02-20 21:06:35 +01:00
|
|
|
std::string network_get_version()
|
|
|
|
{
|
|
|
|
return NETWORK_STREAM_ID;
|
|
|
|
}
|
|
|
|
|
2019-01-29 16:35:19 +01:00
|
|
|
NetworkStats_t network_get_stats()
|
|
|
|
{
|
|
|
|
return gNetwork.GetStats();
|
|
|
|
}
|
|
|
|
|
2019-05-11 21:31:34 +02:00
|
|
|
NetworkServerState_t network_get_server_state()
|
|
|
|
{
|
|
|
|
return gNetwork.GetServerState();
|
|
|
|
}
|
|
|
|
|
|
|
|
bool network_gamestate_snapshots_enabled()
|
|
|
|
{
|
|
|
|
return network_get_server_state().gamestateSnapshotsEnabled;
|
|
|
|
}
|
|
|
|
|
2019-05-05 04:18:50 +02:00
|
|
|
json_t* network_get_server_info_as_json()
|
|
|
|
{
|
|
|
|
return gNetwork.GetServerInfoAsJson();
|
|
|
|
}
|
2015-08-18 09:30:55 +02:00
|
|
|
#else
|
2018-06-22 23:02:47 +02:00
|
|
|
int32_t network_get_mode()
|
|
|
|
{
|
|
|
|
return NETWORK_MODE_NONE;
|
|
|
|
}
|
|
|
|
int32_t network_get_status()
|
|
|
|
{
|
|
|
|
return NETWORK_STATUS_NONE;
|
|
|
|
}
|
|
|
|
int32_t network_get_authstatus()
|
|
|
|
{
|
|
|
|
return NETWORK_AUTH_NONE;
|
|
|
|
}
|
|
|
|
uint32_t network_get_server_tick()
|
|
|
|
{
|
|
|
|
return gCurrentTicks;
|
|
|
|
}
|
|
|
|
void network_flush()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
void network_send_tick()
|
|
|
|
{
|
|
|
|
}
|
2019-02-08 01:32:05 +01:00
|
|
|
bool network_is_desynchronised()
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
2019-05-11 21:31:34 +02:00
|
|
|
bool network_gamestate_snapshots_enabled()
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
bool network_check_desynchronisation()
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
void network_request_gamestate_snapshot()
|
2018-06-22 23:02:47 +02:00
|
|
|
{
|
|
|
|
}
|
|
|
|
void network_send_game_action(const GameAction* action)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
void network_send_map()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
void network_update()
|
|
|
|
{
|
|
|
|
}
|
2018-12-09 20:39:44 +01:00
|
|
|
void network_process_pending()
|
2018-06-22 23:02:47 +02:00
|
|
|
{
|
|
|
|
}
|
2019-02-23 15:54:17 +01:00
|
|
|
int32_t network_begin_client(const std::string& host, int32_t port)
|
2018-06-22 23:02:47 +02:00
|
|
|
{
|
|
|
|
return 1;
|
|
|
|
}
|
2019-02-23 15:54:17 +01:00
|
|
|
int32_t network_begin_server(int32_t port, const std::string& address)
|
2018-06-22 23:02:47 +02:00
|
|
|
{
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
int32_t network_get_num_players()
|
|
|
|
{
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
const char* network_get_player_name(uint32_t index)
|
|
|
|
{
|
|
|
|
return "local (OpenRCT2 compiled without MP)";
|
|
|
|
}
|
|
|
|
uint32_t network_get_player_flags(uint32_t index)
|
|
|
|
{
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
int32_t network_get_player_ping(uint32_t index)
|
|
|
|
{
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
int32_t network_get_player_id(uint32_t index)
|
|
|
|
{
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
money32 network_get_player_money_spent(uint32_t index)
|
|
|
|
{
|
|
|
|
return MONEY(0, 0);
|
|
|
|
}
|
2020-04-27 20:54:29 +02:00
|
|
|
std::string network_get_player_ip_address(uint32_t id)
|
|
|
|
{
|
|
|
|
return {};
|
|
|
|
}
|
|
|
|
std::string network_get_player_public_key_hash(uint32_t id)
|
|
|
|
{
|
|
|
|
return {};
|
|
|
|
}
|
2018-06-22 23:02:47 +02:00
|
|
|
void network_add_player_money_spent(uint32_t index, money32 cost)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
int32_t network_get_player_last_action(uint32_t index, int32_t time)
|
|
|
|
{
|
|
|
|
return -999;
|
|
|
|
}
|
|
|
|
void network_set_player_last_action(uint32_t index, int32_t command)
|
|
|
|
{
|
|
|
|
}
|
2019-12-19 21:48:20 +01:00
|
|
|
CoordsXYZ network_get_player_last_action_coord(uint32_t index)
|
2018-06-22 23:02:47 +02:00
|
|
|
{
|
|
|
|
return { 0, 0, 0 };
|
|
|
|
}
|
2020-02-29 04:30:50 +01:00
|
|
|
void network_set_player_last_action_coord(uint32_t index, const CoordsXYZ& coord)
|
2018-06-22 23:02:47 +02:00
|
|
|
{
|
|
|
|
}
|
|
|
|
uint32_t network_get_player_commands_ran(uint32_t index)
|
|
|
|
{
|
|
|
|
return 0;
|
|
|
|
}
|
2018-11-20 06:04:42 +01:00
|
|
|
int32_t network_get_player_index(uint32_t id)
|
2018-06-22 23:02:47 +02:00
|
|
|
{
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
uint8_t network_get_player_group(uint32_t index)
|
|
|
|
{
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
void network_set_player_group(uint32_t index, uint32_t groupindex)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
int32_t network_get_group_index(uint8_t id)
|
|
|
|
{
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
uint8_t network_get_group_id(uint32_t index)
|
|
|
|
{
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
int32_t network_get_num_groups()
|
|
|
|
{
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
const char* network_get_group_name(uint32_t index)
|
|
|
|
{
|
|
|
|
return "";
|
|
|
|
};
|
2019-05-01 17:54:12 +02:00
|
|
|
|
|
|
|
GameActionResult::Ptr network_set_player_group(
|
|
|
|
NetworkPlayerId_t actionPlayerId, NetworkPlayerId_t playerId, uint8_t groupId, bool isExecuting)
|
2018-06-22 23:02:47 +02:00
|
|
|
{
|
2019-05-01 17:54:12 +02:00
|
|
|
return std::make_unique<GameActionResult>();
|
2018-06-22 23:02:47 +02:00
|
|
|
}
|
2019-05-12 19:54:41 +02:00
|
|
|
GameActionResult::Ptr network_modify_groups(
|
|
|
|
NetworkPlayerId_t actionPlayerId, ModifyGroupType type, uint8_t groupId, const std::string& name, uint32_t permissionIndex,
|
|
|
|
PermissionState permissionState, bool isExecuting)
|
2018-06-22 23:02:47 +02:00
|
|
|
{
|
2019-05-12 19:54:41 +02:00
|
|
|
return std::make_unique<GameActionResult>();
|
2018-06-22 23:02:47 +02:00
|
|
|
}
|
2019-05-13 20:49:06 +02:00
|
|
|
GameActionResult::Ptr network_kick_player(NetworkPlayerId_t playerId, bool isExecuting)
|
2018-06-22 23:02:47 +02:00
|
|
|
{
|
2019-05-12 19:54:41 +02:00
|
|
|
return std::make_unique<GameActionResult>();
|
2018-06-22 23:02:47 +02:00
|
|
|
}
|
|
|
|
uint8_t network_get_default_group()
|
|
|
|
{
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
int32_t network_get_num_actions()
|
|
|
|
{
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
rct_string_id network_get_action_name_string_id(uint32_t index)
|
|
|
|
{
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
int32_t network_can_perform_action(uint32_t groupindex, uint32_t index)
|
|
|
|
{
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
int32_t network_can_perform_command(uint32_t groupindex, int32_t index)
|
|
|
|
{
|
|
|
|
return 0;
|
|
|
|
}
|
2019-02-28 20:28:58 +01:00
|
|
|
void network_set_pickup_peep(uint8_t playerid, Peep* peep)
|
2018-06-22 23:02:47 +02:00
|
|
|
{
|
|
|
|
_pickup_peep = peep;
|
|
|
|
}
|
2019-02-28 20:28:58 +01:00
|
|
|
Peep* network_get_pickup_peep(uint8_t playerid)
|
2018-06-22 23:02:47 +02:00
|
|
|
{
|
|
|
|
return _pickup_peep;
|
|
|
|
}
|
|
|
|
void network_set_pickup_peep_old_x(uint8_t playerid, int32_t x)
|
|
|
|
{
|
|
|
|
_pickup_peep_old_x = x;
|
|
|
|
}
|
|
|
|
int32_t network_get_pickup_peep_old_x(uint8_t playerid)
|
|
|
|
{
|
|
|
|
return _pickup_peep_old_x;
|
|
|
|
}
|
2020-04-28 23:02:14 +02:00
|
|
|
void network_send_chat(const char* text, const std::vector<uint8_t>& playerIds)
|
2018-06-22 23:02:47 +02:00
|
|
|
{
|
|
|
|
}
|
2019-02-23 15:54:17 +01:00
|
|
|
void network_send_password(const std::string& password)
|
2018-06-22 23:02:47 +02:00
|
|
|
{
|
|
|
|
}
|
|
|
|
void network_close()
|
|
|
|
{
|
|
|
|
}
|
2019-02-08 01:32:05 +01:00
|
|
|
void network_reconnect()
|
|
|
|
{
|
|
|
|
}
|
2018-06-22 23:02:47 +02:00
|
|
|
void network_set_env(const std::shared_ptr<OpenRCT2::IPlatformEnvironment>&)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
void network_shutdown_client()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
void network_set_password(const char* password)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
uint8_t network_get_current_player_id()
|
|
|
|
{
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
int32_t network_get_current_player_group_index()
|
|
|
|
{
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
void network_append_chat_log(const utf8* text)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
void network_append_server_log(const utf8* text)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
const utf8* network_get_server_name()
|
|
|
|
{
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
const utf8* network_get_server_description()
|
|
|
|
{
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
const utf8* network_get_server_greeting()
|
|
|
|
{
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
const utf8* network_get_server_provider_name()
|
|
|
|
{
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
const utf8* network_get_server_provider_email()
|
|
|
|
{
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
const utf8* network_get_server_provider_website()
|
|
|
|
{
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
std::string network_get_version()
|
|
|
|
{
|
|
|
|
return "Multiplayer disabled";
|
|
|
|
}
|
2019-01-29 19:51:07 +01:00
|
|
|
NetworkStats_t network_get_stats()
|
2019-01-29 16:35:19 +01:00
|
|
|
{
|
2019-01-29 19:51:07 +01:00
|
|
|
return NetworkStats_t{};
|
2019-01-29 16:35:19 +01:00
|
|
|
}
|
2019-05-11 21:31:34 +02:00
|
|
|
NetworkServerState_t network_get_server_state()
|
|
|
|
{
|
|
|
|
return NetworkServerState_t{};
|
|
|
|
}
|
2019-05-05 04:18:50 +02:00
|
|
|
json_t* network_get_server_info_as_json()
|
|
|
|
{
|
|
|
|
return nullptr;
|
|
|
|
}
|
2015-08-18 09:30:55 +02:00
|
|
|
#endif /* DISABLE_NETWORK */
|