2010-10-07 15:35:36 +02:00
/*
2017-01-01 16:21:30 +01:00
* Copyright (C) 2008-2017 TrinityCore <http://www.trinitycore.org/>
2010-10-07 15:35:36 +02:00
* Copyright (C) 2005-2009 MaNGOS <http://getmangos.com/>
*
* This program is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License as published by the
* Free Software Foundation; either version 2 of the License, or (at your
* option) any later version.
*
* This program is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
* more details.
*
* You should have received a copy of the GNU General Public License along
* with this program. If not, see <http://www.gnu.org/licenses/>.
2008-10-02 16:23:55 -05:00
*/
2009-10-17 15:51:44 -07:00
2008-10-02 16:23:55 -05:00
#include "Common.h"
#include "PlayerDump.h"
2010-06-08 17:01:03 -06:00
#include "DatabaseEnv.h"
2008-10-02 16:23:55 -05:00
#include "ObjectMgr.h"
2015-03-06 04:16:12 +01:00
#include "Player.h"
2010-07-21 12:13:23 -06:00
#include "AccountMgr.h"
2012-11-20 14:54:33 +01:00
#include "World.h"
2009-10-17 15:51:44 -07:00
2016-09-13 04:16:05 +02:00
#define DUMP_TABLE_COUNT 35
2008-10-02 16:23:55 -05:00
struct DumpTable
{
char const * name ;
DumpTableType type ;
};
2009-10-17 15:51:44 -07:00
2016-08-16 02:18:02 -03:00
DumpTable const dumpTables [ DUMP_TABLE_COUNT ] =
2008-10-02 16:23:55 -05:00
{
2009-04-14 18:27:02 -05:00
{ "characters" , DTT_CHARACTER },
2010-05-05 13:11:07 +02:00
{ "character_account_data" , DTT_CHAR_TABLE },
2009-04-14 18:27:02 -05:00
{ "character_achievement" , DTT_CHAR_TABLE },
{ "character_achievement_progress" , DTT_CHAR_TABLE },
{ "character_action" , DTT_CHAR_TABLE },
2015-03-06 04:16:12 +01:00
// { "character_aura", DTT_CHAR_TABLE }, /// @todo: reguid casterguid (full guid)
// { "character_aura_effect", DTT_CHAR_TABLE }, /// @todo: reguid casterguid (full guid)
2012-09-08 16:40:36 +01:00
{ "character_cuf_profiles" , DTT_CHAR_TABLE },
2015-03-06 04:16:12 +01:00
{ "character_currency" , DTT_CURRENCY },
2010-05-05 13:11:07 +02:00
{ "character_declinedname" , DTT_CHAR_TABLE },
{ "character_equipmentsets" , DTT_EQSET_TABLE },
2016-09-13 04:16:05 +02:00
{ "character_fishingsteps" , DTT_CHAR_TABLE },
2010-05-05 13:11:07 +02:00
{ "character_glyphs" , DTT_CHAR_TABLE },
2009-04-14 18:27:02 -05:00
{ "character_homebind" , DTT_CHAR_TABLE },
2010-05-05 13:11:07 +02:00
{ "character_inventory" , DTT_INVENTORY },
{ "character_pet" , DTT_PET },
{ "character_pet_declinedname" , DTT_PET },
{ "character_queststatus" , DTT_CHAR_TABLE },
2015-03-06 04:16:12 +01:00
{ "character_queststatus_objectives" , DTT_CHAR_TABLE },
2016-08-16 02:18:02 -03:00
{ "character_queststatus_daily" , DTT_CHAR_TABLE },
{ "character_queststatus_weekly" , DTT_CHAR_TABLE },
{ "character_queststatus_monthly" , DTT_CHAR_TABLE },
{ "character_queststatus_seasonal" , DTT_CHAR_TABLE },
2010-12-26 04:16:18 +01:00
{ "character_queststatus_rewarded" , DTT_CHAR_TABLE },
2010-05-05 13:11:07 +02:00
{ "character_reputation" , DTT_CHAR_TABLE },
2009-12-14 20:47:43 +01:00
{ "character_skills" , DTT_CHAR_TABLE },
2010-05-05 13:11:07 +02:00
{ "character_spell" , DTT_CHAR_TABLE },
2015-03-06 04:16:12 +01:00
{ "character_spell_charges" , DTT_CHAR_TABLE },
2010-05-05 13:11:07 +02:00
{ "character_spell_cooldown" , DTT_CHAR_TABLE },
2009-09-28 01:14:21 -04:00
{ "character_talent" , DTT_CHAR_TABLE },
2015-03-06 04:16:12 +01:00
/// @todo: character_void_storage
2009-04-14 18:27:02 -05:00
{ "mail" , DTT_MAIL },
2014-02-13 23:16:49 +01:00
{ "mail_items" , DTT_MAIL_ITEM }, // must be after mail
2015-03-06 04:16:12 +01:00
// { "pet_aura", DTT_PET_TABLE }, // must be after character_pet /// @todo: reguid casterguid (full guid)
// { "pet_aura_effect", DTT_PET_TABLE }, // must be after character_pet /// @todo: reguid casterguid (full guid)
2014-02-13 23:16:49 +01:00
{ "pet_spell" , DTT_PET_TABLE }, // must be after character_pet
2015-03-06 04:16:12 +01:00
{ "pet_spell_charges" , DTT_PET_TABLE }, // must be after character_pet
2014-02-13 23:16:49 +01:00
{ "pet_spell_cooldown" , DTT_PET_TABLE }, // must be after character_pet
{ "item_instance" , DTT_ITEM }, // must be after character_inventory and mail_items
{ "character_gifts" , DTT_ITEM_GIFT }, // must be after item_instance
2008-10-02 16:23:55 -05:00
};
2009-10-17 15:51:44 -07:00
2008-10-02 16:23:55 -05:00
// Low level functions
2015-03-06 04:16:12 +01:00
static bool FindTokNth ( std :: string const & str , uint32 n , std :: string :: size_type & s , std :: string :: size_type & e )
2008-10-02 16:23:55 -05:00
{
2015-03-06 04:16:12 +01:00
s = e = 0 ;
uint32 i = 1 ;
for (; s < str . size () && i < n ; ++ s )
if ( str [ s ] == ' ' )
++ i ;
2008-10-02 16:23:55 -05:00
if ( i < n )
return false ;
2009-10-17 15:51:44 -07:00
2008-10-02 16:23:55 -05:00
e = str . find ( ' ' , s );
2009-10-17 15:51:44 -07:00
2008-10-02 16:23:55 -05:00
return e != std :: string :: npos ;
}
2009-10-17 15:51:44 -07:00
2015-03-06 04:16:12 +01:00
std :: string GetTokNth ( std :: string const & str , uint32 n )
2008-10-02 16:23:55 -05:00
{
std :: string :: size_type s = 0 , e = 0 ;
2015-03-06 04:16:12 +01:00
if ( ! FindTokNth ( str , n , s , e ))
2008-10-02 16:23:55 -05:00
return "" ;
2009-10-17 15:51:44 -07:00
2015-03-06 04:16:12 +01:00
return str . substr ( s , e - s );
2008-10-02 16:23:55 -05:00
}
2009-10-17 15:51:44 -07:00
2015-03-06 04:16:12 +01:00
bool FindNth ( std :: string const & str , uint32 n , std :: string :: size_type & s , std :: string :: size_type & e )
2008-10-02 16:23:55 -05:00
{
2015-03-06 04:16:12 +01:00
s = str . find ( "VALUES ('" ) + 9 ;
if ( s == std :: string :: npos )
return false ;
2009-10-17 15:51:44 -07:00
2008-10-02 16:23:55 -05:00
do
{
2011-07-29 14:18:28 +02:00
e = str . find ( '\'' , s );
2015-03-06 04:16:12 +01:00
if ( e == std :: string :: npos )
return false ;
} while ( str [ e - 1 ] == '\\' );
2009-10-17 15:51:44 -07:00
2015-03-06 04:16:12 +01:00
for ( uint32 i = 1 ; i < n ; ++ i )
2008-10-02 16:23:55 -05:00
{
do
{
2015-03-06 04:16:12 +01:00
s = e + 4 ;
2011-07-29 14:18:28 +02:00
e = str . find ( '\'' , s );
2015-03-06 04:16:12 +01:00
if ( e == std :: string :: npos )
return false ;
} while ( str [ e - 1 ] == '\\' );
2008-10-02 16:23:55 -05:00
}
return true ;
}
2009-10-17 15:51:44 -07:00
2015-03-06 04:16:12 +01:00
std :: string GetTableName ( std :: string const & str )
2008-10-02 16:23:55 -05:00
{
2015-03-06 04:16:12 +01:00
static std :: string :: size_type const s = 13 ;
2008-10-02 16:23:55 -05:00
std :: string :: size_type e = str . find ( _TABLE_SIM_ , s );
if ( e == std :: string :: npos )
return "" ;
2009-10-17 15:51:44 -07:00
2015-03-06 04:16:12 +01:00
return str . substr ( s , e - s );
2008-10-02 16:23:55 -05:00
}
2009-10-17 15:51:44 -07:00
2015-03-06 04:16:12 +01:00
bool ChangeNth ( std :: string & str , uint32 n , char const * with , bool insert = false , bool allowZero = false )
2008-10-02 16:23:55 -05:00
{
std :: string :: size_type s , e ;
2015-03-06 04:16:12 +01:00
if ( ! FindNth ( str , n , s , e ))
2008-10-02 16:23:55 -05:00
return false ;
2009-10-17 15:51:44 -07:00
2015-03-06 04:16:12 +01:00
if ( allowZero && str . substr ( s , e - s ) == "0" )
2008-10-02 16:23:55 -05:00
return true ; // not an error
2015-03-06 04:16:12 +01:00
2010-04-07 19:14:10 +02:00
if ( ! insert )
2015-03-06 04:16:12 +01:00
str . replace ( s , e - s , with );
2008-10-02 16:23:55 -05:00
else
str . insert ( s , with );
2009-10-17 15:51:44 -07:00
2008-10-02 16:23:55 -05:00
return true ;
}
2009-10-17 15:51:44 -07:00
2015-03-06 04:16:12 +01:00
std :: string GetNth ( std :: string & str , uint32 n )
2008-10-02 16:23:55 -05:00
{
std :: string :: size_type s , e ;
2015-03-06 04:16:12 +01:00
if ( ! FindNth ( str , n , s , e ))
2008-10-02 16:23:55 -05:00
return "" ;
2009-10-17 15:51:44 -07:00
2008-10-02 16:23:55 -05:00
return str . substr ( s , e - s );
}
2009-10-17 15:51:44 -07:00
2015-03-06 04:16:12 +01:00
bool ChangeTokNth ( std :: string & str , uint32 n , char const * with , bool insert = false , bool allowZero = false )
2008-10-02 16:23:55 -05:00
{
std :: string :: size_type s = 0 , e = 0 ;
2015-03-06 04:16:12 +01:00
if ( ! FindTokNth ( str , n , s , e ))
2008-10-02 16:23:55 -05:00
return false ;
2015-03-06 04:16:12 +01:00
if ( allowZero && str . substr ( s , e - s ) == "0" )
2008-10-02 16:23:55 -05:00
return true ; // not an error
2015-03-06 04:16:12 +01:00
2010-04-07 19:14:10 +02:00
if ( ! insert )
2008-10-02 16:23:55 -05:00
str . replace ( s , e - s , with );
else
str . insert ( s , with );
2009-10-17 15:51:44 -07:00
2008-10-02 16:23:55 -05:00
return true ;
}
2009-10-17 15:51:44 -07:00
2015-03-06 04:16:12 +01:00
ObjectGuid :: LowType RegisterNewGuid ( ObjectGuid :: LowType oldGuid , PlayerDump :: DumpGuidMap & guidMap , ObjectGuid :: LowType guidOffset )
2008-10-02 16:23:55 -05:00
{
2015-03-06 04:16:12 +01:00
PlayerDumpWriter :: DumpGuidMap :: const_iterator itr = guidMap . find ( oldGuid );
2010-04-07 19:14:10 +02:00
if ( itr != guidMap . end ())
2008-10-02 16:23:55 -05:00
return itr -> second ;
2009-10-17 15:51:44 -07:00
2015-03-06 04:16:12 +01:00
ObjectGuid :: LowType newguid = guidOffset + guidMap . size ();
2008-10-02 16:23:55 -05:00
guidMap [ oldGuid ] = newguid ;
return newguid ;
}
2009-10-17 15:51:44 -07:00
2015-03-06 04:16:12 +01:00
bool ChangeGuid ( std :: string & str , uint32 n , PlayerDump :: DumpGuidMap & guidMap , ObjectGuid :: LowType guidOffset , bool allowZero = false )
2008-10-02 16:23:55 -05:00
{
2015-03-06 04:16:12 +01:00
ObjectGuid :: LowType oldGuid = strtoull ( GetNth ( str , n ). c_str (), nullptr , 10 );
if ( allowZero && ! oldGuid )
2008-10-02 16:23:55 -05:00
return true ; // not an error
2009-10-17 15:51:44 -07:00
2015-03-06 04:16:12 +01:00
char chritem [ 21 ];
ObjectGuid :: LowType newGuid = RegisterNewGuid ( oldGuid , guidMap , guidOffset );
2014-10-26 02:57:28 +02:00
snprintf ( chritem , 21 , UI64FMTD , newGuid );
2009-10-17 15:51:44 -07:00
2015-03-06 04:16:12 +01:00
return ChangeNth ( str , n , chritem , false , allowZero );
2008-10-02 16:23:55 -05:00
}
2009-10-17 15:51:44 -07:00
2010-09-11 21:10:54 +02:00
std :: string CreateDumpString ( char const * tableName , QueryResult result )
2008-10-02 16:23:55 -05:00
{
2010-04-07 19:14:10 +02:00
if ( ! tableName || ! result ) return "" ;
2008-10-02 16:23:55 -05:00
std :: ostringstream ss ;
2012-10-24 15:34:23 +02:00
ss << "INSERT INTO " << _TABLE_SIM_ << tableName << _TABLE_SIM_ << " VALUES (" ;
2011-09-15 14:08:17 +02:00
Field * fields = result -> Fetch ();
2009-10-17 16:20:24 -07:00
for ( uint32 i = 0 ; i < result -> GetFieldCount (); ++ i )
2008-10-02 16:23:55 -05:00
{
2011-07-29 14:18:28 +02:00
if ( i == 0 ) ss << '\'' ;
2008-10-02 16:23:55 -05:00
else ss << ", '" ;
2009-10-17 15:51:44 -07:00
2010-09-24 22:16:21 +02:00
std :: string s = fields [ i ]. GetString ();
2011-06-30 14:52:44 +02:00
CharacterDatabase . EscapeString ( s );
2008-10-02 16:23:55 -05:00
ss << s ;
2009-10-17 15:51:44 -07:00
2011-07-29 14:18:28 +02:00
ss << '\'' ;
2008-10-02 16:23:55 -05:00
}
ss << ");" ;
return ss . str ();
}
2009-10-17 15:51:44 -07:00
2014-10-24 19:56:17 +02:00
std :: string PlayerDumpWriter :: GenerateWhereStr ( char const * field , ObjectGuid :: LowType guid )
2008-10-02 16:23:55 -05:00
{
std :: ostringstream wherestr ;
2011-07-29 14:18:28 +02:00
wherestr << field << " = '" << guid << '\'' ;
2008-10-02 16:23:55 -05:00
return wherestr . str ();
}
2009-10-17 15:51:44 -07:00
2015-03-06 04:16:12 +01:00
std :: string PlayerDumpWriter :: GenerateWhereStr ( char const * field , DumpGuidSet const & guids , DumpGuidSet :: const_iterator & itr )
2008-10-02 16:23:55 -05:00
{
std :: ostringstream wherestr ;
wherestr << field << " IN ('" ;
2015-03-06 04:16:12 +01:00
for (; itr != guids . end ();)
2008-10-02 16:23:55 -05:00
{
wherestr << * itr ;
2015-03-06 04:16:12 +01:00
++ itr ;
2009-10-17 15:51:44 -07:00
2010-04-07 19:14:10 +02:00
if ( wherestr . str (). size () > MAX_QUERY_LEN - 50 ) // near to max query
2008-10-02 16:23:55 -05:00
break ;
2009-10-17 15:51:44 -07:00
2015-03-06 04:16:12 +01:00
if ( itr != guids . end ())
2011-04-29 20:47:02 +02:00
wherestr << "', '" ;
2008-10-02 16:23:55 -05:00
}
wherestr << "')" ;
return wherestr . str ();
}
2009-10-17 15:51:44 -07:00
2015-03-06 04:16:12 +01:00
void StoreGUID ( QueryResult result , uint32 field , PlayerDump :: DumpGuidSet & guids )
2008-10-02 16:23:55 -05:00
{
Field * fields = result -> Fetch ();
2014-10-24 19:56:17 +02:00
ObjectGuid :: LowType guid = fields [ field ]. GetUInt64 ();
2010-04-07 19:14:10 +02:00
if ( guid )
2008-10-02 16:23:55 -05:00
guids . insert ( guid );
}
2009-10-17 15:51:44 -07:00
2015-03-06 04:16:12 +01:00
void StoreGUID ( QueryResult result , uint32 data , uint32 field , PlayerDump :: DumpGuidSet & guids )
2008-10-02 16:23:55 -05:00
{
Field * fields = result -> Fetch ();
2010-09-24 22:16:21 +02:00
std :: string dataStr = fields [ data ]. GetString ();
2015-03-06 04:16:12 +01:00
ObjectGuid :: LowType guid = strtoull ( GetTokNth ( dataStr , field ). c_str (), nullptr , 10 );
2010-04-07 19:14:10 +02:00
if ( guid )
2008-10-02 16:23:55 -05:00
guids . insert ( guid );
}
2009-10-17 15:51:44 -07:00
2008-10-02 16:23:55 -05:00
// Writing - High-level functions
2015-03-06 04:16:12 +01:00
bool PlayerDumpWriter :: DumpTable ( std :: string & dump , ObjectGuid :: LowType guid , char const * tableFrom , char const * tableTo , DumpTableType type )
2008-10-02 16:23:55 -05:00
{
2015-03-06 04:16:12 +01:00
DumpGuidSet const * guids = nullptr ;
char const * fieldname = nullptr ;
2009-10-17 15:51:44 -07:00
2010-04-07 22:59:46 +02:00
switch ( type )
2008-10-02 16:23:55 -05:00
{
2015-03-06 04:16:12 +01:00
case DTT_CURRENCY : fieldname = "CharacterGuid" ; break ;
2008-10-02 16:23:55 -05:00
case DTT_ITEM : fieldname = "guid" ; guids = & items ; break ;
case DTT_ITEM_GIFT : fieldname = "item_guid" ; guids = & items ; break ;
case DTT_PET : fieldname = "owner" ; break ;
case DTT_PET_TABLE : fieldname = "guid" ; guids = & pets ; break ;
case DTT_MAIL : fieldname = "receiver" ; break ;
case DTT_MAIL_ITEM : fieldname = "mail_id" ; guids = & mails ; break ;
default : fieldname = "guid" ; break ;
}
2009-10-17 15:51:44 -07:00
2008-10-02 16:23:55 -05:00
// for guid set stop if set is empty
2010-04-07 19:14:10 +02:00
if ( guids && guids -> empty ())
2015-03-06 04:16:12 +01:00
return true ; // nothing to do
2009-10-17 15:51:44 -07:00
2008-10-02 16:23:55 -05:00
// setup for guids case start position
2015-03-06 04:16:12 +01:00
DumpGuidSet :: const_iterator guidsItr ;
2010-04-07 19:14:10 +02:00
if ( guids )
2015-03-06 04:16:12 +01:00
guidsItr = guids -> begin ();
2009-10-17 15:51:44 -07:00
2008-10-02 16:23:55 -05:00
do
{
std :: string wherestr ;
2009-10-17 15:51:44 -07:00
2015-03-06 04:16:12 +01:00
if ( guids ) // set case, get next guids string
wherestr = GenerateWhereStr ( fieldname , * guids , guidsItr );
else // not set case, get single guid string
2011-04-29 20:47:02 +02:00
wherestr = GenerateWhereStr ( fieldname , guid );
2009-10-17 15:51:44 -07:00
2010-09-11 21:10:54 +02:00
QueryResult result = CharacterDatabase . PQuery ( "SELECT * FROM %s WHERE %s" , tableFrom , wherestr . c_str ());
2010-04-07 19:14:10 +02:00
if ( ! result )
2010-08-02 15:14:47 +02:00
return true ;
2009-10-17 15:51:44 -07:00
2008-10-02 16:23:55 -05:00
do
{
// collect guids
2010-04-07 22:59:46 +02:00
switch ( type )
2008-10-02 16:23:55 -05:00
{
2009-07-12 17:56:46 +08:00
case DTT_INVENTORY :
2012-04-09 04:17:12 +02:00
StoreGUID ( result , 3 , items ); // item guid collection (character_inventory.item)
break ;
2009-07-12 17:56:46 +08:00
case DTT_PET :
2012-04-09 04:17:12 +02:00
StoreGUID ( result , 0 , pets ); // pet petnumber collection (character_pet.id)
break ;
2009-07-12 17:56:46 +08:00
case DTT_MAIL :
2012-04-09 04:17:12 +02:00
StoreGUID ( result , 0 , mails ); // mail id collection (mail.id)
break ;
2009-07-12 17:56:46 +08:00
case DTT_MAIL_ITEM :
2012-04-09 04:17:12 +02:00
StoreGUID ( result , 1 , items ); // item guid collection (mail_items.item_guid)
break ;
2010-08-02 15:14:47 +02:00
case DTT_CHARACTER :
{
2012-09-08 16:40:36 +01:00
if ( result -> GetFieldCount () <= 64 ) // avoid crashes on next check
2012-08-17 00:24:50 +02:00
{
2013-11-08 10:50:51 +01:00
TC_LOG_FATAL ( "misc" , "PlayerDumpWriter::DumpTable - Trying to access non-existing or wrong positioned field (`deleteInfos_Account`) in `characters` table." );
2012-08-17 00:24:50 +02:00
return false ;
}
2012-04-09 04:17:12 +02:00
2012-09-08 16:40:36 +01:00
if ( result -> Fetch ()[ 64 ]. GetUInt32 ()) // characters.deleteInfos_Account - if filled error
2010-08-02 15:14:47 +02:00
return false ;
break ;
}
2012-04-09 04:17:12 +02:00
default :
break ;
2008-10-02 16:23:55 -05:00
}
2009-10-17 15:51:44 -07:00
2008-10-02 16:23:55 -05:00
dump += CreateDumpString ( tableTo , result );
dump += " \n " ;
}
while ( result -> NextRow ());
}
2015-03-06 04:16:12 +01:00
while ( guids && guidsItr != guids -> end ()); // not set case iterate single time, set case iterate for all guids
2010-08-02 15:14:47 +02:00
return true ;
2008-10-02 16:23:55 -05:00
}
2009-10-17 15:51:44 -07:00
2014-10-24 19:56:17 +02:00
bool PlayerDumpWriter :: GetDump ( ObjectGuid :: LowType guid , std :: string & dump )
2008-10-02 16:23:55 -05:00
{
2014-09-19 03:30:02 +01:00
dump = "IMPORTANT NOTE: THIS DUMPFILE IS MADE FOR USE WITH THE 'PDUMP' COMMAND ONLY - EITHER THROUGH INGAME CHAT OR ON CONSOLE! \n " ;
2010-05-05 13:11:07 +02:00
dump += "IMPORTANT NOTE: DO NOT apply it directly - it will irreversibly DAMAGE and CORRUPT your database! You have been warned! \n\n " ;
2009-10-17 15:51:44 -07:00
2015-03-06 04:16:12 +01:00
for ( uint8 i = 0 ; i < DUMP_TABLE_COUNT ; ++ i )
2010-08-02 15:14:47 +02:00
if ( ! DumpTable ( dump , guid , dumpTables [ i ]. name , dumpTables [ i ]. name , dumpTables [ i ]. type ))
return false ;
2009-10-17 15:51:44 -07:00
2013-03-08 21:41:30 +01:00
/// @todo Add instance/group..
/// @todo Add a dump level option to skip some non-important tables
2009-10-17 15:51:44 -07:00
2010-08-02 15:14:47 +02:00
return true ;
2008-10-02 16:23:55 -05:00
}
2009-10-17 15:51:44 -07:00
2014-10-24 19:56:17 +02:00
DumpReturn PlayerDumpWriter :: WriteDump ( const std :: string & file , ObjectGuid :: LowType guid )
2008-10-02 16:23:55 -05:00
{
2011-10-08 16:56:04 +03:00
if ( sWorld -> getBoolConfig ( CONFIG_PDUMP_NO_PATHS ))
if ( strstr ( file . c_str (), " \\ " ) || strstr ( file . c_str (), "/" ))
return DUMP_FILE_OPEN_ERROR ;
2015-03-06 04:16:12 +01:00
2011-10-08 16:56:04 +03:00
if ( sWorld -> getBoolConfig ( CONFIG_PDUMP_NO_OVERWRITE ))
if ( FILE * f = fopen ( file . c_str (), "r" ))
{
fclose ( f );
return DUMP_FILE_OPEN_ERROR ;
}
2015-03-06 04:16:12 +01:00
2011-09-15 14:08:17 +02:00
FILE * fout = fopen ( file . c_str (), "w" );
2008-10-26 13:32:42 -05:00
if ( ! fout )
2008-11-20 16:16:57 -06:00
return DUMP_FILE_OPEN_ERROR ;
2009-10-17 15:51:44 -07:00
2010-08-02 15:14:47 +02:00
DumpReturn ret = DUMP_SUCCESS ;
std :: string dump ;
if ( ! GetDump ( guid , dump ))
ret = DUMP_CHARACTER_DELETED ;
2009-10-17 15:51:44 -07:00
2010-08-02 15:14:47 +02:00
fprintf ( fout , "%s \n " , dump . c_str ());
2008-10-02 16:23:55 -05:00
fclose ( fout );
2010-08-02 15:14:47 +02:00
return ret ;
2008-10-02 16:23:55 -05:00
}
2009-10-17 15:51:44 -07:00
2008-10-02 16:23:55 -05:00
// Reading - High-level functions
2010-08-21 03:19:25 +02:00
#define ROLLBACK(DR) {fclose(fin); return (DR);}
2009-10-17 15:51:44 -07:00
2015-03-06 04:16:12 +01:00
void fixNULLfields ( std :: string & line )
2010-08-02 15:14:47 +02:00
{
2015-03-06 04:16:12 +01:00
static std :: string const nullString ( "'NULL'" );
2010-08-02 15:14:47 +02:00
size_t pos = line . find ( nullString );
while ( pos != std :: string :: npos )
{
line . replace ( pos , nullString . length (), "NULL" );
pos = line . find ( nullString );
}
}
2014-10-24 19:56:17 +02:00
DumpReturn PlayerDumpReader :: LoadDump ( std :: string const & file , uint32 account , std :: string name , ObjectGuid :: LowType guid )
2008-10-02 16:23:55 -05:00
{
2011-09-08 10:49:58 +02:00
uint32 charcount = AccountMgr :: GetCharactersCount ( account );
2014-11-03 19:11:14 +01:00
if ( charcount >= sWorld -> getIntConfig ( CONFIG_CHARACTERS_PER_REALM ))
2010-07-21 12:13:23 -06:00
return DUMP_TOO_MANY_CHARS ;
2009-10-17 15:51:44 -07:00
2011-09-15 14:08:17 +02:00
FILE * fin = fopen ( file . c_str (), "r" );
2009-07-10 08:25:33 +08:00
if ( ! fin )
2008-11-20 16:16:57 -06:00
return DUMP_FILE_OPEN_ERROR ;
2009-10-17 15:51:44 -07:00
2015-03-06 04:16:12 +01:00
char newguid [ 20 ], chraccount [ 20 ];
2009-10-17 15:51:44 -07:00
2008-10-02 16:23:55 -05:00
// make sure the same guid doesn't already exist and is safe to use
bool incHighest = true ;
2015-02-25 00:13:14 +01:00
if ( guid && guid < sObjectMgr -> GetGenerator < HighGuid :: Player > (). GetNextAfterMaxUsed ())
2008-10-02 16:23:55 -05:00
{
2012-03-24 01:25:08 +01:00
PreparedStatement * stmt = CharacterDatabase . GetPreparedStatement ( CHAR_SEL_CHECK_GUID );
2014-10-24 19:56:17 +02:00
stmt -> setUInt64 ( 0 , guid );
2012-03-24 01:25:08 +01:00
PreparedQueryResult result = CharacterDatabase . Query ( stmt );
2008-10-02 16:23:55 -05:00
if ( result )
2015-02-25 00:13:14 +01:00
guid = sObjectMgr -> GetGenerator < HighGuid :: Player > (). GetNextAfterMaxUsed (); // use first free if exists
2015-03-06 04:16:12 +01:00
else
incHighest = false ;
2008-10-02 16:23:55 -05:00
}
2008-11-20 16:16:57 -06:00
else
2015-02-25 00:13:14 +01:00
guid = sObjectMgr -> GetGenerator < HighGuid :: Player > (). GetNextAfterMaxUsed ();
2009-10-17 15:51:44 -07:00
2008-10-02 16:23:55 -05:00
// normalize the name if specified and check if it exists
2009-07-10 08:25:33 +08:00
if ( ! normalizePlayerName ( name ))
2014-09-19 03:30:02 +01:00
name . clear ();
2009-10-17 15:51:44 -07:00
2015-08-30 00:17:08 +02:00
if ( ObjectMgr :: CheckPlayerName ( name , sWorld -> GetDefaultDbcLocale (), true ) == CHAR_NAME_SUCCESS )
2008-10-02 16:23:55 -05:00
{
2012-03-24 01:25:08 +01:00
PreparedStatement * stmt = CharacterDatabase . GetPreparedStatement ( CHAR_SEL_CHECK_NAME );
stmt -> setString ( 0 , name );
PreparedQueryResult result = CharacterDatabase . Query ( stmt );
2008-10-02 16:23:55 -05:00
if ( result )
2014-09-19 03:30:02 +01:00
name . clear (); // use the one from the dump
2008-10-02 16:23:55 -05:00
}
2009-07-10 08:25:33 +08:00
else
2014-09-19 03:30:02 +01:00
name . clear ();
2009-10-17 15:51:44 -07:00
2008-10-02 16:23:55 -05:00
// name encoded or empty
2009-10-17 15:51:44 -07:00
2014-10-27 21:56:12 +00:00
snprintf ( newguid , 20 , UI64FMTD , guid );
2011-08-23 18:05:01 +03:00
snprintf ( chraccount , 20 , "%u" , account );
2009-10-17 15:51:44 -07:00
2015-03-06 04:16:12 +01:00
DumpGuidMap items ;
DumpGuidMap mails ;
2008-10-02 16:23:55 -05:00
char buf [ 32000 ] = "" ;
2009-10-17 15:51:44 -07:00
2015-03-06 04:16:12 +01:00
typedef std :: map < uint32 /*old*/ , uint32 /*new*/ > PetIds ;
PetIds petIds ;
2009-10-17 15:51:44 -07:00
2011-11-23 16:57:08 +01:00
uint8 gender = GENDER_NONE ;
uint8 race = RACE_NONE ;
uint8 playerClass = 0 ;
2012-09-09 11:32:14 +02:00
uint8 level = 1 ;
2011-11-23 16:57:08 +01:00
2015-02-25 00:13:14 +01:00
ObjectGuid :: LowType itemLowGuidOffset = sObjectMgr -> GetGenerator < HighGuid :: Item > (). GetNextAfterMaxUsed ();
2015-03-06 04:16:12 +01:00
2010-08-21 03:19:25 +02:00
SQLTransaction trans = CharacterDatabase . BeginTransaction ();
2010-04-07 19:13:19 +02:00
while ( ! feof ( fin ))
2008-10-02 16:23:55 -05:00
{
2010-04-07 19:14:10 +02:00
if ( ! fgets ( buf , 32000 , fin ))
2008-10-02 16:23:55 -05:00
{
2012-03-17 20:13:37 +08:00
if ( feof ( fin ))
break ;
2008-10-26 13:32:42 -05:00
ROLLBACK ( DUMP_FILE_BROKEN );
2008-10-02 16:23:55 -05:00
}
2009-10-17 15:51:44 -07:00
2008-10-02 16:23:55 -05:00
std :: string line ; line . assign ( buf );
2009-10-17 15:51:44 -07:00
2008-10-02 16:23:55 -05:00
// skip empty strings
2009-04-19 18:22:25 -05:00
size_t nw_pos = line . find_first_not_of ( " \t\n\r\7 " );
2010-04-07 23:25:02 +02:00
if ( nw_pos == std :: string :: npos )
2008-10-02 16:23:55 -05:00
continue ;
2009-10-17 15:51:44 -07:00
2010-05-27 04:41:02 +02:00
// skip logfile-side dump start notice, the important notes and dump end notices
2011-04-29 20:47:02 +02:00
if (( line . substr ( nw_pos , 16 ) == "== START DUMP ==" ) ||
( line . substr ( nw_pos , 15 ) == "IMPORTANT NOTE:" ) ||
( line . substr ( nw_pos , 14 ) == "== END DUMP ==" ))
2009-04-19 18:22:25 -05:00
continue ;
2009-10-17 15:51:44 -07:00
2009-04-19 18:22:25 -05:00
// add required_ check
2009-08-28 00:43:34 -04:00
/*
2011-04-29 20:47:02 +02:00
if (line.substr(nw_pos, 41) == "UPDATE character_db_version SET required_")
2009-04-19 18:22:25 -05:00
{
2010-04-07 19:14:10 +02:00
if (!CharacterDatabase.Execute(line.c_str()))
2009-04-19 18:22:25 -05:00
ROLLBACK(DUMP_FILE_BROKEN);
2009-10-17 15:51:44 -07:00
2009-04-19 18:22:25 -05:00
continue;
}
2009-08-28 00:43:34 -04:00
*/
2009-10-17 15:51:44 -07:00
2008-10-02 16:23:55 -05:00
// determine table name and load type
2015-03-06 04:16:12 +01:00
std :: string tn = GetTableName ( line );
2010-04-07 19:14:10 +02:00
if ( tn . empty ())
2008-10-02 16:23:55 -05:00
{
2013-11-08 10:50:51 +01:00
TC_LOG_ERROR ( "misc" , "LoadPlayerDump: Can't extract table name from line: '%s'!" , line . c_str ());
2008-10-26 13:32:42 -05:00
ROLLBACK ( DUMP_FILE_BROKEN );
2008-10-02 16:23:55 -05:00
}
2009-10-17 15:51:44 -07:00
2010-08-21 23:08:54 +02:00
DumpTableType type = DumpTableType ( 0 );
2008-10-02 16:23:55 -05:00
uint8 i ;
2009-10-17 16:20:24 -07:00
for ( i = 0 ; i < DUMP_TABLE_COUNT ; ++ i )
2008-10-02 16:23:55 -05:00
{
if ( tn == dumpTables [ i ]. name )
{
type = dumpTables [ i ]. type ;
break ;
}
}
2009-10-17 15:51:44 -07:00
2008-10-02 16:23:55 -05:00
if ( i == DUMP_TABLE_COUNT )
{
2013-11-08 10:50:51 +01:00
TC_LOG_ERROR ( "misc" , "LoadPlayerDump: Unknown table: '%s'!" , tn . c_str ());
2008-10-26 13:32:42 -05:00
ROLLBACK ( DUMP_FILE_BROKEN );
2008-10-02 16:23:55 -05:00
}
2009-10-17 15:51:44 -07:00
2008-10-02 16:23:55 -05:00
// change the data to server values
2011-09-29 12:43:05 +02:00
switch ( type )
2008-10-02 16:23:55 -05:00
{
2010-05-05 13:11:07 +02:00
case DTT_CHARACTER :
2008-10-02 16:23:55 -05:00
{
2015-03-06 04:16:12 +01:00
if ( ! ChangeNth ( line , 1 , newguid )) // characters.guid update
2008-11-20 16:16:57 -06:00
ROLLBACK ( DUMP_FILE_BROKEN );
2009-10-17 15:51:44 -07:00
2015-03-06 04:16:12 +01:00
if ( ! ChangeNth ( line , 2 , chraccount )) // characters.account update
2008-11-20 16:16:57 -06:00
ROLLBACK ( DUMP_FILE_BROKEN );
2010-05-05 13:11:07 +02:00
2015-03-06 04:16:12 +01:00
race = uint8 ( atoul ( GetNth ( line , 4 ). c_str ()));
playerClass = uint8 ( atoul ( GetNth ( line , 5 ). c_str ()));
gender = uint8 ( atoul ( GetNth ( line , 6 ). c_str ()));
level = uint8 ( atoul ( GetNth ( line , 7 ). c_str ()));
2014-09-19 03:30:02 +01:00
if ( name . empty ())
2008-10-02 16:23:55 -05:00
{
// check if the original name already exists
2015-03-06 04:16:12 +01:00
name = GetNth ( line , 3 );
2009-10-17 15:51:44 -07:00
2012-03-24 01:25:08 +01:00
PreparedStatement * stmt = CharacterDatabase . GetPreparedStatement ( CHAR_SEL_CHECK_NAME );
stmt -> setString ( 0 , name );
PreparedQueryResult result = CharacterDatabase . Query ( stmt );
2008-10-02 16:23:55 -05:00
if ( result )
2015-03-06 04:16:12 +01:00
if ( ! ChangeNth ( line , 41 , "1" )) // characters.at_login set to "rename on login"
2008-11-20 16:16:57 -06:00
ROLLBACK ( DUMP_FILE_BROKEN );
2008-10-02 16:23:55 -05:00
}
2015-03-06 04:16:12 +01:00
else if ( ! ChangeNth ( line , 3 , name . c_str ())) // characters.name
2008-11-20 16:16:57 -06:00
ROLLBACK ( DUMP_FILE_BROKEN );
2009-10-17 15:51:44 -07:00
2010-08-02 15:14:47 +02:00
const char null [ 5 ] = "NULL" ;
2015-03-06 04:16:12 +01:00
if ( ! ChangeNth ( line , 65 , null )) // characters.deleteInfos_Account
2010-08-02 15:14:47 +02:00
ROLLBACK ( DUMP_FILE_BROKEN );
2015-03-06 04:16:12 +01:00
if ( ! ChangeNth ( line , 66 , null )) // characters.deleteInfos_Name
2010-08-02 15:14:47 +02:00
ROLLBACK ( DUMP_FILE_BROKEN );
2015-03-06 04:16:12 +01:00
if ( ! ChangeNth ( line , 67 , null )) // characters.deleteDate
2010-08-02 15:14:47 +02:00
ROLLBACK ( DUMP_FILE_BROKEN );
2008-10-02 16:23:55 -05:00
break ;
}
2010-08-24 11:08:12 +02:00
case DTT_CHAR_TABLE :
2015-03-06 04:16:12 +01:00
case DTT_CURRENCY :
2010-08-24 11:08:12 +02:00
{
2015-03-06 04:16:12 +01:00
if ( ! ChangeNth ( line , 1 , newguid )) // character_*.guid update
2010-08-24 11:08:12 +02:00
ROLLBACK ( DUMP_FILE_BROKEN );
break ;
}
case DTT_EQSET_TABLE :
{
2015-03-06 04:16:12 +01:00
if ( ! ChangeNth ( line , 1 , newguid ))
2010-08-24 11:08:12 +02:00
ROLLBACK ( DUMP_FILE_BROKEN ); // character_equipmentsets.guid
2010-08-24 11:40:40 +02:00
char newSetGuid [ 24 ];
2010-12-22 21:25:23 +01:00
snprintf ( newSetGuid , 24 , UI64FMTD , sObjectMgr -> GenerateEquipmentSetGuid ());
2015-03-06 04:16:12 +01:00
if ( ! ChangeNth ( line , 2 , newSetGuid ))
2010-08-24 11:08:12 +02:00
ROLLBACK ( DUMP_FILE_BROKEN ); // character_equipmentsets.setguid
2015-03-06 04:16:12 +01:00
for ( uint8 slot = 0 ; slot < EQUIPMENT_SLOT_END ; ++ slot )
if ( ! ChangeGuid ( line , 7 + slot , items , itemLowGuidOffset , true ))
ROLLBACK ( DUMP_FILE_BROKEN ); // character_equipmentsets.item
2010-08-24 11:08:12 +02:00
break ;
}
2010-05-05 13:11:07 +02:00
case DTT_INVENTORY :
2008-10-02 16:23:55 -05:00
{
2015-03-06 04:16:12 +01:00
if ( ! ChangeNth ( line , 1 , newguid )) // character_inventory.guid update
2008-11-20 16:16:57 -06:00
ROLLBACK ( DUMP_FILE_BROKEN );
2009-10-17 15:51:44 -07:00
2015-03-06 04:16:12 +01:00
if ( ! ChangeGuid ( line , 2 , items , itemLowGuidOffset , true ))
2010-05-05 13:11:07 +02:00
ROLLBACK ( DUMP_FILE_BROKEN ); // character_inventory.bag update
2015-03-06 04:16:12 +01:00
if ( ! ChangeGuid ( line , 4 , items , itemLowGuidOffset ))
2010-05-05 13:11:07 +02:00
ROLLBACK ( DUMP_FILE_BROKEN ); // character_inventory.item update
2008-10-02 16:23:55 -05:00
break ;
}
2010-08-24 11:08:12 +02:00
case DTT_MAIL : // mail
{
2015-03-06 04:16:12 +01:00
if ( ! ChangeGuid ( line , 1 , mails , sObjectMgr -> _mailId ))
2010-08-24 11:08:12 +02:00
ROLLBACK ( DUMP_FILE_BROKEN ); // mail.id update
2015-03-06 04:16:12 +01:00
if ( ! ChangeNth ( line , 6 , newguid )) // mail.receiver update
2010-08-24 11:08:12 +02:00
ROLLBACK ( DUMP_FILE_BROKEN );
break ;
}
case DTT_MAIL_ITEM : // mail_items
{
2015-03-06 04:16:12 +01:00
if ( ! ChangeGuid ( line , 1 , mails , sObjectMgr -> _mailId ))
2010-08-24 11:08:12 +02:00
ROLLBACK ( DUMP_FILE_BROKEN ); // mail_items.id
2015-03-06 04:16:12 +01:00
if ( ! ChangeGuid ( line , 2 , items , itemLowGuidOffset ))
2010-08-24 11:08:12 +02:00
ROLLBACK ( DUMP_FILE_BROKEN ); // mail_items.item_guid
2015-03-06 04:16:12 +01:00
if ( ! ChangeNth ( line , 3 , newguid )) // mail_items.receiver
2010-08-24 11:08:12 +02:00
ROLLBACK ( DUMP_FILE_BROKEN );
break ;
}
2010-05-05 13:11:07 +02:00
case DTT_ITEM :
2008-10-02 16:23:55 -05:00
{
// item, owner, data field:item, owner guid
2015-03-06 04:16:12 +01:00
if ( ! ChangeGuid ( line , 1 , items , itemLowGuidOffset ))
2010-05-05 13:11:07 +02:00
ROLLBACK ( DUMP_FILE_BROKEN ); // item_instance.guid update
2015-03-06 04:16:12 +01:00
if ( ! ChangeNth ( line , 3 , newguid )) // item_instance.owner_guid update
2008-11-20 16:16:57 -06:00
ROLLBACK ( DUMP_FILE_BROKEN );
2008-10-02 16:23:55 -05:00
break ;
}
2010-05-05 13:11:07 +02:00
case DTT_ITEM_GIFT :
2008-10-02 16:23:55 -05:00
{
2015-03-06 04:16:12 +01:00
if ( ! ChangeNth ( line , 1 , newguid )) // character_gifts.guid update
2008-11-20 16:16:57 -06:00
ROLLBACK ( DUMP_FILE_BROKEN );
2015-03-06 04:16:12 +01:00
if ( ! ChangeGuid ( line , 2 , items , itemLowGuidOffset ))
2010-05-05 13:11:07 +02:00
ROLLBACK ( DUMP_FILE_BROKEN ); // character_gifts.item_guid update
2008-10-02 16:23:55 -05:00
break ;
}
2010-05-05 13:11:07 +02:00
case DTT_PET :
2008-10-02 16:23:55 -05:00
{
2015-03-06 04:16:12 +01:00
// store a map of old pet id to new inserted pet id for use by DTT_PET_TABLE tables
std :: string petIdStr = GetNth ( line , 1 );
2009-10-17 15:51:44 -07:00
2015-03-06 04:16:12 +01:00
uint32 currentPetId = atoul ( petIdStr . c_str ());
2009-10-17 15:51:44 -07:00
2015-03-06 04:16:12 +01:00
PetIds :: const_iterator petIdsItr = petIds . find ( currentPetId );
if ( petIdsItr != petIds . end ()) // duplicate pets
ROLLBACK ( DUMP_FILE_BROKEN );
uint32 newPetId = sObjectMgr -> GeneratePetNumber ();
petIds [ currentPetId ] = newPetId ;
2009-10-17 15:51:44 -07:00
2015-03-06 04:16:12 +01:00
if ( ! ChangeNth ( line , 1 , std :: to_string ( newPetId ). c_str ())) // character_pet.id update
2008-11-20 16:16:57 -06:00
ROLLBACK ( DUMP_FILE_BROKEN );
2015-03-06 04:16:12 +01:00
if ( ! ChangeNth ( line , 3 , newguid )) // character_pet.owner update
2008-11-20 16:16:57 -06:00
ROLLBACK ( DUMP_FILE_BROKEN );
2009-10-17 15:51:44 -07:00
2008-10-02 16:23:55 -05:00
break ;
}
2010-05-05 13:11:07 +02:00
case DTT_PET_TABLE : // pet_aura, pet_spell, pet_spell_cooldown
2008-10-02 16:23:55 -05:00
{
2015-03-06 04:16:12 +01:00
std :: string petIdStr = GetNth ( line , 1 );
2009-10-17 15:51:44 -07:00
2008-10-02 16:23:55 -05:00
// lookup currpetid and match to new inserted pet id
2015-03-06 04:16:12 +01:00
PetIds :: const_iterator petIdsItr = petIds . find ( atoul ( petIdStr . c_str ()));
if ( petIdsItr == petIds . end ()) // couldn't find new inserted id
2008-11-20 16:16:57 -06:00
ROLLBACK ( DUMP_FILE_BROKEN );
2009-10-17 15:51:44 -07:00
2015-03-06 04:16:12 +01:00
if ( ! ChangeNth ( line , 1 , std :: to_string ( petIdsItr -> second ). c_str ()))
2008-11-20 16:16:57 -06:00
ROLLBACK ( DUMP_FILE_BROKEN );
2009-10-17 15:51:44 -07:00
2008-10-02 16:23:55 -05:00
break ;
}
default :
2013-11-08 10:50:51 +01:00
TC_LOG_ERROR ( "misc" , "Unknown dump table type: %u" , type );
2008-10-02 16:23:55 -05:00
break ;
}
2009-10-17 15:51:44 -07:00
2010-08-02 15:14:47 +02:00
fixNULLfields ( line );
2010-08-21 03:19:25 +02:00
trans -> Append ( line . c_str ());
2008-10-02 16:23:55 -05:00
}
2009-10-17 15:51:44 -07:00
2010-08-21 03:19:25 +02:00
CharacterDatabase . CommitTransaction ( trans );
2009-10-17 15:51:44 -07:00
2011-11-16 10:31:38 +01:00
// in case of name conflict player has to rename at login anyway
2015-10-05 15:06:27 +02:00
sWorld -> AddCharacterInfo ( ObjectGuid :: Create < HighGuid :: Player > ( guid ), account , name , gender , race , playerClass , level , false );
2011-11-16 10:31:38 +01:00
2015-02-25 00:13:14 +01:00
sObjectMgr -> GetGenerator < HighGuid :: Item > (). Set ( sObjectMgr -> GetGenerator < HighGuid :: Item > (). GetNextAfterMaxUsed () + items . size ());
2012-02-12 20:10:09 -05:00
sObjectMgr -> _mailId += mails . size ();
2009-10-17 15:51:44 -07:00
2010-04-07 19:14:10 +02:00
if ( incHighest )
2015-02-25 00:13:14 +01:00
sObjectMgr -> GetGenerator < HighGuid :: Player > (). Generate ();
2009-10-17 15:51:44 -07:00
2008-10-02 16:23:55 -05:00
fclose ( fin );
2009-10-17 15:51:44 -07:00
2008-10-26 13:32:42 -05:00
return DUMP_SUCCESS ;
2008-10-02 16:23:55 -05:00
}