From e46338a2f1905c132c785040eb718bf10a8f1f8b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=CE=B6eh=20Matt?= <5415177+ZehMatt@users.noreply.github.com> Date: Wed, 16 Feb 2022 19:41:59 +0200 Subject: [PATCH] Fix scripting API returning 0 instead of null. --- distribution/openrct2.d.ts | 8 +-- src/openrct2/scripting/Duktape.hpp | 6 ++ .../scripting/bindings/entity/ScVehicle.cpp | 59 ++++++++++++++----- .../scripting/bindings/entity/ScVehicle.hpp | 10 ++-- 4 files changed, 61 insertions(+), 22 deletions(-) diff --git a/distribution/openrct2.d.ts b/distribution/openrct2.d.ts index 416ff265fa..6823cf158a 100644 --- a/distribution/openrct2.d.ts +++ b/distribution/openrct2.d.ts @@ -1161,15 +1161,15 @@ declare global { /** * The previous car on the ride. This may be the on the same train or the previous - * train. This will point to the last car if this is the first car on the ride. + * train. This will return null if there is no previous car. */ - previousCarOnRide: number; + previousCarOnRide: number | null; /** * The next car on the ride. This may be the on the same train or the next - * train. This will point to the first car if this is the last car on the ride. + * train. This will return null if there is no next car. */ - nextCarOnRide: number; + nextCarOnRide: number | null; /** * The current station the train is in or departing. diff --git a/src/openrct2/scripting/Duktape.hpp b/src/openrct2/scripting/Duktape.hpp index abf01f670f..afe96f18e2 100644 --- a/src/openrct2/scripting/Duktape.hpp +++ b/src/openrct2/scripting/Duktape.hpp @@ -286,6 +286,12 @@ namespace OpenRCT2::Scripting return DukValue::take_from_stack(ctx); } + template<> inline DukValue ToDuk(duk_context* ctx, const uint16_t& value) + { + duk_push_int(ctx, value); + return DukValue::take_from_stack(ctx); + } + template<> inline DukValue ToDuk(duk_context* ctx, const int32_t& value) { duk_push_int(ctx, value); diff --git a/src/openrct2/scripting/bindings/entity/ScVehicle.cpp b/src/openrct2/scripting/bindings/entity/ScVehicle.cpp index bf564ab0d2..05b0cf4922 100644 --- a/src/openrct2/scripting/bindings/entity/ScVehicle.cpp +++ b/src/openrct2/scripting/bindings/entity/ScVehicle.cpp @@ -196,34 +196,65 @@ namespace OpenRCT2::Scripting } } - uint16_t ScVehicle::previousCarOnRide_get() const + DukValue ScVehicle::previousCarOnRide_get() const { - auto vehicle = GetVehicle(); - return vehicle != nullptr ? vehicle->prev_vehicle_on_ride.ToUnderlying() : EntityId::GetNull().ToUnderlying(); + auto ctx = GetContext()->GetScriptEngine().GetContext(); + + const auto* vehicle = GetVehicle(); + if (vehicle == nullptr) + return ToDuk(ctx, nullptr); + + if (vehicle->prev_vehicle_on_ride.IsNull()) + return ToDuk(ctx, nullptr); + + return ToDuk(ctx, vehicle->prev_vehicle_on_ride.ToUnderlying()); } - void ScVehicle::previousCarOnRide_set(uint16_t value) + void ScVehicle::previousCarOnRide_set(DukValue value) { ThrowIfGameStateNotMutable(); - auto vehicle = GetVehicle(); - if (vehicle != nullptr) + auto* vehicle = GetVehicle(); + if (vehicle == nullptr) + return; + + if (value.type() == DukValue::Type::NUMBER) { - vehicle->prev_vehicle_on_ride = EntityId::FromUnderlying(value); + vehicle->prev_vehicle_on_ride = EntityId::FromUnderlying(value.as_uint()); + } + else + { + vehicle->prev_vehicle_on_ride = EntityId::GetNull(); } } - uint16_t ScVehicle::nextCarOnRide_get() const + DukValue ScVehicle::nextCarOnRide_get() const { - auto vehicle = GetVehicle(); - return vehicle != nullptr ? vehicle->next_vehicle_on_ride.ToUnderlying() : EntityId::GetNull().ToUnderlying(); + auto ctx = GetContext()->GetScriptEngine().GetContext(); + + const auto* vehicle = GetVehicle(); + if (vehicle == nullptr) + return ToDuk(ctx, nullptr); + + if (vehicle->next_vehicle_on_ride.IsNull()) + return ToDuk(ctx, nullptr); + + return ToDuk(ctx, vehicle->next_vehicle_on_ride.ToUnderlying()); } - void ScVehicle::nextCarOnRide_set(uint16_t value) + void ScVehicle::nextCarOnRide_set(DukValue value) { ThrowIfGameStateNotMutable(); - auto vehicle = GetVehicle(); - if (vehicle != nullptr) + auto* vehicle = GetVehicle(); + if (vehicle == nullptr) + return; + + if (value.type() == DukValue::Type::NUMBER) { - vehicle->next_vehicle_on_ride = EntityId::FromUnderlying(value); + vehicle->next_vehicle_on_ride = EntityId::FromUnderlying(value.as_uint()); } + else + { + vehicle->next_vehicle_on_ride = EntityId::GetNull(); + } + } StationIndex ScVehicle::currentStation_get() const diff --git a/src/openrct2/scripting/bindings/entity/ScVehicle.hpp b/src/openrct2/scripting/bindings/entity/ScVehicle.hpp index 743887f1c3..cf8b0d96be 100644 --- a/src/openrct2/scripting/bindings/entity/ScVehicle.hpp +++ b/src/openrct2/scripting/bindings/entity/ScVehicle.hpp @@ -14,6 +14,8 @@ # include "../../../ride/Ride.h" # include "ScEntity.hpp" +# include + namespace OpenRCT2::Scripting { class ScVehicle : public ScEntity @@ -44,11 +46,11 @@ namespace OpenRCT2::Scripting DukValue nextCarOnTrain_get() const; void nextCarOnTrain_set(DukValue value); - uint16_t previousCarOnRide_get() const; - void previousCarOnRide_set(uint16_t value); + DukValue previousCarOnRide_get() const; + void previousCarOnRide_set(DukValue value); - uint16_t nextCarOnRide_get() const; - void nextCarOnRide_set(uint16_t value); + DukValue nextCarOnRide_get() const; + void nextCarOnRide_set(DukValue value); StationIndex currentStation_get() const; void currentStation_set(StationIndex value);