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

318 lines
9.9 KiB
C++
Raw Normal View History

/*****************************************************************************
2023-01-01 04:58:01 -06:00
* Copyright (c) 2014-2023 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
# include "../actions/CustomAction.h"
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>
2020-08-26 01:56:50 +01:00
# include <list>
2020-02-23 12:55:48 +00:00
# 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;
2021-11-10 15:57:44 +02:00
class GameAction;
namespace GameActions
{
class Result;
}
2018-03-31 00:08:59 +01:00
class FileWatcher;
class InteractiveConsole;
namespace OpenRCT2
{
struct IPlatformEnvironment;
}
namespace OpenRCT2::Scripting
{
2023-04-21 21:20:17 +01:00
static constexpr int32_t OPENRCT2_PLUGIN_API_VERSION = 78;
2021-08-07 20:14:06 +03:00
// Versions marking breaking changes.
static constexpr int32_t API_VERSION_33_PEEP_DEPRECATION = 33;
static constexpr int32_t API_VERSION_63_G2_REORDER = 63;
static constexpr int32_t API_VERSION_68_CUSTOM_ACTION_ARGS = 68;
static constexpr int32_t API_VERSION_77_NETWORK_IDS = 77;
2021-08-07 20:14:06 +03:00
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;
std::shared_ptr<Plugin> _backupPlugin;
bool _backupIsGameStateMutable;
2018-03-23 23:24:36 +00:00
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
{
_backupPlugin = _execInfo._plugin;
_backupIsGameStateMutable = _execInfo._isGameStateMutable;
_execInfo._plugin = std::move(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 = _backupPlugin;
_execInfo._isGameStateMutable = _backupIsGameStateMutable;
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
};
using IntervalHandle = int32_t;
struct ScriptInterval
{
std::shared_ptr<Plugin> Owner;
IntervalHandle Handle{};
uint32_t Delay{};
int64_t LastTimestamp{};
DukValue Callback;
bool Repeat{};
bool IsValid() const
{
return Handle != 0;
}
};
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{};
bool _hotReloadingInitialised{};
bool _transientPluginsEnabled{};
bool _transientPluginsStarted{};
2022-02-27 15:48:43 +00:00
bool _intransientPluginsStarted{};
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;
DukValue _parkStorage;
uint32_t _lastIntervalTimestamp{};
std::vector<ScriptInterval> _intervals;
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
2020-08-26 01:56:50 +01:00
std::list<std::shared_ptr<ScSocketBase>> _sockets;
2020-08-17 03:53:37 +01:00
# 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;
}
DukValue GetParkStorage()
{
return _parkStorage;
}
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
std::vector<std::shared_ptr<Plugin>> GetRemotePlugins()
{
std::vector<std::shared_ptr<Plugin>> res;
for (const auto& plugin : _plugins)
{
const auto& metadata = plugin->GetMetadata();
if (metadata.Type == OpenRCT2::Scripting::PluginType::Remote)
{
res.push_back(plugin);
}
}
return res;
}
void ClearParkStorage();
std::string GetParkStorageAsJSON();
void SetParkStorageFromJSON(std::string_view value);
2022-02-27 15:48:43 +00:00
void Initialise();
void LoadTransientPlugins();
void UnloadTransientPlugins();
2022-02-27 15:48:43 +00:00
void StopUnloadRegisterAllPlugins();
2021-12-02 17:35:31 +02:00
void Tick();
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);
DukValue ExecutePluginCall(
std::shared_ptr<Plugin> plugin, const DukValue& func, const DukValue& thisValue, const std::vector<DukValue>& args,
bool isGameStateMutable);
2018-03-18 16:27:48 +00:00
void LogPluginInfo(std::string_view message);
2021-01-07 01:22:22 +09:00
void LogPluginInfo(const std::shared_ptr<Plugin>& plugin, std::string_view message);
2020-02-06 23:15:20 +00:00
2020-02-11 21:26:05 +00:00
void SubscribeToPluginStoppedEvent(std::function<void(std::shared_ptr<Plugin>)> callback)
{
_pluginStoppedSubscriptions.push_back(callback);
}
2021-01-07 01:22:22 +09:00
void AddNetworkPlugin(std::string_view code);
2022-03-21 22:56:21 +00:00
void RemoveNetworkPlugins();
2020-02-22 22:18:22 +00:00
[[nodiscard]] GameActions::Result QueryOrExecuteCustomGameAction(const CustomAction& action, bool isExecute);
2020-03-02 23:55:18 +00:00
bool RegisterCustomAction(
2021-01-07 01:22:22 +09:00
const std::shared_ptr<Plugin>& plugin, std::string_view action, const DukValue& query, const DukValue& execute);
void RunGameActionHooks(const GameAction& action, GameActions::Result& result, bool isExecute);
2023-06-02 22:55:45 -04:00
[[nodiscard]] std::unique_ptr<GameAction> CreateGameAction(
const std::string& actionid, const DukValue& args, const std::string& pluginName);
[[nodiscard]] DukValue GameActionResultToDuk(const GameAction& action, const GameActions::Result& result);
2020-03-01 21:56:49 +00:00
2020-03-06 20:17:15 +00:00
void SaveSharedStorage();
IntervalHandle AddInterval(const std::shared_ptr<Plugin>& plugin, int32_t delay, bool repeat, DukValue&& callback);
void RemoveInterval(const std::shared_ptr<Plugin>& plugin, IntervalHandle handle);
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:
2022-05-10 21:54:28 +01:00
void RegisterConstants();
2021-03-02 00:17:13 +00:00
void RefreshPlugins();
std::vector<std::string> GetPluginFiles() const;
void UnregisterPlugin(std::string_view path);
void RegisterPlugin(std::string_view path);
2022-02-27 15:48:43 +00:00
void CheckAndStartPlugins();
2021-03-02 00:17:13 +00:00
void StartIntransientPlugins();
void StartTransientPlugins();
void LoadPlugin(const std::string& path);
2020-02-22 22:18:22 +00:00
void LoadPlugin(std::shared_ptr<Plugin>& plugin);
2021-03-02 00:17:13 +00:00
void UnloadPlugin(std::shared_ptr<Plugin>& plugin);
void StartPlugin(std::shared_ptr<Plugin> plugin);
2020-02-11 21:26:05 +00:00
void StopPlugin(std::shared_ptr<Plugin> plugin);
2022-02-20 00:23:15 +00:00
void ReloadPlugin(std::shared_ptr<Plugin> plugin);
2021-03-02 00:17:13 +00:00
static bool ShouldLoadScript(std::string_view path);
2020-02-23 02:00:22 +00:00
bool ShouldStartPlugin(const std::shared_ptr<Plugin>& plugin);
void SetupHotReloading();
void DoAutoReloadPluginCheck();
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);
[[nodiscard]] GameActions::Result DukToGameActionResult(const DukValue& d);
static std::string_view ExpenditureTypeToString(ExpenditureType expenditureType);
2021-01-07 01:22:22 +09:00
static ExpenditureType StringToExpenditureType(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
IntervalHandle AllocateHandle();
void UpdateIntervals();
void RemoveIntervals(const std::shared_ptr<Plugin>& plugin);
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();
int32_t GetTargetAPIVersion();
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