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

105 lines
2.5 KiB
C++
Raw Normal View History

2018-03-18 16:27:48 +00:00
/*****************************************************************************
2020-04-22 16:06:29 +01:00
* Copyright (c) 2014-2020 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
*****************************************************************************/
#pragma once
#ifdef ENABLE_SCRIPTING
2019-07-23 23:21:42 +01:00
2020-02-23 15:33:01 +00:00
# include "Duktape.hpp"
# include <memory>
# include <string>
# include <string_view>
# include <vector>
2018-03-18 16:27:48 +00:00
namespace OpenRCT2::Scripting
{
2020-02-06 23:15:20 +00:00
enum class PluginType
{
/**
2020-02-24 17:40:36 +00:00
* Scripts that can run on servers or clients with no impact on the game state and will not
2020-02-06 23:15:20 +00:00
* be uploaded to clients.
*/
2020-02-24 17:40:36 +00:00
Local,
2020-02-06 23:15:20 +00:00
/**
* Scripts that can run on servers and will be uploaded to clients with ability to
* modify game state in certain contexts.
*/
2020-02-24 17:40:36 +00:00
Remote,
2020-02-06 23:15:20 +00:00
};
2018-03-18 16:27:48 +00:00
struct PluginMetadata
{
std::string Name;
std::string Version;
std::vector<std::string> Authors;
2020-04-24 13:23:39 +01:00
PluginType Type{};
2020-02-06 23:15:20 +00:00
int32_t MinApiVersion{};
std::optional<int32_t> TargetApiVersion{};
2018-03-18 16:27:48 +00:00
DukValue Main;
};
class Plugin
{
private:
2020-02-06 23:15:20 +00:00
duk_context* _context{};
2018-03-18 17:43:47 +00:00
std::string _path;
2020-04-24 13:23:39 +01:00
PluginMetadata _metadata{};
2020-02-22 22:18:22 +00:00
std::string _code;
2020-02-06 23:15:20 +00:00
bool _hasStarted{};
2018-03-18 16:27:48 +00:00
public:
2018-07-31 17:59:48 +02:00
std::string GetPath() const
{
return _path;
};
2018-03-31 00:08:59 +01:00
2020-02-22 22:18:22 +00:00
bool HasPath() const
{
2020-02-28 18:21:33 +00:00
return !_path.empty();
2020-02-22 22:18:22 +00:00
}
2020-02-06 23:15:20 +00:00
const PluginMetadata& GetMetadata() const
{
return _metadata;
}
2020-02-22 22:18:22 +00:00
const std::string& GetCode() const
{
return _code;
}
2020-02-06 23:15:20 +00:00
bool HasStarted() const
{
return _hasStarted;
}
int32_t GetTargetAPIVersion() const;
2020-04-22 16:06:29 +01:00
Plugin() = default;
2018-07-31 17:59:48 +02:00
Plugin(duk_context* context, const std::string& path);
2018-03-18 16:27:48 +00:00
Plugin(const Plugin&) = delete;
2018-03-23 22:39:12 +00:00
Plugin(Plugin&&) = delete;
2018-03-18 16:27:48 +00:00
2021-01-07 01:22:22 +09:00
void SetCode(std::string_view code);
2018-03-18 16:27:48 +00:00
void Load();
void Start();
2020-02-06 23:15:20 +00:00
void Stop();
2018-03-18 17:43:47 +00:00
2018-03-18 16:27:48 +00:00
private:
2020-02-22 22:18:22 +00:00
void LoadCodeFromFile();
2018-03-18 16:27:48 +00:00
static PluginMetadata GetMetadata(const DukValue& dukMetadata);
2021-01-07 01:22:22 +09:00
static PluginType ParsePluginType(std::string_view type);
static void CheckForLicence(const DukValue& dukLicence, std::string_view pluginName);
2018-03-18 16:27:48 +00:00
};
2018-07-31 17:59:48 +02:00
} // namespace OpenRCT2::Scripting
2020-02-23 15:33:01 +00:00
#endif