1
0
mirror of https://github.com/OpenRCT2/OpenRCT2 synced 2025-12-23 15:52:55 +01:00

Refactor plugin scope

This commit is contained in:
Ted John
2018-03-23 23:24:36 +00:00
parent f54b3efe9e
commit d445cfc125
3 changed files with 26 additions and 9 deletions

View File

@@ -82,14 +82,13 @@ void HookEngine::Call(HOOK_TYPE type)
auto& hookList = GetHookList(type);
for (auto& hook : hookList.Hooks)
{
_execInfo.SetCurrentPlugin(hook.Owner);
ScriptExecutionInfo::PluginScope scope(_execInfo, hook.Owner);
const auto& function = hook.Function;
function.push();
duk_pcall(function.context(), 0);
duk_pop(function.context());
}
_execInfo.SetCurrentPlugin(nullptr);
}
HookList& HookEngine::GetHookList(HOOK_TYPE type)

View File

@@ -81,7 +81,7 @@ void ScriptEngine::LoadPlugins()
try
{
auto plugin = std::make_shared<Plugin>(_context, path);
_execInfo.SetCurrentPlugin(plugin);
ScriptExecutionInfo::PluginScope scope(_execInfo, plugin);
plugin->Load();
plugin->EnableHotReload();
_plugins.push_back(std::move(plugin));
@@ -91,7 +91,6 @@ void ScriptEngine::LoadPlugins()
_console.WriteLineError(e.what());
}
}
_execInfo.SetCurrentPlugin(nullptr);
}
void ScriptEngine::AutoReloadPlugins()
@@ -103,7 +102,8 @@ void ScriptEngine::AutoReloadPlugins()
try
{
_hookEngine.UnsubscribeAll(plugin);
_execInfo.SetCurrentPlugin(plugin);
ScriptExecutionInfo::PluginScope scope(_execInfo, plugin);
plugin->Load();
plugin->Start();
}
@@ -113,14 +113,13 @@ void ScriptEngine::AutoReloadPlugins()
}
}
}
_execInfo.SetCurrentPlugin(nullptr);
}
void ScriptEngine::StartPlugins()
{
for (auto& plugin : _plugins)
{
_execInfo.SetCurrentPlugin(plugin);
ScriptExecutionInfo::PluginScope scope(_execInfo, plugin);
try
{
plugin->Start();
@@ -130,7 +129,6 @@ void ScriptEngine::StartPlugins()
_console.WriteLineError(e.what());
}
}
_execInfo.SetCurrentPlugin(nullptr);
}
void ScriptEngine::Update()

View File

@@ -13,6 +13,7 @@
#include "HookEngine.h"
#include "Plugin.h"
#include <future>
#include <memory>
#include <queue>
#include <string>
@@ -34,8 +35,27 @@ namespace OpenRCT2::Scripting
std::shared_ptr<Plugin> _plugin;
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; }
void SetCurrentPlugin(std::shared_ptr<Plugin> value) { _plugin = value; }
};
class ScriptEngine