feat(Core/RealmList): port TrinityCore realm api (#5626)

* feat(Core/RealmList): port TrinityCore realm api

* 1

* whitespace cleanup

* Update data/sql/updates/pending_db_auth/rev_1620114805872279900.sql

Co-authored-by: Kitzunu <24550914+Kitzunu@users.noreply.github.com>

* 1

* 2

* Update data/sql/updates/pending_db_auth/rev_1620114805872279900.sql

Co-authored-by: Kitzunu <24550914+Kitzunu@users.noreply.github.com>

* `

* 1

* small corrects

* finish maybe

* realm.Id.Realm

* ws

* 1

Co-authored-by: Kitzunu <24550914+Kitzunu@users.noreply.github.com>
This commit is contained in:
Kargatum
2021-05-27 05:12:46 +07:00
committed by GitHub
parent 78e1719c80
commit ea5f5f2072
23 changed files with 536 additions and 305 deletions

View File

@@ -0,0 +1,73 @@
/*
* Copyright (C) 2016+ AzerothCore <www.azerothcore.org>, released under GNU AGPL v3 license: https://github.com/azerothcore/azerothcore-wotlk/blob/master/LICENSE-AGPL3
* Copyright (C) 2021+ WarheadCore <https://github.com/WarheadCore>
*/
#ifndef Realm_h__
#define Realm_h__
#include "Common.h"
// #include "AsioHacksFwd.h"
#include <ace/INET_Addr.h>
enum RealmFlags
{
REALM_FLAG_NONE = 0x00,
REALM_FLAG_VERSION_MISMATCH = 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
};
struct AC_SHARED_API RealmHandle
{
RealmHandle() : Realm(0) { }
RealmHandle(uint32 index) : Realm(index) { }
uint32 Realm; // primary key in `realmlist` table
bool operator<(RealmHandle const& r) const
{
return Realm < r.Realm;
}
};
/// Type of server, this is values from second column of Cfg_Configs.dbc
enum RealmType
{
REALM_TYPE_NORMAL = 0,
REALM_TYPE_PVP = 1,
REALM_TYPE_NORMAL2 = 4,
REALM_TYPE_RP = 6,
REALM_TYPE_RPPVP = 8,
MAX_CLIENT_REALM_TYPE = 14,
REALM_TYPE_FFA_PVP = 16 // custom, free for all pvp mode like arena PvP in all zones except rest activated places and sanctuaries
// replaced by REALM_PVP in realm list
};
// Storage object for a realm
struct AC_SHARED_API Realm
{
RealmHandle Id;
uint32 Build;
std::unique_ptr<ACE_INET_Addr> ExternalAddress;
std::unique_ptr<ACE_INET_Addr> LocalAddress;
std::unique_ptr<ACE_INET_Addr> LocalSubnetMask;
uint16 Port;
std::string Name;
uint8 Type;
RealmFlags Flags;
uint8 Timezone;
AccountTypes AllowedSecurityLevel;
float PopulationLevel;
// boost::asio::ip::tcp_endpoint GetAddressForClient(boost::asio::ip::address const& clientAddr) const;
};
#endif // Realm_h__

View File

@@ -4,11 +4,14 @@
* Copyright (C) 2005-2009 MaNGOS <http://getmangos.com/>
*/
#include "Common.h"
#include "DatabaseEnv.h"
#include "RealmList.h"
#include "DatabaseEnv.h"
#include "Log.h"
#include "Optional.h"
#include "Util.h"
RealmList::RealmList() : m_NextUpdateTime(time(nullptr)) { }
RealmList::RealmList() :
_updateInterval(0) { }
RealmList* RealmList::instance()
{
@@ -19,81 +22,209 @@ RealmList* RealmList::instance()
// Load the realm list from the database
void RealmList::Initialize(uint32 updateInterval)
{
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, ACE_INET_Addr const& address, ACE_INET_Addr const& localAddr, ACE_INET_Addr const& localSubmask, uint8 icon, RealmFlags flag, uint8 timezone, AccountTypes allowedSecurityLevel, float popu, uint32 build)
{
// 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 = popu;
// Append port to IP address.
realm.ExternalAddress = address;
realm.LocalAddress = localAddr;
realm.LocalSubnetMask = localSubmask;
realm.gamebuild = build;
}
void RealmList::UpdateIfNeed()
{
// maybe disabled or updated recently
if (!m_UpdateInterval || m_NextUpdateTime > time(nullptr))
return;
m_NextUpdateTime = time(nullptr) + m_UpdateInterval;
// Clears Realm list
m_realms.clear();
LoadBuildInfo();
// Get the content of the realmlist table in the database
UpdateRealms();
}
void RealmList::UpdateRealms(bool init)
void RealmList::LoadBuildInfo()
{
LOG_INFO("server", "Updating Realm List...");
// 0 1 2 3 4 5 6
if (QueryResult result = LoginDatabase.Query("SELECT majorVersion, minorVersion, bugfixVersion, hotfixVersion, build, winChecksumSeed, macChecksumSeed FROM build_info ORDER BY build ASC"))
{
do
{
Field* fields = result->Fetch();
RealmBuildInfo& build = _builds.emplace_back();
build.MajorVersion = fields[0].GetUInt32();
build.MinorVersion = fields[1].GetUInt32();
build.BugfixVersion = fields[2].GetUInt32();
std::string hotfixVersion = fields[3].GetString();
if (hotfixVersion.length() < build.HotfixVersion.size())
{
std::copy(hotfixVersion.begin(), hotfixVersion.end(), build.HotfixVersion.begin());
}
else
{
std::fill(hotfixVersion.begin(), hotfixVersion.end(), '\0');
}
build.Build = fields[4].GetUInt32();
std::string windowsHash = fields[5].GetString();
if (windowsHash.length() == build.WindowsHash.size() * 2)
{
HexStrToByteArray(windowsHash, build.WindowsHash);
}
std::string macHash = fields[6].GetString();
if (macHash.length() == build.MacHash.size() * 2)
{
HexStrToByteArray(macHash, build.MacHash);
}
} while (result->NextRow());
}
}
void RealmList::UpdateRealm(RealmHandle const& id, uint32 build, std::string const& name,
ACE_INET_Addr&& address, ACE_INET_Addr&& localAddr, ACE_INET_Addr&& localSubmask,
uint16 port, uint8 icon, RealmFlags flag, uint8 timezone, AccountTypes allowedSecurityLevel, float population)
{
// Create new if not exist or update existed
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;
if (!realm.ExternalAddress || *realm.ExternalAddress != address)
{
realm.ExternalAddress = std::make_unique<ACE_INET_Addr>(std::move(address));
}
if (!realm.LocalAddress || *realm.LocalAddress != localAddr)
{
realm.LocalAddress = std::make_unique<ACE_INET_Addr>(std::move(localAddr));
}
if (!realm.LocalSubnetMask || *realm.LocalSubnetMask != localSubmask)
{
realm.LocalSubnetMask = std::make_unique<ACE_INET_Addr>(std::move(localSubmask));
}
realm.Port = port;
}
void RealmList::UpdateIfNeed()
{
// maybe disabled or updated recently
if (!_updateInterval || _nextUpdateTime > time(nullptr))
{
return;
}
_nextUpdateTime = time(nullptr) + _updateInterval;
// Get the content of the realmlist table in the database
UpdateRealms();
}
void RealmList::UpdateRealms()
{
LOG_DEBUG("server.authserver", "Updating Realm List...");
PreparedStatement* stmt = LoginDatabase.GetPreparedStatement(LOGIN_SEL_REALMLIST);
PreparedQueryResult result = LoginDatabase.Query(stmt);
std::map<RealmHandle, std::string> existingRealms;
for (auto const& [handle, realm] : _realms)
{
existingRealms[handle] = realm.Name;
}
_realms.clear();
// Circle through results and add them to the realm map
if (result)
{
do
{
Field* fields = result->Fetch();
uint32 realmId = fields[0].GetUInt32();
std::string name = fields[1].GetString();
std::string externalAddress = fields[2].GetString();
std::string localAddress = fields[3].GetString();
std::string localSubmask = fields[4].GetString();
uint16 port = fields[5].GetUInt16();
uint8 icon = fields[6].GetUInt8();
Field* fields = result->Fetch();
uint32 realmId = fields[0].GetUInt32();
std::string name = fields[1].GetString();
std::string externalAddressString = fields[2].GetString();
std::string localAddressString = fields[3].GetString();
std::string localSubmaskString = fields[4].GetString();
uint16 port = fields[5].GetUInt16();
Optional<ACE_INET_Addr> externalAddress = ACE_INET_Addr(port, externalAddressString.c_str(), AF_INET);
if (!externalAddress)
{
LOG_ERROR("server.authserver", "Could not resolve address %s for realm \"%s\" id %u", externalAddressString.c_str(), name.c_str(), realmId);
continue;
}
Optional<ACE_INET_Addr> localAddress = ACE_INET_Addr(port, localAddressString.c_str(), AF_INET);
if (!localAddress)
{
LOG_ERROR("server.authserver", "Could not resolve localAddress %s for realm \"%s\" id %u", localAddressString.c_str(), name.c_str(), realmId);
continue;
}
Optional<ACE_INET_Addr> localSubmask = ACE_INET_Addr(0, localSubmaskString.c_str(), AF_INET);
if (!localSubmask)
{
LOG_ERROR("server.authserver", "Could not resolve localSubnetMask %s for realm \"%s\" id %u", localSubmaskString.c_str(), name.c_str(), realmId);
continue;
}
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();
ACE_INET_Addr externalAddr(port, externalAddress.c_str(), AF_INET);
ACE_INET_Addr localAddr(port, localAddress.c_str(), AF_INET);
ACE_INET_Addr submask(0, localSubmask.c_str(), AF_INET);
RealmHandle id{ realmId };
UpdateRealm(realmId, name, externalAddr, localAddr, submask, icon, flag, timezone, (allowedSecurityLevel <= SEC_ADMINISTRATOR ? AccountTypes(allowedSecurityLevel) : SEC_ADMINISTRATOR), pop, build);
UpdateRealm(id, build, name, std::move(externalAddress.value()), std::move(localAddress.value()), std::move(localSubmask.value()), port, icon, flag,
timezone, (allowedSecurityLevel <= SEC_ADMINISTRATOR ? AccountTypes(allowedSecurityLevel) : SEC_ADMINISTRATOR), pop);
if (init)
LOG_INFO("server", "Added realm \"%s\" at %s:%u.", name.c_str(), m_realms[name].ExternalAddress.get_host_addr(), port);
if (!existingRealms.count(id))
{
LOG_INFO("server.authserver", "Added realm \"%s\" at %s:%u.", name.c_str(), externalAddressString.c_str(), port);
}
else
{
LOG_DEBUG("server.authserver", "Updating realm \"%s\" at %s:%u.", name.c_str(), externalAddressString.c_str(), port);
}
existingRealms.erase(id);
} while (result->NextRow());
}
}
Realm const* RealmList::GetRealm(RealmHandle const& id) const
{
auto itr = _realms.find(id);
if (itr != _realms.end())
{
return &itr->second;
}
return nullptr;
}
RealmBuildInfo const* RealmList::GetBuildInfo(uint32 build) const
{
for (RealmBuildInfo const& clientBuild : _builds)
{
if (clientBuild.Build == build)
{
return &clientBuild;
}
}
return nullptr;
}

View File

@@ -7,43 +7,29 @@
#ifndef _REALMLIST_H
#define _REALMLIST_H
#include "Common.h"
#include <ace/INET_Addr.h>
#include "Define.h"
#include "Realm.h"
#include <array>
#include <map>
#include <vector>
#include <unordered_set>
enum RealmFlags
struct RealmBuildInfo
{
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
{
ACE_INET_Addr ExternalAddress;
ACE_INET_Addr LocalAddress;
ACE_INET_Addr LocalSubnetMask;
std::string name;
uint8 icon;
RealmFlags flag;
uint8 timezone;
uint32 m_ID;
AccountTypes allowedSecurityLevel;
float populationLevel;
uint32 gamebuild;
uint32 Build;
uint32 MajorVersion;
uint32 MinorVersion;
uint32 BugfixVersion;
std::array<char, 4> HotfixVersion;
std::array<uint8, 20> WindowsHash;
std::array<uint8, 20> MacHash;
};
/// Storage object for the list of realms on the server
class RealmList
class AC_SHARED_API RealmList
{
public:
typedef std::map<std::string, Realm> RealmMap;
typedef std::map<RealmHandle, Realm> RealmMap;
RealmList();
~RealmList() = default;
@@ -52,19 +38,23 @@ public:
void Initialize(uint32 updateInterval);
void UpdateIfNeed();
void AddRealm(const Realm& NewRealm) { m_realms[NewRealm.name] = NewRealm; }
[[nodiscard]] RealmMap::const_iterator begin() const { return m_realms.begin(); }
[[nodiscard]] RealmMap::const_iterator end() const { return m_realms.end(); }
[[nodiscard]] uint32 size() const { return m_realms.size(); }
RealmMap const& GetRealms() const { return _realms; }
Realm const* GetRealm(RealmHandle const& id) const;
RealmBuildInfo const* GetBuildInfo(uint32 build) const;
private:
void UpdateRealms(bool init = false);
void UpdateRealm(uint32 id, const std::string& name, ACE_INET_Addr const& address, ACE_INET_Addr const& localAddr, ACE_INET_Addr const& localSubmask, uint8 icon, RealmFlags flag, uint8 timezone, AccountTypes allowedSecurityLevel, float popu, uint32 build);
void LoadBuildInfo();
void UpdateRealms();
void UpdateRealm(RealmHandle const& id, uint32 build, std::string const& name,
ACE_INET_Addr&& address, ACE_INET_Addr&& localAddr, ACE_INET_Addr&& localSubmask,
uint16 port, uint8 icon, RealmFlags flag, uint8 timezone, AccountTypes allowedSecurityLevel, float population);
RealmMap m_realms;
uint32 m_UpdateInterval{0};
time_t m_NextUpdateTime;
std::vector<RealmBuildInfo> _builds;
RealmMap _realms;
uint32 _updateInterval;
time_t _nextUpdateTime;
};
#define sRealmList RealmList::instance()