From d005e8f8bd1ce2bdf2fdbcd52e45d49c633c5df9 Mon Sep 17 00:00:00 2001 From: Peter Nelson Date: Tue, 16 Jan 2024 20:50:46 +0000 Subject: [PATCH 1/4] Fix 884b9e66: No need to include extra pixel for pressed state. (#11803) --- src/widget.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/widget.cpp b/src/widget.cpp index d463bebc8e..a84fb546e9 100644 --- a/src/widget.cpp +++ b/src/widget.cpp @@ -3341,7 +3341,7 @@ std::unique_ptr MakeCompanyButtonRows(WidgetID widget_first, Widget Dimension sprite_size = GetSpriteSize(SPR_COMPANY_ICON, nullptr, ZOOM_LVL_OUT_4X); sprite_size.width += WidgetDimensions::unscaled.matrix.Horizontal(); - sprite_size.height += WidgetDimensions::unscaled.matrix.Vertical() + 1; // 1 for the 'offset' of being pressed + sprite_size.height += WidgetDimensions::unscaled.matrix.Vertical(); for (WidgetID widnum = widget_first; widnum <= widget_last; widnum++) { /* Ensure there is room in 'hor' for another button. */ From 8b4c5a6269e2fa1fd851eeaafda39ed781f2ac34 Mon Sep 17 00:00:00 2001 From: Patric Stout Date: Tue, 16 Jan 2024 21:58:55 +0100 Subject: [PATCH 2/4] Codechange: compile-time validate the string format of SlErrorCorruptFmt (#11805) --- src/saveload/saveload_error.hpp | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/src/saveload/saveload_error.hpp b/src/saveload/saveload_error.hpp index f23a81e32d..1bf3c1556c 100644 --- a/src/saveload/saveload_error.hpp +++ b/src/saveload/saveload_error.hpp @@ -20,15 +20,14 @@ void NORETURN SlErrorCorrupt(const std::string &msg); * Issue an SlErrorCorrupt with a format string. * @param format_string The formatting string to tell what to do with the remaining arguments. * @param fmt_args The arguments to be passed to fmt. - * @tparam T The type of formatting parameter. * @tparam Args The types of the fmt arguments. * @note This function does never return as it throws an exception to * break out of all the saveload code. */ -template -inline void NORETURN SlErrorCorruptFmt(const T &format, Args&&... fmt_args) +template +inline void NORETURN SlErrorCorruptFmt(const fmt::format_string format, Args&&... fmt_args) { - SlErrorCorrupt(fmt::format(format, fmt_args...)); + SlErrorCorrupt(fmt::format(format, std::forward(fmt_args)...)); } #endif /* SAVELOAD_ERROR_HPP */ From 0b7410d979118b6e0562ffbdabb23ee619f59c92 Mon Sep 17 00:00:00 2001 From: Patric Stout Date: Tue, 16 Jan 2024 22:04:35 +0100 Subject: [PATCH 3/4] Codechange: compile-time validate the string format of IConsolePrint (#11804) This means we can no longer use runtime picking what string to use. --- src/console_cmds.cpp | 26 ++++++++++++-------------- src/console_func.h | 7 +++---- src/network/network_server.cpp | 14 ++++++++------ 3 files changed, 23 insertions(+), 24 deletions(-) diff --git a/src/console_cmds.cpp b/src/console_cmds.cpp index eec523eeb3..9c72b8fe3e 100644 --- a/src/console_cmds.cpp +++ b/src/console_cmds.cpp @@ -267,20 +267,18 @@ DEF_CONSOLE_CMD(ConZoomToLevel) case 0: IConsolePrint(CC_HELP, "Set the current zoom level of the main viewport."); IConsolePrint(CC_HELP, "Usage: 'zoomto '."); - IConsolePrint( - CC_HELP, - ZOOM_LVL_MIN < _settings_client.gui.zoom_min ? - "The lowest zoom-in level allowed by current client settings is {}." : - "The lowest supported zoom-in level is {}.", - std::max(ZOOM_LVL_MIN, _settings_client.gui.zoom_min) - ); - IConsolePrint( - CC_HELP, - _settings_client.gui.zoom_max < ZOOM_LVL_MAX ? - "The highest zoom-out level allowed by current client settings is {}." : - "The highest supported zoom-out level is {}.", - std::min(_settings_client.gui.zoom_max, ZOOM_LVL_MAX) - ); + + if (ZOOM_LVL_MIN < _settings_client.gui.zoom_min) { + IConsolePrint(CC_HELP, "The lowest zoom-in level allowed by current client settings is {}.", std::max(ZOOM_LVL_MIN, _settings_client.gui.zoom_min)); + } else { + IConsolePrint(CC_HELP, "The lowest supported zoom-in level is {}.", std::max(ZOOM_LVL_MIN, _settings_client.gui.zoom_min)); + } + + if (_settings_client.gui.zoom_max < ZOOM_LVL_MAX) { + IConsolePrint(CC_HELP, "The highest zoom-out level allowed by current client settings is {}.", std::min(_settings_client.gui.zoom_max, ZOOM_LVL_MAX)); + } else { + IConsolePrint(CC_HELP, "The highest supported zoom-out level is {}.", std::min(_settings_client.gui.zoom_max, ZOOM_LVL_MAX)); + } return true; case 2: { diff --git a/src/console_func.h b/src/console_func.h index d0fc3b3ec7..bf7993b78a 100644 --- a/src/console_func.h +++ b/src/console_func.h @@ -34,16 +34,15 @@ void IConsolePrint(TextColour colour_code, const std::string &string); * @param format_string The formatting string to tell what to do with the remaining arguments. * @param first_arg The first argument to the format. * @param other_args The other arguments to the format. - * @tparam T The type of formatting parameter. * @tparam A The type of the first argument. * @tparam Args The types of the other arguments. */ -template -inline void IConsolePrint(TextColour colour_code, const T &format, A first_arg, Args&&... other_args) +template +inline void IConsolePrint(TextColour colour_code, fmt::format_string format, A first_arg, Args&&... other_args) { /* The separate first_arg argument is added to aid overloading. * Otherwise the calls that do no need formatting will still use this function. */ - IConsolePrint(colour_code, fmt::format(format, first_arg, other_args...)); + IConsolePrint(colour_code, fmt::format(format, std::forward(first_arg), std::forward(other_args)...)); } /* Parser */ diff --git a/src/network/network_server.cpp b/src/network/network_server.cpp index 8bab091caa..a22d6b2d42 100644 --- a/src/network/network_server.cpp +++ b/src/network/network_server.cpp @@ -1704,12 +1704,14 @@ void NetworkServer_Tick(bool send_frame) case NetworkClientSocket::STATUS_ACTIVE: if (lag > _settings_client.network.max_lag_time) { /* Client did still not report in within the specified limit. */ - IConsolePrint(CC_WARNING, cs->last_packet + std::chrono::milliseconds(lag * MILLISECONDS_PER_TICK) > std::chrono::steady_clock::now() ? - /* A packet was received in the last three game days, so the client is likely lagging behind. */ - "Client #{} (IP: {}) is dropped because the client's game state is more than {} ticks behind." : - /* No packet was received in the last three game days; sounds like a lost connection. */ - "Client #{} (IP: {}) is dropped because the client did not respond for more than {} ticks.", - cs->client_id, cs->GetClientIP(), lag); + + if (cs->last_packet + std::chrono::milliseconds(lag * MILLISECONDS_PER_TICK) > std::chrono::steady_clock::now()) { + /* A packet was received in the last three game days, so the client is likely lagging behind. */ + IConsolePrint(CC_WARNING, "Client #{} (IP: {}) is dropped because the client's game state is more than {} ticks behind.", cs->client_id, cs->GetClientIP(), lag); + } else { + /* No packet was received in the last three game days; sounds like a lost connection. */ + IConsolePrint(CC_WARNING, "Client #{} (IP: {}) is dropped because the client did not respond for more than {} ticks.", cs->client_id, cs->GetClientIP(), lag); + } cs->SendError(NETWORK_ERROR_TIMEOUT_COMPUTER); continue; } From 6550682b49cf5f0cc980f278fcd7bc89e674aa8f Mon Sep 17 00:00:00 2001 From: Patric Stout Date: Tue, 16 Jan 2024 22:10:34 +0100 Subject: [PATCH 4/4] Codechange: minor bits and pieces related to fmt::format() (#11806) - Don't make run-time formatting what can be done compile-time. - Be explicit about run-time formatting. - Fix datetime printing. --- src/debug.cpp | 2 +- src/gamelog.cpp | 6 +++++- src/network/core/address.cpp | 2 +- src/newgrf_profiling.cpp | 2 +- 4 files changed, 8 insertions(+), 4 deletions(-) diff --git a/src/debug.cpp b/src/debug.cpp index de70791808..6bc66532da 100644 --- a/src/debug.cpp +++ b/src/debug.cpp @@ -95,7 +95,7 @@ void DumpDebugFacilityNames(std::back_insert_iterator &output_itera } else { fmt::format_to(output_iterator, ", "); } - fmt::format_to(output_iterator, i->name); + fmt::format_to(output_iterator, "{}", i->name); written = true; } if (written) { diff --git a/src/gamelog.cpp b/src/gamelog.cpp index 6ed62159df..7555380342 100644 --- a/src/gamelog.cpp +++ b/src/gamelog.cpp @@ -242,7 +242,11 @@ void Gamelog::Print(std::function proc) { /* A NewGRF got removed from the game, either manually or by it missing when loading the game. */ auto gm = grf_names.find(this->grfid); - fmt::format_to(output_iterator, action_type == GLAT_LOAD ? "Missing NewGRF: " : "Removed NewGRF: "); + if (action_type == GLAT_LOAD) { + fmt::format_to(output_iterator, "Missing NewGRF: "); + } else { + fmt::format_to(output_iterator, "Removed NewGRF: "); + } AddGrfInfo(output_iterator, this->grfid, nullptr, gm != grf_names.end() ? gm->second.gc : nullptr); if (gm == grf_names.end()) { fmt::format_to(output_iterator, ". Gamelog inconsistency: GrfID was never added!"); diff --git a/src/network/core/address.cpp b/src/network/core/address.cpp index 81a072c6e0..525edd511d 100644 --- a/src/network/core/address.cpp +++ b/src/network/core/address.cpp @@ -93,7 +93,7 @@ static const char *GetAddressFormatString(uint16_t family, bool with_family) */ std::string NetworkAddress::GetAddressAsString(bool with_family) { - return fmt::format(GetAddressFormatString(this->GetAddress()->ss_family, with_family), this->GetHostname(), this->GetPort()); + return fmt::format(fmt::runtime(GetAddressFormatString(this->GetAddress()->ss_family, with_family)), this->GetHostname(), this->GetPort()); } /** diff --git a/src/newgrf_profiling.cpp b/src/newgrf_profiling.cpp index 221d07f2f8..115dd88b2d 100644 --- a/src/newgrf_profiling.cpp +++ b/src/newgrf_profiling.cpp @@ -131,7 +131,7 @@ void NewGRFProfiler::Abort() */ std::string NewGRFProfiler::GetOutputFilename() const { - return fmt::format("{}grfprofile-{%Y%m%d-%H%M}-{:08X}.csv", FiosGetScreenshotDir(), fmt::localtime(time(nullptr)), BSWAP32(this->grffile->grfid)); + return fmt::format("{}grfprofile-{:%Y%m%d-%H%M}-{:08X}.csv", FiosGetScreenshotDir(), fmt::localtime(time(nullptr)), BSWAP32(this->grffile->grfid)); } /* static */ uint32_t NewGRFProfiler::FinishAll()