From 950b8e156418fc6d5225d4883a0586d67b6a6e12 Mon Sep 17 00:00:00 2001 From: Emagi Date: Mon, 2 Sep 2024 09:19:47 -0400 Subject: [PATCH] revert and check if "remove" function exists in script --- source/WorldServer/LuaInterface.cpp | 65 ++++++++++++++++------------- source/WorldServer/LuaInterface.h | 3 +- source/WorldServer/SpellProcess.cpp | 5 +-- 3 files changed, 38 insertions(+), 35 deletions(-) diff --git a/source/WorldServer/LuaInterface.cpp b/source/WorldServer/LuaInterface.cpp index 32dc600..111bdcd 100644 --- a/source/WorldServer/LuaInterface.cpp +++ b/source/WorldServer/LuaInterface.cpp @@ -288,8 +288,7 @@ bool LuaInterface::LoadLuaSpell(const char* name) { spell->restored = false; spell->initial_caster_char_id = 0; spell->initial_target_char_id = 0; - spell->spell_deleting = false; - + MSpells.lock(); if (spells.count(lua_script) > 0) { @@ -855,36 +854,43 @@ lua_State* LuaInterface::LoadLuaFile(const char* name) { void LuaInterface::RemoveSpell(LuaSpell* spell, bool call_remove_function, bool can_delete, string reason, bool removing_all_spells) { if(call_remove_function){ lua_getglobal(spell->state, "remove"); - LUASpawnWrapper* spawn_wrapper = new LUASpawnWrapper(); - spawn_wrapper->spawn = spell->caster; - AddUserDataPtr(spawn_wrapper, spawn_wrapper->spawn); - lua_pushlightuserdata(spell->state, spawn_wrapper); - if(spell->caster && (spell->initial_target || spell->caster->GetTarget())){ - spawn_wrapper = new LUASpawnWrapper(); - if(!spell->initial_target) - spawn_wrapper->spawn = spell->caster->GetTarget(); - else if(spell->caster->GetZone()) { - spawn_wrapper->spawn = spell->caster->GetZone()->GetSpawnByID(spell->initial_target); - } - else { - spawn_wrapper->spawn = nullptr; // we need it set to something or else the ptr could be loose - } + if (!lua_isfunction(spell->state, lua_gettop(spell->state))){ + lua_pop(spell->state, 1); + } + else { + lua_getglobal(spell->state, "remove"); + LUASpawnWrapper* spawn_wrapper = new LUASpawnWrapper(); + spawn_wrapper->spawn = spell->caster; AddUserDataPtr(spawn_wrapper, spawn_wrapper->spawn); lua_pushlightuserdata(spell->state, spawn_wrapper); + if(spell->caster && (spell->initial_target || spell->caster->GetTarget())){ + spawn_wrapper = new LUASpawnWrapper(); + if(!spell->initial_target) + spawn_wrapper->spawn = spell->caster->GetTarget(); + else if(spell->caster->GetZone()) { + spawn_wrapper->spawn = spell->caster->GetZone()->GetSpawnByID(spell->initial_target); + } + else { + spawn_wrapper->spawn = nullptr; // we need it set to something or else the ptr could be loose + } + AddUserDataPtr(spawn_wrapper, spawn_wrapper->spawn); + lua_pushlightuserdata(spell->state, spawn_wrapper); + } + else + lua_pushlightuserdata(spell->state, 0); + + if (spell->caster && !spell->caster->Alive()) + reason = "dead"; + + lua_pushstring(spell->state, (char*)reason.c_str()); + + MSpells.lock(); + current_spells[spell->state] = spell; + MSpells.unlock(); + + lua_pcall(spell->state, 3, 0, 0); + ResetFunctionStack(spell->state); } - else - lua_pushlightuserdata(spell->state, 0); - - if (spell->caster && !spell->caster->Alive()) - reason = "dead"; - - lua_pushstring(spell->state, (char*)reason.c_str()); - - MSpells.lock(); - current_spells[spell->state] = spell; - MSpells.unlock(); - lua_pcall(spell->state, 3, 0, 0); - ResetFunctionStack(spell->state); } spell->MSpellTargets.readlock(__FUNCTION__, __LINE__); @@ -2079,7 +2085,6 @@ LuaSpell* LuaInterface::GetSpell(const char* name) { new_spell->restored = false; new_spell->initial_caster_char_id = 0; new_spell->initial_target_char_id = 0; - new_spell->spell_deleting = false; return new_spell; } else{ diff --git a/source/WorldServer/LuaInterface.h b/source/WorldServer/LuaInterface.h index b51afb7..8bd5966 100644 --- a/source/WorldServer/LuaInterface.h +++ b/source/WorldServer/LuaInterface.h @@ -22,7 +22,6 @@ #include #include -#include #include "Spawn.h" #include "Spells.h" @@ -103,7 +102,7 @@ struct LuaSpell{ Mutex MSpellTargets; int32 effect_bitmask; bool restored; // restored spell cross zone - std::atomic spell_deleting; + }; class LUAUserData{ diff --git a/source/WorldServer/SpellProcess.cpp b/source/WorldServer/SpellProcess.cpp index a9d9d7d..3ee9ca0 100644 --- a/source/WorldServer/SpellProcess.cpp +++ b/source/WorldServer/SpellProcess.cpp @@ -414,12 +414,11 @@ bool SpellProcess::DeleteCasterSpell(LuaSpell* spell, string reason, bool removi if(shared_lock_spell && !removing_all_spells) { MSpellProcess.lock_shared(); } - + bool ret = false; Spawn* target = 0; bool target_valid = false; - if(spell && !spell->spell_deleting) { - spell->spell_deleting = true; + if(spell) { spell->MSpellTargets.writelock(__FUNCTION__, __LINE__); if(remove_target && spell->targets.size() > 1) { for (int32 i = 0; i < spell->targets.size(); i++) {