Cuberite
A lightweight, fast and extensible game server for Minecraft
MojangAPI.h
Go to the documentation of this file.
1 
2 // MojangAPI.h
3 
4 // Declares the cMojangAPI class representing the various API points provided by Mojang's webservices, and a cache for their results
5 
6 
7 
8 
9 
10 #pragma once
11 
12 #include <time.h>
13 
14 #include "UUID.h"
15 
16 
17 
18 
19 
20 // fwd: ../RankManager.h"
21 class cRankManager;
22 
23 namespace Json
24 {
25  class Value;
26 }
27 
28 
29 
31 
32 // tolua_begin
34 {
35 public:
36  // tolua_end
37 
38  cMojangAPI(void);
39  ~cMojangAPI();
40 
43  void Start(cSettingsRepositoryInterface & a_Settings, bool a_ShouldAuth);
44 
51  cUUID GetUUIDFromPlayerName(const AString & a_PlayerName, bool a_UseOnlyCached = false);
52 
59  AString GetPlayerNameFromUUID(const cUUID & a_UUID, bool a_UseOnlyCached = false);
60 
67  std::vector<cUUID> GetUUIDsFromPlayerNames(const AStringVector & a_PlayerName, bool a_UseOnlyCached = false);
68 
72  void AddPlayerNameToUUIDMapping(const AString & a_PlayerName, const cUUID & a_UUID);
73 
76  void AddPlayerProfile(const AString & a_PlayerName, const cUUID & a_UUID, const Json::Value & a_Properties);
77 
79  void SetRankManager(cRankManager * a_RankManager) { m_RankMgr = a_RankManager; }
80 
81 protected:
83  class cUpdateThread;
84 
85 
87  struct sProfile
88  {
89  AString m_PlayerName; // Case-correct playername
90  cUUID m_UUID; // Player UUID
91  AString m_Textures; // The Textures field of the profile properties
92  AString m_TexturesSignature; // The signature of the Textures field of the profile properties
93  Int64 m_DateTime; // UNIXtime of the profile lookup
94 
96  sProfile(void) :
97  m_PlayerName(),
98  m_UUID(),
99  m_Textures(),
101  m_DateTime(time(nullptr))
102  {
103  }
104 
107  const AString & a_PlayerName,
108  const cUUID & a_UUID,
109  const AString & a_Textures,
110  const AString & a_TexturesSignature,
111  Int64 a_DateTime
112  ) :
113  m_PlayerName(a_PlayerName),
114  m_UUID(a_UUID),
115  m_Textures(a_Textures),
116  m_TexturesSignature(a_TexturesSignature),
117  m_DateTime(a_DateTime)
118  {
119  }
120 
122  sProfile(
123  const AString & a_PlayerName,
124  const cUUID & a_UUID,
125  const Json::Value & a_Properties,
126  Int64 a_DateTime
127  );
128  };
129  using cProfileMap = std::map<AString, sProfile>;
130  using cUUIDProfileMap = std::map<cUUID, sProfile>;
131 
132 
136 
141 
144 
147 
150 
153 
157 
160 
163 
166 
168  std::shared_ptr<cUpdateThread> m_UpdateThread;
169 
170 
172  void LoadCachesFromDisk(void);
173 
175  void SaveCachesToDisk(void);
176 
180  void CacheNamesToUUIDs(const AStringVector & a_PlayerNames);
181 
186  void QueryNamesToUUIDs(AStringVector & a_PlayerNames);
187 
190  void CacheUUIDToProfile(const cUUID & a_UUID);
191 
194  void QueryUUIDToProfile(const cUUID & a_UUID);
195 
198  void NotifyNameUUID(const AString & a_PlayerName, const cUUID & a_PlayerUUID);
199 
201  void Update(void);
202 } ; // tolua_export
203 
204 
205 
206 
signed long long Int64
Definition: Globals.h:151
std::vector< AString > AStringVector
Definition: StringUtils.h:12
std::string AString
Definition: StringUtils.h:11
Definition: Inventory.h:11
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 authenticat...
Definition: MojangAPI.cpp:351
cCriticalSection m_CSNameToUUID
Protects m_NameToUUID against simultaneous multi-threaded access.
Definition: MojangAPI.h:146
void SetRankManager(cRankManager *a_RankManager)
Sets the m_RankMgr that is used for name-uuid notifications.
Definition: MojangAPI.h:79
std::map< AString, sProfile > cProfileMap
Definition: MojangAPI.h:129
AString GetPlayerNameFromUUID(const cUUID &a_UUID, bool a_UseOnlyCached=false)
Converts a UUID into a playername.
Definition: MojangAPI.cpp:281
void NotifyNameUUID(const AString &a_PlayerName, const cUUID &a_PlayerUUID)
Called for each name-uuid pairing that is discovered.
Definition: MojangAPI.cpp:698
void CacheNamesToUUIDs(const AStringVector &a_PlayerNames)
Makes sure all specified names are in the m_PlayerNameToUUID cache.
Definition: MojangAPI.cpp:515
void LoadCachesFromDisk(void)
Loads the caches from a disk storage.
Definition: MojangAPI.cpp:391
void Update(void)
Updates the stale values in the DB from the Mojang servers.
Definition: MojangAPI.cpp:712
cUUIDProfileMap m_UUIDToProfile
Cache for the UUID-to-profile lookups.
Definition: MojangAPI.h:156
cCriticalSection m_CSUUIDToName
Protects m_UUIDToName against simultaneous multi-threaded access.
Definition: MojangAPI.h:152
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.
Definition: MojangAPI.cpp:369
std::map< cUUID, sProfile > cUUIDProfileMap
Definition: MojangAPI.h:130
std::vector< cUUID > GetUUIDsFromPlayerNames(const AStringVector &a_PlayerName, bool a_UseOnlyCached=false)
Converts the player names into UUIDs.
Definition: MojangAPI.cpp:316
cCriticalSection m_CSUUIDToProfile
Protects m_UUIDToProfile against simultaneous multi-threaded access.
Definition: MojangAPI.h:159
AString m_NameToUUIDUrl
The full URL to check when converting player names to UUIDs.
Definition: MojangAPI.h:135
cProfileMap m_NameToUUID
Cache for the Name-to-UUID lookups.
Definition: MojangAPI.h:143
void QueryNamesToUUIDs(AStringVector &a_PlayerNames)
Queries all the specified names and stores them into the m_PlayerNameToUUID cache.
Definition: MojangAPI.cpp:538
void Start(cSettingsRepositoryInterface &a_Settings, bool a_ShouldAuth)
Initializes the API; reads the settings from the specified ini file.
Definition: MojangAPI.cpp:235
cRankManager * m_RankMgr
The rank manager that is notified of the name-uuid pairings.
Definition: MojangAPI.h:162
cCriticalSection m_CSRankMgr
Protects m_RankMgr agains simultaneous multi-threaded access.
Definition: MojangAPI.h:165
void SaveCachesToDisk(void)
Saves the caches to a disk storage.
Definition: MojangAPI.cpp:449
AString m_UUIDToProfileUrl
The full URL to use for converting UUID to profile.
Definition: MojangAPI.h:140
void CacheUUIDToProfile(const cUUID &a_UUID)
Makes sure the specified UUID is in the m_UUIDToProfile cache.
Definition: MojangAPI.cpp:613
void QueryUUIDToProfile(const cUUID &a_UUID)
Queries the specified UUID's profile and stores it in the m_UUIDToProfile cache.
Definition: MojangAPI.cpp:631
cUUID GetUUIDFromPlayerName(const AString &a_PlayerName, bool a_UseOnlyCached=false)
Converts a player name into a UUID.
Definition: MojangAPI.cpp:254
cMojangAPI(void)
Definition: MojangAPI.cpp:216
cUUIDProfileMap m_UUIDToName
Cache for the Name-to-UUID lookups.
Definition: MojangAPI.h:149
std::shared_ptr< cUpdateThread > m_UpdateThread
The thread that periodically updates the stale data in the DB from the Mojang servers.
Definition: MojangAPI.h:168
Holds data for a single player profile.
Definition: MojangAPI.h:88
sProfile(void)
Default constructor for the container's sake.
Definition: MojangAPI.h:96
sProfile(const AString &a_PlayerName, const cUUID &a_UUID, const AString &a_Textures, const AString &a_TexturesSignature, Int64 a_DateTime)
Constructor for the storage creation.
Definition: MojangAPI.h:106
AString m_PlayerName
Definition: MojangAPI.h:89
AString m_TexturesSignature
Definition: MojangAPI.h:92
Definition: UUID.h:11