From 40313b1ea3182adbb4586e9c8ca13032a19a0350 Mon Sep 17 00:00:00 2001 From: ZehMatt Date: Tue, 17 Aug 2021 06:20:07 +0300 Subject: [PATCH] Move network instance into Context --- src/openrct2/Context.cpp | 18 +- src/openrct2/Context.h | 8 +- src/openrct2/network/NetworkBase.cpp | 320 +++++++++++++++------------ src/openrct2/network/NetworkBase.h | 9 +- 4 files changed, 213 insertions(+), 142 deletions(-) diff --git a/src/openrct2/Context.cpp b/src/openrct2/Context.cpp index d0f739970e..620c364caa 100644 --- a/src/openrct2/Context.cpp +++ b/src/openrct2/Context.cpp @@ -46,6 +46,7 @@ #include "localisation/Localisation.h" #include "localisation/LocalisationService.h" #include "network/DiscordService.h" +#include "network/NetworkBase.h" #include "network/network.h" #include "object/ObjectManager.h" #include "object/ObjectRepository.h" @@ -108,6 +109,9 @@ namespace OpenRCT2 #ifdef ENABLE_SCRIPTING ScriptEngine _scriptEngine; #endif +#ifndef DISABLE_NETWORK + NetworkBase _network; +#endif // Game states std::unique_ptr _titleScreen; @@ -149,6 +153,9 @@ namespace OpenRCT2 , _localisationService(std::make_unique(env)) #ifdef ENABLE_SCRIPTING , _scriptEngine(_stdInOutConsole, *env) +#endif +#ifndef DISABLE_NETWORK + , _network(*this) #endif , _painter(std::make_unique(uiContext)) { @@ -164,7 +171,7 @@ namespace OpenRCT2 // If objects use GetContext() in their destructor things won't go well. GameActions::ClearQueue(); - network_close(); + _network.Close(); window_close_all(); // Unload objects after closing all windows, this is to overcome windows like @@ -256,11 +263,18 @@ namespace OpenRCT2 return _drawingEngine.get(); } - virtual Paint::Painter* GetPainter() override + Paint::Painter* GetPainter() override { return _painter.get(); } +#ifndef DISABLE_NETWORK + NetworkBase& GetNetwork() override + { + return _network; + } +#endif + int32_t RunOpenRCT2(int argc, const char** argv) override { if (Initialise()) diff --git a/src/openrct2/Context.h b/src/openrct2/Context.h index 967ac662b7..258f636926 100644 --- a/src/openrct2/Context.h +++ b/src/openrct2/Context.h @@ -1,4 +1,4 @@ -/***************************************************************************** +/***************************************************************************** * Copyright (c) 2014-2020 OpenRCT2 developers * * For a complete list of all authors, please refer to contributors.md @@ -70,6 +70,8 @@ enum CURSOR_PRESSED = CURSOR_DOWN | CURSOR_CHANGED, }; +class NetworkBase; + namespace OpenRCT2 { class GameState; @@ -131,7 +133,9 @@ namespace OpenRCT2 virtual DrawingEngine GetDrawingEngineType() abstract; virtual Drawing::IDrawingEngine* GetDrawingEngine() abstract; virtual Paint::Painter* GetPainter() abstract; - +#ifndef DISABLE_NETWORK + virtual NetworkBase& GetNetwork() abstract; +#endif virtual int32_t RunOpenRCT2(int argc, const char** argv) abstract; virtual bool Initialise() abstract; diff --git a/src/openrct2/network/NetworkBase.cpp b/src/openrct2/network/NetworkBase.cpp index 7f3578ff38..f028427c9d 100644 --- a/src/openrct2/network/NetworkBase.cpp +++ b/src/openrct2/network/NetworkBase.cpp @@ -105,9 +105,8 @@ static void network_get_keys_directory(utf8* buffer, size_t bufferSize); 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); -static NetworkBase gNetwork; - -NetworkBase::NetworkBase() +NetworkBase::NetworkBase(OpenRCT2::IContext& context) + : _context(context) { wsa_initialized = false; mode = NETWORK_MODE_NONE; @@ -561,7 +560,7 @@ void NetworkBase::UpdateClient() auto intent = Intent(WC_NETWORK_STATUS); intent.putExtra(INTENT_EXTRA_MESSAGE, std::string{ str_resolving }); - intent.putExtra(INTENT_EXTRA_CALLBACK, []() -> void { gNetwork.Close(); }); + intent.putExtra(INTENT_EXTRA_CALLBACK, []() -> void { GetContext()->GetNetwork().Close(); }); context_open_intent(&intent); } break; @@ -576,7 +575,7 @@ void NetworkBase::UpdateClient() auto intent = Intent(WC_NETWORK_STATUS); intent.putExtra(INTENT_EXTRA_MESSAGE, std::string{ str_connecting }); - intent.putExtra(INTENT_EXTRA_CALLBACK, []() -> void { gNetwork.Close(); }); + intent.putExtra(INTENT_EXTRA_CALLBACK, []() -> void { GetContext()->GetNetwork().Close(); }); context_open_intent(&intent); server_connect_time = platform_get_ticks(); @@ -593,7 +592,7 @@ void NetworkBase::UpdateClient() auto intent = Intent(WC_NETWORK_STATUS); intent.putExtra(INTENT_EXTRA_MESSAGE, std::string{ str_authenticating }); - intent.putExtra(INTENT_EXTRA_CALLBACK, []() -> void { gNetwork.Close(); }); + intent.putExtra(INTENT_EXTRA_CALLBACK, []() -> void { GetContext()->GetNetwork().Close(); }); context_open_intent(&intent); break; } @@ -1990,7 +1989,7 @@ void NetworkBase::ServerClientDisconnected(std::unique_ptr& c network_get_current_player_id() }; auto res = GameActions::Execute(&pickupAction); } - gNetwork.Server_Send_EVENT_PLAYER_DISCONNECTED( + Server_Send_EVENT_PLAYER_DISCONNECTED( const_cast(connection_player->Name.c_str()), connection->GetLastDisconnectReason()); // Log player disconnected event @@ -2339,7 +2338,7 @@ void NetworkBase::Client_Handle_OBJECTS_LIST(NetworkConnection& connection, Netw auto intent = Intent(WC_NETWORK_STATUS); intent.putExtra(INTENT_EXTRA_MESSAGE, std::string{ objectListMsg }); - intent.putExtra(INTENT_EXTRA_CALLBACK, []() -> void { gNetwork.Close(); }); + intent.putExtra(INTENT_EXTRA_CALLBACK, []() -> void { GetContext()->GetNetwork().Close(); }); context_open_intent(&intent); char objectName[12]{}; @@ -2665,7 +2664,7 @@ void NetworkBase::Client_Handle_MAP([[maybe_unused]] NetworkConnection& connecti auto intent = Intent(WC_NETWORK_STATUS); intent.putExtra(INTENT_EXTRA_MESSAGE, std::string{ str_downloading_map }); - intent.putExtra(INTENT_EXTRA_CALLBACK, []() -> void { gNetwork.Close(); }); + intent.putExtra(INTENT_EXTRA_CALLBACK, []() -> void { GetContext()->GetNetwork().Close(); }); context_open_intent(&intent); std::memcpy(&chunk_buffer[offset], const_cast(static_cast(packet.Read(chunksize))), chunksize); @@ -3220,137 +3219,143 @@ void NetworkBase::Client_Handle_GAMEINFO([[maybe_unused]] NetworkConnection& con void network_set_env(const std::shared_ptr& env) { - gNetwork.SetEnvironment(env); + OpenRCT2::GetContext()->GetNetwork().SetEnvironment(env); } void network_close() { - gNetwork.Close(); + OpenRCT2::GetContext()->GetNetwork().Close(); } void network_reconnect() { - gNetwork.Reconnect(); + OpenRCT2::GetContext()->GetNetwork().Reconnect(); } void network_shutdown_client() { - gNetwork.ServerClientDisconnected(); + OpenRCT2::GetContext()->GetNetwork().ServerClientDisconnected(); } int32_t network_begin_client(const std::string& host, int32_t port) { - return gNetwork.BeginClient(host, port); + return OpenRCT2::GetContext()->GetNetwork().BeginClient(host, port); } int32_t network_begin_server(int32_t port, const std::string& address) { - return gNetwork.BeginServer(port, address); + return OpenRCT2::GetContext()->GetNetwork().BeginServer(port, address); } void network_update() { - gNetwork.Update(); + OpenRCT2::GetContext()->GetNetwork().Update(); } void network_process_pending() { - gNetwork.ProcessPending(); + OpenRCT2::GetContext()->GetNetwork().ProcessPending(); } void network_flush() { - gNetwork.Flush(); + OpenRCT2::GetContext()->GetNetwork().Flush(); } int32_t network_get_mode() { - return gNetwork.GetMode(); + return OpenRCT2::GetContext()->GetNetwork().GetMode(); } int32_t network_get_status() { - return gNetwork.GetStatus(); + return OpenRCT2::GetContext()->GetNetwork().GetStatus(); } bool network_is_desynchronised() { - return gNetwork.IsDesynchronised(); + return OpenRCT2::GetContext()->GetNetwork().IsDesynchronised(); } bool network_check_desynchronisation() { - return gNetwork.CheckDesynchronizaton(); + return OpenRCT2::GetContext()->GetNetwork().CheckDesynchronizaton(); } void network_request_gamestate_snapshot() { - return gNetwork.RequestStateSnapshot(); + return OpenRCT2::GetContext()->GetNetwork().RequestStateSnapshot(); } void network_send_tick() { - gNetwork.Server_Send_TICK(); + OpenRCT2::GetContext()->GetNetwork().Server_Send_TICK(); } NetworkAuth network_get_authstatus() { - return gNetwork.GetAuthStatus(); + return OpenRCT2::GetContext()->GetNetwork().GetAuthStatus(); } uint32_t network_get_server_tick() { - return gNetwork.GetServerTick(); + return OpenRCT2::GetContext()->GetNetwork().GetServerTick(); } uint8_t network_get_current_player_id() { - return gNetwork.GetPlayerID(); + return OpenRCT2::GetContext()->GetNetwork().GetPlayerID(); } int32_t network_get_num_players() { - return static_cast(gNetwork.player_list.size()); + return static_cast(OpenRCT2::GetContext()->GetNetwork().player_list.size()); } const char* network_get_player_name(uint32_t index) { - Guard::IndexInRange(index, gNetwork.player_list); + auto& network = OpenRCT2::GetContext()->GetNetwork(); + Guard::IndexInRange(index, network.player_list); - return static_cast(gNetwork.player_list[index]->Name.c_str()); + return static_cast(network.player_list[index]->Name.c_str()); } uint32_t network_get_player_flags(uint32_t index) { - Guard::IndexInRange(index, gNetwork.player_list); + auto& network = OpenRCT2::GetContext()->GetNetwork(); + Guard::IndexInRange(index, network.player_list); - return gNetwork.player_list[index]->Flags; + return network.player_list[index]->Flags; } int32_t network_get_player_ping(uint32_t index) { - Guard::IndexInRange(index, gNetwork.player_list); + auto& network = OpenRCT2::GetContext()->GetNetwork(); + Guard::IndexInRange(index, network.player_list); - return gNetwork.player_list[index]->Ping; + return network.player_list[index]->Ping; } int32_t network_get_player_id(uint32_t index) { - Guard::IndexInRange(index, gNetwork.player_list); + auto& network = OpenRCT2::GetContext()->GetNetwork(); + Guard::IndexInRange(index, network.player_list); - return gNetwork.player_list[index]->Id; + return network.player_list[index]->Id; } money32 network_get_player_money_spent(uint32_t index) { - Guard::IndexInRange(index, gNetwork.player_list); + auto& network = OpenRCT2::GetContext()->GetNetwork(); + Guard::IndexInRange(index, network.player_list); - return gNetwork.player_list[index]->MoneySpent; + return network.player_list[index]->MoneySpent; } std::string network_get_player_ip_address(uint32_t id) { - auto conn = gNetwork.GetPlayerConnection(id); + auto& network = OpenRCT2::GetContext()->GetNetwork(); + auto conn = network.GetPlayerConnection(id); if (conn != nullptr && conn->Socket != nullptr) { return conn->Socket->GetIpAddress(); @@ -3360,7 +3365,8 @@ std::string network_get_player_ip_address(uint32_t id) std::string network_get_player_public_key_hash(uint32_t id) { - auto player = gNetwork.GetPlayerByID(id); + auto& network = OpenRCT2::GetContext()->GetNetwork(); + auto player = network.GetPlayerByID(id); if (player != nullptr) { return player->KeyHash; @@ -3370,104 +3376,117 @@ std::string network_get_player_public_key_hash(uint32_t id) void network_add_player_money_spent(uint32_t index, money32 cost) { - Guard::IndexInRange(index, gNetwork.player_list); + auto& network = OpenRCT2::GetContext()->GetNetwork(); + Guard::IndexInRange(index, network.player_list); - gNetwork.player_list[index]->AddMoneySpent(cost); + network.player_list[index]->AddMoneySpent(cost); } int32_t network_get_player_last_action(uint32_t index, int32_t time) { - Guard::IndexInRange(index, gNetwork.player_list); + auto& network = OpenRCT2::GetContext()->GetNetwork(); + Guard::IndexInRange(index, network.player_list); - if (time && platform_get_ticks() > gNetwork.player_list[index]->LastActionTime + time) + if (time && platform_get_ticks() > network.player_list[index]->LastActionTime + time) { return -999; } - return gNetwork.player_list[index]->LastAction; + return network.player_list[index]->LastAction; } void network_set_player_last_action(uint32_t index, GameCommand command) { - Guard::IndexInRange(index, gNetwork.player_list); + auto& network = OpenRCT2::GetContext()->GetNetwork(); + Guard::IndexInRange(index, network.player_list); - gNetwork.player_list[index]->LastAction = static_cast(NetworkActions::FindCommand(command)); - gNetwork.player_list[index]->LastActionTime = platform_get_ticks(); + network.player_list[index]->LastAction = static_cast(NetworkActions::FindCommand(command)); + network.player_list[index]->LastActionTime = platform_get_ticks(); } CoordsXYZ network_get_player_last_action_coord(uint32_t index) { - Guard::IndexInRange(index, gNetwork.player_list); + auto& network = OpenRCT2::GetContext()->GetNetwork(); + Guard::IndexInRange(index, OpenRCT2::GetContext()->GetNetwork().player_list); - return gNetwork.player_list[index]->LastActionCoord; + return network.player_list[index]->LastActionCoord; } void network_set_player_last_action_coord(uint32_t index, const CoordsXYZ& coord) { - Guard::IndexInRange(index, gNetwork.player_list); + auto& network = OpenRCT2::GetContext()->GetNetwork(); + Guard::IndexInRange(index, network.player_list); - if (index < gNetwork.player_list.size()) + if (index < network.player_list.size()) { - gNetwork.player_list[index]->LastActionCoord = coord; + network.player_list[index]->LastActionCoord = coord; } } uint32_t network_get_player_commands_ran(uint32_t index) { - Guard::IndexInRange(index, gNetwork.player_list); + auto& network = OpenRCT2::GetContext()->GetNetwork(); + Guard::IndexInRange(index, OpenRCT2::GetContext()->GetNetwork().player_list); - return gNetwork.player_list[index]->CommandsRan; + return network.player_list[index]->CommandsRan; } int32_t network_get_player_index(uint32_t id) { - auto it = gNetwork.GetPlayerIteratorByID(id); - if (it == gNetwork.player_list.end()) + auto& network = OpenRCT2::GetContext()->GetNetwork(); + auto it = network.GetPlayerIteratorByID(id); + if (it == network.player_list.end()) { return -1; } - return static_cast(gNetwork.GetPlayerIteratorByID(id) - gNetwork.player_list.begin()); + return static_cast(network.GetPlayerIteratorByID(id) - network.player_list.begin()); } uint8_t network_get_player_group(uint32_t index) { - Guard::IndexInRange(index, gNetwork.player_list); + auto& network = OpenRCT2::GetContext()->GetNetwork(); + Guard::IndexInRange(index, network.player_list); - return gNetwork.player_list[index]->Group; + return network.player_list[index]->Group; } void network_set_player_group(uint32_t index, uint32_t groupindex) { - Guard::IndexInRange(index, gNetwork.player_list); - Guard::IndexInRange(groupindex, gNetwork.group_list); + auto& network = OpenRCT2::GetContext()->GetNetwork(); + Guard::IndexInRange(index, network.player_list); + Guard::IndexInRange(groupindex, network.group_list); - gNetwork.player_list[index]->Group = gNetwork.group_list[groupindex]->Id; + network.player_list[index]->Group = network.group_list[groupindex]->Id; } int32_t network_get_group_index(uint8_t id) { - auto it = gNetwork.GetGroupIteratorByID(id); - if (it == gNetwork.group_list.end()) + auto& network = OpenRCT2::GetContext()->GetNetwork(); + auto it = network.GetGroupIteratorByID(id); + if (it == network.group_list.end()) { return -1; } - return static_cast(gNetwork.GetGroupIteratorByID(id) - gNetwork.group_list.begin()); + return static_cast(network.GetGroupIteratorByID(id) - network.group_list.begin()); } uint8_t network_get_group_id(uint32_t index) { - Guard::IndexInRange(index, gNetwork.group_list); + auto& network = OpenRCT2::GetContext()->GetNetwork(); + Guard::IndexInRange(index, network.group_list); - return gNetwork.group_list[index]->Id; + return network.group_list[index]->Id; } int32_t network_get_num_groups() { - return static_cast(gNetwork.group_list.size()); + auto& network = OpenRCT2::GetContext()->GetNetwork(); + return static_cast(network.group_list.size()); } const char* network_get_group_name(uint32_t index) { - return gNetwork.group_list[index]->GetName().c_str(); + auto& network = OpenRCT2::GetContext()->GetNetwork(); + return network.group_list[index]->GetName().c_str(); } void network_chat_show_connected_message() @@ -3501,15 +3520,16 @@ void network_chat_show_server_greeting() GameActions::Result::Ptr network_set_player_group( NetworkPlayerId_t actionPlayerId, NetworkPlayerId_t playerId, uint8_t groupId, bool isExecuting) { - NetworkPlayer* player = gNetwork.GetPlayerByID(playerId); + auto& network = OpenRCT2::GetContext()->GetNetwork(); + NetworkPlayer* player = network.GetPlayerByID(playerId); - NetworkGroup* fromgroup = gNetwork.GetGroupByID(actionPlayerId); + NetworkGroup* fromgroup = network.GetGroupByID(actionPlayerId); if (player == nullptr) { return std::make_unique(GameActions::Status::InvalidParameters, STR_CANT_DO_THIS); } - if (!gNetwork.GetGroupByID(groupId)) + if (!network.GetGroupByID(groupId)) { return std::make_unique(GameActions::Status::InvalidParameters, STR_CANT_DO_THIS); } @@ -3532,18 +3552,18 @@ GameActions::Result::Ptr network_set_player_group( if (network_get_mode() == NETWORK_MODE_SERVER) { // Add or update saved user - NetworkUserManager* userManager = &gNetwork._userManager; - NetworkUser* networkUser = userManager->GetOrAddUser(player->KeyHash); + NetworkUserManager& userManager = network._userManager; + NetworkUser* networkUser = userManager.GetOrAddUser(player->KeyHash); networkUser->GroupId = groupId; networkUser->Name = player->Name; - userManager->Save(); + userManager.Save(); } window_invalidate_by_number(WC_PLAYER, playerId); // Log set player group event - NetworkPlayer* game_command_player = gNetwork.GetPlayerByID(actionPlayerId); - NetworkGroup* new_player_group = gNetwork.GetGroupByID(groupId); + NetworkPlayer* game_command_player = network.GetPlayerByID(actionPlayerId); + NetworkGroup* new_player_group = network.GetGroupByID(groupId); char log_msg[256]; const char* args[3] = { player->Name.c_str(), @@ -3560,13 +3580,14 @@ GameActions::Result::Ptr network_modify_groups( NetworkPlayerId_t actionPlayerId, ModifyGroupType type, uint8_t groupId, const std::string& name, uint32_t permissionIndex, PermissionState permissionState, bool isExecuting) { + auto& network = OpenRCT2::GetContext()->GetNetwork(); switch (type) { case ModifyGroupType::AddGroup: { if (isExecuting) { - NetworkGroup* newgroup = gNetwork.AddGroup(); + NetworkGroup* newgroup = network.AddGroup(); if (newgroup == nullptr) { return std::make_unique(GameActions::Status::Unknown, STR_CANT_DO_THIS); @@ -3581,7 +3602,7 @@ GameActions::Result::Ptr network_modify_groups( return std::make_unique( GameActions::Status::Disallowed, STR_THIS_GROUP_CANNOT_BE_MODIFIED); } - for (const auto& it : gNetwork.player_list) + for (const auto& it : network.player_list) { if ((it.get())->Group == groupId) { @@ -3591,7 +3612,7 @@ GameActions::Result::Ptr network_modify_groups( } if (isExecuting) { - gNetwork.RemoveGroup(groupId); + network.RemoveGroup(groupId); } } break; @@ -3603,11 +3624,11 @@ GameActions::Result::Ptr network_modify_groups( GameActions::Status::Disallowed, STR_THIS_GROUP_CANNOT_BE_MODIFIED); } NetworkGroup* mygroup = nullptr; - NetworkPlayer* player = gNetwork.GetPlayerByID(actionPlayerId); + NetworkPlayer* player = network.GetPlayerByID(actionPlayerId); auto networkPermission = static_cast(permissionIndex); if (player != nullptr && permissionState == PermissionState::Toggle) { - mygroup = gNetwork.GetGroupByID(player->Group); + mygroup = network.GetGroupByID(player->Group); if (mygroup == nullptr || !mygroup->CanPerformAction(networkPermission)) { return std::make_unique( @@ -3616,7 +3637,7 @@ GameActions::Result::Ptr network_modify_groups( } if (isExecuting) { - NetworkGroup* group = gNetwork.GetGroupByID(groupId); + NetworkGroup* group = network.GetGroupByID(groupId); if (group != nullptr) { if (permissionState != PermissionState::Toggle) @@ -3643,7 +3664,7 @@ GameActions::Result::Ptr network_modify_groups( break; case ModifyGroupType::SetName: { - NetworkGroup* group = gNetwork.GetGroupByID(groupId); + NetworkGroup* group = network.GetGroupByID(groupId); const char* oldName = group->GetName().c_str(); if (strcmp(oldName, name.c_str()) == 0) @@ -3674,7 +3695,7 @@ GameActions::Result::Ptr network_modify_groups( } if (isExecuting) { - gNetwork.SetDefaultGroup(groupId); + network.SetDefaultGroup(groupId); } } break; @@ -3683,14 +3704,15 @@ GameActions::Result::Ptr network_modify_groups( return std::make_unique(GameActions::Status::InvalidParameters, STR_NONE); } - gNetwork.SaveGroups(); + network.SaveGroups(); return std::make_unique(); } GameActions::Result::Ptr network_kick_player(NetworkPlayerId_t playerId, bool isExecuting) { - NetworkPlayer* player = gNetwork.GetPlayerByID(playerId); + auto& network = OpenRCT2::GetContext()->GetNetwork(); + NetworkPlayer* player = network.GetPlayerByID(playerId); if (player == nullptr) { // Player might be already removed by the PLAYERLIST command, need to refactor non-game commands executing too @@ -3705,14 +3727,14 @@ GameActions::Result::Ptr network_kick_player(NetworkPlayerId_t playerId, bool is if (isExecuting) { - if (gNetwork.GetMode() == NETWORK_MODE_SERVER) + if (network.GetMode() == NETWORK_MODE_SERVER) { - gNetwork.KickPlayer(playerId); + network.KickPlayer(playerId); - NetworkUserManager* networkUserManager = &gNetwork._userManager; - networkUserManager->Load(); - networkUserManager->RemoveUser(player->KeyHash); - networkUserManager->Save(); + NetworkUserManager& networkUserManager = network._userManager; + networkUserManager.Load(); + networkUserManager.RemoveUser(player->KeyHash); + networkUserManager.Save(); } } return std::make_unique(); @@ -3720,7 +3742,8 @@ GameActions::Result::Ptr network_kick_player(NetworkPlayerId_t playerId, bool is uint8_t network_get_default_group() { - return gNetwork.GetDefaultGroup(); + auto& network = OpenRCT2::GetContext()->GetNetwork(); + return network.GetDefaultGroup(); } int32_t network_get_num_actions() @@ -3742,27 +3765,30 @@ rct_string_id network_get_action_name_string_id(uint32_t index) int32_t network_can_perform_action(uint32_t groupindex, NetworkPermission index) { - Guard::IndexInRange(groupindex, gNetwork.group_list); + auto& network = OpenRCT2::GetContext()->GetNetwork(); + Guard::IndexInRange(groupindex, network.group_list); - return gNetwork.group_list[groupindex]->CanPerformAction(index); + return network.group_list[groupindex]->CanPerformAction(index); } int32_t network_can_perform_command(uint32_t groupindex, int32_t index) { - Guard::IndexInRange(groupindex, gNetwork.group_list); + auto& network = OpenRCT2::GetContext()->GetNetwork(); + Guard::IndexInRange(groupindex, network.group_list); - return gNetwork.group_list[groupindex]->CanPerformCommand(static_cast(index)); // TODO + return network.group_list[groupindex]->CanPerformCommand(static_cast(index)); // TODO } void network_set_pickup_peep(uint8_t playerid, Peep* peep) { - if (gNetwork.GetMode() == NETWORK_MODE_NONE) + auto& network = OpenRCT2::GetContext()->GetNetwork(); + if (network.GetMode() == NETWORK_MODE_NONE) { _pickup_peep = peep; } else { - NetworkPlayer* player = gNetwork.GetPlayerByID(playerid); + NetworkPlayer* player = network.GetPlayerByID(playerid); if (player) { player->PickupPeep = peep; @@ -3772,13 +3798,14 @@ void network_set_pickup_peep(uint8_t playerid, Peep* peep) Peep* network_get_pickup_peep(uint8_t playerid) { - if (gNetwork.GetMode() == NETWORK_MODE_NONE) + auto& network = OpenRCT2::GetContext()->GetNetwork(); + if (network.GetMode() == NETWORK_MODE_NONE) { return _pickup_peep; } else { - NetworkPlayer* player = gNetwork.GetPlayerByID(playerid); + NetworkPlayer* player = network.GetPlayerByID(playerid); if (player) { return player->PickupPeep; @@ -3789,13 +3816,14 @@ Peep* network_get_pickup_peep(uint8_t playerid) void network_set_pickup_peep_old_x(uint8_t playerid, int32_t x) { - if (gNetwork.GetMode() == NETWORK_MODE_NONE) + auto& network = OpenRCT2::GetContext()->GetNetwork(); + if (network.GetMode() == NETWORK_MODE_NONE) { _pickup_peep_old_x = x; } else { - NetworkPlayer* player = gNetwork.GetPlayerByID(playerid); + NetworkPlayer* player = network.GetPlayerByID(playerid); if (player) { player->PickupPeepOldX = x; @@ -3805,13 +3833,14 @@ void network_set_pickup_peep_old_x(uint8_t playerid, int32_t x) int32_t network_get_pickup_peep_old_x(uint8_t playerid) { - if (gNetwork.GetMode() == NETWORK_MODE_NONE) + auto& network = OpenRCT2::GetContext()->GetNetwork(); + if (network.GetMode() == NETWORK_MODE_NONE) { return _pickup_peep_old_x; } else { - NetworkPlayer* player = gNetwork.GetPlayerByID(playerid); + NetworkPlayer* player = network.GetPlayerByID(playerid); if (player) { return player->PickupPeepOldX; @@ -3822,7 +3851,8 @@ int32_t network_get_pickup_peep_old_x(uint8_t playerid) int32_t network_get_current_player_group_index() { - NetworkPlayer* player = gNetwork.GetPlayerByID(gNetwork.GetPlayerID()); + auto& network = OpenRCT2::GetContext()->GetNetwork(); + NetworkPlayer* player = network.GetPlayerByID(network.GetPlayerID()); if (player) { return network_get_group_index(player->Group); @@ -3832,31 +3862,33 @@ int32_t network_get_current_player_group_index() void network_send_map() { - gNetwork.Server_Send_MAP(); + auto& network = OpenRCT2::GetContext()->GetNetwork(); + network.Server_Send_MAP(); } void network_send_chat(const char* text, const std::vector& playerIds) { - if (gNetwork.GetMode() == NETWORK_MODE_CLIENT) + auto& network = OpenRCT2::GetContext()->GetNetwork(); + if (network.GetMode() == NETWORK_MODE_CLIENT) { - gNetwork.Client_Send_CHAT(text); + network.Client_Send_CHAT(text); } - else if (gNetwork.GetMode() == NETWORK_MODE_SERVER) + else if (network.GetMode() == NETWORK_MODE_SERVER) { std::string message = text; - if (ProcessChatMessagePluginHooks(gNetwork.GetPlayerID(), message)) + if (ProcessChatMessagePluginHooks(network.GetPlayerID(), message)) { - auto player = gNetwork.GetPlayerByID(gNetwork.GetPlayerID()); + auto player = network.GetPlayerByID(network.GetPlayerID()); if (player != nullptr) { - auto formatted = gNetwork.FormatChat(player, message.c_str()); + auto formatted = network.FormatChat(player, message.c_str()); if (playerIds.empty() - || std::find(playerIds.begin(), playerIds.end(), gNetwork.GetPlayerID()) != playerIds.end()) + || std::find(playerIds.begin(), playerIds.end(), network.GetPlayerID()) != playerIds.end()) { // Server is one of the recipients chat_history_add(formatted); } - gNetwork.Server_Send_CHAT(formatted, playerIds); + network.Server_Send_CHAT(formatted, playerIds); } } } @@ -3864,19 +3896,21 @@ void network_send_chat(const char* text, const std::vector& playerIds) void network_send_game_action(const GameAction* action) { - switch (gNetwork.GetMode()) + auto& network = OpenRCT2::GetContext()->GetNetwork(); + switch (network.GetMode()) { case NETWORK_MODE_SERVER: - gNetwork.Server_Send_GAME_ACTION(action); + network.Server_Send_GAME_ACTION(action); break; case NETWORK_MODE_CLIENT: - gNetwork.Client_Send_GAME_ACTION(action); + network.Client_Send_GAME_ACTION(action); break; } } void network_send_password(const std::string& password) { + auto& network = OpenRCT2::GetContext()->GetNetwork(); utf8 keyPath[MAX_PATH]; network_get_private_key_path(keyPath, sizeof(keyPath), gConfigNetwork.player_name); if (!Platform::FileExists(keyPath)) @@ -3887,36 +3921,39 @@ void network_send_password(const std::string& password) try { auto fs = FileStream(keyPath, FILE_MODE_OPEN); - gNetwork._key.LoadPrivate(&fs); + network._key.LoadPrivate(&fs); } catch (const std::exception&) { log_error("Error reading private key from %s.", keyPath); return; } - const std::string pubkey = gNetwork._key.PublicKeyString(); + const std::string pubkey = network._key.PublicKeyString(); std::vector signature; - gNetwork._key.Sign(gNetwork._challenge.data(), gNetwork._challenge.size(), signature); + network._key.Sign(network._challenge.data(), network._challenge.size(), signature); // 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(); - gNetwork.Client_Send_AUTH(gConfigNetwork.player_name.c_str(), password, pubkey.c_str(), signature); + network._key.Unload(); + network.Client_Send_AUTH(gConfigNetwork.player_name.c_str(), password, pubkey.c_str(), signature); } void network_set_password(const char* password) { - gNetwork.SetPassword(password); + auto& network = OpenRCT2::GetContext()->GetNetwork(); + network.SetPassword(password); } void network_append_chat_log(const utf8* text) { - gNetwork.AppendChatLog(text); + auto& network = OpenRCT2::GetContext()->GetNetwork(); + network.AppendChatLog(text); } void network_append_server_log(const utf8* text) { - gNetwork.AppendServerLog(text); + auto& network = OpenRCT2::GetContext()->GetNetwork(); + network.AppendServerLog(text); } static void network_get_keys_directory(utf8* buffer, size_t bufferSize) @@ -3942,27 +3979,33 @@ static void network_get_public_key_path(utf8* buffer, size_t bufferSize, const s const utf8* network_get_server_name() { - return gNetwork.ServerName.c_str(); + auto& network = OpenRCT2::GetContext()->GetNetwork(); + return network.ServerName.c_str(); } const utf8* network_get_server_description() { - return gNetwork.ServerDescription.c_str(); + auto& network = OpenRCT2::GetContext()->GetNetwork(); + return network.ServerDescription.c_str(); } const utf8* network_get_server_greeting() { - return gNetwork.ServerGreeting.c_str(); + auto& network = OpenRCT2::GetContext()->GetNetwork(); + return network.ServerGreeting.c_str(); } const utf8* network_get_server_provider_name() { - return gNetwork.ServerProviderName.c_str(); + auto& network = OpenRCT2::GetContext()->GetNetwork(); + return network.ServerProviderName.c_str(); } const utf8* network_get_server_provider_email() { - return gNetwork.ServerProviderEmail.c_str(); + auto& network = OpenRCT2::GetContext()->GetNetwork(); + return network.ServerProviderEmail.c_str(); } const utf8* network_get_server_provider_website() { - return gNetwork.ServerProviderWebsite.c_str(); + auto& network = OpenRCT2::GetContext()->GetNetwork(); + return network.ServerProviderWebsite.c_str(); } std::string network_get_version() @@ -3972,12 +4015,14 @@ std::string network_get_version() NetworkStats_t network_get_stats() { - return gNetwork.GetStats(); + auto& network = OpenRCT2::GetContext()->GetNetwork(); + return network.GetStats(); } NetworkServerState_t network_get_server_state() { - return gNetwork.GetServerState(); + auto& network = OpenRCT2::GetContext()->GetNetwork(); + return network.GetServerState(); } bool network_gamestate_snapshots_enabled() @@ -3987,7 +4032,8 @@ bool network_gamestate_snapshots_enabled() json_t network_get_server_info_as_json() { - return gNetwork.GetServerInfoAsJson(); + auto& network = OpenRCT2::GetContext()->GetNetwork(); + return network.GetServerInfoAsJson(); } #else int32_t network_get_mode() diff --git a/src/openrct2/network/NetworkBase.h b/src/openrct2/network/NetworkBase.h index 8981377d9e..7d98f6d1ab 100644 --- a/src/openrct2/network/NetworkBase.h +++ b/src/openrct2/network/NetworkBase.h @@ -12,10 +12,17 @@ #ifndef DISABLE_NETWORK +namespace OpenRCT2 +{ + struct IContext; +} + class NetworkBase { + OpenRCT2::IContext& _context; + public: - NetworkBase(); + NetworkBase(OpenRCT2::IContext& context); public: // Uncategorized bool BeginServer(uint16_t port, const std::string& address);