Files
OpenRCT2-Unity/src/openrct2/scripting/ScriptEngine.h

159 lines
4.2 KiB
C
Raw Normal View History

/*****************************************************************************
* 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"
#include <future>
2018-03-23 23:24:36 +00:00
#include <memory>
2018-03-31 00:08:59 +01:00
#include <mutex>
#include <queue>
#include <string>
#include <unordered_set>
2018-03-31 00:08:59 +01:00
#include <vector>
struct duk_hthread;
typedef struct duk_hthread duk_context;
2018-03-31 00:08:59 +01:00
class FileWatcher;
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)
: _execInfo(execInfo)
, _plugin(plugin)
2018-03-23 23:24:36 +00:00
{
_execInfo._plugin = plugin;
}
PluginScope(const PluginScope&) = delete;
~PluginScope()
{
_execInfo._plugin = nullptr;
}
};
std::shared_ptr<Plugin> GetCurrentPlugin()
{
return _plugin;
}
2018-03-18 23:35:58 +00:00
};
class DukContext
{
private:
duk_context* _context{};
public:
DukContext();
DukContext(DukContext&) = delete;
2020-02-06 23:15:20 +00:00
DukContext(DukContext&& src) noexcept
: _context(std::move(src._context))
{
}
~DukContext();
operator duk_context*()
{
return _context;
}
};
class ScriptEngine
{
private:
InteractiveConsole& _console;
IPlatformEnvironment& _env;
DukContext _context;
2018-03-17 23:26:55 +00:00
bool _initialised{};
2020-02-06 23:15:20 +00:00
bool _pluginsLoaded{};
bool _pluginsStarted{};
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-31 00:08:59 +01:00
std::unique_ptr<FileWatcher> _pluginFileWatcher;
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
public:
ScriptEngine(InteractiveConsole& console, IPlatformEnvironment& env);
2018-03-18 16:27:48 +00:00
ScriptEngine(ScriptEngine&) = delete;
duk_context* GetContext()
{
return _context;
}
HookEngine& GetHookEngine()
{
return _hookEngine;
}
ScriptExecutionInfo& GetExecInfo()
{
return _execInfo;
}
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();
void Update();
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);
}
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();
void LoadPlugin(const std::string& path);
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);
bool ShouldStartPlugin(const std::shared_ptr<Plugin> &plugin);
void SetupHotReloading();
2018-03-18 17:43:47 +00:00
void AutoReloadPlugins();
2020-02-06 23:15:20 +00:00
void ProcessREPL();
};
} // namespace OpenRCT2::Scripting