summaryrefslogtreecommitdiffstats
path: root/src/Bindings
diff options
context:
space:
mode:
authormadmaxoft <github@xoft.cz>2014-01-12 10:04:16 +0100
committermadmaxoft <github@xoft.cz>2014-01-12 10:04:16 +0100
commit180b9b90993870532a8daa2ee6498005b6c3bf49 (patch)
treedac846b760b6f5fd294fbd771975dd55b0856c0d /src/Bindings
parentFixed Linux configuration. (diff)
parentDisabled a useless MSVC warning in Bindings.cpp. (diff)
downloadcuberite-180b9b90993870532a8daa2ee6498005b6c3bf49.tar
cuberite-180b9b90993870532a8daa2ee6498005b6c3bf49.tar.gz
cuberite-180b9b90993870532a8daa2ee6498005b6c3bf49.tar.bz2
cuberite-180b9b90993870532a8daa2ee6498005b6c3bf49.tar.lz
cuberite-180b9b90993870532a8daa2ee6498005b6c3bf49.tar.xz
cuberite-180b9b90993870532a8daa2ee6498005b6c3bf49.tar.zst
cuberite-180b9b90993870532a8daa2ee6498005b6c3bf49.zip
Diffstat (limited to 'src/Bindings')
-rw-r--r--src/Bindings/AllToLua.pkg3
-rw-r--r--src/Bindings/CMakeLists.txt23
-rw-r--r--src/Bindings/LuaState.cpp68
-rw-r--r--src/Bindings/LuaState.h141
-rw-r--r--src/Bindings/ManualBindings.cpp148
-rw-r--r--src/Bindings/Plugin.h4
-rw-r--r--src/Bindings/PluginLua.cpp104
-rw-r--r--src/Bindings/PluginLua.h4
-rw-r--r--src/Bindings/PluginManager.cpp89
-rw-r--r--src/Bindings/PluginManager.h10
-rw-r--r--src/Bindings/tolua++.h6
-rw-r--r--src/Bindings/tolua_base.h128
12 files changed, 453 insertions, 275 deletions
diff --git a/src/Bindings/AllToLua.pkg b/src/Bindings/AllToLua.pkg
index e9a5ea0c6..2d0300ebf 100644
--- a/src/Bindings/AllToLua.pkg
+++ b/src/Bindings/AllToLua.pkg
@@ -1,8 +1,6 @@
$#include "../Globals.h"
-$#include "tolua_base.h"
-
// Typedefs from Globals.h, so that we don't have to process that file:
typedef long long Int64;
typedef int Int32;
@@ -14,6 +12,7 @@ typedef unsigned short UInt16;
$cfile "../ChunkDef.h"
+$cfile "../BiomeDef.h"
$cfile "../../lib/inifile/iniFile.h"
diff --git a/src/Bindings/CMakeLists.txt b/src/Bindings/CMakeLists.txt
index 41c641d9d..50b81e42a 100644
--- a/src/Bindings/CMakeLists.txt
+++ b/src/Bindings/CMakeLists.txt
@@ -2,17 +2,22 @@
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_BINARY_DIR}/Bindings.cpp ${CMAKE_CURRENT_BINARY_DIR}/Bindings.h
-#command execuded to regerate bindings
- COMMAND tolua -L virtual_method_hooks.lua -o Bindings.cpp -H Bindings.h AllToLua.pkg
-#add any new generation dependencies here
- DEPENDS ${CMAKE_CURRENT_SOURCE_DIR}/virtual_method_hooks.lua ${CMAKE_CURRENT_SOURCE_DIR}/AllToLua.pkg tolua
- )
-
+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)
diff --git a/src/Bindings/LuaState.cpp b/src/Bindings/LuaState.cpp
index 64a818a60..149c304ed 100644
--- a/src/Bindings/LuaState.cpp
+++ b/src/Bindings/LuaState.cpp
@@ -228,6 +228,9 @@ bool cLuaState::PushFunction(const char * a_FunctionName)
return false;
}
+ // Push the error handler for lua_pcall()
+ lua_pushcfunction(m_LuaState, &ReportFnCallErrors);
+
lua_getglobal(m_LuaState, a_FunctionName);
if (!lua_isfunction(m_LuaState, -1))
{
@@ -249,6 +252,9 @@ bool cLuaState::PushFunction(int a_FnRef)
ASSERT(IsValid());
ASSERT(m_NumCurrentFunctionArgs == -1); // If not, there's already something pushed onto the stack
+ // Push the error handler for lua_pcall()
+ lua_pushcfunction(m_LuaState, &ReportFnCallErrors);
+
lua_rawgeti(m_LuaState, LUA_REGISTRYINDEX, a_FnRef); // same as lua_getref()
if (!lua_isfunction(m_LuaState, -1))
{
@@ -264,27 +270,29 @@ bool cLuaState::PushFunction(int a_FnRef)
-bool cLuaState::PushFunctionFromRefTable(cRef & a_TableRef, const char * a_FnName)
+bool cLuaState::PushFunction(const cTableRef & a_TableRef)
{
ASSERT(IsValid());
ASSERT(m_NumCurrentFunctionArgs == -1); // If not, there's already something pushed onto the stack
-
- lua_rawgeti(m_LuaState, LUA_REGISTRYINDEX, a_TableRef); // Get the table ref
+
+ // Push the error handler for lua_pcall()
+ lua_pushcfunction(m_LuaState, &ReportFnCallErrors);
+
+ lua_rawgeti(m_LuaState, LUA_REGISTRYINDEX, a_TableRef.GetTableRef()); // Get the table ref
if (!lua_istable(m_LuaState, -1))
{
// Not a table, bail out
lua_pop(m_LuaState, 1);
return false;
}
- lua_getfield(m_LuaState, -1, a_FnName);
+ 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);
return false;
}
- lua_remove(m_LuaState, -2); // Remove the table ref from the stack
- m_CurrentFunctionName = "<table_callback>";
+ Printf(m_CurrentFunctionName, "<table-callback %s>", a_TableRef.GetFnName());
m_NumCurrentFunctionArgs = 0;
return true;
}
@@ -297,7 +305,7 @@ void cLuaState::Push(const AString & a_String)
{
ASSERT(IsValid());
- tolua_pushcppstring(m_LuaState, a_String);
+ lua_pushlstring(m_LuaState, a_String.data(), a_String.size());
m_NumCurrentFunctionArgs += 1;
}
@@ -468,6 +476,18 @@ void cLuaState::Push(cItems * a_Items)
+void cLuaState::Push(const cItems & a_Items)
+{
+ ASSERT(IsValid());
+
+ tolua_pushusertype(m_LuaState, (void *)&a_Items, "cItems");
+ m_NumCurrentFunctionArgs += 1;
+}
+
+
+
+
+
void cLuaState::Push(cClientHandle * a_Client)
{
ASSERT(IsValid());
@@ -720,11 +740,13 @@ void cLuaState::GetReturn(int a_StackPos, double & a_ReturnedVal)
bool cLuaState::CallFunction(int a_NumResults)
{
ASSERT (m_NumCurrentFunctionArgs >= 0); // A function must be pushed to stack first
- ASSERT(lua_isfunction(m_LuaState, -m_NumCurrentFunctionArgs - 1));
+ ASSERT(lua_isfunction(m_LuaState, -m_NumCurrentFunctionArgs - 1)); // The function to call
+ ASSERT(lua_isfunction(m_LuaState, -m_NumCurrentFunctionArgs - 2)); // The error handler
- int s = lua_pcall(m_LuaState, m_NumCurrentFunctionArgs, a_NumResults, 0);
- if (ReportErrors(s))
+ int s = lua_pcall(m_LuaState, m_NumCurrentFunctionArgs, a_NumResults, -m_NumCurrentFunctionArgs - 2);
+ if (s != 0)
{
+ // The error has already been printed together with the stacktrace
LOGWARNING("Error in %s calling function %s()", m_SubsystemName.c_str(), m_CurrentFunctionName.c_str());
m_NumCurrentFunctionArgs = -1;
m_CurrentFunctionName.clear();
@@ -952,15 +974,24 @@ bool cLuaState::ReportErrors(lua_State * a_LuaState, int a_Status)
void cLuaState::LogStackTrace(void)
{
+ LogStackTrace(m_LuaState);
+}
+
+
+
+
+
+void cLuaState::LogStackTrace(lua_State * a_LuaState)
+{
LOGWARNING("Stack trace:");
lua_Debug entry;
int depth = 0;
- while (lua_getstack(m_LuaState, depth, &entry))
+ while (lua_getstack(a_LuaState, depth, &entry))
{
- int status = lua_getinfo(m_LuaState, "Sln", &entry);
+ int status = lua_getinfo(a_LuaState, "Sln", &entry);
assert(status);
- LOGWARNING(" %s(%d): %s", entry.short_src, entry.currentline, entry.name ? entry.name : "?");
+ LOGWARNING(" %s(%d): %s", entry.short_src, entry.currentline, entry.name ? entry.name : "(no name)");
depth++;
}
LOGWARNING("Stack trace end");
@@ -993,6 +1024,17 @@ AString cLuaState::GetTypeText(int a_StackPos)
+int cLuaState::ReportFnCallErrors(lua_State * a_LuaState)
+{
+ LOGWARNING("LUA: %s", lua_tostring(a_LuaState, -1));
+ LogStackTrace(a_LuaState);
+ return 1; // We left the error message on the stack as the return value
+}
+
+
+
+
+
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// cLuaState::cRef:
diff --git a/src/Bindings/LuaState.h b/src/Bindings/LuaState.h
index 15b0cdeff..a43d39732 100644
--- a/src/Bindings/LuaState.h
+++ b/src/Bindings/LuaState.h
@@ -85,6 +85,23 @@ public:
} ;
+ /** Used for calling functions stored in a reference-stored table */
+ class cTableRef
+ {
+ int m_TableRef;
+ const char * m_FnName;
+ public:
+ cTableRef(int a_TableRef, const char * a_FnName) :
+ m_TableRef(a_TableRef),
+ m_FnName(a_FnName)
+ {
+ }
+
+ int GetTableRef(void) const { return m_TableRef; }
+ const char * GetFnName(void) const { return m_FnName; }
+ } ;
+
+
/// A dummy class that's used only to delimit function args from return values for cLuaState::Call()
class cRet
{
@@ -133,24 +150,6 @@ public:
/// Returns true if a_FunctionName is a valid Lua function that can be called
bool HasFunction(const char * a_FunctionName);
- /** Pushes the function of the specified name onto the stack.
- Returns true if successful. Logs a warning on failure (incl. m_SubsystemName)
- */
- bool PushFunction(const char * a_FunctionName);
-
- /** Pushes a function that has been saved into the global registry, identified by a_FnRef.
- Returns true if successful. Logs a warning on failure
- */
- bool PushFunction(int a_FnRef);
-
- /** Pushes a function that is stored in a table ref.
- Returns true if successful, false on failure. Doesn't log failure.
- */
- bool PushFunctionFromRefTable(cRef & a_TableRef, const char * a_FnName);
-
- /// Pushes a usertype of the specified class type onto the stack
- void PushUserType(void * a_Object, const char * a_Type);
-
// Push a value onto the stack
void Push(const AString & a_String);
void Push(const AStringVector & a_Vector);
@@ -165,6 +164,7 @@ public:
void Push(cMonster * a_Monster);
void Push(cItem * a_Item);
void Push(cItems * a_Items);
+ void Push(const cItems & a_Items);
void Push(cClientHandle * a_ClientHandle);
void Push(cPickup * a_Pickup);
void Push(cChunkDesc * a_ChunkDesc);
@@ -182,7 +182,7 @@ public:
void Push(void * a_Ptr);
void Push(cHopperEntity * a_Hopper);
void Push(cBlockEntity * a_BlockEntity);
-
+
/// Call any 0-param 0-return Lua function in a single line:
template <typename FnT>
bool Call(FnT a_FnName)
@@ -240,12 +240,33 @@ public:
return CallFunction(0);
}
+ /// Call any 0-param 1-return Lua function in a single line:
+ template<
+ typename FnT, typename RetT1
+ >
+ bool Call(FnT a_FnName, const cRet & a_Mark, RetT1 & a_Ret1)
+ {
+ UNUSED(a_Mark);
+ if (!PushFunction(a_FnName))
+ {
+ return false;
+ }
+ if (!CallFunction(1))
+ {
+ return false;
+ }
+ GetReturn(-1, a_Ret1);
+ lua_pop(m_LuaState, 1);
+ return true;
+ }
+
/// 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)
{
+ UNUSED(a_Mark);
if (!PushFunction(a_FnName))
{
return false;
@@ -266,6 +287,7 @@ public:
>
bool Call(FnT a_FnName, ArgT1 a_Arg1, ArgT2 a_Arg2, const cRet & a_Mark, RetT1 & a_Ret1)
{
+ UNUSED(a_Mark);
if (!PushFunction(a_FnName))
{
return false;
@@ -287,6 +309,7 @@ public:
>
bool Call(FnT a_FnName, ArgT1 a_Arg1, ArgT2 a_Arg2, ArgT3 a_Arg3, const cRet & a_Mark, RetT1 & a_Ret1)
{
+ UNUSED(a_Mark);
if (!PushFunction(a_FnName))
{
return false;
@@ -309,6 +332,7 @@ public:
>
bool Call(FnT a_FnName, ArgT1 a_Arg1, ArgT2 a_Arg2, ArgT3 a_Arg3, ArgT4 a_Arg4, const cRet & a_Mark, RetT1 & a_Ret1)
{
+ UNUSED(a_Mark);
if (!PushFunction(a_FnName))
{
return false;
@@ -332,6 +356,7 @@ public:
>
bool Call(FnT a_FnName, ArgT1 a_Arg1, ArgT2 a_Arg2, ArgT3 a_Arg3, ArgT4 a_Arg4, ArgT5 a_Arg5, const cRet & a_Mark, RetT1 & a_Ret1)
{
+ UNUSED(a_Mark);
if (!PushFunction(a_FnName))
{
return false;
@@ -357,6 +382,7 @@ public:
>
bool Call(FnT a_FnName, ArgT1 a_Arg1, ArgT2 a_Arg2, ArgT3 a_Arg3, ArgT4 a_Arg4, ArgT5 a_Arg5, ArgT6 a_Arg6, const cRet & a_Mark, RetT1 & a_Ret1)
{
+ UNUSED(a_Mark);
if (!PushFunction(a_FnName))
{
return false;
@@ -383,6 +409,7 @@ public:
>
bool Call(FnT a_FnName, ArgT1 a_Arg1, ArgT2 a_Arg2, ArgT3 a_Arg3, ArgT4 a_Arg4, ArgT5 a_Arg5, ArgT6 a_Arg6, ArgT7 a_Arg7, const cRet & a_Mark, RetT1 & a_Ret1)
{
+ UNUSED(a_Mark);
if (!PushFunction(a_FnName))
{
return false;
@@ -410,6 +437,7 @@ public:
>
bool Call(FnT a_FnName, ArgT1 a_Arg1, ArgT2 a_Arg2, ArgT3 a_Arg3, ArgT4 a_Arg4, ArgT5 a_Arg5, ArgT6 a_Arg6, ArgT7 a_Arg7, ArgT8 a_Arg8, const cRet & a_Mark, RetT1 & a_Ret1)
{
+ UNUSED(a_Mark);
if (!PushFunction(a_FnName))
{
return false;
@@ -438,6 +466,7 @@ public:
>
bool Call(FnT a_FnName, ArgT1 a_Arg1, ArgT2 a_Arg2, ArgT3 a_Arg3, ArgT4 a_Arg4, ArgT5 a_Arg5, ArgT6 a_Arg6, ArgT7 a_Arg7, ArgT8 a_Arg8, ArgT9 a_Arg9, const cRet & a_Mark, RetT1 & a_Ret1)
{
+ UNUSED(a_Mark);
if (!PushFunction(a_FnName))
{
return false;
@@ -467,6 +496,7 @@ public:
>
bool Call(FnT a_FnName, ArgT1 a_Arg1, ArgT2 a_Arg2, ArgT3 a_Arg3, ArgT4 a_Arg4, ArgT5 a_Arg5, ArgT6 a_Arg6, ArgT7 a_Arg7, ArgT8 a_Arg8, ArgT9 a_Arg9, ArgT10 a_Arg10, const cRet & a_Mark, RetT1 & a_Ret1)
{
+ UNUSED(a_Mark);
if (!PushFunction(a_FnName))
{
return false;
@@ -496,6 +526,7 @@ public:
>
bool Call(FnT a_FnName, ArgT1 a_Arg1, const cRet & a_Mark, RetT1 & a_Ret1, RetT2 & a_Ret2)
{
+ UNUSED(a_Mark);
if (!PushFunction(a_FnName))
{
return false;
@@ -517,6 +548,7 @@ public:
>
bool Call(FnT a_FnName, ArgT1 a_Arg1, ArgT2 a_Arg2, const cRet & a_Mark, RetT1 & a_Ret1, RetT2 & a_Ret2)
{
+ UNUSED(a_Mark);
if (!PushFunction(a_FnName))
{
return false;
@@ -540,6 +572,7 @@ public:
>
bool Call(FnT a_FnName, ArgT1 a_Arg1, ArgT2 a_Arg2, ArgT3 a_Arg3, const cRet & a_Mark, RetT1 & a_Ret1, RetT2 & a_Ret2)
{
+ UNUSED(a_Mark);
if (!PushFunction(a_FnName))
{
return false;
@@ -564,6 +597,7 @@ public:
>
bool Call(FnT a_FnName, ArgT1 a_Arg1, ArgT2 a_Arg2, ArgT3 a_Arg3, ArgT4 a_Arg4, const cRet & a_Mark, RetT1 & a_Ret1, RetT2 & a_Ret2)
{
+ UNUSED(a_Mark);
if (!PushFunction(a_FnName))
{
return false;
@@ -589,6 +623,7 @@ public:
>
bool Call(FnT a_FnName, ArgT1 a_Arg1, ArgT2 a_Arg2, ArgT3 a_Arg3, ArgT4 a_Arg4, ArgT5 a_Arg5, const cRet & a_Mark, RetT1 & a_Ret1, RetT2 & a_Ret2)
{
+ UNUSED(a_Mark);
if (!PushFunction(a_FnName))
{
return false;
@@ -616,6 +651,7 @@ public:
>
bool Call(FnT a_FnName, ArgT1 a_Arg1, ArgT2 a_Arg2, ArgT3 a_Arg3, ArgT4 a_Arg4, ArgT5 a_Arg5, ArgT6 a_Arg6, const cRet & a_Mark, RetT1 & a_Ret1, RetT2 & a_Ret2)
{
+ UNUSED(a_Mark);
if (!PushFunction(a_FnName))
{
return false;
@@ -644,6 +680,7 @@ public:
>
bool Call(FnT a_FnName, ArgT1 a_Arg1, ArgT2 a_Arg2, ArgT3 a_Arg3, ArgT4 a_Arg4, ArgT5 a_Arg5, ArgT6 a_Arg6, ArgT7 a_Arg7, const cRet & a_Mark, RetT1 & a_Ret1, RetT2 & a_Ret2)
{
+ UNUSED(a_Mark);
if (!PushFunction(a_FnName))
{
return false;
@@ -673,6 +710,7 @@ public:
>
bool Call(FnT a_FnName, ArgT1 a_Arg1, ArgT2 a_Arg2, ArgT3 a_Arg3, ArgT4 a_Arg4, ArgT5 a_Arg5, ArgT6 a_Arg6, ArgT7 a_Arg7, const cRet & a_Mark, RetT1 & a_Ret1, RetT2 & a_Ret2, RetT3 & a_Ret3)
{
+ UNUSED(a_Mark);
if (!PushFunction(a_FnName))
{
return false;
@@ -703,6 +741,7 @@ public:
>
bool Call(FnT a_FnName, ArgT1 a_Arg1, ArgT2 a_Arg2, ArgT3 a_Arg3, ArgT4 a_Arg4, ArgT5 a_Arg5, ArgT6 a_Arg6, ArgT7 a_Arg7, ArgT8 a_Arg8, const cRet & a_Mark, RetT1 & a_Ret1, RetT2 & a_Ret2, RetT3 & a_Ret3)
{
+ UNUSED(a_Mark);
if (!PushFunction(a_FnName))
{
return false;
@@ -734,6 +773,7 @@ public:
>
bool Call(FnT a_FnName, ArgT1 a_Arg1, ArgT2 a_Arg2, ArgT3 a_Arg3, ArgT4 a_Arg4, ArgT5 a_Arg5, ArgT6 a_Arg6, ArgT7 a_Arg7, ArgT8 a_Arg8, ArgT9 a_Arg9, const cRet & a_Mark, RetT1 & a_Ret1, RetT2 & a_Ret2, RetT3 & a_Ret3, RetT4 & a_Ret4, RetT5 & a_Ret5)
{
+ UNUSED(a_Mark);
if (!PushFunction(a_FnName))
{
return false;
@@ -761,25 +801,6 @@ public:
}
- /// 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
- 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
- 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
- void GetReturn(int a_StackPos, double & a_ReturnedVal);
-
- /**
- Calls the function that has been pushed onto the stack by PushFunction(),
- with arguments pushed by PushXXX().
- Returns true if successful, logs a warning on failure.
- */
- bool CallFunction(int a_NumReturnValues);
-
/// 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);
@@ -807,6 +828,9 @@ public:
/// 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
+ static void LogStackTrace(lua_State * a_LuaState);
+
/// Returns the type of the item on the specified position in the stack
AString GetTypeText(int a_StackPos);
@@ -826,6 +850,47 @@ protected:
/// Number of arguments currently pushed (for the Push / Call chain)
int m_NumCurrentFunctionArgs;
+
+
+ /** Pushes the function of the specified name onto the stack.
+ Returns true if successful. Logs a warning on failure (incl. m_SubsystemName)
+ */
+ bool PushFunction(const char * a_FunctionName);
+
+ /** Pushes a function that has been saved into the global registry, identified by a_FnRef.
+ Returns true if successful. Logs a warning on failure
+ */
+ bool PushFunction(int a_FnRef);
+
+ /** Pushes a function that is stored in a referenced table by name
+ Returns true if successful. Logs a warning on failure
+ */
+ bool PushFunction(const cTableRef & a_TableRef);
+
+ /// 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
+ 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
+ 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
+ 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
+ void GetReturn(int a_StackPos, double & a_ReturnedVal);
+
+ /**
+ Calls the function that has been pushed onto the stack by PushFunction(),
+ with arguments pushed by PushXXX().
+ Returns true if successful, logs a warning on failure.
+ */
+ bool CallFunction(int a_NumReturnValues);
+
+ /** Used as the error reporting function for function calls */
+ static int ReportFnCallErrors(lua_State * a_LuaState);
} ;
diff --git a/src/Bindings/ManualBindings.cpp b/src/Bindings/ManualBindings.cpp
index 2e19c2581..a9368f613 100644
--- a/src/Bindings/ManualBindings.cpp
+++ b/src/Bindings/ManualBindings.cpp
@@ -991,7 +991,6 @@ static int tolua_cPluginManager_GetAllPlugins(lua_State * tolua_S)
const cPluginManager::PluginMap & AllPlugins = self->GetAllPlugins();
lua_newtable(tolua_S);
- int newTable = lua_gettop(tolua_S);
int index = 1;
cPluginManager::PluginMap::const_iterator iter = AllPlugins.begin();
while (iter != AllPlugins.end())
@@ -1137,16 +1136,17 @@ static int tolua_cPluginManager_AddHook(lua_State * tolua_S)
{
/*
Function signatures:
- cPluginManager.AddHook(HOOK_TYPE, CallbackFunction) -- (1) recommended
- cPluginManager:Get():AddHook(HOOK_TYPE, CallbackFunction) -- (2) accepted silently
- cPluginManager:Get():AddHook(Plugin, HOOK_TYPE) -- (3) old style (#121), accepted but complained about
- cPluginManager.AddHook(Plugin, HOOK_TYPE) -- (4) old style (#121) mangled, accepted but complained about
+ cPluginManager:AddHook(HOOK_TYPE, CallbackFunction) -- (1) recommended
+ cPluginManager.AddHook(HOOK_TYPE, CallbackFunction) -- (2) accepted silently (#401 deprecates this)
+ cPluginManager:Get():AddHook(HOOK_TYPE, CallbackFunction) -- (3) accepted silently
+ cPluginManager:Get():AddHook(Plugin, HOOK_TYPE) -- (4) old style (#121), accepted but complained about in the console
+ cPluginManager.AddHook(Plugin, HOOK_TYPE) -- (5) old style (#121) mangled, accepted but complained about in the console
*/
cLuaState S(tolua_S);
cPluginManager * PlgMgr = cPluginManager::Get();
- // If the first param is a cPluginManager, use it instead of the global one:
+ // If the first param is a cPluginManager instance, use it instead of the global one:
int ParamIdx = 1;
tolua_Error err;
if (tolua_isusertype(S, 1, "cPluginManager", 0, &err))
@@ -1161,6 +1161,11 @@ static int tolua_cPluginManager_AddHook(lua_State * tolua_S)
}
ParamIdx += 1;
}
+ else if (tolua_isusertable(S, 1, "cPluginManager", 0, &err))
+ {
+ // Style 1, use the global PlgMgr, but increment ParamIdx
+ ParamIdx++;
+ }
if (lua_isnumber(S, ParamIdx) && lua_isfunction(S, ParamIdx + 1))
{
@@ -1177,7 +1182,7 @@ static int tolua_cPluginManager_AddHook(lua_State * tolua_S)
AString ParamDesc;
Printf(ParamDesc, "%s, %s, %s", S.GetTypeText(1).c_str(), S.GetTypeText(2).c_str(), S.GetTypeText(3).c_str());
- LOGWARNING("cPluginManager.AddHook(): bad parameters. Expected HOOK_TYPE and CallbackFunction, got %s. Hook not added.", ParamDesc.c_str());
+ LOGWARNING("cPluginManager:AddHook(): bad parameters. Expected HOOK_TYPE and CallbackFunction, got %s. Hook not added.", ParamDesc.c_str());
S.LogStackTrace();
return 0;
}
@@ -1877,7 +1882,6 @@ static int tolua_cWebPlugin_GetTabNames(lua_State * tolua_S)
const cWebPlugin::TabNameList & TabNames = self->GetTabNames();
lua_newtable(tolua_S);
- int newTable = lua_gettop(tolua_S);
int index = 1;
cWebPlugin::TabNameList::const_iterator iter = TabNames.begin();
while(iter != TabNames.end())
@@ -1898,6 +1902,35 @@ static int tolua_cWebPlugin_GetTabNames(lua_State * tolua_S)
+static int tolua_cClientHandle_SendPluginMessage(lua_State * L)
+{
+ cLuaState S(L);
+ if (
+ !S.CheckParamUserType(1, "cClientHandle") ||
+ !S.CheckParamString(2, 3) ||
+ !S.CheckParamEnd(4)
+ )
+ {
+ return 0;
+ }
+ cClientHandle * Client = (cClientHandle *)tolua_tousertype(L, 1, NULL);
+ if (Client == NULL)
+ {
+ LOGWARNING("ClientHandle is nil in cClientHandle:SendPluginMessage()");
+ S.LogStackTrace();
+ return 0;
+ }
+ AString Channel, Message;
+ Channel.assign(lua_tostring(L, 2), lua_strlen(L, 2));
+ Message.assign(lua_tostring(L, 3), lua_strlen(L, 3));
+ Client->SendPluginMessage(Channel, Message);
+ return 0;
+}
+
+
+
+
+
static int Lua_ItemGrid_GetSlotCoords(lua_State * L)
{
tolua_Error tolua_err;
@@ -1947,118 +1980,72 @@ public:
virtual bool OnNextBlock(int a_BlockX, int a_BlockY, int a_BlockZ, BLOCKTYPE a_BlockType, NIBBLETYPE a_BlockMeta, char a_EntryFace) override
{
- if (!m_LuaState.PushFunctionFromRefTable(m_TableRef, "OnNextBlock"))
+ bool res = false;
+ if (!m_LuaState.Call(
+ cLuaState::cTableRef(m_TableRef, "OnNextBlock"),
+ a_BlockX, a_BlockY, a_BlockZ, a_BlockType, a_BlockMeta, a_EntryFace,
+ cLuaState::Return, res
+ ))
{
// No such function in the table, skip the callback
return false;
}
- m_LuaState.Push(a_BlockX);
- m_LuaState.Push(a_BlockY);
- m_LuaState.Push(a_BlockZ);
- m_LuaState.Push(a_BlockType);
- m_LuaState.Push(a_BlockMeta);
- m_LuaState.Push(a_EntryFace);
- if (!m_LuaState.CallFunction(1))
- {
- return false;
- }
- bool res = false;
- if (lua_isboolean(m_LuaState, -1))
- {
- res = (lua_toboolean(m_LuaState, -1) != 0);
- }
- lua_pop(m_LuaState, 1);
return res;
}
virtual bool OnNextBlockNoData(int a_BlockX, int a_BlockY, int a_BlockZ, char a_EntryFace) override
{
- if (!m_LuaState.PushFunctionFromRefTable(m_TableRef, "OnNextBlockNoData"))
+ bool res = false;
+ if (!m_LuaState.Call(
+ cLuaState::cTableRef(m_TableRef, "OnNextBlockNoData"),
+ a_BlockX, a_BlockY, a_BlockZ, a_EntryFace,
+ cLuaState::Return, res
+ ))
{
// No such function in the table, skip the callback
return false;
}
- m_LuaState.Push(a_BlockX);
- m_LuaState.Push(a_BlockY);
- m_LuaState.Push(a_BlockZ);
- m_LuaState.Push(a_EntryFace);
- if (!m_LuaState.CallFunction(1))
- {
- return false;
- }
- bool res = false;
- if (lua_isboolean(m_LuaState, -1))
- {
- res = (lua_toboolean(m_LuaState, -1) != 0);
- }
- lua_pop(m_LuaState, 1);
return res;
}
virtual bool OnOutOfWorld(double a_BlockX, double a_BlockY, double a_BlockZ) override
{
- if (!m_LuaState.PushFunctionFromRefTable(m_TableRef, "OnOutOfWorld"))
+ bool res = false;
+ if (!m_LuaState.Call(
+ cLuaState::cTableRef(m_TableRef, "OnOutOfWorld"),
+ a_BlockX, a_BlockY, a_BlockZ,
+ cLuaState::Return, res
+ ))
{
// No such function in the table, skip the callback
return false;
}
- m_LuaState.Push(a_BlockX);
- m_LuaState.Push(a_BlockY);
- m_LuaState.Push(a_BlockZ);
- if (!m_LuaState.CallFunction(1))
- {
- return false;
- }
- bool res = false;
- if (lua_isboolean(m_LuaState, -1))
- {
- res = (lua_toboolean(m_LuaState, -1) != 0);
- }
- lua_pop(m_LuaState, 1);
return res;
}
virtual bool OnIntoWorld(double a_BlockX, double a_BlockY, double a_BlockZ) override
{
- if (!m_LuaState.PushFunctionFromRefTable(m_TableRef, "OnIntoWorld"))
+ bool res = false;
+ if (!m_LuaState.Call(
+ cLuaState::cTableRef(m_TableRef, "OnIntoWorld"),
+ a_BlockX, a_BlockY, a_BlockZ,
+ cLuaState::Return, res
+ ))
{
// No such function in the table, skip the callback
return false;
}
- m_LuaState.Push(a_BlockX);
- m_LuaState.Push(a_BlockY);
- m_LuaState.Push(a_BlockZ);
- if (!m_LuaState.CallFunction(1))
- {
- return false;
- }
- bool res = false;
- if (lua_isboolean(m_LuaState, -1))
- {
- res = (lua_toboolean(m_LuaState, -1) != 0);
- }
- lua_pop(m_LuaState, 1);
return res;
}
virtual void OnNoMoreHits(void) override
{
- if (!m_LuaState.PushFunctionFromRefTable(m_TableRef, "OnNoMoreHits"))
- {
- // No such function in the table, skip the callback
- return;
- }
- m_LuaState.CallFunction(0);
+ m_LuaState.Call(cLuaState::cTableRef(m_TableRef, "OnNoMoreHits"));
}
virtual void OnNoChunk(void) override
{
- if (!m_LuaState.PushFunctionFromRefTable(m_TableRef, "OnNoChunk"))
- {
- // No such function in the table, skip the callback
- return;
- }
- m_LuaState.CallFunction(0);
+ m_LuaState.Call(cLuaState::cTableRef(m_TableRef, "OnNoChunk"));
}
protected:
@@ -2286,6 +2273,7 @@ void ManualBindings::Bind(lua_State * tolua_S)
tolua_beginmodule(tolua_S, "cClientHandle");
tolua_constant(tolua_S, "MAX_VIEW_DISTANCE", cClientHandle::MAX_VIEW_DISTANCE);
tolua_constant(tolua_S, "MIN_VIEW_DISTANCE", cClientHandle::MIN_VIEW_DISTANCE);
+ tolua_function(tolua_S, "SendPluginMessage", tolua_cClientHandle_SendPluginMessage);
tolua_endmodule(tolua_S);
tolua_beginmodule(tolua_S, "cItemGrid");
diff --git a/src/Bindings/Plugin.h b/src/Bindings/Plugin.h
index 9a3c2383e..f4a117721 100644
--- a/src/Bindings/Plugin.h
+++ b/src/Bindings/Plugin.h
@@ -68,6 +68,8 @@ public:
virtual bool OnPlayerBreakingBlock (cPlayer & a_Player, int a_BlockX, int a_BlockY, int a_BlockZ, char a_BlockFace, BLOCKTYPE a_BlockType, NIBBLETYPE a_BlockMeta) = 0;
virtual bool OnPlayerBrokenBlock (cPlayer & a_Player, int a_BlockX, int a_BlockY, int a_BlockZ, char a_BlockFace, BLOCKTYPE a_BlockType, NIBBLETYPE a_BlockMeta) = 0;
virtual bool OnPlayerEating (cPlayer & a_Player) = 0;
+ virtual bool OnPlayerFished (cPlayer & a_Player, const cItems & a_Reward) = 0;
+ virtual bool OnPlayerFishing (cPlayer & a_Player, cItems & a_Reward) = 0;
virtual bool OnPlayerJoined (cPlayer & a_Player) = 0;
virtual bool OnPlayerLeftClick (cPlayer & a_Player, int a_BlockX, int a_BlockY, int a_BlockZ, char a_BlockFace, char a_Status) = 0;
virtual bool OnPlayerMoved (cPlayer & a_Player) = 0;
@@ -82,6 +84,8 @@ public:
virtual bool OnPlayerUsedItem (cPlayer & a_Player, int a_BlockX, int a_BlockY, int a_BlockZ, char a_BlockFace, int a_CursorX, int a_CursorY, int a_CursorZ) = 0;
virtual bool OnPlayerUsingBlock (cPlayer & a_Player, int a_BlockX, int a_BlockY, int a_BlockZ, char a_BlockFace, int a_CursorX, int a_CursorY, int a_CursorZ, BLOCKTYPE a_BlockType, NIBBLETYPE a_BlockMeta) = 0;
virtual bool OnPlayerUsingItem (cPlayer & a_Player, int a_BlockX, int a_BlockY, int a_BlockZ, char a_BlockFace, int a_CursorX, int a_CursorY, int a_CursorZ) = 0;
+ virtual bool OnPluginMessage (cClientHandle & a_Client, const AString & a_Channel, const AString & a_Message) = 0;
+ virtual bool OnPluginsLoaded (void) = 0;
virtual bool OnPostCrafting (const cPlayer * a_Player, const cCraftingGrid * a_Grid, cCraftingRecipe * a_Recipe) = 0;
virtual bool OnPreCrafting (const cPlayer * a_Player, const cCraftingGrid * a_Grid, cCraftingRecipe * a_Recipe) = 0;
virtual bool OnSpawnedEntity (cWorld & a_World, cEntity & a_Entity) = 0;
diff --git a/src/Bindings/PluginLua.cpp b/src/Bindings/PluginLua.cpp
index 0e5a66cd6..eefcd2b09 100644
--- a/src/Bindings/PluginLua.cpp
+++ b/src/Bindings/PluginLua.cpp
@@ -90,6 +90,8 @@ bool cPluginLua::Initialize(void)
// Load all files for this plugin, and execute them
AStringVector Files = cFile::GetFolderContents(PluginPath.c_str());
+
+ int numFiles = 0;
for (AStringVector::const_iterator itr = Files.begin(); itr != Files.end(); ++itr)
{
if (itr->rfind(".lua") == AString::npos)
@@ -101,9 +103,20 @@ bool cPluginLua::Initialize(void)
{
Close();
return false;
+ }
+ else
+ {
+ numFiles++;
}
} // for itr - Files[]
+ if (numFiles == 0)
+ {
+ LOGWARNING("No lua files found: plugin %s is missing.", GetName().c_str());
+ Close();
+ return false;
+ }
+
// Call intialize function
bool res = false;
if (!m_LuaState.Call("Initialize", this, cLuaState::Return, res))
@@ -630,6 +643,46 @@ bool cPluginLua::OnPlayerEating(cPlayer & a_Player)
+bool cPluginLua::OnPlayerFished(cPlayer & a_Player, const cItems & a_Reward)
+{
+ cCSLock Lock(m_CriticalSection);
+ bool res = false;
+ cLuaRefs & Refs = m_HookMap[cPluginManager::HOOK_PLAYER_FISHED];
+ for (cLuaRefs::iterator itr = Refs.begin(), end = Refs.end(); itr != end; ++itr)
+ {
+ m_LuaState.Call((int)(**itr), &a_Player, a_Reward, cLuaState::Return, res);
+ if (res)
+ {
+ return true;
+ }
+ }
+ return false;
+}
+
+
+
+
+
+bool cPluginLua::OnPlayerFishing(cPlayer & a_Player, cItems & a_Reward)
+{
+ cCSLock Lock(m_CriticalSection);
+ bool res = false;
+ cLuaRefs & Refs = m_HookMap[cPluginManager::HOOK_PLAYER_FISHING];
+ for (cLuaRefs::iterator itr = Refs.begin(), end = Refs.end(); itr != end; ++itr)
+ {
+ m_LuaState.Call((int)(**itr), &a_Player, a_Reward, cLuaState::Return, res);
+ if (res)
+ {
+ return true;
+ }
+ }
+ return false;
+}
+
+
+
+
+
bool cPluginLua::OnPlayerJoined(cPlayer & a_Player)
{
cCSLock Lock(m_CriticalSection);
@@ -910,6 +963,44 @@ bool cPluginLua::OnPlayerUsingItem(cPlayer & a_Player, int a_BlockX, int a_Block
+bool cPluginLua::OnPluginMessage(cClientHandle & a_Client, const AString & a_Channel, const AString & a_Message)
+{
+ cCSLock Lock(m_CriticalSection);
+ bool res = false;
+ cLuaRefs & Refs = m_HookMap[cPluginManager::HOOK_PLUGIN_MESSAGE];
+ for (cLuaRefs::iterator itr = Refs.begin(), end = Refs.end(); itr != end; ++itr)
+ {
+ m_LuaState.Call((int)(**itr), &a_Client, a_Channel, a_Message);
+ if (res)
+ {
+ return true;
+ }
+ }
+ return false;
+}
+
+
+
+
+
+bool cPluginLua::OnPluginsLoaded(void)
+{
+ cCSLock Lock(m_CriticalSection);
+ bool res = false;
+ cLuaRefs & Refs = m_HookMap[cPluginManager::HOOK_PLUGINS_LOADED];
+ for (cLuaRefs::iterator itr = Refs.begin(), end = Refs.end(); itr != end; ++itr)
+ {
+ bool ret = false;
+ m_LuaState.Call((int)(**itr), cLuaState::Return, ret);
+ res = res || ret;
+ }
+ return res;
+}
+
+
+
+
+
bool cPluginLua::OnPostCrafting(const cPlayer * a_Player, const cCraftingGrid * a_Grid, cCraftingRecipe * a_Recipe)
{
cCSLock Lock(m_CriticalSection);
@@ -1324,6 +1415,8 @@ const char * cPluginLua::GetHookFnName(int a_HookType)
case cPluginManager::HOOK_PLAYER_USED_ITEM: return "OnPlayerUsedItem";
case cPluginManager::HOOK_PLAYER_USING_BLOCK: return "OnPlayerUsingBlock";
case cPluginManager::HOOK_PLAYER_USING_ITEM: return "OnPlayerUsingItem";
+ case cPluginManager::HOOK_PLUGIN_MESSAGE: return "OnPluginMessage";
+ case cPluginManager::HOOK_PLUGINS_LOADED: return "OnPluginsLoaded";
case cPluginManager::HOOK_POST_CRAFTING: return "OnPostCrafting";
case cPluginManager::HOOK_PRE_CRAFTING: return "OnPreCrafting";
case cPluginManager::HOOK_SPAWNED_ENTITY: return "OnSpawnedEntity";
@@ -1337,8 +1430,17 @@ const char * cPluginLua::GetHookFnName(int a_HookType)
case cPluginManager::HOOK_WEATHER_CHANGED: return "OnWeatherChanged";
case cPluginManager::HOOK_WEATHER_CHANGING: return "OnWeatherChanging";
case cPluginManager::HOOK_WORLD_TICK: return "OnWorldTick";
- default: return NULL;
+
+ case cPluginManager::HOOK_NUM_HOOKS:
+ {
+ // Satisfy a warning that all enum values should be used in a switch
+ // but don't want a default branch, so that we catch new hooks missing from this list.
+ break;
+ }
} // switch (a_Hook)
+ LOGWARNING("Requested name of an unknown hook type function: %d (max is %d)", a_HookType, cPluginManager::HOOK_MAX);
+ ASSERT(!"Unknown hook requested!");
+ return NULL;
}
diff --git a/src/Bindings/PluginLua.h b/src/Bindings/PluginLua.h
index e1e274c72..c01f5ca89 100644
--- a/src/Bindings/PluginLua.h
+++ b/src/Bindings/PluginLua.h
@@ -65,6 +65,8 @@ public:
virtual bool OnPlayerBreakingBlock (cPlayer & a_Player, int a_BlockX, int a_BlockY, int a_BlockZ, char a_BlockFace, BLOCKTYPE a_BlockType, NIBBLETYPE a_BlockMeta) override;
virtual bool OnPlayerBrokenBlock (cPlayer & a_Player, int a_BlockX, int a_BlockY, int a_BlockZ, char a_BlockFace, BLOCKTYPE a_BlockType, NIBBLETYPE a_BlockMeta) override;
virtual bool OnPlayerEating (cPlayer & a_Player) override;
+ virtual bool OnPlayerFished (cPlayer & a_Player, const cItems & a_Reward) override;
+ virtual bool OnPlayerFishing (cPlayer & a_Player, cItems & a_Reward) override;
virtual bool OnPlayerJoined (cPlayer & a_Player) override;
virtual bool OnPlayerMoved (cPlayer & a_Player) override;
virtual bool OnPlayerLeftClick (cPlayer & a_Player, int a_BlockX, int a_BlockY, int a_BlockZ, char a_BlockFace, char a_Status) override;
@@ -79,6 +81,8 @@ public:
virtual bool OnPlayerUsedItem (cPlayer & a_Player, int a_BlockX, int a_BlockY, int a_BlockZ, char a_BlockFace, int a_CursorX, int a_CursorY, int a_CursorZ) override;
virtual bool OnPlayerUsingBlock (cPlayer & a_Player, int a_BlockX, int a_BlockY, int a_BlockZ, char a_BlockFace, int a_CursorX, int a_CursorY, int a_CursorZ, BLOCKTYPE a_BlockType, NIBBLETYPE a_BlockMeta) override;
virtual bool OnPlayerUsingItem (cPlayer & a_Player, int a_BlockX, int a_BlockY, int a_BlockZ, char a_BlockFace, int a_CursorX, int a_CursorY, int a_CursorZ) override;
+ virtual bool OnPluginMessage (cClientHandle & a_Client, const AString & a_Channel, const AString & a_Message) override;
+ virtual bool OnPluginsLoaded (void) override;
virtual bool OnPostCrafting (const cPlayer * a_Player, const cCraftingGrid * a_Grid, cCraftingRecipe * a_Recipe) override;
virtual bool OnPreCrafting (const cPlayer * a_Player, const cCraftingGrid * a_Grid, cCraftingRecipe * a_Recipe) override;
virtual bool OnSpawnedEntity (cWorld & a_World, cEntity & a_Entity) override;
diff --git a/src/Bindings/PluginManager.cpp b/src/Bindings/PluginManager.cpp
index 832dc4249..68e6aea33 100644
--- a/src/Bindings/PluginManager.cpp
+++ b/src/Bindings/PluginManager.cpp
@@ -118,7 +118,7 @@ void cPluginManager::ReloadPluginsNow(cIniFile & a_SettingsIni)
int KeyNum = a_SettingsIni.FindKey("Plugins");
// If it does, how many plugins are there?
- unsigned int NumPlugins = ((KeyNum != -1) ? (a_SettingsIni.GetNumValues(KeyNum)) : 0);
+ int NumPlugins = ((KeyNum != -1) ? (a_SettingsIni.GetNumValues(KeyNum)) : 0);
if (KeyNum == -1)
{
@@ -126,7 +126,7 @@ void cPluginManager::ReloadPluginsNow(cIniFile & a_SettingsIni)
}
else if (NumPlugins > 0)
{
- for(unsigned int i = 0; i < NumPlugins; i++)
+ for (int i = 0; i < NumPlugins; i++)
{
AString ValueName = a_SettingsIni.GetValueName(KeyNum, i);
if (ValueName.compare("Plugin") == 0)
@@ -136,7 +136,7 @@ void cPluginManager::ReloadPluginsNow(cIniFile & a_SettingsIni)
{
if (m_Plugins.find(PluginFile) != m_Plugins.end())
{
- LoadPlugin( PluginFile );
+ LoadPlugin(PluginFile);
}
}
}
@@ -155,6 +155,7 @@ void cPluginManager::ReloadPluginsNow(cIniFile & a_SettingsIni)
{
LOG("-- Loaded 1 Plugin --");
}
+ CallHookPluginsLoaded();
}
@@ -693,6 +694,48 @@ bool cPluginManager::CallHookPlayerEating(cPlayer & a_Player)
+bool cPluginManager::CallHookPlayerFished(cPlayer & a_Player, const cItems a_Reward)
+{
+ HookMap::iterator Plugins = m_Hooks.find(HOOK_PLAYER_FISHED);
+ if (Plugins == m_Hooks.end())
+ {
+ return false;
+ }
+ for (PluginList::iterator itr = Plugins->second.begin(); itr != Plugins->second.end(); ++itr)
+ {
+ if ((*itr)->OnPlayerFished(a_Player, a_Reward))
+ {
+ return true;
+ }
+ }
+ return false;
+}
+
+
+
+
+
+bool cPluginManager::CallHookPlayerFishing(cPlayer & a_Player, cItems a_Reward)
+{
+ HookMap::iterator Plugins = m_Hooks.find(HOOK_PLAYER_FISHING);
+ if (Plugins == m_Hooks.end())
+ {
+ return false;
+ }
+ for (PluginList::iterator itr = Plugins->second.begin(); itr != Plugins->second.end(); ++itr)
+ {
+ if ((*itr)->OnPlayerFishing(a_Player, a_Reward))
+ {
+ return true;
+ }
+ }
+ return false;
+}
+
+
+
+
+
bool cPluginManager::CallHookPlayerJoined(cPlayer & a_Player)
{
HookMap::iterator Plugins = m_Hooks.find(HOOK_PLAYER_JOINED);
@@ -987,6 +1030,46 @@ bool cPluginManager::CallHookPlayerUsingItem(cPlayer & a_Player, int a_BlockX, i
+bool cPluginManager::CallHookPluginMessage(cClientHandle & a_Client, const AString & a_Channel, const AString & a_Message)
+{
+ HookMap::iterator Plugins = m_Hooks.find(HOOK_PLUGIN_MESSAGE);
+ if (Plugins == m_Hooks.end())
+ {
+ return false;
+ }
+ for (PluginList::iterator itr = Plugins->second.begin(); itr != Plugins->second.end(); ++itr)
+ {
+ if ((*itr)->OnPluginMessage(a_Client, a_Channel, a_Message))
+ {
+ return true;
+ }
+ }
+ return false;
+}
+
+
+
+
+
+bool cPluginManager::CallHookPluginsLoaded(void)
+{
+ HookMap::iterator Plugins = m_Hooks.find(HOOK_PLUGINS_LOADED);
+ if (Plugins == m_Hooks.end())
+ {
+ return false;
+ }
+ bool res = false;
+ for (PluginList::iterator itr = Plugins->second.begin(); itr != Plugins->second.end(); ++itr)
+ {
+ res = !(*itr)->OnPluginsLoaded() || res;
+ }
+ return res;
+}
+
+
+
+
+
bool cPluginManager::CallHookPostCrafting(const cPlayer * a_Player, const cCraftingGrid * a_Grid, cCraftingRecipe * a_Recipe)
{
HookMap::iterator Plugins = m_Hooks.find(HOOK_POST_CRAFTING);
diff --git a/src/Bindings/PluginManager.h b/src/Bindings/PluginManager.h
index 04d6470c7..9936f5a35 100644
--- a/src/Bindings/PluginManager.h
+++ b/src/Bindings/PluginManager.h
@@ -80,6 +80,8 @@ public: // tolua_export
HOOK_PLAYER_BREAKING_BLOCK,
HOOK_PLAYER_BROKEN_BLOCK,
HOOK_PLAYER_EATING,
+ HOOK_PLAYER_FISHED,
+ HOOK_PLAYER_FISHING,
HOOK_PLAYER_JOINED,
HOOK_PLAYER_LEFT_CLICK,
HOOK_PLAYER_MOVING,
@@ -94,6 +96,8 @@ public: // tolua_export
HOOK_PLAYER_USED_ITEM,
HOOK_PLAYER_USING_BLOCK,
HOOK_PLAYER_USING_ITEM,
+ HOOK_PLUGIN_MESSAGE,
+ HOOK_PLUGINS_LOADED,
HOOK_POST_CRAFTING,
HOOK_PRE_CRAFTING,
HOOK_SPAWNED_ENTITY,
@@ -167,6 +171,8 @@ public: // tolua_export
bool CallHookPlayerBreakingBlock (cPlayer & a_Player, int a_BlockX, int a_BlockY, int a_BlockZ, char a_BlockFace, BLOCKTYPE a_BlockType, NIBBLETYPE a_BlockMeta);
bool CallHookPlayerBrokenBlock (cPlayer & a_Player, int a_BlockX, int a_BlockY, int a_BlockZ, char a_BlockFace, BLOCKTYPE a_BlockType, NIBBLETYPE a_BlockMeta);
bool CallHookPlayerEating (cPlayer & a_Player);
+ bool CallHookPlayerFished (cPlayer & a_Player, const cItems a_Reward);
+ bool CallHookPlayerFishing (cPlayer & a_Player, cItems a_Reward);
bool CallHookPlayerJoined (cPlayer & a_Player);
bool CallHookPlayerMoving (cPlayer & a_Player);
bool CallHookPlayerLeftClick (cPlayer & a_Player, int a_BlockX, int a_BlockY, int a_BlockZ, char a_BlockFace, char a_Status);
@@ -181,6 +187,8 @@ public: // tolua_export
bool CallHookPlayerUsedItem (cPlayer & a_Player, int a_BlockX, int a_BlockY, int a_BlockZ, char a_BlockFace, int a_CursorX, int a_CursorY, int a_CursorZ);
bool CallHookPlayerUsingBlock (cPlayer & a_Player, int a_BlockX, int a_BlockY, int a_BlockZ, char a_BlockFace, int a_CursorX, int a_CursorY, int a_CursorZ, BLOCKTYPE a_BlockType, NIBBLETYPE a_BlockMeta);
bool CallHookPlayerUsingItem (cPlayer & a_Player, int a_BlockX, int a_BlockY, int a_BlockZ, char a_BlockFace, int a_CursorX, int a_CursorY, int a_CursorZ);
+ bool CallHookPluginMessage (cClientHandle & a_Client, const AString & a_Channel, const AString & a_Message);
+ bool CallHookPluginsLoaded (void);
bool CallHookPostCrafting (const cPlayer * a_Player, const cCraftingGrid * a_Grid, cCraftingRecipe * a_Recipe);
bool CallHookPreCrafting (const cPlayer * a_Player, const cCraftingGrid * a_Grid, cCraftingRecipe * a_Recipe);
bool CallHookSpawnedEntity (cWorld & a_World, cEntity & a_Entity);
@@ -271,7 +279,7 @@ private:
bool m_bReloadPlugins;
cPluginManager();
- ~cPluginManager();
+ virtual ~cPluginManager();
/// Reloads all plugins, defaulting to settings.ini for settings location
void ReloadPluginsNow(void);
diff --git a/src/Bindings/tolua++.h b/src/Bindings/tolua++.h
index 4dfd06318..73e65b746 100644
--- a/src/Bindings/tolua++.h
+++ b/src/Bindings/tolua++.h
@@ -2,12 +2,18 @@
// tolua++.h
// Redirection file, needed because ToLua++ generates the Bindings.cpp file with >> #include "tolua++.h" <<
+// Only used from Bindings.cpp
#include "tolua++/include/tolua++.h"
+#ifdef _MSC_VER
+ // Disable specific warnings for the generated Bindings.cpp file:
+ #pragma warning(disable: 4800) // 'int' : forcing value to bool 'true' or 'false' (performance warning)
+#endif // _MSC_VER
+
diff --git a/src/Bindings/tolua_base.h b/src/Bindings/tolua_base.h
deleted file mode 100644
index 6a76f97b1..000000000
--- a/src/Bindings/tolua_base.h
+++ /dev/null
@@ -1,128 +0,0 @@
-#ifndef TOLUA_BASE_H
-#define TOLUA_BASE_H
-
-#pragma warning(disable:4800) // This file is ONLY included by Bindings.cpp and it throws lots of C4800 warnings
-
-#include "tolua++/include/tolua++.h"
-
-
-
-
-
-class ToluaBase {
-
- int lua_instance;
-
-protected:
-
- lua_State* lua_state;
-
- void lua_stacktrace(lua_State* L) const
- {
- lua_Debug entry;
- int depth = 0;
-
- while (lua_getstack(L, depth, &entry))
- {
- lua_getinfo(L, "Sln", &entry);
-
- LOGERROR("%s(%d): %s", entry.short_src, entry.currentline, entry.name ? entry.name : "?");
- depth++;
- }
- }
-
-
- bool report_errors(int status) const
- {
- if ( status!=0 )
- {
- const char* s = lua_tostring(lua_state, -1);
- LOGERROR("-- %s", s );
- //lua_pop(lua_state, 1);
- LOGERROR("Stack:");
- lua_stacktrace( lua_state );
- return true;
- }
- return false;
- }
-
- bool push_method(const char* name, lua_CFunction f) const {
-
- if (!lua_state) return false;
-
- lua_getref(lua_state, lua_instance);
- lua_pushstring(lua_state, name);
- //LOGINFO("1. push_method() Stack size: %i", lua_gettop( lua_state ) );
- lua_gettable(lua_state, -2);
- //LOGINFO("2. push_method() Stack size: %i", lua_gettop( lua_state ) );
-
- if (lua_isnil(lua_state, -1)) {
-
- // pop the table
- lua_pop(lua_state, 2);
- return false;
-
- } else {
-
- if (f) {
- if (lua_iscfunction(lua_state, -1)) {
- lua_pop(lua_state, 2);
- return false;
- };
- /* // not for now
- lua_pushcfunction(lua_state, f);
- if (lua_rawequal(lua_state, -1, -2)) {
-
- // avoid recursion, pop both functions and the table
- lua_pop(lua_state, 3);
- return false;
- };
-
- // pop f
- lua_pop(lua_state, 1);
- */
- };
-
- // swap table with function
- lua_insert(lua_state, -2);
- };
-
- return true;
- };
-
- void dbcall(lua_State* L, int nargs, int nresults) const {
-
- // using lua_call for now
- int s = lua_pcall(L, nargs, nresults, 0);
- report_errors( s );
- };
-public:
-
- int GetInstance() { return lua_instance; }
- lua_State* GetLuaState() { return lua_state; }
-
- void tolua__set_instance(lua_State* L, lua_Object lo) {
-
- lua_state = L;
-
- lua_pushvalue(L, lo);
- lua_instance = lua_ref(lua_state, 1);
- };
-
- ToluaBase() {
-
- lua_state = NULL;
- };
-
- ~ToluaBase() {
-
- if (lua_state) {
-
- lua_unref(lua_state, lua_instance);
- };
- };
-};
-
-#endif
-
-