Merge remote-tracking branch 'upstream/develop' into new-save-format

This commit is contained in:
ζeh Matt 2021-10-20 00:09:17 +03:00
commit 89bbe47211
No known key found for this signature in database
GPG Key ID: 18CE582C71A225B0
5 changed files with 226 additions and 182 deletions

View File

@ -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;

View File

@ -13,100 +13,15 @@
#include "../common.h"
#include "../core/DataSerialiser.h"
#include "../localisation/StringIds.h"
#include "GameActionResult.h"
#include <any>
#include <array>
#include <functional>
#include <memory>
#include <utility>
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::Result>;
GameActions::Status Error = GameActions::Status::Ok;
StringVariant ErrorTitle;
StringVariant ErrorMessage;
std::array<uint8_t, 32> 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<typename T> void SetData(const T&& data)
{
ResultData = std::forward<const T&&>(data);
}
// This function will throw std::bad_any_cast if the type mismatches.
template<typename T> T GetData() const
{
return std::any_cast<T>(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
/**
*
*/

View File

@ -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

View File

@ -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 <any>
#include <array>
#include <cstdint>
#include <limits>
#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;
}
};
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<std::underlying_type_t<Status>>::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::Result>;
GameActions::Status Error = GameActions::Status::Ok;
StringVariant ErrorTitle;
StringVariant ErrorMessage;
std::array<uint8_t, 32> 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<typename T> void SetData(const T&& data)
{
ResultData = std::forward<const T&&>(data);
}
// This function will throw std::bad_any_cast if the type mismatches.
template<typename T> T GetData() const
{
return std::any_cast<T>(ResultData);
}
};
#ifdef __WARN_SUGGEST_FINAL_METHODS__
# pragma GCC diagnostic pop
#endif
class ConstructClearResult final : public Result
{
public:
uint8_t GroundFlags{ 0 };
};
} // namespace GameActions

View File

@ -68,6 +68,7 @@
<ClInclude Include="actions\FootpathAdditionPlaceAction.h" />
<ClInclude Include="actions\FootpathAdditionRemoveAction.h" />
<ClInclude Include="actions\GameAction.h" />
<ClInclude Include="actions\GameActionResult.h" />
<ClInclude Include="actions\GuestSetFlagsAction.h" />
<ClInclude Include="actions\GuestSetNameAction.h" />
<ClInclude Include="actions\LandBuyRightsAction.h" />
@ -519,6 +520,7 @@
<ClCompile Include="actions\GameAction.cpp" />
<ClCompile Include="actions\GameActionCompat.cpp" />
<ClCompile Include="actions\GameActionRegistration.cpp" />
<ClCompile Include="actions\GameActionResult.cpp" />
<ClCompile Include="actions\GuestSetFlagsAction.cpp" />
<ClCompile Include="actions\GuestSetNameAction.cpp" />
<ClCompile Include="actions\LandBuyRightsAction.cpp" />