Added support for player icons.

This commit is contained in:
hiker
2014-05-23 22:43:19 +10:00
parent de8c6ac8b8
commit 11d80d47ad
5 changed files with 89 additions and 6 deletions

View File

@@ -21,14 +21,13 @@
#include "achievements/achievements_manager.hpp"
#include "challenges/unlock_manager.hpp"
#include "config/player_manager.hpp"
#include "karts/kart_properties.hpp"
#include "karts/kart_properties_manager.hpp"
#include "online/online_player_profile.hpp"
#include "io/xml_node.hpp"
#include "io/utf_writer.hpp"
#include "utils/string_utils.hpp"
#include <sstream>
#include <stdlib.h>
//------------------------------------------------------------------------------
/** Constructor to create a new player that didn't exist before.
* \param name Name of the player.
@@ -75,6 +74,7 @@ PlayerProfile::PlayerProfile(const XMLNode* node)
m_last_was_online = false;
m_story_mode_status = NULL;
m_achievements_status = NULL;
m_icon_filename = "";
node->get("name", &m_local_name );
node->get("guest", &m_is_guest_account);
@@ -85,10 +85,12 @@ PlayerProfile::PlayerProfile(const XMLNode* node)
node->get("saved-token", &m_saved_token );
node->get("last-online-name", &m_last_online_name);
node->get("last-was-online", &m_last_was_online );
node->get("icon-filename", &m_icon_filename );
#ifdef DEBUG
m_magic_number = 0xABCD1234;
#endif
} // PlayerProfile
//------------------------------------------------------------------------------
@@ -124,8 +126,43 @@ void PlayerProfile::initRemainingData()
m_story_mode_status = unlock_manager->createStoryModeStatus();
m_achievements_status =
AchievementsManager::get()->createAchievementsStatus();
int n = m_unique_id % kart_properties_manager->getNumberOfKarts();
std::string source = kart_properties_manager->getKartById(n)
->getAbsoluteIconFile();
// Create the filename for the icon of this player: the unique id
// followed by .png or .jpg.
std::ostringstream out;
out << m_unique_id <<"."<<StringUtils::getExtension(source);
if(file_manager->copyFile(source,
file_manager->getUserConfigFile(out.str())) )
{
m_icon_filename = out.str();
}
else
{
m_icon_filename = "";
}
} // initRemainingData
//------------------------------------------------------------------------------
/** Returns the name of the icon file for this player. If the player icon
* file is undefined, it returns a "?" mark texture. Note, getAsset does
* not return a reference, but only a temporary string. So we must return a
* copy of the string (not a reference to).
*/
const std::string PlayerProfile::getIconFilename() const
{
// If the icon file is undefined or does not exist, return the "?" icon
if(m_icon_filename.size()==0 ||
!file_manager->fileExists(file_manager->getUserConfigFile(m_icon_filename)))
{
return file_manager->getAsset(FileManager::GUI, "main_help.png");
}
return file_manager->getUserConfigFile(m_icon_filename);
} // getIconFilename
//------------------------------------------------------------------------------
/** Writes the data for this player to the specified UTFWriter.
* \param out The utf writer to write the data to.
@@ -136,6 +173,8 @@ void PlayerProfile::save(UTFWriter &out)
<< L"\" guest=\"" << m_is_guest_account
<< L"\" use-frequency=\"" << m_use_frequency << L"\"\n";
out << L" icon-filename=\"" << m_icon_filename <<L"\"\n";
out << L" unique-id=\"" << m_unique_id
<< L"\" saved-session=\"" << m_saved_session << L"\"\n";

View File

@@ -83,6 +83,9 @@ private:
/** A unique number for this player, used to link it to challenges etc. */
unsigned int m_unique_id;
/** Absolute path of the icon file for this player. */
std::string m_icon_filename;
/** True if this user has a saved session. */
bool m_saved_session;
@@ -134,6 +137,7 @@ public:
const irr::core::stringw &info) = 0;
virtual void requestSignOut() = 0;
virtual bool isLoggedIn() const { return false; }
const std::string getIconFilename() const;
// ------------------------------------------------------------------------
/** Sets the name of this player. */
void setName(const core::stringw& name)
@@ -161,7 +165,6 @@ public:
#endif
return m_is_guest_account;
} // isGuestAccount
// ------------------------------------------------------------------------
/** Returns the last used online name. */
const core::stringw& getLastOnlineName() const

View File

@@ -1213,7 +1213,47 @@ bool FileManager::removeDirectory(const std::string &name) const
#endif
} // remove directory
// ----------------------------------------------------------------------------
/** Copies the file source to dest.
* \param source The file to read.
* \param dest The new filename.
* \return True if the copy was successful, false otherwise.
*/
bool FileManager::copyFile(const std::string &source, const std::string &dest)
{
FILE *f_source = fopen(source.c_str(), "rb");
if(!f_source) return false;
FILE *f_dest = fopen(dest.c_str(), "wb");
if(!f_dest)
{
fclose(f_source);
return false;
}
const int BUFFER_SIZE=32768;
char *buffer = new char[BUFFER_SIZE];
if(!buffer) return false;
size_t n;
while((n=fread(buffer, 1, BUFFER_SIZE, f_source))>0)
{
if(fwrite(buffer, 1, n, f_dest)!=n)
{
Log::error("FileManager", "Write error copying '%s' to '%s",
source.c_str(), dest.c_str());
delete buffer;
fclose(f_source);
fclose(f_dest);
return false;
} // if fwrite()!=n
} // while
delete buffer;
fclose(f_source);
fclose(f_dest);
return true;
} // copyFile
// ----------------------------------------------------------------------------
/** Returns true if the first file is newer than the second. The comparison is
* based on the modification time of the two files.

View File

@@ -123,6 +123,7 @@ public:
void checkAndCreateDirForAddons(const std::string &dir);
bool removeFile(const std::string &name) const;
bool removeDirectory(const std::string &name) const;
bool copyFile(const std::string &source, const std::string &dest);
std::vector<std::string>getMusicDirs() const;
std::string getAssetChecked(AssetType type, const std::string& name,
bool abort_on_error=false) const;

View File

@@ -92,8 +92,8 @@ void BaseUserScreen::init()
const PlayerProfile *player = PlayerManager::get()->getPlayer(n);
if (player->isGuestAccount()) continue;
std::string s = StringUtils::toString(n);
m_players->addItem(player->getName(), s, "/karts/nolok/nolokicon.png", 0,
IconButtonWidget::ICON_PATH_TYPE_RELATIVE);
m_players->addItem(player->getName(), s, player->getIconFilename(), 0,
IconButtonWidget::ICON_PATH_TYPE_ABSOLUTE);
if(player==PlayerManager::getCurrentPlayer())
current_player_index = s;
}