Cuberite
A lightweight, fast and extensible game server for Minecraft
Classes | Public Member Functions | Protected Types | Protected Member Functions | Protected Attributes | List of all members
cMojangAPI Class Reference

#include <MojangAPI.h>

Collaboration diagram for cMojangAPI:
Collaboration graph
[legend]

Classes

class  cUpdateThread
 
struct  sProfile
 Holds data for a single player profile. More...
 

Public Member Functions

void AddPlayerNameToUUIDMapping (const AString &a_PlayerName, const cUUID &a_UUID)
 Called by the Authenticator to add a PlayerName -> UUID mapping that it has received from authenticating a user. More...
 
void AddPlayerProfile (const AString &a_PlayerName, const cUUID &a_UUID, const Json::Value &a_Properties)
 Called by the Authenticator to add a profile that it has received from authenticating a user. More...
 
 cMojangAPI (void)
 
AString GetPlayerNameFromUUID (const cUUID &a_UUID, bool a_UseOnlyCached=false)
 Converts a UUID into a playername. More...
 
cUUID GetUUIDFromPlayerName (const AString &a_PlayerName, bool a_UseOnlyCached=false)
 Converts a player name into a UUID. More...
 
std::vector< cUUIDGetUUIDsFromPlayerNames (const AStringVector &a_PlayerName, bool a_UseOnlyCached=false)
 Converts the player names into UUIDs. More...
 
void SetRankManager (cRankManager *a_RankManager)
 Sets the m_RankMgr that is used for name-uuid notifications. More...
 
void Start (cSettingsRepositoryInterface &a_Settings, bool a_ShouldAuth)
 Initializes the API; reads the settings from the specified ini file. More...
 
 ~cMojangAPI ()
 

Protected Types

using cProfileMap = std::map< AString, sProfile >
 
using cUUIDProfileMap = std::map< cUUID, sProfile >
 

Protected Member Functions

void CacheNamesToUUIDs (const AStringVector &a_PlayerNames)
 Makes sure all specified names are in the m_PlayerNameToUUID cache. More...
 
void CacheUUIDToProfile (const cUUID &a_UUID)
 Makes sure the specified UUID is in the m_UUIDToProfile cache. More...
 
void LoadCachesFromDisk (void)
 Loads the caches from a disk storage. More...
 
void NotifyNameUUID (const AString &a_PlayerName, const cUUID &a_PlayerUUID)
 Called for each name-uuid pairing that is discovered. More...
 
void QueryNamesToUUIDs (AStringVector &a_PlayerNames)
 Queries all the specified names and stores them into the m_PlayerNameToUUID cache. More...
 
void QueryUUIDToProfile (const cUUID &a_UUID)
 Queries the specified UUID's profile and stores it in the m_UUIDToProfile cache. More...
 
void SaveCachesToDisk (void)
 Saves the caches to a disk storage. More...
 
void Update (void)
 Updates the stale values in the DB from the Mojang servers. More...
 

Protected Attributes

cCriticalSection m_CSNameToUUID
 Protects m_NameToUUID against simultaneous multi-threaded access. More...
 
cCriticalSection m_CSRankMgr
 Protects m_RankMgr agains simultaneous multi-threaded access. More...
 
cCriticalSection m_CSUUIDToName
 Protects m_UUIDToName against simultaneous multi-threaded access. More...
 
cCriticalSection m_CSUUIDToProfile
 Protects m_UUIDToProfile against simultaneous multi-threaded access. More...
 
cProfileMap m_NameToUUID
 Cache for the Name-to-UUID lookups. More...
 
AString m_NameToUUIDUrl
 The full URL to check when converting player names to UUIDs. More...
 
cRankManagerm_RankMgr
 The rank manager that is notified of the name-uuid pairings. More...
 
std::shared_ptr< cUpdateThreadm_UpdateThread
 The thread that periodically updates the stale data in the DB from the Mojang servers. More...
 
cUUIDProfileMap m_UUIDToName
 Cache for the Name-to-UUID lookups. More...
 
cUUIDProfileMap m_UUIDToProfile
 Cache for the UUID-to-profile lookups. More...
 
AString m_UUIDToProfileUrl
 The full URL to use for converting UUID to profile. More...
 

Detailed Description

Definition at line 33 of file MojangAPI.h.

Member Typedef Documentation

◆ cProfileMap

using cMojangAPI::cProfileMap = std::map<AString, sProfile>
protected

Definition at line 129 of file MojangAPI.h.

◆ cUUIDProfileMap

using cMojangAPI::cUUIDProfileMap = std::map<cUUID, sProfile>
protected

Definition at line 130 of file MojangAPI.h.

Constructor & Destructor Documentation

◆ cMojangAPI()

cMojangAPI::cMojangAPI ( void  )

Definition at line 216 of file MojangAPI.cpp.

◆ ~cMojangAPI()

cMojangAPI::~cMojangAPI ( )

Definition at line 226 of file MojangAPI.cpp.

Member Function Documentation

◆ AddPlayerNameToUUIDMapping()

void cMojangAPI::AddPlayerNameToUUIDMapping ( const AString a_PlayerName,
const cUUID a_UUID 
)

Called by the Authenticator to add a PlayerName -> UUID mapping that it has received from authenticating a user.

This adds the cache item and "refreshes" it if existing, adjusting its datetime stamp to now.

Definition at line 351 of file MojangAPI.cpp.

◆ AddPlayerProfile()

void cMojangAPI::AddPlayerProfile ( const AString a_PlayerName,
const cUUID a_UUID,
const Json::Value &  a_Properties 
)

Called by the Authenticator to add a profile that it has received from authenticating a user.

Adds the profile to the respective mapping caches and updtes their datetime stamp to now.

Definition at line 369 of file MojangAPI.cpp.

◆ CacheNamesToUUIDs()

void cMojangAPI::CacheNamesToUUIDs ( const AStringVector a_PlayerNames)
protected

Makes sure all specified names are in the m_PlayerNameToUUID cache.

Downloads any missing ones from Mojang API servers. Names that are not valid are not added into the cache. ASSUMEs that a_PlayerNames contains lowercased player names.

Definition at line 515 of file MojangAPI.cpp.

◆ CacheUUIDToProfile()

void cMojangAPI::CacheUUIDToProfile ( const cUUID a_UUID)
protected

Makes sure the specified UUID is in the m_UUIDToProfile cache.

If missing, downloads it from Mojang API servers. UUIDs that are not valid will not be added into the cache.

Definition at line 613 of file MojangAPI.cpp.

◆ GetPlayerNameFromUUID()

AString cMojangAPI::GetPlayerNameFromUUID ( const cUUID a_UUID,
bool  a_UseOnlyCached = false 
)

Converts a UUID into a playername.

The returned playername will be empty on error. Uses both m_UUIDToName and m_UUIDToProfile to search for the value. Uses m_UUIDToProfile for cache. If a_UseOnlyCached is true, the function only consults the cached values. If a_UseOnlyCached is false and the name is not found in the cache, it is looked up online, which is a blocking operation, do not use this in world-tick thread!

Definition at line 281 of file MojangAPI.cpp.

◆ GetUUIDFromPlayerName()

cUUID cMojangAPI::GetUUIDFromPlayerName ( const AString a_PlayerName,
bool  a_UseOnlyCached = false 
)

Converts a player name into a UUID.

The UUID will be nil on error. If a_UseOnlyCached is true, the function only consults the cached values. If a_UseOnlyCached is false and the name is not found in the cache, it is looked up online, which is a blocking operation, do not use this in world-tick thread! If you have multiple names to resolve, use the GetUUIDsFromPlayerNames() function, it uses a single request for multiple names.

Definition at line 254 of file MojangAPI.cpp.

◆ GetUUIDsFromPlayerNames()

std::vector< cUUID > cMojangAPI::GetUUIDsFromPlayerNames ( const AStringVector a_PlayerName,
bool  a_UseOnlyCached = false 
)

Converts the player names into UUIDs.

a_PlayerName[idx] will be converted to UUID and returned as idx-th value The UUID will be nil on error. If a_UseOnlyCached is true, only the cached values are returned. If a_UseOnlyCached is false, the names not found in the cache are looked up online, which is a blocking operation, do not use this in world-tick thread!

Definition at line 316 of file MojangAPI.cpp.

◆ LoadCachesFromDisk()

void cMojangAPI::LoadCachesFromDisk ( void  )
protected

Loads the caches from a disk storage.

Definition at line 391 of file MojangAPI.cpp.

◆ NotifyNameUUID()

void cMojangAPI::NotifyNameUUID ( const AString a_PlayerName,
const cUUID a_PlayerUUID 
)
protected

Called for each name-uuid pairing that is discovered.

If assigned, notifies the m_RankManager of the event.

Definition at line 698 of file MojangAPI.cpp.

◆ QueryNamesToUUIDs()

void cMojangAPI::QueryNamesToUUIDs ( AStringVector a_PlayerNames)
protected

Queries all the specified names and stores them into the m_PlayerNameToUUID cache.

Names that are not valid are not added into the cache. ASSUMEs that a_PlayerNames contans lowercased player names. For performance reasons takes a non-const reference and modifies the list given to it, until empty.

Definition at line 538 of file MojangAPI.cpp.

◆ QueryUUIDToProfile()

void cMojangAPI::QueryUUIDToProfile ( const cUUID a_UUID)
protected

Queries the specified UUID's profile and stores it in the m_UUIDToProfile cache.

If already present, updates the cache entry. UUIDs that are not valid will not be added into the cache.

Definition at line 631 of file MojangAPI.cpp.

◆ SaveCachesToDisk()

void cMojangAPI::SaveCachesToDisk ( void  )
protected

Saves the caches to a disk storage.

Definition at line 449 of file MojangAPI.cpp.

◆ SetRankManager()

void cMojangAPI::SetRankManager ( cRankManager a_RankManager)
inline

Sets the m_RankMgr that is used for name-uuid notifications.

Accepts nullptr to remove the binding.

Definition at line 79 of file MojangAPI.h.

◆ Start()

void cMojangAPI::Start ( cSettingsRepositoryInterface a_Settings,
bool  a_ShouldAuth 
)

Initializes the API; reads the settings from the specified ini file.

Loads cached results from disk.

Definition at line 235 of file MojangAPI.cpp.

◆ Update()

void cMojangAPI::Update ( void  )
protected

Updates the stale values in the DB from the Mojang servers.

Called from the cUpdateThread, blocks on the HTTPS API calls.

Definition at line 712 of file MojangAPI.cpp.

Member Data Documentation

◆ m_CSNameToUUID

cCriticalSection cMojangAPI::m_CSNameToUUID
protected

Protects m_NameToUUID against simultaneous multi-threaded access.

Definition at line 146 of file MojangAPI.h.

◆ m_CSRankMgr

cCriticalSection cMojangAPI::m_CSRankMgr
protected

Protects m_RankMgr agains simultaneous multi-threaded access.

Definition at line 165 of file MojangAPI.h.

◆ m_CSUUIDToName

cCriticalSection cMojangAPI::m_CSUUIDToName
protected

Protects m_UUIDToName against simultaneous multi-threaded access.

Definition at line 152 of file MojangAPI.h.

◆ m_CSUUIDToProfile

cCriticalSection cMojangAPI::m_CSUUIDToProfile
protected

Protects m_UUIDToProfile against simultaneous multi-threaded access.

Definition at line 159 of file MojangAPI.h.

◆ m_NameToUUID

cProfileMap cMojangAPI::m_NameToUUID
protected

Cache for the Name-to-UUID lookups.

The map key is lowercased PlayerName. Protected by m_CSNameToUUID.

Definition at line 143 of file MojangAPI.h.

◆ m_NameToUUIDUrl

AString cMojangAPI::m_NameToUUIDUrl
protected

The full URL to check when converting player names to UUIDs.

For example: "https://api.mojang.com/profiles/page/1".

Definition at line 135 of file MojangAPI.h.

◆ m_RankMgr

cRankManager* cMojangAPI::m_RankMgr
protected

The rank manager that is notified of the name-uuid pairings.

May be nullptr. Protected by m_CSRankMgr.

Definition at line 162 of file MojangAPI.h.

◆ m_UpdateThread

std::shared_ptr<cUpdateThread> cMojangAPI::m_UpdateThread
protected

The thread that periodically updates the stale data in the DB from the Mojang servers.

Definition at line 168 of file MojangAPI.h.

◆ m_UUIDToName

cUUIDProfileMap cMojangAPI::m_UUIDToName
protected

Cache for the Name-to-UUID lookups.

The map key is lowercased short UUID. Protected by m_CSUUIDToName.

Definition at line 149 of file MojangAPI.h.

◆ m_UUIDToProfile

cUUIDProfileMap cMojangAPI::m_UUIDToProfile
protected

Cache for the UUID-to-profile lookups.

The map key is lowercased short UUID. Protected by m_CSUUIDToProfile.

Definition at line 156 of file MojangAPI.h.

◆ m_UUIDToProfileUrl

AString cMojangAPI::m_UUIDToProfileUrl
protected

The full URL to use for converting UUID to profile.

UUID% will get replaced with the actual UUID. For example "https://sessionserver.mojang.com/session/minecraft/profile/%UUID%?unsigned=false".

Definition at line 140 of file MojangAPI.h.


The documentation for this class was generated from the following files: