2018-03-17 19:31:06 +00:00
|
|
|
/*****************************************************************************
|
|
|
|
|
* Copyright (c) 2014-2018 OpenRCT2 developers
|
|
|
|
|
*
|
|
|
|
|
* For a complete list of all authors, please refer to contributors.md
|
|
|
|
|
* Interested in contributing? Visit https://github.com/OpenRCT2/OpenRCT2
|
|
|
|
|
*
|
|
|
|
|
* OpenRCT2 is licensed under the GNU General Public License version 3.
|
|
|
|
|
*****************************************************************************/
|
|
|
|
|
|
|
|
|
|
#pragma once
|
|
|
|
|
|
|
|
|
|
#include "../common.h"
|
2018-03-31 00:08:59 +01:00
|
|
|
#include "../core/FileWatcher.h"
|
2018-03-18 23:35:58 +00:00
|
|
|
#include "HookEngine.h"
|
2018-03-18 16:27:48 +00:00
|
|
|
#include "Plugin.h"
|
2018-07-31 17:59:48 +02:00
|
|
|
|
2018-03-17 19:31:06 +00:00
|
|
|
#include <future>
|
2018-03-23 23:24:36 +00:00
|
|
|
#include <memory>
|
2018-03-31 00:08:59 +01:00
|
|
|
#include <mutex>
|
2018-03-17 19:31:06 +00:00
|
|
|
#include <queue>
|
|
|
|
|
#include <string>
|
2020-02-10 18:44:42 +00:00
|
|
|
#include <unordered_set>
|
2018-03-31 00:08:59 +01:00
|
|
|
#include <vector>
|
2018-03-17 19:31:06 +00:00
|
|
|
|
|
|
|
|
struct duk_hthread;
|
|
|
|
|
typedef struct duk_hthread duk_context;
|
|
|
|
|
|
2018-03-31 00:08:59 +01:00
|
|
|
class FileWatcher;
|
2018-03-17 19:31:06 +00:00
|
|
|
class InteractiveConsole;
|
|
|
|
|
|
|
|
|
|
namespace OpenRCT2
|
|
|
|
|
{
|
|
|
|
|
interface IPlatformEnvironment;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
namespace OpenRCT2::Scripting
|
|
|
|
|
{
|
2018-03-18 23:35:58 +00:00
|
|
|
class ScriptExecutionInfo
|
|
|
|
|
{
|
|
|
|
|
private:
|
2018-03-23 22:39:12 +00:00
|
|
|
std::shared_ptr<Plugin> _plugin;
|
2018-03-18 23:35:58 +00:00
|
|
|
|
|
|
|
|
public:
|
2018-03-23 23:24:36 +00:00
|
|
|
class PluginScope
|
|
|
|
|
{
|
|
|
|
|
private:
|
|
|
|
|
ScriptExecutionInfo& _execInfo;
|
|
|
|
|
std::shared_ptr<Plugin> _plugin;
|
|
|
|
|
|
|
|
|
|
public:
|
|
|
|
|
PluginScope(ScriptExecutionInfo& execInfo, std::shared_ptr<Plugin> plugin)
|
2018-07-31 17:59:48 +02:00
|
|
|
: _execInfo(execInfo)
|
|
|
|
|
, _plugin(plugin)
|
2018-03-23 23:24:36 +00:00
|
|
|
{
|
|
|
|
|
_execInfo._plugin = plugin;
|
|
|
|
|
}
|
|
|
|
|
PluginScope(const PluginScope&) = delete;
|
|
|
|
|
~PluginScope()
|
|
|
|
|
{
|
|
|
|
|
_execInfo._plugin = nullptr;
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
2018-07-31 17:59:48 +02:00
|
|
|
std::shared_ptr<Plugin> GetCurrentPlugin()
|
|
|
|
|
{
|
|
|
|
|
return _plugin;
|
|
|
|
|
}
|
2018-03-18 23:35:58 +00:00
|
|
|
};
|
|
|
|
|
|
2018-03-23 23:39:46 +00:00
|
|
|
class DukContext
|
|
|
|
|
{
|
|
|
|
|
private:
|
2018-07-31 17:59:48 +02:00
|
|
|
duk_context* _context{};
|
2018-03-23 23:39:46 +00:00
|
|
|
|
|
|
|
|
public:
|
|
|
|
|
DukContext();
|
|
|
|
|
DukContext(DukContext&) = delete;
|
2020-02-06 23:15:20 +00:00
|
|
|
DukContext(DukContext&& src) noexcept
|
2018-03-23 23:39:46 +00:00
|
|
|
: _context(std::move(src._context))
|
|
|
|
|
{
|
|
|
|
|
}
|
|
|
|
|
~DukContext();
|
|
|
|
|
|
2018-07-31 17:59:48 +02:00
|
|
|
operator duk_context*()
|
|
|
|
|
{
|
|
|
|
|
return _context;
|
|
|
|
|
}
|
2018-03-23 23:39:46 +00:00
|
|
|
};
|
|
|
|
|
|
2018-03-17 19:31:06 +00:00
|
|
|
class ScriptEngine
|
|
|
|
|
{
|
|
|
|
|
private:
|
|
|
|
|
InteractiveConsole& _console;
|
|
|
|
|
IPlatformEnvironment& _env;
|
2018-03-23 23:39:46 +00:00
|
|
|
DukContext _context;
|
2018-03-17 23:26:55 +00:00
|
|
|
bool _initialised{};
|
2020-02-06 23:15:20 +00:00
|
|
|
bool _pluginsLoaded{};
|
|
|
|
|
bool _pluginsStarted{};
|
2018-03-17 19:31:06 +00:00
|
|
|
std::queue<std::tuple<std::promise<void>, std::string>> _evalQueue;
|
2018-03-23 22:39:12 +00:00
|
|
|
std::vector<std::shared_ptr<Plugin>> _plugins;
|
2018-07-31 16:25:50 +02:00
|
|
|
uint32_t _lastHotReloadCheckTick{};
|
2018-03-18 23:35:58 +00:00
|
|
|
HookEngine _hookEngine;
|
|
|
|
|
ScriptExecutionInfo _execInfo;
|
2018-03-17 19:31:06 +00:00
|
|
|
|
2018-03-31 00:08:59 +01:00
|
|
|
std::unique_ptr<FileWatcher> _pluginFileWatcher;
|
2020-02-10 18:44:42 +00:00
|
|
|
std::unordered_set<std::string> _changedPluginFiles;
|
2018-03-31 00:08:59 +01:00
|
|
|
std::mutex _changedPluginFilesMutex;
|
2020-02-11 21:26:05 +00:00
|
|
|
std::vector<std::function<void(std::shared_ptr<Plugin>)>> _pluginStoppedSubscriptions;
|
2018-03-31 00:08:59 +01:00
|
|
|
|
2018-03-17 19:31:06 +00:00
|
|
|
public:
|
|
|
|
|
ScriptEngine(InteractiveConsole& console, IPlatformEnvironment& env);
|
2018-03-18 16:27:48 +00:00
|
|
|
ScriptEngine(ScriptEngine&) = delete;
|
2018-03-17 19:31:06 +00:00
|
|
|
|
2018-07-31 17:59:48 +02:00
|
|
|
duk_context* GetContext()
|
|
|
|
|
{
|
|
|
|
|
return _context;
|
|
|
|
|
}
|
|
|
|
|
HookEngine& GetHookEngine()
|
|
|
|
|
{
|
|
|
|
|
return _hookEngine;
|
|
|
|
|
}
|
|
|
|
|
ScriptExecutionInfo& GetExecInfo()
|
|
|
|
|
{
|
|
|
|
|
return _execInfo;
|
|
|
|
|
}
|
2020-02-22 22:18:22 +00:00
|
|
|
std::vector<std::shared_ptr<Plugin>>& GetPlugins()
|
|
|
|
|
{
|
|
|
|
|
return _plugins;
|
|
|
|
|
}
|
2018-03-18 23:35:58 +00:00
|
|
|
|
2020-02-06 23:15:20 +00:00
|
|
|
void LoadPlugins();
|
|
|
|
|
void UnloadPlugins();
|
2018-03-17 19:31:06 +00:00
|
|
|
void Update();
|
2018-07-31 17:59:48 +02:00
|
|
|
std::future<void> Eval(const std::string& s);
|
2018-03-18 16:27:48 +00:00
|
|
|
|
2020-02-06 23:15:20 +00:00
|
|
|
void LogPluginInfo(const std::shared_ptr<Plugin>& plugin, const std::string_view& message);
|
|
|
|
|
|
2020-02-11 21:26:05 +00:00
|
|
|
void SubscribeToPluginStoppedEvent(std::function<void(std::shared_ptr<Plugin>)> callback)
|
|
|
|
|
{
|
|
|
|
|
_pluginStoppedSubscriptions.push_back(callback);
|
|
|
|
|
}
|
|
|
|
|
|
2020-02-22 22:18:22 +00:00
|
|
|
void AddNetworkPlugin(const std::string_view& code);
|
|
|
|
|
|
2018-03-18 16:27:48 +00:00
|
|
|
private:
|
|
|
|
|
void Initialise();
|
|
|
|
|
void StartPlugins();
|
2020-02-06 23:15:20 +00:00
|
|
|
void StopPlugins();
|
2020-02-10 18:44:42 +00:00
|
|
|
void LoadPlugin(const std::string& path);
|
2020-02-22 22:18:22 +00:00
|
|
|
void LoadPlugin(std::shared_ptr<Plugin>& plugin);
|
2020-02-11 21:26:05 +00:00
|
|
|
void StopPlugin(std::shared_ptr<Plugin> plugin);
|
2020-02-06 21:55:53 +00:00
|
|
|
bool ShouldLoadScript(const std::string& path);
|
2020-02-22 22:18:22 +00:00
|
|
|
bool ShouldStartPlugin(const std::shared_ptr<Plugin> &plugin);
|
2020-02-10 18:44:42 +00:00
|
|
|
void SetupHotReloading();
|
2018-03-18 17:43:47 +00:00
|
|
|
void AutoReloadPlugins();
|
2020-02-06 23:15:20 +00:00
|
|
|
void ProcessREPL();
|
2018-03-17 19:31:06 +00:00
|
|
|
};
|
2018-07-31 17:59:48 +02:00
|
|
|
} // namespace OpenRCT2::Scripting
|