2014-07-29 11:45:55 -04:00
|
|
|
|
|
|
|
// MojangAPI.h
|
|
|
|
|
|
|
|
// Declares the cMojangAPI class representing the various API points provided by Mojang's webservices, and a cache for their results
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
#pragma once
|
|
|
|
|
|
|
|
#include <time.h>
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2014-07-30 07:44:03 -04:00
|
|
|
// tolua_begin
|
2014-07-29 11:45:55 -04:00
|
|
|
class cMojangAPI
|
|
|
|
{
|
|
|
|
public:
|
2014-07-30 07:44:03 -04:00
|
|
|
// tolua_end
|
|
|
|
|
2014-07-29 11:45:55 -04:00
|
|
|
cMojangAPI(void);
|
|
|
|
~cMojangAPI();
|
|
|
|
|
|
|
|
/** Initializes the API; reads the settings from the specified ini file.
|
|
|
|
Loads cached results from disk. */
|
|
|
|
void Start(cIniFile & a_SettingsIni);
|
|
|
|
|
|
|
|
/** 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. */
|
|
|
|
static bool SecureRequest(const AString & a_ServerName, const AString & a_Request, AString & a_Response);
|
|
|
|
|
2014-07-30 07:44:03 -04:00
|
|
|
// tolua_begin
|
|
|
|
|
2014-07-29 11:45:55 -04:00
|
|
|
/** Converts the given UUID to its short form (32 bytes, no dashes).
|
2014-07-30 08:09:30 -04:00
|
|
|
Logs a warning and returns empty string if not a UUID.
|
|
|
|
Note: only checks the string's length, not the actual content. */
|
2014-07-29 11:45:55 -04:00
|
|
|
static AString MakeUUIDShort(const AString & a_UUID);
|
|
|
|
|
|
|
|
/** Converts the given UUID to its dashed form (36 bytes, 4 dashes).
|
2014-07-30 08:09:30 -04:00
|
|
|
Logs a warning and returns empty string if not a UUID.
|
|
|
|
Note: only checks the string's length, not the actual content. */
|
2014-07-29 11:45:55 -04:00
|
|
|
static AString MakeUUIDDashed(const AString & a_UUID);
|
|
|
|
|
2014-07-30 07:44:03 -04:00
|
|
|
// tolua_end
|
|
|
|
|
2014-07-29 11:45:55 -04:00
|
|
|
/** Converts the player names into UUIDs.
|
|
|
|
a_PlayerName[idx] will be converted to UUID and returned as idx-th value
|
|
|
|
The UUID will be empty on error.
|
|
|
|
Blocking operation, do not use in world-tick thread! */
|
|
|
|
AStringVector GetUUIDsFromPlayerNames(const AStringVector & a_PlayerName);
|
|
|
|
|
2014-07-30 07:44:03 -04:00
|
|
|
// tolua_begin
|
|
|
|
|
2014-07-29 11:45:55 -04:00
|
|
|
/** 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. */
|
|
|
|
void AddPlayerNameToUUIDMapping(const AString & a_PlayerName, const AString & a_UUID);
|
2014-07-30 07:44:03 -04:00
|
|
|
|
|
|
|
// tolua_end
|
2014-07-29 11:45:55 -04:00
|
|
|
|
|
|
|
protected:
|
|
|
|
struct sUUIDRecord
|
|
|
|
{
|
|
|
|
AString m_PlayerName; // Case-correct playername
|
|
|
|
AString m_UUID;
|
|
|
|
Int64 m_DateTime; // UNIXtime of the UUID lookup
|
|
|
|
|
|
|
|
sUUIDRecord(void) :
|
|
|
|
m_UUID(),
|
|
|
|
m_DateTime(time(NULL))
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
sUUIDRecord(const AString & a_PlayerName, const AString & a_UUID, Int64 a_DateTime) :
|
|
|
|
m_PlayerName(a_PlayerName),
|
|
|
|
m_UUID(a_UUID),
|
|
|
|
m_DateTime(a_DateTime)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
};
|
|
|
|
typedef std::map<AString, sUUIDRecord> cNameToUUIDMap; // maps Lowercased PlayerName to sUUIDRecord
|
|
|
|
|
|
|
|
/** The server to connect to when converting player names to UUIDs. For example "api.mojang.com". */
|
|
|
|
AString m_NameToUUIDServer;
|
|
|
|
|
|
|
|
/** The URL to use for converting player names to UUIDs, without server part.
|
|
|
|
For example "/profiles/page/1". */
|
|
|
|
AString m_NameToUUIDAddress;
|
|
|
|
|
|
|
|
/** Cache for the Name-to-UUID lookups. The map key is expected lowercased. Protected by m_CSNameToUUID. */
|
|
|
|
cNameToUUIDMap m_NameToUUID;
|
|
|
|
|
|
|
|
/** Protects m_NameToUUID against simultaneous multi-threaded access. */
|
|
|
|
cCriticalSection m_CSNameToUUID;
|
|
|
|
|
|
|
|
|
|
|
|
/** Loads the caches from a disk storage. */
|
|
|
|
void LoadCachesFromDisk(void);
|
|
|
|
|
|
|
|
/** Saves the caches to a disk storage. */
|
|
|
|
void SaveCachesToDisk(void);
|
|
|
|
|
|
|
|
/** Makes sure all specified names are in the 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. */
|
|
|
|
void CacheNamesToUUIDs(const AStringVector & a_PlayerNames);
|
2014-07-30 07:44:03 -04:00
|
|
|
} ; // tolua_export
|
2014-07-29 11:45:55 -04:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|