Merge pull request #15174 from ZehMatt/scripting/deperecate-peep

Deprecate peep in scripting
This commit is contained in:
Michael Steenbeek 2021-08-07 20:45:48 +02:00 committed by GitHub
commit e3c918ba5b
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
9 changed files with 108 additions and 18 deletions

View File

@ -1,6 +1,6 @@
0.3.4.1+ (in development)
------------------------------------------------------------------------
- Feature: [#15084] [Plugin] Add "vehicle.crash" hook
- Feature: [#15084] [Plugin] Add "vehicle.crash" hook.
- Feature: [#15143] Added a shortcut key for Giant Screenshot.
- Fix: [#13465] Creating a scenario based on a won save game results in a scenario thats instantly won.
- Fix: [#14316] Closing the Track Designs Manager window causes broken state.
@ -10,8 +10,8 @@
- Fix: [#15148] Track Designs Manager delete confirmation window doesn't display properly.
- Fix: [#15170] Plugin: incorrect label text alignment.
- Improved: [#3417] Crash dumps are now placed in their own folder.
- Change: [#8601] Revert ToonTower base block fix to re-enable support
blocking.
- Change: [#8601] Revert ToonTower base block fix to re-enable support blocking.
- Change: [#15174] [Plugin] Deprecate the type "peep" and add support to target a specific scripting api version.
0.3.4.1 (2021-07-25)
------------------------------------------------------------------------

View File

@ -135,6 +135,7 @@ declare global {
type: PluginType;
licence: string;
minApiVersion?: number;
targetApiVersion?: number;
main: () => void;
}
@ -515,13 +516,13 @@ declare global {
readonly isClientOnly: boolean;
result: boolean;
}
type VehicleCrashIntoType = "another_vehicle" | "land" | "water";
interface VehicleCrashArgs {
readonly id: number;
readonly crashIntoType: VehicleCrashIntoType;
}
type VehicleCrashIntoType = "another_vehicle" | "land" | "water";
interface VehicleCrashArgs {
readonly id: number;
readonly crashIntoType: VehicleCrashIntoType;
}
/**
* APIs for the in-game date.
@ -570,7 +571,12 @@ declare global {
getTile(x: number, y: number): Tile;
getEntity(id: number): Entity;
getAllEntities(type: EntityType): Entity[];
/**
* @deprecated since version 34, use guest or staff instead.
*/
getAllEntities(type: "peep"): Peep[];
getAllEntities(type: "guest"): Guest[];
getAllEntities(type: "staff"): Staff[];
}
type TileElementType =
@ -1064,8 +1070,13 @@ declare global {
"jumping_fountain_water" |
"litter" |
"money_effect" |
"peep" |
"steam_particle";
"guest" |
"staff" |
"steam_particle" |
/**
* @deprecated since version 34, use guest or staff instead.
*/
"peep";
/**
* Represents an object "entity" on the map that can typically moves and has a sub-tile coordinate.
@ -1076,7 +1087,7 @@ declare global {
*/
readonly id: number;
/**
* The type of entity, e.g. car, duck, litter, or peep.
* The type of entity, e.g. guest, vehicle, etc.
*/
readonly type: EntityType;
/**
@ -1214,10 +1225,16 @@ declare global {
readonly remainingDistance: number;
/**
* List of peep IDs ordered by seat.
* List of guest IDs ordered by seat.
* @deprecated since version 34, use guests instead.
*/
peeps: Array<number | null>;
/**
* List of guest IDs ordered by seat.
*/
guests: Array<number | null>;
/**
* Moves the vehicle forward or backwards along the track, relative to its current
* position. A single visible step is about 8.000 to 14.000 in distance depending
@ -1261,6 +1278,7 @@ declare global {
/**
* Represents a guest or staff member.
* @deprecated since version 34, use guest or staff instead.
*/
interface Peep extends Entity {
/**
@ -1329,6 +1347,9 @@ declare global {
"iceCream" |
"hereWeAre";
/**
* @deprecated since version 34, use EntityType instead.
*/
type PeepType = "guest" | "staff";
/**

View File

@ -41,6 +41,7 @@ registerPlugin({
authors: ['Your Name'],
type: 'remote',
licence: 'MIT',
targetApiVersion: 34,
main: main
});
```
@ -49,6 +50,12 @@ This will log a message to the terminal screen (`stdout`) when you open any park
The hot reload feature can be enabled by editing your `config.ini` file and setting `enable_hot_reloading` to `true` under `[plugin]`. When this is enabled, the game will auto-reload the script in real-time whenever you save your JavaScript file. This allows rapid development of plug-ins as you can write code and quickly preview your changes, such as closing and opening a specific custom window on startup. A demonstration of this can be found on YouTube: [OpenRCT2 plugin hot-reload demo](https://www.youtube.com/watch?v=jmjWzEhmDjk)
## Breaking changes
As of version 34 there are breaking Api changes.
> Version 34
```Entity.type will now return "guest" or "staff" instead of "peep"```
## Frequently Asked Questions
> Why was JavaScript chosen instead of LUA or Python.
@ -73,6 +80,12 @@ Scripts can consist of any behaviour and have a large memory pool available to t
The APIs for OpenRCT2 try to mimic the internal data structures as close as possible but we can only add so many at a time. The best way to grow the plug-in system is to add APIs on-demand. So if you find an API is missing, please raise an issue for it on GitHub and also feel free to submit a pull request afterwards.
> What is ```targetApiVersion```
In case there are breaking Api changes plugins can use this to keep the old Api behavior, as an example in version 34 ```Entity.Type``` would no longer return "peep" for
guests and staff, instead it would return either "guest" or "staff", so if your plugin expects "peep" you can specify the version 33 to keep the old behavior. See the list
of breaking changes. If this is not specified it will default to version 33, it is recommended to specify the current api version.
> How do I debug my script?
Debugging has not yet been implemented, but is planned. In the meantime, you can use `console.log` to print useful information for diagnosing problems.

View File

@ -15,6 +15,7 @@
# include "../OpenRCT2.h"
# include "../core/File.h"
# include "Duktape.hpp"
# include "ScriptEngine.h"
# include <algorithm>
# include <fstream>
@ -120,6 +121,7 @@ PluginMetadata Plugin::GetMetadata(const DukValue& dukMetadata)
metadata.Name = TryGetString(dukMetadata["name"], "Plugin name not specified.");
metadata.Version = TryGetString(dukMetadata["version"], "Plugin version not specified.");
metadata.Type = ParsePluginType(TryGetString(dukMetadata["type"], "Plugin type not specified."));
CheckForLicence(dukMetadata["licence"], metadata.Name);
auto dukMinApiVersion = dukMetadata["minApiVersion"];
@ -128,6 +130,12 @@ PluginMetadata Plugin::GetMetadata(const DukValue& dukMetadata)
metadata.MinApiVersion = dukMinApiVersion.as_int();
}
auto dukTargetApiVersion = dukMetadata["targetApiVersion"];
if (dukTargetApiVersion.type() == DukValue::Type::NUMBER)
{
metadata.TargetApiVersion = dukTargetApiVersion.as_int();
}
auto dukAuthors = dukMetadata["authors"];
dukAuthors.push();
if (dukAuthors.is_array())
@ -161,4 +169,13 @@ void Plugin::CheckForLicence(const DukValue& dukLicence, std::string_view plugin
log_error("Plugin %s does not specify a licence", std::string(pluginName).c_str());
}
int32_t Plugin::GetTargetAPIVersion() const
{
if (_metadata.TargetApiVersion)
return *_metadata.TargetApiVersion;
// If not specified, default to 33 since that is the API version from before 'targetAPIVersion' was introduced.
return 33;
}
#endif

View File

@ -42,6 +42,7 @@ namespace OpenRCT2::Scripting
std::vector<std::string> Authors;
PluginType Type{};
int32_t MinApiVersion{};
std::optional<int32_t> TargetApiVersion{};
DukValue Main;
};
@ -80,6 +81,8 @@ namespace OpenRCT2::Scripting
return _hasStarted;
}
int32_t GetTargetAPIVersion() const;
Plugin() = default;
Plugin(duk_context* context, const std::string& path);
Plugin(const Plugin&) = delete;

View File

@ -48,6 +48,8 @@ namespace OpenRCT2::Scripting
std::string type_get() const
{
const auto targetApiVersion = GetTargetAPIVersion();
auto entity = GetEntity();
if (entity != nullptr)
{
@ -56,8 +58,15 @@ namespace OpenRCT2::Scripting
case EntityType::Vehicle:
return "car";
case EntityType::Guest:
if (targetApiVersion <= API_VERSION_33_PEEP_DEPRECATION)
return "peep";
else
return "guest";
case EntityType::Staff:
return "peep";
if (targetApiVersion <= API_VERSION_33_PEEP_DEPRECATION)
return "peep";
else
return "staff";
case EntityType::SteamParticle:
return "steam_particle";
case EntityType::MoneyEffect:
@ -267,7 +276,8 @@ namespace OpenRCT2::Scripting
ctx, &ScVehicle::poweredAcceleration_get, &ScVehicle::poweredAcceleration_set, "poweredAcceleration");
dukglue_register_property(ctx, &ScVehicle::poweredMaxSpeed_get, &ScVehicle::poweredMaxSpeed_set, "poweredMaxSpeed");
dukglue_register_property(ctx, &ScVehicle::status_get, &ScVehicle::status_set, "status");
dukglue_register_property(ctx, &ScVehicle::peeps_get, nullptr, "peeps");
dukglue_register_property(ctx, &ScVehicle::guests_get, nullptr, "peeps");
dukglue_register_property(ctx, &ScVehicle::guests_get, nullptr, "guests");
dukglue_register_property(ctx, &ScVehicle::gForces_get, nullptr, "gForces");
dukglue_register_method(ctx, &ScVehicle::travelBy, "travelBy");
}
@ -600,7 +610,7 @@ namespace OpenRCT2::Scripting
}
}
std::vector<DukValue> peeps_get() const
std::vector<DukValue> guests_get() const
{
auto ctx = GetContext()->GetScriptEngine().GetContext();
std::vector<DukValue> result;

View File

@ -147,6 +147,20 @@ namespace OpenRCT2::Scripting
result.push_back(GetObjectAsDukValue(_context, std::make_shared<ScStaff>(sprite->sprite_index)));
}
}
else if (type == "guest")
{
for (auto sprite : EntityList<Guest>())
{
result.push_back(GetObjectAsDukValue(_context, std::make_shared<ScGuest>(sprite->sprite_index)));
}
}
else if (type == "staff")
{
for (auto sprite : EntityList<Staff>())
{
result.push_back(GetObjectAsDukValue(_context, std::make_shared<ScStaff>(sprite->sprite_index)));
}
}
else
{
duk_error(_context, DUK_ERR_ERROR, "Invalid entity type.");

View File

@ -1419,4 +1419,11 @@ void OpenRCT2::Scripting::ThrowIfGameStateNotMutable()
}
}
int32_t OpenRCT2::Scripting::GetTargetAPIVersion()
{
auto& scriptEngine = GetContext()->GetScriptEngine();
auto& execInfo = scriptEngine.GetExecInfo();
return execInfo.GetCurrentPlugin()->GetTargetAPIVersion();
}
#endif

View File

@ -46,7 +46,10 @@ namespace OpenRCT2
namespace OpenRCT2::Scripting
{
static constexpr int32_t OPENRCT2_PLUGIN_API_VERSION = 33;
static constexpr int32_t OPENRCT2_PLUGIN_API_VERSION = 34;
// Versions marking breaking changes.
static constexpr int32_t API_VERSION_33_PEEP_DEPRECATION = 33;
# ifndef DISABLE_NETWORK
class ScSocketBase;
@ -264,6 +267,8 @@ namespace OpenRCT2::Scripting
bool IsGameStateMutable();
void ThrowIfGameStateNotMutable();
int32_t GetTargetAPIVersion();
std::string Stringify(const DukValue& value);
} // namespace OpenRCT2::Scripting