summaryrefslogtreecommitdiffstats
path: root/src/Bindings
diff options
context:
space:
mode:
Diffstat (limited to 'src/Bindings')
-rw-r--r--src/Bindings/AllToLua.bat4
-rw-r--r--src/Bindings/CMakeLists.txt24
-rw-r--r--src/Bindings/LuaState.cpp205
-rw-r--r--src/Bindings/LuaState.h143
-rw-r--r--src/Bindings/ManualBindings.cpp305
-rw-r--r--src/Bindings/PluginLua.cpp86
-rw-r--r--src/Bindings/PluginLua.h42
-rw-r--r--src/Bindings/PluginManager.cpp15
-rw-r--r--src/Bindings/PluginManager.h56
9 files changed, 610 insertions, 270 deletions
diff --git a/src/Bindings/AllToLua.bat b/src/Bindings/AllToLua.bat
index b2a192880..f085af9e9 100644
--- a/src/Bindings/AllToLua.bat
+++ b/src/Bindings/AllToLua.bat
@@ -4,17 +4,21 @@
:: When called without any parameters, it will pause for a keypress at the end
:: Call with any parameter to disable the wait (for buildserver use)
+@echo off
+
:: Regenerate the files:
+echo Regenerating LUA bindings . . .
"tolua++.exe" -L virtual_method_hooks.lua -o Bindings.cpp -H Bindings.h AllToLua.pkg
: Wait for keypress, if no param given:
+echo.
if %ALLTOLUA_WAIT%N == N pause
diff --git a/src/Bindings/CMakeLists.txt b/src/Bindings/CMakeLists.txt
deleted file mode 100644
index 50b81e42a..000000000
--- a/src/Bindings/CMakeLists.txt
+++ /dev/null
@@ -1,24 +0,0 @@
-
-cmake_minimum_required (VERSION 2.6)
-project (MCServer)
-
-# NOTE: This CMake file is processed only for Unix builds; Windows(MSVC) builds handle all the subfolders in /src in a single file, /src/CMakeLists.txt
-
-include_directories ("${PROJECT_SOURCE_DIR}/../")
-
-ADD_CUSTOM_COMMAND(
- # add any new generated bindings here
- OUTPUT ${CMAKE_CURRENT_SOURCE_DIR}/Bindings.cpp ${CMAKE_CURRENT_SOURCE_DIR}/Bindings.h
-
- # command execuded to regerate bindings
- COMMAND tolua -L virtual_method_hooks.lua -o Bindings.cpp -H Bindings.h AllToLua.pkg
- WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}
-
- # add any new generation dependencies here
- DEPENDS ${CMAKE_CURRENT_SOURCE_DIR}/virtual_method_hooks.lua ${CMAKE_CURRENT_SOURCE_DIR}/AllToLua.pkg tolua
-)
-
-#add cpp files here
-add_library(Bindings PluginManager LuaState WebPlugin Bindings ManualBindings LuaWindow Plugin PluginLua WebPlugin)
-
-target_link_libraries(Bindings lua sqlite tolualib)
diff --git a/src/Bindings/LuaState.cpp b/src/Bindings/LuaState.cpp
index bfee1d037..d49cd8ef3 100644
--- a/src/Bindings/LuaState.cpp
+++ b/src/Bindings/LuaState.cpp
@@ -235,7 +235,7 @@ bool cLuaState::PushFunction(const char * a_FunctionName)
if (!lua_isfunction(m_LuaState, -1))
{
LOGWARNING("Error in %s: Could not find function %s()", m_SubsystemName.c_str(), a_FunctionName);
- lua_pop(m_LuaState, 1);
+ lua_pop(m_LuaState, 2);
return false;
}
m_CurrentFunctionName.assign(a_FunctionName);
@@ -258,7 +258,7 @@ bool cLuaState::PushFunction(int a_FnRef)
lua_rawgeti(m_LuaState, LUA_REGISTRYINDEX, a_FnRef); // same as lua_getref()
if (!lua_isfunction(m_LuaState, -1))
{
- lua_pop(m_LuaState, 1);
+ lua_pop(m_LuaState, 2);
return false;
}
m_CurrentFunctionName = "<callback>";
@@ -282,16 +282,20 @@ bool cLuaState::PushFunction(const cTableRef & a_TableRef)
if (!lua_istable(m_LuaState, -1))
{
// Not a table, bail out
- lua_pop(m_LuaState, 1);
+ lua_pop(m_LuaState, 2);
return false;
}
lua_getfield(m_LuaState, -1, a_TableRef.GetFnName());
if (lua_isnil(m_LuaState, -1) || !lua_isfunction(m_LuaState, -1))
{
// Not a valid function, bail out
- lua_pop(m_LuaState, 2);
+ lua_pop(m_LuaState, 3);
return false;
}
+
+ // Pop the table off the stack:
+ lua_remove(m_LuaState, -2);
+
Printf(m_CurrentFunctionName, "<table-callback %s>", a_TableRef.GetFnName());
m_NumCurrentFunctionArgs = 0;
return true;
@@ -742,6 +746,10 @@ bool cLuaState::CallFunction(int a_NumResults)
}
m_NumCurrentFunctionArgs = -1;
m_CurrentFunctionName.clear();
+
+ // Remove the error handler from the stack:
+ lua_remove(m_LuaState, -a_NumResults - 1);
+
return true;
}
@@ -1025,21 +1033,184 @@ void cLuaState::LogStackTrace(lua_State * a_LuaState)
AString cLuaState::GetTypeText(int a_StackPos)
{
- int Type = lua_type(m_LuaState, a_StackPos);
- switch (Type)
+ return lua_typename(m_LuaState, lua_type(m_LuaState, a_StackPos));
+}
+
+
+
+
+
+int cLuaState::CallFunctionWithForeignParams(
+ const AString & a_FunctionName,
+ cLuaState & a_SrcLuaState,
+ int a_SrcParamStart,
+ int a_SrcParamEnd
+)
+{
+ ASSERT(IsValid());
+ ASSERT(a_SrcLuaState.IsValid());
+
+ // Store the stack position before any changes
+ int OldTop = lua_gettop(m_LuaState);
+
+ // Push the function to call, including the error handler:
+ if (!PushFunction(a_FunctionName.c_str()))
+ {
+ LOGWARNING("Function '%s' not found", a_FunctionName.c_str());
+ lua_pop(m_LuaState, 2);
+ return -1;
+ }
+
+ // Copy the function parameters to the target state
+ if (CopyStackFrom(a_SrcLuaState, a_SrcParamStart, a_SrcParamEnd) < 0)
+ {
+ // Something went wrong, fix the stack and exit
+ lua_pop(m_LuaState, 2);
+ return -1;
+ }
+
+ // Call the function, with an error handler:
+ int s = lua_pcall(m_LuaState, a_SrcParamEnd - a_SrcParamStart + 1, LUA_MULTRET, OldTop);
+ if (ReportErrors(s))
+ {
+ LOGWARN("Error while calling function '%s' in '%s'", a_FunctionName.c_str(), m_SubsystemName.c_str());
+ // Fix the stack.
+ // We don't know how many values have been pushed, so just get rid of any that weren't there initially
+ int CurTop = lua_gettop(m_LuaState);
+ if (CurTop > OldTop)
+ {
+ lua_pop(m_LuaState, CurTop - OldTop);
+ }
+ return -1;
+ }
+
+ // Reset the internal checking mechanisms:
+ m_NumCurrentFunctionArgs = -1;
+
+ // Remove the error handler from the stack:
+ lua_remove(m_LuaState, OldTop + 1);
+
+ // Return the number of return values:
+ return lua_gettop(m_LuaState) - OldTop;
+}
+
+
+
+
+
+int cLuaState::CopyStackFrom(cLuaState & a_SrcLuaState, int a_SrcStart, int a_SrcEnd)
+{
+ /*
+ // DEBUG:
+ LOGD("Copying stack values from %d to %d", a_SrcStart, a_SrcEnd);
+ a_SrcLuaState.LogStack("Src stack before copying:");
+ LogStack("Dst stack before copying:");
+ */
+ for (int i = a_SrcStart; i <= a_SrcEnd; ++i)
+ {
+ int t = lua_type(a_SrcLuaState, i);
+ switch (t)
+ {
+ case LUA_TNIL:
+ {
+ lua_pushnil(m_LuaState);
+ break;
+ }
+ case LUA_TSTRING:
+ {
+ AString s;
+ a_SrcLuaState.ToString(i, s);
+ Push(s);
+ break;
+ }
+ case LUA_TBOOLEAN:
+ {
+ bool b = (tolua_toboolean(a_SrcLuaState, i, false) != 0);
+ Push(b);
+ break;
+ }
+ case LUA_TNUMBER:
+ {
+ lua_Number d = tolua_tonumber(a_SrcLuaState, i, 0);
+ Push(d);
+ break;
+ }
+ case LUA_TUSERDATA:
+ {
+ // Get the class name:
+ const char * type = NULL;
+ if (lua_getmetatable(a_SrcLuaState, i) == 0)
+ {
+ LOGWARNING("%s: Unknown class in pos %d, cannot copy.", __FUNCTION__, i);
+ lua_pop(m_LuaState, i - a_SrcStart);
+ return -1;
+ }
+ lua_rawget(a_SrcLuaState, LUA_REGISTRYINDEX); // Stack +1
+ type = lua_tostring(a_SrcLuaState, -1);
+ lua_pop(a_SrcLuaState, 1); // Stack -1
+
+ // Copy the value:
+ void * ud = tolua_touserdata(a_SrcLuaState, i, NULL);
+ tolua_pushusertype(m_LuaState, ud, type);
+ }
+ default:
+ {
+ LOGWARNING("%s: Unsupported value: '%s' at stack position %d. Can only copy numbers, strings, bools and classes!",
+ __FUNCTION__, lua_typename(a_SrcLuaState, t), i
+ );
+ a_SrcLuaState.LogStack("Stack where copying failed:");
+ lua_pop(m_LuaState, i - a_SrcStart);
+ return -1;
+ }
+ }
+ }
+ return a_SrcEnd - a_SrcStart + 1;
+}
+
+
+
+
+
+void cLuaState::ToString(int a_StackPos, AString & a_String)
+{
+ size_t len;
+ const char * s = lua_tolstring(m_LuaState, a_StackPos, &len);
+ if (s != NULL)
{
- case LUA_TNONE: return "TNONE";
- case LUA_TNIL: return "TNIL";
- case LUA_TBOOLEAN: return "TBOOLEAN";
- case LUA_TLIGHTUSERDATA: return "TLIGHTUSERDATA";
- case LUA_TNUMBER: return "TNUMBER";
- case LUA_TSTRING: return "TSTRING";
- case LUA_TTABLE: return "TTABLE";
- case LUA_TFUNCTION: return "TFUNCTION";
- case LUA_TUSERDATA: return "TUSERDATA";
- case LUA_TTHREAD: return "TTHREAD";
+ a_String.assign(s, len);
}
- return Printf("Unknown (%d)", Type);
+}
+
+
+
+
+
+void cLuaState::LogStack(const char * a_Header)
+{
+ LogStack(m_LuaState, a_Header);
+}
+
+
+
+
+
+void cLuaState::LogStack(lua_State * a_LuaState, const char * a_Header)
+{
+ LOGD((a_Header != NULL) ? a_Header : "Lua C API Stack contents:");
+ for (int i = lua_gettop(a_LuaState); i >= 0; i--)
+ {
+ AString Value;
+ int Type = lua_type(a_LuaState, i);
+ switch (Type)
+ {
+ case LUA_TBOOLEAN: Value.assign((lua_toboolean(a_LuaState, i) != 0) ? "true" : "false"); break;
+ case LUA_TLIGHTUSERDATA: Printf(Value, "%p", lua_touserdata(a_LuaState, i)); break;
+ case LUA_TNUMBER: Printf(Value, "%f", (double)lua_tonumber(a_LuaState, i)); break;
+ case LUA_TSTRING: Printf(Value, "%s", lua_tostring(a_LuaState, i)); break;
+ default: break;
+ }
+ LOGD(" Idx %d: type %d (%s) %s", i, Type, lua_typename(a_LuaState, Type), Value.c_str());
+ } // for i - stack idx
}
diff --git a/src/Bindings/LuaState.h b/src/Bindings/LuaState.h
index f8b67f5cd..dda45bb28 100644
--- a/src/Bindings/LuaState.h
+++ b/src/Bindings/LuaState.h
@@ -60,23 +60,23 @@ class cBlockEntity;
-/// Encapsulates a Lua state and provides some syntactic sugar for common operations
+/** Encapsulates a Lua state and provides some syntactic sugar for common operations */
class cLuaState
{
public:
- /// Used for storing references to object in the global registry
+ /** Used for storing references to object in the global registry */
class cRef
{
public:
- /// Creates a reference in the specified LuaState for object at the specified StackPos
+ /** Creates a reference in the specified LuaState for object at the specified StackPos */
cRef(cLuaState & a_LuaState, int a_StackPos);
~cRef();
- /// Returns true if the reference is valid
+ /** Returns true if the reference is valid */
bool IsValid(void) const {return (m_Ref != LUA_REFNIL); }
- /// Allows to use this class wherever an int (i. e. ref) is to be used
+ /** Allows to use this class wherever an int (i. e. ref) is to be used */
operator int(void) const { return m_Ref; }
protected:
@@ -102,7 +102,7 @@ public:
} ;
- /// A dummy class that's used only to delimit function args from return values for cLuaState::Call()
+ /** A dummy class that's used only to delimit function args from return values for cLuaState::Call() */
class cRet
{
} ;
@@ -123,22 +123,22 @@ public:
~cLuaState();
- /// Allows this object to be used in the same way as a lua_State *, for example in the LuaLib functions
+ /** Allows this object to be used in the same way as a lua_State *, for example in the LuaLib functions */
operator lua_State * (void) { return m_LuaState; }
- /// Creates the m_LuaState, if not closed already. This state will be automatically closed in the destructor
+ /** Creates the m_LuaState, if not closed already. This state will be automatically closed in the destructor */
void Create(void);
- /// Closes the m_LuaState, if not closed already
+ /** Closes the m_LuaState, if not closed already */
void Close(void);
- /// Attaches the specified state. Operations will be carried out on this state, but it will not be closed in the destructor
+ /** Attaches the specified state. Operations will be carried out on this state, but it will not be closed in the destructor */
void Attach(lua_State * a_State);
- /// Detaches a previously attached state.
+ /** Detaches a previously attached state. */
void Detach(void);
- /// Returns true if the m_LuaState is valid
+ /** Returns true if the m_LuaState is valid */
bool IsValid(void) const { return (m_LuaState != NULL); }
/** Loads the specified file
@@ -147,7 +147,7 @@ public:
*/
bool LoadFile(const AString & a_FileName);
- /// Returns true if a_FunctionName is a valid Lua function that can be called
+ /** Returns true if a_FunctionName is a valid Lua function that can be called */
bool HasFunction(const char * a_FunctionName);
// Push a value onto the stack
@@ -182,7 +182,7 @@ public:
void Push(cHopperEntity * a_Hopper);
void Push(cBlockEntity * a_BlockEntity);
- /// Call any 0-param 0-return Lua function in a single line:
+ /** Call any 0-param 0-return Lua function in a single line: */
template <typename FnT>
bool Call(FnT a_FnName)
{
@@ -193,7 +193,7 @@ public:
return CallFunction(0);
}
- /// Call any 1-param 0-return Lua function in a single line:
+ /** Call any 1-param 0-return Lua function in a single line: */
template<
typename FnT,
typename ArgT1
@@ -208,7 +208,7 @@ public:
return CallFunction(0);
}
- /// Call any 2-param 0-return Lua function in a single line:
+ /** Call any 2-param 0-return Lua function in a single line: */
template<
typename FnT, typename ArgT1, typename ArgT2
>
@@ -223,7 +223,7 @@ public:
return CallFunction(0);
}
- /// Call any 3-param 0-return Lua function in a single line:
+ /** Call any 3-param 0-return Lua function in a single line: */
template<
typename FnT, typename ArgT1, typename ArgT2, typename ArgT3
>
@@ -239,7 +239,7 @@ public:
return CallFunction(0);
}
- /// Call any 0-param 1-return Lua function in a single line:
+ /** Call any 0-param 1-return Lua function in a single line: */
template<
typename FnT, typename RetT1
>
@@ -259,12 +259,13 @@ public:
return true;
}
- /// Call any 1-param 1-return Lua function in a single line:
+ /** Call any 1-param 1-return Lua function in a single line: */
template<
typename FnT, typename ArgT1, typename RetT1
>
bool Call(FnT a_FnName, ArgT1 a_Arg1, const cRet & a_Mark, RetT1 & a_Ret1)
{
+ int InitialTop = lua_gettop(m_LuaState);
UNUSED(a_Mark);
if (!PushFunction(a_FnName))
{
@@ -277,10 +278,11 @@ public:
}
GetReturn(-1, a_Ret1);
lua_pop(m_LuaState, 1);
+ ASSERT(InitialTop == lua_gettop(m_LuaState));
return true;
}
- /// Call any 2-param 1-return Lua function in a single line:
+ /** Call any 2-param 1-return Lua function in a single line: */
template<
typename FnT, typename ArgT1, typename ArgT2, typename RetT1
>
@@ -302,7 +304,7 @@ public:
return true;
}
- /// Call any 3-param 1-return Lua function in a single line:
+ /** Call any 3-param 1-return Lua function in a single line: */
template<
typename FnT, typename ArgT1, typename ArgT2, typename ArgT3, typename RetT1
>
@@ -325,7 +327,7 @@ public:
return true;
}
- /// Call any 4-param 1-return Lua function in a single line:
+ /** Call any 4-param 1-return Lua function in a single line: */
template<
typename FnT, typename ArgT1, typename ArgT2, typename ArgT3, typename ArgT4, typename RetT1
>
@@ -349,7 +351,7 @@ public:
return true;
}
- /// Call any 5-param 1-return Lua function in a single line:
+ /** Call any 5-param 1-return Lua function in a single line: */
template<
typename FnT, typename ArgT1, typename ArgT2, typename ArgT3, typename ArgT4, typename ArgT5, typename RetT1
>
@@ -374,7 +376,7 @@ public:
return true;
}
- /// Call any 6-param 1-return Lua function in a single line:
+ /** Call any 6-param 1-return Lua function in a single line: */
template<
typename FnT, typename ArgT1, typename ArgT2, typename ArgT3, typename ArgT4, typename ArgT5, typename ArgT6,
typename RetT1
@@ -401,7 +403,7 @@ public:
return true;
}
- /// Call any 7-param 1-return Lua function in a single line:
+ /** Call any 7-param 1-return Lua function in a single line: */
template<
typename FnT, typename ArgT1, typename ArgT2, typename ArgT3, typename ArgT4, typename ArgT5, typename ArgT6,
typename ArgT7, typename RetT1
@@ -429,7 +431,7 @@ public:
return true;
}
- /// Call any 8-param 1-return Lua function in a single line:
+ /** Call any 8-param 1-return Lua function in a single line: */
template<
typename FnT, typename ArgT1, typename ArgT2, typename ArgT3, typename ArgT4, typename ArgT5, typename ArgT6,
typename ArgT7, typename ArgT8, typename RetT1
@@ -458,7 +460,7 @@ public:
return true;
}
- /// Call any 9-param 1-return Lua function in a single line:
+ /** Call any 9-param 1-return Lua function in a single line: */
template<
typename FnT, typename ArgT1, typename ArgT2, typename ArgT3, typename ArgT4, typename ArgT5, typename ArgT6,
typename ArgT7, typename ArgT8, typename ArgT9, typename RetT1
@@ -488,7 +490,7 @@ public:
return true;
}
- /// Call any 10-param 1-return Lua function in a single line:
+ /** Call any 10-param 1-return Lua function in a single line: */
template<
typename FnT, typename ArgT1, typename ArgT2, typename ArgT3, typename ArgT4, typename ArgT5, typename ArgT6,
typename ArgT7, typename ArgT8, typename ArgT9, typename ArgT10, typename RetT1
@@ -519,7 +521,7 @@ public:
return true;
}
- /// Call any 1-param 2-return Lua function in a single line:
+ /** Call any 1-param 2-return Lua function in a single line: */
template<
typename FnT, typename ArgT1, typename RetT1, typename RetT2
>
@@ -541,7 +543,7 @@ public:
return true;
}
- /// Call any 2-param 2-return Lua function in a single line:
+ /** Call any 2-param 2-return Lua function in a single line: */
template<
typename FnT, typename ArgT1, typename ArgT2, typename RetT1, typename RetT2
>
@@ -564,7 +566,7 @@ public:
return true;
}
- /// Call any 3-param 2-return Lua function in a single line:
+ /** Call any 3-param 2-return Lua function in a single line: */
template<
typename FnT, typename ArgT1, typename ArgT2, typename ArgT3,
typename RetT1, typename RetT2
@@ -589,7 +591,7 @@ public:
return true;
}
- /// Call any 4-param 2-return Lua function in a single line:
+ /** Call any 4-param 2-return Lua function in a single line: */
template<
typename FnT, typename ArgT1, typename ArgT2, typename ArgT3, typename ArgT4,
typename RetT1, typename RetT2
@@ -615,7 +617,7 @@ public:
return true;
}
- /// Call any 5-param 2-return Lua function in a single line:
+ /** Call any 5-param 2-return Lua function in a single line: */
template<
typename FnT, typename ArgT1, typename ArgT2, typename ArgT3, typename ArgT4, typename ArgT5,
typename RetT1, typename RetT2
@@ -642,7 +644,7 @@ public:
return true;
}
- /// Call any 6-param 2-return Lua function in a single line:
+ /** Call any 6-param 2-return Lua function in a single line: */
template<
typename FnT, typename ArgT1, typename ArgT2, typename ArgT3, typename ArgT4, typename ArgT5,
typename ArgT6,
@@ -671,7 +673,7 @@ public:
return true;
}
- /// Call any 7-param 2-return Lua function in a single line:
+ /** Call any 7-param 2-return Lua function in a single line: */
template<
typename FnT, typename ArgT1, typename ArgT2, typename ArgT3, typename ArgT4, typename ArgT5,
typename ArgT6, typename ArgT7,
@@ -701,7 +703,7 @@ public:
return true;
}
- /// Call any 7-param 3-return Lua function in a single line:
+ /** Call any 7-param 3-return Lua function in a single line: */
template<
typename FnT, typename ArgT1, typename ArgT2, typename ArgT3, typename ArgT4, typename ArgT5,
typename ArgT6, typename ArgT7,
@@ -732,7 +734,7 @@ public:
return true;
}
- /// Call any 8-param 3-return Lua function in a single line:
+ /** Call any 8-param 3-return Lua function in a single line: */
template<
typename FnT, typename ArgT1, typename ArgT2, typename ArgT3, typename ArgT4, typename ArgT5,
typename ArgT6, typename ArgT7, typename ArgT8,
@@ -764,7 +766,7 @@ public:
return true;
}
- /// Call any 9-param 5-return Lua function in a single line:
+ /** Call any 9-param 5-return Lua function in a single line: */
template<
typename FnT, typename ArgT1, typename ArgT2, typename ArgT3, typename ArgT4, typename ArgT5,
typename ArgT6, typename ArgT7, typename ArgT8, typename ArgT9,
@@ -800,46 +802,71 @@ public:
}
- /// Returns true if the specified parameters on the stack are of the specified usertable type; also logs warning if not. Used for static functions
+ /** Returns true if the specified parameters on the stack are of the specified usertable type; also logs warning if not. Used for static functions */
bool CheckParamUserTable(int a_StartParam, const char * a_UserTable, int a_EndParam = -1);
- /// Returns true if the specified parameters on the stack are of the specified usertype; also logs warning if not. Used for regular functions
+ /** Returns true if the specified parameters on the stack are of the specified usertype; also logs warning if not. Used for regular functions */
bool CheckParamUserType(int a_StartParam, const char * a_UserType, int a_EndParam = -1);
- /// Returns true if the specified parameters on the stack are tables; also logs warning if not
+ /** Returns true if the specified parameters on the stack are tables; also logs warning if not */
bool CheckParamTable(int a_StartParam, int a_EndParam = -1);
- /// Returns true if the specified parameters on the stack are numbers; also logs warning if not
+ /** Returns true if the specified parameters on the stack are numbers; also logs warning if not */
bool CheckParamNumber(int a_StartParam, int a_EndParam = -1);
- /// Returns true if the specified parameters on the stack are strings; also logs warning if not
+ /** Returns true if the specified parameters on the stack are strings; also logs warning if not */
bool CheckParamString(int a_StartParam, int a_EndParam = -1);
- /// Returns true if the specified parameters on the stack are functions; also logs warning if not
+ /** Returns true if the specified parameters on the stack are functions; also logs warning if not */
bool CheckParamFunction(int a_StartParam, int a_EndParam = -1);
- /// Returns true if the specified parameter on the stack is nil (indicating an end-of-parameters)
+ /** Returns true if the specified parameter on the stack is nil (indicating an end-of-parameters) */
bool CheckParamEnd(int a_Param);
- /// If the status is nonzero, prints the text on the top of Lua stack and returns true
+ /** If the status is nonzero, prints the text on the top of Lua stack and returns true */
bool ReportErrors(int status);
- /// If the status is nonzero, prints the text on the top of Lua stack and returns true
+ /** If the status is nonzero, prints the text on the top of Lua stack and returns true */
static bool ReportErrors(lua_State * a_LuaState, int status);
- /// Logs all items in the current stack trace to the server console
+ /** Logs all items in the current stack trace to the server console */
void LogStackTrace(void);
- /// Logs all items in the current stack trace to the server console
+ /** Logs all items in the current stack trace to the server console */
static void LogStackTrace(lua_State * a_LuaState);
- /// Returns the type of the item on the specified position in the stack
+ /** Returns the type of the item on the specified position in the stack */
AString GetTypeText(int a_StackPos);
+ /** Calls the function specified by its name, with arguments copied off the foreign state.
+ If successful, keeps the return values on the stack and returns their number.
+ If unsuccessful, returns a negative number and keeps the stack position unchanged. */
+ int CallFunctionWithForeignParams(
+ const AString & a_FunctionName,
+ cLuaState & a_SrcLuaState,
+ int a_SrcParamStart,
+ int a_SrcParamEnd
+ );
+
+ /** Copies objects on the stack from the specified state.
+ Only numbers, bools, strings and userdatas are copied.
+ If successful, returns the number of objects copied.
+ If failed, returns a negative number and rewinds the stack position. */
+ int CopyStackFrom(cLuaState & a_SrcLuaState, int a_SrcStart, int a_SrcEnd);
+
+ /** Reads the value at the specified stack position as a string and sets it to a_String. */
+ void ToString(int a_StackPos, AString & a_String);
+
+ /** Logs all the elements' types on the API stack, with an optional header for the listing. */
+ void LogStack(const char * a_Header);
+
+ /** Logs all the elements' types on the API stack, with an optional header for the listing. */
+ static void LogStack(lua_State * a_LuaState, const char * a_Header = NULL);
+
protected:
lua_State * m_LuaState;
- /// If true, the state is owned by this object and will be auto-Closed. False => attached state
+ /** If true, the state is owned by this object and will be auto-Closed. False => attached state */
bool m_IsOwned;
/** The subsystem name is used for reporting errors to the console, it is either "plugin %s" or "LuaScript"
@@ -847,10 +874,10 @@ protected:
*/
AString m_SubsystemName;
- /// Name of the currently pushed function (for the Push / Call chain)
+ /** Name of the currently pushed function (for the Push / Call chain) */
AString m_CurrentFunctionName;
- /// Number of arguments currently pushed (for the Push / Call chain)
+ /** Number of arguments currently pushed (for the Push / Call chain) */
int m_NumCurrentFunctionArgs;
@@ -869,19 +896,19 @@ protected:
*/
bool PushFunction(const cTableRef & a_TableRef);
- /// Pushes a usertype of the specified class type onto the stack
+ /** Pushes a usertype of the specified class type onto the stack */
void PushUserType(void * a_Object, const char * a_Type);
- /// Retrieve value returned at a_StackPos, if it is a valid bool. If not, a_ReturnedVal is unchanged
+ /** Retrieve value returned at a_StackPos, if it is a valid bool. If not, a_ReturnedVal is unchanged */
void GetReturn(int a_StackPos, bool & a_ReturnedVal);
- /// Retrieve value returned at a_StackPos, if it is a valid string. If not, a_ReturnedVal is unchanged
+ /** Retrieve value returned at a_StackPos, if it is a valid string. If not, a_ReturnedVal is unchanged */
void GetReturn(int a_StackPos, AString & a_ReturnedVal);
- /// Retrieve value returned at a_StackPos, if it is a valid number. If not, a_ReturnedVal is unchanged
+ /** Retrieve value returned at a_StackPos, if it is a valid number. If not, a_ReturnedVal is unchanged */
void GetReturn(int a_StackPos, int & a_ReturnedVal);
- /// Retrieve value returned at a_StackPos, if it is a valid number. If not, a_ReturnedVal is unchanged
+ /** Retrieve value returned at a_StackPos, if it is a valid number. If not, a_ReturnedVal is unchanged */
void GetReturn(int a_StackPos, double & a_ReturnedVal);
/**
diff --git a/src/Bindings/ManualBindings.cpp b/src/Bindings/ManualBindings.cpp
index 2206dd371..dbaf32756 100644
--- a/src/Bindings/ManualBindings.cpp
+++ b/src/Bindings/ManualBindings.cpp
@@ -13,6 +13,7 @@
#include "../Entities/Player.h"
#include "../WebAdmin.h"
#include "../ClientHandle.h"
+#include "../BlockArea.h"
#include "../BlockEntities/ChestEntity.h"
#include "../BlockEntities/CommandBlockEntity.h"
#include "../BlockEntities/DispenserEntity.h"
@@ -22,6 +23,7 @@
#include "../BlockEntities/NoteEntity.h"
#include "md5/md5.h"
#include "../LineBlockTracer.h"
+#include "../WorldStorage/SchematicFileSerializer.h"
@@ -1427,7 +1429,10 @@ static int tolua_cPluginManager_BindCommand(lua_State * L)
// Read the arguments to this API call:
tolua_Error tolua_err;
int idx = 1;
- if (tolua_isusertype(L, 1, "cPluginManager", 0, &tolua_err))
+ if (
+ tolua_isusertype (L, 1, "cPluginManager", 0, &tolua_err) ||
+ tolua_isusertable(L, 1, "cPluginManager", 0, &tolua_err)
+ )
{
idx++;
}
@@ -1540,6 +1545,85 @@ static int tolua_cPluginManager_BindConsoleCommand(lua_State * L)
+static int tolua_cPluginManager_CallPlugin(lua_State * tolua_S)
+{
+ /*
+ Function signature:
+ cPluginManager:CallPlugin("PluginName", "FunctionName", args...)
+ */
+
+ // Check the parameters:
+ cLuaState L(tolua_S);
+ if (
+ !L.CheckParamUserTable(1, "cPluginManager") ||
+ !L.CheckParamString(2, 3))
+ {
+ return 0;
+ }
+
+ // Retrieve the plugin name and function name
+ AString PluginName, FunctionName;
+ L.ToString(2, PluginName);
+ L.ToString(3, FunctionName);
+ if (PluginName.empty() || FunctionName.empty())
+ {
+ LOGWARNING("cPluginManager:CallPlugin(): Invalid plugin name or function name");
+ L.LogStackTrace();
+ return 0;
+ }
+
+ // If requesting calling the current plugin, refuse:
+ cPluginLua * ThisPlugin = GetLuaPlugin(L);
+ if (ThisPlugin == NULL)
+ {
+ return 0;
+ }
+ if (ThisPlugin->GetName() == PluginName)
+ {
+ LOGWARNING("cPluginManager::CallPlugin(): Calling self is not implemented (why would it?)");
+ L.LogStackTrace();
+ return 0;
+ }
+
+ // Call the destination plugin using a plugin callback:
+ class cCallback :
+ public cPluginManager::cPluginCallback
+ {
+ public:
+ int m_NumReturns;
+
+ cCallback(const AString & a_FunctionName, cLuaState & a_SrcLuaState) :
+ m_FunctionName(a_FunctionName),
+ m_SrcLuaState(a_SrcLuaState),
+ m_NumReturns(0)
+ {
+ }
+ protected:
+ const AString & m_FunctionName;
+ cLuaState & m_SrcLuaState;
+
+ virtual bool Item(cPlugin * a_Plugin) override
+ {
+ m_NumReturns = ((cPluginLua *)a_Plugin)->CallFunctionFromForeignState(
+ m_FunctionName, m_SrcLuaState, 4, lua_gettop(m_SrcLuaState)
+ );
+ return true;
+ }
+ } Callback(FunctionName, L);
+ if (!cPluginManager::Get()->DoWithPlugin(PluginName, Callback))
+ {
+ // TODO 2014_01_20 _X: This might be too much logging, plugins cannot know if other plugins are loaded (async)
+ LOGWARNING("cPluginManager::CallPlugin: No such plugin name (\"%s\")", PluginName.c_str());
+ L.LogStackTrace();
+ return 0;
+ }
+ return Callback.m_NumReturns;
+}
+
+
+
+
+
static int tolua_cPlayer_GetGroups(lua_State* tolua_S)
{
cPlayer* self = (cPlayer*) tolua_tousertype(tolua_S,1,0);
@@ -1734,112 +1818,28 @@ static int tolua_cPluginLua_AddTab(lua_State* tolua_S)
-// Perhaps use this as well for copying tables https://github.com/keplerproject/rings/pull/1
-static int copy_lua_values(lua_State * a_Source, lua_State * a_Destination, int i, int top)
-{
- for(; i <= top; ++i )
- {
- int t = lua_type(a_Source, i);
- switch (t) {
- case LUA_TSTRING: /* strings */
- {
- const char * s = lua_tostring(a_Source, i);
- LOGD("%i push string: %s", i, s);
- tolua_pushstring(a_Destination, s);
- }
- break;
- case LUA_TBOOLEAN: /* booleans */
- {
- int b = tolua_toboolean(a_Source, i, false);
- LOGD("%i push bool: %i", i, b);
- tolua_pushboolean(a_Destination, b );
- }
- break;
- case LUA_TNUMBER: /* numbers */
- {
- lua_Number d = tolua_tonumber(a_Source, i, 0);
- LOGD("%i push number: %0.2f", i, d);
- tolua_pushnumber(a_Destination, d );
- }
- break;
- case LUA_TUSERDATA:
- {
- const char * type = 0;
- if (lua_getmetatable(a_Source,i))
- {
- lua_rawget(a_Source, LUA_REGISTRYINDEX);
- type = lua_tostring(a_Source, -1);
- lua_pop(a_Source, 1); // Pop.. something?! I don't knooow~~ T_T
- }
-
- // don't need tolua_tousertype we already have the type
- void * ud = tolua_touserdata(a_Source, i, 0);
- LOGD("%i push usertype: %p of type '%s'", i, ud, type);
- if( type == 0 )
- {
- LOGERROR("Call(): Something went wrong when trying to get usertype name!");
- return 0;
- }
- tolua_pushusertype(a_Destination, ud, type);
- }
- break;
- default: /* other values */
- LOGERROR("Call(): Unsupported value: '%s'. Can only use numbers and strings!", lua_typename(a_Source, t));
- return 0;
- }
- }
- return 1;
-}
-
-
-
-
-static int tolua_cPlugin_Call(lua_State* tolua_S)
+static int tolua_cPlugin_Call(lua_State * tolua_S)
{
- cPluginLua * self = (cPluginLua *) tolua_tousertype(tolua_S, 1, 0);
- lua_State* targetState = self->GetLuaState();
- int targetTop = lua_gettop(targetState);
-
- int top = lua_gettop(tolua_S);
- LOGD("total in stack: %i", top );
-
- std::string funcName = tolua_tostring(tolua_S, 2, "");
- LOGD("Func name: %s", funcName.c_str() );
-
- lua_getglobal(targetState, funcName.c_str());
- if(!lua_isfunction(targetState,-1))
- {
- LOGWARN("Error could not find function '%s' in plugin '%s'", funcName.c_str(), self->GetName().c_str() );
- lua_pop(targetState,1);
- return 0;
- }
-
- if( copy_lua_values(tolua_S, targetState, 3, top) == 0 ) // Start at 3 because 1 and 2 are the plugin and function name respectively
- {
- // something went wrong, exit
- return 0;
- }
+ cLuaState L(tolua_S);
- int s = lua_pcall(targetState, top - 2, LUA_MULTRET, 0);
- if (cLuaState::ReportErrors(targetState, s))
- {
- LOGWARN("Error while calling function '%s' in plugin '%s'", funcName.c_str(), self->GetName().c_str() );
- return 0;
- }
-
- int nresults = lua_gettop(targetState) - targetTop;
- LOGD("num results: %i", nresults);
- int ttop = lua_gettop(targetState);
- if( copy_lua_values(targetState, tolua_S, targetTop+1, ttop) == 0 ) // Start at targetTop+1 and I have no idea why xD
+ // Log the obsoletion warning:
+ LOGWARNING("cPlugin:Call() is obsolete and unsafe, use cPluginManager:CallPlugin() instead.");
+ L.LogStackTrace();
+
+ // Retrieve the params: plugin and the function name to call
+ cPluginLua * TargetPlugin = (cPluginLua *) tolua_tousertype(tolua_S, 1, 0);
+ AString FunctionName = tolua_tostring(tolua_S, 2, "");
+
+ // Call the function:
+ int NumReturns = TargetPlugin->CallFunctionFromForeignState(FunctionName, L, 3, lua_gettop(L));
+ if (NumReturns < 0)
{
- // something went wrong, exit
+ LOGWARNING("cPlugin::Call() failed to call destination function");
+ L.LogStackTrace();
return 0;
}
-
- lua_pop(targetState, nresults); // I have no idea what I'm doing, but it works
-
- return nresults;
+ return NumReturns;
}
@@ -2131,26 +2131,40 @@ protected:
static int tolua_cLineBlockTracer_Trace(lua_State * tolua_S)
{
- // cLineBlockTracer.Trace(World, Callbacks, StartX, StartY, StartZ, EndX, EndY, EndZ)
+ /* Supported function signatures:
+ cLineBlockTracer:Trace(World, Callbacks, StartX, StartY, StartZ, EndX, EndY, EndZ) // Canonical
+ cLineBlockTracer.Trace(World, Callbacks, StartX, StartY, StartZ, EndX, EndY, EndZ)
+ */
+
+ // If the first param is the cLineBlockTracer class, shift param index by one:
+ int idx = 1;
+ tolua_Error err;
+ if (tolua_isusertable(tolua_S, 1, "cLineBlockTracer", 0, &err))
+ {
+ idx = 2;
+ }
+
+ // Check params:
cLuaState L(tolua_S);
if (
- !L.CheckParamUserType(1, "cWorld") ||
- !L.CheckParamTable (2) ||
- !L.CheckParamNumber (3, 8) ||
- !L.CheckParamEnd (9)
+ !L.CheckParamUserType(idx, "cWorld") ||
+ !L.CheckParamTable (idx + 1) ||
+ !L.CheckParamNumber (idx + 2, idx + 7) ||
+ !L.CheckParamEnd (idx + 8)
)
{
return 0;
}
- cWorld * World = (cWorld *)tolua_tousertype(L, 1, NULL);
- cLuaBlockTracerCallbacks Callbacks(L, 2);
- double StartX = tolua_tonumber(L, 3, 0);
- double StartY = tolua_tonumber(L, 4, 0);
- double StartZ = tolua_tonumber(L, 5, 0);
- double EndX = tolua_tonumber(L, 6, 0);
- double EndY = tolua_tonumber(L, 7, 0);
- double EndZ = tolua_tonumber(L, 8, 0);
+ // Trace:
+ cWorld * World = (cWorld *)tolua_tousertype(L, idx, NULL);
+ cLuaBlockTracerCallbacks Callbacks(L, idx + 1);
+ double StartX = tolua_tonumber(L, idx + 2, 0);
+ double StartY = tolua_tonumber(L, idx + 3, 0);
+ double StartZ = tolua_tonumber(L, idx + 4, 0);
+ double EndX = tolua_tonumber(L, idx + 5, 0);
+ double EndY = tolua_tonumber(L, idx + 6, 0);
+ double EndZ = tolua_tonumber(L, idx + 7, 0);
bool res = cLineBlockTracer::Trace(*World, Callbacks, StartX, StartY, StartZ, EndX, EndY, EndZ);
tolua_pushboolean(L, res ? 1 : 0);
return 1;
@@ -2239,6 +2253,63 @@ static int tolua_cHopperEntity_GetOutputBlockPos(lua_State * tolua_S)
+
+static int tolua_cBlockArea_LoadFromSchematicFile(lua_State * tolua_S)
+{
+ // function cBlockArea::LoadFromSchematicFile
+ // Exported manually because function has been moved to SchematicFileSerilizer.cpp
+ cLuaState L(tolua_S);
+ if (
+ !L.CheckParamUserType(1, "cBlockArea") ||
+ !L.CheckParamString (2) ||
+ !L.CheckParamEnd (3)
+ )
+ {
+ return 0;
+ }
+ cBlockArea * self = (cBlockArea *)tolua_tousertype(tolua_S, 1, 0);
+ if (self == NULL)
+ {
+ tolua_error(tolua_S, "invalid 'self' in function 'cBlockArea::LoadFromSchematicFile'", NULL);
+ return 0;
+ }
+
+ AString Filename = tolua_tostring(tolua_S, 2, 0);
+ bool res = cSchematicFileSerializer::LoadFromSchematicFile(*self,Filename);
+ tolua_pushboolean(tolua_S, res);
+ return 1;
+}
+
+
+
+
+static int tolua_cBlockArea_SaveToSchematicFile(lua_State * tolua_S)
+{
+ // function cBlockArea::SaveToSchematicFile
+ // Exported manually because function has been moved to SchematicFileSerilizer.cpp
+ cLuaState L(tolua_S);
+ if (
+ !L.CheckParamUserType(1, "cBlockArea") ||
+ !L.CheckParamString (2) ||
+ !L.CheckParamEnd (3)
+ )
+ {
+ return 0;
+ }
+ cBlockArea * self = (cBlockArea *)tolua_tousertype(tolua_S, 1, 0);
+ if (self == NULL)
+ {
+ tolua_error(tolua_S, "invalid 'self' in function 'cBlockArea::SaveToSchematicFile'", NULL);
+ return 0;
+ }
+ AString Filename = tolua_tostring(tolua_S, 2, 0);
+ bool res = cSchematicFileSerializer::SaveToSchematicFile(*self,Filename);
+ tolua_pushboolean(tolua_S, res);
+ return 1;
+}
+
+
+
void ManualBindings::Bind(lua_State * tolua_S)
{
tolua_beginmodule(tolua_S, NULL);
@@ -2254,6 +2325,11 @@ void ManualBindings::Bind(lua_State * tolua_S)
tolua_function(tolua_S, "GetFolderContents", tolua_cFile_GetFolderContents);
tolua_endmodule(tolua_S);
+ tolua_beginmodule(tolua_S, "cBlockArea");
+ tolua_function(tolua_S, "LoadFromSchematicFile", tolua_cBlockArea_LoadFromSchematicFile);
+ tolua_function(tolua_S, "SaveToSchematicFile", tolua_cBlockArea_SaveToSchematicFile);
+ tolua_endmodule(tolua_S);
+
tolua_beginmodule(tolua_S, "cHopperEntity");
tolua_function(tolua_S, "GetOutputBlockPos", tolua_cHopperEntity_GetOutputBlockPos);
tolua_endmodule(tolua_S);
@@ -2305,6 +2381,7 @@ void ManualBindings::Bind(lua_State * tolua_S)
tolua_function(tolua_S, "AddHook", tolua_cPluginManager_AddHook);
tolua_function(tolua_S, "BindCommand", tolua_cPluginManager_BindCommand);
tolua_function(tolua_S, "BindConsoleCommand", tolua_cPluginManager_BindConsoleCommand);
+ tolua_function(tolua_S, "CallPlugin", tolua_cPluginManager_CallPlugin);
tolua_function(tolua_S, "ForEachCommand", tolua_cPluginManager_ForEachCommand);
tolua_function(tolua_S, "ForEachConsoleCommand", tolua_cPluginManager_ForEachConsoleCommand);
tolua_function(tolua_S, "GetAllPlugins", tolua_cPluginManager_GetAllPlugins);
diff --git a/src/Bindings/PluginLua.cpp b/src/Bindings/PluginLua.cpp
index 7fc1db391..773d522c4 100644
--- a/src/Bindings/PluginLua.cpp
+++ b/src/Bindings/PluginLua.cpp
@@ -88,36 +88,55 @@ bool cPluginLua::Initialize(void)
std::string PluginPath = FILE_IO_PREFIX + GetLocalFolder() + "/";
- // Load all files for this plugin, and execute them
+ // List all Lua files for this plugin. Info.lua has a special handling - make it the last to load:
AStringVector Files = cFile::GetFolderContents(PluginPath.c_str());
-
- int numFiles = 0;
- for (AStringVector::const_iterator itr = Files.begin(); itr != Files.end(); ++itr)
+ AStringVector LuaFiles;
+ bool HasInfoLua = false;
+ for (AStringVector::const_iterator itr = Files.begin(), end = Files.end(); itr != end; ++itr)
{
- if (itr->rfind(".lua") == AString::npos)
+ if (itr->rfind(".lua") != AString::npos)
{
- continue;
+ if (*itr == "Info.lua")
+ {
+ HasInfoLua = true;
+ }
+ else
+ {
+ LuaFiles.push_back(*itr);
+ }
}
+ }
+ std::sort(LuaFiles.begin(), LuaFiles.end());
+
+ // Warn if there are no Lua files in the plugin folder:
+ if (LuaFiles.empty())
+ {
+ LOGWARNING("No lua files found: plugin %s is missing.", GetName().c_str());
+ Close();
+ return false;
+ }
+
+ // Load all files in the list, including the Info.lua as last, if it exists:
+ for (AStringVector::const_iterator itr = LuaFiles.begin(), end = LuaFiles.end(); itr != end; ++itr)
+ {
AString Path = PluginPath + *itr;
if (!m_LuaState.LoadFile(Path))
{
Close();
return false;
- }
- else
- {
- numFiles++;
}
} // for itr - Files[]
-
- if (numFiles == 0)
+ if (HasInfoLua)
{
- LOGWARNING("No lua files found: plugin %s is missing.", GetName().c_str());
- Close();
- return false;
+ AString Path = PluginPath + "Info.lua";
+ if (!m_LuaState.LoadFile(Path))
+ {
+ Close();
+ return false;
+ }
}
- // Call intialize function
+ // Call the Initialize function:
bool res = false;
if (!m_LuaState.Call("Initialize", this, cLuaState::Return, res))
{
@@ -125,7 +144,6 @@ bool cPluginLua::Initialize(void)
Close();
return false;
}
-
if (!res)
{
LOGINFO("Plugin %s: Initialize() call failed, plugin is temporarily disabled.", GetName().c_str());
@@ -1489,6 +1507,40 @@ bool cPluginLua::AddHookRef(int a_HookType, int a_FnRefIdx)
+int cPluginLua::CallFunctionFromForeignState(
+ const AString & a_FunctionName,
+ cLuaState & a_ForeignState,
+ int a_ParamStart,
+ int a_ParamEnd
+)
+{
+ cCSLock Lock(m_CriticalSection);
+
+ // Call the function:
+ int NumReturns = m_LuaState.CallFunctionWithForeignParams(a_FunctionName, a_ForeignState, a_ParamStart, a_ParamEnd);
+ if (NumReturns < 0)
+ {
+ // The call has failed, an error has already been output to the log, so just silently bail out with the same error
+ return NumReturns;
+ }
+
+ // Copy all the return values:
+ int Top = lua_gettop(m_LuaState);
+ int res = a_ForeignState.CopyStackFrom(m_LuaState, Top - NumReturns + 1, Top);
+
+ // Remove the return values off this stack:
+ if (NumReturns > 0)
+ {
+ lua_pop(m_LuaState, NumReturns);
+ }
+
+ return res;
+}
+
+
+
+
+
AString cPluginLua::HandleWebRequest(const HTTPRequest * a_Request )
{
cCSLock Lock(m_CriticalSection);
diff --git a/src/Bindings/PluginLua.h b/src/Bindings/PluginLua.h
index 589de66fa..ad0cfbe5a 100644
--- a/src/Bindings/PluginLua.h
+++ b/src/Bindings/PluginLua.h
@@ -106,7 +106,7 @@ public:
virtual void ClearConsoleCommands(void) override;
- /// Returns true if the plugin contains the function for the specified hook type, using the old-style registration (#121)
+ /** 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 override
@@ -116,26 +116,26 @@ public:
virtual AString HandleWebRequest(const HTTPRequest * a_Request ) override;
bool AddWebTab(const AString & a_Title, lua_State * a_LuaState, int a_FunctionReference); // >> EXPORTED IN MANUALBINDINGS <<
- /// Binds the command to call the function specified by a Lua function reference. Simply adds to CommandMap.
+ /** 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.
+ /** 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())
+ /** 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
+ /** 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.
+ /** 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
+ /** 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.
@@ -144,37 +144,47 @@ public:
*/
bool AddHookRef(int a_HookType, int a_FnRefIdx);
+ /** 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.
+ Returns the number of values returned, if successful, or negative number on failure. */
+ int CallFunctionFromForeignState(
+ const AString & a_FunctionName,
+ cLuaState & a_ForeignState,
+ int a_ParamStart,
+ int a_ParamEnd
+ );
+
// The following templates allow calls to arbitrary Lua functions residing in the plugin:
- /// Call a Lua function with 0 args
+ /** Call a Lua function with 0 args */
template <typename FnT> bool Call(FnT a_Fn)
{
cCSLock Lock(m_CriticalSection);
return m_LuaState.Call(a_Fn);
}
- /// Call a Lua function with 1 arg
+ /** Call a Lua function with 1 arg */
template <typename FnT, typename ArgT0> bool Call(FnT a_Fn, ArgT0 a_Arg0)
{
cCSLock Lock(m_CriticalSection);
return m_LuaState.Call(a_Fn, a_Arg0);
}
- /// Call a Lua function with 2 args
+ /** Call a Lua function with 2 args */
template <typename FnT, typename ArgT0, typename ArgT1> bool Call(FnT a_Fn, ArgT0 a_Arg0, ArgT1 a_Arg1)
{
cCSLock Lock(m_CriticalSection);
return m_LuaState.Call(a_Fn, a_Arg0, a_Arg1);
}
- /// Call a Lua function with 3 args
+ /** Call a Lua function with 3 args */
template <typename FnT, typename ArgT0, typename ArgT1, typename ArgT2> bool Call(FnT a_Fn, ArgT0 a_Arg0, ArgT1 a_Arg1, ArgT2 a_Arg2)
{
cCSLock Lock(m_CriticalSection);
return m_LuaState.Call(a_Fn, a_Arg0, a_Arg1, a_Arg2);
}
- /// Call a Lua function with 4 args
+ /** Call a Lua function with 4 args */
template <typename FnT, typename ArgT0, typename ArgT1, typename ArgT2, typename ArgT3> bool Call(FnT a_Fn, ArgT0 a_Arg0, ArgT1 a_Arg1, ArgT2 a_Arg2, ArgT3 a_Arg3)
{
cCSLock Lock(m_CriticalSection);
@@ -182,13 +192,13 @@ public:
}
protected:
- /// Maps command name into Lua function reference
+ /** Maps command name into Lua function reference */
typedef std::map<AString, int> CommandMap;
- /// Provides an array of Lua function references
+ /** Provides an array of Lua function references */
typedef std::vector<cLuaState::cRef *> cLuaRefs;
- /// Maps hook types into arrays of Lua function references to call for each hook type
+ /** Maps hook types into arrays of Lua function references to call for each hook type */
typedef std::map<int, cLuaRefs> cHookMap;
cCriticalSection m_CriticalSection;
@@ -199,7 +209,7 @@ protected:
cHookMap m_HookMap;
- /// Releases all Lua references and closes the LuaState
+ /** Releases all Lua references and closes the LuaState */
void Close(void);
} ; // tolua_export
diff --git a/src/Bindings/PluginManager.cpp b/src/Bindings/PluginManager.cpp
index 9e318554a..a20583550 100644
--- a/src/Bindings/PluginManager.cpp
+++ b/src/Bindings/PluginManager.cpp
@@ -1757,6 +1757,21 @@ bool cPluginManager::IsValidHookType(int a_HookType)
+bool cPluginManager::DoWithPlugin(const AString & a_PluginName, cPluginCallback & a_Callback)
+{
+ // TODO: Implement locking for plugins
+ PluginMap::iterator itr = m_Plugins.find(a_PluginName);
+ if ((itr == m_Plugins.end()) || (itr->second == NULL))
+ {
+ return false;
+ }
+ return a_Callback.Item(itr->second);
+}
+
+
+
+
+
bool cPluginManager::AddPlugin(cPlugin * a_Plugin)
{
m_Plugins[a_Plugin->GetDirectory()] = a_Plugin;
diff --git a/src/Bindings/PluginManager.h b/src/Bindings/PluginManager.h
index a700ef6ce..c78bceda1 100644
--- a/src/Bindings/PluginManager.h
+++ b/src/Bindings/PluginManager.h
@@ -123,7 +123,7 @@ public: // tolua_export
} ;
// tolua_end
- /// Used as a callback for enumerating bound commands
+ /** Used as a callback for enumerating bound commands */
class cCommandEnumCallback
{
public:
@@ -133,7 +133,11 @@ public: // tolua_export
virtual bool Command(const AString & a_Command, const cPlugin * a_Plugin, const AString & a_Permission, const AString & a_HelpString) = 0;
} ;
- /// Returns the instance of the Plugin Manager (there is only ever one)
+ /** The interface used for enumerating and extern-calling plugins */
+ typedef cItemCallback<cPlugin> cPluginCallback;
+
+
+ /** Returns the instance of the Plugin Manager (there is only ever one) */
static cPluginManager * Get(void); // tolua_export
typedef std::map< AString, cPlugin * > PluginMap;
@@ -144,7 +148,7 @@ public: // tolua_export
void FindPlugins(); // tolua_export
void ReloadPlugins(); // tolua_export
- /// Adds the plugin to the list of plugins called for the specified hook type. Handles multiple adds as a single add
+ /** Adds the plugin to the list of plugins called for the specified hook type. Handles multiple adds as a single add */
void AddHook(cPlugin * a_Plugin, int a_HookType);
unsigned int GetNumPlugins() const; // tolua_export
@@ -208,46 +212,46 @@ public: // tolua_export
bool DisablePlugin(const AString & a_PluginName); // tolua_export
bool LoadPlugin (const AString & a_PluginName); // tolua_export
- /// Removes all hooks the specified plugin has registered
+ /** Removes all hooks the specified plugin has registered */
void RemoveHooks(cPlugin * a_Plugin);
- /// Removes the plugin from the internal structures and deletes its object.
+ /** Removes the plugin from the internal structures and deletes its object. */
void RemovePlugin(cPlugin * a_Plugin);
- /// Removes all command bindings that the specified plugin has made
+ /** Removes all command bindings that the specified plugin has made */
void RemovePluginCommands(cPlugin * a_Plugin);
- /// Binds a command to the specified plugin. Returns true if successful, false if command already bound.
+ /** Binds a command to the specified plugin. Returns true if successful, false if command already bound. */
bool BindCommand(const AString & a_Command, cPlugin * a_Plugin, const AString & a_Permission, const AString & a_HelpString); // Exported in ManualBindings.cpp, without the a_Plugin param
- /// Calls a_Callback for each bound command, returns true if all commands were enumerated
+ /** Calls a_Callback for each bound command, returns true if all commands were enumerated */
bool ForEachCommand(cCommandEnumCallback & a_Callback); // Exported in ManualBindings.cpp
- /// Returns true if the command is in the command map
+ /** Returns true if the command is in the command map */
bool IsCommandBound(const AString & a_Command); // tolua_export
- /// Returns the permission needed for the specified command; empty string if command not found
+ /** Returns the permission needed for the specified command; empty string if command not found */
AString GetCommandPermission(const AString & a_Command); // tolua_export
- /// Executes the command, as if it was requested by a_Player. Checks permissions first. Returns true if executed.
+ /** Executes the command, as if it was requested by a_Player. Checks permissions first. Returns true if executed. */
bool ExecuteCommand(cPlayer * a_Player, const AString & a_Command); // tolua_export
- /// Executes the command, as if it was requested by a_Player. Permisssions are not checked. Returns true if executed (false if not found)
+ /** Executes the command, as if it was requested by a_Player. Permisssions are not checked. Returns true if executed (false if not found) */
bool ForceExecuteCommand(cPlayer * a_Player, const AString & a_Command); // tolua_export
- /// Removes all console command bindings that the specified plugin has made
+ /** Removes all console command bindings that the specified plugin has made */
void RemovePluginConsoleCommands(cPlugin * a_Plugin);
- /// Binds a console command to the specified plugin. Returns true if successful, false if command already bound.
+ /** Binds a console command to the specified plugin. Returns true if successful, false if command already bound. */
bool BindConsoleCommand(const AString & a_Command, cPlugin * a_Plugin, const AString & a_HelpString); // Exported in ManualBindings.cpp, without the a_Plugin param
- /// Calls a_Callback for each bound console command, returns true if all commands were enumerated
+ /** Calls a_Callback for each bound console command, returns true if all commands were enumerated */
bool ForEachConsoleCommand(cCommandEnumCallback & a_Callback); // Exported in ManualBindings.cpp
- /// Returns true if the console command is in the command map
+ /** Returns true if the console command is in the command map */
bool IsConsoleCommandBound(const AString & a_Command); // tolua_export
- /// Executes the command split into a_Split, as if it was given on the console. Returns true if executed. Output is sent to the a_Output callback
+ /** Executes the command split into a_Split, as if it was given on the console. Returns true if executed. Output is sent to the a_Output callback */
bool ExecuteConsoleCommand(const AStringVector & a_Split, cCommandOutputCallback & a_Output);
/** Appends all commands beginning with a_Text (case-insensitive) into a_Results.
@@ -255,9 +259,13 @@ public: // tolua_export
*/
void TabCompleteCommand(const AString & a_Text, AStringVector & a_Results, cPlayer * a_Player);
- /// Returns true if the specified hook type is within the allowed range
+ /** Returns true if the specified hook type is within the allowed range */
static bool IsValidHookType(int a_HookType);
+ /** Calls the specified callback with the plugin object of the specified plugin.
+ Returns false if plugin not found, and the value that the callback has returned otherwise. */
+ bool DoWithPlugin(const AString & a_PluginName, cPluginCallback & a_Callback);
+
private:
friend class cRoot;
@@ -283,22 +291,22 @@ private:
cPluginManager();
virtual ~cPluginManager();
- /// Reloads all plugins, defaulting to settings.ini for settings location
+ /** Reloads all plugins, defaulting to settings.ini for settings location */
void ReloadPluginsNow(void);
- /// Reloads all plugins with a cIniFile object expected to be initialised to settings.ini
+ /** Reloads all plugins with a cIniFile object expected to be initialised to settings.ini */
void ReloadPluginsNow(cIniFile & a_SettingsIni);
- /// Unloads all plugins
+ /** Unloads all plugins */
void UnloadPluginsNow(void);
- /// Handles writing default plugins if 'Plugins' key not found using a cIniFile object expected to be intialised to settings.ini
+ /** Handles writing default plugins if 'Plugins' key not found using a cIniFile object expected to be intialised to settings.ini */
void InsertDefaultPlugins(cIniFile & a_SettingsIni);
- /// Adds the plugin into the internal list of plugins and initializes it. If initialization fails, the plugin is removed again.
+ /** Adds the plugin into the internal list of plugins and initializes it. If initialization fails, the plugin is removed again. */
bool AddPlugin(cPlugin * a_Plugin);
- /// Tries to match a_Command to the internal table of commands, if a match is found, the corresponding plugin is called. Returns true if the command is handled.
+ /** Tries to match a_Command to the internal table of commands, if a match is found, the corresponding plugin is called. Returns true if the command is handled. */
bool HandleCommand(cPlayer * a_Player, const AString & a_Command, bool a_ShouldCheckPermissions, bool & a_WasCommandForbidden);
bool HandleCommand(cPlayer * a_Player, const AString & a_Command, bool a_ShouldCheckPermissions)
{