1
0

Changed world tasks to use cLuaState::cCallback.

This commit is contained in:
Mattes D 2016-03-01 17:25:45 +01:00
parent eb044e140e
commit d0a04a7a82
2 changed files with 43 additions and 72 deletions

View File

@ -1876,8 +1876,8 @@ end
function HandleConsoleSchedule(a_Split) function HandleConsoleSchedule(a_Split)
local prev = os.clock() local prev = os.clock()
LOG("Scheduling a task for 2 seconds in the future (current os.clock is " .. prev .. ")") LOG("Scheduling a task for 5 seconds in the future (current os.clock is " .. prev .. ")")
cRoot:Get():GetDefaultWorld():ScheduleTask(40, cRoot:Get():GetDefaultWorld():ScheduleTask(5 * 20,
function () function ()
local current = os.clock() local current = os.clock()
local diff = current - prev local diff = current - prev

View File

@ -466,67 +466,41 @@ 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) static int tolua_cWorld_QueueTask(lua_State * tolua_S)
{ {
// Binding for cWorld::QueueTask // Function signature:
// Params: function // World:QueueTask(Callback)
// 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: // Retrieve the args:
cWorld * self = reinterpret_cast<cWorld *>(tolua_tousertype(tolua_S, 1, nullptr)); cLuaState L(tolua_S);
if (self == nullptr) if (
!L.CheckParamUserType(1, "cWorld") ||
!L.CheckParamNumber (2) ||
!L.CheckParamFunction(3)
)
{
return 0;
}
cWorld * World;
auto Task = std::make_shared<cLuaState::cCallback>();
if (!L.GetStackValues(1, World, Task))
{
return cManualBindings::lua_do_error(tolua_S, "Error in function call '#funcname#': Cannot read parameters");
}
if (World == nullptr)
{ {
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#': Not called on an object instance");
} }
if (!lua_isfunction(tolua_S, 2)) if (!Task->IsValid())
{ {
return cManualBindings::lua_do_error(tolua_S, "Error in function call '#funcname#': Expected a function for parameter #1"); return cManualBindings::lua_do_error(tolua_S, "Error in function call '#funcname#': Could not store the callback parameter");
} }
// Create a reference to the function: World->QueueTask([Task](cWorld & a_World)
int FnRef = luaL_ref(tolua_S, LUA_REGISTRYINDEX); {
if (FnRef == LUA_REFNIL) Task->Call(&a_World);
{ }
return cManualBindings::lua_do_error(tolua_S, "Error in function call '#funcname#': Could not get function reference of parameter #1"); );
}
auto ResettableTask = std::make_shared<cLuaWorldTask>(*Plugin, FnRef);
Plugin->AddResettable(ResettableTask);
self->QueueTask(std::bind(&cLuaWorldTask::Run, ResettableTask, std::placeholders::_1));
return 0; return 0;
} }
@ -576,16 +550,8 @@ static int tolua_cWorld_SetSignLines(lua_State * tolua_S)
static int tolua_cWorld_ScheduleTask(lua_State * tolua_S) static int tolua_cWorld_ScheduleTask(lua_State * tolua_S)
{ {
// Binding for cWorld::ScheduleTask // Function signature:
// Params: function, Ticks // World:ScheduleTask(NumTicks, Callback)
// 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: // Retrieve the args:
cLuaState L(tolua_S); cLuaState L(tolua_S);
@ -597,22 +563,27 @@ static int tolua_cWorld_ScheduleTask(lua_State * tolua_S)
{ {
return 0; return 0;
} }
cWorld * World = reinterpret_cast<cWorld *>(tolua_tousertype(tolua_S, 1, nullptr)); 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");
}
if (World == nullptr) if (World == nullptr)
{ {
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#': 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 get function reference of parameter #1"); return cManualBindings::lua_do_error(tolua_S, "Error in function call '#funcname#': Could not store the callback parameter");
} }
auto ResettableTask = std::make_shared<cLuaWorldTask>(*Plugin, FnRef); World->ScheduleTask(NumTicks, [Task](cWorld & a_World)
Plugin->AddResettable(ResettableTask); {
World->ScheduleTask(static_cast<int>(tolua_tonumber(tolua_S, 2, 0)), std::bind(&cLuaWorldTask::Run, ResettableTask, std::placeholders::_1)); Task->Call(&a_World);
}
);
return 0; return 0;
} }