Skip to content

Commit

Permalink
Make offline UUIDs consistent with vanilla. (cuberite#4178)
Browse files Browse the repository at this point in the history
Fixes cuberite#4177

This is a breaking change to existing Cuberite permissions settings.
  • Loading branch information
peterbell10 authored Jul 19, 2018
1 parent 211cec6 commit 4fbf044
Show file tree
Hide file tree
Showing 3 changed files with 59 additions and 12 deletions.
6 changes: 2 additions & 4 deletions src/ClientHandle.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -273,11 +273,9 @@ cUUID cClientHandle::GenerateOfflineUUID(const AString & a_Username)
// Online UUIDs are always version 4 (random)
// We use Version 3 (MD5 hash) UUIDs for the offline UUIDs
// This guarantees that they will never collide with an online UUID and can be distinguished.
// This is also consistent with the vanilla offline UUID scheme.

// First make the username lowercase:
AString lcUsername = StrToLower(a_Username);

return cUUID::GenerateVersion3(lcUsername);
return cUUID::GenerateVersion3("OfflinePlayer:" + a_Username);
}


Expand Down
59 changes: 55 additions & 4 deletions src/Entities/Player.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,41 @@
// 1000 = once per second
#define PLAYER_LIST_TIME_MS std::chrono::milliseconds(1000)

namespace
{

/** Returns the old Offline UUID generated before becoming vanilla compliant. */
cUUID GetOldStyleOfflineUUID(const AString & a_PlayerName)
{
// Use lowercase username
auto BaseUUID = cUUID::GenerateVersion3(StrToLower(a_PlayerName)).ToRaw();
// Clobber a full nibble around the variant bits
BaseUUID[8] = (BaseUUID[8] & 0x0f) | 0x80;

cUUID Ret;
Ret.FromRaw(BaseUUID);
return Ret;
}





/** Returns the folder for the player data based on the UUID given.
This can be used both for online and offline UUIDs. */
AString GetUUIDFolderName(const cUUID & a_Uuid)
{
AString UUID = a_Uuid.ToShortString();

AString res(FILE_IO_PREFIX "players/");
res.append(UUID, 0, 2);
res.push_back('/');
return res;
}

} // namespace (anonymous)





Expand Down Expand Up @@ -2055,8 +2090,25 @@ bool cPlayer::LoadFromDisk(cWorldPtr & a_World)
return true;
}

// Load from the offline UUID file, if allowed:
// Check for old offline UUID filename, if it exists migrate to new filename
cUUID OfflineUUID = cClientHandle::GenerateOfflineUUID(GetName());
auto OldFilename = GetUUIDFileName(GetOldStyleOfflineUUID(GetName()));
auto NewFilename = GetUUIDFileName(m_UUID);
// Only move if there isn't already a new file
if (!cFile::IsFile(NewFilename) && cFile::IsFile(OldFilename))
{
cFile::CreateFolderRecursive(GetUUIDFolderName(m_UUID)); // Ensure folder exists to move to
if (
cFile::Rename(OldFilename, NewFilename) &&
(m_UUID == OfflineUUID) &&
LoadFromFile(NewFilename, a_World)
)
{
return true;
}
}

// Load from the offline UUID file, if allowed:
const char * OfflineUsage = " (unused)";
if (cRoot::Get()->GetServer()->ShouldLoadOfflinePlayerData())
{
Expand Down Expand Up @@ -2227,8 +2279,7 @@ void cPlayer::OpenHorseInventory()

bool cPlayer::SaveToDisk()
{
cFile::CreateFolder(FILE_IO_PREFIX + AString("players/")); // Create the "players" folder, if it doesn't exist yet (#1268)
cFile::CreateFolder(FILE_IO_PREFIX + AString("players/") + m_UUID.ToShortString().substr(0, 2));
cFile::CreateFolderRecursive(GetUUIDFolderName(m_UUID));

// create the JSON data
Json::Value JSON_PlayerPosition;
Expand Down Expand Up @@ -2864,7 +2915,7 @@ AString cPlayer::GetUUIDFileName(const cUUID & a_UUID)
{
AString UUID = a_UUID.ToLongString();

AString res("players/");
AString res(FILE_IO_PREFIX "players/");
res.append(UUID, 0, 2);
res.push_back('/');
res.append(UUID, 2, AString::npos);
Expand Down
6 changes: 2 additions & 4 deletions src/UUID.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -270,10 +270,8 @@ cUUID cUUID::GenerateVersion3(const AString & a_Name)
// Insert version number
UUID.m_UUID[6] = (UUID.m_UUID[6] & 0x0f) | 0x30;

/* Insert variant number
Note that by using 1000 instead of 10xx we are losing 2 bits
but this is needed for compatibility with the old string uuid generator */
UUID.m_UUID[8] = (UUID.m_UUID[8] & 0x0f) | 0x80;
// Insert variant number
UUID.m_UUID[8] = (UUID.m_UUID[8] & 0x3f) | 0x80;

return UUID;
}
Expand Down

0 comments on commit 4fbf044

Please sign in to comment.