From e7581fd42d9740ca68adfc2ec55edb49d51af237 Mon Sep 17 00:00:00 2001 From: rubidium42 Date: Wed, 5 May 2021 19:21:12 +0200 Subject: [PATCH] Change: [Network] Update server's NetworkServerGameInfo only when needed Split the updating in a "static" version that only needs to be called when a new map is loaded or some settings are changed, and a "dynamic" version that updates everything that changes regularly such as the current game date or the number of spectators. --- src/network/core/game_info.cpp | 13 ++++++++++--- src/network/core/game_info.h | 1 + src/network/network.cpp | 1 + src/network/network_func.h | 1 + src/network/network_server.cpp | 6 ++++++ src/openttd.cpp | 29 +++++++++++++++++++---------- src/settings.cpp | 2 ++ src/table/settings.ini | 2 ++ 8 files changed, 42 insertions(+), 13 deletions(-) diff --git a/src/network/core/game_info.cpp b/src/network/core/game_info.cpp index e5e85105e3..9f4d23b3b4 100644 --- a/src/network/core/game_info.cpp +++ b/src/network/core/game_info.cpp @@ -122,10 +122,10 @@ void CheckGameCompatibility(NetworkGameInfo &ngi) } /** - * Get the NetworkServerGameInfo structure with the latest information of the server. - * @return The current NetworkServerGameInfo. + * Fill a NetworkServerGameInfo structure with the static content, or things + * that are so static they can be updated on request from a settings change. */ -const NetworkServerGameInfo *GetCurrentNetworkServerGameInfo() +void FillStaticNetworkServerGameInfo() { _network_game_info.use_password = !StrEmpty(_settings_client.network.server_password); _network_game_info.start_date = ConvertYMDToDate(_settings_game.game_creation.starting_year, 0, 1); @@ -140,7 +140,14 @@ const NetworkServerGameInfo *GetCurrentNetworkServerGameInfo() strecpy(_network_game_info.server_name, _settings_client.network.server_name, lastof(_network_game_info.server_name)); strecpy(_network_game_info.server_revision, GetNetworkRevisionString(), lastof(_network_game_info.server_revision)); +} +/** + * Get the NetworkServerGameInfo structure with the latest information of the server. + * @return The current NetworkServerGameInfo. + */ +const NetworkServerGameInfo *GetCurrentNetworkServerGameInfo() +{ /* Client_on is used as global variable to keep track on the number of clients. */ _network_game_info.companies_on = (byte)Company::GetNumItems(); _network_game_info.spectators_on = NetworkSpectatorCount(); diff --git a/src/network/core/game_info.h b/src/network/core/game_info.h index 10f922f318..7c84d7b154 100644 --- a/src/network/core/game_info.h +++ b/src/network/core/game_info.h @@ -94,6 +94,7 @@ const char *GetNetworkRevisionString(); bool IsNetworkCompatibleVersion(const char *other); void CheckGameCompatibility(NetworkGameInfo &ngi); +void FillStaticNetworkServerGameInfo(); const NetworkServerGameInfo *GetCurrentNetworkServerGameInfo(); void DeserializeGRFIdentifier(Packet *p, GRFIdentifier *grf); diff --git a/src/network/network.cpp b/src/network/network.cpp index c00a3650fb..4708151865 100644 --- a/src/network/network.cpp +++ b/src/network/network.cpp @@ -805,6 +805,7 @@ static void NetworkInitGameInfo() strecpy(_settings_client.network.server_name, "Unnamed Server", lastof(_settings_client.network.server_name)); } + FillStaticNetworkServerGameInfo(); /* The server is a client too */ _network_game_info.clients_on = _network_dedicated ? 0 : 1; diff --git a/src/network/network_func.h b/src/network/network_func.h index 5f3e27c12f..ba26e06c09 100644 --- a/src/network/network_func.h +++ b/src/network/network_func.h @@ -68,6 +68,7 @@ void NetworkServerDailyLoop(); void NetworkServerMonthlyLoop(); void NetworkServerYearlyLoop(); void NetworkServerSendConfigUpdate(); +void NetworkServerUpdateGameInfo(); void NetworkServerShowStatusToConsole(); bool NetworkServerStart(); void NetworkServerNewCompany(const Company *company, NetworkClientInfo *ci); diff --git a/src/network/network_server.cpp b/src/network/network_server.cpp index 905290bfc5..1a9ddecfbc 100644 --- a/src/network/network_server.cpp +++ b/src/network/network_server.cpp @@ -1992,6 +1992,12 @@ void NetworkServerSendConfigUpdate() } } +/** Update the server's NetworkServerGameInfo due to changes in settings. */ +void NetworkServerUpdateGameInfo() +{ + if (_network_server) FillStaticNetworkServerGameInfo(); +} + /** * Tell that a particular company is (not) passworded. * @param company_id The company that got/removed the password. diff --git a/src/openttd.cpp b/src/openttd.cpp index 234e3a4197..d4c7da6e4c 100644 --- a/src/openttd.cpp +++ b/src/openttd.cpp @@ -832,15 +832,30 @@ void HandleExitGameRequest() } } +/** + * Triggers everything that should be triggered when starting a game. + * @param dedicated_server Whether this is a dedicated server or not. + */ +static void OnStartGame(bool dedicated_server) +{ + /* Update the local company for a loaded game. It is either always + * company #1 (eg 0) or in the case of a dedicated server a spectator */ + SetLocalCompany(dedicated_server ? COMPANY_SPECTATOR : COMPANY_FIRST); + + /* Update the static game info to set the values from the new game. */ + NetworkServerUpdateGameInfo(); + /* Execute the game-start script */ + IConsoleCmdExec("exec scripts/game_start.scr 0"); +} + static void MakeNewGameDone() { SettingsDisableElrail(_settings_game.vehicle.disable_elrails); /* In a dedicated server, the server does not play */ if (!VideoDriver::GetInstance()->HasGUI()) { - SetLocalCompany(COMPANY_SPECTATOR); + OnStartGame(true); if (_settings_client.gui.pause_on_newgame) DoCommandP(0, PM_PAUSED_NORMAL, 1, CMD_PAUSE); - IConsoleCmdExec("exec scripts/game_start.scr 0"); return; } @@ -858,9 +873,7 @@ static void MakeNewGameDone() _company_colours[c->index] = (Colours)c->colour; } - IConsoleCmdExec("exec scripts/game_start.scr 0"); - - SetLocalCompany(COMPANY_FIRST); + OnStartGame(false); InitializeRailGUI(); InitializeRoadGUI(); @@ -1032,11 +1045,7 @@ void SwitchToMode(SwitchMode new_mode) /* Reset engine pool to simplify changing engine NewGRFs in scenario editor. */ EngineOverrideManager::ResetToCurrentNewGRFConfig(); } - /* Update the local company for a loaded game. It is either always - * company #1 (eg 0) or in the case of a dedicated server a spectator */ - SetLocalCompany(_network_dedicated ? COMPANY_SPECTATOR : COMPANY_FIRST); - /* Execute the game-start script */ - IConsoleCmdExec("exec scripts/game_start.scr 0"); + OnStartGame(_network_dedicated); /* Decrease pause counter (was increased from opening load dialog) */ DoCommandP(0, PM_PAUSED_SAVELOAD, 0, CMD_PAUSE); } diff --git a/src/settings.cpp b/src/settings.cpp index c5819297fc..c0b0dd8edd 100644 --- a/src/settings.cpp +++ b/src/settings.cpp @@ -1416,6 +1416,7 @@ static bool UpdateServerPassword(int32 p1) _settings_client.network.server_password[0] = '\0'; } + NetworkServerUpdateGameInfo(); return true; } @@ -1430,6 +1431,7 @@ static bool UpdateRconPassword(int32 p1) static bool UpdateClientConfigValues(int32 p1) { + NetworkServerUpdateGameInfo(); if (_network_server) NetworkServerSendConfigUpdate(); return true; diff --git a/src/table/settings.ini b/src/table/settings.ini index 4e64bd0326..d12f58236d 100644 --- a/src/table/settings.ini +++ b/src/table/settings.ini @@ -3962,6 +3962,7 @@ type = SLE_STRB flags = SLF_NOT_IN_SAVE | SLF_NO_NETWORK_SYNC guiflags = SGF_NETWORK_ONLY def = nullptr +proc = UpdateClientConfigValues cat = SC_BASIC [SDTC_STR] @@ -4029,6 +4030,7 @@ guiflags = SGF_NETWORK_ONLY def = 25 min = 2 max = MAX_CLIENTS +proc = UpdateClientConfigValues cat = SC_BASIC [SDTC_VAR]