1
0
mirror of https://github.com/OpenRCT2/OpenRCT2 synced 2025-12-24 00:03:11 +01:00

Allow plugins to target a specific API version

This commit is contained in:
ZehMatt
2021-08-04 20:58:02 +03:00
parent af102e391e
commit a7bef5442b
6 changed files with 40 additions and 2 deletions

View File

@@ -135,6 +135,7 @@ declare global {
type: PluginType;
licence: string;
minApiVersion?: number;
targetApiVersion?: number;
main: () => void;
}

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,9 +58,15 @@ namespace OpenRCT2::Scripting
case EntityType::Vehicle:
return "car";
case EntityType::Guest:
return "guest";
if (targetApiVersion <= 33)
return "peep";
else
return "guest";
case EntityType::Staff:
return "staff";
if (targetApiVersion <= 33)
return "peep";
else
return "staff";
case EntityType::SteamParticle:
return "steam_particle";
case EntityType::MoneyEffect:

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

@@ -264,6 +264,8 @@ namespace OpenRCT2::Scripting
bool IsGameStateMutable();
void ThrowIfGameStateNotMutable();
int32_t GetTargetAPIVersion();
std::string Stringify(const DukValue& value);
} // namespace OpenRCT2::Scripting