From 4e34e438f12abb31f2409ce9d50df8acc2128555 Mon Sep 17 00:00:00 2001 From: faketruth Date: Thu, 26 Jan 2012 20:39:46 +0000 Subject: Working on a new plugin design with Lua. While retaining backwards compatibility of course... In this new design a plugin consists of a folder with Lua files, this should enable plugin developers to keep things separate and have more overview. git-svn-id: http://mc-server.googlecode.com/svn/trunk@172 0a769ca7-a7f5-676a-18bf-c427514a06d6 --- source/AllToLua.pkg | 1 + source/Bindings.cpp | 337 +++++++++++++++++++++++++++++++++++++++++++--- source/Bindings.h | 2 +- source/cChunk.cpp | 56 ++++---- source/cPluginManager.cpp | 18 +++ source/cPlugin_NewLua.cpp | 121 +++++++++++++++++ source/cPlugin_NewLua.h | 20 +++ 7 files changed, 508 insertions(+), 47 deletions(-) create mode 100644 source/cPlugin_NewLua.cpp create mode 100644 source/cPlugin_NewLua.h (limited to 'source') diff --git a/source/AllToLua.pkg b/source/AllToLua.pkg index 8338f0088..0a10104d0 100644 --- a/source/AllToLua.pkg +++ b/source/AllToLua.pkg @@ -18,6 +18,7 @@ $cfile "cPawn.h" $cfile "cPlayer.h" $cfile "cPluginManager.h" $cfile "cPlugin.h" +$cfile "cPlugin_NewLua.h" $cfile "cPlugin_Lua.h" $cfile "cServer.h" $cfile "cWorld.h" diff --git a/source/Bindings.cpp b/source/Bindings.cpp index 930ed4e45..b8396c33b 100644 --- a/source/Bindings.cpp +++ b/source/Bindings.cpp @@ -1,6 +1,6 @@ /* ** Lua binding: AllToLua -** Generated automatically by tolua++-1.0.92 on 01/22/12 20:44:42. +** Generated automatically by tolua++-1.0.92 on 01/26/12 18:52:03. */ #ifndef __cplusplus @@ -30,6 +30,7 @@ TOLUA_API int tolua_AllToLua_open (lua_State* tolua_S); #include "cPlayer.h" #include "cPluginManager.h" #include "cPlugin.h" +#include "cPlugin_NewLua.h" #include "cPlugin_Lua.h" #include "cServer.h" #include "cWorld.h" @@ -162,50 +163,52 @@ static void tolua_reg_types (lua_State* tolua_S) tolua_usertype(tolua_S,"cPlugin"); tolua_usertype(tolua_S,"cStringMap"); tolua_usertype(tolua_S,"Json::Value"); - tolua_usertype(tolua_S,"cServer"); + tolua_usertype(tolua_S,"cInventory"); tolua_usertype(tolua_S,"cRoot"); + tolua_usertype(tolua_S,"Lua__cPickup"); + tolua_usertype(tolua_S,"Lua__cPacket_BlockDig"); tolua_usertype(tolua_S,"cPlugin::CommandStruct"); tolua_usertype(tolua_S,"cPickup"); - tolua_usertype(tolua_S,"cRecipeChecker"); + tolua_usertype(tolua_S,"cWorld"); tolua_usertype(tolua_S,"cPacket_Login"); tolua_usertype(tolua_S,"cClientHandle"); - tolua_usertype(tolua_S,"Lua__cPacket_BlockDig"); + tolua_usertype(tolua_S,"Vector3i"); tolua_usertype(tolua_S,"cFurnaceRecipe"); - tolua_usertype(tolua_S,"cCuboid"); + tolua_usertype(tolua_S,"cGroup"); tolua_usertype(tolua_S,"cChatColor"); - tolua_usertype(tolua_S,"cMCLogger"); + tolua_usertype(tolua_S,"cTracer"); tolua_usertype(tolua_S,"cPacket_PickupSpawn"); tolua_usertype(tolua_S,"Lua__cWebPlugin"); tolua_usertype(tolua_S,"Lua__cPawn"); - tolua_usertype(tolua_S,"cGroup"); + tolua_usertype(tolua_S,"cMCLogger"); tolua_usertype(tolua_S,"cItem"); tolua_usertype(tolua_S,"Vector3f"); tolua_usertype(tolua_S,"cPlugin_Lua"); - tolua_usertype(tolua_S,"cTracer"); + tolua_usertype(tolua_S,"cCuboid"); tolua_usertype(tolua_S,"Lua__cPlayer"); tolua_usertype(tolua_S,"cPacket"); tolua_usertype(tolua_S,"cPacket_BlockDig"); tolua_usertype(tolua_S,"cWebAdmin"); - tolua_usertype(tolua_S,"Vector3i"); - tolua_usertype(tolua_S,"cBlockEntity"); - tolua_usertype(tolua_S,"cWebPlugin"); tolua_usertype(tolua_S,"Lua__cTCPLink"); - tolua_usertype(tolua_S,"Lua__cPlugin"); + tolua_usertype(tolua_S,"cBlockEntity"); tolua_usertype(tolua_S,"cTCPLink"); + tolua_usertype(tolua_S,"cRecipeChecker"); + tolua_usertype(tolua_S,"Lua__cPlugin"); + tolua_usertype(tolua_S,"cGroupManager"); tolua_usertype(tolua_S,"cPacket_BlockPlace"); tolua_usertype(tolua_S,"cLadder"); - tolua_usertype(tolua_S,"cGroupManager"); - tolua_usertype(tolua_S,"cWorld"); - tolua_usertype(tolua_S,"cIniFile"); tolua_usertype(tolua_S,"cPluginManager"); + tolua_usertype(tolua_S,"Lua__cPlugin_NewLua"); + tolua_usertype(tolua_S,"cIniFile"); + tolua_usertype(tolua_S,"Lua__cEntity"); tolua_usertype(tolua_S,"HTTPRequest"); tolua_usertype(tolua_S,"cPawn"); tolua_usertype(tolua_S,"cPlayer"); tolua_usertype(tolua_S,"cTorch"); - tolua_usertype(tolua_S,"Lua__cPickup"); tolua_usertype(tolua_S,"cEntity"); - tolua_usertype(tolua_S,"cInventory"); - tolua_usertype(tolua_S,"Lua__cEntity"); + tolua_usertype(tolua_S,"cWebPlugin"); + tolua_usertype(tolua_S,"cPlugin_NewLua"); + tolua_usertype(tolua_S,"cServer"); tolua_usertype(tolua_S,"cStairs"); tolua_usertype(tolua_S,"Vector3d"); } @@ -8008,6 +8011,295 @@ static int tolua_collect_Lua__cPlugin (lua_State* tolua_S) } #endif +/* method: Initialize of class cPlugin_NewLua */ +#ifndef TOLUA_DISABLE_tolua_AllToLua_cPlugin_NewLua_Initialize00 +static int tolua_AllToLua_cPlugin_NewLua_Initialize00(lua_State* tolua_S) +{ +#ifndef TOLUA_RELEASE + tolua_Error tolua_err; + if ( + !tolua_isusertype(tolua_S,1,"cPlugin_NewLua",0,&tolua_err) || + !tolua_isnoobj(tolua_S,2,&tolua_err) + ) + goto tolua_lerror; + else +#endif + { + cPlugin_NewLua* self = (cPlugin_NewLua*) tolua_tousertype(tolua_S,1,0); +#ifndef TOLUA_RELEASE + if (!self) tolua_error(tolua_S,"invalid 'self' in function 'Initialize'", NULL); +#endif + { + bool tolua_ret = (bool) self->Initialize(); + tolua_pushboolean(tolua_S,(bool)tolua_ret); + } + } + return 1; +#ifndef TOLUA_RELEASE + tolua_lerror: + tolua_error(tolua_S,"#ferror in function 'Initialize'.",&tolua_err); + return 0; +#endif +} +#endif //#ifndef TOLUA_DISABLE + + class Lua__cPlugin_NewLua : public cPlugin_NewLua, public ToluaBase { +public: + bool Initialize( void ) { + if (push_method("Initialize", tolua_AllToLua_cPlugin_NewLua_Initialize00)) { + ToluaBase::dbcall(lua_state, 1, 1); + bool tolua_ret = ( bool )tolua_toboolean(lua_state, -1, 0); + lua_pop(lua_state, 1); + return tolua_ret; + } else { + return ( bool ) cPlugin_NewLua:: Initialize(); + }; + }; + void OnDisable( void ) { + if (push_method("OnDisable", tolua_AllToLua_cPlugin_OnDisable00)) { + ToluaBase::dbcall(lua_state, 1, 0); + } else { + return ( void ) cPlugin_NewLua:: OnDisable(); + }; + }; + void Tick( float a_Dt) { + if (push_method("Tick", tolua_AllToLua_cPlugin_Tick00)) { + tolua_pushnumber(lua_state, (lua_Number)a_Dt); + ToluaBase::dbcall(lua_state, 2, 0); + } else { + return ( void ) cPlugin_NewLua:: Tick(a_Dt); + }; + }; + bool OnCollectItem( cPickup* a_Pickup, cPlayer* a_Player) { + if (push_method("OnCollectItem", tolua_AllToLua_cPlugin_OnCollectItem00)) { + tolua_pushusertype(lua_state, (void*)a_Pickup, "cPickup"); + tolua_pushusertype(lua_state, (void*)a_Player, "cPlayer"); + ToluaBase::dbcall(lua_state, 3, 1); + bool tolua_ret = ( bool )tolua_toboolean(lua_state, -1, 0); + lua_pop(lua_state, 1); + return tolua_ret; + } else { + return ( bool ) cPlugin_NewLua:: OnCollectItem(a_Pickup,a_Player); + }; + }; + bool OnDisconnect( std::string a_Reason, cPlayer* a_Player) { + if (push_method("OnDisconnect", tolua_AllToLua_cPlugin_OnDisconnect00)) { + tolua_pushcppstring(lua_state, (const char*)a_Reason); + tolua_pushusertype(lua_state, (void*)a_Player, "cPlayer"); + ToluaBase::dbcall(lua_state, 3, 1); + bool tolua_ret = ( bool )tolua_toboolean(lua_state, -1, 0); + lua_pop(lua_state, 1); + return tolua_ret; + } else { + return ( bool ) cPlugin_NewLua:: OnDisconnect(a_Reason,a_Player); + }; + }; + bool OnBlockPlace( cPacket_BlockPlace* a_PacketData, cPlayer* a_Player) { + if (push_method("OnBlockPlace", tolua_AllToLua_cPlugin_OnBlockPlace00)) { + tolua_pushusertype(lua_state, (void*)a_PacketData, "cPacket_BlockPlace"); + tolua_pushusertype(lua_state, (void*)a_Player, "cPlayer"); + ToluaBase::dbcall(lua_state, 3, 1); + bool tolua_ret = ( bool )tolua_toboolean(lua_state, -1, 0); + lua_pop(lua_state, 1); + return tolua_ret; + } else { + return ( bool ) cPlugin_NewLua:: OnBlockPlace(a_PacketData,a_Player); + }; + }; + bool OnBlockDig( cPacket_BlockDig* a_PacketData, cPlayer* a_Player, cItem* a_PickupItem) { + if (push_method("OnBlockDig", tolua_AllToLua_cPlugin_OnBlockDig00)) { + tolua_pushusertype(lua_state, (void*)a_PacketData, "cPacket_BlockDig"); + tolua_pushusertype(lua_state, (void*)a_Player, "cPlayer"); + tolua_pushusertype(lua_state, (void*)a_PickupItem, "cItem"); + ToluaBase::dbcall(lua_state, 4, 1); + bool tolua_ret = ( bool )tolua_toboolean(lua_state, -1, 0); + lua_pop(lua_state, 1); + return tolua_ret; + } else { + return ( bool ) cPlugin_NewLua:: OnBlockDig(a_PacketData,a_Player,a_PickupItem); + }; + }; + bool OnChat( const char* a_Chat, cPlayer* a_Player) { + if (push_method("OnChat", tolua_AllToLua_cPlugin_OnChat00)) { + tolua_pushstring(lua_state, (const char*)a_Chat); + tolua_pushusertype(lua_state, (void*)a_Player, "cPlayer"); + ToluaBase::dbcall(lua_state, 3, 1); + bool tolua_ret = ( bool )tolua_toboolean(lua_state, -1, 0); + lua_pop(lua_state, 1); + return tolua_ret; + } else { + return ( bool ) cPlugin_NewLua:: OnChat(a_Chat,a_Player); + }; + }; + bool OnLogin( cPacket_Login* a_PacketData) { + if (push_method("OnLogin", tolua_AllToLua_cPlugin_OnLogin00)) { + tolua_pushusertype(lua_state, (void*)a_PacketData, "cPacket_Login"); + ToluaBase::dbcall(lua_state, 2, 1); + bool tolua_ret = ( bool )tolua_toboolean(lua_state, -1, 0); + lua_pop(lua_state, 1); + return tolua_ret; + } else { + return ( bool ) cPlugin_NewLua:: OnLogin(a_PacketData); + }; + }; + void OnPlayerSpawn( cPlayer* a_Player) { + if (push_method("OnPlayerSpawn", tolua_AllToLua_cPlugin_OnPlayerSpawn00)) { + tolua_pushusertype(lua_state, (void*)a_Player, "cPlayer"); + ToluaBase::dbcall(lua_state, 2, 0); + } else { + return ( void ) cPlugin_NewLua:: OnPlayerSpawn(a_Player); + }; + }; + bool OnPlayerJoin( cPlayer* a_Player) { + if (push_method("OnPlayerJoin", tolua_AllToLua_cPlugin_OnPlayerJoin00)) { + tolua_pushusertype(lua_state, (void*)a_Player, "cPlayer"); + ToluaBase::dbcall(lua_state, 2, 1); + bool tolua_ret = ( bool )tolua_toboolean(lua_state, -1, 0); + lua_pop(lua_state, 1); + return tolua_ret; + } else { + return ( bool ) cPlugin_NewLua:: OnPlayerJoin(a_Player); + }; + }; + void OnPlayerMove( cPlayer* a_Player) { + if (push_method("OnPlayerMove", tolua_AllToLua_cPlugin_OnPlayerMove00)) { + tolua_pushusertype(lua_state, (void*)a_Player, "cPlayer"); + ToluaBase::dbcall(lua_state, 2, 0); + } else { + return ( void ) cPlugin_NewLua:: OnPlayerMove(a_Player); + }; + }; + void OnTakeDamage( cPawn* a_Pawn, TakeDamageInfo* a_TakeDamageInfo) { + if (push_method("OnTakeDamage", tolua_AllToLua_cPlugin_OnTakeDamage00)) { + tolua_pushusertype(lua_state, (void*)a_Pawn, "cPawn"); + tolua_pushusertype(lua_state, (void*)a_TakeDamageInfo, "TakeDamageInfo"); + ToluaBase::dbcall(lua_state, 3, 0); + } else { + return ( void ) cPlugin_NewLua:: OnTakeDamage(a_Pawn,a_TakeDamageInfo); + }; + }; + bool OnKilled( cPawn* a_Killed, cEntity* a_Killer) { + if (push_method("OnKilled", tolua_AllToLua_cPlugin_OnKilled00)) { + tolua_pushusertype(lua_state, (void*)a_Killed, "cPawn"); + tolua_pushusertype(lua_state, (void*)a_Killer, "cEntity"); + ToluaBase::dbcall(lua_state, 3, 1); + bool tolua_ret = ( bool )tolua_toboolean(lua_state, -1, 0); + lua_pop(lua_state, 1); + return tolua_ret; + } else { + return ( bool ) cPlugin_NewLua:: OnKilled(a_Killed,a_Killer); + }; + }; + + bool cPlugin_NewLua__Initialize( void ) { + return ( bool )cPlugin_NewLua::Initialize(); + }; + void cPlugin_NewLua__OnDisable( void ) { + return ( void )cPlugin_NewLua::OnDisable(); + }; + void cPlugin_NewLua__Tick( float a_Dt) { + return ( void )cPlugin_NewLua::Tick(a_Dt); + }; + bool cPlugin_NewLua__OnCollectItem( cPickup* a_Pickup, cPlayer* a_Player) { + return ( bool )cPlugin_NewLua::OnCollectItem(a_Pickup,a_Player); + }; + bool cPlugin_NewLua__OnDisconnect( std::string a_Reason, cPlayer* a_Player) { + return ( bool )cPlugin_NewLua::OnDisconnect(a_Reason,a_Player); + }; + bool cPlugin_NewLua__OnBlockPlace( cPacket_BlockPlace* a_PacketData, cPlayer* a_Player) { + return ( bool )cPlugin_NewLua::OnBlockPlace(a_PacketData,a_Player); + }; + bool cPlugin_NewLua__OnBlockDig( cPacket_BlockDig* a_PacketData, cPlayer* a_Player, cItem* a_PickupItem) { + return ( bool )cPlugin_NewLua::OnBlockDig(a_PacketData,a_Player,a_PickupItem); + }; + bool cPlugin_NewLua__OnChat( const char* a_Chat, cPlayer* a_Player) { + return ( bool )cPlugin_NewLua::OnChat(a_Chat,a_Player); + }; + bool cPlugin_NewLua__OnLogin( cPacket_Login* a_PacketData) { + return ( bool )cPlugin_NewLua::OnLogin(a_PacketData); + }; + void cPlugin_NewLua__OnPlayerSpawn( cPlayer* a_Player) { + return ( void )cPlugin_NewLua::OnPlayerSpawn(a_Player); + }; + bool cPlugin_NewLua__OnPlayerJoin( cPlayer* a_Player) { + return ( bool )cPlugin_NewLua::OnPlayerJoin(a_Player); + }; + void cPlugin_NewLua__OnPlayerMove( cPlayer* a_Player) { + return ( void )cPlugin_NewLua::OnPlayerMove(a_Player); + }; + void cPlugin_NewLua__OnTakeDamage( cPawn* a_Pawn, TakeDamageInfo* a_TakeDamageInfo) { + return ( void )cPlugin_NewLua::OnTakeDamage(a_Pawn,a_TakeDamageInfo); + }; + bool cPlugin_NewLua__OnKilled( cPawn* a_Killed, cEntity* a_Killer) { + return ( bool )cPlugin_NewLua::OnKilled(a_Killed,a_Killer); + }; +}; + +/* method: tolua__set_instance of class Lua__cPlugin_NewLua */ +#ifndef TOLUA_DISABLE_tolua_AllToLua_Lua__cPlugin_NewLua_tolua__set_instance00 +static int tolua_AllToLua_Lua__cPlugin_NewLua_tolua__set_instance00(lua_State* tolua_S) +{ +#ifndef TOLUA_RELEASE + tolua_Error tolua_err; + if ( + !tolua_isusertype(tolua_S,1,"Lua__cPlugin_NewLua",0,&tolua_err) || + !tolua_isnoobj(tolua_S,3,&tolua_err) + ) + goto tolua_lerror; + else +#endif + { + Lua__cPlugin_NewLua* self = (Lua__cPlugin_NewLua*) tolua_tousertype(tolua_S,1,0); + lua_State* L = tolua_S; + lua_Object lo = ((lua_Object) tolua_tovalue(tolua_S,2,0)); +#ifndef TOLUA_RELEASE + if (!self) tolua_error(tolua_S,"invalid 'self' in function 'tolua__set_instance'", NULL); +#endif + { + self->tolua__set_instance(L,lo); + } + } + return 0; +#ifndef TOLUA_RELEASE + tolua_lerror: + tolua_error(tolua_S,"#ferror in function 'tolua__set_instance'.",&tolua_err); + return 0; +#endif +} +#endif //#ifndef TOLUA_DISABLE + +/* method: cPlugin_NewLua__Initialize of class Lua__cPlugin_NewLua */ +#ifndef TOLUA_DISABLE_tolua_AllToLua_Lua__cPlugin_NewLua_cPlugin_NewLua__Initialize00 +static int tolua_AllToLua_Lua__cPlugin_NewLua_cPlugin_NewLua__Initialize00(lua_State* tolua_S) +{ +#ifndef TOLUA_RELEASE + tolua_Error tolua_err; + if ( + !tolua_isusertype(tolua_S,1,"Lua__cPlugin_NewLua",0,&tolua_err) || + !tolua_isnoobj(tolua_S,2,&tolua_err) + ) + goto tolua_lerror; + else +#endif + { + Lua__cPlugin_NewLua* self = (Lua__cPlugin_NewLua*) tolua_tousertype(tolua_S,1,0); +#ifndef TOLUA_RELEASE + if (!self) tolua_error(tolua_S,"invalid 'self' in function 'cPlugin_NewLua__Initialize'", NULL); +#endif + { + bool tolua_ret = (bool) self->cPlugin_NewLua__Initialize(); + tolua_pushboolean(tolua_S,(bool)tolua_ret); + } + } + return 1; +#ifndef TOLUA_RELEASE + tolua_lerror: + tolua_error(tolua_S,"#ferror in function 'cPlugin_NewLua__Initialize'.",&tolua_err); + return 0; +#endif +} +#endif //#ifndef TOLUA_DISABLE + /* method: GetFileName of class cPlugin_Lua */ #ifndef TOLUA_DISABLE_tolua_AllToLua_cPlugin_Lua_GetFileName00 static int tolua_AllToLua_cPlugin_Lua_GetFileName00(lua_State* tolua_S) @@ -16201,6 +16493,15 @@ TOLUA_API int tolua_AllToLua_open (lua_State* tolua_S) tolua_function(tolua_S,".call",tolua_AllToLua_Lua__cPlugin_new00_local); tolua_function(tolua_S,"delete",tolua_AllToLua_Lua__cPlugin_delete00); tolua_endmodule(tolua_S); + tolua_cclass(tolua_S,"cPlugin_NewLua","cPlugin_NewLua","cPlugin",NULL); + tolua_beginmodule(tolua_S,"cPlugin_NewLua"); + tolua_function(tolua_S,"Initialize",tolua_AllToLua_cPlugin_NewLua_Initialize00); + tolua_endmodule(tolua_S); + tolua_cclass(tolua_S,"Lua__cPlugin_NewLua","Lua__cPlugin_NewLua","cPlugin_NewLua",NULL); + tolua_beginmodule(tolua_S,"Lua__cPlugin_NewLua"); + tolua_function(tolua_S,"tolua__set_instance",tolua_AllToLua_Lua__cPlugin_NewLua_tolua__set_instance00); + tolua_function(tolua_S,"cPlugin_NewLua__Initialize",tolua_AllToLua_Lua__cPlugin_NewLua_cPlugin_NewLua__Initialize00); + tolua_endmodule(tolua_S); tolua_cclass(tolua_S,"cPlugin_Lua","cPlugin_Lua","",NULL); tolua_beginmodule(tolua_S,"cPlugin_Lua"); tolua_function(tolua_S,"GetFileName",tolua_AllToLua_cPlugin_Lua_GetFileName00); diff --git a/source/Bindings.h b/source/Bindings.h index 7f80ee6a0..6e7719b93 100644 --- a/source/Bindings.h +++ b/source/Bindings.h @@ -1,6 +1,6 @@ /* ** Lua binding: AllToLua -** Generated automatically by tolua++-1.0.92 on 01/22/12 20:44:42. +** Generated automatically by tolua++-1.0.92 on 01/26/12 18:52:03. */ /* Exported function */ diff --git a/source/cChunk.cpp b/source/cChunk.cpp index 875cefebb..04f7cc979 100644 --- a/source/cChunk.cpp +++ b/source/cChunk.cpp @@ -188,32 +188,32 @@ void cChunk::Tick(float a_Dt) m_pState->BlockListCriticalSection.Lock(); unsigned int PendingSendBlocks = m_pState->PendingSendBlocks.size(); - if( PendingSendBlocks > 1 ) - { - cPacket_MultiBlock MultiBlock; - MultiBlock.m_ChunkX = m_PosX; - MultiBlock.m_ChunkZ = m_PosZ; - MultiBlock.m_NumBlocks = (short)PendingSendBlocks; - MultiBlock.m_BlockCoordinates = new unsigned short[PendingSendBlocks]; - MultiBlock.m_BlockTypes = new char[PendingSendBlocks]; - MultiBlock.m_BlockMetas = new char[PendingSendBlocks]; - //LOG("Sending multiblock packet for %i blocks", PendingSendBlocks ); - for( unsigned int i = 0; i < PendingSendBlocks; i++) - { - unsigned int index = m_pState->PendingSendBlocks[i]; - unsigned int Y = index % 128; - unsigned int Z = (index / 128) % 16; - unsigned int X = (index / (128*16)); - - MultiBlock.m_BlockCoordinates[i] = (Z&0xf) | (X&0xf)<<4 | (Y&0xff)<<8; - //LOG("X: %i Y: %i Z: %i Combo: 0x%04x", X, Y, Z, MultiBlock.m_BlockCoordinates[i] ); - MultiBlock.m_BlockTypes[i] = m_BlockType[index]; - MultiBlock.m_BlockMetas[i] = GetLight( m_BlockMeta, index ); - } - m_pState->PendingSendBlocks.clear(); - PendingSendBlocks = m_pState->PendingSendBlocks.size(); - Broadcast( MultiBlock ); - } + if( PendingSendBlocks > 1 ) + { + cPacket_MultiBlock MultiBlock; + MultiBlock.m_ChunkX = m_PosX; + MultiBlock.m_ChunkZ = m_PosZ; + MultiBlock.m_NumBlocks = (short)PendingSendBlocks; + MultiBlock.m_BlockCoordinates = new unsigned short[PendingSendBlocks]; + MultiBlock.m_BlockTypes = new char[PendingSendBlocks]; + MultiBlock.m_BlockMetas = new char[PendingSendBlocks]; + //LOG("Sending multiblock packet for %i blocks", PendingSendBlocks ); + for( unsigned int i = 0; i < PendingSendBlocks; i++) + { + unsigned int index = m_pState->PendingSendBlocks[i]; + unsigned int Y = index % 128; + unsigned int Z = (index / 128) % 16; + unsigned int X = (index / (128*16)); + + MultiBlock.m_BlockCoordinates[i] = (Z&0xf) | (X&0xf)<<4 | (Y&0xff)<<8; + //LOG("X: %i Y: %i Z: %i Combo: 0x%04x", X, Y, Z, MultiBlock.m_BlockCoordinates[i] ); + MultiBlock.m_BlockTypes[i] = m_BlockType[index]; + MultiBlock.m_BlockMetas[i] = GetLight( m_BlockMeta, index ); + } + m_pState->PendingSendBlocks.clear(); + PendingSendBlocks = m_pState->PendingSendBlocks.size(); + Broadcast( MultiBlock ); + } if( PendingSendBlocks > 0 ) { for( unsigned int i = 0; i < PendingSendBlocks; i++) @@ -969,7 +969,7 @@ bool cChunk::SaveToDisk() char SourceFile[128]; sprintf_s(SourceFile, 128, "world/X%i_Y%i_Z%i.bin", m_PosX, m_PosY, m_PosZ ); - #ifdef _WIN32 + #ifdef _WIN32 { SECURITY_ATTRIBUTES Attrib; Attrib.nLength = sizeof(SECURITY_ATTRIBUTES); @@ -979,7 +979,7 @@ bool cChunk::SaveToDisk() } #else { - mkdir("world", S_IRWXU | S_IRWXG | S_IRWXO); + mkdir("world", S_IRWXU | S_IRWXG | S_IRWXO); } #endif diff --git a/source/cPluginManager.cpp b/source/cPluginManager.cpp index f1369bf46..007e0fd92 100644 --- a/source/cPluginManager.cpp +++ b/source/cPluginManager.cpp @@ -1,6 +1,7 @@ #include "cPluginManager.h" #include "cPlugin.h" #include "cPlugin_Lua.h" +#include "cPlugin_NewLua.h" #include "cMCLogger.h" #include "cWebAdmin.h" #include "cItem.h" @@ -99,6 +100,18 @@ void cPluginManager::ReloadPluginsNow() } } } + else if( ValueName.compare("NewPlugin") == 0 ) // New plugin style + { + std::string PluginFile = IniFile.GetValue(KeyNum, i ); + if( !PluginFile.empty() ) + { + cPlugin_NewLua* Plugin = new cPlugin_NewLua( PluginFile.c_str() ); + if( !AddPlugin( Plugin ) ) + { + delete Plugin; + } + } + } #if USE_SQUIRREL else if( ValueName.compare("Squirrel") == 0 ) // Squirrel plugin { @@ -481,6 +494,11 @@ cPlugin_Lua* cPluginManager::GetLuaPlugin( lua_State* a_State ) void cPluginManager::AddHook( cPlugin* a_Plugin, PluginHook a_Hook ) { + if( !a_Plugin ) + { + LOGWARN("Called cPluginManager::AddHook while a_Plugin is NULL"); + return; + } PluginList & Plugins = m_pState->Hooks[ a_Hook ]; Plugins.remove( a_Plugin ); Plugins.push_back( a_Plugin ); diff --git a/source/cPlugin_NewLua.cpp b/source/cPlugin_NewLua.cpp new file mode 100644 index 000000000..90647e011 --- /dev/null +++ b/source/cPlugin_NewLua.cpp @@ -0,0 +1,121 @@ +#define LUA_USE_POSIX +#include "cPlugin_NewLua.h" +#include "cMCLogger.h" + +extern "C" +{ +#include "lualib.h" +} + +#include "tolua++.h" +#include "Bindings.h" +#include "ManualBindings.h" + +extern bool report_errors(lua_State* lua, int status); + +cPlugin_NewLua::cPlugin_NewLua( const char* a_PluginName ) + : m_LuaState( 0 ) +{ + m_Directory = a_PluginName; +} + +cPlugin_NewLua::~cPlugin_NewLua() +{ + if( m_LuaState ) + { + lua_close( m_LuaState ); + m_LuaState = 0; + } +} + +bool cPlugin_NewLua::Initialize() +{ + if( !m_LuaState ) + { + m_LuaState = lua_open(); + luaL_openlibs( m_LuaState ); + tolua_AllToLua_open(m_LuaState); + ManualBindings::Bind( m_LuaState ); + } + + std::string PluginFiles[] = { std::string("Plugins/") + m_Directory + "/tick.lua" + ,std::string("Plugins/") + m_Directory + "/main.lua" }; + + + // Load all files for this plugin, and execute them + for( int i = 0; i < 2; ++i ) + { + int s = luaL_loadfile(m_LuaState, PluginFiles[i].c_str() ); + if( report_errors( m_LuaState, s ) ) + { + LOGERROR("Can't load plugin %s because of an error in file %s", m_Directory.c_str(), PluginFiles[i].c_str() ); + lua_close( m_LuaState ); + m_LuaState = 0; + return false; + } + + s = lua_pcall(m_LuaState, 0, LUA_MULTRET, 0); + if( report_errors( m_LuaState, s ) ) + { + LOGERROR("Error in plugin %s in file %s", m_Directory.c_str(), PluginFiles[i].c_str() ); + lua_close( m_LuaState ); + m_LuaState = 0; + return false; + } + } + + + // Call intialize function + lua_getglobal(m_LuaState, "Initialize"); + if(!lua_isfunction(m_LuaState,-1)) + { + LOGWARN("Error in plugin %s: Could not find function Initialize()", m_Directory.c_str() ); + lua_pop(m_LuaState,1); + lua_close( m_LuaState ); + m_LuaState = 0; + return false; + } + + tolua_pushusertype(m_LuaState, this, "cPlugin_NewLua"); + + // do the call (1 arguments, 1 result) + int s = lua_pcall(m_LuaState, 1, 1, 0); + if( report_errors( m_LuaState, s ) ) + { + LOGWARN("Error in plugin %s calling function Initialize()", m_Directory.c_str() ); + lua_close( m_LuaState ); + m_LuaState = 0; + return false; + } + + if( !lua_isboolean( m_LuaState, -1 ) ) + { + LOGWARN("Error in plugin %s Initialize() must return a boolean value!", m_Directory.c_str() ); + lua_close( m_LuaState ); + m_LuaState = 0; + return false; + } + + bool bSuccess = (tolua_toboolean( m_LuaState, -1, 0) > 0); + return bSuccess; +} + +void cPlugin_NewLua::Tick(float a_Dt) +{ + lua_getglobal(m_LuaState, "Tick"); + if(!lua_isfunction(m_LuaState,-1)) + { + LOGWARN("Error in plugin %s: Could not find function Tick()", m_Directory.c_str() ); + lua_pop(m_LuaState,1); + return; + } + + tolua_pushnumber( m_LuaState, a_Dt ); + + // do the call (1 arguments, 0 result)/ + int s = lua_pcall(m_LuaState, 1, 0, 0); + if( report_errors( m_LuaState, s ) ) + { + LOGWARN("Error in plugin %s calling function Tick()", m_Directory.c_str() ); + } +} \ No newline at end of file diff --git a/source/cPlugin_NewLua.h b/source/cPlugin_NewLua.h new file mode 100644 index 000000000..418865879 --- /dev/null +++ b/source/cPlugin_NewLua.h @@ -0,0 +1,20 @@ +#pragma once + +#include "cPlugin.h" +#include + +typedef struct lua_State lua_State; + +class cPlugin_NewLua : public cPlugin //tolua_export +{ //tolua_export +public: //tolua_export + cPlugin_NewLua( const char* a_PluginName ); + ~cPlugin_NewLua(); + + virtual bool Initialize(); //tolua_export + virtual void Tick(float a_Dt); //tolua_export + +private: + std::string m_Directory; + lua_State* m_LuaState; +};//tolua_export \ No newline at end of file -- cgit v1.2.3