TrinityCore
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
PlayerDumpReader Class Reference

#include <PlayerDump.h>

Public Member Functions

 PlayerDumpReader ()
 
DumpReturn LoadDump (std::string const &file, uint32 account, std::string name, ObjectGuid::LowType guid)
 

Additional Inherited Members

- Public Types inherited from PlayerDump
typedef std::set
< ObjectGuid::LowType
DumpGuidSet
 
typedef std::map
< ObjectGuid::LowType,
ObjectGuid::LowType
DumpGuidMap
 
- Protected Member Functions inherited from PlayerDump
 PlayerDump ()
 

Constructor & Destructor Documentation

PlayerDumpReader::PlayerDumpReader ( )
inline
100 { }

Member Function Documentation

DumpReturn PlayerDumpReader::LoadDump ( std::string const file,
uint32  account,
std::string  name,
ObjectGuid::LowType  guid 
)
407 {
408  uint32 charcount = AccountMgr::GetCharactersCount(account);
409  if (charcount >= sWorld->getIntConfig(CONFIG_CHARACTERS_PER_REALM))
410  return DUMP_TOO_MANY_CHARS;
411 
412  FILE* fin = fopen(file.c_str(), "r");
413  if (!fin)
414  return DUMP_FILE_OPEN_ERROR;
415 
416  char newguid[20], chraccount[20];
417 
418  // make sure the same guid doesn't already exist and is safe to use
419  bool incHighest = true;
420  if (guid && guid < sObjectMgr->GetGenerator<HighGuid::Player>().GetNextAfterMaxUsed())
421  {
423  stmt->setUInt64(0, guid);
425 
426  if (result)
427  guid = sObjectMgr->GetGenerator<HighGuid::Player>().GetNextAfterMaxUsed(); // use first free if exists
428  else
429  incHighest = false;
430  }
431  else
432  guid = sObjectMgr->GetGenerator<HighGuid::Player>().GetNextAfterMaxUsed();
433 
434  // normalize the name if specified and check if it exists
435  if (!normalizePlayerName(name))
436  name.clear();
437 
438  if (ObjectMgr::CheckPlayerName(name, sWorld->GetDefaultDbcLocale(), true) == CHAR_NAME_SUCCESS)
439  {
441  stmt->setString(0, name);
443 
444  if (result)
445  name.clear(); // use the one from the dump
446  }
447  else
448  name.clear();
449 
450  // name encoded or empty
451 
452  snprintf(newguid, 20, UI64FMTD, guid);
453  snprintf(chraccount, 20, "%u", account);
454 
455  DumpGuidMap items;
456  DumpGuidMap mails;
457  char buf[32000] = "";
458 
459  typedef std::map<uint32 /*old*/, uint32 /*new*/> PetIds;
460  PetIds petIds;
461 
462  uint8 gender = GENDER_NONE;
463  uint8 race = RACE_NONE;
464  uint8 playerClass = 0;
465  uint8 level = 1;
466 
467  ObjectGuid::LowType itemLowGuidOffset = sObjectMgr->GetGenerator<HighGuid::Item>().GetNextAfterMaxUsed();
468 
470  while (!feof(fin))
471  {
472  if (!fgets(buf, 32000, fin))
473  {
474  if (feof(fin))
475  break;
477  }
478 
479  std::string line; line.assign(buf);
480 
481  // skip empty strings
482  size_t nw_pos = line.find_first_not_of(" \t\n\r\7");
483  if (nw_pos == std::string::npos)
484  continue;
485 
486  // skip logfile-side dump start notice, the important notes and dump end notices
487  if ((line.substr(nw_pos, 16) == "== START DUMP ==") ||
488  (line.substr(nw_pos, 15) == "IMPORTANT NOTE:") ||
489  (line.substr(nw_pos, 14) == "== END DUMP =="))
490  continue;
491 
492  // add required_ check
493  /*
494  if (line.substr(nw_pos, 41) == "UPDATE character_db_version SET required_")
495  {
496  if (!CharacterDatabase.Execute(line.c_str()))
497  ROLLBACK(DUMP_FILE_BROKEN);
498 
499  continue;
500  }
501  */
502 
503  // determine table name and load type
504  std::string tn = GetTableName(line);
505  if (tn.empty())
506  {
507  TC_LOG_ERROR("misc", "LoadPlayerDump: Can't extract table name from line: '%s'!", line.c_str());
509  }
510 
511  DumpTableType type = DumpTableType(0);
512  uint8 i;
513  for (i = 0; i < DUMP_TABLE_COUNT; ++i)
514  {
515  if (tn == dumpTables[i].name)
516  {
517  type = dumpTables[i].type;
518  break;
519  }
520  }
521 
522  if (i == DUMP_TABLE_COUNT)
523  {
524  TC_LOG_ERROR("misc", "LoadPlayerDump: Unknown table: '%s'!", tn.c_str());
526  }
527 
528  // change the data to server values
529  switch (type)
530  {
531  case DTT_CHARACTER:
532  {
533  if (!ChangeNth(line, 1, newguid)) // characters.guid update
535 
536  if (!ChangeNth(line, 2, chraccount)) // characters.account update
538 
539  race = uint8(atoul(GetNth(line, 4).c_str()));
540  playerClass = uint8(atoul(GetNth(line, 5).c_str()));
541  gender = uint8(atoul(GetNth(line, 6).c_str()));
542  level = uint8(atoul(GetNth(line, 7).c_str()));
543  if (name.empty())
544  {
545  // check if the original name already exists
546  name = GetNth(line, 3);
547 
549  stmt->setString(0, name);
551 
552  if (result)
553  if (!ChangeNth(line, 41, "1")) // characters.at_login set to "rename on login"
555  }
556  else if (!ChangeNth(line, 3, name.c_str())) // characters.name
558 
559  const char null[5] = "NULL";
560  if (!ChangeNth(line, 65, null)) // characters.deleteInfos_Account
562  if (!ChangeNth(line, 66, null)) // characters.deleteInfos_Name
564  if (!ChangeNth(line, 67, null)) // characters.deleteDate
566  break;
567  }
568  case DTT_CHAR_TABLE:
569  case DTT_CURRENCY:
570  {
571  if (!ChangeNth(line, 1, newguid)) // character_*.guid update
573  break;
574  }
575  case DTT_EQSET_TABLE:
576  {
577  if (!ChangeNth(line, 1, newguid))
578  ROLLBACK(DUMP_FILE_BROKEN); // character_equipmentsets.guid
579 
580  char newSetGuid[24];
581  snprintf(newSetGuid, 24, UI64FMTD, sObjectMgr->GenerateEquipmentSetGuid());
582  if (!ChangeNth(line, 2, newSetGuid))
583  ROLLBACK(DUMP_FILE_BROKEN); // character_equipmentsets.setguid
584 
585  for (uint8 slot = 0; slot < EQUIPMENT_SLOT_END; ++slot)
586  if (!ChangeGuid(line, 7 + slot, items, itemLowGuidOffset, true))
587  ROLLBACK(DUMP_FILE_BROKEN); // character_equipmentsets.item
588  break;
589  }
590  case DTT_INVENTORY:
591  {
592  if (!ChangeNth(line, 1, newguid)) // character_inventory.guid update
594 
595  if (!ChangeGuid(line, 2, items, itemLowGuidOffset, true))
596  ROLLBACK(DUMP_FILE_BROKEN); // character_inventory.bag update
597  if (!ChangeGuid(line, 4, items, itemLowGuidOffset))
598  ROLLBACK(DUMP_FILE_BROKEN); // character_inventory.item update
599  break;
600  }
601  case DTT_MAIL: // mail
602  {
603  if (!ChangeGuid(line, 1, mails, sObjectMgr->_mailId))
604  ROLLBACK(DUMP_FILE_BROKEN); // mail.id update
605  if (!ChangeNth(line, 6, newguid)) // mail.receiver update
607  break;
608  }
609  case DTT_MAIL_ITEM: // mail_items
610  {
611  if (!ChangeGuid(line, 1, mails, sObjectMgr->_mailId))
612  ROLLBACK(DUMP_FILE_BROKEN); // mail_items.id
613  if (!ChangeGuid(line, 2, items, itemLowGuidOffset))
614  ROLLBACK(DUMP_FILE_BROKEN); // mail_items.item_guid
615  if (!ChangeNth(line, 3, newguid)) // mail_items.receiver
617  break;
618  }
619  case DTT_ITEM:
620  {
621  // item, owner, data field:item, owner guid
622  if (!ChangeGuid(line, 1, items, itemLowGuidOffset))
623  ROLLBACK(DUMP_FILE_BROKEN); // item_instance.guid update
624  if (!ChangeNth(line, 3, newguid)) // item_instance.owner_guid update
626  break;
627  }
628  case DTT_ITEM_GIFT:
629  {
630  if (!ChangeNth(line, 1, newguid)) // character_gifts.guid update
632  if (!ChangeGuid(line, 2, items, itemLowGuidOffset))
633  ROLLBACK(DUMP_FILE_BROKEN); // character_gifts.item_guid update
634  break;
635  }
636  case DTT_PET:
637  {
638  // store a map of old pet id to new inserted pet id for use by DTT_PET_TABLE tables
639  std::string petIdStr = GetNth(line, 1);
640 
641  uint32 currentPetId = atoul(petIdStr.c_str());
642 
643  PetIds::const_iterator petIdsItr = petIds.find(currentPetId);
644  if (petIdsItr != petIds.end()) // duplicate pets
646 
647  uint32 newPetId = sObjectMgr->GeneratePetNumber();
648  petIds[currentPetId] = newPetId;
649 
650  if (!ChangeNth(line, 1, std::to_string(newPetId).c_str())) // character_pet.id update
652  if (!ChangeNth(line, 3, newguid)) // character_pet.owner update
654 
655  break;
656  }
657  case DTT_PET_TABLE: // pet_aura, pet_spell, pet_spell_cooldown
658  {
659  std::string petIdStr = GetNth(line, 1);
660 
661  // lookup currpetid and match to new inserted pet id
662  PetIds::const_iterator petIdsItr = petIds.find(atoul(petIdStr.c_str()));
663  if (petIdsItr == petIds.end()) // couldn't find new inserted id
665 
666  if (!ChangeNth(line, 1, std::to_string(petIdsItr->second).c_str()))
668 
669  break;
670  }
671  default:
672  TC_LOG_ERROR("misc", "Unknown dump table type: %u", type);
673  break;
674  }
675 
676  fixNULLfields(line);
677 
678  trans->Append(line.c_str());
679  }
680 
682 
683  // in case of name conflict player has to rename at login anyway
684  sWorld->AddCharacterInfo(ObjectGuid::Create<HighGuid::Player>(guid), account, name, gender, race, playerClass, level, false);
685 
686  sObjectMgr->GetGenerator<HighGuid::Item>().Set(sObjectMgr->GetGenerator<HighGuid::Item>().GetNextAfterMaxUsed() + items.size());
687  sObjectMgr->_mailId += mails.size();
688 
689  if (incHighest)
690  sObjectMgr->GetGenerator<HighGuid::Player>().Generate();
691 
692  fclose(fin);
693 
694  return DUMP_SUCCESS;
695 }
#define ROLLBACK(DR)
Definition: PlayerDump.cpp:393
static ResponseCodes CheckPlayerName(std::string const &name, LocaleConstant locale, bool create=false)
Definition: ObjectMgr.cpp:7580
#define DUMP_TABLE_COUNT
Definition: PlayerDump.cpp:28
Definition: PlayerDump.h:31
std::shared_ptr< PreparedResultSet > PreparedQueryResult
Definition: QueryResult.h:107
Definition: PlayerDump.h:49
DumpTableType type
Definition: PlayerDump.cpp:32
#define snprintf
Definition: Common.h:76
Definition: SharedDefines.h:101
SQLTransaction BeginTransaction()
Begins an automanaged transaction pointer that will automatically rollback if not commited...
Definition: DatabaseWorkerPool.h:221
void setString(const uint8 index, const std::string &value)
Definition: PreparedStatement.cpp:187
static uint32 GetCharactersCount(uint32 accountId)
Definition: AccountMgr.cpp:368
Definition: SharedDefines.h:4505
DumpTableType
Definition: PlayerDump.h:27
#define sWorld
Definition: World.h:887
std::map< ObjectGuid::LowType, ObjectGuid::LowType > DumpGuidMap
Definition: PlayerDump.h:72
Definition: PlayerDump.h:61
uint64 LowType
Definition: ObjectGuid.h:199
#define sObjectMgr
Definition: ObjectMgr.h:1567
Definition: PreparedStatement.h:74
void CommitTransaction(SQLTransaction transaction)
Definition: DatabaseWorkerPool.cpp:179
Definition: PlayerDump.h:60
bool normalizePlayerName(std::string &name)
Definition: ObjectMgr.cpp:133
Definition: PlayerDump.h:37
unsigned long atoul(char const *str)
Definition: Common.h:90
Definition: CharacterDatabase.h:37
bool ChangeGuid(std::string &str, uint32 n, PlayerDump::DumpGuidMap &guidMap, ObjectGuid::LowType guidOffset, bool allowZero=false)
Definition: PlayerDump.cpp:191
#define UI64FMTD
Definition: Define.h:137
Definition: PlayerDump.h:64
uint32_t uint32
Definition: Define.h:150
Definition: PlayerDump.h:46
std::string GetNth(std::string &str, uint32 n)
Definition: PlayerDump.cpp:154
PreparedStatement * GetPreparedStatement(PreparedStatementIndex index)
Definition: DatabaseWorkerPool.h:263
std::string GetTableName(std::string const &str)
Definition: PlayerDump.cpp:127
Definition: PlayerDump.h:39
uint8_t uint8
Definition: g3dmath.h:164
Definition: PlayerDump.h:29
QueryResult Query(const char *sql, T *connection=nullptr)
Definition: DatabaseWorkerPool.cpp:113
Definition: PlayerDump.h:55
Definition: PlayerDump.h:62
static DumpTable dumpTables[DUMP_TABLE_COUNT]
Definition: PlayerDump.cpp:35
Definition: PlayerDump.h:52
void setUInt64(const uint8 index, const uint64 value)
Definition: PreparedStatement.cpp:124
uint8_t uint8
Definition: Define.h:152
bool ChangeNth(std::string &str, uint32 n, char const *with, bool insert=false, bool allowZero=false)
Definition: PlayerDump.cpp:137
void fixNULLfields(std::string &line)
Definition: PlayerDump.cpp:395
CharacterDatabaseWorkerPool CharacterDatabase
Accessor to the character database.
Definition: DatabaseEnv.cpp:21
Definition: PlayerDump.h:54
#define TC_LOG_ERROR(filterType__,...)
Definition: Log.h:207
Definition: World.h:231
Definition: PlayerDump.h:41
Definition: CharacterDatabase.h:38
Definition: PlayerDump.h:43
Definition: SharedDefines.h:95
std::shared_ptr< Transaction > SQLTransaction
Definition: Transaction.h:58

+ Here is the call graph for this function:


The documentation for this class was generated from the following files: