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

107 lines
3.2 KiB
C++
Raw Normal View History

2018-03-18 16:27:48 +00:00
/*****************************************************************************
2018-06-17 15:48:42 +02:00
* Copyright (c) 2014-2018 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
*****************************************************************************/
#include "Plugin.h"
2018-07-31 17:59:48 +02:00
2019-07-24 21:08:03 +01:00
#include "..\OpenRCT2.h"
2019-07-23 23:21:42 +01:00
#include "Duktape.hpp"
2018-07-31 17:59:48 +02:00
#include <algorithm>
2018-03-18 16:27:48 +00:00
#include <fstream>
#include <memory>
using namespace OpenRCT2::Scripting;
2018-07-31 17:59:48 +02:00
Plugin::Plugin(duk_context* context, const std::string& path)
: _context(context)
, _path(path)
2018-03-18 16:27:48 +00:00
{
}
void Plugin::Load()
{
2019-07-24 21:08:03 +01:00
std::string projectedVariables = "console,context,map,network,park";
if (!gOpenRCT2Headless)
{
projectedVariables += ",ui";
}
2018-03-18 16:27:48 +00:00
std::string code;
{
std::ifstream fs(_path);
if (fs.is_open())
{
2018-07-31 17:59:48 +02:00
fs.seekg(0, std::ios::end);
2018-03-18 16:27:48 +00:00
code.reserve(fs.tellg());
fs.seekg(0, std::ios::beg);
2018-07-31 17:59:48 +02:00
code.assign(std::istreambuf_iterator<char>(fs), std::istreambuf_iterator<char>());
2018-03-18 16:27:48 +00:00
}
}
// Wrap the script in a function and pass the global objects as variables
// so that if the script modifies them, they are not modified for other scripts.
code = "(function(" + projectedVariables + "){" + code + "})(" + projectedVariables + ");";
auto flags = DUK_COMPILE_EVAL | DUK_COMPILE_SAFE | DUK_COMPILE_NOSOURCE | DUK_COMPILE_NOFILENAME;
auto result = duk_eval_raw(_context, code.c_str(), code.size(), flags);
if (result != DUK_ERR_NONE)
{
auto val = std::string(duk_safe_to_string(_context, -1));
duk_pop(_context);
throw std::runtime_error("Failed to load plug-in script: " + val);
}
_metadata = GetMetadata(DukValue::take_from_stack(_context));
}
void Plugin::Start()
{
const auto& mainFunc = _metadata.Main;
2018-03-18 17:43:47 +00:00
if (mainFunc.context() == nullptr)
{
throw std::runtime_error("No main function specified.");
}
2018-03-18 16:27:48 +00:00
mainFunc.push();
auto result = duk_pcall(_context, 0);
if (result != DUK_ERR_NONE)
{
auto val = std::string(duk_safe_to_string(_context, -1));
duk_pop(_context);
throw std::runtime_error("[" + _metadata.Name + "] " + val);
}
duk_pop(_context);
}
2018-03-18 17:43:47 +00:00
void Plugin::Update()
{
}
2018-03-18 16:27:48 +00:00
PluginMetadata Plugin::GetMetadata(const DukValue& dukMetadata)
{
PluginMetadata metadata;
if (dukMetadata.type() == DukValue::Type::OBJECT)
{
metadata.Name = dukMetadata["name"].as_string();
metadata.Version = dukMetadata["version"].as_string();
auto dukAuthors = dukMetadata["authors"];
dukAuthors.push();
if (dukAuthors.is_array())
{
auto elements = dukAuthors.as_array();
2018-07-31 17:59:48 +02:00
std::transform(elements.begin(), elements.end(), std::back_inserter(metadata.Authors), [](const DukValue& v) {
return v.as_string();
});
2018-03-18 16:27:48 +00:00
}
else
{
metadata.Authors = { dukAuthors.as_string() };
}
metadata.Main = dukMetadata["main"];
}
return metadata;
}