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

231 lines
6.8 KiB
C++
Raw Normal View History

/*****************************************************************************
2020-07-21 15:04:34 +02:00
* Copyright (c) 2014-2020 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
#ifdef ENABLE_SCRIPTING
2018-07-31 17:59:48 +02:00
2020-02-23 12:55:48 +00:00
# include "../common.h"
# include "../core/FileWatcher.h"
# include "../management/Finance.h"
2020-03-04 20:03:13 +00:00
# include "../world/Location.hpp"
2020-02-23 12:55:48 +00:00
# include "HookEngine.h"
# include "Plugin.h"
# include <future>
# include <memory>
# include <mutex>
# include <queue>
# include <string>
2020-03-02 23:55:18 +00:00
# include <unordered_map>
2020-02-23 12:55:48 +00:00
# include <unordered_set>
# include <vector>
struct duk_hthread;
typedef struct duk_hthread duk_context;
2020-03-04 20:03:13 +00:00
struct GameAction;
2020-03-01 21:56:49 +00:00
class GameActionResult;
2018-03-31 00:08:59 +01:00
class FileWatcher;
class InteractiveConsole;
namespace OpenRCT2
{
struct IPlatformEnvironment;
}
namespace OpenRCT2::Scripting
{
2020-08-17 17:46:00 +01:00
# ifndef DISABLE_NETWORK
2020-08-17 03:53:37 +01:00
class ScSocketBase;
2020-08-17 17:46:00 +01:00
# endif
2020-08-17 03:53:37 +01:00
2018-03-18 23:35:58 +00:00
class ScriptExecutionInfo
{
private:
2018-03-23 22:39:12 +00:00
std::shared_ptr<Plugin> _plugin;
2020-02-23 02:00:22 +00:00
bool _isGameStateMutable{};
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:
2020-02-23 02:00:22 +00:00
PluginScope(ScriptExecutionInfo& execInfo, std::shared_ptr<Plugin> plugin, bool isGameStateMutable)
2018-07-31 17:59:48 +02:00
: _execInfo(execInfo)
, _plugin(plugin)
2018-03-23 23:24:36 +00:00
{
_execInfo._plugin = plugin;
2020-02-23 02:00:22 +00:00
_execInfo._isGameStateMutable = isGameStateMutable;
2018-03-23 23:24:36 +00:00
}
PluginScope(const PluginScope&) = delete;
~PluginScope()
{
_execInfo._plugin = nullptr;
2020-02-23 02:00:22 +00:00
_execInfo._isGameStateMutable = false;
2018-03-23 23:24:36 +00:00
}
};
2018-07-31 17:59:48 +02:00
std::shared_ptr<Plugin> GetCurrentPlugin()
{
return _plugin;
}
2020-02-23 02:00:22 +00:00
bool IsGameStateMutable()
{
return _isGameStateMutable;
}
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))
{
src._context = {};
2018-03-23 23:39:46 +00:00
}
~DukContext();
2018-07-31 17:59:48 +02:00
operator duk_context*()
{
return _context;
}
2018-03-23 23:39:46 +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{};
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;
2020-03-06 20:17:15 +00:00
DukValue _sharedStorage;
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
struct CustomActionInfo
2020-03-02 23:55:18 +00:00
{
2020-03-04 00:01:17 +00:00
std::shared_ptr<Plugin> Owner;
2020-03-02 23:55:18 +00:00
std::string Name;
DukValue Query;
DukValue Execute;
};
std::unordered_map<std::string, CustomActionInfo> _customActions;
2020-08-17 03:53:37 +01:00
# ifndef DISABLE_NETWORK
std::vector<std::shared_ptr<ScSocketBase>> _sockets;
# endif
2020-03-02 23:55:18 +00:00
public:
ScriptEngine(InteractiveConsole& console, IPlatformEnvironment& env);
2018-03-18 16:27:48 +00:00
ScriptEngine(ScriptEngine&) = delete;
2018-07-31 17:59:48 +02:00
duk_context* GetContext()
{
return _context;
}
HookEngine& GetHookEngine()
{
return _hookEngine;
}
ScriptExecutionInfo& GetExecInfo()
{
return _execInfo;
}
2020-03-06 20:17:15 +00:00
DukValue GetSharedStorage()
{
return _sharedStorage;
}
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();
void Update();
2018-07-31 17:59:48 +02:00
std::future<void> Eval(const std::string& s);
2020-03-02 23:55:18 +00:00
DukValue ExecutePluginCall(
const std::shared_ptr<Plugin>& plugin, const DukValue& func, const std::vector<DukValue>& args,
bool isGameStateMutable);
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);
2020-03-02 23:55:18 +00:00
std::unique_ptr<GameActionResult> QueryOrExecuteCustomGameAction(
const std::string_view& id, const std::string_view& args, bool isExecute);
bool RegisterCustomAction(
const std::shared_ptr<Plugin>& plugin, const std::string_view& action, const DukValue& query,
const DukValue& execute);
2020-03-04 20:03:13 +00:00
void RunGameActionHooks(const GameAction& action, std::unique_ptr<GameActionResult>& result, bool isExecute);
std::unique_ptr<GameAction> CreateGameAction(const std::string& actionid, const DukValue& args);
2020-03-01 21:56:49 +00:00
2020-03-06 20:17:15 +00:00
void SaveSharedStorage();
2020-08-17 17:46:00 +01:00
# ifndef DISABLE_NETWORK
2020-08-17 03:53:37 +01:00
void AddSocket(const std::shared_ptr<ScSocketBase>& socket);
2020-08-17 17:46:00 +01:00
# endif
2020-08-17 03:53:37 +01:00
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);
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-23 02:00:22 +00:00
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();
2020-03-02 23:55:18 +00:00
void RemoveCustomGameActions(const std::shared_ptr<Plugin>& plugin);
std::unique_ptr<GameActionResult> DukToGameActionResult(const DukValue& d);
2020-03-04 20:03:13 +00:00
DukValue GameActionResultToDuk(const GameAction& action, const std::unique_ptr<GameActionResult>& result);
static std::string_view ExpenditureTypeToString(ExpenditureType expenditureType);
static ExpenditureType StringToExpenditureType(const std::string_view& expenditureType);
2020-03-06 20:17:15 +00:00
2020-03-07 12:37:07 +00:00
void InitSharedStorage();
2020-03-06 20:17:15 +00:00
void LoadSharedStorage();
2020-08-17 03:53:37 +01:00
void UpdateSockets();
void RemoveSockets(const std::shared_ptr<Plugin>& plugin);
};
2020-02-23 02:00:22 +00:00
bool IsGameStateMutable();
void ThrowIfGameStateNotMutable();
2020-02-25 00:43:49 +00:00
std::string Stringify(const DukValue& value);
2020-02-23 02:00:22 +00:00
2018-07-31 17:59:48 +02:00
} // namespace OpenRCT2::Scripting
2020-02-23 12:55:48 +00:00
#endif