1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
|
/*
* This file is part of the TrinityCore Project. See AUTHORS file for Copyright information
*
* 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/>.
*/
#include "PlayerDump.h"
#include "AccountMgr.h"
#include "CharacterCache.h"
#include "Common.h"
#include "DatabaseEnv.h"
#include "Log.h"
#include "ObjectMgr.h"
#include "Player.h"
#include "UpdateFields.h"
#include "World.h"
// static data
enum GuidType : uint8
{
// 32 bit long guids
GUID_TYPE_ACCOUNT,
GUID_TYPE_CHAR,
GUID_TYPE_PET,
GUID_TYPE_MAIL,
GUID_TYPE_ITEM,
// 64 bit long guids
GUID_TYPE_EQUIPMENT_SET,
// 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 }
};
struct DumpTable
{
char const* Name;
DumpTableType Type;
};
DumpTable const DumpTables[] =
{
{ "characters", DTT_CHARACTER },
{ "character_account_data", DTT_CHAR_TABLE },
{ "character_achievement", DTT_CHAR_TABLE },
{ "character_achievement_progress", DTT_CHAR_TABLE },
{ "character_action", DTT_CHAR_TABLE },
{ "character_aura", DTT_CHAR_TABLE },
{ "character_declinedname", DTT_CHAR_TABLE },
{ "character_equipmentsets", DTT_EQSET_TABLE },
{ "character_fishingsteps", DTT_CHAR_TABLE },
{ "character_glyphs", DTT_CHAR_TABLE },
{ "character_homebind", DTT_CHAR_TABLE },
{ "character_inventory", DTT_INVENTORY },
{ "character_pet", DTT_PET },
{ "character_pet_declinedname", DTT_PET },
{ "character_queststatus", DTT_CHAR_TABLE },
{ "character_queststatus_daily", DTT_CHAR_TABLE },
{ "character_queststatus_weekly", DTT_CHAR_TABLE },
{ "character_queststatus_monthly", DTT_CHAR_TABLE },
{ "character_queststatus_seasonal", DTT_CHAR_TABLE },
{ "character_queststatus_rewarded", DTT_CHAR_TABLE },
{ "character_reputation", DTT_CHAR_TABLE },
{ "character_skills", DTT_CHAR_TABLE },
{ "character_spell", DTT_CHAR_TABLE },
{ "character_spell_cooldown", DTT_CHAR_TABLE },
{ "character_talent", DTT_CHAR_TABLE },
{ "mail", DTT_MAIL },
{ "mail_items", DTT_MAIL_ITEM }, // must be after mail
{ "pet_aura", DTT_PET_TABLE }, // must be after character_pet
{ "pet_spell", DTT_PET_TABLE }, // must be after character_pet
{ "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
};
uint32 const DUMP_TABLE_COUNT = std::extent<decltype(DumpTables)>::value;
// helper class to dump sql queries to a printable string
class StringTransaction
{
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;
};
// dynamic data, loaded at startup
struct TableField
{
std::string FieldName;
GuidType FieldGuidType = GUID_TYPE_ACCOUNT;
bool IsDependentField = false;
};
struct TableStruct
{
std::string TableName;
std::string WhereFieldName;
std::vector<TableField> TableFields;
// for lookup
std::unordered_map<std::string /*fieldName*/, int32 /*index*/> FieldIndices;
};
std::vector<TableStruct> CharacterTables;
inline bool StringsEqualCaseInsensitive(std::string const& left, std::string const& right)
{
std::string upperLeftString = left;
bool leftResult = Utf8ToUpperOnlyLatin(upperLeftString);
ASSERT(leftResult);
std::string upperRightString = right;
bool rightResult = Utf8ToUpperOnlyLatin(upperRightString);
ASSERT(rightResult);
return upperLeftString == upperRightString;
}
inline auto FindColumnByName(TableStruct& tableStruct, std::string const& columnName) -> decltype(tableStruct.TableFields.begin())
{
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;
return itr->second;
}
inline void MarkDependentColumn(TableStruct& tableStruct, std::string const& columnName, GuidType dependentType)
{
auto itr = FindColumnByName(tableStruct, columnName);
if (itr == tableStruct.TableFields.end())
{
TC_LOG_FATAL("server.loading", "Column `%s` declared in table `%s` marked as dependent but doesn't exist, PlayerDump will not work properly, please update table definitions",
columnName.c_str(), tableStruct.TableName.c_str());
ABORT();
return;
}
if (itr->IsDependentField)
{
TC_LOG_FATAL("server.loading", "Attempt to mark column `%s` in table `%s` as dependent column but already marked! please check your code.",
columnName.c_str(), tableStruct.TableName.c_str());
ABORT();
return;
}
itr->IsDependentField = true;
itr->FieldGuidType = dependentType;
}
inline void MarkWhereField(TableStruct& tableStruct, std::string const& whereField)
{
ASSERT(tableStruct.WhereFieldName.empty());
auto whereFieldItr = FindColumnByName(tableStruct, whereField);
if (whereFieldItr == tableStruct.TableFields.end())
{
TC_LOG_FATAL("server.loading", "Column name `%s` set as 'WHERE' column for table `%s` doesn't exist. PlayerDump won't work properly",
whereField.c_str(), tableStruct.TableName.c_str());
ABORT();
return;
}
tableStruct.WhereFieldName = whereField;
}
inline void AssertBaseTable(BaseTable const& baseTable)
{
auto itr = std::find_if(CharacterTables.begin(), CharacterTables.end(), [baseTable](TableStruct const& tableStruct) -> bool
{
return StringsEqualCaseInsensitive(tableStruct.TableName, baseTable.TableName);
});
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());
}
void PlayerDump::InitializeTables()
{
uint32 oldMSTime = getMSTime();
for (DumpTable const& dumpTable : DumpTables)
{
TableStruct t;
t.TableName = dumpTable.Name;
QueryResult result = CharacterDatabase.PQuery("DESC %s", dumpTable.Name);
// prepared statement is correct (checked at startup) so table must exist
ASSERT(result);
int32 i = 0;
do
{
std::string columnName = (*result)[0].GetString();
t.FieldIndices.emplace(columnName, i++);
TableField f;
f.FieldName = columnName;
bool toUpperResult = Utf8ToUpperOnlyLatin(columnName);
ASSERT(toUpperResult);
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_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)
{
std::string itColumn = Trinity::StringFormat("item%u", j);
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_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_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:
TC_LOG_FATAL("server.loading", "Wrong dump table type %u, probably added a new table type without updating code", uint32(dumpTable.Type));
ABORT();
return;
}
CharacterTables.emplace_back(std::move(t));
}
// perform some sanity checks
for (TableStruct const& tableStruct : CharacterTables)
{
if (tableStruct.WhereFieldName.empty())
{
TC_LOG_FATAL("server.loading", "Table `%s` defined in player dump doesn't have a WHERE query field", tableStruct.TableName.c_str());
ABORT();
}
}
for (BaseTable const& baseTable : BaseTables)
AssertBaseTable(baseTable);
ASSERT(CharacterTables.size() == DUMP_TABLE_COUNT);
TC_LOG_INFO("server.loading", ">> Initialized tables for PlayerDump in %u ms.", GetMSTimeDiffToNow(oldMSTime));
}
// 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 += 9;
do
{
e = str.find('\'', s);
if (e == std::string::npos)
return false;
} while (str[e - 1] == '\\');
for (int32 i = 1; i < columnIndex; ++i)
{
do
{
// length of "', '"
s = e + 4;
e = str.find('\'', s);
if (e == std::string::npos)
return false;
} while (str[e - 1] == '\\');
}
return true;
}
inline std::string GetTableName(std::string const& str)
{
// length of "INSERT INTO `"
static std::string::size_type const s = 13;
std::string::size_type e = str.find('`', s);
if (e == std::string::npos)
return "";
return str.substr(s, e - s);
}
inline bool ValidateFields(TableStruct const& ts, std::string const& str, size_t lineNumber)
{
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)
{
TC_LOG_ERROR("misc", "LoadPlayerDump: (line " UI64FMTD ") dump format not recognized.", lineNumber);
return false;
}
s += 4;
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)
{
TC_LOG_ERROR("misc", "LoadPlayerDump: (line " UI64FMTD ") unexpected end of line", lineNumber);
return false;
}
do
{
std::string column = str.substr(s, e - s);
int32 columnIndex = GetColumnIndexByName(ts, column);
if (columnIndex == -1)
{
TC_LOG_ERROR("misc", "LoadPlayerDump: (line " UI64FMTD ") unknown column name `%s` for table `%s`, aborting due to incompatible DB structure.", lineNumber, column.c_str(), ts.TableName.c_str());
return false;
}
// length of "`, `"
s = e + 4;
e = str.find('`', s);
} while (e < valPos);
return true;
}
inline bool ChangeColumn(TableStruct const& ts, std::string& str, std::string const& column, std::string const& with, bool allowZero = false)
{
std::string::size_type s, e;
if (!FindColumn(ts, str, column, s, e))
return false;
if (allowZero && str.substr(s, e - s) == "0")
return true; // not an error
str.replace(s, e - s, with);
return true;
}
inline std::string GetColumn(TableStruct const& ts, std::string& str, std::string const& column)
{
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);
if (itr != guidMap.end())
return itr->second;
T newguid = guidOffset + T(guidMap.size());
guidMap.emplace(oldGuid, newguid);
return newguid;
}
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)
{
T oldGuid(atoull(GetColumn(ts, str, column).c_str()));
if (allowZero && !oldGuid)
return true; // not an error
std::string chritem;
T newGuid = RegisterNewGuid(oldGuid, guidMap, guidOffset);
chritem = std::to_string(newGuid);
return ChangeColumn(ts, str, column, chritem, allowZero);
}
inline void AppendTableDump(StringTransaction& trans, TableStruct const& tableStruct, QueryResult result)
{
if (!result)
return;
do
{
std::ostringstream ss;
ss << "INSERT INTO `" << tableStruct.TableName << "` (";
for (auto itr = tableStruct.TableFields.begin(); itr != tableStruct.TableFields.end();)
{
ss << '`' << itr->FieldName << '`';
++itr;
if (itr != tableStruct.TableFields.end())
ss << ", ";
}
ss << ") VALUES (";
uint32 const fieldSize = uint32(tableStruct.TableFields.size());
Field* fields = result->Fetch();
for (uint32 i = 0; i < fieldSize;)
{
char const* cString = fields[i].GetCString();
++i;
// null pointer -> we have null
if (!cString)
ss << "'NULL'";
else
{
std::string s(cString);
CharacterDatabase.EscapeString(s);
ss << '\'' << s << '\'';
}
if (i != fieldSize)
ss << ", ";
}
ss << ");";
trans.Append(ss.str().c_str());
} while (result->NextRow());
}
inline std::string GenerateWhereStr(std::string const& field, ObjectGuid::LowType guid)
{
std::ostringstream whereStr;
whereStr << field << " = '" << guid << '\'';
return whereStr.str();
}
template <typename T, template<class, class...> class SetType, class... Rest>
inline std::string GenerateWhereStr(std::string const& field, SetType<T, Rest...> const& guidSet)
{
std::ostringstream whereStr;
whereStr << field << " IN ('";
for (auto itr = guidSet.begin(); itr != guidSet.end();)
{
whereStr << *itr;
++itr;
if (whereStr.str().size() > MAX_QUERY_LEN - 50) // near to max query
break;
if (itr != guidSet.end())
whereStr << "','";
}
whereStr << "')";
return whereStr.str();
}
// Writing - High-level functions
void PlayerDumpWriter::PopulateGuids(ObjectGuid::LowType guid)
{
for (BaseTable const& baseTable : BaseTables)
{
switch (baseTable.StoredType)
{
case GUID_TYPE_ITEM:
case GUID_TYPE_MAIL:
case GUID_TYPE_PET:
case GUID_TYPE_EQUIPMENT_SET:
break;
default:
return;
}
std::string whereStr = GenerateWhereStr(baseTable.PlayerGuid, guid);
QueryResult result = CharacterDatabase.PQuery("SELECT %s FROM %s WHERE %s", baseTable.PrimaryKey, baseTable.TableName, whereStr.c_str());
if (!result)
continue;
do
{
switch (baseTable.StoredType)
{
case GUID_TYPE_ITEM:
if (ObjectGuid::LowType itemLowGuid = (*result)[0].GetUInt32())
_items.insert(itemLowGuid);
break;
case GUID_TYPE_MAIL:
if (ObjectGuid::LowType mailLowGuid = (*result)[0].GetUInt32())
_mails.insert(mailLowGuid);
break;
case GUID_TYPE_PET:
if (ObjectGuid::LowType petLowGuid = (*result)[0].GetUInt32())
_pets.insert(petLowGuid);
break;
case GUID_TYPE_EQUIPMENT_SET:
if (uint64 eqSetId = (*result)[0].GetUInt64())
_itemSets.insert(eqSetId);
break;
default:
break;
}
} while (result->NextRow());
}
}
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:
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:
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;
}
QueryResult result = CharacterDatabase.PQuery("SELECT * FROM %s WHERE %s", dumpTable.Name, whereStr.c_str());
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;
}
AppendTableDump(trans, tableStruct, result);
return true;
}
bool PlayerDumpWriter::GetDump(ObjectGuid::LowType guid, std::string& dump)
{
dump = "IMPORTANT NOTE: THIS DUMPFILE IS MADE FOR USE WITH THE 'PDUMP' COMMAND ONLY - EITHER THROUGH INGAME CHAT OR ON CONSOLE!\n";
dump += "IMPORTANT NOTE: DO NOT apply it directly - it will irreversibly DAMAGE and CORRUPT your database! You have been warned!\n\n";
StringTransaction trans;
// collect guids
PopulateGuids(guid);
for (uint32 i = 0; i < DUMP_TABLE_COUNT; ++i)
if (!AppendTable(trans, guid, CharacterTables[i], DumpTables[i]))
return false;
dump += trans.GetBuffer();
/// @todo Add instance/group..
/// @todo Add a dump level option to skip some non-important tables
return true;
}
DumpReturn PlayerDumpWriter::WriteDump(std::string const& file, ObjectGuid::LowType guid)
{
if (sWorld->getBoolConfig(CONFIG_PDUMP_NO_PATHS))
if (strchr(file.c_str(), '\\') || strchr(file.c_str(), '/'))
return DUMP_FILE_OPEN_ERROR;
if (sWorld->getBoolConfig(CONFIG_PDUMP_NO_OVERWRITE))
{
// check if file exists already
if (GetFileHandle(file.c_str(), "r"))
return DUMP_FILE_OPEN_ERROR;
}
FileHandle fout = GetFileHandle(file.c_str(), "w");
if (!fout)
return DUMP_FILE_OPEN_ERROR;
DumpReturn ret = DUMP_SUCCESS;
std::string dump;
if (!GetDump(guid, dump))
ret = DUMP_CHARACTER_DELETED;
fprintf(fout.get(), "%s", dump.c_str());
return ret;
}
// Reading - High-level functions
inline void FixNULLfields(std::string& line)
{
static std::string const NullString("'NULL'");
size_t pos = line.find(NullString);
while (pos != std::string::npos)
{
line.replace(pos, NullString.length(), "NULL");
pos = line.find(NullString);
}
}
DumpReturn PlayerDumpReader::LoadDump(std::string const& file, uint32 account, std::string name, ObjectGuid::LowType guid)
{
uint32 charcount = AccountMgr::GetCharactersCount(account);
if (charcount >= 10)
return DUMP_TOO_MANY_CHARS;
FileHandle fin = GetFileHandle(file.c_str(), "r");
if (!fin)
return DUMP_FILE_OPEN_ERROR;
std::string newguid, chraccount;
// make sure the same guid doesn't already exist and is safe to use
bool incHighest = true;
if (guid && guid < sObjectMgr->GetGenerator<HighGuid::Player>().GetNextAfterMaxUsed())
{
PreparedStatement* stmt = CharacterDatabase.GetPreparedStatement(CHAR_SEL_CHECK_GUID);
stmt->setUInt32(0, guid);
if (PreparedQueryResult result = CharacterDatabase.Query(stmt))
guid = sObjectMgr->GetGenerator<HighGuid::Player>().GetNextAfterMaxUsed(); // use first free if exists
else
incHighest = false;
}
else
guid = sObjectMgr->GetGenerator<HighGuid::Player>().GetNextAfterMaxUsed();
// normalize the name if specified and check if it exists
if (!normalizePlayerName(name))
name.clear();
if (ObjectMgr::CheckPlayerName(name, sWorld->GetDefaultDbcLocale(), true) == CHAR_NAME_SUCCESS)
{
PreparedStatement* stmt = CharacterDatabase.GetPreparedStatement(CHAR_SEL_CHECK_NAME);
stmt->setString(0, name);
if (PreparedQueryResult result = CharacterDatabase.Query(stmt))
name.clear(); // use the one from the dump
}
else
name.clear();
// name encoded or empty
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();
std::map<ObjectGuid::LowType, ObjectGuid::LowType> mails;
ObjectGuid::LowType mailLowGuidOffset = sObjectMgr->_mailId;
std::map<ObjectGuid::LowType, ObjectGuid::LowType> petIds;
ObjectGuid::LowType petLowGuidOffset = sObjectMgr->_hiPetNumber;
std::map<uint64, uint64> equipmentSetIds;
uint64 equipmentSetGuidOffset = sObjectMgr->_equipmentSetGuid;
static size_t const BUFFER_SIZE = 32000;
char buf[BUFFER_SIZE] = { };
uint8 gender = GENDER_NONE;
uint8 race = RACE_NONE;
uint8 playerClass = CLASS_NONE;
uint8 level = 1;
// for logs
size_t lineNumber = 0;
SQLTransaction trans = CharacterDatabase.BeginTransaction();
while (!feof(fin.get()))
{
if (!fgets(buf, BUFFER_SIZE, fin.get()))
{
if (feof(fin.get()))
break;
return DUMP_FILE_BROKEN;
}
std::string line;
line.assign(buf);
++lineNumber;
// skip empty strings
size_t nw_pos = line.find_first_not_of(" \t\n\r\7");
if (nw_pos == std::string::npos)
continue;
// skip the important notes
static std::string const SkippedLine = "IMPORTANT NOTE:";
if (line.substr(nw_pos, SkippedLine.size()) == SkippedLine)
continue;
// determine table name and load type
std::string tn = GetTableName(line);
if (tn.empty())
{
TC_LOG_ERROR("misc", "LoadPlayerDump: (line " UI64FMTD ") Can't extract table name!", lineNumber);
return DUMP_FILE_BROKEN;
}
DumpTableType type = DTT_CHARACTER;
uint32 i;
for (i = 0; i < DUMP_TABLE_COUNT; ++i)
{
if (tn == DumpTables[i].Name)
{
type = DumpTables[i].Type;
break;
}
}
if (i == DUMP_TABLE_COUNT)
{
TC_LOG_ERROR("misc", "LoadPlayerDump: (line " UI64FMTD ") Unknown table: `%s`!", lineNumber, tn.c_str());
return DUMP_FILE_BROKEN;
}
TableStruct const& ts = CharacterTables[i];
if (!ValidateFields(ts, line, lineNumber))
return DUMP_FILE_BROKEN;
// per field guid offsetting
for (TableField const& field : ts.TableFields)
{
if (!field.IsDependentField)
continue;
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:
{
static std::string const NullString("NULL");
if (!ChangeColumn(ts, line, field.FieldName, NullString))
return DUMP_FILE_BROKEN;
break;
}
}
}
// extra modifications for other tables
switch (type)
{
case DTT_CHARACTER:
{
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())
{
// check if the original name already exists
name = GetColumn(ts, line, "name");
PreparedStatement* stmt = CharacterDatabase.GetPreparedStatement(CHAR_SEL_CHECK_NAME);
stmt->setString(0, name);
PreparedQueryResult result = CharacterDatabase.Query(stmt);
// characters.at_login set to "rename on login"
if (result)
if (!ChangeColumn(ts, line, "at_login", "1"))
return DUMP_FILE_BROKEN;
}
else if (!ChangeColumn(ts, line, "name", name)) // characters.name
return DUMP_FILE_BROKEN;
break;
}
default:
break;
}
FixNULLfields(line);
trans->Append(line.c_str());
}
CharacterDatabase.CommitTransaction(trans);
// in case of name conflict player has to rename at login anyway
sCharacterCache->AddCharacterCacheEntry(ObjectGuid(HighGuid::Player, guid), account, name, gender, race, playerClass, level);
sObjectMgr->GetGenerator<HighGuid::Item>().Set(sObjectMgr->GetGenerator<HighGuid::Item>().GetNextAfterMaxUsed() + items.size());
sObjectMgr->_mailId += mails.size();
sObjectMgr->_hiPetNumber += petIds.size();
sObjectMgr->_equipmentSetGuid += equipmentSetIds.size();
if (incHighest)
sObjectMgr->GetGenerator<HighGuid::Player>().Generate();
return DUMP_SUCCESS;
}
|