2 * Copyright (C) 2005-2009 MaNGOS <http://getmangos.com/>
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
20 #include "PlayerDump.h"
21 #include "Database/DatabaseEnv.h"
22 #include "Database/SQLStorage.h"
23 #include "UpdateFields.h"
24 #include "ObjectMgr.h"
26 // Character Dump tables
27 #define DUMP_TABLE_COUNT 21
35 static DumpTable dumpTables
[DUMP_TABLE_COUNT
] =
37 { "characters", DTT_CHARACTER
},
38 { "character_achievement", DTT_CHAR_TABLE
},
39 { "character_achievement_progress", DTT_CHAR_TABLE
},
40 { "character_queststatus", DTT_CHAR_TABLE
},
41 { "character_reputation", DTT_CHAR_TABLE
},
42 { "character_spell", DTT_CHAR_TABLE
},
43 { "character_spell_cooldown", DTT_CHAR_TABLE
},
44 { "character_action", DTT_CHAR_TABLE
},
45 { "character_aura", DTT_CHAR_TABLE
},
46 { "character_homebind", DTT_CHAR_TABLE
},
47 { "character_ticket", DTT_CHAR_TABLE
},
48 { "character_inventory", DTT_INVENTORY
},
50 { "mail_items", DTT_MAIL_ITEM
},
51 { "item_instance", DTT_ITEM
},
52 { "character_gifts", DTT_ITEM_GIFT
},
53 { "item_text", DTT_ITEM_TEXT
},
54 { "character_pet", DTT_PET
},
55 { "pet_aura", DTT_PET_TABLE
},
56 { "pet_spell", DTT_PET_TABLE
},
57 { "pet_spell_cooldown", DTT_PET_TABLE
},
60 // Low level functions
61 static bool findtoknth(std::string
&str
, int n
, std::string::size_type
&s
, std::string::size_type
&e
)
64 std::string::size_type size
= str
.size();
65 for(i
= 1; s
< size
&& i
< n
; s
++) if(str
[s
] == ' ') ++i
;
71 return e
!= std::string::npos
;
74 std::string
gettoknth(std::string
&str
, int n
)
76 std::string::size_type s
= 0, e
= 0;
77 if(!findtoknth(str
, n
, s
, e
))
80 return str
.substr(s
, e
-s
);
83 bool findnth(std::string
&str
, int n
, std::string::size_type
&s
, std::string::size_type
&e
)
85 s
= str
.find("VALUES ('")+9;
86 if (s
== std::string::npos
) return false;
91 if (e
== std::string::npos
) return false;
92 } while(str
[e
-1] == '\\');
94 for(int i
= 1; i
< n
; i
++)
100 if (e
== std::string::npos
) return false;
101 } while (str
[e
-1] == '\\');
106 std::string
gettablename(std::string
&str
)
108 std::string::size_type s
= 13;
109 std::string::size_type e
= str
.find(_TABLE_SIM_
, s
);
110 if (e
== std::string::npos
)
113 return str
.substr(s
, e
-s
);
116 bool changenth(std::string
&str
, int n
, const char *with
, bool insert
= false, bool nonzero
= false)
118 std::string::size_type s
, e
;
119 if(!findnth(str
,n
,s
,e
))
122 if(nonzero
&& str
.substr(s
,e
-s
) == "0")
123 return true; // not an error
125 str
.replace(s
,e
-s
, with
);
132 std::string
getnth(std::string
&str
, int n
)
134 std::string::size_type s
, e
;
135 if(!findnth(str
,n
,s
,e
))
138 return str
.substr(s
, e
-s
);
141 bool changetoknth(std::string
&str
, int n
, const char *with
, bool insert
= false, bool nonzero
= false)
143 std::string::size_type s
= 0, e
= 0;
144 if(!findtoknth(str
, n
, s
, e
))
146 if(nonzero
&& str
.substr(s
,e
-s
) == "0")
147 return true; // not an error
149 str
.replace(s
, e
-s
, with
);
156 uint32
registerNewGuid(uint32 oldGuid
, std::map
<uint32
, uint32
> &guidMap
, uint32 hiGuid
)
158 std::map
<uint32
, uint32
>::iterator itr
= guidMap
.find(oldGuid
);
159 if(itr
!= guidMap
.end())
162 uint32 newguid
= hiGuid
+ guidMap
.size();
163 guidMap
[oldGuid
] = newguid
;
167 bool changeGuid(std::string
&str
, int n
, std::map
<uint32
, uint32
> &guidMap
, uint32 hiGuid
, bool nonzero
= false)
170 uint32 oldGuid
= atoi(getnth(str
, n
).c_str());
171 if (nonzero
&& oldGuid
== 0)
172 return true; // not an error
174 uint32 newGuid
= registerNewGuid(oldGuid
, guidMap
, hiGuid
);
175 snprintf(chritem
, 20, "%d", newGuid
);
177 return changenth(str
, n
, chritem
, false, nonzero
);
180 bool changetokGuid(std::string
&str
, int n
, std::map
<uint32
, uint32
> &guidMap
, uint32 hiGuid
, bool nonzero
= false)
183 uint32 oldGuid
= atoi(gettoknth(str
, n
).c_str());
184 if (nonzero
&& oldGuid
== 0)
185 return true; // not an error
187 uint32 newGuid
= registerNewGuid(oldGuid
, guidMap
, hiGuid
);
188 snprintf(chritem
, 20, "%d", newGuid
);
190 return changetoknth(str
, n
, chritem
, false, nonzero
);
193 std::string
CreateDumpString(char const* tableName
, QueryResult
*result
)
195 if(!tableName
|| !result
) return "";
196 std::ostringstream ss
;
197 ss
<< "INSERT INTO "<< _TABLE_SIM_
<< tableName
<< _TABLE_SIM_
<< " VALUES (";
198 Field
*fields
= result
->Fetch();
199 for(uint32 i
= 0; i
< result
->GetFieldCount(); i
++)
201 if (i
== 0) ss
<< "'";
204 std::string s
= fields
[i
].GetCppString();
205 CharacterDatabase
.escape_string(s
);
214 std::string
PlayerDumpWriter::GenerateWhereStr(char const* field
, uint32 guid
)
216 std::ostringstream wherestr
;
217 wherestr
<< field
<< " = '" << guid
<< "'";
218 return wherestr
.str();
221 std::string
PlayerDumpWriter::GenerateWhereStr(char const* field
, GUIDs
const& guids
, GUIDs::const_iterator
& itr
)
223 std::ostringstream wherestr
;
224 wherestr
<< field
<< " IN ('";
225 for(; itr
!= guids
.end(); ++itr
)
229 if(wherestr
.str().size() > MAX_QUERY_LEN
- 50) // near to max query
235 GUIDs::const_iterator itr2
= itr
;
236 if(++itr2
!= guids
.end())
240 return wherestr
.str();
243 void StoreGUID(QueryResult
*result
,uint32 field
,std::set
<uint32
>& guids
)
245 Field
* fields
= result
->Fetch();
246 uint32 guid
= fields
[field
].GetUInt32();
251 void StoreGUID(QueryResult
*result
,uint32 data
,uint32 field
, std::set
<uint32
>& guids
)
253 Field
* fields
= result
->Fetch();
254 std::string dataStr
= fields
[data
].GetCppString();
255 uint32 guid
= atoi(gettoknth(dataStr
, field
).c_str());
260 // Writing - High-level functions
261 void PlayerDumpWriter::DumpTable(std::string
& dump
, uint32 guid
, char const*tableFrom
, char const*tableTo
, DumpTableType type
)
263 GUIDs
const* guids
= NULL
;
264 char const* fieldname
= NULL
;
268 case DTT_ITEM
: fieldname
= "guid"; guids
= &items
; break;
269 case DTT_ITEM_GIFT
: fieldname
= "item_guid"; guids
= &items
; break;
270 case DTT_PET
: fieldname
= "owner"; break;
271 case DTT_PET_TABLE
: fieldname
= "guid"; guids
= &pets
; break;
272 case DTT_MAIL
: fieldname
= "receiver"; break;
273 case DTT_MAIL_ITEM
: fieldname
= "mail_id"; guids
= &mails
; break;
274 case DTT_ITEM_TEXT
: fieldname
= "id"; guids
= &texts
; break;
275 default: fieldname
= "guid"; break;
278 // for guid set stop if set is empty
279 if(guids
&& guids
->empty())
280 return; // nothing to do
282 // setup for guids case start position
283 GUIDs::const_iterator guids_itr
;
285 guids_itr
= guids
->begin();
289 std::string wherestr
;
291 if(guids
) // set case, get next guids string
292 wherestr
= GenerateWhereStr(fieldname
,*guids
,guids_itr
);
293 else // not set case, get single guid string
294 wherestr
= GenerateWhereStr(fieldname
,guid
);
296 QueryResult
*result
= CharacterDatabase
.PQuery("SELECT * FROM %s WHERE %s", tableFrom
, wherestr
.c_str());
306 StoreGUID(result
,3,items
); break; // item guid collection
308 StoreGUID(result
,0,ITEM_FIELD_ITEM_TEXT_ID
,texts
); break;
309 // item text id collection
311 StoreGUID(result
,0,pets
); break; // pet guid collection
313 StoreGUID(result
,0,mails
); // mail id collection
314 StoreGUID(result
,6,texts
); break; // item text id collection
316 StoreGUID(result
,1,items
); break; // item guid collection
320 dump
+= CreateDumpString(tableTo
, result
);
323 while (result
->NextRow());
327 while(guids
&& guids_itr
!= guids
->end()); // not set case iterate single time, set case iterate for all guids
330 std::string
PlayerDumpWriter::GetDump(uint32 guid
)
334 dump
+= "IMPORTANT NOTE: This sql queries not created for apply directly, use '.pdump load' command in console or client chat instead.\n";
335 dump
+= "IMPORTANT NOTE: NOT APPLY ITS DIRECTLY to character DB or you will DAMAGE and CORRUPT character DB\n\n";
337 // revision check guard
338 QueryResult
* result
= CharacterDatabase
.Query("SELECT * FROM character_db_version LIMIT 1");
341 QueryResult::FieldNames
const& namesMap
= result
->GetFieldNames();
343 for(QueryResult::FieldNames::const_iterator itr
= namesMap
.begin(); itr
!= namesMap
.end(); ++itr
)
345 if(itr
->second
.substr(0,9)=="required_")
347 reqName
= itr
->second
;
354 // this will fail at wrong character DB version
355 dump
+= "UPDATE character_db_version SET "+reqName
+" = 1 WHERE FALSE;\n\n";
358 sLog
.outError("Table 'character_db_version' not have revision guard field, revision guard query not added to pdump.");
361 sLog
.outError("Character DB not have 'character_db_version' table, revision guard query not added to pdump.");
363 for(int i
= 0; i
< DUMP_TABLE_COUNT
; i
++)
364 DumpTable(dump
, guid
, dumpTables
[i
].name
, dumpTables
[i
].name
, dumpTables
[i
].type
);
366 // TODO: Add instance/group..
367 // TODO: Add a dump level option to skip some non-important tables
372 DumpReturn
PlayerDumpWriter::WriteDump(const std::string
& file
, uint32 guid
)
374 FILE *fout
= fopen(file
.c_str(), "w");
376 return DUMP_FILE_OPEN_ERROR
;
378 std::string dump
= GetDump(guid
);
380 fprintf(fout
,"%s\n",dump
.c_str());
385 // Reading - High-level functions
386 #define ROLLBACK(DR) {CharacterDatabase.RollbackTransaction(); fclose(fin); return (DR);}
388 DumpReturn
PlayerDumpReader::LoadDump(const std::string
& file
, uint32 account
, std::string name
, uint32 guid
)
390 // check character count
392 QueryResult
*result
= CharacterDatabase
.PQuery("SELECT COUNT(guid) FROM characters WHERE account = '%d'", account
);
396 Field
*fields
=result
->Fetch();
397 charcount
= fields
[0].GetUInt8();
401 return DUMP_TOO_MANY_CHARS
;
405 FILE *fin
= fopen(file
.c_str(), "r");
407 return DUMP_FILE_OPEN_ERROR
;
409 QueryResult
* result
= NULL
;
410 char newguid
[20], chraccount
[20], newpetid
[20], currpetid
[20], lastpetid
[20];
412 // make sure the same guid doesn't already exist and is safe to use
413 bool incHighest
= true;
414 if(guid
!= 0 && guid
< objmgr
.m_hiCharGuid
)
416 result
= CharacterDatabase
.PQuery("SELECT * FROM characters WHERE guid = '%d'", guid
);
419 guid
= objmgr
.m_hiCharGuid
; // use first free if exists
422 else incHighest
= false;
425 guid
= objmgr
.m_hiCharGuid
;
427 // normalize the name if specified and check if it exists
428 if(!normalizePlayerName(name
))
431 if(ObjectMgr::IsValidName(name
,true))
433 CharacterDatabase
.escape_string(name
); // for safe, we use name only for sql quearies anyway
434 result
= CharacterDatabase
.PQuery("SELECT * FROM characters WHERE name = '%s'", name
.c_str());
437 name
= ""; // use the one from the dump
443 // name encoded or empty
445 snprintf(newguid
, 20, "%d", guid
);
446 snprintf(chraccount
, 20, "%d", account
);
447 snprintf(newpetid
, 20, "%d", objmgr
.GeneratePetNumber());
448 snprintf(lastpetid
, 20, "%s", "");
450 std::map
<uint32
,uint32
> items
;
451 std::map
<uint32
,uint32
> mails
;
452 char buf
[32000] = "";
454 typedef std::map
<uint32
, uint32
> PetIds
; // old->new petid relation
455 typedef PetIds::value_type PetIdsPair
;
458 CharacterDatabase
.BeginTransaction();
461 if(!fgets(buf
, 32000, fin
))
464 ROLLBACK(DUMP_FILE_BROKEN
);
467 std::string line
; line
.assign(buf
);
469 // skip empty strings
470 size_t nw_pos
= line
.find_first_not_of(" \t\n\r\7");
471 if(nw_pos
==std::string::npos
)
475 if(line
.substr(nw_pos
,15)=="IMPORTANT NOTE:")
478 // add required_ check
479 if(line
.substr(nw_pos
,41)=="UPDATE character_db_version SET required_")
481 if(!CharacterDatabase
.Execute(line
.c_str()))
482 ROLLBACK(DUMP_FILE_BROKEN
);
487 // determine table name and load type
488 std::string tn
= gettablename(line
);
491 sLog
.outError("LoadPlayerDump: Can't extract table name from line: '%s'!", line
.c_str());
492 ROLLBACK(DUMP_FILE_BROKEN
);
497 for(i
= 0; i
< DUMP_TABLE_COUNT
; i
++)
499 if (tn
== dumpTables
[i
].name
)
501 type
= dumpTables
[i
].type
;
506 if (i
== DUMP_TABLE_COUNT
)
508 sLog
.outError("LoadPlayerDump: Unknown table: '%s'!", tn
.c_str());
509 ROLLBACK(DUMP_FILE_BROKEN
);
512 // change the data to server values
516 if(!changenth(line
, 1, newguid
))
517 ROLLBACK(DUMP_FILE_BROKEN
);
520 case DTT_CHARACTER
: // character t.
522 if(!changenth(line
, 1, newguid
))
523 ROLLBACK(DUMP_FILE_BROKEN
);
525 // guid, data field:guid, items
526 if(!changenth(line
, 2, chraccount
))
527 ROLLBACK(DUMP_FILE_BROKEN
);
528 std::string vals
= getnth(line
, 3);
529 if(!changetoknth(vals
, OBJECT_FIELD_GUID
+1, newguid
))
530 ROLLBACK(DUMP_FILE_BROKEN
);
531 for(uint16 field
= PLAYER_FIELD_INV_SLOT_HEAD
; field
< PLAYER_FARSIGHT
; field
++)
532 if(!changetokGuid(vals
, field
+1, items
, objmgr
.m_hiItemGuid
, true))
533 ROLLBACK(DUMP_FILE_BROKEN
);
534 if(!changenth(line
, 3, vals
.c_str()))
535 ROLLBACK(DUMP_FILE_BROKEN
);
538 // check if the original name already exists
539 name
= getnth(line
, 4);
540 CharacterDatabase
.escape_string(name
);
542 result
= CharacterDatabase
.PQuery("SELECT * FROM characters WHERE name = '%s'", name
.c_str());
546 // rename on login: `at_login` field 30 in raw field list
547 if(!changenth(line
, 30, "1"))
548 ROLLBACK(DUMP_FILE_BROKEN
);
551 else if(!changenth(line
, 4, name
.c_str()))
552 ROLLBACK(DUMP_FILE_BROKEN
);
556 case DTT_INVENTORY
: // character_inventory t.
558 if(!changenth(line
, 1, newguid
))
559 ROLLBACK(DUMP_FILE_BROKEN
);
562 if(!changeGuid(line
, 2, items
, objmgr
.m_hiItemGuid
, true))
563 ROLLBACK(DUMP_FILE_BROKEN
);
564 if(!changeGuid(line
, 4, items
, objmgr
.m_hiItemGuid
))
565 ROLLBACK(DUMP_FILE_BROKEN
);
568 case DTT_ITEM
: // item_instance t.
570 // item, owner, data field:item, owner guid
571 if(!changeGuid(line
, 1, items
, objmgr
.m_hiItemGuid
))
572 ROLLBACK(DUMP_FILE_BROKEN
);
573 if(!changenth(line
, 2, newguid
))
574 ROLLBACK(DUMP_FILE_BROKEN
);
575 std::string vals
= getnth(line
,3);
576 if(!changetokGuid(vals
, OBJECT_FIELD_GUID
+1, items
, objmgr
.m_hiItemGuid
))
577 ROLLBACK(DUMP_FILE_BROKEN
);
578 if(!changetoknth(vals
, ITEM_FIELD_OWNER
+1, newguid
))
579 ROLLBACK(DUMP_FILE_BROKEN
);
580 if(!changenth(line
, 3, vals
.c_str()))
581 ROLLBACK(DUMP_FILE_BROKEN
);
584 case DTT_ITEM_GIFT
: // character_gift
587 if(!changenth(line
, 1, newguid
))
588 ROLLBACK(DUMP_FILE_BROKEN
);
589 if(!changeGuid(line
, 2, items
, objmgr
.m_hiItemGuid
))
590 ROLLBACK(DUMP_FILE_BROKEN
);
593 case DTT_PET
: // character_pet t
595 //store a map of old pet id to new inserted pet id for use by type 5 tables
596 snprintf(currpetid
, 20, "%s", getnth(line
, 1).c_str());
597 if(strlen(lastpetid
)==0) snprintf(lastpetid
, 20, "%s", currpetid
);
598 if(strcmp(lastpetid
,currpetid
)!=0)
600 snprintf(newpetid
, 20, "%d", objmgr
.GeneratePetNumber());
601 snprintf(lastpetid
, 20, "%s", currpetid
);
604 std::map
<uint32
, uint32
> :: const_iterator petids_iter
= petids
.find(atoi(currpetid
));
606 if(petids_iter
== petids
.end())
608 petids
.insert(PetIdsPair(atoi(currpetid
), atoi(newpetid
)));
611 // item, entry, owner, ...
612 if(!changenth(line
, 1, newpetid
))
613 ROLLBACK(DUMP_FILE_BROKEN
);
614 if(!changenth(line
, 3, newguid
))
615 ROLLBACK(DUMP_FILE_BROKEN
);
619 case DTT_PET_TABLE
: // pet_aura, pet_spell, pet_spell_cooldown t
621 snprintf(currpetid
, 20, "%s", getnth(line
, 1).c_str());
623 // lookup currpetid and match to new inserted pet id
624 std::map
<uint32
, uint32
> :: const_iterator petids_iter
= petids
.find(atoi(currpetid
));
625 if(petids_iter
== petids
.end()) // couldn't find new inserted id
626 ROLLBACK(DUMP_FILE_BROKEN
);
628 snprintf(newpetid
, 20, "%d", petids_iter
->second
);
630 if(!changenth(line
, 1, newpetid
))
631 ROLLBACK(DUMP_FILE_BROKEN
);
635 case DTT_MAIL
: // mail
637 // id,messageType,stationery,sender,receiver
638 if(!changeGuid(line
, 1, mails
, objmgr
.m_mailid
))
639 ROLLBACK(DUMP_FILE_BROKEN
);
640 if(!changenth(line
, 5, newguid
))
641 ROLLBACK(DUMP_FILE_BROKEN
);
644 case DTT_MAIL_ITEM
: // mail_items
646 // mail_id,item_guid,item_template,receiver
647 if(!changeGuid(line
, 1, mails
, objmgr
.m_mailid
))
648 ROLLBACK(DUMP_FILE_BROKEN
);
649 if(!changeGuid(line
, 2, items
, objmgr
.m_hiItemGuid
))
650 ROLLBACK(DUMP_FILE_BROKEN
);
651 if(!changenth(line
, 4, newguid
))
652 ROLLBACK(DUMP_FILE_BROKEN
);
656 sLog
.outError("Unknown dump table type: %u",type
);
660 if(!CharacterDatabase
.Execute(line
.c_str()))
661 ROLLBACK(DUMP_FILE_BROKEN
);
664 CharacterDatabase
.CommitTransaction();
666 objmgr
.m_hiItemGuid
+= items
.size();
667 objmgr
.m_mailid
+= mails
.size();
670 ++objmgr
.m_hiCharGuid
;