summaryrefslogtreecommitdiffstats
path: root/source
diff options
context:
space:
mode:
Diffstat (limited to '')
-rw-r--r--source/ManualBindings.cpp66
-rw-r--r--source/cWorld.cpp20
-rw-r--r--source/cWorld.h9
3 files changed, 64 insertions, 31 deletions
diff --git a/source/ManualBindings.cpp b/source/ManualBindings.cpp
index ba844b702..840109a8f 100644
--- a/source/ManualBindings.cpp
+++ b/source/ManualBindings.cpp
@@ -16,7 +16,17 @@
#include "md5/md5.h"
-
+static bool report_errors(lua_State* lua, int status)
+{
+ if ( status!=0 )
+ {
+ std::string s = lua_tostring(lua, -1);
+ LOGERROR("-- %s", s.c_str() );
+ lua_pop(lua, 1);
+ return true;
+ }
+ return false;
+}
/****************************
@@ -91,13 +101,59 @@ static int tolua_LOGERROR(lua_State* tolua_S)
-static int tolua_cWorld_GetAllPlayers(lua_State* tolua_S)
+static int tolua_cWorld_ForEachPlayer(lua_State* tolua_S)
{
cWorld* self = (cWorld*) tolua_tousertype(tolua_S,1,0);
- lua_State* L = tolua_S;
- self->GetAllPlayers(L);
+ if( !lua_isfunction( tolua_S, 2 ) )
+ {
+ LOGWARN("Error in function call 'ForEachPlayer': Expected a function for parameter #1");
+ return 0;
+ }
+
+
+ int Reference = luaL_ref(tolua_S, LUA_REGISTRYINDEX);
+ if( Reference == LUA_REFNIL )
+ {
+ LOGWARN("Error in function call 'ForEachPlayer': Could not get function reference");
+ return 0;
+ }
+
+ class cLuaPlayerCallback : public cPlayerListCallback
+ {
+ virtual bool Item(cPlayer * a_Player) override
+ {
+ lua_rawgeti( LuaState, LUA_REGISTRYINDEX, Reference);
+ tolua_pushusertype( LuaState, a_Player, "cPlayer" );
+
+ int s = lua_pcall( LuaState, 1, 1, 0);
+ if( report_errors( LuaState, s ) )
+ {
+ return false;
+ }
+
+ if( lua_isboolean( LuaState, -1 ) )
+ {
+ return (tolua_toboolean( LuaState, -1, 0) > 0);
+ }
+
+ LOGINFO("Stack size: %i", lua_gettop(LuaState) );
+
+ return false;
+ }
+ public:
+ lua_State* LuaState;
+ int Reference;
+ } Callback;
+
+ Callback.LuaState = tolua_S;
+ Callback.Reference = Reference;
+
+ bool bRetVal = self->ForEachPlayer( &Callback );
+
+ luaL_unref( tolua_S, LUA_REGISTRYINDEX, Reference );
+ tolua_pushboolean( tolua_S, bRetVal );
return 1;
}
@@ -397,7 +453,7 @@ void ManualBindings::Bind( lua_State* tolua_S )
tolua_function(tolua_S,"Log",tolua_LOG); // Deprecated
tolua_beginmodule(tolua_S,"cWorld");
- tolua_function(tolua_S,"GetAllPlayers",tolua_cWorld_GetAllPlayers);
+ tolua_function(tolua_S,"ForEachPlayer",tolua_cWorld_ForEachPlayer);
tolua_endmodule(tolua_S);
tolua_beginmodule(tolua_S,"cPlugin");
tolua_function(tolua_S,"GetCommands",tolua_cPlugin_GetCommands);
diff --git a/source/cWorld.cpp b/source/cWorld.cpp
index c007070c6..737751901 100644
--- a/source/cWorld.cpp
+++ b/source/cWorld.cpp
@@ -989,26 +989,6 @@ bool cWorld::ForEachPlayer(cPlayerListCallback * a_Callback)
-
-void cWorld::GetAllPlayers( lua_State* L )
-{
- lua_createtable(L, m_Players.size(), 0);
- int newTable = lua_gettop(L);
- int index = 1;
- cPlayerList::const_iterator iter = m_Players.begin();
- while(iter != m_Players.end())
- {
- tolua_pushusertype( L, (*iter), "cPlayer" );
- lua_rawseti(L, newTable, index);
- ++iter;
- ++index;
- }
-}
-
-
-
-
-
// TODO: This interface is dangerous!
cPlayer* cWorld::GetPlayer( const char* a_PlayerName )
{
diff --git a/source/cWorld.h b/source/cWorld.h
index b895a8a9b..146724262 100644
--- a/source/cWorld.h
+++ b/source/cWorld.h
@@ -76,13 +76,10 @@ public:
void AddPlayer( cPlayer* a_Player );
void RemovePlayer( cPlayer* a_Player );
- bool ForEachPlayer(cPlayerListCallback * a_Callback); // Calls the callback for each player in the list
-
- // TODO: This interface is dangerous!
- cPlayerList & GetAllPlayers() {return m_Players; }
-
+
typedef struct lua_State lua_State;
- void GetAllPlayers( lua_State* L ); // >> EXPORTED IN MANUALBINDINGS <<
+ bool ForEachPlayer(cPlayerListCallback * a_Callback); // Calls the callback for each player in the list
+ // >> EXPORTED IN MANUALBINDINGS <<
unsigned int GetNumPlayers(); //tolua_export
// TODO: This interface is dangerous