Skip to content

Commit e491f8c

Browse files
committedJun 23, 2014
Only try to load from possible player files
1 parent 7e6db1b commit e491f8c

File tree

3 files changed

+37
-67
lines changed

3 files changed

+37
-67
lines changed
 

‎src/environment.cpp

+15-26
Original file line numberDiff line numberDiff line change
@@ -443,46 +443,35 @@ void ServerEnvironment::savePlayer(const std::string &playername)
443443

444444
Player *ServerEnvironment::loadPlayer(const std::string &playername)
445445
{
446-
std::string players_path = m_path_world + DIR_DELIM "players";
446+
std::string players_path = m_path_world + DIR_DELIM "players" DIR_DELIM;
447447

448448
RemotePlayer *player = static_cast<RemotePlayer*>(getPlayer(playername.c_str()));
449449
bool newplayer = false;
450-
bool foundplayer = false;
450+
bool found = false;
451451
if (!player) {
452452
player = new RemotePlayer(m_gamedef);
453453
newplayer = true;
454454
}
455455

456-
std::vector<fs::DirListNode> player_files = fs::GetDirListing(players_path);
457-
for (u32 i = 0; i < player_files.size(); i++) {
458-
if (player_files[i].dir)
459-
continue;
460-
461-
// Full path to this file
462-
std::string path = players_path + "/" + player_files[i].name;
463-
464-
// Load player to see what is its name
456+
RemotePlayer testplayer(m_gamedef);
457+
std::string path = players_path + playername;
458+
for (u32 i = 0; i < 1000; i++) {
459+
// Open file and deserialize
465460
std::ifstream is(path.c_str(), std::ios_base::binary);
466461
if (!is.good()) {
467-
infostream << "Failed to read " << path << std::endl;
468-
continue;
462+
return NULL;
469463
}
470-
player->deSerialize(is, player_files[i].name);
471-
472-
if (!string_allowed(player->getName(), PLAYERNAME_ALLOWED_CHARS)) {
473-
infostream << "Not loading player with invalid name: "
474-
<< player->getName() << std::endl;
475-
continue;
476-
}
477-
478-
if (player->getName() == playername) {
479-
// We found our player
480-
foundplayer = true;
464+
testplayer.deSerialize(is, path);
465+
if (testplayer.getName() == playername) {
466+
*player = testplayer;
467+
found = true;
481468
break;
482469
}
483-
470+
path = players_path + playername + itos(i);
484471
}
485-
if (!foundplayer) {
472+
if (!found) {
473+
infostream << "Player file for player " << playername
474+
<< " not found" << std::endl;
486475
return NULL;
487476
}
488477
if (newplayer) {

‎src/player.cpp

+21-40
Original file line numberDiff line numberDiff line change
@@ -284,69 +284,50 @@ void Player::clearHud()
284284
}
285285

286286

287-
void RemotePlayer::save(const std::string &savedir)
287+
void RemotePlayer::save(std::string savedir)
288288
{
289-
bool newplayer = true;
290-
291-
/* We have to iterate through all files in the players directory
289+
/*
290+
* We have to open all possible player files in the players directory
292291
* and check their player names because some file systems are not
293292
* case-sensitive and player names are case-sensitive.
294293
*/
295294

296295
// A player to deserialize files into to check their names
297296
RemotePlayer testplayer(m_gamedef);
298297

299-
std::vector<fs::DirListNode> player_files = fs::GetDirListing(savedir);
300-
for(u32 i = 0; i < player_files.size(); i++) {
301-
if (player_files[i].dir || player_files[i].name[0] == '.') {
302-
continue;
298+
savedir += DIR_DELIM;
299+
std::string path = savedir + m_name;
300+
for (u32 i = 0; i < 1000; i++) {
301+
if (!fs::PathExists(path)) {
302+
// Open file and serialize
303+
std::ostringstream ss(std::ios_base::binary);
304+
serialize(ss);
305+
if (!fs::safeWriteToFile(path, ss.str())) {
306+
infostream << "Failed to write " << path << std::endl;
307+
}
308+
return;
303309
}
304-
305-
// Full path to this file
306-
std::string path = savedir + "/" + player_files[i].name;
307-
308310
// Open file and deserialize
309311
std::ifstream is(path.c_str(), std::ios_base::binary);
310312
if (!is.good()) {
311-
infostream << "Failed to read " << path << std::endl;
312-
continue;
313+
infostream << "Failed to open " << path << std::endl;
314+
return;
313315
}
314-
testplayer.deSerialize(is, player_files[i].name);
315-
316+
testplayer.deSerialize(is, path);
316317
if (strcmp(testplayer.getName(), m_name) == 0) {
317318
// Open file and serialize
318319
std::ostringstream ss(std::ios_base::binary);
319320
serialize(ss);
320321
if (!fs::safeWriteToFile(path, ss.str())) {
321322
infostream << "Failed to write " << path << std::endl;
322323
}
323-
newplayer = false;
324-
break;
325-
}
326-
}
327-
328-
if (newplayer) {
329-
bool found = false;
330-
std::string path = savedir + "/" + m_name;
331-
for (u32 i = 0; i < 1000; i++) {
332-
if (!fs::PathExists(path)) {
333-
found = true;
334-
break;
335-
}
336-
path = savedir + "/" + m_name + itos(i);
337-
}
338-
if (!found) {
339-
infostream << "Didn't find free file for player " << m_name << std::endl;
340324
return;
341325
}
342-
343-
// Open file and serialize
344-
std::ostringstream ss(std::ios_base::binary);
345-
serialize(ss);
346-
if (!fs::safeWriteToFile(path, ss.str())) {
347-
infostream << "Failed to write " << path << std::endl;
348-
}
326+
path = savedir + m_name + itos(i);
349327
}
328+
329+
infostream << "Didn't find free file for player " << m_name << std::endl;
330+
return;
350331
}
351332

352333
/*

‎src/player.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -335,7 +335,7 @@ class RemotePlayer : public Player
335335
RemotePlayer(IGameDef *gamedef): Player(gamedef), m_sao(0) {}
336336
virtual ~RemotePlayer() {}
337337

338-
void save(const std::string &savedir);
338+
void save(std::string savedir);
339339

340340
PlayerSAO *getPlayerSAO()
341341
{ return m_sao; }

0 commit comments

Comments
 (0)
Please sign in to comment.