Skip to content

Commit

Permalink
Core: Backport 6.x realm changes
Browse files Browse the repository at this point in the history
Make acessible all the info about current realm (e.g name) anywhere, not only realm id
Reduce the number of differences between the two branches

Original changes by Shauren

Partial port of bacc90b and 63def8a
  • Loading branch information
DDuarte committed Feb 28, 2016
1 parent ffdf75a commit e7b0092
Show file tree
Hide file tree
Showing 23 changed files with 267 additions and 128 deletions.
1 change: 1 addition & 0 deletions src/server/authserver/CMakeLists.txt
Expand Up @@ -57,6 +57,7 @@ include_directories(
${CMAKE_SOURCE_DIR}/src/server/database
${CMAKE_SOURCE_DIR}/src/server/database/Database
${CMAKE_SOURCE_DIR}/src/server/database/Logging
${CMAKE_SOURCE_DIR}/src/server/shared
${CMAKE_SOURCE_DIR}/src/server/shared/Networking
${CMAKE_SOURCE_DIR}/src/server/shared/Packets
${CMAKE_SOURCE_DIR}/src/server/shared/Service
Expand Down
2 changes: 1 addition & 1 deletion src/server/authserver/Main.cpp
Expand Up @@ -134,7 +134,7 @@ int main(int argc, char** argv)
// Get the list of realms for the server
sRealmList->Initialize(*_ioService, sConfigMgr->GetIntDefault("RealmsStateUpdateDelay", 20));

if (sRealmList->size() == 0)
if (sRealmList->GetRealms().empty())
{
TC_LOG_ERROR("server.authserver", "No valid realms specified.");
StopDB();
Expand Down
65 changes: 39 additions & 26 deletions src/server/authserver/Realms/RealmList.cpp
Expand Up @@ -16,14 +16,14 @@
* with this program. If not, see <http://www.gnu.org/licenses/>.
*/

#include <boost/asio/ip/tcp.hpp>
#include "Common.h"
#include "RealmList.h"
#include "Database/DatabaseEnv.h"
#include "RealmList.h"
#include <boost/asio/ip/tcp.hpp>

namespace boost { namespace asio { namespace ip { class address; } } }

RealmList::RealmList() : m_UpdateInterval(0), m_NextUpdateTime(time(NULL)), _resolver(nullptr) { }
RealmList::RealmList() : _updateInterval(0), _nextUpdateTime(time(NULL)), _resolver(nullptr) { }
RealmList::~RealmList()
{
delete _resolver;
Expand All @@ -33,45 +33,43 @@ RealmList::~RealmList()
void RealmList::Initialize(boost::asio::io_service& ioService, uint32 updateInterval)
{
_resolver = new boost::asio::ip::tcp::resolver(ioService);
m_UpdateInterval = updateInterval;
_updateInterval = updateInterval;

// Get the content of the realmlist table in the database
UpdateRealms(true);
}

void RealmList::UpdateRealm(uint32 id, const std::string& name, ip::address const& address, ip::address const& localAddr,
ip::address const& localSubmask, uint16 port, uint8 icon, RealmFlags flag, uint8 timezone, AccountTypes allowedSecurityLevel, float population, uint32 build)
void RealmList::UpdateRealm(RealmHandle const& id, uint32 build, const std::string& name, ip::address const& address, ip::address const& localAddr,
ip::address const& localSubmask, uint16 port, uint8 icon, RealmFlags flag, uint8 timezone, AccountTypes allowedSecurityLevel,
float population)
{
// Create new if not exist or update existed
Realm& realm = m_realms[name];

realm.m_ID = id;
realm.name = name;
realm.icon = icon;
realm.flag = flag;
realm.timezone = timezone;
realm.allowedSecurityLevel = allowedSecurityLevel;
realm.populationLevel = population;

// Append port to IP address.

Realm& realm = _realms[id];

realm.Id = id;
realm.Build = build;
realm.Name = name;
realm.Type = icon;
realm.Flags = flag;
realm.Timezone = timezone;
realm.AllowedSecurityLevel = allowedSecurityLevel;
realm.PopulationLevel = population;
realm.ExternalAddress = address;
realm.LocalAddress = localAddr;
realm.LocalSubnetMask = localSubmask;
realm.port = port;
realm.gamebuild = build;
realm.Port = port;
}

void RealmList::UpdateIfNeed()
{
// maybe disabled or updated recently
if (!m_UpdateInterval || m_NextUpdateTime > time(NULL))
if (!_updateInterval || _nextUpdateTime > time(NULL))
return;

m_NextUpdateTime = time(NULL) + m_UpdateInterval;
_nextUpdateTime = time(NULL) + _updateInterval;

// Clears Realm list
m_realms.clear();
_realms.clear();

// Get the content of the realmlist table in the database
UpdateRealms();
Expand Down Expand Up @@ -130,17 +128,23 @@ void RealmList::UpdateRealms(bool init)

uint16 port = fields[5].GetUInt16();
uint8 icon = fields[6].GetUInt8();
if (icon == REALM_TYPE_FFA_PVP)
icon = REALM_TYPE_PVP;
if (icon >= MAX_CLIENT_REALM_TYPE)
icon = REALM_TYPE_NORMAL;
RealmFlags flag = RealmFlags(fields[7].GetUInt8());
uint8 timezone = fields[8].GetUInt8();
uint8 allowedSecurityLevel = fields[9].GetUInt8();
float pop = fields[10].GetFloat();
uint32 build = fields[11].GetUInt32();

UpdateRealm(realmId, name, externalAddress, localAddress, localSubmask, port, icon, flag, timezone,
(allowedSecurityLevel <= SEC_ADMINISTRATOR ? AccountTypes(allowedSecurityLevel) : SEC_ADMINISTRATOR), pop, build);
RealmHandle id{ realmId };

UpdateRealm(id, build, name, externalAddress, localAddress, localSubmask, port, icon, flag,
timezone, (allowedSecurityLevel <= SEC_ADMINISTRATOR ? AccountTypes(allowedSecurityLevel) : SEC_ADMINISTRATOR), pop);

if (init)
TC_LOG_INFO("server.authserver", "Added realm \"%s\" at %s:%u.", name.c_str(), m_realms[name].ExternalAddress.to_string().c_str(), port);
TC_LOG_INFO("server.authserver", "Added realm \"%s\" at %s:%u.", name.c_str(), externalAddress.to_string().c_str(), port);
}
catch (std::exception& ex)
{
Expand All @@ -151,3 +155,12 @@ void RealmList::UpdateRealms(bool init)
while (result->NextRow());
}
}

Realm const* RealmList::GetRealm(RealmHandle const& id) const
{
auto itr = _realms.find(id);
if (itr != _realms.end())
return &itr->second;

return NULL;
}
52 changes: 11 additions & 41 deletions src/server/authserver/Realms/RealmList.h
Expand Up @@ -19,48 +19,19 @@
#ifndef _REALMLIST_H
#define _REALMLIST_H

#include "Common.h"
#include "Realm/Realm.h"
#include <boost/asio/ip/address.hpp>
#include <boost/asio/ip/tcp.hpp>
#include <boost/asio/io_service.hpp>
#include "Common.h"

using namespace boost::asio;

enum RealmFlags
{
REALM_FLAG_NONE = 0x00,
REALM_FLAG_INVALID = 0x01,
REALM_FLAG_OFFLINE = 0x02,
REALM_FLAG_SPECIFYBUILD = 0x04,
REALM_FLAG_UNK1 = 0x08,
REALM_FLAG_UNK2 = 0x10,
REALM_FLAG_RECOMMENDED = 0x20,
REALM_FLAG_NEW = 0x40,
REALM_FLAG_FULL = 0x80
};

// Storage object for a realm
struct Realm
{
ip::address ExternalAddress;
ip::address LocalAddress;
ip::address LocalSubnetMask;
uint16 port;
std::string name;
uint8 icon;
RealmFlags flag;
uint8 timezone;
uint32 m_ID;
AccountTypes allowedSecurityLevel;
float populationLevel;
uint32 gamebuild;
};

/// Storage object for the list of realms on the server
class RealmList
{
public:
typedef std::map<std::string, Realm> RealmMap;
typedef std::map<RealmHandle, Realm> RealmMap;

static RealmList* instance()
{
Expand All @@ -74,22 +45,21 @@ class RealmList

void UpdateIfNeed();

void AddRealm(const Realm& NewRealm) { m_realms[NewRealm.name] = NewRealm; }
void AddRealm(const Realm& NewRealm) { _realms[NewRealm.Id] = NewRealm; }

RealmMap::const_iterator begin() const { return m_realms.begin(); }
RealmMap::const_iterator end() const { return m_realms.end(); }
uint32 size() const { return m_realms.size(); }
RealmMap const& GetRealms() const { return _realms; }
Realm const* GetRealm(RealmHandle const& id) const;

private:
RealmList();

void UpdateRealms(bool init = false);
void UpdateRealm(uint32 id, const std::string& name, ip::address const& address, ip::address const& localAddr,
ip::address const& localSubmask, uint16 port, uint8 icon, RealmFlags flag, uint8 timezone, AccountTypes allowedSecurityLevel, float population, uint32 build);
void UpdateRealm(RealmHandle const& id, uint32 build, const std::string& name, ip::address const& address, ip::address const& localAddr,
ip::address const& localSubmask, uint16 port, uint8 icon, RealmFlags flag, uint8 timezone, AccountTypes allowedSecurityLevel, float population);

RealmMap m_realms;
uint32 m_UpdateInterval;
time_t m_NextUpdateTime;
RealmMap _realms;
uint32 _updateInterval;
time_t _nextUpdateTime;
boost::asio::ip::tcp::resolver* _resolver;
};

Expand Down
26 changes: 13 additions & 13 deletions src/server/authserver/Server/AuthSession.cpp
Expand Up @@ -844,7 +844,7 @@ tcp::endpoint const GetAddressForClient(Realm const& realm, ip::address const& c
realmIp = realm.ExternalAddress;
}

tcp::endpoint endpoint(realmIp, realm.port);
tcp::endpoint endpoint(realmIp, realm.Port);

// Return external IP
return endpoint;
Expand Down Expand Up @@ -887,15 +887,15 @@ void AuthSession::RealmListCallback(PreparedQueryResult result)
ByteBuffer pkt;

size_t RealmListSize = 0;
for (RealmList::RealmMap::const_iterator i = sRealmList->begin(); i != sRealmList->end(); ++i)
for (RealmList::RealmMap::value_type const& i : sRealmList->GetRealms())
{
const Realm &realm = i->second;
const Realm &realm = i.second;
// don't work with realms which not compatible with the client
bool okBuild = ((_expversion & POST_BC_EXP_FLAG) && realm.gamebuild == _build) || ((_expversion & PRE_BC_EXP_FLAG) && !AuthHelper::IsPreBCAcceptedClientBuild(realm.gamebuild));
bool okBuild = ((_expversion & POST_BC_EXP_FLAG) && realm.Build == _build) || ((_expversion & PRE_BC_EXP_FLAG) && !AuthHelper::IsPreBCAcceptedClientBuild(realm.Build));

// No SQL injection. id of realm is controlled by the database.
uint32 flag = realm.flag;
RealmBuildInfo const* buildInfo = AuthHelper::GetBuildInfo(realm.gamebuild);
uint32 flag = realm.Flags;
RealmBuildInfo const* buildInfo = AuthHelper::GetBuildInfo(realm.Build);
if (!okBuild)
{
if (!buildInfo)
Expand All @@ -907,27 +907,27 @@ void AuthSession::RealmListCallback(PreparedQueryResult result)
if (!buildInfo)
flag &= ~REALM_FLAG_SPECIFYBUILD;

std::string name = i->first;
std::string name = realm.Name;
if (_expversion & PRE_BC_EXP_FLAG && flag & REALM_FLAG_SPECIFYBUILD)
{
std::ostringstream ss;
ss << name << " (" << buildInfo->MajorVersion << '.' << buildInfo->MinorVersion << '.' << buildInfo->BugfixVersion << ')';
name = ss.str();
}

uint8 lock = (realm.allowedSecurityLevel > _accountInfo.SecurityLevel) ? 1 : 0;
uint8 lock = (realm.AllowedSecurityLevel > _accountInfo.SecurityLevel) ? 1 : 0;

pkt << uint8(realm.icon); // realm type
pkt << uint8(realm.Type); // realm type
if (_expversion & POST_BC_EXP_FLAG) // only 2.x and 3.x clients
pkt << uint8(lock); // if 1, then realm locked
pkt << uint8(flag); // RealmFlags
pkt << name;
pkt << boost::lexical_cast<std::string>(GetAddressForClient(realm, GetRemoteIpAddress()));
pkt << float(realm.populationLevel);
pkt << uint8(characterCounts[realm.m_ID]);
pkt << uint8(realm.timezone); // realm category
pkt << float(realm.PopulationLevel);
pkt << uint8(characterCounts[realm.Id.Realm]);
pkt << uint8(realm.Timezone); // realm category
if (_expversion & POST_BC_EXP_FLAG) // 2.x and 3.x clients
pkt << uint8(realm.m_ID);
pkt << uint8(realm.Id.Realm);
else
pkt << uint8(0x0); // 1.12.1 and 1.12.2 clients

Expand Down
2 changes: 1 addition & 1 deletion src/server/game/Accounts/AccountMgr.cpp
Expand Up @@ -462,7 +462,7 @@ void AccountMgr::LoadRBAC()
while (result->NextRow());

TC_LOG_DEBUG("rbac", "AccountMgr::LoadRBAC: Loading default permissions");
result = LoginDatabase.PQuery("SELECT secId, permissionId FROM rbac_default_permissions WHERE (realmId = %u OR realmId = -1) ORDER BY secId ASC", realmID);
result = LoginDatabase.PQuery("SELECT secId, permissionId FROM rbac_default_permissions WHERE (realmId = %u OR realmId = -1) ORDER BY secId ASC", realm.Id.Realm);
if (!result)
{
TC_LOG_INFO("server.loading", ">> Loaded 0 default permission definitions. DB table `rbac_default_permissions` is empty.");
Expand Down
2 changes: 1 addition & 1 deletion src/server/game/AuctionHouse/AuctionHouseMgr.cpp
Expand Up @@ -130,7 +130,7 @@ void AuctionHouseMgr::SendAuctionWonMail(AuctionEntry* auction, SQLTransaction&
else
{
bidderAccId = sObjectMgr->GetPlayerAccountIdByGUID(bidderGuid);
logGmTrade = AccountMgr::HasPermission(bidderAccId, rbac::RBAC_PERM_LOG_GM_TRADE, realmID);
logGmTrade = AccountMgr::HasPermission(bidderAccId, rbac::RBAC_PERM_LOG_GM_TRADE, realm.Id.Realm);

if (logGmTrade && !sObjectMgr->GetPlayerNameByGUID(bidderGuid, bidderName))
bidderName = sObjectMgr->GetTrinityStringForDBCLocale(LANG_UNKNOWN);
Expand Down
1 change: 1 addition & 0 deletions src/server/game/CMakeLists.txt
Expand Up @@ -196,6 +196,7 @@ include_directories(
${CMAKE_SOURCE_DIR}/src/server/shared/Dynamic/LinkedReference
${CMAKE_SOURCE_DIR}/src/server/shared/Networking
${CMAKE_SOURCE_DIR}/src/server/shared/Packets
${CMAKE_SOURCE_DIR}/src/server/shared/Realm
${MYSQL_INCLUDE_DIR}
${OPENSSL_INCLUDE_DIR}
${VALGRIND_INCLUDE_DIR}
Expand Down
2 changes: 1 addition & 1 deletion src/server/game/Chat/Chat.cpp
Expand Up @@ -108,7 +108,7 @@ bool ChatHandler::HasLowerSecurityAccount(WorldSession* target, uint32 target_ac
if (target)
target_sec = target->GetSecurity();
else if (target_account)
target_sec = AccountMgr::GetSecurity(target_account, realmID);
target_sec = AccountMgr::GetSecurity(target_account, realm.Id.Realm);
else
return true; // caller must report error for (target == NULL && target_account == 0)

Expand Down
4 changes: 2 additions & 2 deletions src/server/game/Handlers/CharacterHandler.cpp
Expand Up @@ -627,13 +627,13 @@ void WorldSession::HandleCharCreateCallback(PreparedQueryResult result, Characte

PreparedStatement* stmt = LoginDatabase.GetPreparedStatement(LOGIN_DEL_REALM_CHARACTERS_BY_REALM);
stmt->setUInt32(0, GetAccountId());
stmt->setUInt32(1, realmID);
stmt->setUInt32(1, realm.Id.Realm);
trans->Append(stmt);

stmt = LoginDatabase.GetPreparedStatement(LOGIN_INS_REALM_CHARACTERS);
stmt->setUInt32(0, createInfo->CharCount);
stmt->setUInt32(1, GetAccountId());
stmt->setUInt32(2, realmID);
stmt->setUInt32(2, realm.Id.Realm);
trans->Append(stmt);

LoginDatabase.CommitTransaction(trans);
Expand Down
2 changes: 1 addition & 1 deletion src/server/game/Handlers/MiscHandler.cpp
Expand Up @@ -569,7 +569,7 @@ void WorldSession::HandleAddFriendOpcodeCallBack(PreparedQueryResult result, std
team = Player::TeamForRace(fields[1].GetUInt8());
friendAccountId = fields[2].GetUInt32();

if (HasPermission(rbac::RBAC_PERM_ALLOW_GM_FRIEND) || AccountMgr::IsPlayerAccount(AccountMgr::GetSecurity(friendAccountId, realmID)))
if (HasPermission(rbac::RBAC_PERM_ALLOW_GM_FRIEND) || AccountMgr::IsPlayerAccount(AccountMgr::GetSecurity(friendAccountId, realm.Id.Realm)))
{
if (friendGuid)
{
Expand Down
10 changes: 5 additions & 5 deletions src/server/game/Server/WorldSession.cpp
Expand Up @@ -1220,7 +1220,7 @@ void WorldSession::LoadPermissions()
uint32 id = GetAccountId();
uint8 secLevel = GetSecurity();

_RBACData = new rbac::RBACData(id, _accountName, realmID, secLevel);
_RBACData = new rbac::RBACData(id, _accountName, realm.Id.Realm, secLevel);
_RBACData->LoadFromDB();
}

Expand All @@ -1230,9 +1230,9 @@ PreparedQueryResultFuture WorldSession::LoadPermissionsAsync()
uint8 secLevel = GetSecurity();

TC_LOG_DEBUG("rbac", "WorldSession::LoadPermissions [AccountId: %u, Name: %s, realmId: %d, secLevel: %u]",
id, _accountName.c_str(), realmID, secLevel);
id, _accountName.c_str(), realm.Id.Realm, secLevel);

_RBACData = new rbac::RBACData(id, _accountName, realmID, secLevel);
_RBACData = new rbac::RBACData(id, _accountName, realm.Id.Realm, secLevel);
return _RBACData->LoadFromDBAsync();
}

Expand Down Expand Up @@ -1310,15 +1310,15 @@ bool WorldSession::HasPermission(uint32 permission)

bool hasPermission = _RBACData->HasPermission(permission);
TC_LOG_DEBUG("rbac", "WorldSession::HasPermission [AccountId: %u, Name: %s, realmId: %d]",
_RBACData->GetId(), _RBACData->GetName().c_str(), realmID);
_RBACData->GetId(), _RBACData->GetName().c_str(), realm.Id.Realm);

return hasPermission;
}

void WorldSession::InvalidateRBACData()
{
TC_LOG_DEBUG("rbac", "WorldSession::Invalidaterbac::RBACData [AccountId: %u, Name: %s, realmId: %d]",
_RBACData->GetId(), _RBACData->GetName().c_str(), realmID);
_RBACData->GetId(), _RBACData->GetName().c_str(), realm.Id.Realm);
delete _RBACData;
_RBACData = NULL;
}
Expand Down

0 comments on commit e7b0092

Please sign in to comment.