From a87b804386aeac9d74fd7e0799b2236d65288b37 Mon Sep 17 00:00:00 2001 From: Peter Nelson Date: Sat, 15 Mar 2025 20:09:11 +0000 Subject: [PATCH] Fix #13760: Store encoded error message inside CommandCost. (#13764) Encoded error message was previously static to avoid memmory allocation, however this causes complications. --- src/command.cpp | 4 +--- src/command_func.h | 2 +- src/command_type.h | 11 +++++------ src/error.h | 2 +- src/error_gui.cpp | 2 +- 5 files changed, 9 insertions(+), 12 deletions(-) diff --git a/src/command.cpp b/src/command.cpp index 1609fca847..529af6674e 100644 --- a/src/command.cpp +++ b/src/command.cpp @@ -235,7 +235,7 @@ std::tuple CommandHelperBase::InternalPostBefore(Commands cmd, * @param err_message Message prefix to show on error. * @param my_cmd Is the command from this client? */ -void CommandHelperBase::InternalPostResult(const CommandCost &res, TileIndex tile, bool estimate_only, bool only_sending, StringID err_message, bool my_cmd) +void CommandHelperBase::InternalPostResult(CommandCost &res, TileIndex tile, bool estimate_only, bool only_sending, StringID err_message, bool my_cmd) { int x = TileX(tile) * TILE_SIZE; int y = TileY(tile) * TILE_SIZE; @@ -407,8 +407,6 @@ void CommandCost::AddCost(const CommandCost &ret) } } -/* static */ EncodedString CommandCost::encoded_message; - /** * Return an error status, with string and parameter. * @param str StringID of error. diff --git a/src/command_func.h b/src/command_func.h index 188a51bbca..52c70f6aab 100644 --- a/src/command_func.h +++ b/src/command_func.h @@ -87,7 +87,7 @@ protected: static void InternalDoBefore(bool top_level, bool test); static void InternalDoAfter(CommandCost &res, DoCommandFlags flags, bool top_level, bool test); static std::tuple InternalPostBefore(Commands cmd, CommandFlags flags, TileIndex tile, StringID err_message, bool network_command); - static void InternalPostResult(const CommandCost &res, TileIndex tile, bool estimate_only, bool only_sending, StringID err_message, bool my_cmd); + static void InternalPostResult(CommandCost &res, TileIndex tile, bool estimate_only, bool only_sending, StringID err_message, bool my_cmd); static bool InternalExecutePrepTest(CommandFlags cmd_flags, TileIndex tile, Backup &cur_company); static std::tuple InternalExecuteValidateTestAndPrepExec(CommandCost &res, CommandFlags cmd_flags, bool estimate_only, bool network_command, Backup &cur_company); static CommandCost InternalExecuteProcessResult(Commands cmd, CommandFlags cmd_flags, const CommandCost &res_test, const CommandCost &res_exec, Money extra_cash, TileIndex tile, Backup &cur_company); diff --git a/src/command_type.h b/src/command_type.h index 011babfadf..ddc0d91cb2 100644 --- a/src/command_type.h +++ b/src/command_type.h @@ -28,8 +28,7 @@ class CommandCost { bool success; ///< Whether the command went fine up to this moment Owner owner = CompanyID::Invalid(); ///< Originator owner of error. StringID extra_message = INVALID_STRING_ID; ///< Additional warning message for when success is unset - - static EncodedString encoded_message; ///< Encoded error message, used if the error message includes parameters. + EncodedString encoded_message{}; ///< Encoded error message, used if the error message includes parameters. public: /** @@ -70,18 +69,18 @@ public: * @note Do not set an encoded message if the error is not for the local player, as it will never be seen. * @param message EncodedString message to set. */ - static void SetEncodedMessage(EncodedString &&message) + void SetEncodedMessage(EncodedString &&message) { - CommandCost::encoded_message = std::move(message); + this->encoded_message = std::move(message); } /** * Get the last encoded error message. * @returns Reference to the encoded message. */ - static EncodedString &GetEncodedMessage() + EncodedString &GetEncodedMessage() { - return CommandCost::encoded_message; + return this->encoded_message; } /** diff --git a/src/error.h b/src/error.h index fe3857da31..720677a85c 100644 --- a/src/error.h +++ b/src/error.h @@ -50,7 +50,7 @@ typedef std::list ErrorList; void ScheduleErrorMessage(ErrorList &datas); void ScheduleErrorMessage(const ErrorMessageData &data); -void ShowErrorMessage(EncodedString &&summary_msg, int x, int y, const CommandCost &cc); +void ShowErrorMessage(EncodedString &&summary_msg, int x, int y, CommandCost &cc); void ShowErrorMessage(EncodedString &&summary_msg, EncodedString &&detailed_msg, WarningLevel wl, int x = 0, int y = 0, EncodedString &&extra_msg = {}, CompanyID company = CompanyID::Invalid()); bool HideActiveErrorMessage(); diff --git a/src/error_gui.cpp b/src/error_gui.cpp index 6c53594310..ac3546ede7 100644 --- a/src/error_gui.cpp +++ b/src/error_gui.cpp @@ -287,7 +287,7 @@ void UnshowCriticalError() * @param y World Y position (TileVirtY) of the error location. Set both x and y to 0 to just center the message when there is no related error tile. * @param cc CommandCost containing the optional detailed and extra error messages shown in the second and third lines (can be empty). */ -void ShowErrorMessage(EncodedString &&summary_msg, int x, int y, const CommandCost &cc) +void ShowErrorMessage(EncodedString &&summary_msg, int x, int y, CommandCost &cc) { EncodedString error = std::move(cc.GetEncodedMessage()); if (error.empty()) error = GetEncodedStringIfValid(cc.GetErrorMessage());