1
0
mirror of https://github.com/OpenRCT2/OpenRCT2 synced 2026-01-22 22:34:33 +01:00

Implement strings for GameAction errors

This commit is contained in:
Ted John
2020-03-03 21:11:26 +00:00
parent 2ad37db817
commit 83fe0cf5c5
18 changed files with 135 additions and 41 deletions

View File

@@ -20,4 +20,19 @@ template<typename T> DukValue GetObjectAsDukValue(duk_context* ctx, const std::s
return DukValue::take_from_stack(ctx);
}
template<typename T>
const T AsOrDefault(const DukValue& value, const T& defaultValue = {}) = delete;
template<>
inline const std::string AsOrDefault(const DukValue& value, const std::string& defaultValue)
{
return value.type() == DukValue::STRING ? value.as_string() : defaultValue;
}
template<>
inline const int32_t AsOrDefault(const DukValue& value, const int32_t& defaultValue)
{
return value.type() == DukValue::NUMBER ? value.as_int() : defaultValue;
}
#endif

View File

@@ -160,11 +160,11 @@ namespace OpenRCT2::Scripting
if (res.Error != GA_ERROR::OK)
{
auto title = format_string(res.ErrorTitle, nullptr);
auto title = res.GetErrorTitle();
duk_push_string(ctx, title.c_str());
duk_put_prop_string(ctx, objIdx, "errorTitle");
auto message = format_string(res.ErrorMessage, res.ErrorMessageArgs.data());
auto message = res.GetErrorMessage();
duk_push_string(ctx, message.c_str());
duk_put_prop_string(ctx, objIdx, "errorMessage");
}

View File

@@ -732,10 +732,10 @@ std::unique_ptr<GameActionResult> ScriptEngine::QueryOrExecuteCustomGameAction(
std::unique_ptr<GameActionResult> ScriptEngine::DukToGameActionResult(const DukValue& d)
{
auto result = std::make_unique<GameActionResult>();
result->Error = d["error"].type() == DukValue::Type::NUMBER ? static_cast<GA_ERROR>(d["error"].as_int()) : GA_ERROR::OK;
auto errorTitle = d["errorTitle"].type() == DukValue::Type::STRING ? d["errorTitle"].as_string() : std::string();
auto errorMessage = d["errorMessage"].type() == DukValue::Type::STRING ? d["errorMessage"].as_string() : std::string();
result->Cost = d["cost"].type() == DukValue::Type::NUMBER ? d["cost"].as_int() : 0;
result->Error = static_cast<GA_ERROR>(AsOrDefault<int32_t>(d["error"]));
result->ErrorTitle = AsOrDefault<std::string>(d["errorTitle"]);
result->ErrorMessage = AsOrDefault<std::string>(d["errorMessage"]);
result->Cost = AsOrDefault<int32_t>(d["cost"]);
return result;
}