From 2d78a5efc3505cb6378dc35557f664f412c7d210 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=CE=B6eh=20Matt?= <5415177+ZehMatt@users.noreply.github.com> Date: Tue, 19 Oct 2021 13:50:24 -0700 Subject: [PATCH] Move GameActionResult into its own unit (#15588) --- src/openrct2/actions/GameAction.cpp | 49 ------- src/openrct2/actions/GameAction.h | 140 +------------------ src/openrct2/actions/GameActionResult.cpp | 56 ++++++++ src/openrct2/actions/GameActionResult.h | 161 ++++++++++++++++++++++ src/openrct2/libopenrct2.vcxproj | 2 + 5 files changed, 226 insertions(+), 182 deletions(-) create mode 100644 src/openrct2/actions/GameActionResult.cpp create mode 100644 src/openrct2/actions/GameActionResult.h diff --git a/src/openrct2/actions/GameAction.cpp b/src/openrct2/actions/GameAction.cpp index 0856d018a3..ed31433c72 100644 --- a/src/openrct2/actions/GameAction.cpp +++ b/src/openrct2/actions/GameAction.cpp @@ -34,55 +34,6 @@ using namespace OpenRCT2; namespace GameActions { - Result::Result(GameActions::Status error, rct_string_id message) - { - Error = error; - ErrorMessage = message; - } - - Result::Result(GameActions::Status error, rct_string_id title, rct_string_id message) - { - Error = error; - ErrorTitle = title; - ErrorMessage = message; - } - - Result::Result(GameActions::Status error, rct_string_id title, rct_string_id message, uint8_t* args) - { - Error = error; - ErrorTitle = title; - ErrorMessage = message; - std::copy_n(args, ErrorMessageArgs.size(), ErrorMessageArgs.begin()); - } - - std::string GameActions::Result::GetErrorTitle() const - { - std::string title; - if (auto error = ErrorTitle.AsString()) - { - title = *error; - } - else - { - title = format_string(ErrorTitle.GetStringId(), ErrorMessageArgs.data()); - } - return title; - } - - std::string GameActions::Result::GetErrorMessage() const - { - std::string message; - if (auto error = ErrorMessage.AsString()) - { - message = *error; - } - else - { - message = format_string(ErrorMessage.GetStringId(), ErrorMessageArgs.data()); - } - return message; - } - struct QueuedGameAction { uint32_t tick; diff --git a/src/openrct2/actions/GameAction.h b/src/openrct2/actions/GameAction.h index 8a567dfd23..474d8fbf84 100644 --- a/src/openrct2/actions/GameAction.h +++ b/src/openrct2/actions/GameAction.h @@ -13,100 +13,15 @@ #include "../common.h" #include "../core/DataSerialiser.h" #include "../localisation/StringIds.h" +#include "GameActionResult.h" -#include #include #include #include #include -class StringVariant -{ -private: - rct_string_id StringId = STR_NONE; - std::string String; - -public: - StringVariant() = default; - - StringVariant(rct_string_id stringId) - : StringId(stringId) - { - } - - StringVariant(const std::string& s) - : String(s) - { - } - - StringVariant(std::string&& s) - : String(std::move(s)) - { - } - - StringVariant(const char* s) - : String(s) - { - } - - const std::string* AsString() const - { - if (!String.empty()) - { - return &String; - } - return {}; - } - - const rct_string_id* AsStringId() const - { - if (String.empty()) - { - return &StringId; - } - return {}; - } - - rct_string_id GetStringId() const - { - return String.empty() ? StringId : STR_NONE; - } -}; - -#ifdef __WARN_SUGGEST_FINAL_METHODS__ -# pragma GCC diagnostic push -# pragma GCC diagnostic ignored "-Wsuggest-final-methods" -# pragma GCC diagnostic ignored "-Wsuggest-final-types" -#endif - namespace GameActions { - /** - * Common error codes for game actions. - */ - enum class Status : uint16_t - { - Ok, - InvalidParameters, - Disallowed, - GamePaused, - InsufficientFunds, - NotInEditorMode, - - NotOwned, - TooLow, - TooHigh, - NoClearance, - ItemAlreadyPlaced, - - NotClosed, - Broken, - - NoFreeElements, - - Unknown = UINT16_MAX, - }; - namespace Flags { constexpr uint16_t AllowWhilePaused = 1 << 0; @@ -114,55 +29,14 @@ namespace GameActions constexpr uint16_t EditorOnly = 1 << 2; } // namespace Flags - /** - * Represents the result of a game action query or execution. - */ - class Result - { - public: - using Ptr = std::unique_ptr; - - GameActions::Status Error = GameActions::Status::Ok; - StringVariant ErrorTitle; - StringVariant ErrorMessage; - std::array ErrorMessageArgs{}; - CoordsXYZ Position = { LOCATION_NULL, LOCATION_NULL, LOCATION_NULL }; - money32 Cost = 0; - ExpenditureType Expenditure = ExpenditureType::Count; - std::any ResultData; - - Result() = default; - Result(GameActions::Status error, rct_string_id message); - Result(GameActions::Status error, rct_string_id title, rct_string_id message); - Result(GameActions::Status error, rct_string_id title, rct_string_id message, uint8_t* args); - Result(const GameActions::Result&) = delete; - virtual ~Result(){}; - - std::string GetErrorTitle() const; - std::string GetErrorMessage() const; - - // It is recommended to use strong types since a type alias such as 'using MyType = uint32_t' - // is still just uint32_t, this guarantees the data is associated with the correct type. - template void SetData(const T&& data) - { - ResultData = std::forward(data); - } - - // This function will throw std::bad_any_cast if the type mismatches. - template T GetData() const - { - return std::any_cast(ResultData); - } - }; - - class ConstructClearResult final : public Result - { - public: - uint8_t GroundFlags{ 0 }; - }; - } // namespace GameActions +#ifdef __WARN_SUGGEST_FINAL_METHODS__ +# pragma GCC diagnostic push +# pragma GCC diagnostic ignored "-Wsuggest-final-methods" +# pragma GCC diagnostic ignored "-Wsuggest-final-types" +#endif + /** * */ diff --git a/src/openrct2/actions/GameActionResult.cpp b/src/openrct2/actions/GameActionResult.cpp new file mode 100644 index 0000000000..b00fc39416 --- /dev/null +++ b/src/openrct2/actions/GameActionResult.cpp @@ -0,0 +1,56 @@ +#include "GameActionResult.h" + +#include "../localisation/Localisation.h" + +namespace GameActions +{ + Result::Result(GameActions::Status error, rct_string_id message) + { + Error = error; + ErrorMessage = message; + } + + Result::Result(GameActions::Status error, rct_string_id title, rct_string_id message) + { + Error = error; + ErrorTitle = title; + ErrorMessage = message; + } + + Result::Result(GameActions::Status error, rct_string_id title, rct_string_id message, uint8_t* args) + { + Error = error; + ErrorTitle = title; + ErrorMessage = message; + std::copy_n(args, ErrorMessageArgs.size(), ErrorMessageArgs.begin()); + } + + std::string GameActions::Result::GetErrorTitle() const + { + std::string title; + if (auto error = ErrorTitle.AsString()) + { + title = *error; + } + else + { + title = format_string(ErrorTitle.GetStringId(), ErrorMessageArgs.data()); + } + return title; + } + + std::string GameActions::Result::GetErrorMessage() const + { + std::string message; + if (auto error = ErrorMessage.AsString()) + { + message = *error; + } + else + { + message = format_string(ErrorMessage.GetStringId(), ErrorMessageArgs.data()); + } + return message; + } + +} // namespace GameActions diff --git a/src/openrct2/actions/GameActionResult.h b/src/openrct2/actions/GameActionResult.h new file mode 100644 index 0000000000..9daab74d27 --- /dev/null +++ b/src/openrct2/actions/GameActionResult.h @@ -0,0 +1,161 @@ +/***************************************************************************** + * Copyright (c) 2014-2021 OpenRCT2 developers + * + * For a complete list of all authors, please refer to contributors.md + * Interested in contributing? Visit https://github.com/OpenRCT2/OpenRCT2 + * + * OpenRCT2 is licensed under the GNU General Public License version 3. + *****************************************************************************/ +#pragma once + +#include "../localisation/StringIds.h" +#include "../management/Finance.h" +#include "../world/Location.hpp" + +#include +#include +#include +#include +#include +#include +#include + +class StringVariant +{ +private: + rct_string_id StringId = STR_NONE; + std::string String; + +public: + StringVariant() = default; + + StringVariant(rct_string_id stringId) + : StringId(stringId) + { + } + + StringVariant(const std::string& s) + : String(s) + { + } + + StringVariant(std::string&& s) + : String(std::move(s)) + { + } + + StringVariant(const char* s) + : String(s) + { + } + + const std::string* AsString() const + { + if (!String.empty()) + { + return &String; + } + return {}; + } + + const rct_string_id* AsStringId() const + { + if (String.empty()) + { + return &StringId; + } + return {}; + } + + rct_string_id GetStringId() const + { + return String.empty() ? StringId : STR_NONE; + } +}; + +namespace GameActions +{ + /** + * Common error codes for game actions. + */ + enum class Status : uint16_t + { + Ok, + InvalidParameters, + Disallowed, + GamePaused, + InsufficientFunds, + NotInEditorMode, + + NotOwned, + TooLow, + TooHigh, + NoClearance, + ItemAlreadyPlaced, + + NotClosed, + Broken, + + NoFreeElements, + + Unknown = std::numeric_limits>::max(), + }; + +#ifdef __WARN_SUGGEST_FINAL_METHODS__ +# pragma GCC diagnostic push +# pragma GCC diagnostic ignored "-Wsuggest-final-methods" +# pragma GCC diagnostic ignored "-Wsuggest-final-types" +#endif + + /** + * Represents the result of a game action query or execution. + */ + class Result + { + public: + using Ptr = std::unique_ptr; + + GameActions::Status Error = GameActions::Status::Ok; + StringVariant ErrorTitle; + StringVariant ErrorMessage; + std::array ErrorMessageArgs{}; + CoordsXYZ Position = { LOCATION_NULL, LOCATION_NULL, LOCATION_NULL }; + money32 Cost = 0; + ExpenditureType Expenditure = ExpenditureType::Count; + std::any ResultData; + + Result() = default; + Result(GameActions::Status error, rct_string_id message); + Result(GameActions::Status error, rct_string_id title, rct_string_id message); + Result(GameActions::Status error, rct_string_id title, rct_string_id message, uint8_t* args); + Result(const GameActions::Result&) = delete; + virtual ~Result(){}; + + std::string GetErrorTitle() const; + std::string GetErrorMessage() const; + + // It is recommended to use strong types since a type alias such as 'using MyType = uint32_t' + // is still just uint32_t, this guarantees the data is associated with the correct type. + template void SetData(const T&& data) + { + ResultData = std::forward(data); + } + + // This function will throw std::bad_any_cast if the type mismatches. + template T GetData() const + { + return std::any_cast(ResultData); + } + }; + +#ifdef __WARN_SUGGEST_FINAL_METHODS__ +# pragma GCC diagnostic pop +#endif + + class ConstructClearResult final : public Result + { + public: + uint8_t GroundFlags{ 0 }; + }; + +} // namespace GameActions diff --git a/src/openrct2/libopenrct2.vcxproj b/src/openrct2/libopenrct2.vcxproj index 2e110ea5d6..26dcee802b 100644 --- a/src/openrct2/libopenrct2.vcxproj +++ b/src/openrct2/libopenrct2.vcxproj @@ -68,6 +68,7 @@ + @@ -519,6 +520,7 @@ +