summaryrefslogtreecommitdiffstats
path: root/src/Bindings/ManualBindings_World.cpp
diff options
context:
space:
mode:
authorMattes D <github@xoft.cz>2016-03-21 10:40:38 +0100
committerMattes D <github@xoft.cz>2016-03-21 10:40:38 +0100
commitfa53e98419152a83b2a74c5ea45a60449abc59c2 (patch)
tree766f4703a8c30a01bfc5148fe54c6d6b4a3e3cc7 /src/Bindings/ManualBindings_World.cpp
parentMerge pull request #3084 from cuberite/seadragon91-patch-1 (diff)
parentRevert "Lua callback" (diff)
downloadcuberite-fa53e98419152a83b2a74c5ea45a60449abc59c2.tar
cuberite-fa53e98419152a83b2a74c5ea45a60449abc59c2.tar.gz
cuberite-fa53e98419152a83b2a74c5ea45a60449abc59c2.tar.bz2
cuberite-fa53e98419152a83b2a74c5ea45a60449abc59c2.tar.lz
cuberite-fa53e98419152a83b2a74c5ea45a60449abc59c2.tar.xz
cuberite-fa53e98419152a83b2a74c5ea45a60449abc59c2.tar.zst
cuberite-fa53e98419152a83b2a74c5ea45a60449abc59c2.zip
Diffstat (limited to 'src/Bindings/ManualBindings_World.cpp')
-rw-r--r--src/Bindings/ManualBindings_World.cpp105
1 files changed, 67 insertions, 38 deletions
diff --git a/src/Bindings/ManualBindings_World.cpp b/src/Bindings/ManualBindings_World.cpp
index 3faf038aa..00d2169d8 100644
--- a/src/Bindings/ManualBindings_World.cpp
+++ b/src/Bindings/ManualBindings_World.cpp
@@ -466,41 +466,67 @@ static int tolua_cWorld_PrepareChunk(lua_State * tolua_S)
+class cLuaWorldTask :
+ public cPluginLua::cResettable
+{
+public:
+ cLuaWorldTask(cPluginLua & a_Plugin, int a_FnRef) :
+ cPluginLua::cResettable(a_Plugin),
+ m_FnRef(a_FnRef)
+ {
+ }
+
+ void Run(cWorld & a_World)
+ {
+ cCSLock Lock(m_CSPlugin);
+ if (m_Plugin != nullptr)
+ {
+ m_Plugin->Call(m_FnRef, &a_World);
+ }
+ }
+
+protected:
+ int m_FnRef;
+};
+
+
+
+
+
static int tolua_cWorld_QueueTask(lua_State * tolua_S)
{
- // Function signature:
- // World:QueueTask(Callback)
+ // Binding for cWorld::QueueTask
+ // Params: function
- // Retrieve the args:
- cLuaState L(tolua_S);
- if (
- !L.CheckParamUserType(1, "cWorld") ||
- !L.CheckParamNumber (2) ||
- !L.CheckParamFunction(3)
- )
+ // Retrieve the cPlugin from the LuaState:
+ cPluginLua * Plugin = cManualBindings::GetLuaPlugin(tolua_S);
+ if (Plugin == nullptr)
{
+ // An error message has been already printed in GetLuaPlugin()
return 0;
}
- cWorld * World;
- auto Task = std::make_shared<cLuaState::cCallback>();
- if (!L.GetStackValues(1, World, Task))
+
+ // Retrieve the args:
+ cWorld * self = reinterpret_cast<cWorld *>(tolua_tousertype(tolua_S, 1, nullptr));
+ if (self == nullptr)
{
- return cManualBindings::lua_do_error(tolua_S, "Error in function call '#funcname#': Cannot read parameters");
+ return cManualBindings::lua_do_error(tolua_S, "Error in function call '#funcname#': Not called on an object instance");
}
- if (World == nullptr)
+ if (!lua_isfunction(tolua_S, 2))
{
- return cManualBindings::lua_do_error(tolua_S, "Error in function call '#funcname#': Not called on an object instance");
+ return cManualBindings::lua_do_error(tolua_S, "Error in function call '#funcname#': Expected a function for parameter #1");
}
- if (!Task->IsValid())
+
+ // Create a reference to the function:
+ int FnRef = luaL_ref(tolua_S, LUA_REGISTRYINDEX);
+ if (FnRef == LUA_REFNIL)
{
- return cManualBindings::lua_do_error(tolua_S, "Error in function call '#funcname#': Could not store the callback parameter");
+ return cManualBindings::lua_do_error(tolua_S, "Error in function call '#funcname#': Could not get function reference of parameter #1");
}
- World->QueueTask([Task](cWorld & a_World)
- {
- Task->Call(&a_World);
- }
- );
+ auto ResettableTask = std::make_shared<cLuaWorldTask>(*Plugin, FnRef);
+ Plugin->AddResettable(ResettableTask);
+ self->QueueTask(std::bind(&cLuaWorldTask::Run, ResettableTask, std::placeholders::_1));
return 0;
}
@@ -550,8 +576,16 @@ static int tolua_cWorld_SetSignLines(lua_State * tolua_S)
static int tolua_cWorld_ScheduleTask(lua_State * tolua_S)
{
- // Function signature:
- // World:ScheduleTask(NumTicks, Callback)
+ // Binding for cWorld::ScheduleTask
+ // Params: function, Ticks
+
+ // Retrieve the cPlugin from the LuaState:
+ cPluginLua * Plugin = cManualBindings::GetLuaPlugin(tolua_S);
+ if (Plugin == nullptr)
+ {
+ // An error message has been already printed in GetLuaPlugin()
+ return 0;
+ }
// Retrieve the args:
cLuaState L(tolua_S);
@@ -563,27 +597,22 @@ static int tolua_cWorld_ScheduleTask(lua_State * tolua_S)
{
return 0;
}
- cWorld * World;
- int NumTicks;
- auto Task = std::make_shared<cLuaState::cCallback>();
- if (!L.GetStackValues(1, World, NumTicks, Task))
- {
- return cManualBindings::lua_do_error(tolua_S, "Error in function call '#funcname#': Cannot read parameters");
- }
+ cWorld * World = reinterpret_cast<cWorld *>(tolua_tousertype(tolua_S, 1, nullptr));
if (World == nullptr)
{
return cManualBindings::lua_do_error(tolua_S, "Error in function call '#funcname#': Not called on an object instance");
}
- if (!Task->IsValid())
+
+ // Create a reference to the function:
+ int FnRef = luaL_ref(tolua_S, LUA_REGISTRYINDEX);
+ if (FnRef == LUA_REFNIL)
{
- return cManualBindings::lua_do_error(tolua_S, "Error in function call '#funcname#': Could not store the callback parameter");
+ return cManualBindings::lua_do_error(tolua_S, "Error in function call '#funcname#': Could not get function reference of parameter #1");
}
- World->ScheduleTask(NumTicks, [Task](cWorld & a_World)
- {
- Task->Call(&a_World);
- }
- );
+ auto ResettableTask = std::make_shared<cLuaWorldTask>(*Plugin, FnRef);
+ Plugin->AddResettable(ResettableTask);
+ World->ScheduleTask(static_cast<int>(tolua_tonumber(tolua_S, 2, 0)), std::bind(&cLuaWorldTask::Run, ResettableTask, std::placeholders::_1));
return 0;
}