Cuberite
A lightweight, fast and extensible game server for Minecraft
Classes | Public Member Functions | Static 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 ()
 

Static Public Member Functions

static bool SecureRequest (const AString &a_ServerName, const AString &a_Request, AString &a_Response)
 Connects to the specified server using SSL, sends the given request and receives the response. More...
 

Protected Types

typedef std::map< AString, sProfilecProfileMap
 
typedef std::map< cUUID, sProfilecUUIDProfileMap
 

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_NameToUUIDAddress
 The URL to use for converting player names to UUIDs, without server part. More...
 
AString m_NameToUUIDServer
 The server to connect to 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_UUIDToProfileAddress
 The URL to use for converting UUID to profile, without the server part. More...
 
AString m_UUIDToProfileServer
 The server to connect to when converting UUID to profile. More...
 

Detailed Description

Definition at line 33 of file MojangAPI.h.

Member Typedef Documentation

◆ cProfileMap

typedef std::map<AString, sProfile> cMojangAPI::cProfileMap
protected

Definition at line 134 of file MojangAPI.h.

◆ cUUIDProfileMap

typedef std::map<cUUID, sProfile> cMojangAPI::cUUIDProfileMap
protected

Definition at line 135 of file MojangAPI.h.

Constructor & Destructor Documentation

◆ cMojangAPI()

cMojangAPI::cMojangAPI ( void  )

Definition at line 281 of file MojangAPI.cpp.

◆ ~cMojangAPI()

cMojangAPI::~cMojangAPI ( )

Definition at line 295 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 418 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 436 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 637 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 764 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 348 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 321 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 383 of file MojangAPI.cpp.

◆ LoadCachesFromDisk()

void cMojangAPI::LoadCachesFromDisk ( void  )
protected

Loads the caches from a disk storage.

Definition at line 513 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 877 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 660 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 782 of file MojangAPI.cpp.

◆ SaveCachesToDisk()

void cMojangAPI::SaveCachesToDisk ( void  )
protected

Saves the caches to a disk storage.

Definition at line 571 of file MojangAPI.cpp.

◆ SecureRequest()

bool cMojangAPI::SecureRequest ( const AString a_ServerName,
const AString a_Request,
AString a_Response 
)
static

Connects to the specified server using SSL, sends the given request and receives the response.

Checks Mojang certificates using the hard-coded Starfield root CA certificate. Returns true if all was successful, false on failure.

Definition at line 458 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 84 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 304 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 891 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 156 of file MojangAPI.h.

◆ m_CSRankMgr

cCriticalSection cMojangAPI::m_CSRankMgr
protected

Protects m_RankMgr agains simultaneous multi-threaded access.

Definition at line 175 of file MojangAPI.h.

◆ m_CSUUIDToName

cCriticalSection cMojangAPI::m_CSUUIDToName
protected

Protects m_UUIDToName against simultaneous multi-threaded access.

Definition at line 162 of file MojangAPI.h.

◆ m_CSUUIDToProfile

cCriticalSection cMojangAPI::m_CSUUIDToProfile
protected

Protects m_UUIDToProfile against simultaneous multi-threaded access.

Definition at line 169 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 153 of file MojangAPI.h.

◆ m_NameToUUIDAddress

AString cMojangAPI::m_NameToUUIDAddress
protected

The URL to use for converting player names to UUIDs, without server part.

For example "/profiles/page/1".

Definition at line 143 of file MojangAPI.h.

◆ m_NameToUUIDServer

AString cMojangAPI::m_NameToUUIDServer
protected

The server to connect to when converting player names to UUIDs.

For example "api.mojang.com".

Definition at line 139 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 172 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 178 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 159 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 166 of file MojangAPI.h.

◆ m_UUIDToProfileAddress

AString cMojangAPI::m_UUIDToProfileAddress
protected

The URL to use for converting UUID to profile, without the server part.

Will replace UUID% with the actual UUID. For example "session/minecraft/profile/%UUID%?unsigned=false".

Definition at line 150 of file MojangAPI.h.

◆ m_UUIDToProfileServer

AString cMojangAPI::m_UUIDToProfileServer
protected

The server to connect to when converting UUID to profile.

For example "sessionserver.mojang.com".

Definition at line 146 of file MojangAPI.h.


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