mirror of
https://github.com/OpenRCT2/OpenRCT2
synced 2026-01-23 23:04:36 +01:00
Fix: GameActionResult does not comply to API specification
This commit is contained in:
@@ -1131,11 +1131,13 @@ DukValue ScriptEngine::GameActionResultToDuk(const GameAction& action, const Gam
|
||||
DukStackFrame frame(_context);
|
||||
DukObject obj(_context);
|
||||
|
||||
auto player = action.GetPlayer();
|
||||
if (player != -1)
|
||||
obj.Set("error", static_cast<duk_int_t>(result.Error));
|
||||
if (result.Error != GameActions::Status::Ok)
|
||||
{
|
||||
obj.Set("player", action.GetPlayer());
|
||||
obj.Set("errorTitle", result.GetErrorTitle());
|
||||
obj.Set("errorMessage", result.GetErrorMessage());
|
||||
}
|
||||
|
||||
if (result.Cost != MONEY32_UNDEFINED)
|
||||
{
|
||||
obj.Set("cost", result.Cost);
|
||||
@@ -1144,12 +1146,12 @@ DukValue ScriptEngine::GameActionResultToDuk(const GameAction& action, const Gam
|
||||
{
|
||||
obj.Set("position", ToDuk(_context, result.Position));
|
||||
}
|
||||
|
||||
if (result.Expenditure != ExpenditureType::Count)
|
||||
{
|
||||
obj.Set("expenditureType", ExpenditureTypeToString(result.Expenditure));
|
||||
}
|
||||
|
||||
// RideCreateAction only
|
||||
if (action.GetType() == GameCommand::CreateRide)
|
||||
{
|
||||
if (result.Error == GameActions::Status::Ok)
|
||||
@@ -1158,6 +1160,7 @@ DukValue ScriptEngine::GameActionResultToDuk(const GameAction& action, const Gam
|
||||
obj.Set("ride", rideIndex.ToUnderlying());
|
||||
}
|
||||
}
|
||||
// StaffHireNewAction only
|
||||
else if (action.GetType() == GameCommand::HireNewStaffMember)
|
||||
{
|
||||
if (result.Error == GameActions::Status::Ok)
|
||||
|
||||
@@ -242,6 +242,7 @@ namespace OpenRCT2::Scripting
|
||||
const std::shared_ptr<Plugin>& plugin, std::string_view action, const DukValue& query, const DukValue& execute);
|
||||
void RunGameActionHooks(const GameAction& action, GameActions::Result& result, bool isExecute);
|
||||
[[nodiscard]] std::unique_ptr<GameAction> CreateGameAction(const std::string& actionid, const DukValue& args);
|
||||
[[nodiscard]] DukValue GameActionResultToDuk(const GameAction& action, const GameActions::Result& result);
|
||||
|
||||
void SaveSharedStorage();
|
||||
|
||||
@@ -275,7 +276,6 @@ namespace OpenRCT2::Scripting
|
||||
void ProcessREPL();
|
||||
void RemoveCustomGameActions(const std::shared_ptr<Plugin>& plugin);
|
||||
[[nodiscard]] GameActions::Result DukToGameActionResult(const DukValue& d);
|
||||
[[nodiscard]] DukValue GameActionResultToDuk(const GameAction& action, const GameActions::Result& result);
|
||||
static std::string_view ExpenditureTypeToString(ExpenditureType expenditureType);
|
||||
static ExpenditureType StringToExpenditureType(std::string_view expenditureType);
|
||||
|
||||
|
||||
@@ -339,15 +339,15 @@ namespace OpenRCT2::Scripting
|
||||
if (isExecute)
|
||||
{
|
||||
action->SetCallback(
|
||||
[this, plugin, callback](const GameAction*, const GameActions::Result* res) -> void {
|
||||
HandleGameActionResult(plugin, *res, callback);
|
||||
[this, plugin, callback](const GameAction* act, const GameActions::Result* res) -> void {
|
||||
HandleGameActionResult(plugin, *act, *res, callback);
|
||||
});
|
||||
GameActions::Execute(action.get());
|
||||
}
|
||||
else
|
||||
{
|
||||
auto res = GameActions::Query(action.get());
|
||||
HandleGameActionResult(plugin, res, callback);
|
||||
HandleGameActionResult(plugin, *action, res, callback);
|
||||
}
|
||||
}
|
||||
else
|
||||
@@ -362,38 +362,15 @@ namespace OpenRCT2::Scripting
|
||||
}
|
||||
|
||||
void HandleGameActionResult(
|
||||
const std::shared_ptr<Plugin>& plugin, const GameActions::Result& res, const DukValue& callback)
|
||||
const std::shared_ptr<Plugin>& plugin, const GameAction& action, const GameActions::Result& res,
|
||||
const DukValue& callback)
|
||||
{
|
||||
// Construct result object
|
||||
auto& scriptEngine = GetContext()->GetScriptEngine();
|
||||
auto ctx = scriptEngine.GetContext();
|
||||
auto objIdx = duk_push_object(ctx);
|
||||
duk_push_int(ctx, static_cast<duk_int_t>(res.Error));
|
||||
duk_put_prop_string(ctx, objIdx, "error");
|
||||
|
||||
if (res.Error != GameActions::Status::Ok)
|
||||
{
|
||||
auto title = res.GetErrorTitle();
|
||||
duk_push_string(ctx, title.c_str());
|
||||
duk_put_prop_string(ctx, objIdx, "errorTitle");
|
||||
|
||||
auto message = res.GetErrorMessage();
|
||||
duk_push_string(ctx, message.c_str());
|
||||
duk_put_prop_string(ctx, objIdx, "errorMessage");
|
||||
}
|
||||
|
||||
duk_push_int(ctx, static_cast<duk_int_t>(res.Cost));
|
||||
duk_put_prop_string(ctx, objIdx, "cost");
|
||||
|
||||
duk_push_int(ctx, static_cast<duk_int_t>(res.Expenditure));
|
||||
duk_put_prop_string(ctx, objIdx, "expenditureType");
|
||||
|
||||
auto args = DukValue::take_from_stack(ctx);
|
||||
|
||||
if (callback.is_function())
|
||||
{
|
||||
auto& scriptEngine = GetContext()->GetScriptEngine();
|
||||
auto dukResult = scriptEngine.GameActionResultToDuk(action, res);
|
||||
// Call the plugin callback and pass the result object
|
||||
scriptEngine.ExecutePluginCall(plugin, callback, { args }, false);
|
||||
scriptEngine.ExecutePluginCall(plugin, callback, { dukResult }, false);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user