1
0

First working version of cLuaChunkStay.

It works, but has random failures, probably due to threading issues.
This commit is contained in:
madmaxoft 2014-02-09 20:39:45 +01:00
parent 4ef6c56f32
commit 47a497fa89
4 changed files with 92 additions and 17 deletions

View File

@ -11,20 +11,29 @@
cLuaChunkStay::cLuaChunkStay(void) :
m_LuaState((lua_State *)NULL) // Want a detached Lua state by default
{
}
void cLuaChunkStay::Enable( void cLuaChunkStay::Enable(
cWorld & a_World, cLuaState & a_LuaState, cWorld & a_World, lua_State * a_LuaState,
const cLuaState::cRef & a_OnChunkAvailable, const cLuaState::cRef & a_OnAllChunksAvailable int a_OnChunkAvailableStackPos, int a_OnAllChunksAvailableStackPos
) )
{ {
if (m_LuaState != NULL) if (m_LuaState.IsValid())
{ {
LOGWARNING("LuaChunkStay: Already enabled. Ignoring this call."); LOGWARNING("LuaChunkStay: Already enabled. Ignoring this call.");
a_LuaState.LogStackTrace(); cLuaState::LogStackTrace(a_LuaState);
return; return;
} }
m_LuaState = &a_LuaState; m_LuaState.Attach(a_LuaState);
m_OnChunkAvailable = a_OnAllChunksAvailable; m_OnChunkAvailable.RefStack(m_LuaState, a_OnChunkAvailableStackPos);
m_OnAllChunksAvailable = a_OnAllChunksAvailable; m_OnAllChunksAvailable.RefStack(m_LuaState, a_OnAllChunksAvailableStackPos);
super::Enable(*a_World.GetChunkMap()); super::Enable(*a_World.GetChunkMap());
} }
@ -32,9 +41,27 @@ void cLuaChunkStay::Enable(
void cLuaChunkStay::Disable(void)
{
super::Disable();
// If the state was valid (bound to Lua functions), unbind those functions:
if (!m_LuaState.IsValid())
{
return;
}
m_OnAllChunksAvailable.UnRef();
m_OnChunkAvailable.UnRef();
m_LuaState.Detach();
}
void cLuaChunkStay::OnChunkAvailable(int a_ChunkX, int a_ChunkZ) void cLuaChunkStay::OnChunkAvailable(int a_ChunkX, int a_ChunkZ)
{ {
m_LuaState->Call(m_OnChunkAvailable, a_ChunkX, a_ChunkZ); m_LuaState.Call(m_OnChunkAvailable, a_ChunkX, a_ChunkZ);
} }
@ -43,7 +70,7 @@ void cLuaChunkStay::OnChunkAvailable(int a_ChunkX, int a_ChunkZ)
void cLuaChunkStay::OnAllChunksAvailable(void) void cLuaChunkStay::OnAllChunksAvailable(void)
{ {
m_LuaState->Call(m_OnAllChunksAvailable); m_LuaState.Call(m_OnAllChunksAvailable);
} }

View File

@ -24,23 +24,28 @@ class cLuaChunkStay
public: public:
// Allow Lua to construct objects of this class: // Allow Lua to construct objects of this class:
cLuaChunkStay(void) {} cLuaChunkStay(void);
// Allow Lua to garbage-collect objects of this class: // Allow Lua to garbage-collect objects of this class:
~cLuaChunkStay() { } ~cLuaChunkStay() { }
// tolua_end // tolua_end
/** Enabled the ChunkStay for the specified world, with the specified Lua callbacks. /** Enables the ChunkStay for the specified world, with the specified Lua callbacks.
Exported in ManualBindings. */ Exported in ManualBindings. */
void Enable( void Enable(
cWorld & a_World, cLuaState & a_LuaState, cWorld & a_World, lua_State * a_LuaState,
const cLuaState::cRef & a_OnChunkAvailable, const cLuaState::cRef & a_OnAllChunksAvailable int a_OnChunkAvailableStackPos, int a_OnAllChunksAvailableStackPos
); );
// tolua_begin
/** Disables the ChunkStay. Cleans up the bound Lua state and callbacks */
virtual void Disable(void);
protected: protected:
/** The Lua state associated with the callbacks. Only valid when enabled. */ /** The Lua state associated with the callbacks. Only valid when enabled. */
cLuaState * m_LuaState; cLuaState m_LuaState;
/** The Lua function to call in OnChunkAvailable. Only valid when enabled. */ /** The Lua function to call in OnChunkAvailable. Only valid when enabled. */
cLuaState::cRef m_OnChunkAvailable; cLuaState::cRef m_OnChunkAvailable;
@ -51,7 +56,9 @@ protected:
// cChunkStay overrides: // cChunkStay overrides:
virtual void OnChunkAvailable(int a_ChunkX, int a_ChunkZ) override; virtual void OnChunkAvailable(int a_ChunkX, int a_ChunkZ) override;
virtual void OnAllChunksAvailable(void) override; virtual void OnAllChunksAvailable(void) override;
} ; // tolua_export } ;
// tolua_end

View File

@ -8,6 +8,7 @@
#include "PluginLua.h" #include "PluginLua.h"
#include "PluginManager.h" #include "PluginManager.h"
#include "LuaWindow.h" #include "LuaWindow.h"
#include "LuaChunkStay.h"
#include "../Root.h" #include "../Root.h"
#include "../World.h" #include "../World.h"
#include "../Entities/Player.h" #include "../Entities/Player.h"
@ -1638,6 +1639,42 @@ static int tolua_cPluginManager_CallPlugin(lua_State * tolua_S)
static int tolua_cLuaChunkStay_Enable(lua_State * tolua_S)
{
cLuaState L(tolua_S);
if (
!L.CheckParamUserType(1, "cLuaChunkStay") ||
!L.CheckParamUserType(2, "cWorld") ||
!L.CheckParamFunction(3, 4)
)
{
return 0;
}
// Read the params:
cLuaChunkStay * ChunkStay = (cLuaChunkStay *)tolua_tousertype(tolua_S, 1, NULL);
if (ChunkStay == NULL)
{
LOGWARNING("cLuaChunkStay:Enable(): invalid self");
L.LogStackTrace();
return 0;
}
cWorld * World = (cWorld *)tolua_tousertype(tolua_S, 2, NULL);
if (World == NULL)
{
LOGWARNING("cLuaChunkStay:Enable(): invalid world parameter");
L.LogStackTrace();
return 0;
}
ChunkStay->Enable(*World, tolua_S, 3, 4);
return 0;
}
static int tolua_cPlayer_GetGroups(lua_State* tolua_S) static int tolua_cPlayer_GetGroups(lua_State* tolua_S)
{ {
cPlayer* self = (cPlayer*) tolua_tousertype(tolua_S,1,0); cPlayer* self = (cPlayer*) tolua_tousertype(tolua_S,1,0);
@ -2403,6 +2440,10 @@ void ManualBindings::Bind(lua_State * tolua_S)
tolua_function(tolua_S, "LogStackTrace", tolua_cPluginManager_LogStackTrace); tolua_function(tolua_S, "LogStackTrace", tolua_cPluginManager_LogStackTrace);
tolua_endmodule(tolua_S); tolua_endmodule(tolua_S);
tolua_beginmodule(tolua_S, "cLuaChunkStay");
tolua_function(tolua_S, "Enable", tolua_cLuaChunkStay_Enable);
tolua_endmodule(tolua_S);
tolua_beginmodule(tolua_S, "cPlayer"); tolua_beginmodule(tolua_S, "cPlayer");
tolua_function(tolua_S, "GetGroups", tolua_cPlayer_GetGroups); tolua_function(tolua_S, "GetGroups", tolua_cPlayer_GetGroups);
tolua_function(tolua_S, "GetResolvedPermissions", tolua_cPlayer_GetResolvedPermissions); tolua_function(tolua_S, "GetResolvedPermissions", tolua_cPlayer_GetResolvedPermissions);

View File

@ -62,7 +62,7 @@ public:
/** Disables the ChunkStay, the chunks are released and the ChunkStay /** Disables the ChunkStay, the chunks are released and the ChunkStay
object can be edited with Add() and Remove() again*/ object can be edited with Add() and Remove() again*/
void Disable(void); virtual void Disable(void);
// tolua_end // tolua_end