2010-10-07 15:35:36 +02:00
/*
2020-01-02 06:44:10 +01:00
* This file is part of the TrinityCore Project. See AUTHORS file for Copyright information
2010-10-07 15:35:36 +02:00
*
* 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 "PlayerDump.h"
2017-05-18 23:52:58 +02:00
#include "AccountMgr.h"
2017-02-05 23:14:19 +01:00
#include "CharacterCache.h"
2017-05-18 23:52:58 +02:00
#include "Common.h"
2010-06-08 17:01:03 -06:00
#include "DatabaseEnv.h"
2017-05-18 23:52:58 +02:00
#include "Log.h"
2008-10-02 16:23:55 -05:00
#include "ObjectMgr.h"
2015-03-06 04:16:12 +01:00
#include "Player.h"
2012-11-20 14:54:33 +01:00
#include "World.h"
2017-05-26 12:06:38 -03:00
#include <boost/algorithm/string/find.hpp>
2020-09-17 21:49:52 +02:00
#include <fstream>
2017-05-18 23:52:58 +02:00
#include <sstream>
2009-10-17 15:51:44 -07:00
2017-05-26 12:06:38 -03:00
// static data
enum GuidType : uint8
{
// 32 bit long guids
GUID_TYPE_ACCOUNT ,
GUID_TYPE_MAIL ,
// 64 bit long guids
GUID_TYPE_CHAR ,
GUID_TYPE_EQUIPMENT_SET ,
GUID_TYPE_ITEM ,
GUID_TYPE_PET ,
// special types
GUID_TYPE_NULL // set to null
};
// for RAII
struct FileCloser
{
void operator ()( FILE * f ) const
{
if ( f )
fclose ( f );
}
};
typedef std :: unique_ptr < FILE , FileCloser > FileHandle ;
inline FileHandle GetFileHandle ( char const * path , char const * mode )
{
return FileHandle ( fopen ( path , mode ), FileCloser ());
}
struct BaseTable
{
char const * TableName ;
char const * PrimaryKey ;
char const * PlayerGuid ;
GuidType StoredType ;
};
BaseTable const BaseTables [] =
{
{ "character_pet" , "id" , "owner" , GUID_TYPE_PET },
{ "mail" , "id" , "receiver" , GUID_TYPE_MAIL },
{ "item_instance" , "guid" , "owner_guid" , GUID_TYPE_ITEM },
{ "character_equipmentsets" , "setguid" , "guid" , GUID_TYPE_EQUIPMENT_SET },
{ "character_transmog_outfits" , "setguid" , "guid" , GUID_TYPE_EQUIPMENT_SET }
};
2008-10-02 16:23:55 -05:00
struct DumpTable
{
2017-05-26 12:06:38 -03:00
char const * Name ;
DumpTableType Type ;
2008-10-02 16:23:55 -05:00
};
2009-10-17 15:51:44 -07:00
2017-05-26 12:06:38 -03:00
DumpTable const DumpTables [] =
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 },
2017-05-26 12:06:38 -03:00
{ "character_aura" , DTT_CHAR_TABLE },
{ "character_aura_effect" , DTT_CHAR_TABLE },
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 },
2017-05-26 12:06:38 -03:00
{ "character_favorite_auctions" , DTT_CHAR_TABLE },
2016-09-13 04:16:05 +02:00
{ "character_fishingsteps" , DTT_CHAR_TABLE },
2018-02-25 16:09:01 +01:00
{ "character_garrison" , DTT_CHAR_TABLE },
{ "character_garrison_blueprints" , DTT_CHAR_TABLE },
{ "character_garrison_buildings" , DTT_CHAR_TABLE },
/// @todo: character_garrison_follower_abilities
/// @todo: character_garrison_followers
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 },
2017-05-26 12:06:38 -03:00
{ "character_pvp_talent" , DTT_CHAR_TABLE },
2010-05-05 13:11:07 +02:00
{ "character_queststatus" , DTT_CHAR_TABLE },
2016-08-16 02:18:02 -03:00
{ "character_queststatus_daily" , DTT_CHAR_TABLE },
{ "character_queststatus_monthly" , DTT_CHAR_TABLE },
2018-02-25 16:09:01 +01:00
{ "character_queststatus_objectives" , DTT_CHAR_TABLE },
{ "character_queststatus_objectives_criteria" , DTT_CHAR_TABLE },
{ "character_queststatus_objectives_criteria_progress" , DTT_CHAR_TABLE },
2010-12-26 04:16:18 +01:00
{ "character_queststatus_rewarded" , DTT_CHAR_TABLE },
2017-05-26 12:06:38 -03:00
{ "character_queststatus_seasonal" , DTT_CHAR_TABLE },
{ "character_queststatus_weekly" , 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 },
2018-02-25 16:09:01 +01:00
{ "character_transmog_outfits" , DTT_CHAR_TRANSMOG },
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
2017-05-26 12:06:38 -03:00
{ "pet_aura" , DTT_PET_TABLE }, // must be after character_pet
{ "pet_aura_effect" , DTT_PET_TABLE }, // must be after character_pet
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
2018-02-25 16:09:01 +01:00
{ "character_equipmentsets" , DTT_EQSET_TABLE }, // must be after item_instance
2014-02-13 23:16:49 +01:00
{ "character_gifts" , DTT_ITEM_GIFT }, // must be after item_instance
2018-02-25 16:09:01 +01:00
{ "item_instance_artifact" , DTT_ITEM_TABLE }, // must be after item_instance
{ "item_instance_artifact_powers" , DTT_ITEM_TABLE }, // must be after item_instance
2017-05-26 12:06:38 -03:00
{ "item_instance_azerite" , DTT_ITEM_TABLE }, // must be after item_instance
{ "item_instance_azerite_empowered" , DTT_ITEM_TABLE }, // must be after item_instance
{ "item_instance_azerite_milestone_power" , DTT_ITEM_TABLE }, // must be after item_instance
{ "item_instance_azerite_unlocked_essence" , DTT_ITEM_TABLE }, // must be after item_instance
2018-02-25 16:09:01 +01:00
{ "item_instance_gems" , DTT_ITEM_TABLE }, // must be after item_instance
{ "item_instance_modifiers" , DTT_ITEM_TABLE }, // must be after item_instance
{ "item_instance_transmog" , DTT_ITEM_TABLE }, // must be after item_instance
2008-10-02 16:23:55 -05:00
};
2018-02-25 16:09:01 +01:00
2017-05-26 12:06:38 -03:00
uint32 const DUMP_TABLE_COUNT = std :: extent < decltype ( DumpTables ) >:: value ;
// helper class to dump sql queries to a printable string
class StringTransaction
2018-02-25 16:09:01 +01:00
{
2017-05-26 12:06:38 -03:00
public :
StringTransaction () : _buf () { }
void Append ( char const * sql )
{
std :: ostringstream oss ;
oss << sql << '\n' ;
_buf += oss . str ();
}
char const * GetBuffer () const
{
return _buf . c_str ();
}
private :
std :: string _buf ;
};
2018-02-25 16:09:01 +01:00
2017-05-26 12:06:38 -03:00
// dynamic data, loaded at startup
struct TableField
2018-02-25 16:09:01 +01:00
{
2017-05-26 12:06:38 -03:00
std :: string FieldName ;
2018-02-25 16:09:01 +01:00
2017-05-26 12:06:38 -03:00
GuidType FieldGuidType = GUID_TYPE_ACCOUNT ;
bool IsDependentField = false ;
bool IsBinaryField = false ;
};
2018-02-25 16:09:01 +01:00
2017-05-26 12:06:38 -03:00
struct TableStruct
{
std :: string TableName ;
std :: string WhereFieldName ;
std :: vector < TableField > TableFields ;
2018-02-25 16:09:01 +01:00
2017-05-26 12:06:38 -03:00
// for lookup
std :: unordered_map < std :: string /*fieldName*/ , int32 /*index*/ > FieldIndices ;
};
std :: vector < TableStruct > CharacterTables ;
2018-02-25 16:09:01 +01:00
2017-05-26 12:06:38 -03:00
inline bool StringsEqualCaseInsensitive ( std :: string const & left , std :: string const & right )
{
std :: string upperLeftString = left ;
2018-02-17 23:26:50 +01:00
bool leftResult = Utf8ToUpperOnlyLatin ( upperLeftString );
ASSERT ( leftResult );
2017-05-26 12:06:38 -03:00
std :: string upperRightString = right ;
2018-02-17 23:26:50 +01:00
bool rightResult = Utf8ToUpperOnlyLatin ( upperRightString );
ASSERT ( rightResult );
2017-05-26 12:06:38 -03:00
return upperLeftString == upperRightString ;
2018-02-25 16:09:01 +01:00
}
2009-10-17 15:51:44 -07:00
2017-05-26 12:06:38 -03:00
inline auto FindColumnByName ( TableStruct & tableStruct , std :: string const & columnName ) -> decltype ( tableStruct . TableFields . begin ())
2018-02-25 16:09:01 +01:00
{
2017-05-26 12:06:38 -03:00
return std :: find_if ( tableStruct . TableFields . begin (), tableStruct . TableFields . end (), [ columnName ]( TableField const & tableField ) -> bool
{
return StringsEqualCaseInsensitive ( tableField . FieldName , columnName );
});
}
inline int32 GetColumnIndexByName ( TableStruct const & tableStruct , std :: string const & columnName )
{
auto itr = tableStruct . FieldIndices . find ( columnName );
if ( itr == tableStruct . FieldIndices . end ())
return - 1 ;
2018-02-25 16:09:01 +01:00
return itr -> second ;
}
2017-05-26 12:06:38 -03:00
inline void MarkDependentColumn ( TableStruct & tableStruct , std :: string const & columnName , GuidType dependentType )
2008-10-02 16:23:55 -05:00
{
2017-05-26 12:06:38 -03:00
auto itr = FindColumnByName ( tableStruct , columnName );
if ( itr == tableStruct . TableFields . end ())
{
2023-01-08 21:16:53 +01:00
TC_LOG_FATAL ( "server.loading" , "Column `{}` declared in table `{}` marked as dependent but doesn't exist, PlayerDump will not work properly, please update table definitions" ,
columnName , tableStruct . TableName );
2017-05-26 12:06:38 -03:00
ABORT ();
return ;
}
2015-03-06 04:16:12 +01:00
2017-05-26 12:06:38 -03:00
if ( itr -> IsDependentField )
{
2023-01-08 21:16:53 +01:00
TC_LOG_FATAL ( "server.loading" , "Attempt to mark column `{}` in table `{}` as dependent column but already marked! please check your code." ,
columnName , tableStruct . TableName );
2017-05-26 12:06:38 -03:00
ABORT ();
return ;
}
2015-03-06 04:16:12 +01:00
2017-05-26 12:06:38 -03:00
itr -> IsDependentField = true ;
itr -> FieldGuidType = dependentType ;
}
2009-10-17 15:51:44 -07:00
2017-05-26 12:06:38 -03:00
inline void MarkWhereField ( TableStruct & tableStruct , std :: string const & whereField )
{
ASSERT ( tableStruct . WhereFieldName . empty ());
2009-10-17 15:51:44 -07:00
2017-05-26 12:06:38 -03:00
auto whereFieldItr = FindColumnByName ( tableStruct , whereField );
if ( whereFieldItr == tableStruct . TableFields . end ())
{
2023-01-08 21:16:53 +01:00
TC_LOG_FATAL ( "server.loading" , "Column name `{}` set as 'WHERE' column for table `{}` doesn't exist. PlayerDump won't work properly" ,
whereField , tableStruct . TableName );
2017-05-26 12:06:38 -03:00
ABORT ();
return ;
}
tableStruct . WhereFieldName = whereField ;
2008-10-02 16:23:55 -05:00
}
2009-10-17 15:51:44 -07:00
2017-05-26 12:06:38 -03:00
inline void AssertBaseTable ( BaseTable const & baseTable )
2008-10-02 16:23:55 -05:00
{
2017-05-26 12:06:38 -03:00
auto itr = std :: find_if ( CharacterTables . begin (), CharacterTables . end (), [ baseTable ]( TableStruct const & tableStruct ) -> bool
{
return StringsEqualCaseInsensitive ( tableStruct . TableName , baseTable . TableName );
});
2009-10-17 15:51:44 -07:00
2017-05-26 12:06:38 -03:00
ASSERT ( itr != CharacterTables . end ());
auto columnItr = FindColumnByName ( * itr , baseTable . PrimaryKey );
ASSERT ( columnItr != itr -> TableFields . end ());
columnItr = FindColumnByName ( * itr , baseTable . PlayerGuid );
ASSERT ( columnItr != itr -> TableFields . end ());
2008-10-02 16:23:55 -05:00
}
2009-10-17 15:51:44 -07:00
2017-05-26 12:06:38 -03:00
void PlayerDump :: InitializeTables ()
2008-10-02 16:23:55 -05:00
{
2017-05-26 12:06:38 -03:00
uint32 oldMSTime = getMSTime ();
2009-10-17 15:51:44 -07:00
2017-05-26 12:06:38 -03:00
for ( DumpTable const & dumpTable : DumpTables )
2008-10-02 16:23:55 -05:00
{
2017-05-26 12:06:38 -03:00
TableStruct t ;
t . TableName = dumpTable . Name ;
2023-01-08 21:16:53 +01:00
QueryResult result = CharacterDatabase . PQuery ( "DESC {}" , dumpTable . Name );
2017-05-26 12:06:38 -03:00
// prepared statement is correct (checked at startup) so table must exist
ASSERT ( result );
int32 i = 0 ;
do
{
std :: string columnName = ( * result )[ 0 ]. GetString ();
std :: string typeName = ( * result )[ 1 ]. GetString ();
t . FieldIndices . emplace ( columnName , i ++ );
TableField f ;
f . FieldName = columnName ;
f . IsBinaryField = ! boost :: ifind_first ( typeName , "binary" ). empty () || ! boost :: ifind_first ( typeName , "blob" ). empty ();
2018-02-17 23:26:50 +01:00
bool toUpperResult = Utf8ToUpperOnlyLatin ( columnName );
ASSERT ( toUpperResult );
2009-10-17 15:51:44 -07:00
2017-05-26 12:06:38 -03:00
t . TableFields . emplace_back ( std :: move ( f ));
} while ( result -> NextRow ());
switch ( dumpTable . Type )
{
case DTT_CHARACTER :
MarkWhereField ( t , "guid" );
MarkDependentColumn ( t , "guid" , GUID_TYPE_CHAR );
MarkDependentColumn ( t , "account" , GUID_TYPE_ACCOUNT );
MarkDependentColumn ( t , "deleteInfos_Account" , GUID_TYPE_NULL );
MarkDependentColumn ( t , "deleteInfos_Name" , GUID_TYPE_NULL );
MarkDependentColumn ( t , "deleteDate" , GUID_TYPE_NULL );
break ;
case DTT_CHAR_TABLE :
MarkWhereField ( t , "guid" );
MarkDependentColumn ( t , "guid" , GUID_TYPE_CHAR );
break ;
case DTT_CURRENCY :
MarkWhereField ( t , "CharacterGuid" );
MarkDependentColumn ( t , "CharacterGuid" , GUID_TYPE_CHAR );
break ;
case DTT_EQSET_TABLE :
MarkWhereField ( t , "guid" );
MarkDependentColumn ( t , "guid" , GUID_TYPE_CHAR );
MarkDependentColumn ( t , "setguid" , GUID_TYPE_EQUIPMENT_SET );
// item0 - item18
for ( uint32 j = 0 ; j < EQUIPMENT_SLOT_END ; ++ j )
{
2023-01-08 21:16:53 +01:00
std :: string itColumn = Trinity :: StringFormat ( "item{}" , j );
2017-05-26 12:06:38 -03:00
MarkDependentColumn ( t , itColumn , GUID_TYPE_ITEM );
}
break ;
case DTT_INVENTORY :
MarkWhereField ( t , "guid" );
MarkDependentColumn ( t , "guid" , GUID_TYPE_CHAR );
MarkDependentColumn ( t , "bag" , GUID_TYPE_ITEM );
MarkDependentColumn ( t , "item" , GUID_TYPE_ITEM );
break ;
case DTT_CHAR_TRANSMOG :
MarkWhereField ( t , "guid" );
MarkDependentColumn ( t , "guid" , GUID_TYPE_CHAR );
MarkDependentColumn ( t , "setguid" , GUID_TYPE_EQUIPMENT_SET );
break ;
case DTT_MAIL :
MarkWhereField ( t , "receiver" );
MarkDependentColumn ( t , "id" , GUID_TYPE_MAIL );
MarkDependentColumn ( t , "receiver" , GUID_TYPE_CHAR );
break ;
case DTT_MAIL_ITEM :
MarkWhereField ( t , "mail_id" );
MarkDependentColumn ( t , "mail_id" , GUID_TYPE_MAIL );
MarkDependentColumn ( t , "item_guid" , GUID_TYPE_ITEM );
MarkDependentColumn ( t , "receiver" , GUID_TYPE_CHAR );
break ;
case DTT_ITEM :
MarkWhereField ( t , "guid" );
MarkDependentColumn ( t , "guid" , GUID_TYPE_ITEM );
MarkDependentColumn ( t , "owner_guid" , GUID_TYPE_CHAR );
break ;
case DTT_ITEM_GIFT :
MarkWhereField ( t , "item_guid" );
MarkDependentColumn ( t , "guid" , GUID_TYPE_CHAR );
MarkDependentColumn ( t , "item_guid" , GUID_TYPE_ITEM );
break ;
case DTT_ITEM_TABLE :
MarkWhereField ( t , "itemGuid" );
MarkDependentColumn ( t , "itemGuid" , GUID_TYPE_ITEM );
break ;
case DTT_PET :
MarkWhereField ( t , "owner" );
MarkDependentColumn ( t , "id" , GUID_TYPE_PET );
MarkDependentColumn ( t , "owner" , GUID_TYPE_CHAR );
break ;
case DTT_PET_TABLE :
MarkWhereField ( t , "guid" );
MarkDependentColumn ( t , "guid" , GUID_TYPE_PET );
break ;
default :
2023-01-08 21:16:53 +01:00
TC_LOG_FATAL ( "server.loading" , "Wrong dump table type {}, probably added a new table type without updating code" , uint32 ( dumpTable . Type ));
2017-05-26 12:06:38 -03:00
ABORT ();
return ;
}
CharacterTables . emplace_back ( std :: move ( t ));
}
// perform some sanity checks
for ( TableStruct const & tableStruct : CharacterTables )
{
if ( tableStruct . WhereFieldName . empty ())
{
2023-01-08 21:16:53 +01:00
TC_LOG_FATAL ( "server.loading" , "Table `{}` defined in player dump doesn't have a WHERE query field" , tableStruct . TableName );
2017-05-26 12:06:38 -03:00
ABORT ();
}
}
for ( BaseTable const & baseTable : BaseTables )
AssertBaseTable ( baseTable );
ASSERT ( CharacterTables . size () == DUMP_TABLE_COUNT );
2023-01-08 21:16:53 +01:00
TC_LOG_INFO ( "server.loading" , ">> Initialized tables for PlayerDump in {} ms." , GetMSTimeDiffToNow ( oldMSTime ));
2017-05-26 12:06:38 -03:00
}
// Low level functions
inline bool FindColumn ( TableStruct const & ts , std :: string const & str , std :: string const & column , std :: string :: size_type & s , std :: string :: size_type & e )
{
int32 columnIndex = GetColumnIndexByName ( ts , column );
if ( columnIndex == - 1 )
return false ;
// array indices start at 0, compensate
++ columnIndex ;
s = str . find ( "VALUES (" );
if ( s == std :: string :: npos )
return false ;
s += 8 ;
e = s ;
bool isQuoted = str [ s ] == '\'' ;
if ( isQuoted )
2008-10-02 16:23:55 -05:00
{
2017-05-26 12:06:38 -03:00
++ s ;
++ e ;
// find first unescaped quote
2008-10-02 16:23:55 -05:00
do
{
2017-05-26 12:06:38 -03:00
e = str . find ( '\'' , e );
2015-03-06 04:16:12 +01:00
if ( e == std :: string :: npos )
return false ;
2017-05-26 12:06:38 -03:00
if ( str [ e - 1 ] == '\\' )
continue ;
if ( e + 1 < str . length () && str [ e + 1 ] == '\'' )
{
++ e ;
continue ;
}
break ;
} while ( true );
}
else
e = str . find_first_of ( ",)" , e );
for ( int32 i = 1 ; i < columnIndex ; ++ i )
{
// if previous value was quoted, move old e to comma
if ( isQuoted )
++ e ;
// move past ", "
s = e + 2 ;
e = s ;
isQuoted = str [ s ] == '\'' ;
if ( isQuoted )
{
++ s ;
++ e ;
// find first unescaped quote
do
{
e = str . find ( '\'' , e );
if ( e == std :: string :: npos )
return false ;
if ( str [ e - 1 ] == '\\' )
continue ;
if ( e + 1 < str . length () && str [ e + 1 ] == '\'' )
{
++ e ;
continue ;
}
break ;
} while ( str [ e - 1 ] == '\\' );
}
else
e = str . find_first_of ( ",)" , e );
2008-10-02 16:23:55 -05:00
}
2017-05-26 12:06:38 -03:00
2008-10-02 16:23:55 -05:00
return true ;
}
2009-10-17 15:51:44 -07:00
2017-05-26 12:06:38 -03:00
inline std :: string GetTableName ( std :: string const & str )
2008-10-02 16:23:55 -05:00
{
2017-05-26 12:06:38 -03:00
// length of "INSERT INTO `"
2015-03-06 04:16:12 +01:00
static std :: string :: size_type const s = 13 ;
2017-05-12 18:49:51 +02:00
std :: string :: size_type e = str . find ( '`' , s );
2008-10-02 16:23:55 -05:00
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
2017-05-26 12:06:38 -03:00
inline bool ValidateFields ( TableStruct const & ts , std :: string const & str , size_t lineNumber )
2008-10-02 16:23:55 -05:00
{
2017-05-26 12:06:38 -03:00
std :: string :: size_type s = str . find ( "` VALUES (" );
if ( s != std :: string :: npos ) // old dump format (no column names)
return true ;
// new format has insert with columns, need validation else we risk executing an invalid query
s = str . find ( "` (`" );
if ( s == std :: string :: npos )
{
2023-01-08 21:16:53 +01:00
TC_LOG_ERROR ( "misc" , "LoadPlayerDump: (line {}) dump format not recognized." , lineNumber );
2008-10-02 16:23:55 -05:00
return false ;
2017-05-26 12:06:38 -03:00
}
s += 4 ;
2009-10-17 15:51:44 -07:00
2017-05-26 12:06:38 -03:00
std :: string :: size_type valPos = str . find ( "VALUES ('" );
std :: string :: size_type e = str . find ( '`' , s );
if ( e == std :: string :: npos || valPos == std :: string :: npos )
{
2023-01-08 21:16:53 +01:00
TC_LOG_ERROR ( "misc" , "LoadPlayerDump: (line {}) unexpected end of line" , lineNumber );
2017-05-26 12:06:38 -03:00
return false ;
}
2015-03-06 04:16:12 +01:00
2017-05-26 12:06:38 -03:00
do
{
std :: string column = str . substr ( s , e - s );
int32 columnIndex = GetColumnIndexByName ( ts , column );
if ( columnIndex == - 1 )
{
2023-01-08 21:16:53 +01:00
TC_LOG_ERROR ( "misc" , "LoadPlayerDump: (line {}) unknown column name `{}` for table `{}`, aborting due to incompatible DB structure." , lineNumber , column , ts . TableName );
2017-05-26 12:06:38 -03:00
return false ;
}
// length of "`, `"
s = e + 4 ;
e = str . find ( '`' , s );
} while ( e < valPos );
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
2017-05-26 12:06:38 -03:00
inline bool ChangeColumn ( TableStruct const & ts , std :: string & str , std :: string const & column , std :: string const & with , bool allowZero = false )
2008-10-02 16:23:55 -05:00
{
std :: string :: size_type s , e ;
2017-05-26 12:06:38 -03:00
if ( ! FindColumn ( ts , str , column , 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
2017-05-26 12:06:38 -03:00
str . replace ( s , e - s , with );
2008-10-02 16:23:55 -05:00
return true ;
}
2009-10-17 15:51:44 -07:00
2017-05-26 12:06:38 -03:00
inline std :: string GetColumn ( TableStruct const & ts , std :: string & str , std :: string const & column )
2008-10-02 16:23:55 -05:00
{
2017-05-26 12:06:38 -03:00
std :: string :: size_type s , e ;
if ( ! FindColumn ( ts , str , column , s , e ))
return "" ;
return str . substr ( s , e - s );
}
template < typename T , template < class , class , class ... > class MapType , class ... Rest >
inline T RegisterNewGuid ( T oldGuid , MapType < T , T , Rest ... >& guidMap , T guidOffset )
{
auto 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
2017-05-26 12:06:38 -03:00
T newguid = guidOffset + T ( guidMap . size ());
guidMap . emplace ( oldGuid , newguid );
2008-10-02 16:23:55 -05:00
return newguid ;
}
2009-10-17 15:51:44 -07:00
2017-05-26 12:06:38 -03:00
template < typename T , template < class , class , class ... > class MapType , class ... Rest >
inline bool ChangeGuid ( TableStruct const & ts , std :: string & str , std :: string const & column , MapType < T , T , Rest ... >& guidMap , T guidOffset , bool allowZero = false )
2008-10-02 16:23:55 -05:00
{
2017-05-26 12:06:38 -03:00
T oldGuid ( atoull ( GetColumn ( ts , str , column ). c_str ()));
2015-03-06 04:16:12 +01:00
if ( allowZero && ! oldGuid )
2008-10-02 16:23:55 -05:00
return true ; // not an error
2009-10-17 15:51:44 -07:00
2017-05-26 12:06:38 -03:00
std :: string chritem ;
T newGuid = RegisterNewGuid ( oldGuid , guidMap , guidOffset );
chritem = std :: to_string ( newGuid );
2009-10-17 15:51:44 -07:00
2017-05-26 12:06:38 -03:00
return ChangeColumn ( ts , str , column , chritem , allowZero );
2008-10-02 16:23:55 -05:00
}
2009-10-17 15:51:44 -07:00
2017-05-26 12:06:38 -03:00
inline void AppendTableDump ( StringTransaction & trans , TableStruct const & tableStruct , QueryResult result )
2008-10-02 16:23:55 -05:00
{
2017-05-26 12:06:38 -03:00
if ( ! result )
return ;
do
2008-10-02 16:23:55 -05:00
{
2017-05-26 12:06:38 -03:00
std :: ostringstream ss ;
ss << "INSERT INTO `" << tableStruct . TableName << "` (" ;
for ( auto itr = tableStruct . TableFields . begin (); itr != tableStruct . TableFields . end ();)
{
ss << '`' << itr -> FieldName << '`' ;
++ itr ;
2009-10-17 15:51:44 -07:00
2017-05-26 12:06:38 -03:00
if ( itr != tableStruct . TableFields . end ())
ss << ", " ;
}
ss << ") VALUES (" ;
2009-10-17 15:51:44 -07:00
2017-05-26 12:06:38 -03:00
uint32 const fieldSize = uint32 ( tableStruct . TableFields . size ());
Field * fields = result -> Fetch ();
for ( uint32 i = 0 ; i < fieldSize ;)
{
if ( fields [ i ]. IsNull ())
ss << "'NULL'" ;
else
{
if ( ! tableStruct . TableFields [ i ]. IsBinaryField )
{
std :: string s ( fields [ i ]. GetString ());
CharacterDatabase . EscapeString ( s );
ss << '\'' << s << '\'' ;
}
else
{
std :: vector < uint8 > b ( fields [ i ]. GetBinary ());
2022-10-18 12:19:52 +03:00
if ( ! b . empty ())
ss << "0x" << ByteArrayToHexStr ( b );
else
ss << '\'' << '\'' ;
2017-05-26 12:06:38 -03:00
}
}
++ i ;
if ( i != fieldSize )
ss << ", " ;
}
ss << ");" ;
trans . Append ( ss . str (). c_str ());
} while ( result -> NextRow ());
2008-10-02 16:23:55 -05:00
}
2009-10-17 15:51:44 -07:00
2017-05-26 12:06:38 -03:00
inline std :: string GenerateWhereStr ( std :: string const & field , ObjectGuid :: LowType guid )
2008-10-02 16:23:55 -05:00
{
2017-05-26 12:06:38 -03:00
std :: ostringstream whereStr ;
whereStr << field << " = '" << guid << '\'' ;
return whereStr . str ();
2008-10-02 16:23:55 -05:00
}
2009-10-17 15:51:44 -07:00
2017-05-26 12:06:38 -03:00
template < typename T , template < class , class ... > class SetType , class ... Rest >
inline std :: string GenerateWhereStr ( std :: string const & field , SetType < T , Rest ... > const & guidSet )
2008-10-02 16:23:55 -05:00
{
2017-05-26 12:06:38 -03:00
std :: ostringstream whereStr ;
whereStr << field << " IN ('" ;
for ( auto itr = guidSet . begin (); itr != guidSet . end ();)
2008-10-02 16:23:55 -05:00
{
2017-05-26 12:06:38 -03:00
whereStr << * itr ;
2015-03-06 04:16:12 +01:00
++ itr ;
2009-10-17 15:51:44 -07:00
2017-05-26 12:06:38 -03: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
2017-05-26 12:06:38 -03:00
if ( itr != guidSet . end ())
whereStr << "','" ;
2008-10-02 16:23:55 -05:00
}
2017-05-26 12:06:38 -03:00
whereStr << "')" ;
return whereStr . str ();
2008-10-02 16:23:55 -05:00
}
2009-10-17 15:51:44 -07:00
2008-10-02 16:23:55 -05:00
// Writing - High-level functions
2017-05-26 12:06:38 -03:00
void PlayerDumpWriter :: PopulateGuids ( ObjectGuid :: LowType guid )
2008-10-02 16:23:55 -05:00
{
2017-05-26 12:06:38 -03:00
for ( BaseTable const & baseTable : BaseTables )
2008-10-02 16:23:55 -05:00
{
2017-05-26 12:06:38 -03:00
switch ( baseTable . StoredType )
{
case GUID_TYPE_ITEM :
case GUID_TYPE_MAIL :
case GUID_TYPE_PET :
case GUID_TYPE_EQUIPMENT_SET :
break ;
default :
return ;
}
2009-10-17 15:51:44 -07:00
2017-05-26 12:06:38 -03:00
std :: string whereStr = GenerateWhereStr ( baseTable . PlayerGuid , guid );
2023-01-08 21:16:53 +01:00
QueryResult result = CharacterDatabase . PQuery ( "SELECT {} FROM {} WHERE {}" , baseTable . PrimaryKey , baseTable . TableName , whereStr );
2010-04-07 19:14:10 +02:00
if ( ! result )
2017-05-26 12:06:38 -03:00
continue ;
2009-10-17 15:51:44 -07:00
2008-10-02 16:23:55 -05:00
do
{
2017-05-26 12:06:38 -03:00
switch ( baseTable . StoredType )
2008-10-02 16:23:55 -05:00
{
2017-05-26 12:06:38 -03:00
case GUID_TYPE_ITEM :
2018-01-22 23:55:33 -03:00
if ( ObjectGuid :: LowType itemLowGuid = ( * result )[ 0 ]. GetUInt32 ())
_items . insert ( itemLowGuid );
2012-04-09 04:17:12 +02:00
break ;
2017-05-26 12:06:38 -03:00
case GUID_TYPE_MAIL :
2018-01-22 23:55:33 -03:00
if ( uint32 mailLowGuid = ( * result )[ 0 ]. GetUInt32 ())
_mails . insert ( mailLowGuid );
2012-04-09 04:17:12 +02:00
break ;
2017-05-26 12:06:38 -03:00
case GUID_TYPE_PET :
2018-01-22 23:55:33 -03:00
if ( uint32 petLowGuid = ( * result )[ 0 ]. GetUInt32 ())
_pets . insert ( petLowGuid );
2012-04-09 04:17:12 +02:00
break ;
2017-05-26 12:06:38 -03:00
case GUID_TYPE_EQUIPMENT_SET :
if ( uint64 eqSetId = ( * result )[ 0 ]. GetUInt64 ())
_itemSets . insert ( eqSetId );
2010-08-02 15:14:47 +02:00
break ;
2012-04-09 04:17:12 +02:00
default :
break ;
2008-10-02 16:23:55 -05:00
}
2017-05-26 12:06:38 -03:00
} while ( result -> NextRow ());
}
}
2009-10-17 15:51:44 -07:00
2017-05-26 12:06:38 -03:00
bool PlayerDumpWriter :: AppendTable ( StringTransaction & trans , ObjectGuid :: LowType guid , TableStruct const & tableStruct , DumpTable const & dumpTable )
{
std :: string whereStr ;
switch ( dumpTable . Type )
{
case DTT_ITEM :
case DTT_ITEM_GIFT :
case DTT_ITEM_TABLE :
if ( _items . empty ())
return true ;
whereStr = GenerateWhereStr ( tableStruct . WhereFieldName , _items );
break ;
case DTT_PET_TABLE :
if ( _pets . empty ())
return true ;
whereStr = GenerateWhereStr ( tableStruct . WhereFieldName , _pets );
break ;
case DTT_MAIL_ITEM :
if ( _mails . empty ())
return true ;
whereStr = GenerateWhereStr ( tableStruct . WhereFieldName , _mails );
break ;
case DTT_EQSET_TABLE :
case DTT_CHAR_TRANSMOG :
if ( _itemSets . empty ())
return true ;
whereStr = GenerateWhereStr ( tableStruct . WhereFieldName , _itemSets );
break ;
default :
// not set case, get single guid string
whereStr = GenerateWhereStr ( tableStruct . WhereFieldName , guid );
break ;
}
2023-01-08 21:16:53 +01:00
QueryResult result = CharacterDatabase . PQuery ( "SELECT * FROM {} WHERE {}" , dumpTable . Name , whereStr );
2017-05-26 12:06:38 -03:00
switch ( dumpTable . Type )
{
case DTT_CHARACTER :
if ( result )
{
// characters.deleteInfos_Account - if filled error
int32 index = GetColumnIndexByName ( tableStruct , "deleteInfos_Account" );
ASSERT ( index != - 1 ); // checked at startup
if (( * result )[ index ]. GetUInt32 ())
return false ;
}
break ;
default :
break ;
2008-10-02 16:23:55 -05:00
}
2017-05-26 12:06:38 -03:00
AppendTableDump ( trans , tableStruct , result );
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
2017-05-26 12:06:38 -03: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
2017-05-26 12:06:38 -03:00
StringTransaction trans ;
// collect guids
PopulateGuids ( guid );
for ( uint32 i = 0 ; i < DUMP_TABLE_COUNT ; ++ i )
if ( ! AppendTable ( trans , guid , CharacterTables [ i ], DumpTables [ i ]))
2010-08-02 15:14:47 +02:00
return false ;
2009-10-17 15:51:44 -07:00
2017-05-26 12:06:38 -03:00
dump += trans . GetBuffer ();
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
2020-09-17 21:49:52 +02:00
DumpReturn PlayerDumpWriter :: WriteDumpToFile ( std :: string const & 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 ))
2017-02-25 17:07:22 +01:00
if ( strchr ( file . c_str (), '\\' ) || strchr ( file . c_str (), '/' ))
2011-10-08 16:56:04 +03:00
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 ))
2017-05-26 12:06:38 -03:00
{
// check if file exists already
if ( GetFileHandle ( file . c_str (), "r" ))
2011-10-08 16:56:04 +03:00
return DUMP_FILE_OPEN_ERROR ;
2017-05-26 12:06:38 -03:00
}
2015-03-06 04:16:12 +01:00
2017-05-26 12:06:38 -03:00
FileHandle fout = GetFileHandle ( 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
2017-05-26 12:06:38 -03:00
fprintf ( fout . get (), "%s" , dump . c_str ());
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
2020-09-17 21:49:52 +02:00
DumpReturn PlayerDumpWriter :: WriteDumpToString ( std :: string & dump , ObjectGuid :: LowType guid )
{
DumpReturn ret = DUMP_SUCCESS ;
if ( ! GetDump ( guid , dump ))
ret = DUMP_CHARACTER_DELETED ;
return ret ;
}
2008-10-02 16:23:55 -05:00
// Reading - High-level functions
2017-05-26 12:06:38 -03:00
inline void FixNULLfields ( std :: string & line )
2010-08-02 15:14:47 +02:00
{
2017-05-26 12:06:38 -03:00
static std :: string const NullString ( "'NULL'" );
size_t pos = line . find ( NullString );
2010-08-02 15:14:47 +02:00
while ( pos != std :: string :: npos )
{
2017-05-26 12:06:38 -03:00
line . replace ( pos , NullString . length (), "NULL" );
pos = line . find ( NullString );
2010-08-02 15:14:47 +02:00
}
}
2020-09-17 21:49:52 +02:00
DumpReturn PlayerDumpReader :: LoadDump ( std :: istream & input , 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
2017-05-26 12:06:38 -03:00
std :: string newguid , chraccount ;
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
{
2019-07-27 01:00:37 +02:00
CharacterDatabasePreparedStatement * 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
2017-05-26 12:06:38 -03:00
if ( PreparedQueryResult result = CharacterDatabase . Query ( stmt ))
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
{
2019-07-27 01:00:37 +02:00
CharacterDatabasePreparedStatement * stmt = CharacterDatabase . GetPreparedStatement ( CHAR_SEL_CHECK_NAME );
2012-03-24 01:25:08 +01:00
stmt -> setString ( 0 , name );
2017-05-26 12:06:38 -03:00
if ( PreparedQueryResult result = CharacterDatabase . Query ( stmt ))
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
2017-05-26 12:06:38 -03:00
newguid = std :: to_string ( guid );
chraccount = std :: to_string ( account );
std :: map < ObjectGuid :: LowType , ObjectGuid :: LowType > items ;
ObjectGuid :: LowType itemLowGuidOffset = sObjectMgr -> GetGenerator < HighGuid :: Item > (). GetNextAfterMaxUsed ();
2009-10-17 15:51:44 -07:00
2023-01-28 01:53:26 +01:00
std :: map < uint64 , uint64 > mails ;
uint64 mailLowGuidOffset = sObjectMgr -> _mailId ;
2009-10-17 15:51:44 -07:00
2017-05-26 12:06:38 -03:00
std :: map < uint32 , uint32 > petIds ;
uint32 petLowGuidOffset = sObjectMgr -> _hiPetNumber ;
2009-10-17 15:51:44 -07:00
2017-05-26 12:06:38 -03:00
std :: map < uint64 , uint64 > equipmentSetIds ;
uint64 equipmentSetGuidOffset = sObjectMgr -> _equipmentSetGuid ;
2020-09-17 21:49:52 +02:00
std :: string line ;
2009-10-17 15:51:44 -07:00
2011-11-23 16:57:08 +01:00
uint8 gender = GENDER_NONE ;
uint8 race = RACE_NONE ;
2017-05-26 12:06:38 -03:00
uint8 playerClass = CLASS_NONE ;
2012-09-09 11:32:14 +02:00
uint8 level = 1 ;
2011-11-23 16:57:08 +01:00
2017-05-26 12:06:38 -03:00
// for logs
size_t lineNumber = 0 ;
2015-03-06 04:16:12 +01:00
2019-07-27 10:52:33 +02:00
CharacterDatabaseTransaction trans = CharacterDatabase . BeginTransaction ();
2020-09-17 21:49:52 +02:00
while ( std :: getline ( input , line ))
2008-10-02 16:23:55 -05:00
{
2017-05-26 12:06:38 -03:00
++ lineNumber ;
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
2017-05-26 12:06:38 -03:00
// skip the important notes
static std :: string const SkippedLine = "IMPORTANT NOTE:" ;
if ( line . substr ( nw_pos , SkippedLine . size ()) == SkippedLine )
2009-04-19 18:22:25 -05:00
continue ;
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
{
2023-01-08 21:16:53 +01:00
TC_LOG_ERROR ( "misc" , "LoadPlayerDump: (line {}) Can't extract table name!" , lineNumber );
2017-05-26 12:06:38 -03:00
return DUMP_FILE_BROKEN ;
2008-10-02 16:23:55 -05:00
}
2009-10-17 15:51:44 -07:00
2017-05-26 12:06:38 -03:00
DumpTableType type = DTT_CHARACTER ;
uint32 i ;
for ( i = 0 ; i < DUMP_TABLE_COUNT ; ++ i )
2008-10-02 16:23:55 -05:00
{
2017-05-26 12:06:38 -03:00
if ( tn == DumpTables [ i ]. Name )
2008-10-02 16:23:55 -05:00
{
2017-05-26 12:06:38 -03:00
type = DumpTables [ i ]. Type ;
2008-10-02 16:23:55 -05:00
break ;
}
}
2009-10-17 15:51:44 -07:00
2017-05-26 12:06:38 -03:00
if ( i == DUMP_TABLE_COUNT )
2008-10-02 16:23:55 -05:00
{
2023-01-08 21:16:53 +01:00
TC_LOG_ERROR ( "misc" , "LoadPlayerDump: (line {}) Unknown table: `{}`!" , lineNumber , tn );
2017-05-26 12:06:38 -03:00
return DUMP_FILE_BROKEN ;
2008-10-02 16:23:55 -05:00
}
2009-10-17 15:51:44 -07:00
2017-05-26 12:06:38 -03:00
TableStruct const & ts = CharacterTables [ i ];
if ( ! ValidateFields ( ts , line , lineNumber ))
return DUMP_FILE_BROKEN ;
2009-10-17 15:51:44 -07:00
2017-05-26 12:06:38 -03:00
// per field guid offsetting
for ( TableField const & field : ts . TableFields )
{
if ( ! field . IsDependentField )
continue ;
2010-05-05 13:11:07 +02:00
2017-05-26 12:06:38 -03:00
switch ( field . FieldGuidType )
{
case GUID_TYPE_ACCOUNT :
if ( ! ChangeColumn ( ts , line , field . FieldName , chraccount ))
return DUMP_FILE_BROKEN ;
break ;
case GUID_TYPE_CHAR :
if ( ! ChangeColumn ( ts , line , field . FieldName , newguid ))
return DUMP_FILE_BROKEN ;
break ;
case GUID_TYPE_PET :
if ( ! ChangeGuid ( ts , line , field . FieldName , petIds , petLowGuidOffset ))
return DUMP_FILE_BROKEN ;
break ;
case GUID_TYPE_MAIL :
if ( ! ChangeGuid ( ts , line , field . FieldName , mails , mailLowGuidOffset ))
return DUMP_FILE_BROKEN ;
break ;
case GUID_TYPE_ITEM :
if ( ! ChangeGuid ( ts , line , field . FieldName , items , itemLowGuidOffset , true ))
return DUMP_FILE_BROKEN ;
break ;
case GUID_TYPE_EQUIPMENT_SET :
if ( ! ChangeGuid ( ts , line , field . FieldName , equipmentSetIds , equipmentSetGuidOffset ))
return DUMP_FILE_BROKEN ;
break ;
case GUID_TYPE_NULL :
2008-10-02 16:23:55 -05:00
{
2017-05-26 12:06:38 -03:00
static std :: string const NullString ( "NULL" );
if ( ! ChangeColumn ( ts , line , field . FieldName , NullString ))
return DUMP_FILE_BROKEN ;
break ;
2008-10-02 16:23:55 -05:00
}
}
2017-05-26 12:06:38 -03:00
}
2009-10-17 15:51:44 -07:00
2017-05-26 12:06:38 -03:00
// extra modifications for other tables
switch ( type )
{
case DTT_CHARACTER :
2008-10-02 16:23:55 -05:00
{
2017-05-26 12:06:38 -03:00
race = uint8 ( atoul ( GetColumn ( ts , line , "race" ). c_str ()));
playerClass = uint8 ( atoul ( GetColumn ( ts , line , "class" ). c_str ()));
gender = uint8 ( atoul ( GetColumn ( ts , line , "gender" ). c_str ()));
level = uint8 ( atoul ( GetColumn ( ts , line , "level" ). c_str ()));
if ( name . empty ())
{
// generate a temporary name
2023-01-08 21:16:53 +01:00
std :: string guidPart = Trinity :: StringFormat ( "{:X}" , guid );
2017-05-26 12:06:38 -03:00
std :: size_t maxCharsFromOriginalName = MAX_PLAYER_NAME - guidPart . length ();
2009-10-17 15:51:44 -07:00
2017-05-26 12:06:38 -03:00
name = GetColumn ( ts , line , "name" ). substr ( 0 , maxCharsFromOriginalName ) + guidPart ;
2009-10-17 15:51:44 -07:00
2017-05-26 12:06:38 -03:00
// characters.at_login set to "rename on login"
if ( ! ChangeColumn ( ts , line , "name" , name ))
return DUMP_FILE_BROKEN ;
if ( ! ChangeColumn ( ts , line , "at_login" , "1" ))
return DUMP_FILE_BROKEN ;
}
2017-07-15 16:41:59 +02:00
else if ( ! ChangeColumn ( ts , line , "name" , name )) // characters.name
2017-05-26 12:06:38 -03:00
return DUMP_FILE_BROKEN ;
2008-10-02 16:23:55 -05:00
break ;
}
default :
break ;
}
2009-10-17 15:51:44 -07:00
2017-05-26 12:06:38 -03:00
FixNULLfields ( line );
2010-08-02 15:14:47 +02:00
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
2020-09-17 21:49:52 +02:00
if ( input . fail () && ! input . eof ())
return DUMP_FILE_BROKEN ;
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
2017-02-05 23:14:19 +01:00
sCharacterCache -> AddCharacterCacheEntry ( 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 ());
2017-05-26 12:06:38 -03:00
sObjectMgr -> _mailId += mails . size ();
sObjectMgr -> _hiPetNumber += petIds . size ();
sObjectMgr -> _equipmentSetGuid += equipmentSetIds . 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
2020-09-13 18:45:30 +02:00
sWorld -> UpdateRealmCharCount ( account );
2008-10-26 13:32:42 -05:00
return DUMP_SUCCESS ;
2008-10-02 16:23:55 -05:00
}
2020-09-17 21:49:52 +02:00
DumpReturn PlayerDumpReader :: LoadDumpFromString ( std :: string const & dump , uint32 account , std :: string name , ObjectGuid :: LowType guid )
{
std :: istringstream input ( dump );
return LoadDump ( input , account , name , guid );
}
DumpReturn PlayerDumpReader :: LoadDumpFromFile ( std :: string const & file , uint32 account , std :: string name , ObjectGuid :: LowType guid )
{
std :: ifstream input ( file );
if ( ! input )
return DUMP_FILE_OPEN_ERROR ;
return LoadDump ( input , account , name , guid );
}