1
0

Preparation for player UUID-based storage: LoadFromFile()

This commit is contained in:
madmaxoft 2014-07-11 00:06:05 +02:00
parent 729cc7f6ff
commit 2bd486660a
2 changed files with 42 additions and 21 deletions

View File

@ -75,11 +75,6 @@ cPlayer::cPlayer(cClientHandle* a_Client, const AString & a_PlayerName)
, m_TicksUntilNextSave(PLAYER_INVENTORY_SAVE_INTERVAL) , m_TicksUntilNextSave(PLAYER_INVENTORY_SAVE_INTERVAL)
, m_bIsTeleporting(false) , m_bIsTeleporting(false)
{ {
LOGD("Created a player object for \"%s\" @ \"%s\" at %p, ID %d",
a_PlayerName.c_str(), a_Client->GetIPString().c_str(),
this, GetUniqueID()
);
m_InventoryWindow = new cInventoryWindow(*this); m_InventoryWindow = new cInventoryWindow(*this);
m_CurrentWindow = m_InventoryWindow; m_CurrentWindow = m_InventoryWindow;
m_InventoryWindow->OpenedByPlayer(*this); m_InventoryWindow->OpenedByPlayer(*this);
@ -1690,50 +1685,67 @@ void cPlayer::LoadPermissionsFromDisk()
bool cPlayer::LoadFromDisk()
bool cPlayer::LoadFromDisk(void)
{ {
LoadPermissionsFromDisk(); LoadPermissionsFromDisk();
AString SourceFile; AString SourceFile;
Printf(SourceFile, "players/%s.json", GetName().c_str()); Printf(SourceFile, "players/%s.json", GetName().c_str());
bool res = LoadFromFile(SourceFile);
if (res)
{
return true;
}
}
bool cPlayer::LoadFromFile(const AString & a_FileName)
{
// Load the data from the file:
cFile f; cFile f;
if (!f.Open(SourceFile, cFile::fmRead)) if (!f.Open(a_FileName, cFile::fmRead))
{ {
// This is a new player whom we haven't seen yet, bail out, let them have the defaults // This is a new player whom we haven't seen yet, bail out, let them have the defaults
return false; return false;
} }
AString buffer; AString buffer;
if (f.ReadRestOfFile(buffer) != f.GetSize()) if (f.ReadRestOfFile(buffer) != f.GetSize())
{ {
LOGWARNING("Cannot read player data from file \"%s\"", SourceFile.c_str()); LOGWARNING("Cannot read player data from file \"%s\"", a_FileName.c_str());
return false; return false;
} }
f.Close(); //cool kids play nice f.Close();
// Parse the JSON format:
Json::Value root; Json::Value root;
Json::Reader reader; Json::Reader reader;
if (!reader.parse(buffer, root, false)) if (!reader.parse(buffer, root, false))
{ {
LOGWARNING("Cannot parse player data in file \"%s\", player will be reset", SourceFile.c_str()); LOGWARNING("Cannot parse player data in file \"%s\"", a_FileName.c_str());
return false;
} }
// Load the player data:
Json::Value & JSON_PlayerPosition = root["position"]; Json::Value & JSON_PlayerPosition = root["position"];
if (JSON_PlayerPosition.size() == 3) if (JSON_PlayerPosition.size() == 3)
{ {
SetPosX(JSON_PlayerPosition[(unsigned int)0].asDouble()); SetPosX(JSON_PlayerPosition[(unsigned)0].asDouble());
SetPosY(JSON_PlayerPosition[(unsigned int)1].asDouble()); SetPosY(JSON_PlayerPosition[(unsigned)1].asDouble());
SetPosZ(JSON_PlayerPosition[(unsigned int)2].asDouble()); SetPosZ(JSON_PlayerPosition[(unsigned)2].asDouble());
m_LastPos = GetPosition(); m_LastPos = GetPosition();
} }
Json::Value & JSON_PlayerRotation = root["rotation"]; Json::Value & JSON_PlayerRotation = root["rotation"];
if (JSON_PlayerRotation.size() == 3) if (JSON_PlayerRotation.size() == 3)
{ {
SetYaw ((float)JSON_PlayerRotation[(unsigned int)0].asDouble()); SetYaw ((float)JSON_PlayerRotation[(unsigned)0].asDouble());
SetPitch ((float)JSON_PlayerRotation[(unsigned int)1].asDouble()); SetPitch ((float)JSON_PlayerRotation[(unsigned)1].asDouble());
SetRoll ((float)JSON_PlayerRotation[(unsigned int)2].asDouble()); SetRoll ((float)JSON_PlayerRotation[(unsigned)2].asDouble());
} }
m_Health = root.get("health", 0).asInt(); m_Health = root.get("health", 0).asInt();
@ -1763,8 +1775,8 @@ bool cPlayer::LoadFromDisk()
cStatSerializer StatSerializer(cRoot::Get()->GetDefaultWorld()->GetName(), GetName(), &m_Stats); cStatSerializer StatSerializer(cRoot::Get()->GetDefaultWorld()->GetName(), GetName(), &m_Stats);
StatSerializer.Load(); StatSerializer.Load();
LOGD("Player \"%s\" was read from file, spawning at {%.2f, %.2f, %.2f} in world \"%s\"", LOGD("Player \"%s\" was read from file \"%s\", spawning at {%.2f, %.2f, %.2f} in world \"%s\"",
GetName().c_str(), GetPosX(), GetPosY(), GetPosZ(), m_LoadedWorldName.c_str() GetName().c_str(), a_FileName.c_str(), GetPosX(), GetPosY(), GetPosZ(), m_LoadedWorldName.c_str()
); );
return true; return true;

View File

@ -41,6 +41,7 @@ public:
cPlayer(cClientHandle * a_Client, const AString & a_PlayerName); cPlayer(cClientHandle * a_Client, const AString & a_PlayerName);
virtual ~cPlayer(); virtual ~cPlayer();
virtual void SpawnOn(cClientHandle & a_Client) override; virtual void SpawnOn(cClientHandle & a_Client) override;
@ -337,7 +338,15 @@ public:
bool MoveToWorld(const char * a_WorldName); // tolua_export bool MoveToWorld(const char * a_WorldName); // tolua_export
bool SaveToDisk(void); bool SaveToDisk(void);
/** Loads the player data from the disk file.
Returns true on success, false on failure. */
bool LoadFromDisk(void); bool LoadFromDisk(void);
/** Loads the player data from the specified file.
Returns true on success, false on failure. */
bool LoadFromFile(const AString & a_FileName);
void LoadPermissionsFromDisk(void); // tolua_export void LoadPermissionsFromDisk(void); // tolua_export
const AString & GetLoadedWorldName() { return m_LoadedWorldName; } const AString & GetLoadedWorldName() { return m_LoadedWorldName; }