Files
OpenRCT2-Web/src/openrct2/scripting/Plugin.cpp
T

241 lines
6.8 KiB
C++
Raw Normal View History

2018-03-18 16:27:48 +00:00
/*****************************************************************************
2026-01-05 14:42:59 +01:00
* Copyright (c) 2014-2026 OpenRCT2 developers
2018-03-18 16:27:48 +00:00
*
2018-06-17 15:48:42 +02:00
* For a complete list of all authors, please refer to contributors.md
* Interested in contributing? Visit https://github.com/OpenRCT2/OpenRCT2
2018-03-18 16:27:48 +00:00
*
2018-06-17 15:48:42 +02:00
* OpenRCT2 is licensed under the GNU General Public License version 3.
2018-03-18 16:27:48 +00:00
*****************************************************************************/
#ifdef ENABLE_SCRIPTING
2018-07-31 17:59:48 +02:00
#include "Plugin.h"
2019-07-23 23:21:42 +01:00
2025-09-29 02:05:00 +10:00
#include "../Context.h"
#include "../Diagnostic.h"
#include "../OpenRCT2.h"
#include "../core/File.h"
#include "ScriptEngine.h"
2020-02-23 15:33:01 +00:00
#include <fstream>
#include <memory>
2018-03-18 16:27:48 +00:00
using namespace OpenRCT2::Scripting;
2025-09-29 02:05:00 +10:00
Plugin::Plugin(std::string_view path)
: _path(path)
2018-03-18 16:27:48 +00:00
{
}
2021-01-07 01:22:22 +09:00
void Plugin::SetCode(std::string_view code)
2020-02-22 22:18:22 +00:00
{
_code = code;
}
2025-09-29 02:05:00 +10:00
static JSValue registerPlugin(JSContext* ctx, JSValue thisVal, int argc, JSValue* argv)
{
Plugin* plugin = static_cast<Plugin*>(JS_GetContextOpaque(ctx));
plugin->SetMetadata(argv[0]);
return JS_UNDEFINED;
}
2018-03-18 16:27:48 +00:00
void Plugin::Load()
{
2025-09-29 02:05:00 +10:00
if (_context)
{
JS_FreeContext(_context);
}
2020-02-22 22:18:22 +00:00
if (!_path.empty())
{
LoadCodeFromFile();
}
2025-09-29 02:05:00 +10:00
auto& scriptEngine = OpenRCT2::GetContext()->GetScriptEngine();
_context = scriptEngine.CreateContext();
JS_SetContextOpaque(_context, this);
JSValue registerFunc = JS_NewCFunction(_context, registerPlugin, "registerPlugin", 1);
JSValue glb = JS_GetGlobalObject(_context);
JS_SetPropertyStr(_context, glb, "registerPlugin", registerFunc);
JS_FreeValue(_context, glb);
JSValue res = JS_Eval(_context, _code.c_str(), _code.length(), _path.c_str(), JS_EVAL_TYPE_GLOBAL);
if (JS_IsException(res))
2019-07-24 21:08:03 +01:00
{
2025-09-29 02:05:00 +10:00
JSValue exceptionVal = JS_GetException(_context);
std::string details = Stringify(_context, exceptionVal);
JS_FreeValue(_context, exceptionVal);
JS_FreeValue(_context, res);
JS_FreeContext(_context);
throw std::runtime_error("Failed to load plug-in script: " + _path + "\n" + details);
2019-07-24 21:08:03 +01:00
}
2025-09-29 02:05:00 +10:00
JS_FreeValue(_context, res);
2020-02-22 22:18:22 +00:00
2021-03-02 00:17:13 +00:00
_hasLoaded = true;
2018-03-18 16:27:48 +00:00
}
void Plugin::Start()
{
2021-03-02 00:17:13 +00:00
if (!_hasLoaded)
{
throw std::runtime_error("Plugin has not been loaded.");
}
2025-09-29 02:05:00 +10:00
const JSValue mainFunc = _metadata.Main.callback;
if (!JS_IsFunction(_context, mainFunc))
2018-03-18 17:43:47 +00:00
{
throw std::runtime_error("No main function specified.");
}
2022-02-25 20:05:10 +00:00
_hasStarted = true;
2025-09-29 02:05:00 +10:00
const JSValue res = JS_Call(_context, mainFunc, JS_UNDEFINED, 0, nullptr);
if (JS_IsException(res))
2018-03-18 16:27:48 +00:00
{
2025-09-29 02:05:00 +10:00
JSValue exceptionVal = JS_GetException(_context);
std::string details = Stringify(_context, exceptionVal);
JS_FreeValue(_context, exceptionVal);
JS_FreeValue(_context, res);
throw std::runtime_error("[" + _metadata.Name + "]\n" + details);
2018-03-18 16:27:48 +00:00
}
2025-09-29 02:05:00 +10:00
JS_FreeValue(_context, res);
2020-02-06 23:15:20 +00:00
}
void Plugin::StopBegin()
2020-02-06 23:15:20 +00:00
{
_isStopping = true;
}
void Plugin::StopEnd()
{
_isStopping = false;
2020-02-06 23:15:20 +00:00
_hasStarted = false;
2018-03-18 16:27:48 +00:00
}
2018-03-18 17:43:47 +00:00
2021-03-02 00:17:13 +00:00
void Plugin::Unload()
{
2025-09-29 02:05:00 +10:00
if (!_context)
{
throw std::runtime_error("Plugin is not loaded");
}
JS_FreeContext(_context);
_context = nullptr;
// https://gcc.gnu.org/bugzilla/show_bug.cgi?id=105937, fixed in GCC13
#if defined(__GNUC__) && !defined(__clang__)
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wmaybe-uninitialized"
#endif
2021-03-02 00:17:13 +00:00
_metadata.Main = {};
#if defined(__GNUC__) && !defined(__clang__)
#pragma GCC diagnostic pop
#endif
2021-03-02 00:17:13 +00:00
_hasLoaded = false;
}
2020-02-22 22:18:22 +00:00
void Plugin::LoadCodeFromFile()
{
_code = File::ReadAllText(_path);
2020-02-22 22:18:22 +00:00
}
2025-09-29 02:05:00 +10:00
std::string Plugin::TryGetString(const JSValue value, const char* property, const std::string& message) const
2020-02-06 23:15:20 +00:00
{
2025-09-29 02:05:00 +10:00
const JSValue res = JS_GetPropertyStr(_context, value, property);
if (!JS_IsString(res))
{
JS_FreeValue(_context, res);
2020-02-06 23:15:20 +00:00
throw std::runtime_error(message);
2025-09-29 02:05:00 +10:00
}
std::string str = JSToStdString(_context, res);
JS_FreeValue(_context, res);
return str;
2020-02-06 23:15:20 +00:00
}
2025-09-29 02:05:00 +10:00
void Plugin::SetMetadata(const JSValue obj)
2018-03-18 16:27:48 +00:00
{
2025-09-29 02:05:00 +10:00
PluginMetadata metadata{};
if (JS_IsObject(obj))
2018-03-18 16:27:48 +00:00
{
2025-09-29 02:05:00 +10:00
metadata.Name = TryGetString(obj, "name", "Plugin name not specified.");
metadata.Version = TryGetString(obj, "version", "Plugin version not specified.");
metadata.Type = ParsePluginType(TryGetString(obj, "type", "Plugin type not specified."));
2025-09-29 02:05:00 +10:00
const JSValue licence = JS_GetPropertyStr(_context, obj, "licence");
int64_t licenseLen = -1;
if (!JS_IsString(licence) || JS_GetLength(_context, licence, &licenseLen) == -1 || licenseLen == 0)
2020-02-06 23:15:20 +00:00
{
2025-09-29 02:05:00 +10:00
LOG_ERROR("Plugin %s does not specify a licence", _metadata.Name.c_str());
2020-02-06 23:15:20 +00:00
}
2025-09-29 02:05:00 +10:00
JS_FreeValue(_context, licence);
2018-03-18 16:27:48 +00:00
2025-09-29 02:05:00 +10:00
const JSValue minApiVersion = JS_GetPropertyStr(_context, obj, "minApiVersion");
if (JS_IsNumber(minApiVersion))
{
2025-09-29 02:05:00 +10:00
int64_t val = -1;
JS_ToInt64(_context, &val, minApiVersion);
metadata.MinApiVersion = val;
}
JS_FreeValue(_context, minApiVersion);
const JSValue targetApiVersion = JS_GetPropertyStr(_context, obj, "targetApiVersion");
if (JS_IsNumber(targetApiVersion))
{
int64_t val = -1;
JS_ToInt64(_context, &val, targetApiVersion);
metadata.TargetApiVersion = val;
}
else
{
LOG_ERROR(
u8"Plug-in “%s” does not specify a target API version or specifies it incorrectly. Emulating deprecated APIs.",
metadata.Name.c_str());
}
2025-09-29 02:05:00 +10:00
JS_FreeValue(_context, targetApiVersion);
2025-09-29 02:05:00 +10:00
const JSValue authors = JS_GetPropertyStr(_context, obj, "authors");
if (JS_IsArray(authors))
2018-03-18 16:27:48 +00:00
{
2025-09-29 02:05:00 +10:00
JSIterateArray(_context, authors, [&metadata](JSContext* ctx2, JSValue val) {
if (JS_IsString(val))
metadata.Authors.emplace_back(JSToStdString(ctx2, val));
2018-07-31 17:59:48 +02:00
});
2018-03-18 16:27:48 +00:00
}
2025-09-29 02:05:00 +10:00
else if (JS_IsString(authors))
2018-03-18 16:27:48 +00:00
{
2025-09-29 02:05:00 +10:00
metadata.Authors = { JSToStdString(_context, authors) };
2018-03-18 16:27:48 +00:00
}
2025-09-29 02:05:00 +10:00
JS_FreeValue(_context, authors);
metadata.Main = JSToCallback(_context, obj, "main");
2018-03-18 16:27:48 +00:00
}
2025-09-29 02:05:00 +10:00
_metadata = metadata;
2018-03-18 16:27:48 +00:00
}
2020-02-06 23:15:20 +00:00
2021-01-07 01:22:22 +09:00
PluginType Plugin::ParsePluginType(std::string_view type)
2020-02-06 23:15:20 +00:00
{
2020-02-24 17:40:36 +00:00
if (type == "local")
return PluginType::Local;
if (type == "remote")
return PluginType::Remote;
2021-03-02 00:17:13 +00:00
if (type == "intransient")
return PluginType::Intransient;
2020-02-06 23:15:20 +00:00
throw std::invalid_argument("Unknown plugin type.");
}
2020-02-23 15:33:01 +00:00
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;
}
2021-03-02 00:17:13 +00:00
bool Plugin::IsTransient() const
{
return _metadata.Type != PluginType::Intransient;
}
2020-02-23 15:33:01 +00:00
#endif