1
0

Added Lua function cRoot:ForEachWorld(), removed the obsolete cRoot:GetWorld() method (both C++ and Lua)

git-svn-id: http://mc-server.googlecode.com/svn/trunk@534 0a769ca7-a7f5-676a-18bf-c427514a06d6
This commit is contained in:
madmaxoft@gmail.com 2012-06-02 09:38:51 +00:00
parent 43c9546203
commit 1d6c55afcc
6 changed files with 137 additions and 64 deletions

View File

@ -1,6 +1,6 @@
/* /*
** Lua binding: AllToLua ** Lua binding: AllToLua
** Generated automatically by tolua++-1.0.92 on 05/31/12 22:03:38. ** Generated automatically by tolua++-1.0.92 on 06/02/12 11:29:17.
*/ */
#ifndef __cplusplus #ifndef __cplusplus
@ -12888,38 +12888,6 @@ static int tolua_AllToLua_cRoot_GetServer00(lua_State* tolua_S)
} }
#endif //#ifndef TOLUA_DISABLE #endif //#ifndef TOLUA_DISABLE
/* method: GetWorld of class cRoot */
#ifndef TOLUA_DISABLE_tolua_AllToLua_cRoot_GetWorld00
static int tolua_AllToLua_cRoot_GetWorld00(lua_State* tolua_S)
{
#ifndef TOLUA_RELEASE
tolua_Error tolua_err;
if (
!tolua_isusertype(tolua_S,1,"cRoot",0,&tolua_err) ||
!tolua_isnoobj(tolua_S,2,&tolua_err)
)
goto tolua_lerror;
else
#endif
{
cRoot* self = (cRoot*) tolua_tousertype(tolua_S,1,0);
#ifndef TOLUA_RELEASE
if (!self) tolua_error(tolua_S,"invalid 'self' in function 'GetWorld'", NULL);
#endif
{
OBSOLETE cWorld* tolua_ret = (OBSOLETE cWorld*) self->GetWorld();
tolua_pushusertype(tolua_S,(void*)tolua_ret,"cWorld");
}
}
return 1;
#ifndef TOLUA_RELEASE
tolua_lerror:
tolua_error(tolua_S,"#ferror in function 'GetWorld'.",&tolua_err);
return 0;
#endif
}
#endif //#ifndef TOLUA_DISABLE
/* method: GetDefaultWorld of class cRoot */ /* method: GetDefaultWorld of class cRoot */
#ifndef TOLUA_DISABLE_tolua_AllToLua_cRoot_GetDefaultWorld00 #ifndef TOLUA_DISABLE_tolua_AllToLua_cRoot_GetDefaultWorld00
static int tolua_AllToLua_cRoot_GetDefaultWorld00(lua_State* tolua_S) static int tolua_AllToLua_cRoot_GetDefaultWorld00(lua_State* tolua_S)
@ -12953,9 +12921,10 @@ static int tolua_AllToLua_cRoot_GetDefaultWorld00(lua_State* tolua_S)
#endif //#ifndef TOLUA_DISABLE #endif //#ifndef TOLUA_DISABLE
/* method: GetWorld of class cRoot */ /* method: GetWorld of class cRoot */
#ifndef TOLUA_DISABLE_tolua_AllToLua_cRoot_GetWorld01 #ifndef TOLUA_DISABLE_tolua_AllToLua_cRoot_GetWorld00
static int tolua_AllToLua_cRoot_GetWorld01(lua_State* tolua_S) static int tolua_AllToLua_cRoot_GetWorld00(lua_State* tolua_S)
{ {
#ifndef TOLUA_RELEASE
tolua_Error tolua_err; tolua_Error tolua_err;
if ( if (
!tolua_isusertype(tolua_S,1,"cRoot",0,&tolua_err) || !tolua_isusertype(tolua_S,1,"cRoot",0,&tolua_err) ||
@ -12964,6 +12933,7 @@ static int tolua_AllToLua_cRoot_GetWorld01(lua_State* tolua_S)
) )
goto tolua_lerror; goto tolua_lerror;
else else
#endif
{ {
cRoot* self = (cRoot*) tolua_tousertype(tolua_S,1,0); cRoot* self = (cRoot*) tolua_tousertype(tolua_S,1,0);
const AString a_WorldName = ((const AString) tolua_tocppstring(tolua_S,2,0)); const AString a_WorldName = ((const AString) tolua_tocppstring(tolua_S,2,0));
@ -12977,8 +12947,11 @@ static int tolua_AllToLua_cRoot_GetWorld01(lua_State* tolua_S)
} }
} }
return 2; return 2;
tolua_lerror: #ifndef TOLUA_RELEASE
return tolua_AllToLua_cRoot_GetWorld00(tolua_S); tolua_lerror:
tolua_error(tolua_S,"#ferror in function 'GetWorld'.",&tolua_err);
return 0;
#endif
} }
#endif //#ifndef TOLUA_DISABLE #endif //#ifndef TOLUA_DISABLE
@ -18563,9 +18536,8 @@ TOLUA_API int tolua_AllToLua_open (lua_State* tolua_S)
tolua_beginmodule(tolua_S,"cRoot"); tolua_beginmodule(tolua_S,"cRoot");
tolua_function(tolua_S,"Get",tolua_AllToLua_cRoot_Get00); tolua_function(tolua_S,"Get",tolua_AllToLua_cRoot_Get00);
tolua_function(tolua_S,"GetServer",tolua_AllToLua_cRoot_GetServer00); tolua_function(tolua_S,"GetServer",tolua_AllToLua_cRoot_GetServer00);
tolua_function(tolua_S,"GetWorld",tolua_AllToLua_cRoot_GetWorld00);
tolua_function(tolua_S,"GetDefaultWorld",tolua_AllToLua_cRoot_GetDefaultWorld00); tolua_function(tolua_S,"GetDefaultWorld",tolua_AllToLua_cRoot_GetDefaultWorld00);
tolua_function(tolua_S,"GetWorld",tolua_AllToLua_cRoot_GetWorld01); tolua_function(tolua_S,"GetWorld",tolua_AllToLua_cRoot_GetWorld00);
tolua_function(tolua_S,"GetGroupManager",tolua_AllToLua_cRoot_GetGroupManager00); tolua_function(tolua_S,"GetGroupManager",tolua_AllToLua_cRoot_GetGroupManager00);
tolua_function(tolua_S,"GetRecipeChecker",tolua_AllToLua_cRoot_GetRecipeChecker00); tolua_function(tolua_S,"GetRecipeChecker",tolua_AllToLua_cRoot_GetRecipeChecker00);
tolua_function(tolua_S,"GetFurnaceRecipe",tolua_AllToLua_cRoot_GetFurnaceRecipe00); tolua_function(tolua_S,"GetFurnaceRecipe",tolua_AllToLua_cRoot_GetFurnaceRecipe00);

View File

@ -1,6 +1,6 @@
/* /*
** Lua binding: AllToLua ** Lua binding: AllToLua
** Generated automatically by tolua++-1.0.92 on 05/31/12 22:03:38. ** Generated automatically by tolua++-1.0.92 on 06/02/12 11:29:17.
*/ */
/* Exported function */ /* Exported function */

View File

@ -102,6 +102,94 @@ static int tolua_LOGERROR(lua_State* tolua_S)
static int tolua_cRoot_ForEachWorld(lua_State * tolua_S)
{
int NumArgs = lua_gettop( tolua_S ) - 1; // This includes 'self'
if ((NumArgs != 1) && (NumArgs != 2))
{
LOGWARN("Error in function call 'ForEachWorld': Requires 1 or 2 arguments, got %i", NumArgs);
return 0;
}
cRoot * self = (cRoot *)tolua_tousertype(tolua_S, 1, 0);
if (!lua_isfunction(tolua_S, 2))
{
LOGWARN("Error in function call 'ForEachWorld': Expected a function for parameter #1");
return 0;
}
// luaL_ref gets reference to value on top of the stack, the table is the last argument and therefore on the top
int TableRef = LUA_REFNIL;
if( NumArgs == 2 )
{
TableRef = luaL_ref(tolua_S, LUA_REGISTRYINDEX);
if( TableRef == LUA_REFNIL )
{
LOGWARN("Error in function call 'ForEachWorld': Could not get value reference of parameter #2");
return 0;
}
}
// table value is popped, and now function is on top of the stack
int FuncRef = luaL_ref(tolua_S, LUA_REGISTRYINDEX);
if( FuncRef == LUA_REFNIL )
{
LOGWARN("Error in function call 'ForEachWorld': Could not get function reference of parameter #1");
return 0;
}
class cLuaWorldCallback : public cWorldListCallback
{
public:
cLuaWorldCallback( lua_State* a_LuaState, int a_FuncRef, int a_TableRef )
: LuaState( a_LuaState )
, FuncRef( a_FuncRef )
, TableRef( a_TableRef )
{}
private:
virtual bool Item(cWorld * a_World) override
{
lua_rawgeti( LuaState, LUA_REGISTRYINDEX, FuncRef); // Push function reference
tolua_pushusertype( LuaState, a_World, "cWorld" );
if (TableRef != LUA_REFNIL)
{
lua_rawgeti(LuaState, LUA_REGISTRYINDEX, TableRef); // Push table reference
}
int s = lua_pcall( LuaState, (TableRef == LUA_REFNIL ? 1 : 2), 1, 0);
if (report_errors(LuaState, s))
{
return true; // break enumeration
}
if (lua_isboolean( LuaState, -1))
{
return (tolua_toboolean( LuaState, -1, 0) > 0);
}
return false; // continue enumeration
}
lua_State* LuaState;
int FuncRef;
int TableRef;
} Callback( tolua_S, FuncRef, TableRef );
bool bRetVal = self->ForEachWorld(Callback);
// Unreference the values again, so the LUA_REGISTRYINDEX can make place for other references
luaL_unref( tolua_S, LUA_REGISTRYINDEX, TableRef );
luaL_unref( tolua_S, LUA_REGISTRYINDEX, FuncRef );
// Push return value on stack
tolua_pushboolean( tolua_S, bRetVal );
return 1;
}
static int tolua_cWorld_ForEachPlayer(lua_State* tolua_S) static int tolua_cWorld_ForEachPlayer(lua_State* tolua_S)
{ {
int NumArgs = lua_gettop( tolua_S )-1; // This includes 'self' int NumArgs = lua_gettop( tolua_S )-1; // This includes 'self'
@ -161,16 +249,16 @@ static int tolua_cWorld_ForEachPlayer(lua_State* tolua_S)
int s = lua_pcall( LuaState, (TableRef==LUA_REFNIL?1:2), 1, 0); int s = lua_pcall( LuaState, (TableRef==LUA_REFNIL?1:2), 1, 0);
if( report_errors( LuaState, s ) ) if( report_errors( LuaState, s ) )
{ {
return false; // Maybe we should return true? return true; // Abort enumeration
} }
if( lua_isboolean( LuaState, -1 ) ) if( lua_isboolean( LuaState, -1 ) )
{ {
return (tolua_toboolean( LuaState, -1, 0) > 0); return (tolua_toboolean( LuaState, -1, 0) > 0);
} }
return false; return false; // Continue enumeration
} }
lua_State* LuaState; lua_State * LuaState;
int FuncRef; int FuncRef;
int TableRef; int TableRef;
} Callback( tolua_S, FuncRef, TableRef ); } Callback( tolua_S, FuncRef, TableRef );
@ -481,6 +569,9 @@ void ManualBindings::Bind( lua_State* tolua_S )
tolua_function(tolua_S,"LOGERROR",tolua_LOGERROR); tolua_function(tolua_S,"LOGERROR",tolua_LOGERROR);
tolua_function(tolua_S,"Log",tolua_LOG); // Deprecated tolua_function(tolua_S,"Log",tolua_LOG); // Deprecated
tolua_beginmodule(tolua_S,"cRoot");
tolua_function(tolua_S,"ForEachWorld",tolua_cRoot_ForEachWorld);
tolua_endmodule(tolua_S);
tolua_beginmodule(tolua_S,"cWorld"); tolua_beginmodule(tolua_S,"cWorld");
tolua_function(tolua_S,"ForEachPlayer",tolua_cWorld_ForEachPlayer); tolua_function(tolua_S,"ForEachPlayer",tolua_cWorld_ForEachPlayer);
tolua_endmodule(tolua_S); tolua_endmodule(tolua_S);

View File

@ -247,15 +247,6 @@ void cRoot::UnloadWorlds()
cWorld* cRoot::GetWorld()
{
return GetDefaultWorld();
}
cWorld* cRoot::GetDefaultWorld() cWorld* cRoot::GetDefaultWorld()
{ {
return m_pState->pDefaultWorld; return m_pState->pDefaultWorld;
@ -277,6 +268,22 @@ cWorld* cRoot::GetWorld( const AString & a_WorldName )
bool cRoot::ForEachWorld(cWorldListCallback & a_Callback)
{
for( WorldMap::iterator itr = m_pState->WorldsByName.begin(); itr != m_pState->WorldsByName.end(); ++itr )
{
if (a_Callback.Item(itr->second))
{
return false;
}
}
return true;
}
void cRoot::TickWorlds( float a_Dt ) void cRoot::TickWorlds( float a_Dt )
{ {
for( WorldMap::iterator itr = m_pState->WorldsByName.begin(); itr != m_pState->WorldsByName.end(); ++itr ) for( WorldMap::iterator itr = m_pState->WorldsByName.begin(); itr != m_pState->WorldsByName.end(); ++itr )

View File

@ -21,6 +21,7 @@ class cServer;
class cWorld; class cWorld;
class cPlayer; class cPlayer;
typedef cItemCallback<cPlayer> cPlayerListCallback; typedef cItemCallback<cPlayer> cPlayerListCallback;
typedef cItemCallback<cWorld> cWorldListCallback;
@ -36,18 +37,21 @@ public:
void Start(); void Start();
cServer* GetServer() { return m_Server; } //tolua_export cServer* GetServer(void) { return m_Server; } //tolua_export
OBSOLETE cWorld* GetWorld(); //tolua_export cWorld* GetDefaultWorld(void); //tolua_export
cWorld* GetDefaultWorld(); //tolua_export
cWorld* GetWorld( const AString & a_WorldName ); //tolua_export cWorld* GetWorld( const AString & a_WorldName ); //tolua_export
/// Calls the callback for each world; returns true if the callback didn't abort (return true)
bool ForEachWorld(cWorldListCallback & a_Callback); // >> Exported in ManualBindings <<
cMonsterConfig *GetMonsterConfig() { return m_MonsterConfig;} cMonsterConfig *GetMonsterConfig() { return m_MonsterConfig;}
cGroupManager* GetGroupManager() { return m_GroupManager; } //tolua_export cGroupManager * GetGroupManager (void) { return m_GroupManager; } // tolua_export
cRecipeChecker* GetRecipeChecker() { return m_RecipeChecker; } //tolua_export cRecipeChecker * GetRecipeChecker(void) { return m_RecipeChecker; } // tolua_export
cFurnaceRecipe* GetFurnaceRecipe() { return m_FurnaceRecipe; } //tolua_export cFurnaceRecipe * GetFurnaceRecipe(void) { return m_FurnaceRecipe; } // tolua_export
cWebAdmin* GetWebAdmin() { return m_WebAdmin; } //tolua_export cWebAdmin * GetWebAdmin (void) { return m_WebAdmin; } // tolua_export
cPluginManager* GetPluginManager() { return m_PluginManager; } //tolua_export cPluginManager * GetPluginManager(void) { return m_PluginManager; } // tolua_export
cAuthenticator & GetAuthenticator() {return m_Authenticator; } cAuthenticator & GetAuthenticator(void) { return m_Authenticator; }
void ServerCommand(const char* a_Cmd ); //tolua_export void ServerCommand(const char* a_Cmd ); //tolua_export

View File

@ -123,9 +123,8 @@ public:
void AddPlayer( cPlayer* a_Player ); void AddPlayer( cPlayer* a_Player );
void RemovePlayer( cPlayer* a_Player ); void RemovePlayer( cPlayer* a_Player );
typedef struct lua_State lua_State; bool ForEachPlayer(cPlayerListCallback & a_Callback); // >> 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 unsigned int GetNumPlayers(); //tolua_export
// TODO: This interface is dangerous - rewrite to DoWithPlayer(playername, action) // TODO: This interface is dangerous - rewrite to DoWithPlayer(playername, action)