From af8c96026d458481543ead176bd15651857826bd Mon Sep 17 00:00:00 2001 From: Mattes D Date: Wed, 2 Mar 2016 10:05:10 +0100 Subject: Removed cWebPlugin, WebAdmin uses cLuaState::cCallback. --- src/Bindings/PluginLua.h | 15 ++++----------- 1 file changed, 4 insertions(+), 11 deletions(-) (limited to 'src/Bindings/PluginLua.h') diff --git a/src/Bindings/PluginLua.h b/src/Bindings/PluginLua.h index db6612671..dac782a43 100644 --- a/src/Bindings/PluginLua.h +++ b/src/Bindings/PluginLua.h @@ -10,7 +10,6 @@ #pragma once #include "Plugin.h" -#include "WebPlugin.h" #include "LuaState.h" // Names for the global variables through which the plugin is identified in its LuaState @@ -29,8 +28,7 @@ class cWindow; // tolua_begin class cPluginLua : - public cPlugin, - public cWebPlugin + public cPlugin { typedef cPlugin super; @@ -181,14 +179,6 @@ public: /** Returns true if the plugin contains the function for the specified hook type, using the old-style registration (#121) */ bool CanAddOldStyleHook(int a_HookType); - // cWebPlugin overrides - virtual const AString GetWebTitle(void) const override {return GetName(); } - virtual AString HandleWebRequest(const HTTPRequest & a_Request) override; - - /** Adds a new web tab to webadmin. - Displaying the tab calls the referenced function. */ - bool AddWebTab(const AString & a_Title, lua_State * a_LuaState, int a_FunctionReference); // Exported in ManualBindings.cpp - /** Binds the command to call the function specified by a Lua function reference. Simply adds to CommandMap. */ void BindCommand(const AString & a_Command, int a_FnRef); @@ -270,6 +260,9 @@ protected: /** Releases all Lua references, notifies and removes all m_Resettables[] and closes the m_LuaState. */ void Close(void); + + /** Removes all WebTabs currently registered for this plugin from the WebAdmin. */ + void ClearWebTabs(void); } ; // tolua_export -- cgit v1.2.3 From 4489a89fdec9f4a507400150af34623899b64f46 Mon Sep 17 00:00:00 2001 From: Mattes D Date: Wed, 2 Mar 2016 10:12:43 +0100 Subject: Changed plugin hook registrations to use cLuaState::cCallback. --- src/Bindings/PluginLua.h | 33 ++++++++++++++++++++++++++------- 1 file changed, 26 insertions(+), 7 deletions(-) (limited to 'src/Bindings/PluginLua.h') diff --git a/src/Bindings/PluginLua.h b/src/Bindings/PluginLua.h index dac782a43..3205ac397 100644 --- a/src/Bindings/PluginLua.h +++ b/src/Bindings/PluginLua.h @@ -201,11 +201,9 @@ public: /** Returns the name of Lua function that should handle the specified hook type in the older (#121) API */ static const char * GetHookFnName(int a_HookType); - /** Adds a Lua function to be called for the specified hook. - The function has to be on the Lua stack at the specified index a_FnRefIdx - Returns true if the hook was added successfully. - */ - bool AddHookRef(int a_HookType, int a_FnRefIdx); + /** Adds a Lua callback to be called for the specified hook. + Returns true if the hook was added successfully. */ + bool AddHookCallback(int a_HookType, cLuaState::cCallbackPtr a_Callback); /** Calls a function in this plugin's LuaState with parameters copied over from a_ForeignState. The values that the function returns are placed onto a_ForeignState. @@ -233,10 +231,10 @@ protected: typedef std::map CommandMap; /** Provides an array of Lua function references */ - typedef std::vector cLuaRefs; + typedef std::vector cLuaCallbacks; /** Maps hook types into arrays of Lua function references to call for each hook type */ - typedef std::map cHookMap; + typedef std::map cHookMap; /** The mutex protecting m_LuaState and each of the m_Resettables[] against multithreaded use. */ @@ -263,6 +261,27 @@ protected: /** Removes all WebTabs currently registered for this plugin from the WebAdmin. */ void ClearWebTabs(void); + + /** Calls a hook that has the simple format - single bool return value specifying whether the chain should continue. + The advanced hook types that need more processing implement a similar loop manually instead. + Returns true if any of hook calls wants to abort the hook (returned true), false if all hook calls returned false. */ + template + bool CallSimpleHooks(int a_HookType, Args && ... a_Args) + { + cCSLock lock(m_CriticalSection); + auto & hooks = m_HookMap[a_HookType]; + bool res = false; + for (auto & hook: hooks) + { + hook->Call(std::forward(a_Args)..., cLuaState::Return, res); + if (res) + { + // Hook wants to terminate the chain processing + return true; + } + } + return false; + } } ; // tolua_export -- cgit v1.2.3 From a4737251215461c3d624bcd366785a3e8d36b1f1 Mon Sep 17 00:00:00 2001 From: Mattes D Date: Tue, 1 Mar 2016 17:40:15 +0100 Subject: Removed the cPluginLua::cResettable class. The functionality provided by that class has been superseded by cLuaState::cCallback, with better multithreading support. --- src/Bindings/PluginLua.h | 38 +------------------------------------- 1 file changed, 1 insertion(+), 37 deletions(-) (limited to 'src/Bindings/PluginLua.h') diff --git a/src/Bindings/PluginLua.h b/src/Bindings/PluginLua.h index 3205ac397..e53fbaaa8 100644 --- a/src/Bindings/PluginLua.h +++ b/src/Bindings/PluginLua.h @@ -62,36 +62,6 @@ public: - /** A base class that represents something related to a plugin - The plugin can reset this class so that the instance can continue to exist but will not engage the (possibly non-existent) plugin anymore. - This is used for scheduled tasks etc., so that they can be queued and reset when the plugin is terminated, without removing them from the queue. */ - class cResettable - { - public: - /** Creates a new instance bound to the specified plugin. */ - cResettable(cPluginLua & a_Plugin); - - // Force a virtual destructor in descendants: - virtual ~cResettable() {} - - /** Resets the plugin instance stored within. - The instance will continue to exist, but should not call into the plugin anymore. */ - virtual void Reset(void); - - protected: - /** The plugin that this instance references. - If nullptr, the plugin has already unloaded and the instance should bail out any processing. - Protected against multithreaded access by m_CSPlugin. */ - cPluginLua * m_Plugin; - - /** The mutex protecting m_Plugin against multithreaded access. */ - cCriticalSection m_CSPlugin; - }; - - typedef SharedPtr cResettablePtr; - typedef std::vector cResettablePtrs; - - cPluginLua(const AString & a_PluginDirectory); ~cPluginLua(); @@ -223,9 +193,6 @@ public: return m_LuaState.Call(a_Fn, a_Args...); } - /** Adds the specified cResettable instance to m_Resettables, so that it is notified when the plugin is being closed. */ - void AddResettable(cResettablePtr a_Resettable); - protected: /** Maps command name into Lua function reference */ typedef std::map CommandMap; @@ -237,15 +204,12 @@ protected: typedef std::map cHookMap; - /** The mutex protecting m_LuaState and each of the m_Resettables[] against multithreaded use. */ + /** The mutex protecting m_LuaState against multithreaded use. */ cCriticalSection m_CriticalSection; /** The plugin's Lua state. */ cLuaState m_LuaState; - /** Objects that need notification when the plugin is about to be unloaded. */ - cResettablePtrs m_Resettables; - /** In-game commands that the plugin has registered. */ CommandMap m_Commands; -- cgit v1.2.3 From 24853397ef4648155d886b112e00c3e2c3d1e900 Mon Sep 17 00:00:00 2001 From: Mattes D Date: Sun, 12 Jun 2016 16:53:24 +0200 Subject: LuaState: Implemented proper locking for cCallback. --- src/Bindings/PluginLua.h | 21 +++++---------------- 1 file changed, 5 insertions(+), 16 deletions(-) (limited to 'src/Bindings/PluginLua.h') diff --git a/src/Bindings/PluginLua.h b/src/Bindings/PluginLua.h index e53fbaaa8..8e40ff584 100644 --- a/src/Bindings/PluginLua.h +++ b/src/Bindings/PluginLua.h @@ -47,7 +47,7 @@ public: public: cOperation(cPluginLua & a_Plugin) : m_Plugin(a_Plugin), - m_Lock(a_Plugin.m_CriticalSection) + m_Lock(a_Plugin.m_LuaState) { } @@ -56,8 +56,8 @@ public: protected: cPluginLua & m_Plugin; - /** RAII lock for m_Plugin.m_CriticalSection */ - cCSLock m_Lock; + /** RAII lock for the Lua state. */ + cLuaState::cLock m_Lock; } ; @@ -155,13 +155,6 @@ public: /** Binds the console command to call the function specified by a Lua function reference. Simply adds to CommandMap. */ void BindConsoleCommand(const AString & a_Command, int a_FnRef); - cLuaState & GetLuaState(void) { return m_LuaState; } - - cCriticalSection & GetCriticalSection(void) { return m_CriticalSection; } - - /** Removes a previously referenced object (luaL_unref()) */ - void Unreference(int a_LuaRef); - /** Calls the plugin-specified "cLuaWindow closing" callback. Returns true only if the callback returned true */ bool CallbackWindowClosing(int a_FnRef, cWindow & a_Window, cPlayer & a_Player, bool a_CanRefuse); @@ -189,8 +182,7 @@ public: template bool Call(FnT a_Fn, Args && ... a_Args) { - cCSLock Lock(m_CriticalSection); - return m_LuaState.Call(a_Fn, a_Args...); + return cOperation(*this)().Call(a_Fn, a_Args...); } protected: @@ -204,9 +196,6 @@ protected: typedef std::map cHookMap; - /** The mutex protecting m_LuaState against multithreaded use. */ - cCriticalSection m_CriticalSection; - /** The plugin's Lua state. */ cLuaState m_LuaState; @@ -232,7 +221,7 @@ protected: template bool CallSimpleHooks(int a_HookType, Args && ... a_Args) { - cCSLock lock(m_CriticalSection); + cOperation op(*this); auto & hooks = m_HookMap[a_HookType]; bool res = false; for (auto & hook: hooks) -- cgit v1.2.3 From 257c5a1a54396a3610f63bf469d6cf50ec76aef5 Mon Sep 17 00:00:00 2001 From: Mattes D Date: Sun, 12 Jun 2016 18:11:40 +0200 Subject: cPluginManager: Use a callback for command handler registration. --- src/Bindings/PluginLua.h | 23 ----------------------- 1 file changed, 23 deletions(-) (limited to 'src/Bindings/PluginLua.h') diff --git a/src/Bindings/PluginLua.h b/src/Bindings/PluginLua.h index 8e40ff584..9e6b53e75 100644 --- a/src/Bindings/PluginLua.h +++ b/src/Bindings/PluginLua.h @@ -138,23 +138,9 @@ public: virtual bool OnWorldStarted (cWorld & a_World) override; virtual bool OnWorldTick (cWorld & a_World, std::chrono::milliseconds a_Dt, std::chrono::milliseconds a_LastTickDurationMSec) override; - virtual bool HandleCommand(const AStringVector & a_Split, cPlayer & a_Player, const AString & a_FullCommand) override; - - virtual bool HandleConsoleCommand(const AStringVector & a_Split, cCommandOutputCallback & a_Output, const AString & a_FullCommand) override; - - virtual void ClearCommands(void) override; - - virtual void ClearConsoleCommands(void) override; - /** Returns true if the plugin contains the function for the specified hook type, using the old-style registration (#121) */ bool CanAddOldStyleHook(int a_HookType); - /** Binds the command to call the function specified by a Lua function reference. Simply adds to CommandMap. */ - void BindCommand(const AString & a_Command, int a_FnRef); - - /** Binds the console command to call the function specified by a Lua function reference. Simply adds to CommandMap. */ - void BindConsoleCommand(const AString & a_Command, int a_FnRef); - /** Calls the plugin-specified "cLuaWindow closing" callback. Returns true only if the callback returned true */ bool CallbackWindowClosing(int a_FnRef, cWindow & a_Window, cPlayer & a_Player, bool a_CanRefuse); @@ -186,9 +172,6 @@ public: } protected: - /** Maps command name into Lua function reference */ - typedef std::map CommandMap; - /** Provides an array of Lua function references */ typedef std::vector cLuaCallbacks; @@ -199,12 +182,6 @@ protected: /** The plugin's Lua state. */ cLuaState m_LuaState; - /** In-game commands that the plugin has registered. */ - CommandMap m_Commands; - - /** Console commands that the plugin has registered. */ - CommandMap m_ConsoleCommands; - /** Hooks that the plugin has registered. */ cHookMap m_HookMap; -- cgit v1.2.3 From 7a6670d1d110be96ed73ccab4f33c69e4a01f28d Mon Sep 17 00:00:00 2001 From: Mattes D Date: Sun, 12 Jun 2016 18:24:01 +0200 Subject: Removed dead code related to callbacks. --- src/Bindings/PluginLua.h | 6 ------ 1 file changed, 6 deletions(-) (limited to 'src/Bindings/PluginLua.h') diff --git a/src/Bindings/PluginLua.h b/src/Bindings/PluginLua.h index 9e6b53e75..e28e30698 100644 --- a/src/Bindings/PluginLua.h +++ b/src/Bindings/PluginLua.h @@ -141,12 +141,6 @@ public: /** Returns true if the plugin contains the function for the specified hook type, using the old-style registration (#121) */ bool CanAddOldStyleHook(int a_HookType); - /** Calls the plugin-specified "cLuaWindow closing" callback. Returns true only if the callback returned true */ - bool CallbackWindowClosing(int a_FnRef, cWindow & a_Window, cPlayer & a_Player, bool a_CanRefuse); - - /** Calls the plugin-specified "cLuaWindow slot changed" callback. */ - void CallbackWindowSlotChanged(int a_FnRef, cWindow & a_Window, int a_SlotNum); - /** Returns the name of Lua function that should handle the specified hook type in the older (#121) API */ static const char * GetHookFnName(int a_HookType); -- cgit v1.2.3 From bf88312a1664311968736b4ba7ce1458c8b0954e Mon Sep 17 00:00:00 2001 From: Mattes D Date: Mon, 27 Jun 2016 20:49:59 +0200 Subject: Converted cLuaState::cCallbackPtr into a UniquePtr. --- src/Bindings/PluginLua.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'src/Bindings/PluginLua.h') diff --git a/src/Bindings/PluginLua.h b/src/Bindings/PluginLua.h index e28e30698..dc3c91880 100644 --- a/src/Bindings/PluginLua.h +++ b/src/Bindings/PluginLua.h @@ -146,7 +146,7 @@ public: /** Adds a Lua callback to be called for the specified hook. Returns true if the hook was added successfully. */ - bool AddHookCallback(int a_HookType, cLuaState::cCallbackPtr a_Callback); + bool AddHookCallback(int a_HookType, cLuaState::cCallbackPtr && a_Callback); /** Calls a function in this plugin's LuaState with parameters copied over from a_ForeignState. The values that the function returns are placed onto a_ForeignState. -- cgit v1.2.3