mirror of
https://github.com/OpenRCT2/OpenRCT2
synced 2025-12-24 00:03:11 +01:00
Refactor plugin scope
This commit is contained in:
@@ -82,14 +82,13 @@ void HookEngine::Call(HOOK_TYPE type)
|
|||||||
auto& hookList = GetHookList(type);
|
auto& hookList = GetHookList(type);
|
||||||
for (auto& hook : hookList.Hooks)
|
for (auto& hook : hookList.Hooks)
|
||||||
{
|
{
|
||||||
_execInfo.SetCurrentPlugin(hook.Owner);
|
ScriptExecutionInfo::PluginScope scope(_execInfo, hook.Owner);
|
||||||
|
|
||||||
const auto& function = hook.Function;
|
const auto& function = hook.Function;
|
||||||
function.push();
|
function.push();
|
||||||
duk_pcall(function.context(), 0);
|
duk_pcall(function.context(), 0);
|
||||||
duk_pop(function.context());
|
duk_pop(function.context());
|
||||||
}
|
}
|
||||||
_execInfo.SetCurrentPlugin(nullptr);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
HookList& HookEngine::GetHookList(HOOK_TYPE type)
|
HookList& HookEngine::GetHookList(HOOK_TYPE type)
|
||||||
|
|||||||
@@ -81,7 +81,7 @@ void ScriptEngine::LoadPlugins()
|
|||||||
try
|
try
|
||||||
{
|
{
|
||||||
auto plugin = std::make_shared<Plugin>(_context, path);
|
auto plugin = std::make_shared<Plugin>(_context, path);
|
||||||
_execInfo.SetCurrentPlugin(plugin);
|
ScriptExecutionInfo::PluginScope scope(_execInfo, plugin);
|
||||||
plugin->Load();
|
plugin->Load();
|
||||||
plugin->EnableHotReload();
|
plugin->EnableHotReload();
|
||||||
_plugins.push_back(std::move(plugin));
|
_plugins.push_back(std::move(plugin));
|
||||||
@@ -91,7 +91,6 @@ void ScriptEngine::LoadPlugins()
|
|||||||
_console.WriteLineError(e.what());
|
_console.WriteLineError(e.what());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
_execInfo.SetCurrentPlugin(nullptr);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void ScriptEngine::AutoReloadPlugins()
|
void ScriptEngine::AutoReloadPlugins()
|
||||||
@@ -103,7 +102,8 @@ void ScriptEngine::AutoReloadPlugins()
|
|||||||
try
|
try
|
||||||
{
|
{
|
||||||
_hookEngine.UnsubscribeAll(plugin);
|
_hookEngine.UnsubscribeAll(plugin);
|
||||||
_execInfo.SetCurrentPlugin(plugin);
|
|
||||||
|
ScriptExecutionInfo::PluginScope scope(_execInfo, plugin);
|
||||||
plugin->Load();
|
plugin->Load();
|
||||||
plugin->Start();
|
plugin->Start();
|
||||||
}
|
}
|
||||||
@@ -113,14 +113,13 @@ void ScriptEngine::AutoReloadPlugins()
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
_execInfo.SetCurrentPlugin(nullptr);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void ScriptEngine::StartPlugins()
|
void ScriptEngine::StartPlugins()
|
||||||
{
|
{
|
||||||
for (auto& plugin : _plugins)
|
for (auto& plugin : _plugins)
|
||||||
{
|
{
|
||||||
_execInfo.SetCurrentPlugin(plugin);
|
ScriptExecutionInfo::PluginScope scope(_execInfo, plugin);
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
plugin->Start();
|
plugin->Start();
|
||||||
@@ -130,7 +129,6 @@ void ScriptEngine::StartPlugins()
|
|||||||
_console.WriteLineError(e.what());
|
_console.WriteLineError(e.what());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
_execInfo.SetCurrentPlugin(nullptr);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void ScriptEngine::Update()
|
void ScriptEngine::Update()
|
||||||
|
|||||||
@@ -13,6 +13,7 @@
|
|||||||
#include "HookEngine.h"
|
#include "HookEngine.h"
|
||||||
#include "Plugin.h"
|
#include "Plugin.h"
|
||||||
#include <future>
|
#include <future>
|
||||||
|
#include <memory>
|
||||||
#include <queue>
|
#include <queue>
|
||||||
#include <string>
|
#include <string>
|
||||||
|
|
||||||
@@ -34,8 +35,27 @@ namespace OpenRCT2::Scripting
|
|||||||
std::shared_ptr<Plugin> _plugin;
|
std::shared_ptr<Plugin> _plugin;
|
||||||
|
|
||||||
public:
|
public:
|
||||||
|
class PluginScope
|
||||||
|
{
|
||||||
|
private:
|
||||||
|
ScriptExecutionInfo& _execInfo;
|
||||||
|
std::shared_ptr<Plugin> _plugin;
|
||||||
|
|
||||||
|
public:
|
||||||
|
PluginScope(ScriptExecutionInfo& execInfo, std::shared_ptr<Plugin> plugin)
|
||||||
|
: _execInfo(execInfo),
|
||||||
|
_plugin(plugin)
|
||||||
|
{
|
||||||
|
_execInfo._plugin = plugin;
|
||||||
|
}
|
||||||
|
PluginScope(const PluginScope&) = delete;
|
||||||
|
~PluginScope()
|
||||||
|
{
|
||||||
|
_execInfo._plugin = nullptr;
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
std::shared_ptr<Plugin> GetCurrentPlugin() { return _plugin; }
|
std::shared_ptr<Plugin> GetCurrentPlugin() { return _plugin; }
|
||||||
void SetCurrentPlugin(std::shared_ptr<Plugin> value) { _plugin = value; }
|
|
||||||
};
|
};
|
||||||
|
|
||||||
class ScriptEngine
|
class ScriptEngine
|
||||||
|
|||||||
Reference in New Issue
Block a user