mirror of
https://github.com/OpenRCT2/OpenRCT2
synced 2026-01-04 13:42:55 +01:00
Use shared_ptr for Plugin
This commit is contained in:
@@ -35,7 +35,7 @@ HookEngine::HookEngine(ScriptExecutionInfo& execInfo) :
|
||||
}
|
||||
}
|
||||
|
||||
uint32 HookEngine::Subscribe(HOOK_TYPE type, Plugin& owner, const DukValue &function)
|
||||
uint32 HookEngine::Subscribe(HOOK_TYPE type, std::shared_ptr<Plugin> owner, const DukValue &function)
|
||||
{
|
||||
auto& hookList = GetHookList(type);
|
||||
auto cookie = _nextCookie++;
|
||||
@@ -58,14 +58,14 @@ void HookEngine::Unsubscribe(HOOK_TYPE type, uint32 cookie)
|
||||
}
|
||||
}
|
||||
|
||||
void HookEngine::UnsubscribeAll(const Plugin& owner)
|
||||
void HookEngine::UnsubscribeAll(std::shared_ptr<const Plugin> owner)
|
||||
{
|
||||
for (auto& hookList : _hookMap)
|
||||
{
|
||||
auto& hooks = hookList.Hooks;
|
||||
for (auto it = hooks.begin(); it != hooks.end();)
|
||||
{
|
||||
if (it->Owner == &owner)
|
||||
if (it->Owner == owner)
|
||||
{
|
||||
it = hooks.erase(it);
|
||||
}
|
||||
|
||||
@@ -33,13 +33,13 @@ namespace OpenRCT2::Scripting
|
||||
struct Hook
|
||||
{
|
||||
uint32 Cookie;
|
||||
Plugin * Owner;
|
||||
std::shared_ptr<Plugin> Owner;
|
||||
DukValue Function;
|
||||
|
||||
Hook();
|
||||
Hook(uint32 cookie, Plugin& owner, const DukValue &function)
|
||||
Hook(uint32 cookie, std::shared_ptr<Plugin> owner, const DukValue &function)
|
||||
: Cookie(cookie),
|
||||
Owner(&owner),
|
||||
Owner(owner),
|
||||
Function(function)
|
||||
{
|
||||
}
|
||||
@@ -70,9 +70,9 @@ namespace OpenRCT2::Scripting
|
||||
public:
|
||||
HookEngine(ScriptExecutionInfo& execInfo);
|
||||
HookEngine(const HookEngine&) = delete;
|
||||
uint32 Subscribe(HOOK_TYPE type, Plugin& owner, const DukValue &function);
|
||||
uint32 Subscribe(HOOK_TYPE type, std::shared_ptr<Plugin> owner, const DukValue &function);
|
||||
void Unsubscribe(HOOK_TYPE type, uint32 cookie);
|
||||
void UnsubscribeAll(const Plugin& owner);
|
||||
void UnsubscribeAll(std::shared_ptr<const Plugin> owner);
|
||||
void Call(HOOK_TYPE type);
|
||||
|
||||
private:
|
||||
|
||||
@@ -34,20 +34,6 @@ Plugin::Plugin(duk_context * context, const std::string &path)
|
||||
{
|
||||
}
|
||||
|
||||
Plugin::Plugin(Plugin&& src)
|
||||
: _context(src._context),
|
||||
_path(src._path),
|
||||
_metadata(src._metadata),
|
||||
_hotReloadData(src._hotReloadData),
|
||||
_hotReloadEnabled(src._hotReloadEnabled)
|
||||
{
|
||||
src._context = nullptr;
|
||||
src._path = std::string();
|
||||
src._metadata = PluginMetadata();
|
||||
src._hotReloadData = HotReloadData();
|
||||
src._hotReloadEnabled = false;
|
||||
}
|
||||
|
||||
Plugin::~Plugin()
|
||||
{
|
||||
DisableHotReload();
|
||||
|
||||
@@ -50,7 +50,7 @@ namespace OpenRCT2::Scripting
|
||||
Plugin() { }
|
||||
Plugin(duk_context * context, const std::string &path);
|
||||
Plugin(const Plugin&) = delete;
|
||||
Plugin(Plugin&&);
|
||||
Plugin(Plugin&&) = delete;
|
||||
~Plugin();
|
||||
|
||||
void Load();
|
||||
|
||||
@@ -45,7 +45,7 @@ namespace OpenRCT2::Scripting
|
||||
throw DukException() << "Not in a plugin context";
|
||||
}
|
||||
|
||||
auto cookie = _hookEngine.Subscribe(hookType, *owner, callback);
|
||||
auto cookie = _hookEngine.Subscribe(hookType, owner, callback);
|
||||
return std::make_shared<ScDisposable>(
|
||||
[this, hookType, cookie]()
|
||||
{
|
||||
|
||||
@@ -80,11 +80,11 @@ void ScriptEngine::LoadPlugins()
|
||||
auto path = std::string(scanner->GetPath());
|
||||
try
|
||||
{
|
||||
Plugin p(_context, path);
|
||||
_execInfo.SetCurrentPlugin(&p);
|
||||
p.Load();
|
||||
p.EnableHotReload();
|
||||
_plugins.push_back(std::move(p));
|
||||
auto plugin = std::make_shared<Plugin>(_context, path);
|
||||
_execInfo.SetCurrentPlugin(plugin);
|
||||
plugin->Load();
|
||||
plugin->EnableHotReload();
|
||||
_plugins.push_back(std::move(plugin));
|
||||
}
|
||||
catch (const std::exception &e)
|
||||
{
|
||||
@@ -98,14 +98,14 @@ void ScriptEngine::AutoReloadPlugins()
|
||||
{
|
||||
for (auto& plugin : _plugins)
|
||||
{
|
||||
if (plugin.ShouldHotReload())
|
||||
if (plugin->ShouldHotReload())
|
||||
{
|
||||
try
|
||||
{
|
||||
_hookEngine.UnsubscribeAll(plugin);
|
||||
_execInfo.SetCurrentPlugin(&plugin);
|
||||
plugin.Load();
|
||||
plugin.Start();
|
||||
_execInfo.SetCurrentPlugin(plugin);
|
||||
plugin->Load();
|
||||
plugin->Start();
|
||||
}
|
||||
catch (const std::exception &e)
|
||||
{
|
||||
@@ -120,10 +120,10 @@ void ScriptEngine::StartPlugins()
|
||||
{
|
||||
for (auto& plugin : _plugins)
|
||||
{
|
||||
_execInfo.SetCurrentPlugin(&plugin);
|
||||
_execInfo.SetCurrentPlugin(plugin);
|
||||
try
|
||||
{
|
||||
plugin.Start();
|
||||
plugin->Start();
|
||||
}
|
||||
catch (const std::exception &e)
|
||||
{
|
||||
|
||||
@@ -31,11 +31,11 @@ namespace OpenRCT2::Scripting
|
||||
class ScriptExecutionInfo
|
||||
{
|
||||
private:
|
||||
Plugin * _plugin;
|
||||
std::shared_ptr<Plugin> _plugin;
|
||||
|
||||
public:
|
||||
Plugin * GetCurrentPlugin() { return _plugin; }
|
||||
void SetCurrentPlugin(Plugin * value) { _plugin = value; }
|
||||
std::shared_ptr<Plugin> GetCurrentPlugin() { return _plugin; }
|
||||
void SetCurrentPlugin(std::shared_ptr<Plugin> value) { _plugin = value; }
|
||||
};
|
||||
|
||||
class ScriptEngine
|
||||
@@ -46,7 +46,7 @@ namespace OpenRCT2::Scripting
|
||||
bool _initialised{};
|
||||
duk_context * _context{};
|
||||
std::queue<std::tuple<std::promise<void>, std::string>> _evalQueue;
|
||||
std::vector<Plugin> _plugins;
|
||||
std::vector<std::shared_ptr<Plugin>> _plugins;
|
||||
uint32 _lastHotReloadCheckTick{};
|
||||
HookEngine _hookEngine;
|
||||
ScriptExecutionInfo _execInfo;
|
||||
|
||||
Reference in New Issue
Block a user