//Used to clean up any pending events when restarting. void DestroyAllLuaEvents(PLUA_INSTANCE instance) { //Clean up for all events. li::References::iterator itr = instance->m_globalFRefs.begin(); ptrdiff_t ref = LUA_REFNIL; GET_LOCK; for(; itr != instance->m_globalFRefs.end(); ++itr) { if((*itr) != NULL && (*itr)->head_node != NULL && (*itr)->head_node->type == LUA_TFUNCTION) { ref = (*itr)->head_node->val.obj_ref; sEventMgr.RemoveEvents(World::getSingletonPtr(), ref + LUA_EVENTS_END); cleanup_varparam((*itr), instance->lu); } } instance->m_globalFRefs.clear(); }
void lua_engine::ExecuteLuaFunction(variadic_parameter* params) { if(params != NULL) { PLUA_INSTANCE li_ = lua_instance.get(); if(li_->m_globalFRefs.find(params) != li_->m_globalFRefs.end()) { lua_State* lu = li_->lu; //place the function on the stack. ReferenceHandler::getReference(lu, params->head_node->val.obj_ref); int arg_cnt = params->count - 2; //retrieve the repeats. variadic_node* function_node = params->head_node; ptrdiff_t repeats = params->head_node->next->val.bewl; /* Prepare to push arguments, 1st assign the head node to the actual arguments registered to this function */ params->head_node = function_node->next->next; //subtract the function n repeat node from arg count. params->count -= 2; //Now we push all args. luabridge::tdstack<variadic_parameter*>::push(lu, params); params->head_node = function_node; params->count += 2; //call the function if(lua_pcall(lu, arg_cnt, 0, 0)) report(lu); //if it's not an infinite/one time call event. if(repeats > 1) //decrement repeats and put it back in the params. function_node->next->val.bewl = (int)--repeats; else if(repeats == 1) { //remove this function from storage. li_->m_globalFRefs.erase(params); //clean up the rest of the args cleanup_varparam(params, lu); } } } }
void LuaGameObject::Destroy() { sEventMgr.RemoveEvents(_gameobject, EVENT_LUA_GAMEOBJ_EVENTS); PLUA_INSTANCE ref = lua_instance.get(); { li::GOInterfaceMap::iterator it; std::pair< li::GOInterfaceMap::iterator, li::GOInterfaceMap::iterator> interfaces = ref->m_goInterfaceMap.equal_range(_gameobject->GetEntry()); for(; interfaces.first != interfaces.second;) { it = interfaces.first++; if(it->second != NULL && it->second == this) ref->m_goInterfaceMap.erase(it); } } //clean up any refs being used by this go. { std::pair<li::ObjectFRefMap::iterator, li::ObjectFRefMap::iterator> frefs = ref->m_goFRefs.equal_range(_gameobject->GetLowGUID()); for(; frefs.first != frefs.second; ++frefs.first) cleanup_varparam(frefs.first->second, ref->lu); ref->m_goFRefs.erase(_gameobject->GetLowGUID()); } delete this; }
void LuaCreature::Destroy() { sEventMgr.RemoveEvents(_unit, EVENT_LUA_CREATURE_EVENTS); PLUA_INSTANCE li_ = lua_instance.get(); { std::pair<li::CreatureInterfaceMap::iterator, li::CreatureInterfaceMap::iterator> interfaces = li_->m_creatureInterfaceMap.equal_range(_unit->GetEntry()); li::CreatureInterfaceMap::iterator it; for(; interfaces.first != interfaces.second;) { it = interfaces.first++; if(it->second != NULL && it->second == this) li_->m_creatureInterfaceMap.erase(it); } } { //Function Ref clean up std::pair<li::ObjectFRefMap::iterator, li::ObjectFRefMap::iterator> frefs = li_->m_creatureFRefs.equal_range(_unit->GetLowGUID()); for(; frefs.first != frefs.second; ++frefs.first) cleanup_varparam(frefs.first->second, li_->lu); li_->m_creatureFRefs.erase(_unit->GetLowGUID()); } delete this; }