mirror of
https://github.com/OpenRCT2/OpenRCT2
synced 2025-12-10 09:32:29 +01:00
Merge pull request #15610 from ZehMatt/refactor/stringvariant
Replace StringVariant with std::variant
This commit is contained in:
@@ -1810,7 +1810,7 @@ static void window_ride_construction_construct(rct_window* w)
|
||||
// Used by some functions
|
||||
if (res->Error != GameActions::Status::Ok)
|
||||
{
|
||||
if (auto error = res->ErrorMessage.AsStringId())
|
||||
if (auto* error = std::get_if<rct_string_id>(&res->ErrorMessage))
|
||||
gGameCommandErrorText = *error;
|
||||
else
|
||||
gGameCommandErrorText = STR_NONE;
|
||||
|
||||
@@ -1905,7 +1905,7 @@ static void window_top_toolbar_scenery_tool_down(const ScreenCoordsXY& windowPos
|
||||
break;
|
||||
}
|
||||
|
||||
if (auto message = res->ErrorMessage.AsStringId())
|
||||
if (const auto* message = std::get_if<rct_string_id>(&res->ErrorMessage))
|
||||
{
|
||||
if (*message == STR_NOT_ENOUGH_CASH_REQUIRES || *message == STR_CAN_ONLY_BUILD_THIS_ON_WATER)
|
||||
{
|
||||
@@ -1958,7 +1958,7 @@ static void window_top_toolbar_scenery_tool_down(const ScreenCoordsXY& windowPos
|
||||
break;
|
||||
}
|
||||
|
||||
if (auto message = res->ErrorMessage.AsStringId())
|
||||
if (const auto* message = std::get_if<rct_string_id>(&res->ErrorMessage))
|
||||
{
|
||||
if (*message == STR_NOT_ENOUGH_CASH_REQUIRES || *message == STR_CAN_ONLY_BUILD_THIS_ON_WATER)
|
||||
{
|
||||
|
||||
@@ -144,11 +144,12 @@ money32 maze_set_track(
|
||||
|
||||
// NOTE: ride_construction_tooldown_construct requires them to be set.
|
||||
// Refactor result type once there's no C code referencing this function.
|
||||
if (auto title = res->ErrorTitle.AsStringId())
|
||||
if (const auto* title = std::get_if<rct_string_id>(&res->ErrorTitle))
|
||||
gGameCommandErrorTitle = *title;
|
||||
else
|
||||
gGameCommandErrorTitle = STR_NONE;
|
||||
if (auto message = res->ErrorMessage.AsStringId())
|
||||
|
||||
if (const auto* message = std::get_if<rct_string_id>(&res->ErrorMessage))
|
||||
gGameCommandErrorText = *message;
|
||||
else
|
||||
gGameCommandErrorText = STR_NONE;
|
||||
|
||||
@@ -25,32 +25,28 @@ namespace GameActions
|
||||
std::copy_n(args, ErrorMessageArgs.size(), ErrorMessageArgs.begin());
|
||||
}
|
||||
|
||||
struct StringVariantVisitor
|
||||
{
|
||||
const void* ErrorMessageArgs{};
|
||||
|
||||
std::string operator()(const std::string& str) const
|
||||
{
|
||||
return str;
|
||||
}
|
||||
std::string operator()(const rct_string_id strId) const
|
||||
{
|
||||
return format_string(strId, ErrorMessageArgs);
|
||||
}
|
||||
};
|
||||
|
||||
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;
|
||||
return std::visit(StringVariantVisitor{ ErrorMessageArgs.data() }, ErrorTitle);
|
||||
}
|
||||
|
||||
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;
|
||||
return std::visit(StringVariantVisitor{ ErrorMessageArgs.data() }, ErrorMessage);
|
||||
}
|
||||
|
||||
} // namespace GameActions
|
||||
|
||||
@@ -19,59 +19,7 @@
|
||||
#include <memory>
|
||||
#include <string>
|
||||
#include <type_traits>
|
||||
|
||||
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;
|
||||
}
|
||||
};
|
||||
#include <variant>
|
||||
|
||||
namespace GameActions
|
||||
{
|
||||
@@ -114,10 +62,11 @@ namespace GameActions
|
||||
{
|
||||
public:
|
||||
using Ptr = std::unique_ptr<GameActions::Result>;
|
||||
using StringVariant = std::variant<std::string, rct_string_id>;
|
||||
|
||||
GameActions::Status Error = GameActions::Status::Ok;
|
||||
StringVariant ErrorTitle;
|
||||
StringVariant ErrorMessage;
|
||||
StringVariant ErrorTitle = STR_NONE;
|
||||
StringVariant ErrorMessage = STR_NONE;
|
||||
std::array<uint8_t, 32> ErrorMessageArgs{};
|
||||
CoordsXYZ Position = { LOCATION_NULL, LOCATION_NULL, LOCATION_NULL };
|
||||
money32 Cost = 0;
|
||||
|
||||
@@ -130,9 +130,8 @@ GameActions::Result::Ptr LandSetHeightAction::Query() const
|
||||
CREATE_CROSSING_MODE_NONE);
|
||||
if (clearResult->Error != GameActions::Status::Ok)
|
||||
{
|
||||
return std::make_unique<GameActions::Result>(
|
||||
GameActions::Status::Disallowed, STR_NONE, clearResult->ErrorMessage.GetStringId(),
|
||||
clearResult->ErrorMessageArgs.data());
|
||||
clearResult->Error = GameActions::Status::Disallowed;
|
||||
return clearResult;
|
||||
}
|
||||
|
||||
tileElement = CheckUnremovableObstructions(reinterpret_cast<TileElement*>(surfaceElement), zCorner);
|
||||
|
||||
@@ -608,7 +608,7 @@ GameActions::Result::Ptr LandSmoothAction::SmoothLand(bool isExecuting) const
|
||||
}
|
||||
default:
|
||||
log_error("Invalid map selection %u", _selectionType);
|
||||
return MakeResult(GameActions::Status::InvalidParameters, res->ErrorTitle.GetStringId());
|
||||
return MakeResult(GameActions::Status::InvalidParameters, std::get<rct_string_id>(res->ErrorTitle));
|
||||
} // switch selectionType
|
||||
|
||||
// Raise / lower the land tool selection area
|
||||
|
||||
@@ -109,9 +109,8 @@ GameActions::Result::Ptr MazeSetTrackAction::Query() const
|
||||
auto constructResult = MapCanConstructAt({ _loc.ToTileStart(), baseHeight, clearanceHeight }, { 0b1111, 0 });
|
||||
if (constructResult->Error != GameActions::Status::Ok)
|
||||
{
|
||||
return MakeResult(
|
||||
GameActions::Status::NoClearance, res->ErrorTitle.GetStringId(), constructResult->ErrorMessage.GetStringId(),
|
||||
constructResult->ErrorMessageArgs.data());
|
||||
constructResult->ErrorTitle = STR_RIDE_CONSTRUCTION_CANT_CONSTRUCT_THIS_HERE;
|
||||
return constructResult;
|
||||
}
|
||||
|
||||
if (constructResult->GroundFlags & ELEMENT_IS_UNDERWATER)
|
||||
|
||||
@@ -86,9 +86,8 @@ GameActions::Result::Ptr PlaceParkEntranceAction::Query() const
|
||||
|
||||
if (auto res2 = MapCanConstructAt({ entranceLoc, zLow, zHigh }, { 0b1111, 0 }); res2->Error != GameActions::Status::Ok)
|
||||
{
|
||||
return std::make_unique<GameActions::Result>(
|
||||
GameActions::Status::NoClearance, STR_CANT_BUILD_THIS_HERE, res2->ErrorMessage.GetStringId(),
|
||||
res2->ErrorMessageArgs.data());
|
||||
res2->ErrorTitle = STR_CANT_BUILD_THIS_HERE;
|
||||
return res2;
|
||||
}
|
||||
|
||||
// Check that entrance element does not already exist at this location
|
||||
|
||||
@@ -85,8 +85,7 @@ GameActions::Result::Ptr WaterSetHeightAction::Query() const
|
||||
|
||||
if (auto res2 = MapCanConstructAt({ _coords, zLow, zHigh }, { 0b1111, 0b1111 }); res2->Error != GameActions::Status::Ok)
|
||||
{
|
||||
return MakeResult(
|
||||
GameActions::Status::NoClearance, STR_NONE, res2->ErrorMessage.GetStringId(), res2->ErrorMessageArgs.data());
|
||||
return res2;
|
||||
}
|
||||
if (surfaceElement->HasTrackThatNeedsWater())
|
||||
{
|
||||
|
||||
@@ -14,7 +14,7 @@
|
||||
constexpr const rct_string_id STR_NONE = 0xFFFF;
|
||||
constexpr const rct_string_id STR_VIEWPORT = 0xFFFE;
|
||||
|
||||
enum
|
||||
enum : uint16_t
|
||||
{
|
||||
STR_EMPTY = 0,
|
||||
STR_RIDE_NAME_DEFAULT = 1,
|
||||
|
||||
@@ -579,9 +579,10 @@ std::unique_ptr<GameActions::Result> Peep::Place(const TileCoordsXYZ& location,
|
||||
{
|
||||
tileElement = reinterpret_cast<TileElement*>(map_get_surface_element_at(location.ToCoordsXYZ()));
|
||||
}
|
||||
|
||||
if (tileElement == nullptr)
|
||||
{
|
||||
return std::make_unique<GameActions::Result>(GameActions::Status::InvalidParameters, STR_ERR_CANT_PLACE_PERSON_HERE);
|
||||
}
|
||||
|
||||
// Set the coordinate of destination to be exactly
|
||||
// in the middle of a tile.
|
||||
@@ -595,14 +596,11 @@ std::unique_ptr<GameActions::Result> Peep::Place(const TileCoordsXYZ& location,
|
||||
if (auto res = MapCanConstructAt({ destination, destination.z, destination.z + (1 * 8) }, { 0b1111, 0 });
|
||||
res->Error != GameActions::Status::Ok)
|
||||
{
|
||||
if (res->ErrorMessage.GetStringId() != STR_RAISE_OR_LOWER_LAND_FIRST)
|
||||
const auto stringId = std::get<rct_string_id>(res->ErrorMessage);
|
||||
if (stringId != STR_RAISE_OR_LOWER_LAND_FIRST && stringId != STR_FOOTPATH_IN_THE_WAY)
|
||||
{
|
||||
if (res->ErrorMessage.GetStringId() != STR_FOOTPATH_IN_THE_WAY)
|
||||
{
|
||||
return std::make_unique<GameActions::Result>(
|
||||
GameActions::Status::NoClearance, STR_ERR_CANT_PLACE_PERSON_HERE, res->ErrorMessage.GetStringId(),
|
||||
res->ErrorMessageArgs.data());
|
||||
}
|
||||
return std::make_unique<GameActions::Result>(
|
||||
GameActions::Status::NoClearance, STR_ERR_CANT_PLACE_PERSON_HERE, stringId, res->ErrorMessageArgs.data());
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user