From 38c516c527c5cec3aa02d08c2bc5372acb8b9d6b Mon Sep 17 00:00:00 2001 From: Rochet2 Date: Thu, 24 Jul 2014 23:37:42 +0300 Subject: [PATCH] Eluna add boost support and fix warnings --- CMakeLists.txt | 4 ++ CreatureMethods.h | 8 ++-- ElunaTemplate.h | 2 +- LuaEngine.cpp | 117 ++++++++++++++++++++++++++++++++-------------- LuaEngine.h | 1 + LuaFunctions.cpp | 2 +- PlayerMethods.h | 16 +++++++ UnitMethods.h | 2 +- 8 files changed, 111 insertions(+), 41 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 8295dd5..bb3aaf3 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -119,10 +119,13 @@ include_directories( ${CMAKE_SOURCE_DIR}/src/server/game/World ${CMAKE_SOURCE_DIR}/src/server/scripts/PrecompiledHeaders ${ACE_INCLUDE_DIR} + ${Boost_LIBRARIES} ${MYSQL_INCLUDE_DIR} ${OPENSSL_INCLUDE_DIR} ) +find_package(Boost COMPONENTS system filesystem REQUIRED) + add_library(LuaEngine STATIC ${LuaEngine_STAT_SRCS} ${game_STAT_SRCS} @@ -131,6 +134,7 @@ add_library(LuaEngine STATIC target_link_libraries(LuaEngine game + ${Boost_LIBRARIES} ) add_dependencies(LuaEngine game) diff --git a/CreatureMethods.h b/CreatureMethods.h index 692aaf3..84e020e 100644 --- a/CreatureMethods.h +++ b/CreatureMethods.h @@ -38,14 +38,14 @@ namespace LuaCreature return 1; } - int IsTargetAcceptable(lua_State* L, Creature* creature) + int IsTargetableForAttack(lua_State* L, Creature* creature) { - Unit* target = Eluna::CHECKOBJ(L, 2); + bool inversAlive = Eluna::CHECKOBJ(L, 2); #ifdef MANGOS - Eluna::Push(L, creature->IsTargetableForAttack(target)); + Eluna::Push(L, creature->IsTargetableForAttack(inversAlive)); #else - Eluna::Push(L, creature->isTargetableForAttack(target)); + Eluna::Push(L, creature->isTargetableForAttack(inversAlive)); #endif return 1; } diff --git a/ElunaTemplate.h b/ElunaTemplate.h index ffa3461..a10e593 100644 --- a/ElunaTemplate.h +++ b/ElunaTemplate.h @@ -205,7 +205,7 @@ public: lua_pushfstring(L, "%p", *ptrHold); lua_gettable(L, -2); lua_remove(L, -2); - bool valid = lua_isuserdata(L, -1); + bool valid = lua_isuserdata(L, -1) != 0; lua_remove(L, -1); if (!valid) { diff --git a/LuaEngine.cpp b/LuaEngine.cpp index 9822da5..746b521 100644 --- a/LuaEngine.cpp +++ b/LuaEngine.cpp @@ -4,9 +4,6 @@ * Please see the included DOCS/LICENSE.md for more information */ -#include -#include -#include #include "HookMgr.h" #include "LuaEngine.h" #include "ElunaBinding.h" @@ -15,6 +12,16 @@ #include "ElunaTemplate.h" #include "ElunaUtility.h" +// Some dummy includes containing BOOST_VERSION: +// ObjectAccessor.h Config.h Log.h +#ifdef BOOST_VERSION +#include +#else +#include +#include +#include +#endif + extern "C" { #include "lua.h" @@ -70,12 +77,15 @@ void Eluna::ReloadEluna() #ifdef TRINITY // Re initialize creature AI restoring C++ AI or applying lua AI { +#ifdef BOOST_VERSION + boost::shared_lock lock(*HashMapHolder::GetLock()); +#else TRINITY_READ_GUARD(HashMapHolder::LockType, *HashMapHolder::GetLock()); +#endif HashMapHolder::MapType const& m = ObjectAccessor::GetCreatures(); for (HashMapHolder::MapType::const_iterator iter = m.begin(); iter != m.end(); ++iter) - { - iter->second->AIM_Initialize(); - } + if (iter->second->IsInWorld()) + iter->second->AIM_Initialize(); } #endif @@ -150,11 +160,66 @@ Eluna::~Eluna() lua_close(L); } +void Eluna::AddScriptPath(std::string filename, std::string fullpath, ScriptList& scripts) +{ + ELUNA_LOG_DEBUG("[Eluna]: AddScriptPath Checking file `%s`", fullpath.c_str()); + + // split file name + std::size_t extDot = filename.find_last_of('.'); + if (extDot == std::string::npos) + return; + std::string ext = filename.substr(extDot); + filename = filename.substr(0, extDot); + + // check extension and add path to scripts to load + bool luascript = ext == ".lua" || ext == ".dll"; + bool extension = ext == ".ext" || (filename.length() >= 4 && filename.find_last_of("_ext") == filename.length() - 4); + if (!luascript && !extension) + return; + + LuaScript script; + script.fileext = ext; + script.filename = filename; + script.filepath = fullpath; + script.modulepath = fullpath.substr(0, fullpath.length() - ext.length()); + if (extension) + lua_extensions.push_back(script); + else + scripts.push_back(script); + ELUNA_LOG_DEBUG("[Eluna]: GetScripts add path `%s`", fullpath.c_str()); +} + // Finds lua script files from given path (including subdirectories) and pushes them to scripts void Eluna::GetScripts(std::string path, ScriptList& scripts) { ELUNA_LOG_DEBUG("[Eluna]: GetScripts from path `%s`", path.c_str()); +#ifdef BOOST_VERSION + boost::filesystem::path someDir(path); + boost::filesystem::directory_iterator end_iter; + + if (boost::filesystem::exists(someDir) && boost::filesystem::is_directory(someDir)) + { + for (boost::filesystem::directory_iterator dir_iter(someDir); dir_iter != end_iter; ++dir_iter) + { + std::string fullpath = dir_iter->path().generic_string(); + + // load subfolder + if (boost::filesystem::is_directory(dir_iter->status())) + { + GetScripts(fullpath, scripts); + continue; + } + + if (boost::filesystem::is_regular_file(dir_iter->status())) + { + // was file, try add + std::string filename = dir_iter->path().filename().generic_string(); + AddScriptPath(filename, fullpath, scripts); + } + } + } +#else ACE_Dirent dir; if (dir.open(path.c_str()) == -1) { @@ -183,34 +248,16 @@ void Eluna::GetScripts(std::string path, ScriptList& scripts) continue; } - // was file, check extension - ELUNA_LOG_DEBUG("[Eluna]: GetScripts Checking file `%s`", fullpath.c_str()); - - // split file name + // was file, try add std::string filename = directory->d_name; - std::size_t extDot = filename.find_last_of('.'); - if (extDot == std::string::npos) - continue; - std::string ext = filename.substr(extDot); - filename = filename.substr(0, extDot); - - // check extension and add path to scripts to load - bool luascript = ext == ".lua" || ext == ".dll"; - bool extension = ext == ".ext" || (filename.length() >= 4 && filename.find_last_of("_ext") == filename.length() - 4); - if (!luascript && !extension) - continue; - - LuaScript script; - script.fileext = ext; - script.filename = filename; - script.filepath = fullpath; - script.modulepath = fullpath.substr(0, fullpath.length() - ext.length()); - if (extension) - lua_extensions.push_back(script); - else - scripts.push_back(script); - ELUNA_LOG_DEBUG("[Eluna]: GetScripts add path `%s`", fullpath.c_str()); + AddScriptPath(filename, fullpath, scripts); } +#endif +} + +static bool ScriptpathComparator(const LuaScript& first, const LuaScript& second) +{ + return first.filepath.compare(second.filepath) < 0; } void Eluna::RunScripts() @@ -219,6 +266,8 @@ void Eluna::RunScripts() uint32 count = 0; ScriptList scripts; + lua_extensions.sort(ScriptpathComparator); + lua_scripts.sort(ScriptpathComparator); scripts.insert(scripts.end(), lua_extensions.begin(), lua_extensions.end()); scripts.insert(scripts.end(), lua_scripts.begin(), lua_scripts.end()); @@ -415,11 +464,11 @@ void Eluna::Push(lua_State* L, Object const* obj) } template<> bool Eluna::CHECKVAL(lua_State* L, int narg) { - return lua_isnumber(L, narg) ? luaL_optnumber(L, narg, 1) ? true : false : lua_toboolean(L, narg); + return lua_isnumber(L, narg) != 0 ? luaL_optnumber(L, narg, 1) ? true : false : lua_toboolean(L, narg) != 0; } template<> bool Eluna::CHECKVAL(lua_State* L, int narg, bool def) { - return lua_isnone(L, narg) ? def : lua_isnumber(L, narg) ? luaL_optnumber(L, narg, 1) ? true : false : lua_toboolean(L, narg); + return lua_isnone(L, narg) != 0 ? def : lua_isnumber(L, narg) != 0 ? luaL_optnumber(L, narg, 1) != 0 ? true : false : lua_toboolean(L, narg) != 0; } template<> float Eluna::CHECKVAL(lua_State* L, int narg) { diff --git a/LuaEngine.h b/LuaEngine.h index ed2b001..73f08e7 100644 --- a/LuaEngine.h +++ b/LuaEngine.h @@ -132,6 +132,7 @@ public: // This will be called on next update static void ReloadEluna(); static void GetScripts(std::string path, ScriptList& scripts); + static void AddScriptPath(std::string filename, std::string fullpath, ScriptList& scripts); static void report(lua_State*); static void ExecuteCall(lua_State* L, int params, int res); diff --git a/LuaFunctions.cpp b/LuaFunctions.cpp index 6424d23..004fb57 100644 --- a/LuaFunctions.cpp +++ b/LuaFunctions.cpp @@ -763,7 +763,7 @@ ElunaRegister CreatureMethods[] = { "IsTappedBy", &LuaCreature::IsTappedBy }, // :IsTappedBy(player) { "HasLootRecipient", &LuaCreature::HasLootRecipient }, // :HasLootRecipient() - Returns true if the creature has a loot recipient { "CanAssistTo", &LuaCreature::CanAssistTo }, // :CanAssistTo(unit, enemy[, checkfaction]) - Returns true if the creature can assist unit with enemy - { "IsTargetAcceptable", &LuaCreature::IsTargetAcceptable }, // :IsTargetAcceptable(unit) - Returns true if the creature can target unit + { "IsTargetableForAttack", &LuaCreature::IsTargetableForAttack }, // :IsTargetableForAttack([inversAlive]) - Returns true if the creature can be attacked { "HasInvolvedQuest", &LuaCreature::HasInvolvedQuest }, // :HasInvolvedQuest(questId) - Returns true if the creature can finish the quest for players { "IsRegeneratingHealth", &LuaCreature::IsRegeneratingHealth }, // :IsRegeneratingHealth() - Returns true if the creature is regenerating health { "IsReputationGainDisabled", &LuaCreature::IsReputationGainDisabled }, // :IsReputationGainDisabled() - Returns true if the creature has reputation gain disabled diff --git a/PlayerMethods.h b/PlayerMethods.h index 8b9363b..d4b96ec 100644 --- a/PlayerMethods.h +++ b/PlayerMethods.h @@ -1624,8 +1624,12 @@ namespace LuaPlayer { #ifdef CATA Eluna::Push(L, player->GetNextResetTalentsCost()); +#else +#ifdef TRINITY + Eluna::Push(L, player->ResetTalentsCost()); #else Eluna::Push(L, player->resetTalentsCost()); +#endif #endif return 1; } @@ -1636,9 +1640,13 @@ namespace LuaPlayer #ifdef CATA player->ResetTalents(no_cost); +#else +#ifdef TRINITY + player->ResetTalents(no_cost); #else player->resetTalents(no_cost); #endif +#endif #if (!defined(TBC) && !defined(CLASSIC)) player->SendTalentsInfoData(false); #endif @@ -1651,7 +1659,11 @@ namespace LuaPlayer bool disabled = Eluna::CHECKVAL(L, 3, false); bool learn_low_rank = Eluna::CHECKVAL(L, 4, true); +#ifdef TRINITY + player->RemoveSpell(entry, disabled, learn_low_rank); +#else player->removeSpell(entry, disabled, learn_low_rank); +#endif return 0; } @@ -2094,7 +2106,11 @@ namespace LuaPlayer { uint32 id = Eluna::CHECKVAL(L, 2); +#ifdef TRINITY + player->LearnSpell(id, false); +#else player->learnSpell(id, false); +#endif return 0; } diff --git a/UnitMethods.h b/UnitMethods.h index b9c9ebe..6a84495 100644 --- a/UnitMethods.h +++ b/UnitMethods.h @@ -1593,7 +1593,7 @@ namespace LuaUnit Unit* target = Eluna::CHECKOBJ(L, 2); uint32 spell = Eluna::CHECKVAL(L, 3); uint32 amount = Eluna::CHECKVAL(L, 4); - uint32 critical = Eluna::CHECKVAL(L, 5, false); + bool critical = Eluna::CHECKVAL(L, 5, false); #ifndef TRINITY if (const SpellInfo* info = sSpellStore.LookupEntry(spell))