diff options
145 files changed, 1191 insertions, 986 deletions
diff --git a/cmake/platform/win/settings.cmake b/cmake/platform/win/settings.cmake index 0c1a103304a..eb9b12990dd 100644 --- a/cmake/platform/win/settings.cmake +++ b/cmake/platform/win/settings.cmake @@ -1,4 +1,6 @@ add_definitions(-D_WIN32_WINNT=0x0601) +add_definitions(-DWIN32_LEAN_AND_MEAN) +add_definitions(-DNOMINMAX) if (CMAKE_CXX_COMPILER_ID STREQUAL "MSVC") include(${CMAKE_SOURCE_DIR}/cmake/compiler/msvc/settings.cmake) diff --git a/src/common/Collision/Management/MMapManager.cpp b/src/common/Collision/Management/MMapManager.cpp index 3e57c095eb1..6065763aa60 100644 --- a/src/common/Collision/Management/MMapManager.cpp +++ b/src/common/Collision/Management/MMapManager.cpp @@ -17,6 +17,7 @@ */ #include "MMapManager.h" +#include "Errors.h" #include "Log.h" #include "Config.h" #include "MMapFactory.h" diff --git a/src/common/Collision/VMapTools.h b/src/common/Collision/VMapTools.h deleted file mode 100644 index 41ce1039744..00000000000 --- a/src/common/Collision/VMapTools.h +++ /dev/null @@ -1,152 +0,0 @@ -/* - * Copyright (C) 2008-2017 TrinityCore <http://www.trinitycore.org/> - * Copyright (C) 2005-2010 MaNGOS <http://getmangos.com/> - * - * This program is free software; you can redistribute it and/or modify it - * under the terms of the GNU General Public License as published by the - * Free Software Foundation; either version 2 of the License, or (at your - * option) any later version. - * - * This program is distributed in the hope that it will be useful, but WITHOUT - * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or - * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for - * more details. - * - * You should have received a copy of the GNU General Public License along - * with this program. If not, see <http://www.gnu.org/licenses/>. - */ - -#ifndef _VMAPTOOLS_H -#define _VMAPTOOLS_H - -#include <G3D/CollisionDetection.h> -#include <G3D/AABox.h> - -#include "Define.h" - -#include "NodeValueAccess.h" - -/** -The Class is mainly taken from G3D/AABSPTree.h but modified to be able to use our internal data structure. -This is an iterator that helps us analysing the BSP-Trees. -The collision detection is modified to return true, if we are inside an object. -*/ - -namespace VMAP -{ - template<class TValue> - class IntersectionCallBack { - public: - TValue* closestEntity; - G3D::Vector3 hitLocation; - G3D::Vector3 hitNormal; - - void operator()(const G3D::Ray& ray, const TValue* entity, bool pStopAtFirstHit, float& distance) { - entity->intersect(ray, distance, pStopAtFirstHit, hitLocation, hitNormal); - } - }; - - //============================================================== - //============================================================== - //============================================================== - - class TC_COMMON_API MyCollisionDetection - { - private: - public: - - static bool collisionLocationForMovingPointFixedAABox( - const G3D::Vector3& origin, - const G3D::Vector3& dir, - const G3D::AABox& box, - G3D::Vector3& location, - bool& Inside) - { - - // Integer representation of a floating-point value. -#define IR(x) (reinterpret_cast<G3D::uint32 const&>(x)) - - Inside = true; - const G3D::Vector3& MinB = box.low(); - const G3D::Vector3& MaxB = box.high(); - G3D::Vector3 MaxT(-1.0f, -1.0f, -1.0f); - - // Find candidate planes. - for (int i = 0; i < 3; ++i) - { - if (origin[i] < MinB[i]) - { - location[i] = MinB[i]; - Inside = false; - - // Calculate T distances to candidate planes - if (IR(dir[i])) - { - MaxT[i] = (MinB[i] - origin[i]) / dir[i]; - } - } - else if (origin[i] > MaxB[i]) - { - location[i] = MaxB[i]; - Inside = false; - - // Calculate T distances to candidate planes - if (IR(dir[i])) - { - MaxT[i] = (MaxB[i] - origin[i]) / dir[i]; - } - } - } - - if (Inside) - { - // definite hit - location = origin; - return true; - } - - // Get largest of the maxT's for final choice of intersection - int WhichPlane = 0; - if (MaxT[1] > MaxT[WhichPlane]) - { - WhichPlane = 1; - } - - if (MaxT[2] > MaxT[WhichPlane]) - { - WhichPlane = 2; - } - - // Check final candidate actually inside box - if (IR(MaxT[WhichPlane]) & 0x80000000) - { - // Miss the box - return false; - } - - for (int i = 0; i < 3; ++i) - { - if (i != WhichPlane) - { - location[i] = origin[i] + MaxT[WhichPlane] * dir[i]; - if ((location[i] < MinB[i]) || - (location[i] > MaxB[i])) - { - // On this plane we're outside the box extents, so - // we miss the box - return false; - } - } - } - /* - // Choose the normal to be the plane normal facing into the ray - normal = G3D::Vector3::zero(); - normal[WhichPlane] = (dir[WhichPlane] > 0) ? -1.0 : 1.0; - */ - return true; - -#undef IR - } - }; -} -#endif diff --git a/src/common/Common.h b/src/common/Common.h index a37111fcaf9..8b681af3054 100644 --- a/src/common/Common.h +++ b/src/common/Common.h @@ -20,42 +20,11 @@ #define TRINITYCORE_COMMON_H #include "Define.h" - -#include <algorithm> -#include <array> -#include <exception> -#include <list> -#include <map> #include <memory> -#include <queue> -#include <set> -#include <sstream> #include <string> -#include <type_traits> -#include <unordered_map> -#include <unordered_set> -#include <vector> -#include <numeric> - -#include <cmath> -#include <cstdio> -#include <cstdlib> -#include <cstring> -#include <ctime> -#include <cerrno> -#include <csignal> - -#include <boost/optional.hpp> -#include <boost/utility/in_place_factory.hpp> -#include <boost/functional/hash.hpp> - -#include "Debugging/Errors.h" - -#include "Threading/LockedQueue.h" +#include <utility> #if TRINITY_PLATFORM == TRINITY_PLATFORM_WINDOWS -# include <ws2tcpip.h> - # if TRINITY_COMPILER == TRINITY_COMPILER_INTEL # if !defined(BOOST_ASIO_HAS_MOVE) # define BOOST_ASIO_HAS_MOVE @@ -73,8 +42,6 @@ #if TRINITY_COMPILER == TRINITY_COMPILER_MICROSOFT -#include <float.h> - #define snprintf _snprintf #define atoll _atoi64 #define vsnprintf _vsnprintf @@ -87,8 +54,6 @@ #endif -inline float finiteAlways(float f) { return std::isfinite(f) ? f : 0.0f; } - inline unsigned long atoul(char const* str) { return strtoul(str, nullptr, 10); } inline unsigned long long atoull(char const* str) { return strtoull(str, nullptr, 10); } @@ -139,9 +104,7 @@ const uint8 OLD_TOTAL_LOCALES = 9; /// @todo convert in simple system TC_COMMON_API extern char const* localeNames[TOTAL_LOCALES]; -TC_COMMON_API LocaleConstant GetLocaleByName(const std::string& name); - -typedef std::vector<std::string> StringVector; +TC_COMMON_API LocaleConstant GetLocaleByName(std::string const& name); #pragma pack(push, 1) @@ -152,7 +115,7 @@ struct TC_COMMON_API LocalizedString #pragma pack(pop) -// we always use stdlibc++ std::max/std::min, undefine some not C++ standard defines (Win API and some other platforms) +// we always use stdlib std::max/std::min, undefine some not C++ standard defines (Win API and some other platforms) #ifdef max #undef max #endif @@ -167,10 +130,6 @@ struct TC_COMMON_API LocalizedString #define MAX_QUERY_LEN 32*1024 -//! Optional helper class to wrap optional values within. -template <typename T> -using Optional = boost::optional<T>; - namespace Trinity { //! std::make_unique implementation (TODO: remove this once C++14 is supported) @@ -181,19 +140,4 @@ namespace Trinity } } -//! Hash implementation for std::pair to allow using pairs in unordered_set or as key for unordered_map -//! Individual types used in pair must be hashable by boost::hash -namespace std -{ - template<class K, class V> - struct hash<std::pair<K, V>> - { - public: - size_t operator()(std::pair<K, V> const& key) const - { - return boost::hash_value(key); - } - }; -} - #endif diff --git a/src/common/Configuration/BuiltInConfig.h b/src/common/Configuration/BuiltInConfig.h index cc21555f4bf..a818407fb74 100644 --- a/src/common/Configuration/BuiltInConfig.h +++ b/src/common/Configuration/BuiltInConfig.h @@ -18,8 +18,8 @@ #ifndef BUILT_IN_CONFIG_H #define BUILT_IN_CONFIG_H -#include <string> #include "Define.h" +#include <string> /// Provides helper functions to access built-in values /// which can be overwritten in config diff --git a/src/common/Configuration/Config.cpp b/src/common/Configuration/Config.cpp index be05916e805..d4e56716501 100644 --- a/src/common/Configuration/Config.cpp +++ b/src/common/Configuration/Config.cpp @@ -16,14 +16,23 @@ * with this program. If not, see <http://www.gnu.org/licenses/>. */ -#include <algorithm> -#include <mutex> -#include <boost/property_tree/ptree.hpp> -#include <boost/property_tree/ini_parser.hpp> #include "Config.h" #include "Log.h" +#include "Util.h" +#include <boost/property_tree/ini_parser.hpp> +#include <algorithm> +#include <memory> +#include <mutex> -using namespace boost::property_tree; +namespace bpt = boost::property_tree; + +namespace +{ + std::string _filename; + std::vector<std::string> _args; + bpt::ptree _config; + std::mutex _configLock; +} bool ConfigMgr::LoadInitial(std::string const& file, std::vector<std::string> args, std::string& error) @@ -35,8 +44,8 @@ bool ConfigMgr::LoadInitial(std::string const& file, std::vector<std::string> ar try { - ptree fullTree; - ini_parser::read_ini(file, fullTree); + bpt::ptree fullTree; + bpt::ini_parser::read_ini(file, fullTree); if (fullTree.empty()) { @@ -47,7 +56,7 @@ bool ConfigMgr::LoadInitial(std::string const& file, std::vector<std::string> ar // Since we're using only one section per config file, we skip the section and have direct property access _config = fullTree.begin()->second; } - catch (ini_parser::ini_parser_error const& e) + catch (bpt::ini_parser::ini_parser_error const& e) { if (e.line() == 0) error = e.message() + " (" + e.filename() + ")"; @@ -75,14 +84,14 @@ T ConfigMgr::GetValueDefault(std::string const& name, T def) const { try { - return _config.get<T>(ptree::path_type(name, '/')); + return _config.get<T>(bpt::ptree::path_type(name, '/')); } - catch (boost::property_tree::ptree_bad_path) + catch (bpt::ptree_bad_path) { TC_LOG_WARN("server.loading", "Missing name %s in config file %s, add \"%s = %s\" to this file", name.c_str(), _filename.c_str(), name.c_str(), std::to_string(def).c_str()); } - catch (boost::property_tree::ptree_bad_data) + catch (bpt::ptree_bad_data) { TC_LOG_ERROR("server.loading", "Bad value defined for name %s in config file %s, going to use %s instead", name.c_str(), _filename.c_str(), std::to_string(def).c_str()); @@ -96,14 +105,14 @@ std::string ConfigMgr::GetValueDefault<std::string>(std::string const& name, std { try { - return _config.get<std::string>(ptree::path_type(name, '/')); + return _config.get<std::string>(bpt::ptree::path_type(name, '/')); } - catch (boost::property_tree::ptree_bad_path) + catch (bpt::ptree_bad_path) { TC_LOG_WARN("server.loading", "Missing name %s in config file %s, add \"%s = %s\" to this file", name.c_str(), _filename.c_str(), name.c_str(), def.c_str()); } - catch (boost::property_tree::ptree_bad_data) + catch (bpt::ptree_bad_data) { TC_LOG_ERROR("server.loading", "Bad value defined for name %s in config file %s, going to use %s instead", name.c_str(), _filename.c_str(), def.c_str()); @@ -123,7 +132,7 @@ bool ConfigMgr::GetBoolDefault(std::string const& name, bool def) const { std::string val = GetValueDefault(name, std::string(def ? "1" : "0")); val.erase(std::remove(val.begin(), val.end(), '"'), val.end()); - return (val == "1" || val == "true" || val == "TRUE" || val == "yes" || val == "YES"); + return StringToBool(val); } int ConfigMgr::GetIntDefault(std::string const& name, int def) const @@ -142,13 +151,18 @@ std::string const& ConfigMgr::GetFilename() return _filename; } -std::list<std::string> ConfigMgr::GetKeysByString(std::string const& name) +std::vector<std::string> const& ConfigMgr::GetArguments() const +{ + return _args; +} + +std::vector<std::string> ConfigMgr::GetKeysByString(std::string const& name) { std::lock_guard<std::mutex> lock(_configLock); - std::list<std::string> keys; + std::vector<std::string> keys; - for (const ptree::value_type& child : _config) + for (bpt::ptree::value_type const& child : _config) if (child.first.compare(0, name.length(), name) == 0) keys.push_back(child.first); diff --git a/src/common/Configuration/Config.h b/src/common/Configuration/Config.h index 46b9bf73697..a503e419657 100644 --- a/src/common/Configuration/Config.h +++ b/src/common/Configuration/Config.h @@ -20,12 +20,8 @@ #define CONFIG_H #include "Define.h" - #include <string> -#include <list> #include <vector> -#include <mutex> -#include <boost/property_tree/ptree.hpp> class TC_COMMON_API ConfigMgr { @@ -36,8 +32,7 @@ class TC_COMMON_API ConfigMgr public: /// Method used only for loading main configuration files (bnetserver.conf and worldserver.conf) - bool LoadInitial(std::string const& file, std::vector<std::string> args, - std::string& error); + bool LoadInitial(std::string const& file, std::vector<std::string> args, std::string& error); static ConfigMgr* instance(); @@ -49,15 +44,10 @@ public: float GetFloatDefault(std::string const& name, float def) const; std::string const& GetFilename(); - std::vector<std::string> const& GetArguments() const { return _args; } - std::list<std::string> GetKeysByString(std::string const& name); + std::vector<std::string> const& GetArguments() const; + std::vector<std::string> GetKeysByString(std::string const& name); private: - std::string _filename; - std::vector<std::string> _args; - boost::property_tree::ptree _config; - std::mutex _configLock; - template<class T> T GetValueDefault(std::string const& name, T def) const; }; diff --git a/src/common/Cryptography/BigNumber.h b/src/common/Cryptography/BigNumber.h index c8ca5860933..8936ffffe53 100644 --- a/src/common/Cryptography/BigNumber.h +++ b/src/common/Cryptography/BigNumber.h @@ -19,8 +19,8 @@ #ifndef _AUTH_BIGNUMBER_H #define _AUTH_BIGNUMBER_H -#include <memory> #include "Define.h" +#include <memory> #include <string> struct bignum_st; diff --git a/src/common/Cryptography/HmacHash.cpp b/src/common/Cryptography/HmacHash.cpp index 8432ce3a322..1c38a7e0117 100644 --- a/src/common/Cryptography/HmacHash.cpp +++ b/src/common/Cryptography/HmacHash.cpp @@ -18,7 +18,8 @@ #include "HmacHash.h" #include "BigNumber.h" -#include "Common.h" +#include "Errors.h" +#include <cstring> template<HashCreateFn HashCreator, uint32 DigestLength> HmacHash<HashCreator, DigestLength>::HmacHash(uint32 len, uint8 const* seed) diff --git a/src/common/Cryptography/SessionKeyGeneration.h b/src/common/Cryptography/SessionKeyGeneration.h index 4507ae1aa0a..fdf468701bb 100644 --- a/src/common/Cryptography/SessionKeyGeneration.h +++ b/src/common/Cryptography/SessionKeyGeneration.h @@ -19,7 +19,8 @@ #ifndef SessionKeyGeneration_h__ #define SessionKeyGeneration_h__ -#include "Common.h" +#include "Define.h" +#include <cstring> template<class Hash> class SessionKeyGenerator diff --git a/src/common/DataStores/DB2FileLoader.cpp b/src/common/DataStores/DB2FileLoader.cpp index 4d12e2f67e1..0dc9edb19f0 100644 --- a/src/common/DataStores/DB2FileLoader.cpp +++ b/src/common/DataStores/DB2FileLoader.cpp @@ -21,6 +21,7 @@ #include "DB2Meta.h" #include "Errors.h" #include "Log.h" +#include <sstream> DB2FileLoadInfo::DB2FileLoadInfo() : Fields(nullptr), FieldCount(0), Meta(nullptr) { diff --git a/src/common/Debugging/WheatyExceptionReport.cpp b/src/common/Debugging/WheatyExceptionReport.cpp index 25d768654f6..46192f33d34 100644 --- a/src/common/Debugging/WheatyExceptionReport.cpp +++ b/src/common/Debugging/WheatyExceptionReport.cpp @@ -6,7 +6,9 @@ #include "CompilerDefs.h" #if TRINITY_PLATFORM == TRINITY_PLATFORM_WINDOWS && !defined(__MINGW32__) +#ifndef WIN32_LEAN_AND_MEAN #define WIN32_LEAN_AND_MEAN +#endif #pragma warning(disable:4996) #pragma warning(disable:4312) #pragma warning(disable:4311) @@ -21,6 +23,7 @@ #include "Common.h" #include "GitRevision.h" +#include <algorithm> #define CrashFolder _T("Crashes") #pragma comment(linker, "/DEFAULTLIB:dbghelp.lib") diff --git a/src/common/Define.h b/src/common/Define.h index 4e7cf03956e..56a5068e528 100644 --- a/src/common/Define.h +++ b/src/common/Define.h @@ -56,7 +56,7 @@ #endif #if TRINITY_PLATFORM == TRINITY_PLATFORM_WINDOWS -# define TRINITY_PATH_MAX MAX_PATH +# define TRINITY_PATH_MAX 260 # define _USE_MATH_DEFINES # ifndef DECLSPEC_NORETURN # define DECLSPEC_NORETURN __declspec(noreturn) diff --git a/src/common/GitRevision.cpp b/src/common/GitRevision.cpp index e0626a7df1d..96e31e5e6e5 100644 --- a/src/common/GitRevision.cpp +++ b/src/common/GitRevision.cpp @@ -1,3 +1,20 @@ +/* + * Copyright (C) 2008-2017 TrinityCore <http://www.trinitycore.org/> + * + * This program is free software; you can redistribute it and/or modify it + * under the terms of the GNU General Public License as published by the + * Free Software Foundation; either version 2 of the License, or (at your + * option) any later version. + * + * This program is distributed in the hope that it will be useful, but WITHOUT + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for + * more details. + * + * You should have received a copy of the GNU General Public License along + * with this program. If not, see <http://www.gnu.org/licenses/>. + */ + #include "GitRevision.h" #include "revision_data.h" diff --git a/src/common/GitRevision.h b/src/common/GitRevision.h index e69fcd6c812..a0a0fb85867 100644 --- a/src/common/GitRevision.h +++ b/src/common/GitRevision.h @@ -18,7 +18,6 @@ #ifndef __GITREVISION_H__ #define __GITREVISION_H__ -#include <string> #include "Define.h" namespace GitRevision diff --git a/src/common/Logging/Appender.cpp b/src/common/Logging/Appender.cpp index ec88d5fd4c1..4a995e5ab69 100644 --- a/src/common/Logging/Appender.cpp +++ b/src/common/Logging/Appender.cpp @@ -16,27 +16,10 @@ */ #include "Appender.h" -#include "Common.h" -#include "Util.h" +#include "LogMessage.h" #include "StringFormat.h" - -#include <utility> #include <sstream> -std::string LogMessage::getTimeStr(time_t time) -{ - tm aTm; - localtime_r(&time, &aTm); - char buf[20]; - snprintf(buf, 20, "%04d-%02d-%02d_%02d:%02d:%02d", aTm.tm_year+1900, aTm.tm_mon+1, aTm.tm_mday, aTm.tm_hour, aTm.tm_min, aTm.tm_sec); - return std::string(buf); -} - -std::string LogMessage::getTimeStr() -{ - return getTimeStr(mtime); -} - Appender::Appender(uint8 _id, std::string const& _name, LogLevel _level /* = LOG_LEVEL_DISABLED */, AppenderFlags _flags /* = APPENDER_FLAGS_NONE */): id(_id), name(_name), level(_level), flags(_flags) { } diff --git a/src/common/Logging/Appender.h b/src/common/Logging/Appender.h index 6a9932b7935..ff9d11f4713 100644 --- a/src/common/Logging/Appender.h +++ b/src/common/Logging/Appender.h @@ -18,72 +18,13 @@ #ifndef APPENDER_H #define APPENDER_H -#include <unordered_map> +#include "Define.h" +#include "LogCommon.h" #include <stdexcept> #include <string> -#include <time.h> -#include <type_traits> #include <vector> -#include <utility> -#include "Define.h" - -// Values assigned have their equivalent in enum ACE_Log_Priority -enum LogLevel -{ - LOG_LEVEL_DISABLED = 0, - LOG_LEVEL_TRACE = 1, - LOG_LEVEL_DEBUG = 2, - LOG_LEVEL_INFO = 3, - LOG_LEVEL_WARN = 4, - LOG_LEVEL_ERROR = 5, - LOG_LEVEL_FATAL = 6 -}; - -const uint8 MaxLogLevels = 6; - -enum AppenderType : uint8 -{ - APPENDER_NONE, - APPENDER_CONSOLE, - APPENDER_FILE, - APPENDER_DB -}; - -enum AppenderFlags -{ - APPENDER_FLAGS_NONE = 0x00, - APPENDER_FLAGS_PREFIX_TIMESTAMP = 0x01, - APPENDER_FLAGS_PREFIX_LOGLEVEL = 0x02, - APPENDER_FLAGS_PREFIX_LOGFILTERTYPE = 0x04, - APPENDER_FLAGS_USE_TIMESTAMP = 0x08, // only used by FileAppender - APPENDER_FLAGS_MAKE_FILE_BACKUP = 0x10 // only used by FileAppender -}; - -struct TC_COMMON_API LogMessage -{ - LogMessage(LogLevel _level, std::string const& _type, std::string&& _text) - : level(_level), type(_type), text(std::forward<std::string>(_text)), mtime(time(NULL)) - { } - LogMessage(LogMessage const& /*other*/) = delete; - LogMessage& operator=(LogMessage const& /*other*/) = delete; - - static std::string getTimeStr(time_t time); - std::string getTimeStr(); - - LogLevel const level; - std::string const type; - std::string const text; - std::string prefix; - std::string param1; - time_t mtime; - - ///@ Returns size of the log message content in bytes - uint32 Size() const - { - return static_cast<uint32>(prefix.size() + text.size()); - } -}; +struct LogMessage; class TC_COMMON_API Appender { @@ -111,18 +52,6 @@ class TC_COMMON_API Appender AppenderFlags flags; }; -typedef std::unordered_map<uint8, Appender*> AppenderMap; - -typedef std::vector<char const*> ExtraAppenderArgs; -typedef Appender*(*AppenderCreatorFn)(uint8 id, std::string const& name, LogLevel level, AppenderFlags flags, ExtraAppenderArgs extraArgs); -typedef std::unordered_map<uint8, AppenderCreatorFn> AppenderCreatorMap; - -template<class AppenderImpl> -Appender* CreateAppender(uint8 id, std::string const& name, LogLevel level, AppenderFlags flags, ExtraAppenderArgs extraArgs) -{ - return new AppenderImpl(id, name, level, flags, std::forward<ExtraAppenderArgs>(extraArgs)); -} - class TC_COMMON_API InvalidAppenderArgsException : public std::length_error { public: diff --git a/src/common/Logging/AppenderConsole.cpp b/src/common/Logging/AppenderConsole.cpp index 3378c185cc5..7ae1987162e 100644 --- a/src/common/Logging/AppenderConsole.cpp +++ b/src/common/Logging/AppenderConsole.cpp @@ -15,20 +15,19 @@ * with this program. If not, see <http://www.gnu.org/licenses/>. */ -#include <sstream> - #include "AppenderConsole.h" -#include "Config.h" +#include "LogMessage.h" #include "Util.h" +#include <sstream> #if TRINITY_PLATFORM == TRINITY_PLATFORM_WINDOWS #include <Windows.h> #endif -AppenderConsole::AppenderConsole(uint8 id, std::string const& name, LogLevel level, AppenderFlags flags, ExtraAppenderArgs extraArgs) +AppenderConsole::AppenderConsole(uint8 id, std::string const& name, LogLevel level, AppenderFlags flags, std::vector<char const*> extraArgs) : Appender(id, name, level, flags), _colored(false) { - for (uint8 i = 0; i < MaxLogLevels; ++i) + for (uint8 i = 0; i < NUM_ENABLED_LOG_LEVELS; ++i) _colors[i] = ColorTypes(MaxColors); if (!extraArgs.empty()) @@ -43,11 +42,11 @@ void AppenderConsole::InitColors(std::string const& str) return; } - int color[MaxLogLevels]; + int color[NUM_ENABLED_LOG_LEVELS]; std::istringstream ss(str); - for (uint8 i = 0; i < MaxLogLevels; ++i) + for (uint8 i = 0; i < NUM_ENABLED_LOG_LEVELS; ++i) { ss >> color[i]; @@ -58,7 +57,7 @@ void AppenderConsole::InitColors(std::string const& str) return; } - for (uint8 i = 0; i < MaxLogLevels; ++i) + for (uint8 i = 0; i < NUM_ENABLED_LOG_LEVELS; ++i) _colors[i] = ColorTypes(color[i]); _colored = true; diff --git a/src/common/Logging/AppenderConsole.h b/src/common/Logging/AppenderConsole.h index c56a745bfba..9fd57c99f8a 100644 --- a/src/common/Logging/AppenderConsole.h +++ b/src/common/Logging/AppenderConsole.h @@ -18,7 +18,6 @@ #ifndef APPENDERCONSOLE_H #define APPENDERCONSOLE_H -#include <string> #include "Appender.h" enum ColorTypes @@ -47,7 +46,7 @@ class TC_COMMON_API AppenderConsole : public Appender public: typedef std::integral_constant<AppenderType, APPENDER_CONSOLE>::type TypeIndex; - AppenderConsole(uint8 _id, std::string const& name, LogLevel level, AppenderFlags flags, ExtraAppenderArgs extraArgs); + AppenderConsole(uint8 _id, std::string const& name, LogLevel level, AppenderFlags flags, std::vector<char const*> extraArgs); void InitColors(const std::string& init_str); AppenderType getType() const override { return TypeIndex::value; } @@ -56,7 +55,7 @@ class TC_COMMON_API AppenderConsole : public Appender void ResetColor(bool stdout_stream); void _write(LogMessage const* message) override; bool _colored; - ColorTypes _colors[MaxLogLevels]; + ColorTypes _colors[NUM_ENABLED_LOG_LEVELS]; }; #endif diff --git a/src/common/Logging/AppenderFile.cpp b/src/common/Logging/AppenderFile.cpp index a1595594a36..94a586649f4 100644 --- a/src/common/Logging/AppenderFile.cpp +++ b/src/common/Logging/AppenderFile.cpp @@ -16,15 +16,11 @@ */ #include "AppenderFile.h" -#include "Common.h" -#include "StringFormat.h" #include "Log.h" +#include "LogMessage.h" +#include <algorithm> -#if TRINITY_PLATFORM == TRINITY_PLATFORM_WINDOWS -# include <Windows.h> -#endif - -AppenderFile::AppenderFile(uint8 id, std::string const& name, LogLevel level, AppenderFlags flags, ExtraAppenderArgs extraArgs) : +AppenderFile::AppenderFile(uint8 id, std::string const& name, LogLevel level, AppenderFlags flags, std::vector<char const*> extraArgs) : Appender(id, name, level, flags), logfile(NULL), _logDir(sLog->GetLogsDir()), diff --git a/src/common/Logging/AppenderFile.h b/src/common/Logging/AppenderFile.h index aeb1c2d8001..8e728cbdf4d 100644 --- a/src/common/Logging/AppenderFile.h +++ b/src/common/Logging/AppenderFile.h @@ -18,15 +18,15 @@ #ifndef APPENDERFILE_H #define APPENDERFILE_H -#include <atomic> #include "Appender.h" +#include <atomic> class TC_COMMON_API AppenderFile : public Appender { public: typedef std::integral_constant<AppenderType, APPENDER_FILE>::type TypeIndex; - AppenderFile(uint8 id, std::string const& name, LogLevel level, AppenderFlags flags, ExtraAppenderArgs extraArgs); + AppenderFile(uint8 id, std::string const& name, LogLevel level, AppenderFlags flags, std::vector<char const*> extraArgs); ~AppenderFile(); FILE* OpenFile(std::string const& name, std::string const& mode, bool backup); AppenderType getType() const override { return TypeIndex::value; } diff --git a/src/common/Logging/Log.cpp b/src/common/Logging/Log.cpp index 691352aacbe..3bb9edaa9a1 100644 --- a/src/common/Logging/Log.cpp +++ b/src/common/Logging/Log.cpp @@ -17,14 +17,17 @@ */ #include "Log.h" -#include "Common.h" -#include "Config.h" -#include "Util.h" #include "AppenderConsole.h" #include "AppenderFile.h" +#include "AsioHacksImpl.h" +#include "Common.h" +#include "Config.h" +#include "Errors.h" +#include "Logger.h" +#include "LogMessage.h" #include "LogOperation.h" - -#include <cstdio> +#include "Util.h" +#include <chrono> #include <sstream> Log::Log() : AppenderId(0), lowestLogLevel(LOG_LEVEL_FATAL), _ioService(nullptr), _strand(nullptr) @@ -45,25 +48,13 @@ uint8 Log::NextAppenderId() return AppenderId++; } -int32 GetConfigIntDefault(std::string base, const char* name, int32 value) -{ - base.append(name); - return sConfigMgr->GetIntDefault(base.c_str(), value); -} - -std::string GetConfigStringDefault(std::string base, const char* name, const char* value) -{ - base.append(name); - return sConfigMgr->GetStringDefault(base.c_str(), value); -} - Appender* Log::GetAppenderByName(std::string const& name) { - AppenderMap::iterator it = appenders.begin(); + auto it = appenders.begin(); while (it != appenders.end() && it->second && it->second->getName() != name) ++it; - return it == appenders.end() ? NULL : it->second; + return it == appenders.end() ? nullptr : it->second.get(); } void Log::CreateAppenderFromConfig(std::string const& appenderName) @@ -77,7 +68,7 @@ void Log::CreateAppenderFromConfig(std::string const& appenderName) std::string options = sConfigMgr->GetStringDefault(appenderName.c_str(), ""); Tokenizer tokens(options, ','); - Tokenizer::const_iterator iter = tokens.begin(); + auto iter = tokens.begin(); size_t size = tokens.size(); std::string name = appenderName.substr(9); @@ -110,8 +101,8 @@ void Log::CreateAppenderFromConfig(std::string const& appenderName) try { - Appender* appender = factoryFunction->second(NextAppenderId(), name, level, flags, ExtraAppenderArgs(iter, tokens.end())); - appenders[appender->getId()] = appender; + Appender* appender = factoryFunction->second(NextAppenderId(), name, level, flags, std::vector<char const*>(iter, tokens.end())); + appenders[appender->getId()].reset(appender); } catch (InvalidAppenderArgsException const& iaae) { @@ -145,8 +136,8 @@ void Log::CreateLoggerFromConfig(std::string const& appenderName) return; } - Logger& logger = loggers[name]; - if (!logger.getName().empty()) + std::unique_ptr<Logger>& logger = loggers[name]; + if (logger) { fprintf(stderr, "Error while configuring Logger %s. Already defined\n", name.c_str()); return; @@ -162,7 +153,7 @@ void Log::CreateLoggerFromConfig(std::string const& appenderName) if (level < lowestLogLevel) lowestLogLevel = level; - logger.Create(name, level); + logger = Trinity::make_unique<Logger>(name, level); //fprintf(stdout, "Log::CreateLoggerFromConfig: Created Logger %s, Level %u\n", name.c_str(), level); std::istringstream ss(*iter); @@ -173,7 +164,7 @@ void Log::CreateLoggerFromConfig(std::string const& appenderName) { if (Appender* appender = GetAppenderByName(str)) { - logger.addAppender(appender->getId(), appender); + logger->addAppender(appender->getId(), appender); //fprintf(stdout, "Log::CreateLoggerFromConfig: Added Appender %s to Logger %s\n", appender->getName().c_str(), name.c_str()); } else @@ -184,24 +175,16 @@ void Log::CreateLoggerFromConfig(std::string const& appenderName) void Log::ReadAppendersFromConfig() { - std::list<std::string> keys = sConfigMgr->GetKeysByString("Appender."); - - while (!keys.empty()) - { - CreateAppenderFromConfig(keys.front()); - keys.pop_front(); - } + std::vector<std::string> keys = sConfigMgr->GetKeysByString("Appender."); + for (std::string const& appenderName : keys) + CreateAppenderFromConfig(appenderName); } void Log::ReadLoggersFromConfig() { - std::list<std::string> keys = sConfigMgr->GetKeysByString("Logger."); - - while (!keys.empty()) - { - CreateLoggerFromConfig(keys.front()); - keys.pop_front(); - } + std::vector<std::string> keys = sConfigMgr->GetKeysByString("Logger."); + for (std::string const& loggerName : keys) + CreateLoggerFromConfig(loggerName); // Bad config configuration, creating default config if (loggers.find(LOGGER_ROOT) == loggers.end()) @@ -211,26 +194,43 @@ void Log::ReadLoggersFromConfig() Close(); // Clean any Logger or Appender created - AppenderConsole* appender = new AppenderConsole(NextAppenderId(), "Console", LOG_LEVEL_DEBUG, APPENDER_FLAGS_NONE, ExtraAppenderArgs()); - appenders[appender->getId()] = appender; + AppenderConsole* appender = new AppenderConsole(NextAppenderId(), "Console", LOG_LEVEL_DEBUG, APPENDER_FLAGS_NONE, std::vector<char const*>()); + appenders[appender->getId()].reset(appender); - Logger& rootLogger = loggers[LOGGER_ROOT]; - rootLogger.Create(LOGGER_ROOT, LOG_LEVEL_ERROR); - rootLogger.addAppender(appender->getId(), appender); + Logger* rootLogger = new Logger(LOGGER_ROOT, LOG_LEVEL_ERROR); + rootLogger->addAppender(appender->getId(), appender); + loggers[LOGGER_ROOT].reset(rootLogger); - Logger& serverLogger = loggers["server"]; - serverLogger.Create("server", LOG_LEVEL_INFO); - serverLogger.addAppender(appender->getId(), appender); + Logger* serverLogger = new Logger("server", LOG_LEVEL_INFO); + serverLogger->addAppender(appender->getId(), appender); + loggers["server"].reset(serverLogger); } } +void Log::RegisterAppender(uint8 index, AppenderCreatorFn appenderCreateFn) +{ + auto itr = appenderFactory.find(index); + ASSERT(itr == appenderFactory.end()); + appenderFactory[index] = appenderCreateFn; +} + +void Log::outMessage(std::string const& filter, LogLevel const level, std::string&& message) +{ + write(Trinity::make_unique<LogMessage>(level, filter, std::move(message))); +} + +void Log::outCommand(std::string&& message, std::string&& param1) +{ + write(Trinity::make_unique<LogMessage>(LOG_LEVEL_INFO, "commands.gm", std::move(message), std::move(param1))); +} + void Log::write(std::unique_ptr<LogMessage>&& msg) const { Logger const* logger = GetLoggerByType(msg->type); if (_ioService) { - auto logOperation = std::shared_ptr<LogOperation>(new LogOperation(logger, std::move(msg))); + auto logOperation = std::make_shared<LogOperation>(logger, std::move(msg)); _ioService->post(_strand->wrap([logOperation](){ logOperation->call(); })); } @@ -238,6 +238,23 @@ void Log::write(std::unique_ptr<LogMessage>&& msg) const logger->write(msg.get()); } +Logger const* Log::GetLoggerByType(std::string const& type) const +{ + auto it = loggers.find(type); + if (it != loggers.end()) + return it->second.get(); + + if (type == LOGGER_ROOT) + return NULL; + + std::string parentLogger = LOGGER_ROOT; + size_t found = type.find_last_of("."); + if (found != std::string::npos) + parentLogger = type.substr(0, found); + + return GetLoggerByType(parentLogger); +} + std::string Log::GetTimestampStr() { time_t tt = std::chrono::system_clock::to_time_t(std::chrono::system_clock::now()); @@ -272,14 +289,14 @@ bool Log::SetLogLevel(std::string const& name, const char* newLevelc, bool isLog if (isLogger) { - LoggerMap::iterator it = loggers.begin(); - while (it != loggers.end() && it->second.getName() != name) + auto it = loggers.begin(); + while (it != loggers.end() && it->second->getName() != name) ++it; if (it == loggers.end()) return false; - it->second.setLogLevel(newLevel); + it->second->setLogLevel(newLevel); if (newLevel != LOG_LEVEL_DISABLED && newLevel < lowestLogLevel) lowestLogLevel = newLevel; @@ -316,19 +333,34 @@ void Log::outCharDump(char const* str, uint32 accountId, uint64 guid, char const void Log::SetRealmId(uint32 id) { - for (AppenderMap::iterator it = appenders.begin(); it != appenders.end(); ++it) + for (auto it = appenders.begin(); it != appenders.end(); ++it) it->second->setRealmId(id); } void Log::Close() { loggers.clear(); - for (AppenderMap::iterator it = appenders.begin(); it != appenders.end(); ++it) - delete it->second; - appenders.clear(); } +bool Log::ShouldLog(std::string const& type, LogLevel level) const +{ + // TODO: Use cache to store "Type.sub1.sub2": "Type" equivalence, should + // Speed up in cases where requesting "Type.sub1.sub2" but only configured + // Logger "Type" + + // Don't even look for a logger if the LogLevel is lower than lowest log levels across all loggers + if (level < lowestLogLevel) + return false; + + Logger const* logger = GetLoggerByType(type); + if (!logger) + return false; + + LogLevel logLevel = logger->getLogLevel(); + return logLevel != LOG_LEVEL_DISABLED && logLevel <= level; +} + Log* Log::instance() { static Log instance; @@ -340,7 +372,7 @@ void Log::Initialize(boost::asio::io_service* ioService) if (ioService) { _ioService = ioService; - _strand = new boost::asio::strand(*ioService); + _strand = new Trinity::AsioStrand(*ioService); } LoadFromConfig(); diff --git a/src/common/Logging/Log.h b/src/common/Logging/Log.h index 218d03f804d..f5970fc045a 100644 --- a/src/common/Logging/Log.h +++ b/src/common/Logging/Log.h @@ -20,30 +20,46 @@ #define TRINITYCORE_LOG_H #include "Define.h" -#include "Appender.h" -#include "Logger.h" +#include "AsioHacksFwd.h" +#include "LogCommon.h" #include "StringFormat.h" -#include "Common.h" -#include <boost/asio/io_service.hpp> -#include <boost/asio/strand.hpp> - -#include <stdarg.h> -#include <unordered_map> -#include <string> #include <memory> +#include <unordered_map> +#include <vector> + +class Appender; +class Logger; +struct LogMessage; + +namespace boost +{ + namespace asio + { + class io_service; + } +} #define LOGGER_ROOT "root" -class TC_COMMON_API Log +typedef Appender*(*AppenderCreatorFn)(uint8 id, std::string const& name, LogLevel level, AppenderFlags flags, std::vector<char const*>&& extraArgs); + +template<class AppenderImpl> +Appender* CreateAppender(uint8 id, std::string const& name, LogLevel level, AppenderFlags flags, std::vector<char const*>&& extraArgs) { - typedef std::unordered_map<std::string, Logger> LoggerMap; + return new AppenderImpl(id, name, level, flags, std::forward<std::vector<char const*>>(extraArgs)); +} +class TC_COMMON_API Log +{ private: Log(); ~Log(); + Log(Log const&) = delete; + Log(Log&&) = delete; + Log& operator=(Log const&) = delete; + Log& operator=(Log&&) = delete; public: - static Log* instance(); void Initialize(boost::asio::io_service* ioService); @@ -56,8 +72,7 @@ class TC_COMMON_API Log template<typename Format, typename... Args> inline void outMessage(std::string const& filter, LogLevel const level, Format&& fmt, Args&&... args) { - write(Trinity::make_unique<LogMessage>(level, filter, - Trinity::StringFormat(std::forward<Format>(fmt), std::forward<Args>(args)...))); + outMessage(filter, level, Trinity::StringFormat(std::forward<Format>(fmt), std::forward<Args>(args)...)); } template<typename Format, typename... Args> @@ -66,13 +81,7 @@ class TC_COMMON_API Log if (!ShouldLog("commands.gm", LOG_LEVEL_INFO)) return; - std::unique_ptr<LogMessage> msg = - Trinity::make_unique<LogMessage>(LOG_LEVEL_INFO, "commands.gm", - Trinity::StringFormat(std::forward<Format>(fmt), std::forward<Args>(args)...)); - - msg->param1 = std::to_string(account); - - write(std::move(msg)); + outCommand(Trinity::StringFormat(std::forward<Format>(fmt), std::forward<Args>(args)...), std::to_string(account)); } void outCharDump(char const* str, uint32 account_id, uint64 guid, char const* name); @@ -83,9 +92,7 @@ class TC_COMMON_API Log void RegisterAppender() { using Index = typename AppenderImpl::TypeIndex; - auto itr = appenderFactory.find(Index::value); - ASSERT(itr == appenderFactory.end()); - appenderFactory[Index::value] = &CreateAppender<AppenderImpl>; + RegisterAppender(Index::value, &CreateAppender<AppenderImpl>); } std::string const& GetLogsDir() const { return m_logsDir; } @@ -102,10 +109,13 @@ class TC_COMMON_API Log void CreateLoggerFromConfig(std::string const& name); void ReadAppendersFromConfig(); void ReadLoggersFromConfig(); + void RegisterAppender(uint8 index, AppenderCreatorFn appenderCreateFn); + void outMessage(std::string const& filter, LogLevel const level, std::string&& message); + void outCommand(std::string&& message, std::string&& param1); - AppenderCreatorMap appenderFactory; - AppenderMap appenders; - LoggerMap loggers; + std::unordered_map<uint8, AppenderCreatorFn> appenderFactory; + std::unordered_map<uint8, std::unique_ptr<Appender>> appenders; + std::unordered_map<std::string, std::unique_ptr<Logger>> loggers; uint8 AppenderId; LogLevel lowestLogLevel; @@ -113,44 +123,9 @@ class TC_COMMON_API Log std::string m_logsTimestamp; boost::asio::io_service* _ioService; - boost::asio::strand* _strand; + Trinity::AsioStrand* _strand; }; -inline Logger const* Log::GetLoggerByType(std::string const& type) const -{ - LoggerMap::const_iterator it = loggers.find(type); - if (it != loggers.end()) - return &(it->second); - - if (type == LOGGER_ROOT) - return NULL; - - std::string parentLogger = LOGGER_ROOT; - size_t found = type.find_last_of("."); - if (found != std::string::npos) - parentLogger = type.substr(0,found); - - return GetLoggerByType(parentLogger); -} - -inline bool Log::ShouldLog(std::string const& type, LogLevel level) const -{ - // TODO: Use cache to store "Type.sub1.sub2": "Type" equivalence, should - // Speed up in cases where requesting "Type.sub1.sub2" but only configured - // Logger "Type" - - // Don't even look for a logger if the LogLevel is lower than lowest log levels across all loggers - if (level < lowestLogLevel) - return false; - - Logger const* logger = GetLoggerByType(type); - if (!logger) - return false; - - LogLevel logLevel = logger->getLogLevel(); - return logLevel != LOG_LEVEL_DISABLED && logLevel <= level; -} - #define sLog Log::instance() #define LOG_EXCEPTION_FREE(filterType__, level__, ...) \ diff --git a/src/common/Logging/LogCommon.h b/src/common/Logging/LogCommon.h new file mode 100644 index 00000000000..7579884bbb1 --- /dev/null +++ b/src/common/Logging/LogCommon.h @@ -0,0 +1,52 @@ +/* + * Copyright (C) 2008-2017 TrinityCore <http://www.trinitycore.org/> + * + * This program is free software; you can redistribute it and/or modify it + * under the terms of the GNU General Public License as published by the + * Free Software Foundation; either version 2 of the License, or (at your + * option) any later version. + * + * This program is distributed in the hope that it will be useful, but WITHOUT + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for + * more details. + * + * You should have received a copy of the GNU General Public License along + * with this program. If not, see <http://www.gnu.org/licenses/>. + */ + +#ifndef LogCommon_h__ +#define LogCommon_h__ + +enum LogLevel +{ + LOG_LEVEL_DISABLED = 0, + LOG_LEVEL_TRACE = 1, + LOG_LEVEL_DEBUG = 2, + LOG_LEVEL_INFO = 3, + LOG_LEVEL_WARN = 4, + LOG_LEVEL_ERROR = 5, + LOG_LEVEL_FATAL = 6, + + NUM_ENABLED_LOG_LEVELS = 6 +}; + +enum AppenderType : uint8 +{ + APPENDER_NONE, + APPENDER_CONSOLE, + APPENDER_FILE, + APPENDER_DB +}; + +enum AppenderFlags +{ + APPENDER_FLAGS_NONE = 0x00, + APPENDER_FLAGS_PREFIX_TIMESTAMP = 0x01, + APPENDER_FLAGS_PREFIX_LOGLEVEL = 0x02, + APPENDER_FLAGS_PREFIX_LOGFILTERTYPE = 0x04, + APPENDER_FLAGS_USE_TIMESTAMP = 0x08, // only used by FileAppender + APPENDER_FLAGS_MAKE_FILE_BACKUP = 0x10 // only used by FileAppender +}; + +#endif // LogCommon_h__ diff --git a/src/common/Logging/LogMessage.cpp b/src/common/Logging/LogMessage.cpp new file mode 100644 index 00000000000..85a5d14135e --- /dev/null +++ b/src/common/Logging/LogMessage.cpp @@ -0,0 +1,43 @@ +/* + * Copyright (C) 2008-2017 TrinityCore <http://www.trinitycore.org/> + * + * This program is free software; you can redistribute it and/or modify it + * under the terms of the GNU General Public License as published by the + * Free Software Foundation; either version 2 of the License, or (at your + * option) any later version. + * + * This program is distributed in the hope that it will be useful, but WITHOUT + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for + * more details. + * + * You should have received a copy of the GNU General Public License along + * with this program. If not, see <http://www.gnu.org/licenses/>. + */ + +#include "LogMessage.h" +#include "Util.h" + +LogMessage::LogMessage(LogLevel _level, std::string const& _type, std::string&& _text) + : level(_level), type(_type), text(std::forward<std::string>(_text)), mtime(time(nullptr)) +{ +} + +LogMessage::LogMessage(LogLevel _level, std::string const& _type, std::string&& _text, std::string&& _param1) + : level(_level), type(_type), text(std::forward<std::string>(_text)), param1(std::forward<std::string>(_param1)), mtime(time(nullptr)) +{ +} + +std::string LogMessage::getTimeStr(time_t time) +{ + tm aTm; + localtime_r(&time, &aTm); + char buf[20]; + snprintf(buf, 20, "%04d-%02d-%02d_%02d:%02d:%02d", aTm.tm_year + 1900, aTm.tm_mon + 1, aTm.tm_mday, aTm.tm_hour, aTm.tm_min, aTm.tm_sec); + return std::string(buf); +} + +std::string LogMessage::getTimeStr() +{ + return getTimeStr(mtime); +} diff --git a/src/common/Logging/LogMessage.h b/src/common/Logging/LogMessage.h new file mode 100644 index 00000000000..84f2a0579fa --- /dev/null +++ b/src/common/Logging/LogMessage.h @@ -0,0 +1,51 @@ +/* + * Copyright (C) 2008-2017 TrinityCore <http://www.trinitycore.org/> + * + * This program is free software; you can redistribute it and/or modify it + * under the terms of the GNU General Public License as published by the + * Free Software Foundation; either version 2 of the License, or (at your + * option) any later version. + * + * This program is distributed in the hope that it will be useful, but WITHOUT + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for + * more details. + * + * You should have received a copy of the GNU General Public License along + * with this program. If not, see <http://www.gnu.org/licenses/>. + */ + +#ifndef LogMessage_h__ +#define LogMessage_h__ + +#include "Define.h" +#include "LogCommon.h" +#include <string> +#include <ctime> + +struct TC_COMMON_API LogMessage +{ + LogMessage(LogLevel _level, std::string const& _type, std::string&& _text); + LogMessage(LogLevel _level, std::string const& _type, std::string&& _text, std::string&& _param1); + + LogMessage(LogMessage const& /*other*/) = delete; + LogMessage& operator=(LogMessage const& /*other*/) = delete; + + static std::string getTimeStr(time_t time); + std::string getTimeStr(); + + LogLevel const level; + std::string const type; + std::string const text; + std::string prefix; + std::string param1; + time_t mtime; + + ///@ Returns size of the log message content in bytes + uint32 Size() const + { + return static_cast<uint32>(prefix.size() + text.size()); + } +}; + +#endif // LogMessage_h__ diff --git a/src/common/Logging/LogOperation.cpp b/src/common/Logging/LogOperation.cpp index 81bb98b9cce..c3a3711cc49 100644 --- a/src/common/Logging/LogOperation.cpp +++ b/src/common/Logging/LogOperation.cpp @@ -17,6 +17,15 @@ #include "LogOperation.h" #include "Logger.h" +#include "LogMessage.h" + +LogOperation::LogOperation(Logger const* _logger, std::unique_ptr<LogMessage>&& _msg) : logger(_logger), msg(std::forward<std::unique_ptr<LogMessage>>(_msg)) +{ +} + +LogOperation::~LogOperation() +{ +} int LogOperation::call() { diff --git a/src/common/Logging/LogOperation.h b/src/common/Logging/LogOperation.h index a7230d1749d..ddf5ef1debc 100644 --- a/src/common/Logging/LogOperation.h +++ b/src/common/Logging/LogOperation.h @@ -18,8 +18,8 @@ #ifndef LOGOPERATION_H #define LOGOPERATION_H -#include <memory> #include "Define.h" +#include <memory> class Logger; struct LogMessage; @@ -27,11 +27,9 @@ struct LogMessage; class TC_COMMON_API LogOperation { public: - LogOperation(Logger const* _logger, std::unique_ptr<LogMessage>&& _msg) - : logger(_logger), msg(std::forward<std::unique_ptr<LogMessage>>(_msg)) - { } + LogOperation(Logger const* _logger, std::unique_ptr<LogMessage>&& _msg); - ~LogOperation() { } + ~LogOperation(); int call(); diff --git a/src/common/Logging/Logger.cpp b/src/common/Logging/Logger.cpp index 8e1f9c7ef9d..a3a72f15642 100644 --- a/src/common/Logging/Logger.cpp +++ b/src/common/Logging/Logger.cpp @@ -16,14 +16,10 @@ */ #include "Logger.h" +#include "Appender.h" +#include "LogMessage.h" -Logger::Logger(): name(""), level(LOG_LEVEL_DISABLED) { } - -void Logger::Create(std::string const& _name, LogLevel _level) -{ - name = _name; - level = _level; -} +Logger::Logger(std::string const& _name, LogLevel _level): name(_name), level(_level) { } std::string const& Logger::getName() const { @@ -58,7 +54,7 @@ void Logger::write(LogMessage* message) const return; } - for (AppenderMap::const_iterator it = appenders.begin(); it != appenders.end(); ++it) + for (auto it = appenders.begin(); it != appenders.end(); ++it) if (it->second) it->second->write(message); } diff --git a/src/common/Logging/Logger.h b/src/common/Logging/Logger.h index 1446062c22b..dabeb8ced3a 100644 --- a/src/common/Logging/Logger.h +++ b/src/common/Logging/Logger.h @@ -18,15 +18,19 @@ #ifndef LOGGER_H #define LOGGER_H -#include "Appender.h" +#include "Define.h" +#include "LogCommon.h" +#include <unordered_map> + +class Appender; +struct LogMessage; class TC_COMMON_API Logger { public: - Logger(); + Logger(std::string const& name, LogLevel level); - void Create(std::string const& name, LogLevel level); - void addAppender(uint8 type, Appender *); + void addAppender(uint8 type, Appender* appender); void delAppender(uint8 type); std::string const& getName() const; @@ -37,7 +41,7 @@ class TC_COMMON_API Logger private: std::string name; LogLevel level; - AppenderMap appenders; + std::unordered_map<uint8, Appender*> appenders; }; #endif diff --git a/src/common/Metric/Metric.cpp b/src/common/Metric/Metric.cpp index 7091dd0d376..963528c0627 100644 --- a/src/common/Metric/Metric.cpp +++ b/src/common/Metric/Metric.cpp @@ -1,27 +1,33 @@ /* -* Copyright (C) 2008-2017 TrinityCore <http://www.trinitycore.org/> -* -* This program is free software; you can redistribute it and/or modify it -* under the terms of the GNU General Public License as published by the -* Free Software Foundation; either version 2 of the License, or (at your -* option) any later version. -* -* This program is distributed in the hope that it will be useful, but WITHOUT -* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or -* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for -* more details. -* -* You should have received a copy of the GNU General Public License along -* with this program. If not, see <http://www.gnu.org/licenses/>. -*/ + * Copyright (C) 2008-2017 TrinityCore <http://www.trinitycore.org/> + * + * This program is free software; you can redistribute it and/or modify it + * under the terms of the GNU General Public License as published by the + * Free Software Foundation; either version 2 of the License, or (at your + * option) any later version. + * + * This program is distributed in the hope that it will be useful, but WITHOUT + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for + * more details. + * + * You should have received a copy of the GNU General Public License along + * with this program. If not, see <http://www.gnu.org/licenses/>. + */ #include "Metric.h" -#include "Log.h" +#include "AsioHacksImpl.h" +#include "Common.h" #include "Config.h" +#include "Log.h" #include "Util.h" +#include <boost/algorithm/string/replace.hpp> +#include <boost/asio/deadline_timer.hpp> +#include <boost/asio/ip/tcp.hpp> void Metric::Initialize(std::string const& realmName, boost::asio::io_service& ioService, std::function<void()> overallStatusLogger) { + _dataStream = Trinity::make_unique<boost::asio::ip::tcp::iostream>(); _realmName = FormatInfluxDBTagValue(realmName); _batchTimer = Trinity::make_unique<boost::asio::deadline_timer>(ioService); _overallStatusTimer = Trinity::make_unique<boost::asio::deadline_timer>(ioService); @@ -31,8 +37,9 @@ void Metric::Initialize(std::string const& realmName, boost::asio::io_service& i bool Metric::Connect() { - _dataStream.connect(_hostname, _port); - auto error = _dataStream.error(); + auto& stream = static_cast<boost::asio::ip::tcp::iostream&>(GetDataStream()); + stream.connect(_hostname, _port); + auto error = stream.error(); if (error) { TC_LOG_ERROR("metric", "Error connecting to '%s:%s', disabling Metric. Error message : %s", @@ -40,7 +47,7 @@ bool Metric::Connect() _enabled = false; return false; } - _dataStream.clear(); + stream.clear(); return true; } @@ -156,22 +163,22 @@ void Metric::SendBatch() return; } - if (!_dataStream.good() && !Connect()) + if (!GetDataStream().good() && !Connect()) return; - _dataStream << "POST " << "/write?db=" << _databaseName << " HTTP/1.1\r\n"; - _dataStream << "Host: " << _hostname << ":" << _port << "\r\n"; - _dataStream << "Accept: */*\r\n"; - _dataStream << "Content-Type: application/octet-stream\r\n"; - _dataStream << "Content-Transfer-Encoding: binary\r\n"; + GetDataStream() << "POST " << "/write?db=" << _databaseName << " HTTP/1.1\r\n"; + GetDataStream() << "Host: " << _hostname << ":" << _port << "\r\n"; + GetDataStream() << "Accept: */*\r\n"; + GetDataStream() << "Content-Type: application/octet-stream\r\n"; + GetDataStream() << "Content-Transfer-Encoding: binary\r\n"; - _dataStream << "Content-Length: " << std::to_string(batchedData.tellp()) << "\r\n\r\n"; - _dataStream << batchedData.rdbuf(); + GetDataStream() << "Content-Length: " << std::to_string(batchedData.tellp()) << "\r\n\r\n"; + GetDataStream() << batchedData.rdbuf(); std::string http_version; - _dataStream >> http_version; + GetDataStream() >> http_version; unsigned int status_code = 0; - _dataStream >> status_code; + GetDataStream() >> status_code; if (status_code != 204) { TC_LOG_ERROR("metric", "Error sending data, returned HTTP code: %u", status_code); @@ -179,14 +186,12 @@ void Metric::SendBatch() // Read and ignore the status description std::string status_description; - std::getline(_dataStream, status_description); + std::getline(GetDataStream(), status_description); // Read headers std::string header; - while (std::getline(_dataStream, header) && header != "\r") - { + while (std::getline(GetDataStream(), header) && header != "\r") if (header == "Connection: close\r") - _dataStream.close(); - } + static_cast<boost::asio::ip::tcp::iostream&>(GetDataStream()).close(); ScheduleSend(); } @@ -200,7 +205,7 @@ void Metric::ScheduleSend() } else { - _dataStream.close(); + static_cast<boost::asio::ip::tcp::iostream&>(GetDataStream()).close(); MetricData* data; // Clear the queue while (_queuedData.Dequeue(data)) @@ -228,8 +233,62 @@ void Metric::ScheduleOverallStatusLog() } } +std::string Metric::FormatInfluxDBValue(bool value) +{ + return value ? "t" : "f"; +} + +template<class T> +std::string Metric::FormatInfluxDBValue(T value) +{ + return std::to_string(value) + 'i'; +} + +std::string Metric::FormatInfluxDBValue(std::string const& value) +{ + return '"' + boost::replace_all_copy(value, "\"", "\\\"") + '"'; +} + +std::string Metric::FormatInfluxDBValue(const char* value) +{ + return FormatInfluxDBValue(std::string(value)); +} + +std::string Metric::FormatInfluxDBValue(double value) +{ + return std::to_string(value); +} + +std::string Metric::FormatInfluxDBValue(float value) +{ + return FormatInfluxDBValue(double(value)); +} + +std::string Metric::FormatInfluxDBTagValue(std::string const& value) +{ + // ToDo: should handle '=' and ',' characters too + return boost::replace_all_copy(value, " ", "\\ "); +} + +Metric::Metric() +{ +} + +Metric::~Metric() +{ +} + Metric* Metric::instance() { static Metric instance; return &instance; } + +template TC_COMMON_API std::string Metric::FormatInfluxDBValue(int8); +template TC_COMMON_API std::string Metric::FormatInfluxDBValue(uint8); +template TC_COMMON_API std::string Metric::FormatInfluxDBValue(int16); +template TC_COMMON_API std::string Metric::FormatInfluxDBValue(uint16); +template TC_COMMON_API std::string Metric::FormatInfluxDBValue(int32); +template TC_COMMON_API std::string Metric::FormatInfluxDBValue(uint32); +template TC_COMMON_API std::string Metric::FormatInfluxDBValue(int64); +template TC_COMMON_API std::string Metric::FormatInfluxDBValue(uint64); diff --git a/src/common/Metric/Metric.h b/src/common/Metric/Metric.h index 8e4d1292519..b54d1b758ef 100644 --- a/src/common/Metric/Metric.h +++ b/src/common/Metric/Metric.h @@ -1,28 +1,39 @@ /* -* Copyright (C) 2008-2017 TrinityCore <http://www.trinitycore.org/> -* -* This program is free software; you can redistribute it and/or modify it -* under the terms of the GNU General Public License as published by the -* Free Software Foundation; either version 2 of the License, or (at your -* option) any later version. -* -* This program is distributed in the hope that it will be useful, but WITHOUT -* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or -* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for -* more details. -* -* You should have received a copy of the GNU General Public License along -* with this program. If not, see <http://www.gnu.org/licenses/>. -*/ + * Copyright (C) 2008-2017 TrinityCore <http://www.trinitycore.org/> + * + * This program is free software; you can redistribute it and/or modify it + * under the terms of the GNU General Public License as published by the + * Free Software Foundation; either version 2 of the License, or (at your + * option) any later version. + * + * This program is distributed in the hope that it will be useful, but WITHOUT + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for + * more details. + * + * You should have received a copy of the GNU General Public License along + * with this program. If not, see <http://www.gnu.org/licenses/>. + */ #ifndef METRIC_H__ #define METRIC_H__ -#include "Common.h" -#include "Threading/MPSCQueue.h" -#include <boost/asio/ip/tcp.hpp> -#include <boost/algorithm/string.hpp> -#include <type_traits> +#include "Define.h" +#include "AsioHacksFwd.h" +#include "MPSCQueue.h" +#include <chrono> +#include <functional> +#include <iosfwd> +#include <memory> +#include <string> + +namespace boost +{ + namespace asio + { + class io_service; + } +} enum MetricDataType { @@ -33,7 +44,7 @@ enum MetricDataType struct MetricData { std::string Category; - std::chrono::time_point<std::chrono::system_clock> Timestamp; + std::chrono::system_clock::time_point Timestamp; MetricDataType Type; // LogValue-specific fields @@ -47,7 +58,8 @@ struct MetricData class TC_COMMON_API Metric { private: - boost::asio::ip::tcp::iostream _dataStream; + std::iostream& GetDataStream() { return *_dataStream; } + std::unique_ptr<std::iostream> _dataStream; MPSCQueue<MetricData> _queuedData; std::unique_ptr<boost::asio::deadline_timer> _batchTimer; std::unique_ptr<boost::asio::deadline_timer> _overallStatusTimer; @@ -66,31 +78,24 @@ private: void ScheduleSend(); void ScheduleOverallStatusLog(); - template<class T, typename std::enable_if<std::is_integral<T>::value>::type* = nullptr> - static std::string FormatInfluxDBValue(T value) { return std::to_string(value) + 'i'; } - - static std::string FormatInfluxDBValue(std::string const& value) - { - return '"' + boost::replace_all_copy(value, "\"", "\\\"") + '"'; - } - - static std::string FormatInfluxDBValue(bool value) { return value ? "t" : "f"; } - static std::string FormatInfluxDBValue(const char* value) { return FormatInfluxDBValue(std::string(value)); } - static std::string FormatInfluxDBValue(double value) { return std::to_string(value); } - static std::string FormatInfluxDBValue(float value) { return FormatInfluxDBValue(double(value)); } + static std::string FormatInfluxDBValue(bool value); + template<class T> + static std::string FormatInfluxDBValue(T value); + static std::string FormatInfluxDBValue(std::string const& value); + static std::string FormatInfluxDBValue(const char* value); + static std::string FormatInfluxDBValue(double value); + static std::string FormatInfluxDBValue(float value); - static std::string FormatInfluxDBTagValue(std::string const& value) - { - // ToDo: should handle '=' and ',' characters too - return boost::replace_all_copy(value, " ", "\\ "); - } + static std::string FormatInfluxDBTagValue(std::string const& value); // ToDo: should format TagKey and FieldKey too in the same way as TagValue public: + Metric(); + ~Metric(); static Metric* instance(); - void Initialize(std::string const& realmName, boost::asio::io_service& ioService, std::function<void()> overallStatusLogger = [](){}); + void Initialize(std::string const& realmName, boost::asio::io_service& ioService, std::function<void()> overallStatusLogger); void LoadFromConfigs(); void Update(); diff --git a/src/common/PrecompiledHeaders/commonPCH.h b/src/common/PrecompiledHeaders/commonPCH.h index 1d3f8928e82..fc9d8a0f6a7 100644 --- a/src/common/PrecompiledHeaders/commonPCH.h +++ b/src/common/PrecompiledHeaders/commonPCH.h @@ -1,10 +1,24 @@ +#include "BoundingIntervalHierarchy.h" +#include "Common.h" +#include "Config.h" #include "Define.h" -#include "VMapDefinitions.h" +#include "Errors.h" +#include "GitRevision.h" +#include "Log.h" +#include "LogMessage.h" #include "MapTree.h" -#include "WorldModel.h" #include "ModelInstance.h" -#include "BoundingIntervalHierarchy.h" -#include "RegularGrid.h" -#include "BoundingIntervalHierarchyWrapper.h" -#include "GameObjectModel.h" -#include "GitRevision.h" +#include "Util.h" +#include "VMapDefinitions.h" +#include "WorldModel.h" +#include <G3D/Ray.h> +#include <G3D/Vector3.h> +#include <algorithm> +#include <cstring> +#include <memory> +#include <mutex> +#include <set> +#include <sstream> +#include <string> +#include <unordered_map> +#include <vector> diff --git a/src/common/Threading/ProcessPriority.cpp b/src/common/Threading/ProcessPriority.cpp new file mode 100644 index 00000000000..da3cc83a544 --- /dev/null +++ b/src/common/Threading/ProcessPriority.cpp @@ -0,0 +1,95 @@ +/* + * Copyright (C) 2008-2017 TrinityCore <http://www.trinitycore.org/> + * + * This program is free software; you can redistribute it and/or modify it + * under the terms of the GNU General Public License as published by the + * Free Software Foundation; either version 2 of the License, or (at your + * option) any later version. + * + * This program is distributed in the hope that it will be useful, but WITHOUT + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for + * more details. + * + * You should have received a copy of the GNU General Public License along + * with this program. If not, see <http://www.gnu.org/licenses/>. + */ + +#include "ProcessPriority.h" +#include "Log.h" + +#ifdef _WIN32 // Windows +#include <Windows.h> +#elif defined(__linux__) +#include <sched.h> +#include <sys/resource.h> +#define PROCESS_HIGH_PRIORITY -15 // [-20, 19], default is 0 +#endif + +void SetProcessPriority(std::string const& logChannel, uint32 affinity, bool highPriority) +{ + ///- Handle affinity for multiple processors and process priority +#ifdef _WIN32 // Windows + + HANDLE hProcess = GetCurrentProcess(); + if (affinity > 0) + { + ULONG_PTR appAff; + ULONG_PTR sysAff; + + if (GetProcessAffinityMask(hProcess, &appAff, &sysAff)) + { + // remove non accessible processors + ULONG_PTR currentAffinity = affinity & appAff; + + if (!currentAffinity) + TC_LOG_ERROR(logChannel, "Processors marked in UseProcessors bitmask (hex) %x are not accessible. Accessible processors bitmask (hex): %x", affinity, appAff); + else if (SetProcessAffinityMask(hProcess, currentAffinity)) + TC_LOG_INFO(logChannel, "Using processors (bitmask, hex): %x", currentAffinity); + else + TC_LOG_ERROR(logChannel, "Can't set used processors (hex): %x", currentAffinity); + } + } + + if (highPriority) + { + if (SetPriorityClass(hProcess, HIGH_PRIORITY_CLASS)) + TC_LOG_INFO(logChannel, "Process priority class set to HIGH"); + else + TC_LOG_ERROR(logChannel, "Can't set process priority class."); + } + +#elif defined(__linux__) // Linux + + if (affinity > 0) + { + cpu_set_t mask; + CPU_ZERO(&mask); + + for (unsigned int i = 0; i < sizeof(affinity) * 8; ++i) + if (affinity & (1 << i)) + CPU_SET(i, &mask); + + if (sched_setaffinity(0, sizeof(mask), &mask)) + TC_LOG_ERROR(logChannel, "Can't set used processors (hex): %x, error: %s", affinity, strerror(errno)); + else + { + CPU_ZERO(&mask); + sched_getaffinity(0, sizeof(mask), &mask); + TC_LOG_INFO(logChannel, "Using processors (bitmask, hex): %lx", *(__cpu_mask*)(&mask)); + } + } + + if (highPriority) + { + if (setpriority(PRIO_PROCESS, 0, PROCESS_HIGH_PRIORITY)) + TC_LOG_ERROR(logChannel, "Can't set process priority class, error: %s", strerror(errno)); + else + TC_LOG_INFO(logChannel, "Process priority class set to %i", getpriority(PRIO_PROCESS, 0)); + } +#else + (void)logChannel; + (void)affinity; + (void)highPriority; +#endif +} diff --git a/src/common/Threading/ProcessPriority.h b/src/common/Threading/ProcessPriority.h index b4ff3f5ecf6..3cb866c7de1 100644 --- a/src/common/Threading/ProcessPriority.h +++ b/src/common/Threading/ProcessPriority.h @@ -18,86 +18,12 @@ #ifndef _PROCESSPRIO_H #define _PROCESSPRIO_H -#include "Configuration/Config.h" +#include "Define.h" +#include <string> -#ifdef __linux__ -#include <sched.h> -#include <sys/resource.h> -#define PROCESS_HIGH_PRIORITY -15 // [-20, 19], default is 0 -#endif - -void SetProcessPriority(const std::string& logChannel) -{ -#if defined(_WIN32) || defined(__linux__) - - ///- Handle affinity for multiple processors and process priority - uint32 affinity = sConfigMgr->GetIntDefault("UseProcessors", 0); - bool highPriority = sConfigMgr->GetBoolDefault("ProcessPriority", false); - -#ifdef _WIN32 // Windows - - HANDLE hProcess = GetCurrentProcess(); - if (affinity > 0) - { - ULONG_PTR appAff; - ULONG_PTR sysAff; - - if (GetProcessAffinityMask(hProcess, &appAff, &sysAff)) - { - // remove non accessible processors - ULONG_PTR currentAffinity = affinity & appAff; - - if (!currentAffinity) - TC_LOG_ERROR(logChannel, "Processors marked in UseProcessors bitmask (hex) %x are not accessible. Accessible processors bitmask (hex): %x", affinity, appAff); - else if (SetProcessAffinityMask(hProcess, currentAffinity)) - TC_LOG_INFO(logChannel, "Using processors (bitmask, hex): %x", currentAffinity); - else - TC_LOG_ERROR(logChannel, "Can't set used processors (hex): %x", currentAffinity); - } - } - - if (highPriority) - { - if (SetPriorityClass(hProcess, HIGH_PRIORITY_CLASS)) - TC_LOG_INFO(logChannel, "Process priority class set to HIGH"); - else - TC_LOG_ERROR(logChannel, "Can't set process priority class."); - } +#define CONFIG_PROCESSOR_AFFINITY "UseProcessors" +#define CONFIG_HIGH_PRIORITY "ProcessPriority" -#else // Linux - - if (affinity > 0) - { - cpu_set_t mask; - CPU_ZERO(&mask); - - for (unsigned int i = 0; i < sizeof(affinity) * 8; ++i) - if (affinity & (1 << i)) - CPU_SET(i, &mask); - - if (sched_setaffinity(0, sizeof(mask), &mask)) - TC_LOG_ERROR(logChannel, "Can't set used processors (hex): %x, error: %s", affinity, strerror(errno)); - else - { - CPU_ZERO(&mask); - sched_getaffinity(0, sizeof(mask), &mask); - TC_LOG_INFO(logChannel, "Using processors (bitmask, hex): %lx", *(__cpu_mask*)(&mask)); - } - } - - if (highPriority) - { - if (setpriority(PRIO_PROCESS, 0, PROCESS_HIGH_PRIORITY)) - TC_LOG_ERROR(logChannel, "Can't set process priority class, error: %s", strerror(errno)); - else - TC_LOG_INFO(logChannel, "Process priority class set to %i", getpriority(PRIO_PROCESS, 0)); - } - -#endif -#else - // Suppresses unused argument warning for all other platforms - (void)logChannel; -#endif -} +void TC_COMMON_API SetProcessPriority(std::string const& logChannel, uint32 affinity, bool highPriority); #endif diff --git a/src/common/Utilities/AsioHacksFwd.h b/src/common/Utilities/AsioHacksFwd.h new file mode 100644 index 00000000000..6d4c9f5a3ed --- /dev/null +++ b/src/common/Utilities/AsioHacksFwd.h @@ -0,0 +1,48 @@ +/* + * Copyright (C) 2008-2017 TrinityCore <http://www.trinitycore.org/> + * + * This program is free software; you can redistribute it and/or modify it + * under the terms of the GNU General Public License as published by the + * Free Software Foundation; either version 2 of the License, or (at your + * option) any later version. + * + * This program is distributed in the hope that it will be useful, but WITHOUT + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for + * more details. + * + * You should have received a copy of the GNU General Public License along + * with this program. If not, see <http://www.gnu.org/licenses/>. + */ + +#ifndef AsioHacksFwd_h__ +#define AsioHacksFwd_h__ + +namespace boost +{ + namespace posix_time + { + class ptime; + } + + namespace asio + { + template <typename Time> + struct time_traits; + + template <typename TimeType, typename TimeTraits> + class deadline_timer_service; + + template <typename Time, typename TimeTraits, typename TimerService> + class basic_deadline_timer; + + typedef basic_deadline_timer<posix_time::ptime, time_traits<posix_time::ptime>, deadline_timer_service<posix_time::ptime, time_traits<posix_time::ptime>>> deadline_timer; + } +} + +namespace Trinity +{ + class AsioStrand; +} + +#endif // AsioHacksFwd_h__ diff --git a/src/common/Utilities/AsioHacksImpl.h b/src/common/Utilities/AsioHacksImpl.h new file mode 100644 index 00000000000..86888927a72 --- /dev/null +++ b/src/common/Utilities/AsioHacksImpl.h @@ -0,0 +1,32 @@ +/* + * Copyright (C) 2008-2017 TrinityCore <http://www.trinitycore.org/> + * + * This program is free software; you can redistribute it and/or modify it + * under the terms of the GNU General Public License as published by the + * Free Software Foundation; either version 2 of the License, or (at your + * option) any later version. + * + * This program is distributed in the hope that it will be useful, but WITHOUT + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for + * more details. + * + * You should have received a copy of the GNU General Public License along + * with this program. If not, see <http://www.gnu.org/licenses/>. + */ + +#ifndef AsioHacksImpl_h__ +#define AsioHacksImpl_h__ + +#include <boost/asio/strand.hpp> + +namespace Trinity +{ + class AsioStrand : public boost::asio::io_service::strand + { + public: + AsioStrand(boost::asio::io_service& io_service) : boost::asio::io_service::strand(io_service) { } + }; +} + +#endif // AsioHacksImpl_h__ diff --git a/src/common/Utilities/EventMap.cpp b/src/common/Utilities/EventMap.cpp index d4ea70174c4..e1c46d0ae65 100644 --- a/src/common/Utilities/EventMap.cpp +++ b/src/common/Utilities/EventMap.cpp @@ -16,6 +16,7 @@ */ #include "EventMap.h" +#include "Random.h" void EventMap::Reset() { @@ -32,6 +33,11 @@ void EventMap::SetPhase(uint8 phase) _phase = uint8(1 << (phase - 1)); } +void EventMap::ScheduleEvent(uint32 eventId, Milliseconds const& minTime, Milliseconds const& maxTime, uint32 group /*= 0*/, uint32 phase /*= 0*/) +{ + ScheduleEvent(eventId, urand(uint32(minTime.count()), uint32(maxTime.count())), group, phase); +} + void EventMap::ScheduleEvent(uint32 eventId, uint32 time, uint32 group /*= 0*/, uint8 phase /*= 0*/) { if (group && group <= 8) @@ -43,6 +49,16 @@ void EventMap::ScheduleEvent(uint32 eventId, uint32 time, uint32 group /*= 0*/, _eventMap.insert(EventStore::value_type(_time + time, eventId)); } +void EventMap::RescheduleEvent(uint32 eventId, Milliseconds const& minTime, Milliseconds const& maxTime, uint32 group /*= 0*/, uint32 phase /*= 0*/) +{ + RescheduleEvent(eventId, urand(uint32(minTime.count()), uint32(maxTime.count())), group, phase); +} + +void EventMap::Repeat(uint32 minTime, uint32 maxTime) +{ + Repeat(urand(minTime, maxTime)); +} + uint32 EventMap::ExecuteEvent() { while (!Empty()) diff --git a/src/common/Utilities/EventMap.h b/src/common/Utilities/EventMap.h index edcc7a64455..22899ea6b60 100644 --- a/src/common/Utilities/EventMap.h +++ b/src/common/Utilities/EventMap.h @@ -18,9 +18,9 @@ #ifndef _EVENT_MAP_H_ #define _EVENT_MAP_H_ -#include "Common.h" +#include "Define.h" #include "Duration.h" -#include "Util.h" +#include <map> class TC_COMMON_API EventMap { @@ -134,10 +134,7 @@ public: * @param group The group which the event is associated to. Has to be between 1 and 8. 0 means it has no group. * @param phase The phase in which the event can occur. Has to be between 1 and 8. 0 means it can occur in all phases. */ - void ScheduleEvent(uint32 eventId, Milliseconds const& minTime, Milliseconds const& maxTime, uint32 group = 0, uint32 phase = 0) - { - ScheduleEvent(eventId, urand(uint32(minTime.count()), uint32(maxTime.count())), group, phase); - } + void ScheduleEvent(uint32 eventId, Milliseconds const& minTime, Milliseconds const& maxTime, uint32 group = 0, uint32 phase = 0); /** * @name ScheduleEvent @@ -171,10 +168,7 @@ public: * @param group The group which the event is associated to. Has to be between 1 and 8. 0 means it has no group. * @param phase The phase in which the event can occur. Has to be between 1 and 8. 0 means it can occur in all phases. */ - void RescheduleEvent(uint32 eventId, Milliseconds const& minTime, Milliseconds const& maxTime, uint32 group = 0, uint32 phase = 0) - { - RescheduleEvent(eventId, urand(uint32(minTime.count()), uint32(maxTime.count())), group, phase); - } + void RescheduleEvent(uint32 eventId, Milliseconds const& minTime, Milliseconds const& maxTime, uint32 group = 0, uint32 phase = 0); /** * @name RescheduleEvent @@ -227,10 +221,7 @@ public: * @param minTime Minimum time until the event occurs. * @param maxTime Maximum time until the event occurs. */ - void Repeat(uint32 minTime, uint32 maxTime) - { - Repeat(urand(minTime, maxTime)); - } + void Repeat(uint32 minTime, uint32 maxTime); /** * @name ExecuteEvent diff --git a/src/common/Utilities/EventProcessor.cpp b/src/common/Utilities/EventProcessor.cpp index 07fce187ad3..76517abf007 100644 --- a/src/common/Utilities/EventProcessor.cpp +++ b/src/common/Utilities/EventProcessor.cpp @@ -44,7 +44,7 @@ void EventProcessor::Update(uint32 p_time) m_time += p_time; // main event loop - EventList::iterator i; + std::multimap<uint64, BasicEvent*>::iterator i; while (((i = m_events.begin()) != m_events.end()) && i->first <= m_time) { // get and remove event from queue diff --git a/src/common/Utilities/EventProcessor.h b/src/common/Utilities/EventProcessor.h index 3b924dbb3e5..081c9e82884 100644 --- a/src/common/Utilities/EventProcessor.h +++ b/src/common/Utilities/EventProcessor.h @@ -68,8 +68,6 @@ class TC_COMMON_API BasicEvent uint64 m_execTime; // planned time of next execution, filled by event handler }; -typedef std::multimap<uint64, BasicEvent*> EventList; - class TC_COMMON_API EventProcessor { public: @@ -83,7 +81,7 @@ class TC_COMMON_API EventProcessor protected: uint64 m_time; - EventList m_events; + std::multimap<uint64, BasicEvent*> m_events; }; #endif diff --git a/src/common/Utilities/Hash.h b/src/common/Utilities/Hash.h new file mode 100644 index 00000000000..fe2047f7024 --- /dev/null +++ b/src/common/Utilities/Hash.h @@ -0,0 +1,51 @@ +/* + * Copyright (C) 2008-2017 TrinityCore <http://www.trinitycore.org/> + * + * This program is free software; you can redistribute it and/or modify it + * under the terms of the GNU General Public License as published by the + * Free Software Foundation; either version 2 of the License, or (at your + * option) any later version. + * + * This program is distributed in the hope that it will be useful, but WITHOUT + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for + * more details. + * + * You should have received a copy of the GNU General Public License along + * with this program. If not, see <http://www.gnu.org/licenses/>. + */ + +#ifndef TrinityCore_Hash_h__ +#define TrinityCore_Hash_h__ + +#include <functional> +#include <utility> + +namespace Trinity +{ + template<typename T> + inline void hash_combine(std::size_t& seed, T const& val) + { + seed ^= std::hash<T>()(val) + 0x9E3779B9 + (seed << 6) + (seed >> 2); + } +} + + //! Hash implementation for std::pair to allow using pairs in unordered_set or as key for unordered_map + //! Individual types used in pair must be hashable by std::hash +namespace std +{ + template<class K, class V> + struct hash<std::pair<K, V>> + { + public: + size_t operator()(std::pair<K, V> const& p) const + { + size_t hashVal = 0; + Trinity::hash_combine(hashVal, p.first); + Trinity::hash_combine(hashVal, p.second); + return hashVal; + } + }; +} + +#endif // TrinityCore_Hash_h__ diff --git a/src/common/Utilities/Optional.h b/src/common/Utilities/Optional.h new file mode 100644 index 00000000000..c13a3a6a298 --- /dev/null +++ b/src/common/Utilities/Optional.h @@ -0,0 +1,28 @@ +/* + * Copyright (C) 2008-2017 TrinityCore <http://www.trinitycore.org/> + * + * This program is free software; you can redistribute it and/or modify it + * under the terms of the GNU General Public License as published by the + * Free Software Foundation; either version 2 of the License, or (at your + * option) any later version. + * + * This program is distributed in the hope that it will be useful, but WITHOUT + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for + * more details. + * + * You should have received a copy of the GNU General Public License along + * with this program. If not, see <http://www.gnu.org/licenses/>. + */ + +#ifndef TrinityCore_Optional_h__ +#define TrinityCore_Optional_h__ + +#include <boost/optional.hpp> +#include <boost/utility/in_place_factory.hpp> + + //! Optional helper class to wrap optional values within. +template <typename T> +using Optional = boost::optional<T>; + +#endif // TrinityCore_Optional_h__ diff --git a/src/common/Utilities/Random.cpp b/src/common/Utilities/Random.cpp index 3a0cdedcf4b..890ba1b8fce 100644 --- a/src/common/Utilities/Random.cpp +++ b/src/common/Utilities/Random.cpp @@ -16,7 +16,6 @@ */ #include "Random.h" -#include "Common.h" #include "Errors.h" #include "SFMT.h" #include <boost/thread/tss.hpp> @@ -53,8 +52,8 @@ uint32 urand(uint32 min, uint32 max) uint32 urandms(uint32 min, uint32 max) { ASSERT(max >= min); - ASSERT(INT_MAX / IN_MILLISECONDS >= max); - return GetRng()->URandom(min * IN_MILLISECONDS, max * IN_MILLISECONDS); + ASSERT(std::numeric_limits<uint32>::max() / Milliseconds::period::den >= max); + return GetRng()->URandom(min * Milliseconds::period::den, max * Milliseconds::period::den); } float frand(float min, float max) diff --git a/src/common/Utilities/Random.h b/src/common/Utilities/Random.h index ded0a6c6e5c..d200282093a 100644 --- a/src/common/Utilities/Random.h +++ b/src/common/Utilities/Random.h @@ -21,7 +21,6 @@ #include "Define.h" #include "Duration.h" #include <limits> -#include <random> /* Return a random number in the range min..max. */ TC_COMMON_API int32 irand(int32 min, int32 max); diff --git a/src/common/Utilities/StartProcess.cpp b/src/common/Utilities/StartProcess.cpp index 6f4676232ca..32182f67761 100644 --- a/src/common/Utilities/StartProcess.cpp +++ b/src/common/Utilities/StartProcess.cpp @@ -16,27 +16,20 @@ */ #include "StartProcess.h" - -#include <atomic> -#include <thread> -#include <functional> +#include "Errors.h" +#include "Log.h" +#include "Optional.h" #include <boost/algorithm/string/join.hpp> -#include <boost/iostreams/stream.hpp> #include <boost/iostreams/copy.hpp> -#include <boost/iostreams/concepts.hpp> -#include <boost/iostreams/device/file_descriptor.hpp> #include <boost/process.hpp> -#include <boost/system/system_error.hpp> - -#include "Common.h" -#include "Log.h" using namespace boost::process; using namespace boost::process::initializers; using namespace boost::iostreams; -namespace Trinity { +namespace Trinity +{ template<typename T> class TCLogSink @@ -250,19 +243,15 @@ std::shared_ptr<AsyncProcessResult> return handle; } -Optional<std::string> SearchExecutableInPath(std::string const& filename) +std::string SearchExecutableInPath(std::string const& filename) { try { - auto result = search_path(filename); - if (result.empty()) - return boost::none; - else - return result; + return search_path(filename); } catch (...) { - return boost::none; + return ""; } } diff --git a/src/common/Utilities/StartProcess.h b/src/common/Utilities/StartProcess.h index 120c4f26ea6..249d4ebd47e 100644 --- a/src/common/Utilities/StartProcess.h +++ b/src/common/Utilities/StartProcess.h @@ -18,11 +18,13 @@ #ifndef Process_h__ #define Process_h__ +#include "Define.h" #include <future> #include <memory> -#include "Common.h" +#include <vector> -namespace Trinity { +namespace Trinity +{ /// Starts a process with the given arguments and parameters and will block /// until the process is finished. @@ -60,7 +62,7 @@ TC_COMMON_API std::shared_ptr<AsyncProcessResult> /// Searches for the given executable in the PATH variable /// and returns a present optional when it was found. -TC_COMMON_API Optional<std::string> SearchExecutableInPath(std::string const& filename); +TC_COMMON_API std::string SearchExecutableInPath(std::string const& filename); } // namespace Trinity diff --git a/src/common/Utilities/TaskScheduler.cpp b/src/common/Utilities/TaskScheduler.cpp index 730ef8d4b89..a9dcf5dc09b 100644 --- a/src/common/Utilities/TaskScheduler.cpp +++ b/src/common/Utilities/TaskScheduler.cpp @@ -16,6 +16,7 @@ */ #include "TaskScheduler.h" +#include "Errors.h" TaskScheduler& TaskScheduler::ClearValidator() { diff --git a/src/common/Utilities/TaskScheduler.h b/src/common/Utilities/TaskScheduler.h index d6c399639bc..0213fb90f9b 100644 --- a/src/common/Utilities/TaskScheduler.h +++ b/src/common/Utilities/TaskScheduler.h @@ -18,6 +18,9 @@ #ifndef _TASK_SCHEDULER_H_ #define _TASK_SCHEDULER_H_ +#include "Duration.h" +#include "Optional.h" +#include "Random.h" #include <algorithm> #include <chrono> #include <vector> @@ -26,11 +29,6 @@ #include <utility> #include <set> -#include <boost/optional.hpp> - -#include "Util.h" -#include "Duration.h" - class TaskContext; /// The TaskScheduler class provides the ability to schedule std::function's in the near future. @@ -73,13 +71,13 @@ class TC_COMMON_API TaskScheduler timepoint_t _end; duration_t _duration; - boost::optional<group_t> _group; + Optional<group_t> _group; repeated_t _repeated; task_handler_t _task; public: // All Argument construct - Task(timepoint_t const& end, duration_t const& duration, boost::optional<group_t> const& group, + Task(timepoint_t const& end, duration_t const& duration, Optional<group_t> const& group, repeated_t const repeated, task_handler_t const& task) : _end(end), _duration(duration), _group(group), _repeated(repeated), _task(task) { } diff --git a/src/common/Utilities/Timer.h b/src/common/Utilities/Timer.h index e774808bcb0..30a8873102c 100644 --- a/src/common/Utilities/Timer.h +++ b/src/common/Utilities/Timer.h @@ -19,6 +19,7 @@ #ifndef TRINITY_TIMER_H #define TRINITY_TIMER_H +#include "Define.h" #include <chrono> inline uint32 getMSTime() diff --git a/src/common/Utilities/Util.cpp b/src/common/Utilities/Util.cpp index ae08d3ba5a6..05dfee273d8 100644 --- a/src/common/Utilities/Util.cpp +++ b/src/common/Utilities/Util.cpp @@ -18,11 +18,12 @@ #include "Util.h" #include "Common.h" -#include "CompilerDefs.h" -#include "utf8.h" -#include "Errors.h" // for ASSERT -#include <stdarg.h> -#include <boost/algorithm/string/case_conv.hpp> +#include <boost/asio/ip/address.hpp> +#include <utf8.h> +#include <algorithm> +#include <sstream> +#include <cstdarg> +#include <ctime> #if TRINITY_COMPILER == TRINITY_COMPILER_GNU #include <sys/socket.h> @@ -149,7 +150,7 @@ int64 MoneyStringToMoney(const std::string& moneyString) if (gCount + sCount + cCount != 1) return 0; - uint64 amount = atoull(*itr); + uint64 amount = strtoull(*itr, nullptr, 10); if (gCount == 1) money += amount * 100 * 100; else if (sCount == 1) @@ -171,8 +172,8 @@ uint32 TimeStringToSecs(const std::string& timestring) { if (isdigit(*itr)) { - buffer*=10; - buffer+= (*itr)-'0'; + buffer *= 10; + buffer += *itr - '0'; } else { @@ -184,9 +185,9 @@ uint32 TimeStringToSecs(const std::string& timestring) case 's': multiplier = 1; break; default : return 0; //bad format } - buffer*=multiplier; - secs+=buffer; - buffer=0; + buffer *= multiplier; + secs += buffer; + buffer = 0; } } @@ -214,9 +215,9 @@ bool IsIPAddress(char const* ipaddress) if (!ipaddress) return false; - // Let the big boys do it. - // Drawback: all valid ip address formats are recognized e.g.: 12.23, 121234, 0xABCD) - return inet_addr(ipaddress) != INADDR_NONE; + boost::system::error_code error; + boost::asio::ip::address::from_string(ipaddress, error); + return !error; } /// create PID file @@ -374,6 +375,16 @@ bool WStrToUtf8(std::wstring const& wstr, std::string& utf8str) typedef wchar_t const* const* wstrlist; +void wstrToUpper(std::wstring& str) +{ + std::transform(str.begin(), str.end(), str.begin(), wcharToUpper); +} + +void wstrToLower(std::wstring& str) +{ + std::transform(str.begin(), str.end(), str.begin(), wcharToLower); +} + std::wstring GetMainPartOfName(std::wstring const& wname, uint32 declension) { // supported only Cyrillic cases @@ -558,6 +569,7 @@ void HexStrToByteArray(std::string const& str, uint8* out, bool reverse /*= fals bool StringToBool(std::string const& str) { - std::string lowerStr = boost::algorithm::to_lower_copy(str); + std::string lowerStr = str; + std::transform(str.begin(), str.end(), lowerStr.begin(), ::tolower); return lowerStr == "1" || lowerStr == "true" || lowerStr == "yes"; } diff --git a/src/common/Utilities/Util.h b/src/common/Utilities/Util.h index c7a8289cb74..cd17946a40b 100644 --- a/src/common/Utilities/Util.h +++ b/src/common/Utilities/Util.h @@ -21,25 +21,9 @@ #include "Define.h" #include "Errors.h" -#include "Random.h" - -#include <algorithm> #include <string> #include <sstream> #include <vector> -#include <list> -#include <map> -#include <ctime> - -// Searcher for map of structs -template<typename T, class S> struct Finder -{ - T val_; - T S::* idMember_; - - Finder(T val, T S::* idMember) : val_(val), idMember_(idMember) {} - bool operator()(const std::pair<int, S> &obj) { return obj.second.*idMember_ == val_; } -}; class TC_COMMON_API Tokenizer { @@ -297,15 +281,8 @@ inline wchar_t wcharToLower(wchar_t wchar) return wchar; } -inline void wstrToUpper(std::wstring& str) -{ - std::transform( str.begin(), str.end(), str.begin(), wcharToUpper ); -} - -inline void wstrToLower(std::wstring& str) -{ - std::transform( str.begin(), str.end(), str.begin(), wcharToLower ); -} +TC_COMMON_API void wstrToUpper(std::wstring& str); +TC_COMMON_API void wstrToLower(std::wstring& str); TC_COMMON_API std::wstring GetMainPartOfName(std::wstring const& wname, uint32 declension); diff --git a/src/server/bnetserver/Main.cpp b/src/server/bnetserver/Main.cpp index ccee80e058a..25e45b81cef 100644 --- a/src/server/bnetserver/Main.cpp +++ b/src/server/bnetserver/Main.cpp @@ -25,6 +25,7 @@ #include "SessionManager.h" #include "AppenderDB.h" +#include "Config.h" #include "ProcessPriority.h" #include "RealmList.h" #include "GitRevision.h" @@ -188,7 +189,7 @@ int main(int argc, char** argv) signals.async_wait(std::bind(&SignalHandler, std::weak_ptr<boost::asio::io_service>(ioService), std::placeholders::_1, std::placeholders::_2)); // Set process priority according to configuration settings - SetProcessPriority("server.bnetserver"); + SetProcessPriority("server.bnetserver", sConfigMgr->GetIntDefault(CONFIG_PROCESSOR_AFFINITY, 0), sConfigMgr->GetBoolDefault(CONFIG_HIGH_PRIORITY, false)); // Enabled a timed callback for handling the database keep alive ping int32 dbPingInterval = sConfigMgr->GetIntDefault("MaxPingTime", 30); diff --git a/src/server/database/Database/DatabaseWorkerPool.cpp b/src/server/database/Database/DatabaseWorkerPool.cpp index e680ed834a3..aba2c573c4c 100644 --- a/src/server/database/Database/DatabaseWorkerPool.cpp +++ b/src/server/database/Database/DatabaseWorkerPool.cpp @@ -34,6 +34,12 @@ DatabaseWorkerPool<T>::DatabaseWorkerPool() } template <class T> +DatabaseWorkerPool<T>::~DatabaseWorkerPool() +{ + _queue->Cancel(); +} + +template <class T> void DatabaseWorkerPool<T>::SetConnectionInfo(std::string const& infoString, uint8 const asyncThreads, uint8 const synchThreads) { @@ -177,6 +183,12 @@ QueryResultHolderFuture DatabaseWorkerPool<T>::DelayQueryHolder(SQLQueryHolder* } template <class T> +SQLTransaction DatabaseWorkerPool<T>::BeginTransaction() +{ + return std::make_shared<Transaction>(); +} + +template <class T> void DatabaseWorkerPool<T>::CommitTransaction(SQLTransaction transaction) { #ifdef TRINITY_DEBUG diff --git a/src/server/database/Database/DatabaseWorkerPool.h b/src/server/database/Database/DatabaseWorkerPool.h index 6e5f25ff297..adca6b3f836 100644 --- a/src/server/database/Database/DatabaseWorkerPool.h +++ b/src/server/database/Database/DatabaseWorkerPool.h @@ -60,10 +60,7 @@ class DatabaseWorkerPool /* Activity state */ DatabaseWorkerPool(); - ~DatabaseWorkerPool() - { - _queue->Cancel(); - } + ~DatabaseWorkerPool(); void SetConnectionInfo(std::string const& infoString, uint8 const asyncThreads, uint8 const synchThreads); @@ -211,10 +208,7 @@ class DatabaseWorkerPool */ //! Begins an automanaged transaction pointer that will automatically rollback if not commited. (Autocommit=0) - SQLTransaction BeginTransaction() - { - return SQLTransaction(new Transaction); - } + SQLTransaction BeginTransaction(); //! Enqueues a collection of one-way SQL operations (can be both adhoc and prepared). The order in which these operations //! were appended to the transaction will be respected during execution. diff --git a/src/server/database/Database/Field.h b/src/server/database/Database/Field.h index 4daa447e481..8035dd16869 100644 --- a/src/server/database/Database/Field.h +++ b/src/server/database/Database/Field.h @@ -21,6 +21,9 @@ #include "Common.h" #include "Log.h" +#ifdef _WIN32 +#include <winsock2.h> +#endif #include <mysql.h> /** diff --git a/src/server/database/Database/MySQLConnection.h b/src/server/database/Database/MySQLConnection.h index 0a9b136386d..42fb976c729 100644 --- a/src/server/database/Database/MySQLConnection.h +++ b/src/server/database/Database/MySQLConnection.h @@ -19,6 +19,7 @@ #include "Transaction.h" #include "Util.h" #include "ProducerConsumerQueue.h" +#include <map> #ifndef _MYSQLCONNECTION_H #define _MYSQLCONNECTION_H diff --git a/src/server/database/Database/QueryCallback.h b/src/server/database/Database/QueryCallback.h index f9c93000da7..8258da37afb 100644 --- a/src/server/database/Database/QueryCallback.h +++ b/src/server/database/Database/QueryCallback.h @@ -20,6 +20,8 @@ #include "QueryResult.h" #include <future> +#include <list> +#include <queue> class TC_DATABASE_API QueryCallback { diff --git a/src/server/database/Database/QueryResult.h b/src/server/database/Database/QueryResult.h index 2df5d3a4f70..5d2cc425ae8 100644 --- a/src/server/database/Database/QueryResult.h +++ b/src/server/database/Database/QueryResult.h @@ -20,6 +20,7 @@ #define QUERYRESULT_H #include <memory> +#include "Errors.h" #include "Field.h" #ifdef _WIN32 diff --git a/src/server/database/Database/Transaction.h b/src/server/database/Database/Transaction.h index 6e6d68302f5..fbb96c053ce 100644 --- a/src/server/database/Database/Transaction.h +++ b/src/server/database/Database/Transaction.h @@ -20,6 +20,8 @@ #include "SQLOperation.h" #include "StringFormat.h" +#include <list> +#include <mutex> //- Forward declare (don't include header to prevent circular includes) class PreparedStatement; diff --git a/src/server/database/Logging/AppenderDB.cpp b/src/server/database/Logging/AppenderDB.cpp index 1a6e11209e9..f14bb04ddea 100644 --- a/src/server/database/Logging/AppenderDB.cpp +++ b/src/server/database/Logging/AppenderDB.cpp @@ -17,8 +17,9 @@ #include "AppenderDB.h" #include "Database/DatabaseEnv.h" +#include "LogMessage.h" -AppenderDB::AppenderDB(uint8 id, std::string const& name, LogLevel level, AppenderFlags /*flags*/, ExtraAppenderArgs /*extraArgs*/) +AppenderDB::AppenderDB(uint8 id, std::string const& name, LogLevel level, AppenderFlags /*flags*/, std::vector<char const*> /*extraArgs*/) : Appender(id, name, level), realmId(0), enabled(false) { } AppenderDB::~AppenderDB() { } diff --git a/src/server/database/Logging/AppenderDB.h b/src/server/database/Logging/AppenderDB.h index 56f5fc13b94..3f53a33210a 100644 --- a/src/server/database/Logging/AppenderDB.h +++ b/src/server/database/Logging/AppenderDB.h @@ -25,7 +25,7 @@ class TC_DATABASE_API AppenderDB: public Appender public: typedef std::integral_constant<AppenderType, APPENDER_DB>::type TypeIndex; - AppenderDB(uint8 id, std::string const& name, LogLevel level, AppenderFlags flags, ExtraAppenderArgs extraArgs); + AppenderDB(uint8 id, std::string const& name, LogLevel level, AppenderFlags flags, std::vector<char const*> extraArgs); ~AppenderDB(); void setRealmId(uint32 realmId) override; diff --git a/src/server/database/Updater/DBUpdater.cpp b/src/server/database/Updater/DBUpdater.cpp index 63257571177..117c3097478 100644 --- a/src/server/database/Updater/DBUpdater.cpp +++ b/src/server/database/Updater/DBUpdater.cpp @@ -41,18 +41,12 @@ bool DBUpdaterUtil::CheckExecutable() boost::filesystem::path exe(GetCorrectedMySQLExecutable()); if (!exists(exe)) { - exe.clear(); - - if (auto path = Trinity::SearchExecutableInPath("mysql")) + exe = Trinity::SearchExecutableInPath("mysql"); + if (!exe.empty() && exists(exe)) { - exe = std::move(*path); - - if (!exe.empty() && exists(exe)) - { - // Correct the path to the cli - corrected_path() = absolute(exe).generic_string(); - return true; - } + // Correct the path to the cli + corrected_path() = absolute(exe).generic_string(); + return true; } TC_LOG_FATAL("sql.updates", "Didn't find any executable MySQL binary at \'%s\' or in path, correct the path in the *.conf (\"MySQLExecutable\").", @@ -347,7 +341,7 @@ bool DBUpdater<T>::Populate(DatabaseWorkerPool<T>& pool) template<class T> QueryResult DBUpdater<T>::Retrieve(DatabaseWorkerPool<T>& pool, std::string const& query) { - return pool.PQuery(query.c_str()); + return pool.Query(query.c_str()); } template<class T> diff --git a/src/server/database/Updater/UpdateFetcher.h b/src/server/database/Updater/UpdateFetcher.h index 51f735e797f..0ca18f43886 100644 --- a/src/server/database/Updater/UpdateFetcher.h +++ b/src/server/database/Updater/UpdateFetcher.h @@ -23,6 +23,7 @@ #include <functional> #include <string> #include <memory> +#include <set> #include <vector> class TC_DATABASE_API UpdateFetcher diff --git a/src/server/game/Achievements/CriteriaHandler.h b/src/server/game/Achievements/CriteriaHandler.h index ab5625b0742..8959997041c 100644 --- a/src/server/game/Achievements/CriteriaHandler.h +++ b/src/server/game/Achievements/CriteriaHandler.h @@ -22,6 +22,9 @@ #include "ObjectGuid.h" #include "Transaction.h" #include "Common.h" +#include <map> +#include <unordered_map> +#include <vector> class Player; class Unit; diff --git a/src/server/game/AuctionHouseBot/AuctionHouseBotBuyer.cpp b/src/server/game/AuctionHouseBot/AuctionHouseBotBuyer.cpp index 1c34fc49abb..5bcaa084950 100644 --- a/src/server/game/AuctionHouseBot/AuctionHouseBotBuyer.cpp +++ b/src/server/game/AuctionHouseBot/AuctionHouseBotBuyer.cpp @@ -19,6 +19,7 @@ #include "Item.h" #include "ItemTemplate.h" #include "AuctionHouseBotBuyer.h" +#include "Random.h" AuctionBotBuyer::AuctionBotBuyer() : _checkInterval(20 * MINUTE) { diff --git a/src/server/game/AuctionHouseBot/AuctionHouseBotSeller.cpp b/src/server/game/AuctionHouseBot/AuctionHouseBotSeller.cpp index ae05c53dace..4c74727a03e 100644 --- a/src/server/game/AuctionHouseBot/AuctionHouseBotSeller.cpp +++ b/src/server/game/AuctionHouseBot/AuctionHouseBotSeller.cpp @@ -19,6 +19,7 @@ #include "ObjectMgr.h" #include "AuctionHouseMgr.h" #include "AuctionHouseBotSeller.h" +#include "Random.h" AuctionBotSeller::AuctionBotSeller() { diff --git a/src/server/game/Battlegrounds/BattlegroundMgr.cpp b/src/server/game/Battlegrounds/BattlegroundMgr.cpp index f7405c0337e..aedce2d6f51 100644 --- a/src/server/game/Battlegrounds/BattlegroundMgr.cpp +++ b/src/server/game/Battlegrounds/BattlegroundMgr.cpp @@ -43,6 +43,7 @@ #include "SharedDefines.h" #include "DisableMgr.h" #include "Opcodes.h" +#include "Containers.h" /*********************************************************/ /*** BATTLEGROUND MANAGER ***/ @@ -866,9 +867,11 @@ BattlegroundTypeId BattlegroundMgr::GetRandomBG(BattlegroundTypeId bgTypeId) { if (BattlegroundTemplate const* bgTemplate = GetBattlegroundTemplateByTypeId(bgTypeId)) { - uint32 weight = 0; BattlegroundSelectionWeightMap selectionWeights; - + std::vector<BattlegroundTypeId> ids; + ids.reserve(16); + std::vector<double> weights; + weights.reserve(16); for (int32 mapId : bgTemplate->BattlemasterEntry->MapID) { if (mapId == -1) @@ -876,28 +879,12 @@ BattlegroundTypeId BattlegroundMgr::GetRandomBG(BattlegroundTypeId bgTypeId) if (BattlegroundTemplate const* bg = GetBattlegroundTemplateByMapId(mapId)) { - weight += bg->Weight; - selectionWeights[bg->Id] = bg->Weight; + ids.push_back(bg->Id); + weights.push_back(bg->Weight); } } - // there is only one bg to select - if (selectionWeights.size() == 1) - return selectionWeights.begin()->first; - - if (weight) - { - // Select a random value - uint32 selectedWeight = urand(0, weight - 1); - // Select the correct bg (if we have in DB A(10), B(20), C(10), D(15) --> [0---A---9|10---B---29|30---C---39|40---D---54]) - weight = 0; - for (auto it : selectionWeights) - { - weight += it.second; - if (selectedWeight < weight) - return it.first; - } - } + return *Trinity::Containers::SelectRandomWeightedContainerElement(ids, weights); } return BATTLEGROUND_TYPE_NONE; diff --git a/src/server/game/Battlegrounds/Zones/BattlegroundAB.cpp b/src/server/game/Battlegrounds/Zones/BattlegroundAB.cpp index 68930e23cf9..180e82755f2 100644 --- a/src/server/game/Battlegrounds/Zones/BattlegroundAB.cpp +++ b/src/server/game/Battlegrounds/Zones/BattlegroundAB.cpp @@ -22,6 +22,7 @@ #include "Creature.h" #include "Language.h" #include "Player.h" +#include "Random.h" #include "Util.h" #include "WorldSession.h" diff --git a/src/server/game/Battlegrounds/Zones/BattlegroundDS.cpp b/src/server/game/Battlegrounds/Zones/BattlegroundDS.cpp index 7ca9d636533..00ab1c2e964 100644 --- a/src/server/game/Battlegrounds/Zones/BattlegroundDS.cpp +++ b/src/server/game/Battlegrounds/Zones/BattlegroundDS.cpp @@ -19,6 +19,7 @@ #include "BattlegroundDS.h" #include "Creature.h" #include "Player.h" +#include "Random.h" #include "WorldPacket.h" BattlegroundDS::BattlegroundDS() diff --git a/src/server/game/Battlegrounds/Zones/BattlegroundEY.cpp b/src/server/game/Battlegrounds/Zones/BattlegroundEY.cpp index 335f95d03fc..45ab8a542f4 100644 --- a/src/server/game/Battlegrounds/Zones/BattlegroundEY.cpp +++ b/src/server/game/Battlegrounds/Zones/BattlegroundEY.cpp @@ -22,6 +22,7 @@ #include "Creature.h" #include "Language.h" #include "Player.h" +#include "Random.h" #include "Util.h" // these variables aren't used outside of this file, so declare them only here diff --git a/src/server/game/Battlegrounds/Zones/BattlegroundSA.cpp b/src/server/game/Battlegrounds/Zones/BattlegroundSA.cpp index 0224adbdbea..0fa25fc3f78 100644 --- a/src/server/game/Battlegrounds/Zones/BattlegroundSA.cpp +++ b/src/server/game/Battlegrounds/Zones/BattlegroundSA.cpp @@ -22,6 +22,7 @@ #include "ObjectMgr.h" #include "Player.h" #include "ScriptedCreature.h" +#include "UpdateData.h" #include "WorldPacket.h" BattlegroundSA::BattlegroundSA() diff --git a/src/server/game/Chat/Channels/Channel.h b/src/server/game/Chat/Channels/Channel.h index 056cf9a4c3f..50dd5c8ee96 100644 --- a/src/server/game/Chat/Channels/Channel.h +++ b/src/server/game/Chat/Channels/Channel.h @@ -22,6 +22,7 @@ #include "Common.h" #include "ObjectGuid.h" #include "WorldPacket.h" +#include <map> class Player; diff --git a/src/server/game/Chat/Channels/ChannelMgr.h b/src/server/game/Chat/Channels/ChannelMgr.h index f8bcaedf57b..66d57c38d55 100644 --- a/src/server/game/Chat/Channels/ChannelMgr.h +++ b/src/server/game/Chat/Channels/ChannelMgr.h @@ -19,6 +19,8 @@ #define __TRINITY_CHANNELMGR_H #include "Common.h" +#include "Hash.h" +#include <unordered_map> class Channel; diff --git a/src/server/game/Chat/ChatLink.h b/src/server/game/Chat/ChatLink.h index 2a7d1954d84..f8656a7870e 100644 --- a/src/server/game/Chat/ChatLink.h +++ b/src/server/game/Chat/ChatLink.h @@ -19,10 +19,11 @@ #define TRINITYCORE_CHATLINK_H #include "SharedDefines.h" -#include <sstream> +#include "Common.h" #include <list> +#include <sstream> +#include <vector> #include <cstring> -#include "Common.h" struct ItemLocale; struct ItemTemplate; diff --git a/src/server/game/Conditions/ConditionMgr.h b/src/server/game/Conditions/ConditionMgr.h index cf9ef40c2bf..14eef8db623 100644 --- a/src/server/game/Conditions/ConditionMgr.h +++ b/src/server/game/Conditions/ConditionMgr.h @@ -20,6 +20,10 @@ #define TRINITY_CONDITIONMGR_H #include "Common.h" +#include "Hash.h" +#include <array> +#include <unordered_map> +#include <vector> class Creature; class Player; diff --git a/src/server/game/DataStores/DB2Stores.cpp b/src/server/game/DataStores/DB2Stores.cpp index 689c36710df..a28e77aecbc 100644 --- a/src/server/game/DataStores/DB2Stores.cpp +++ b/src/server/game/DataStores/DB2Stores.cpp @@ -24,6 +24,11 @@ #include "TransportMgr.h" #include "World.h" +// temporary hack until database includes are sorted out (don't want to pull in Windows.h everywhere from mysql.h) +#ifdef GetClassName +#undef GetClassName +#endif + DB2Storage<AchievementEntry> sAchievementStore("Achievement.db2", AchievementLoadInfo::Instance()); DB2Storage<AnimKitEntry> sAnimKitStore("AnimKit.db2", AnimKitLoadInfo::Instance()); DB2Storage<AreaGroupMemberEntry> sAreaGroupMemberStore("AreaGroupMember.db2", AreaGroupMemberLoadInfo::Instance()); diff --git a/src/server/game/DataStores/DB2Stores.h b/src/server/game/DataStores/DB2Stores.h index d4211b76d4f..ea2d02883bd 100644 --- a/src/server/game/DataStores/DB2Stores.h +++ b/src/server/game/DataStores/DB2Stores.h @@ -20,8 +20,15 @@ #include "DB2Store.h" #include "DB2Structure.h" +#include "Hash.h" #include "SharedDefines.h" #include "Regex.h" +#include <array> +#include <map> +#include <set> +#include <unordered_map> +#include <unordered_set> +#include <vector> TC_GAME_API extern DB2Storage<AchievementEntry> sAchievementStore; TC_GAME_API extern DB2Storage<AnimKitEntry> sAnimKitStore; diff --git a/src/server/game/DataStores/GameTables.h b/src/server/game/DataStores/GameTables.h index a9d1a01b1c3..93a0fb6b532 100644 --- a/src/server/game/DataStores/GameTables.h +++ b/src/server/game/DataStores/GameTables.h @@ -20,6 +20,7 @@ #include "SharedDefines.h" #include "Common.h" +#include <vector> struct GtArmorMitigationByLvlEntry { diff --git a/src/server/game/DungeonFinding/LFG.h b/src/server/game/DungeonFinding/LFG.h index 568fc300448..09d63b6d47e 100644 --- a/src/server/game/DungeonFinding/LFG.h +++ b/src/server/game/DungeonFinding/LFG.h @@ -20,6 +20,7 @@ #include "Common.h" #include "ObjectGuid.h" +#include <map> namespace lfg { diff --git a/src/server/game/Entities/AreaTrigger/AreaTriggerTemplate.h b/src/server/game/Entities/AreaTrigger/AreaTriggerTemplate.h index 98778d9fa83..d6ef0a43d79 100644 --- a/src/server/game/Entities/AreaTrigger/AreaTriggerTemplate.h +++ b/src/server/game/Entities/AreaTrigger/AreaTriggerTemplate.h @@ -18,9 +18,10 @@ #ifndef TRINITYCORE_AREATRIGGER_TEMPLATE_H #define TRINITYCORE_AREATRIGGER_TEMPLATE_H -#include <G3D/Vector3.h> - #include "Define.h" +#include <G3D/Vector3.h> +#include <vector> +#include <cstring> #define MAX_AREATRIGGER_ENTITY_DATA 6 #define MAX_AREATRIGGER_SCALE 7 diff --git a/src/server/game/Entities/Corpse/Corpse.cpp b/src/server/game/Entities/Corpse/Corpse.cpp index ac34ebc0e9c..27a8623bad0 100644 --- a/src/server/game/Entities/Corpse/Corpse.cpp +++ b/src/server/game/Entities/Corpse/Corpse.cpp @@ -19,6 +19,7 @@ #include "Common.h" #include "Corpse.h" #include "Player.h" +#include "UpdateData.h" #include "ObjectAccessor.h" #include "DatabaseEnv.h" diff --git a/src/server/game/Entities/Creature/Creature.h b/src/server/game/Entities/Creature/Creature.h index 32775918cb2..94ef4c532d6 100644 --- a/src/server/game/Entities/Creature/Creature.h +++ b/src/server/game/Entities/Creature/Creature.h @@ -20,6 +20,7 @@ #define TRINITYCORE_CREATURE_H #include "Common.h" +#include "Duration.h" #include "Unit.h" #include "ItemTemplate.h" #include "LootMgr.h" @@ -482,21 +483,21 @@ typedef std::unordered_map<uint16, CreatureBaseStats> CreatureBaseStatsContainer struct CreatureLocale { - StringVector Name; - StringVector NameAlt; - StringVector Title; - StringVector TitleAlt; + std::vector<std::string> Name; + std::vector<std::string> NameAlt; + std::vector<std::string> Title; + std::vector<std::string> TitleAlt; }; struct GossipMenuItemsLocale { - StringVector OptionText; - StringVector BoxText; + std::vector<std::string> OptionText; + std::vector<std::string> BoxText; }; struct PointOfInterestLocale { - StringVector Name; + std::vector<std::string> Name; }; struct EquipmentItem diff --git a/src/server/game/Entities/GameObject/GameObject.h b/src/server/game/Entities/GameObject/GameObject.h index 6f444d857c9..fe4fcd08cad 100644 --- a/src/server/game/Entities/GameObject/GameObject.h +++ b/src/server/game/Entities/GameObject/GameObject.h @@ -865,9 +865,9 @@ union GameObjectValue struct GameObjectLocale { - StringVector Name; - StringVector CastBarCaption; - StringVector Unk1; + std::vector<std::string> Name; + std::vector<std::string> CastBarCaption; + std::vector<std::string> Unk1; }; // `gameobject_addon` table diff --git a/src/server/game/Entities/Item/Item.cpp b/src/server/game/Entities/Item/Item.cpp index 5d039a615b9..6c97161ba3a 100644 --- a/src/server/game/Entities/Item/Item.cpp +++ b/src/server/game/Entities/Item/Item.cpp @@ -34,6 +34,7 @@ #include "GameTables.h" #include "CollectionMgr.h" #include "ArtifactPackets.h" +#include "UpdateData.h" void AddItemsSetItem(Player* player, Item* item) { diff --git a/src/server/game/Entities/Item/ItemEnchantmentMgr.cpp b/src/server/game/Entities/Item/ItemEnchantmentMgr.cpp index ce88ba61f3e..a9bef8e415f 100644 --- a/src/server/game/Entities/Item/ItemEnchantmentMgr.cpp +++ b/src/server/game/Entities/Item/ItemEnchantmentMgr.cpp @@ -22,6 +22,7 @@ #include "Log.h" #include "ObjectMgr.h" #include "Util.h" +#include "Random.h" #include <list> #include <vector> diff --git a/src/server/game/Entities/Object/ObjectGuid.cpp b/src/server/game/Entities/Object/ObjectGuid.cpp index 92484f9e184..292dcdd60a4 100644 --- a/src/server/game/Entities/Object/ObjectGuid.cpp +++ b/src/server/game/Entities/Object/ObjectGuid.cpp @@ -17,8 +17,9 @@ */ #include "ObjectGuid.h" +#include "Hash.h" +#include "Log.h" #include "World.h" - #include <sstream> #include <iomanip> @@ -109,6 +110,14 @@ std::string ObjectGuid::ToString() const return str.str(); } +std::size_t ObjectGuid::GetHash() const +{ + std::size_t hashVal = 0; + Trinity::hash_combine(hashVal, _low); + Trinity::hash_combine(hashVal, _high); + return hashVal; +} + ObjectGuid ObjectGuid::Global(HighGuid type, LowType counter) { return ObjectGuid(uint64(uint64(type) << 58), counter); diff --git a/src/server/game/Entities/Object/ObjectGuid.h b/src/server/game/Entities/Object/ObjectGuid.h index 88172c970cb..883476b5f21 100644 --- a/src/server/game/Entities/Object/ObjectGuid.h +++ b/src/server/game/Entities/Object/ObjectGuid.h @@ -19,10 +19,14 @@ #ifndef ObjectGuid_h__ #define ObjectGuid_h__ -#include "Common.h" #include "ByteBuffer.h" -#include <type_traits> +#include <deque> #include <functional> +#include <list> +#include <set> +#include <type_traits> +#include <unordered_set> +#include <vector> enum TypeID { @@ -316,6 +320,7 @@ class TC_GAME_API ObjectGuid static char const* GetTypeName(HighGuid high); char const* GetTypeName() const { return !IsEmpty() ? GetTypeName(GetHigh()) : "None"; } std::string ToString() const; + std::size_t GetHash() const; private: static bool HasEntry(HighGuid high) @@ -415,7 +420,7 @@ namespace std public: size_t operator()(ObjectGuid const& key) const { - return boost::hash_range(reinterpret_cast<uint64 const*>(&key), reinterpret_cast<uint64 const*>(&key) + 2); + return key.GetHash(); } }; } diff --git a/src/server/game/Entities/Object/ObjectPosSelector.h b/src/server/game/Entities/Object/ObjectPosSelector.h index d5414e0e245..3d75a8daafb 100644 --- a/src/server/game/Entities/Object/ObjectPosSelector.h +++ b/src/server/game/Entities/Object/ObjectPosSelector.h @@ -19,9 +19,9 @@ #ifndef _OBJECT_POS_SELECTOR_H #define _OBJECT_POS_SELECTOR_H -#include<Common.h> - -#include<map> +#include "Common.h" +#include <map> +#include <cmath> enum UsedPosType { USED_POS_PLUS, USED_POS_MINUS }; diff --git a/src/server/game/Entities/Object/Position.cpp b/src/server/game/Entities/Object/Position.cpp index 5cf7fdd51f1..bbb6ee271cb 100644 --- a/src/server/game/Entities/Object/Position.cpp +++ b/src/server/game/Entities/Object/Position.cpp @@ -18,6 +18,7 @@ #include "Position.h" #include "ByteBuffer.h" #include "GridDefines.h" +#include "Random.h" #include <G3D/g3dmath.h> diff --git a/src/server/game/Entities/Player/Player.cpp b/src/server/game/Entities/Player/Player.cpp index 936e793bfdb..eb99ccb70c3 100644 --- a/src/server/game/Entities/Player/Player.cpp +++ b/src/server/game/Entities/Player/Player.cpp @@ -19621,6 +19621,8 @@ void Player::SaveToDB(bool create /*=false*/) stmt->setUInt64(0, GetGUID().GetCounter()); trans->Append(stmt); + auto finiteAlways = [](float f) { return std::isfinite(f) ? f : 0.0f; }; + if (create) { //! Insert query diff --git a/src/server/game/Entities/Player/SceneMgr.h b/src/server/game/Entities/Player/SceneMgr.h index 0a7c13322cd..9064d9c892b 100644 --- a/src/server/game/Entities/Player/SceneMgr.h +++ b/src/server/game/Entities/Player/SceneMgr.h @@ -19,6 +19,7 @@ #define SceneMgr_h__ #include "Common.h" +#include <map> enum SceneFlags { diff --git a/src/server/game/Entities/Player/TradeData.cpp b/src/server/game/Entities/Player/TradeData.cpp index 3c193df220b..fe5b8231109 100644 --- a/src/server/game/Entities/Player/TradeData.cpp +++ b/src/server/game/Entities/Player/TradeData.cpp @@ -17,6 +17,7 @@ #include "TradeData.h" #include "Player.h" +#include "Random.h" #include "TradePackets.h" TradeData* TradeData::GetTraderData() const @@ -143,3 +144,8 @@ void TradeData::SetAccepted(bool state, bool forTrader /*= false*/) _player->GetSession()->SendTradeStatus(info); } } + +void TradeData::UpdateServerStateIndex() +{ + _serverStateIndex = rand32(); +} diff --git a/src/server/game/Entities/Player/TradeData.h b/src/server/game/Entities/Player/TradeData.h index 3fa800eb557..2a5b9f79777 100644 --- a/src/server/game/Entities/Player/TradeData.h +++ b/src/server/game/Entities/Player/TradeData.h @@ -65,7 +65,7 @@ public: void UpdateClientStateIndex() { ++_clientStateIndex; } uint32 GetServerStateIndex() const { return _serverStateIndex; } - void UpdateServerStateIndex() { _serverStateIndex = rand32(); } + void UpdateServerStateIndex(); private: void Update(bool for_trader = true) const; diff --git a/src/server/game/Entities/Transport/Transport.cpp b/src/server/game/Entities/Transport/Transport.cpp index b8329d24a8b..2c09cf6f9e2 100644 --- a/src/server/game/Entities/Transport/Transport.cpp +++ b/src/server/game/Entities/Transport/Transport.cpp @@ -27,6 +27,7 @@ #include "Cell.h" #include "CellImpl.h" #include "Totem.h" +#include "UpdateData.h" Transport::Transport() : GameObject(), _transportInfo(NULL), _isMoving(true), _pendingStop(false), diff --git a/src/server/game/Entities/Unit/StatSystem.cpp b/src/server/game/Entities/Unit/StatSystem.cpp index fab73a76b40..72f58e0f17a 100644 --- a/src/server/game/Entities/Unit/StatSystem.cpp +++ b/src/server/game/Entities/Unit/StatSystem.cpp @@ -25,6 +25,7 @@ #include "SpellAuras.h" #include "SpellAuraEffects.h" #include "World.h" +#include <numeric> inline bool _ModifyUInt32(bool apply, uint32& baseValue, int32& amount) { diff --git a/src/server/game/Events/GameEventMgr.h b/src/server/game/Events/GameEventMgr.h index 77b39e543d3..22e9ee717f4 100644 --- a/src/server/game/Events/GameEventMgr.h +++ b/src/server/game/Events/GameEventMgr.h @@ -23,6 +23,8 @@ #include "SharedDefines.h" #include "Define.h" #include "ObjectGuid.h" +#include <map> +#include <unordered_map> #define max_ge_check_delay DAY // 1 day in seconds diff --git a/src/server/game/Garrison/GarrisonMgr.h b/src/server/game/Garrison/GarrisonMgr.h index 67e5b13df7d..29f9d4187be 100644 --- a/src/server/game/Garrison/GarrisonMgr.h +++ b/src/server/game/Garrison/GarrisonMgr.h @@ -19,8 +19,12 @@ #define GarrisonMgr_h__ #include "DB2Stores.h" -#include <unordered_set> #include "Position.h" +#include <list> +#include <set> +#include <unordered_map> +#include <unordered_set> +#include <vector> struct FinalizeGarrisonPlotGOInfo { diff --git a/src/server/game/Globals/ObjectAccessor.cpp b/src/server/game/Globals/ObjectAccessor.cpp index 2fc726e3601..564b02adc67 100644 --- a/src/server/game/Globals/ObjectAccessor.cpp +++ b/src/server/game/Globals/ObjectAccessor.cpp @@ -37,6 +37,10 @@ template<class T> void HashMapHolder<T>::Insert(T* o) { + static_assert(std::is_same<Player, T>::value + || std::is_same<Transport, T>::value, + "Only Player and Transport can be registered in global HashMapHolder"); + boost::unique_lock<boost::shared_mutex> lock(*GetLock()); GetContainer()[o->GetGUID()] = o; diff --git a/src/server/game/Globals/ObjectAccessor.h b/src/server/game/Globals/ObjectAccessor.h index 1dae898e832..08bf2bc3b58 100644 --- a/src/server/game/Globals/ObjectAccessor.h +++ b/src/server/game/Globals/ObjectAccessor.h @@ -19,27 +19,26 @@ #ifndef TRINITY_OBJECTACCESSOR_H #define TRINITY_OBJECTACCESSOR_H -#include <mutex> -#include <set> +#include "ObjectGuid.h" #include <unordered_map> -#include <boost/thread/locks.hpp> -#include <boost/thread/shared_mutex.hpp> - -#include "Define.h" -#include "GridDefines.h" -#include "UpdateData.h" -#include "Object.h" +class Object; +class WorldObject; +class Unit; class Creature; +class Player; class Corpse; -class Unit; class GameObject; class DynamicObject; -class WorldObject; -class Vehicle; -class Map; -class WorldRunnable; +class AreaTrigger; +class Pet; class Transport; +class Map; + +namespace boost +{ + class shared_mutex; +} template <class T> class TC_GAME_API HashMapHolder @@ -48,10 +47,6 @@ class TC_GAME_API HashMapHolder HashMapHolder() { } public: - static_assert(std::is_same<Player, T>::value - || std::is_same<Transport, T>::value, - "Only Player and Transport can be registered in global HashMapHolder"); - typedef std::unordered_map<ObjectGuid, T*> MapType; static void Insert(T* o); diff --git a/src/server/game/Globals/ObjectMgr.cpp b/src/server/game/Globals/ObjectMgr.cpp index a0077de693e..d081a1d0ed9 100644 --- a/src/server/game/Globals/ObjectMgr.cpp +++ b/src/server/game/Globals/ObjectMgr.cpp @@ -38,6 +38,7 @@ #include "MapManager.h" #include "Object.h" #include "PoolMgr.h" +#include "Random.h" #include "ReputationMgr.h" #include "ScriptMgr.h" #include "SpellAuras.h" @@ -294,7 +295,7 @@ ObjectMgr::~ObjectMgr() delete itr2->second; } -void ObjectMgr::AddLocaleString(std::string const& value, LocaleConstant localeConstant, StringVector& data) +void ObjectMgr::AddLocaleString(std::string const& value, LocaleConstant localeConstant, std::vector<std::string>& data) { if (!value.empty()) { @@ -7218,8 +7219,8 @@ void ObjectMgr::LoadPetNumber() std::string ObjectMgr::GeneratePetName(uint32 entry) { - StringVector& list0 = _petHalfName0[entry]; - StringVector& list1 = _petHalfName1[entry]; + std::vector<std::string>& list0 = _petHalfName0[entry]; + std::vector<std::string>& list1 = _petHalfName1[entry]; if (list0.empty() || list1.empty()) { diff --git a/src/server/game/Globals/ObjectMgr.h b/src/server/game/Globals/ObjectMgr.h index 40ea4618c39..5828bd65016 100644 --- a/src/server/game/Globals/ObjectMgr.h +++ b/src/server/game/Globals/ObjectMgr.h @@ -430,7 +430,7 @@ typedef std::unordered_map<uint32/*(mapid, spawnMode) pair*/, CellObjectGuidsMap struct TrinityString { - StringVector Content; + std::vector<std::string> Content; }; typedef std::map<ObjectGuid, ObjectGuid> LinkedRespawnContainer; @@ -1368,8 +1368,8 @@ class TC_GAME_API ObjectMgr // for wintergrasp only GraveYardContainer GraveYardStore; - static void AddLocaleString(std::string const& value, LocaleConstant localeConstant, StringVector& data); - static inline void GetLocaleString(StringVector const& data, LocaleConstant localeConstant, std::string& value) + static void AddLocaleString(std::string const& value, LocaleConstant localeConstant, std::vector<std::string>& data); + static inline void GetLocaleString(std::vector<std::string> const& data, LocaleConstant localeConstant, std::string& value) { if (data.size() > size_t(localeConstant) && !data[localeConstant].empty()) value = data[localeConstant]; @@ -1535,7 +1535,7 @@ class TC_GAME_API ObjectMgr FishingBaseSkillContainer _fishingBaseForAreaStore; std::unordered_map<uint32, SkillTiersEntry> _skillTiers; - typedef std::map<uint32, StringVector> HalfNameContainer; + typedef std::map<uint32, std::vector<std::string>> HalfNameContainer; HalfNameContainer _petHalfName0; HalfNameContainer _petHalfName1; diff --git a/src/server/game/Grids/NGrid.cpp b/src/server/game/Grids/NGrid.cpp new file mode 100644 index 00000000000..e5d69dae44a --- /dev/null +++ b/src/server/game/Grids/NGrid.cpp @@ -0,0 +1,29 @@ +/* + * Copyright (C) 2008-2017 TrinityCore <http://www.trinitycore.org/> + * + * This program is free software; you can redistribute it and/or modify it + * under the terms of the GNU General Public License as published by the + * Free Software Foundation; either version 2 of the License, or (at your + * option) any later version. + * + * This program is distributed in the hope that it will be useful, but WITHOUT + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for + * more details. + * + * You should have received a copy of the GNU General Public License along + * with this program. If not, see <http://www.gnu.org/licenses/>. + */ + +#include "NGrid.h" +#include "Random.h" + +GridInfo::GridInfo() : i_timer(0), vis_Update(0, irand(0, DEFAULT_VISIBILITY_NOTIFY_PERIOD)), + i_unloadActiveLockCount(0), i_unloadExplicitLock(false), i_unloadReferenceLock(false) +{ +} + +GridInfo::GridInfo(time_t expiry, bool unload /*= true */) : i_timer(expiry), vis_Update(0, irand(0, DEFAULT_VISIBILITY_NOTIFY_PERIOD)), + i_unloadActiveLockCount(0), i_unloadExplicitLock(!unload), i_unloadReferenceLock(false) +{ +} diff --git a/src/server/game/Grids/NGrid.h b/src/server/game/Grids/NGrid.h index 986f083c053..a26dadef962 100644 --- a/src/server/game/Grids/NGrid.h +++ b/src/server/game/Grids/NGrid.h @@ -29,15 +29,11 @@ #define DEFAULT_VISIBILITY_NOTIFY_PERIOD 1000 -class GridInfo +class TC_GAME_API GridInfo { public: - GridInfo() - : i_timer(0), vis_Update(0, irand(0, DEFAULT_VISIBILITY_NOTIFY_PERIOD)), - i_unloadActiveLockCount(0), i_unloadExplicitLock(false), i_unloadReferenceLock(false) { } - GridInfo(time_t expiry, bool unload = true ) - : i_timer(expiry), vis_Update(0, irand(0, DEFAULT_VISIBILITY_NOTIFY_PERIOD)), - i_unloadActiveLockCount(0), i_unloadExplicitLock(!unload), i_unloadReferenceLock(false) { } + GridInfo(); + GridInfo(time_t expiry, bool unload = true ); const TimeTracker& getTimeTracker() const { return i_timer; } bool getUnloadLock() const { return i_unloadActiveLockCount || i_unloadExplicitLock || i_unloadReferenceLock; } void setUnloadExplicitLock(bool on) { i_unloadExplicitLock = on; } diff --git a/src/server/game/Groups/Group.cpp b/src/server/game/Groups/Group.cpp index e9961be50f7..788feed0193 100644 --- a/src/server/game/Groups/Group.cpp +++ b/src/server/game/Groups/Group.cpp @@ -33,11 +33,13 @@ #include "MapManager.h" #include "InstanceSaveMgr.h" #include "Util.h" +#include "Random.h" #include "LFGMgr.h" #include "UpdateFieldFlags.h" #include "SpellAuras.h" #include "PartyPackets.h" #include "LootPackets.h" +#include "UpdateData.h" Roll::Roll(LootItem const& li) : itemid(li.itemid), itemRandomPropId(li.randomPropertyId), itemRandomSuffix(li.randomSuffix), itemCount(li.count), diff --git a/src/server/game/Handlers/MiscHandler.cpp b/src/server/game/Handlers/MiscHandler.cpp index 69c65e91348..0805da1eae3 100644 --- a/src/server/game/Handlers/MiscHandler.cpp +++ b/src/server/game/Handlers/MiscHandler.cpp @@ -49,6 +49,8 @@ #include "InstancePackets.h" #include "InstanceScript.h" #include "AreaTriggerPackets.h" +#include <boost/thread/shared_mutex.hpp> +#include <boost/thread/locks.hpp> void WorldSession::HandleRepopRequest(WorldPackets::Misc::RepopRequest& /*packet*/) { diff --git a/src/server/game/Handlers/NPCHandler.h b/src/server/game/Handlers/NPCHandler.h index d3c702f9d17..04bf128edab 100644 --- a/src/server/game/Handlers/NPCHandler.h +++ b/src/server/game/Handlers/NPCHandler.h @@ -34,7 +34,7 @@ struct NpcText struct PageTextLocale { - StringVector Text; + std::vector<std::string> Text; }; #endif diff --git a/src/server/game/Maps/Map.h b/src/server/game/Maps/Map.h index 38eca73adf2..0854090bc3f 100644 --- a/src/server/game/Maps/Map.h +++ b/src/server/game/Maps/Map.h @@ -34,6 +34,7 @@ #include <bitset> #include <list> #include <memory> +#include <mutex> class Battleground; class BattlegroundMap; diff --git a/src/server/game/Movement/MovementGenerators/ConfusedMovementGenerator.cpp b/src/server/game/Movement/MovementGenerators/ConfusedMovementGenerator.cpp index b54aef633ef..84e7640ac6b 100755 --- a/src/server/game/Movement/MovementGenerators/ConfusedMovementGenerator.cpp +++ b/src/server/game/Movement/MovementGenerators/ConfusedMovementGenerator.cpp @@ -22,6 +22,7 @@ #include "MoveSplineInit.h" #include "MoveSpline.h" #include "Player.h" +#include "Random.h" template<class T> void ConfusedMovementGenerator<T>::DoInitialize(T* unit) diff --git a/src/server/game/Movement/MovementGenerators/RandomMovementGenerator.cpp b/src/server/game/Movement/MovementGenerators/RandomMovementGenerator.cpp index ad6761d6218..fe501b20cb5 100644 --- a/src/server/game/Movement/MovementGenerators/RandomMovementGenerator.cpp +++ b/src/server/game/Movement/MovementGenerators/RandomMovementGenerator.cpp @@ -23,6 +23,7 @@ #include "CreatureGroups.h" #include "MoveSplineInit.h" #include "MoveSpline.h" +#include "Random.h" #define RUNNING_CHANCE_RANDOMMV 20 //will be "1 / RUNNING_CHANCE_RANDOMMV" diff --git a/src/server/game/Movement/Spline/MoveSplineInitArgs.h b/src/server/game/Movement/Spline/MoveSplineInitArgs.h index a90b2012ae0..d9688674dfc 100644 --- a/src/server/game/Movement/Spline/MoveSplineInitArgs.h +++ b/src/server/game/Movement/Spline/MoveSplineInitArgs.h @@ -21,6 +21,7 @@ #include "MoveSplineFlag.h" #include "ObjectGuid.h" +#include "Optional.h" #include <G3D/Vector3.h> class Unit; diff --git a/src/server/game/Movement/Spline/MovementUtil.cpp b/src/server/game/Movement/Spline/MovementUtil.cpp index dc025403ba5..58b21602270 100644 --- a/src/server/game/Movement/Spline/MovementUtil.cpp +++ b/src/server/game/Movement/Spline/MovementUtil.cpp @@ -17,6 +17,7 @@ */ #include "MoveSplineFlag.h" +#include <cmath> namespace Movement { diff --git a/src/server/game/Movement/Spline/Spline.h b/src/server/game/Movement/Spline/Spline.h index cf17fda4f4a..e0f7cdce7fc 100644 --- a/src/server/game/Movement/Spline/Spline.h +++ b/src/server/game/Movement/Spline/Spline.h @@ -20,6 +20,7 @@ #define TRINITYSERVER_SPLINE_H #include "MovementTypedefs.h" +#include "Errors.h" #include <G3D/Vector3.h> #include <limits> diff --git a/src/server/game/Quests/QuestDef.h b/src/server/game/Quests/QuestDef.h index aa8d99d5301..15eb95280b9 100644 --- a/src/server/game/Quests/QuestDef.h +++ b/src/server/game/Quests/QuestDef.h @@ -255,30 +255,30 @@ enum QuestObjectiveFlags struct QuestTemplateLocale { - StringVector LogTitle; - StringVector LogDescription; - StringVector QuestDescription; - StringVector AreaDescription; - StringVector PortraitGiverText; - StringVector PortraitGiverName; - StringVector PortraitTurnInText; - StringVector PortraitTurnInName; - StringVector QuestCompletionLog; + std::vector<std::string> LogTitle; + std::vector<std::string> LogDescription; + std::vector<std::string> QuestDescription; + std::vector<std::string> AreaDescription; + std::vector<std::string> PortraitGiverText; + std::vector<std::string> PortraitGiverName; + std::vector<std::string> PortraitTurnInText; + std::vector<std::string> PortraitTurnInName; + std::vector<std::string> QuestCompletionLog; }; struct QuestRequestItemsLocale { - StringVector CompletionText; + std::vector<std::string> CompletionText; }; struct QuestObjectivesLocale { - StringVector Description; + std::vector<std::string> Description; }; struct QuestOfferRewardLocale { - StringVector RewardText; + std::vector<std::string> RewardText; }; struct QuestObjective diff --git a/src/server/game/Scenarios/ScenarioMgr.h b/src/server/game/Scenarios/ScenarioMgr.h index 08861e83ef7..94fb64df41f 100644 --- a/src/server/game/Scenarios/ScenarioMgr.h +++ b/src/server/game/Scenarios/ScenarioMgr.h @@ -19,7 +19,11 @@ #define ScenarioMgr_h__ #include "Common.h" +#include "Hash.h" #include "SharedDefines.h" +#include <map> +#include <unordered_map> +#include <vector> class InstanceScenario; class Map; diff --git a/src/server/game/Scripting/ScriptReloadMgr.cpp b/src/server/game/Scripting/ScriptReloadMgr.cpp index 3079c548b71..e4f15c66431 100644 --- a/src/server/game/Scripting/ScriptReloadMgr.cpp +++ b/src/server/game/Scripting/ScriptReloadMgr.cpp @@ -17,6 +17,7 @@ #include "ScriptReloadMgr.h" #include "Errors.h" +#include "Optional.h" #ifndef TRINITY_API_USE_DYNAMIC_LINKING diff --git a/src/server/game/Server/Packets/AreaTriggerPackets.h b/src/server/game/Server/Packets/AreaTriggerPackets.h index 967a8ba0f5f..02e30093501 100644 --- a/src/server/game/Server/Packets/AreaTriggerPackets.h +++ b/src/server/game/Server/Packets/AreaTriggerPackets.h @@ -21,6 +21,7 @@ #include "Packet.h" #include "PacketUtilities.h" #include "ObjectGuid.h" +#include "Optional.h" namespace WorldPackets { diff --git a/src/server/game/Server/Packets/BattlegroundPackets.h b/src/server/game/Server/Packets/BattlegroundPackets.h index 967829c2c17..725edc21d41 100644 --- a/src/server/game/Server/Packets/BattlegroundPackets.h +++ b/src/server/game/Server/Packets/BattlegroundPackets.h @@ -23,6 +23,7 @@ #include "LFGPackets.h" #include "PacketUtilities.h" #include "Packet.h" +#include "Optional.h" namespace WorldPackets { diff --git a/src/server/game/Server/Packets/BattlenetPackets.h b/src/server/game/Server/Packets/BattlenetPackets.h index 6b7a8c9c50e..ba3f4299ab9 100644 --- a/src/server/game/Server/Packets/BattlenetPackets.h +++ b/src/server/game/Server/Packets/BattlenetPackets.h @@ -21,6 +21,7 @@ #include "Packet.h" #include "MessageBuffer.h" #include "BattlenetRpcErrorCodes.h" +#include <array> namespace WorldPackets { diff --git a/src/server/game/Server/Packets/GuildFinderPackets.h b/src/server/game/Server/Packets/GuildFinderPackets.h index 1cc3b00094c..843b39e1c32 100644 --- a/src/server/game/Server/Packets/GuildFinderPackets.h +++ b/src/server/game/Server/Packets/GuildFinderPackets.h @@ -20,6 +20,7 @@ #include "Packet.h" #include "ObjectGuid.h" +#include "Optional.h" namespace WorldPackets { diff --git a/src/server/game/Server/Packets/GuildPackets.cpp b/src/server/game/Server/Packets/GuildPackets.cpp index d9bb746eb73..0b5f38909ea 100644 --- a/src/server/game/Server/Packets/GuildPackets.cpp +++ b/src/server/game/Server/Packets/GuildPackets.cpp @@ -749,7 +749,9 @@ ByteBuffer& operator<<(ByteBuffer& data, WorldPackets::Guild::GuildNewsEvent con WorldPacket const* WorldPackets::Guild::GuildNews::Write() { - _worldPacket << NewsEvents; + _worldPacket << uint32(NewsEvents.size()); + for (GuildNewsEvent const& newsEvent : NewsEvents) + _worldPacket << newsEvent; return &_worldPacket; } diff --git a/src/server/game/Server/Packets/HotfixPackets.h b/src/server/game/Server/Packets/HotfixPackets.h index eb87ddba1e6..884478ee45b 100644 --- a/src/server/game/Server/Packets/HotfixPackets.h +++ b/src/server/game/Server/Packets/HotfixPackets.h @@ -22,6 +22,7 @@ #include "Common.h" #include "DB2Stores.h" #include "ObjectGuid.h" +#include "Optional.h" namespace WorldPackets { diff --git a/src/server/game/Server/Packets/ItemPackets.h b/src/server/game/Server/Packets/ItemPackets.h index 6b813c82d54..012e6800174 100644 --- a/src/server/game/Server/Packets/ItemPackets.h +++ b/src/server/game/Server/Packets/ItemPackets.h @@ -21,6 +21,7 @@ #include "Packet.h" #include "Item.h" #include "PacketUtilities.h" +#include "Optional.h" struct VoidStorageItem; diff --git a/src/server/game/Server/Packets/MovementPackets.h b/src/server/game/Server/Packets/MovementPackets.h index 2695db47e4b..243faeec7ad 100644 --- a/src/server/game/Server/Packets/MovementPackets.h +++ b/src/server/game/Server/Packets/MovementPackets.h @@ -20,7 +20,7 @@ #include "Packet.h" #include "Object.h" - +#include "Optional.h" #include <G3D/Vector3.h> namespace Movement diff --git a/src/server/game/Server/Packets/PartyPackets.h b/src/server/game/Server/Packets/PartyPackets.h index 3fbbc3381a7..d4c03e3b78c 100644 --- a/src/server/game/Server/Packets/PartyPackets.h +++ b/src/server/game/Server/Packets/PartyPackets.h @@ -21,6 +21,7 @@ #include "Packet.h" #include "ObjectGuid.h" #include "Group.h" +#include "Optional.h" namespace WorldPackets { diff --git a/src/server/game/Server/Packets/SystemPackets.h b/src/server/game/Server/Packets/SystemPackets.h index 362fdb3af8b..7440bebd853 100644 --- a/src/server/game/Server/Packets/SystemPackets.h +++ b/src/server/game/Server/Packets/SystemPackets.h @@ -19,6 +19,7 @@ #define SystemPackets_h__ #include "Packet.h" +#include "Optional.h" namespace WorldPackets { diff --git a/src/server/game/Server/Packets/TaxiPackets.h b/src/server/game/Server/Packets/TaxiPackets.h index 2fd002aacf1..c9d7ba8e061 100644 --- a/src/server/game/Server/Packets/TaxiPackets.h +++ b/src/server/game/Server/Packets/TaxiPackets.h @@ -21,6 +21,7 @@ #include "Packet.h" #include "ObjectGuid.h" #include "DBCEnums.h" +#include "Optional.h" namespace WorldPackets { diff --git a/src/server/game/Server/Packets/TicketPackets.h b/src/server/game/Server/Packets/TicketPackets.h index 27866e767ef..4cce35a0b85 100644 --- a/src/server/game/Server/Packets/TicketPackets.h +++ b/src/server/game/Server/Packets/TicketPackets.h @@ -20,6 +20,7 @@ #include "Packet.h" #include "LFGPackets.h" +#include "Optional.h" #include <G3D/Vector3.h> namespace WorldPackets diff --git a/src/server/game/Server/Protocol/Opcodes.cpp b/src/server/game/Server/Protocol/Opcodes.cpp index 56f5cd1128d..103f3ebbd4d 100644 --- a/src/server/game/Server/Protocol/Opcodes.cpp +++ b/src/server/game/Server/Protocol/Opcodes.cpp @@ -17,8 +17,11 @@ */ #include "Opcodes.h" +#include "Log.h" #include "WorldSession.h" #include "Packets/AllPackets.h" +#include <iomanip> +#include <sstream> template<class PacketClass, void(WorldSession::*HandlerFunction)(PacketClass&)> class PacketHandler : public ClientOpcodeHandler @@ -61,6 +64,21 @@ struct get_packet_class<void(WorldSession::*)(PacketClass&)> using type = PacketClass; }; +OpcodeTable::OpcodeTable() +{ + memset(_internalTableClient, 0, sizeof(_internalTableClient)); + memset(_internalTableServer, 0, sizeof(_internalTableServer)); +} + +OpcodeTable::~OpcodeTable() +{ + for (uint16 i = 0; i < NUM_OPCODE_HANDLERS; ++i) + { + delete _internalTableClient[i]; + delete _internalTableServer[i]; + } +} + template<typename Handler, Handler HandlerFunction> void OpcodeTable::ValidateAndSetClientOpcode(OpcodeClient opcode, char const* name, SessionStatus status, PacketProcessing processing) { @@ -1818,3 +1836,34 @@ void OpcodeTable::Initialize() #undef DEFINE_SERVER_OPCODE_HANDLER }; + +template<typename T> +inline std::string GetOpcodeNameForLoggingImpl(T id) +{ + uint32 opcode = uint32(id); + std::ostringstream ss; + ss << '['; + + if (static_cast<uint32>(id) < NUM_OPCODE_HANDLERS) + { + if (OpcodeHandler const* handler = opcodeTable[id]) + ss << handler->Name; + else + ss << "UNKNOWN OPCODE"; + } + else + ss << "INVALID OPCODE"; + + ss << " 0x" << std::hex << std::setw(4) << std::setfill('0') << std::uppercase << opcode << std::nouppercase << std::dec << " (" << opcode << ")]"; + return ss.str(); +} + +std::string GetOpcodeNameForLogging(OpcodeClient opcode) +{ + return GetOpcodeNameForLoggingImpl(opcode); +} + +std::string GetOpcodeNameForLogging(OpcodeServer opcode) +{ + return GetOpcodeNameForLoggingImpl(opcode); +} diff --git a/src/server/game/Server/Protocol/Opcodes.h b/src/server/game/Server/Protocol/Opcodes.h index c764b6d669e..58834139c01 100644 --- a/src/server/game/Server/Protocol/Opcodes.h +++ b/src/server/game/Server/Protocol/Opcodes.h @@ -23,10 +23,10 @@ #ifndef _OPCODES_H #define _OPCODES_H -#include "Common.h" -#include <iomanip> +#include "Define.h" +#include <string> -enum ConnectionType +enum ConnectionType : int8 { CONNECTION_TYPE_REALM = 0, CONNECTION_TYPE_INSTANCE = 1, @@ -1785,8 +1785,6 @@ enum PacketProcessing class WorldPacket; class WorldSession; -#pragma pack(push, 1) - class OpcodeHandler { public: @@ -1820,23 +1818,12 @@ public: class OpcodeTable { public: - OpcodeTable() - { - memset(_internalTableClient, 0, sizeof(_internalTableClient)); - memset(_internalTableServer, 0, sizeof(_internalTableServer)); - } + OpcodeTable(); OpcodeTable(OpcodeTable const&) = delete; OpcodeTable& operator=(OpcodeTable const&) = delete; - ~OpcodeTable() - { - for (uint16 i = 0; i < NUM_OPCODE_HANDLERS; ++i) - { - delete _internalTableClient[i]; - delete _internalTableServer[i]; - } - } + ~OpcodeTable(); void Initialize(); @@ -1862,29 +1849,9 @@ class OpcodeTable extern OpcodeTable opcodeTable; -#pragma pack(pop) - -/// Lookup opcode name for human understandable logging (T = OpcodeClient|OpcodeServer) -template<typename T> -inline std::string GetOpcodeNameForLogging(T id) -{ - uint32 opcode = uint32(id); - std::ostringstream ss; - ss << '['; - - if (static_cast<uint32>(id) < NUM_OPCODE_HANDLERS) - { - if (OpcodeHandler const* handler = opcodeTable[id]) - ss << handler->Name; - else - ss << "UNKNOWN OPCODE"; - } - else - ss << "INVALID OPCODE"; - - ss << " 0x" << std::hex << std::setw(4) << std::setfill('0') << std::uppercase << opcode << std::nouppercase << std::dec << " (" << opcode << ")]"; - return ss.str(); -} +/// Lookup opcode name for human understandable logging +std::string GetOpcodeNameForLogging(OpcodeClient opcode); +std::string GetOpcodeNameForLogging(OpcodeServer opcode); #endif /// @} diff --git a/src/server/game/Server/WorldSession.cpp b/src/server/game/Server/WorldSession.cpp index 62e83ce253e..cd0de310b75 100644 --- a/src/server/game/Server/WorldSession.cpp +++ b/src/server/game/Server/WorldSession.cpp @@ -53,6 +53,7 @@ #include "PacketUtilities.h" #include "CollectionMgr.h" #include "Metric.h" +#include "Random.h" #include <zlib.h> diff --git a/src/server/game/Skills/SkillDiscovery.cpp b/src/server/game/Skills/SkillDiscovery.cpp index e762f5347ce..6b4ce80e1f2 100644 --- a/src/server/game/Skills/SkillDiscovery.cpp +++ b/src/server/game/Skills/SkillDiscovery.cpp @@ -23,6 +23,7 @@ #include "SkillDiscovery.h" #include "SpellMgr.h" #include "Player.h" +#include "Random.h" #include "SpellInfo.h" #include <map> diff --git a/src/server/game/Spells/SpellMgr.h b/src/server/game/Spells/SpellMgr.h index 77220b378c4..2d8b4326454 100644 --- a/src/server/game/Spells/SpellMgr.h +++ b/src/server/game/Spells/SpellMgr.h @@ -22,6 +22,7 @@ // For static or at-server-startup loaded spell data #include "Define.h" +#include "Duration.h" #include "SharedDefines.h" #include "Util.h" diff --git a/src/server/game/Texts/CreatureTextMgr.h b/src/server/game/Texts/CreatureTextMgr.h index 7e69f017429..943101c852e 100644 --- a/src/server/game/Texts/CreatureTextMgr.h +++ b/src/server/game/Texts/CreatureTextMgr.h @@ -53,7 +53,7 @@ struct CreatureTextEntry struct CreatureTextLocale { - StringVector Text; + std::vector<std::string> Text; }; struct CreatureTextId diff --git a/src/server/game/Warden/WardenWin.cpp b/src/server/game/Warden/WardenWin.cpp index b73a0af4361..6c7fdce98aa 100644 --- a/src/server/game/Warden/WardenWin.cpp +++ b/src/server/game/Warden/WardenWin.cpp @@ -32,6 +32,7 @@ #include "WardenModuleWin.h" #include "WardenCheckMgr.h" #include "SHA1.h" +#include "Random.h" #include <openssl/md5.h> diff --git a/src/server/game/Weather/Weather.cpp b/src/server/game/Weather/Weather.cpp index 1684b7bf6f3..37be7e40e85 100644 --- a/src/server/game/Weather/Weather.cpp +++ b/src/server/game/Weather/Weather.cpp @@ -29,6 +29,7 @@ #include "ScriptMgr.h" #include "WorldSession.h" #include "MiscPackets.h" +#include "Random.h" /// Create the Weather object Weather::Weather(uint32 zone, WeatherData const* weatherChances) diff --git a/src/server/game/World/World.h b/src/server/game/World/World.h index 4bab6c0fb3b..bb763ec4f55 100644 --- a/src/server/game/World/World.h +++ b/src/server/game/World/World.h @@ -31,11 +31,13 @@ #include "QueryResult.h" #include "QueryCallbackProcessor.h" #include "Realm/Realm.h" +#include "LockedQueue.h" #include <atomic> #include <map> #include <set> #include <list> +#include <unordered_map> class Object; class WorldPacket; diff --git a/src/server/scripts/Commands/cs_gm.cpp b/src/server/scripts/Commands/cs_gm.cpp index c47be969620..17abb11f790 100644 --- a/src/server/scripts/Commands/cs_gm.cpp +++ b/src/server/scripts/Commands/cs_gm.cpp @@ -30,6 +30,8 @@ EndScriptData */ #include "World.h" #include "Player.h" #include "Opcodes.h" +#include <boost/thread/shared_mutex.hpp> +#include <boost/thread/locks.hpp> class gm_commandscript : public CommandScript { diff --git a/src/server/scripts/Commands/cs_misc.cpp b/src/server/scripts/Commands/cs_misc.cpp index 88e968b6621..678c7342eda 100644 --- a/src/server/scripts/Commands/cs_misc.cpp +++ b/src/server/scripts/Commands/cs_misc.cpp @@ -15,6 +15,7 @@ * with this program. If not, see <http://www.gnu.org/licenses/>. */ +#include "DB2Stores.h" #include "Chat.h" #include "ScriptMgr.h" #include "AccountMgr.h" @@ -41,6 +42,11 @@ #include "Transport.h" #include "MapManager.h" + // temporary hack until database includes are sorted out (don't want to pull in Windows.h everywhere from mysql.h) +#ifdef GetClassName +#undef GetClassName +#endif + class misc_commandscript : public CommandScript { public: diff --git a/src/server/scripts/Commands/cs_reset.cpp b/src/server/scripts/Commands/cs_reset.cpp index c879c3c4c0b..743081203c0 100644 --- a/src/server/scripts/Commands/cs_reset.cpp +++ b/src/server/scripts/Commands/cs_reset.cpp @@ -29,6 +29,8 @@ EndScriptData */ #include "Player.h" #include "Pet.h" #include "ScriptMgr.h" +#include <boost/thread/shared_mutex.hpp> +#include <boost/thread/locks.hpp> class reset_commandscript : public CommandScript { diff --git a/src/server/scripts/Spells/spell_paladin.cpp b/src/server/scripts/Spells/spell_paladin.cpp index f9b245ef053..ba8d6eaa17b 100644 --- a/src/server/scripts/Spells/spell_paladin.cpp +++ b/src/server/scripts/Spells/spell_paladin.cpp @@ -27,6 +27,7 @@ #include "SpellAuraEffects.h" #include "SpellHistory.h" #include "Group.h" +#include "Random.h" enum PaladinSpells { diff --git a/src/server/shared/JSON/ProtobufJSON.cpp b/src/server/shared/JSON/ProtobufJSON.cpp index 4d14cf199de..68e46f3fd57 100644 --- a/src/server/shared/JSON/ProtobufJSON.cpp +++ b/src/server/shared/JSON/ProtobufJSON.cpp @@ -18,6 +18,7 @@ #include "ProtobufJSON.h" #include "StringFormat.h" #include "Common.h" +#include "Errors.h" #include "Log.h" #include <rapidjson/writer.h> #include <rapidjson/reader.h> diff --git a/src/server/shared/Packets/ByteBuffer.h b/src/server/shared/Packets/ByteBuffer.h index 3933f617910..82cc00a0f4e 100644 --- a/src/server/shared/Packets/ByteBuffer.h +++ b/src/server/shared/Packets/ByteBuffer.h @@ -24,6 +24,7 @@ #include "ByteConverter.h" #include "Util.h" #include <cstring> +#include <ctime> class MessageBuffer; @@ -679,85 +680,6 @@ class TC_SHARED_API ByteBuffer std::vector<uint8> _storage; }; -template <typename T> -inline ByteBuffer &operator<<(ByteBuffer &b, std::vector<T> v) -{ - b << (uint32)v.size(); - for (typename std::vector<T>::iterator i = v.begin(); i != v.end(); ++i) - { - b << *i; - } - return b; -} - -template <typename T> -inline ByteBuffer &operator>>(ByteBuffer &b, std::vector<T> &v) -{ - uint32 vsize; - b >> vsize; - v.clear(); - while (vsize--) - { - T t; - b >> t; - v.push_back(t); - } - return b; -} - -template <typename T> -inline ByteBuffer &operator<<(ByteBuffer &b, std::list<T> v) -{ - b << (uint32)v.size(); - for (typename std::list<T>::iterator i = v.begin(); i != v.end(); ++i) - { - b << *i; - } - return b; -} - -template <typename T> -inline ByteBuffer &operator>>(ByteBuffer &b, std::list<T> &v) -{ - uint32 vsize; - b >> vsize; - v.clear(); - while (vsize--) - { - T t; - b >> t; - v.push_back(t); - } - return b; -} - -template <typename K, typename V> -inline ByteBuffer &operator<<(ByteBuffer &b, std::map<K, V> &m) -{ - b << (uint32)m.size(); - for (typename std::map<K, V>::iterator i = m.begin(); i != m.end(); ++i) - { - b << i->first << i->second; - } - return b; -} - -template <typename K, typename V> -inline ByteBuffer &operator>>(ByteBuffer &b, std::map<K, V> &m) -{ - uint32 msize; - b >> msize; - m.clear(); - while (msize--) - { - K k; - V v; - b >> k >> v; - m.insert(make_pair(k, v)); - } - return b; -} - /// @todo Make a ByteBuffer.cpp and move all this inlining to it. template<> inline std::string ByteBuffer::read<std::string>() { diff --git a/src/server/shared/Realm/RealmList.h b/src/server/shared/Realm/RealmList.h index 92e8d9f7445..594ca2bf1f6 100644 --- a/src/server/shared/Realm/RealmList.h +++ b/src/server/shared/Realm/RealmList.h @@ -25,6 +25,7 @@ #include <boost/asio/ip/tcp.hpp> #include <boost/asio/io_service.hpp> #include <boost/asio/deadline_timer.hpp> +#include <unordered_set> using namespace boost::asio; diff --git a/src/server/worldserver/Main.cpp b/src/server/worldserver/Main.cpp index 2136b5f7654..4a42305139a 100644 --- a/src/server/worldserver/Main.cpp +++ b/src/server/worldserver/Main.cpp @@ -210,7 +210,7 @@ extern int main(int argc, char** argv) threadPool->push_back(std::thread([ioService]() { ioService->run(); })); // Set process priority according to configuration settings - SetProcessPriority("server.worldserver"); + SetProcessPriority("server.worldserver", sConfigMgr->GetIntDefault(CONFIG_PROCESSOR_AFFINITY, 0), sConfigMgr->GetBoolDefault(CONFIG_HIGH_PRIORITY, false)); // Start the databases if (!StartDB()) |