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
33 bool isValid() const { return name
!= NULL
; }
36 static DumpTable dumpTables
[] =
38 { "characters", DTT_CHARACTER
},
39 { "character_achievement", DTT_CHAR_TABLE
},
40 { "character_achievement_progress", DTT_CHAR_TABLE
},
41 { "character_queststatus", DTT_CHAR_TABLE
},
42 { "character_reputation", DTT_CHAR_TABLE
},
43 { "character_spell", DTT_CHAR_TABLE
},
44 { "character_spell_cooldown", DTT_CHAR_TABLE
},
45 { "character_action", DTT_CHAR_TABLE
},
46 { "character_aura", DTT_CHAR_TABLE
},
47 { "character_homebind", DTT_CHAR_TABLE
},
48 { "character_skills", DTT_CHAR_TABLE
},
49 { "character_ticket", DTT_CHAR_TABLE
},
50 { "character_inventory", DTT_INVENTORY
},
52 { "mail_items", DTT_MAIL_ITEM
},
53 { "item_instance", DTT_ITEM
},
54 { "character_gifts", DTT_ITEM_GIFT
},
55 { "item_text", DTT_ITEM_TEXT
},
56 { "character_pet", DTT_PET
},
57 { "pet_aura", DTT_PET_TABLE
},
58 { "pet_spell", DTT_PET_TABLE
},
59 { "pet_spell_cooldown", DTT_PET_TABLE
},
60 { NULL
, DTT_CHAR_TABLE
}, // end marker
63 // Low level functions
64 static bool findtoknth(std::string
&str
, int n
, std::string::size_type
&s
, std::string::size_type
&e
)
67 std::string::size_type size
= str
.size();
68 for(i
= 1; s
< size
&& i
< n
; s
++) if(str
[s
] == ' ') ++i
;
74 return e
!= std::string::npos
;
77 std::string
gettoknth(std::string
&str
, int n
)
79 std::string::size_type s
= 0, e
= 0;
80 if(!findtoknth(str
, n
, s
, e
))
83 return str
.substr(s
, e
-s
);
86 bool findnth(std::string
&str
, int n
, std::string::size_type
&s
, std::string::size_type
&e
)
88 s
= str
.find("VALUES ('")+9;
89 if (s
== std::string::npos
) return false;
94 if (e
== std::string::npos
) return false;
95 } while(str
[e
-1] == '\\');
97 for(int i
= 1; i
< n
; ++i
)
103 if (e
== std::string::npos
) return false;
104 } while (str
[e
-1] == '\\');
109 std::string
gettablename(std::string
&str
)
111 std::string::size_type s
= 13;
112 std::string::size_type e
= str
.find(_TABLE_SIM_
, s
);
113 if (e
== std::string::npos
)
116 return str
.substr(s
, e
-s
);
119 bool changenth(std::string
&str
, int n
, const char *with
, bool insert
= false, bool nonzero
= false)
121 std::string::size_type s
, e
;
122 if(!findnth(str
,n
,s
,e
))
125 if(nonzero
&& str
.substr(s
,e
-s
) == "0")
126 return true; // not an error
128 str
.replace(s
,e
-s
, with
);
135 std::string
getnth(std::string
&str
, int n
)
137 std::string::size_type s
, e
;
138 if(!findnth(str
,n
,s
,e
))
141 return str
.substr(s
, e
-s
);
144 bool changetoknth(std::string
&str
, int n
, const char *with
, bool insert
= false, bool nonzero
= false)
146 std::string::size_type s
= 0, e
= 0;
147 if(!findtoknth(str
, n
, s
, e
))
149 if(nonzero
&& str
.substr(s
,e
-s
) == "0")
150 return true; // not an error
152 str
.replace(s
, e
-s
, with
);
159 uint32
registerNewGuid(uint32 oldGuid
, std::map
<uint32
, uint32
> &guidMap
, uint32 hiGuid
)
161 std::map
<uint32
, uint32
>::const_iterator itr
= guidMap
.find(oldGuid
);
162 if(itr
!= guidMap
.end())
165 uint32 newguid
= hiGuid
+ guidMap
.size();
166 guidMap
[oldGuid
] = newguid
;
170 bool changeGuid(std::string
&str
, int n
, std::map
<uint32
, uint32
> &guidMap
, uint32 hiGuid
, bool nonzero
= false)
173 uint32 oldGuid
= atoi(getnth(str
, n
).c_str());
174 if (nonzero
&& oldGuid
== 0)
175 return true; // not an error
177 uint32 newGuid
= registerNewGuid(oldGuid
, guidMap
, hiGuid
);
178 snprintf(chritem
, 20, "%d", newGuid
);
180 return changenth(str
, n
, chritem
, false, nonzero
);
183 bool changetokGuid(std::string
&str
, int n
, std::map
<uint32
, uint32
> &guidMap
, uint32 hiGuid
, bool nonzero
= false)
186 uint32 oldGuid
= atoi(gettoknth(str
, n
).c_str());
187 if (nonzero
&& oldGuid
== 0)
188 return true; // not an error
190 uint32 newGuid
= registerNewGuid(oldGuid
, guidMap
, hiGuid
);
191 snprintf(chritem
, 20, "%d", newGuid
);
193 return changetoknth(str
, n
, chritem
, false, nonzero
);
196 std::string
CreateDumpString(char const* tableName
, QueryResult
*result
)
198 if(!tableName
|| !result
) return "";
199 std::ostringstream ss
;
200 ss
<< "INSERT INTO "<< _TABLE_SIM_
<< tableName
<< _TABLE_SIM_
<< " VALUES (";
201 Field
*fields
= result
->Fetch();
202 for(uint32 i
= 0; i
< result
->GetFieldCount(); ++i
)
204 if (i
== 0) ss
<< "'";
207 std::string s
= fields
[i
].GetCppString();
208 CharacterDatabase
.escape_string(s
);
217 std::string
PlayerDumpWriter::GenerateWhereStr(char const* field
, uint32 guid
)
219 std::ostringstream wherestr
;
220 wherestr
<< field
<< " = '" << guid
<< "'";
221 return wherestr
.str();
224 std::string
PlayerDumpWriter::GenerateWhereStr(char const* field
, GUIDs
const& guids
, GUIDs::const_iterator
& itr
)
226 std::ostringstream wherestr
;
227 wherestr
<< field
<< " IN ('";
228 for(; itr
!= guids
.end(); ++itr
)
232 if(wherestr
.str().size() > MAX_QUERY_LEN
- 50) // near to max query
238 GUIDs::const_iterator itr2
= itr
;
239 if(++itr2
!= guids
.end())
243 return wherestr
.str();
246 void StoreGUID(QueryResult
*result
,uint32 field
,std::set
<uint32
>& guids
)
248 Field
* fields
= result
->Fetch();
249 uint32 guid
= fields
[field
].GetUInt32();
254 void StoreGUID(QueryResult
*result
,uint32 data
,uint32 field
, std::set
<uint32
>& guids
)
256 Field
* fields
= result
->Fetch();
257 std::string dataStr
= fields
[data
].GetCppString();
258 uint32 guid
= atoi(gettoknth(dataStr
, field
).c_str());
263 // Writing - High-level functions
264 void PlayerDumpWriter::DumpTableContent(std::string
& dump
, uint32 guid
, char const*tableFrom
, char const*tableTo
, DumpTableType type
)
266 GUIDs
const* guids
= NULL
;
267 char const* fieldname
= NULL
;
271 case DTT_ITEM
: fieldname
= "guid"; guids
= &items
; break;
272 case DTT_ITEM_GIFT
: fieldname
= "item_guid"; guids
= &items
; break;
273 case DTT_PET
: fieldname
= "owner"; break;
274 case DTT_PET_TABLE
: fieldname
= "guid"; guids
= &pets
; break;
275 case DTT_MAIL
: fieldname
= "receiver"; break;
276 case DTT_MAIL_ITEM
: fieldname
= "mail_id"; guids
= &mails
; break;
277 case DTT_ITEM_TEXT
: fieldname
= "id"; guids
= &texts
; break;
278 default: fieldname
= "guid"; break;
281 // for guid set stop if set is empty
282 if(guids
&& guids
->empty())
283 return; // nothing to do
285 // setup for guids case start position
286 GUIDs::const_iterator guids_itr
;
288 guids_itr
= guids
->begin();
292 std::string wherestr
;
294 if(guids
) // set case, get next guids string
295 wherestr
= GenerateWhereStr(fieldname
,*guids
,guids_itr
);
296 else // not set case, get single guid string
297 wherestr
= GenerateWhereStr(fieldname
,guid
);
299 QueryResult
*result
= CharacterDatabase
.PQuery("SELECT * FROM %s WHERE %s", tableFrom
, wherestr
.c_str());
309 StoreGUID(result
,3,items
); break; // item guid collection
311 StoreGUID(result
,0,ITEM_FIELD_ITEM_TEXT_ID
,texts
); break;
312 // item text id collection
314 StoreGUID(result
,0,pets
); break; // pet guid collection
316 StoreGUID(result
,0,mails
); // mail id collection
317 StoreGUID(result
,7,texts
); break; // item text id collection
319 StoreGUID(result
,1,items
); break; // item guid collection
323 dump
+= CreateDumpString(tableTo
, result
);
326 while (result
->NextRow());
330 while(guids
&& guids_itr
!= guids
->end()); // not set case iterate single time, set case iterate for all guids
333 std::string
PlayerDumpWriter::GetDump(uint32 guid
)
337 dump
+= "IMPORTANT NOTE: This sql queries not created for apply directly, use '.pdump load' command in console or client chat instead.\n";
338 dump
+= "IMPORTANT NOTE: NOT APPLY ITS DIRECTLY to character DB or you will DAMAGE and CORRUPT character DB\n\n";
340 // revision check guard
341 QueryNamedResult
* result
= CharacterDatabase
.QueryNamed("SELECT * FROM character_db_version LIMIT 1");
344 QueryFieldNames
const& namesMap
= result
->GetFieldNames();
346 for(QueryFieldNames::const_iterator itr
= namesMap
.begin(); itr
!= namesMap
.end(); ++itr
)
348 if(itr
->substr(0,9)=="required_")
357 // this will fail at wrong character DB version
358 dump
+= "UPDATE character_db_version SET "+reqName
+" = 1 WHERE FALSE;\n\n";
361 sLog
.outError("Table 'character_db_version' not have revision guard field, revision guard query not added to pdump.");
366 sLog
.outError("Character DB not have 'character_db_version' table, revision guard query not added to pdump.");
368 for(DumpTable
* itr
= &dumpTables
[0]; itr
->isValid(); ++itr
)
369 DumpTableContent(dump
, guid
, itr
->name
, itr
->name
, itr
->type
);
371 // TODO: Add instance/group..
372 // TODO: Add a dump level option to skip some non-important tables
377 DumpReturn
PlayerDumpWriter::WriteDump(const std::string
& file
, uint32 guid
)
379 FILE *fout
= fopen(file
.c_str(), "w");
381 return DUMP_FILE_OPEN_ERROR
;
383 std::string dump
= GetDump(guid
);
385 fprintf(fout
,"%s\n",dump
.c_str());
390 // Reading - High-level functions
391 #define ROLLBACK(DR) {CharacterDatabase.RollbackTransaction(); fclose(fin); return (DR);}
393 DumpReturn
PlayerDumpReader::LoadDump(const std::string
& file
, uint32 account
, std::string name
, uint32 guid
)
395 // check character count
397 QueryResult
*result
= CharacterDatabase
.PQuery("SELECT COUNT(guid) FROM characters WHERE account = '%d'", account
);
401 Field
*fields
=result
->Fetch();
402 charcount
= fields
[0].GetUInt8();
406 return DUMP_TOO_MANY_CHARS
;
410 FILE *fin
= fopen(file
.c_str(), "r");
412 return DUMP_FILE_OPEN_ERROR
;
414 QueryResult
* result
= NULL
;
415 char newguid
[20], chraccount
[20], newpetid
[20], currpetid
[20], lastpetid
[20];
417 // make sure the same guid doesn't already exist and is safe to use
418 bool incHighest
= true;
419 if (guid
!= 0 && guid
< sObjectMgr
.m_hiCharGuid
)
421 result
= CharacterDatabase
.PQuery("SELECT * FROM characters WHERE guid = '%d'", guid
);
424 guid
= sObjectMgr
.m_hiCharGuid
; // use first free if exists
427 else incHighest
= false;
430 guid
= sObjectMgr
.m_hiCharGuid
;
432 // normalize the name if specified and check if it exists
433 if (!normalizePlayerName(name
))
436 if (ObjectMgr::CheckPlayerName(name
,true) == CHAR_NAME_SUCCESS
)
438 CharacterDatabase
.escape_string(name
); // for safe, we use name only for sql quearies anyway
439 result
= CharacterDatabase
.PQuery("SELECT * FROM characters WHERE name = '%s'", name
.c_str());
442 name
= ""; // use the one from the dump
449 // name encoded or empty
451 snprintf(newguid
, 20, "%d", guid
);
452 snprintf(chraccount
, 20, "%d", account
);
453 snprintf(newpetid
, 20, "%d", sObjectMgr
.GeneratePetNumber());
454 snprintf(lastpetid
, 20, "%s", "");
456 std::map
<uint32
,uint32
> items
;
457 std::map
<uint32
,uint32
> mails
;
458 std::map
<uint32
,uint32
> itemTexts
;
459 char buf
[32000] = "";
461 typedef std::map
<uint32
, uint32
> PetIds
; // old->new petid relation
462 typedef PetIds::value_type PetIdsPair
;
465 CharacterDatabase
.BeginTransaction();
468 if(!fgets(buf
, 32000, fin
))
471 ROLLBACK(DUMP_FILE_BROKEN
);
474 std::string line
; line
.assign(buf
);
476 // skip empty strings
477 size_t nw_pos
= line
.find_first_not_of(" \t\n\r\7");
478 if(nw_pos
==std::string::npos
)
482 if(line
.substr(nw_pos
,15)=="IMPORTANT NOTE:")
485 // add required_ check
486 if(line
.substr(nw_pos
,41)=="UPDATE character_db_version SET required_")
488 if(!CharacterDatabase
.Execute(line
.c_str()))
489 ROLLBACK(DUMP_FILE_BROKEN
);
494 // determine table name and load type
495 std::string tn
= gettablename(line
);
498 sLog
.outError("LoadPlayerDump: Can't extract table name from line: '%s'!", line
.c_str());
499 ROLLBACK(DUMP_FILE_BROKEN
);
503 DumpTable
* dTable
= &dumpTables
[0];
504 for(; dTable
->isValid(); ++dTable
)
506 if (tn
== dTable
->name
)
513 if (!dTable
->isValid())
515 sLog
.outError("LoadPlayerDump: Unknown table: '%s'!", tn
.c_str());
516 ROLLBACK(DUMP_FILE_BROKEN
);
519 // change the data to server values
523 if(!changenth(line
, 1, newguid
))
524 ROLLBACK(DUMP_FILE_BROKEN
);
527 case DTT_CHARACTER
: // character t.
529 if(!changenth(line
, 1, newguid
))
530 ROLLBACK(DUMP_FILE_BROKEN
);
532 // guid, data field:guid, items
533 if(!changenth(line
, 2, chraccount
))
534 ROLLBACK(DUMP_FILE_BROKEN
);
535 std::string vals
= getnth(line
, 3);
536 if(!changetoknth(vals
, OBJECT_FIELD_GUID
+1, newguid
))
537 ROLLBACK(DUMP_FILE_BROKEN
);
538 for(uint16 field
= PLAYER_FIELD_INV_SLOT_HEAD
; field
< PLAYER_FARSIGHT
; field
++)
539 if(!changetokGuid(vals
, field
+1, items
, sObjectMgr
.m_hiItemGuid
, true))
540 ROLLBACK(DUMP_FILE_BROKEN
);
541 if(!changenth(line
, 3, vals
.c_str()))
542 ROLLBACK(DUMP_FILE_BROKEN
);
545 // check if the original name already exists
546 name
= getnth(line
, 4);
547 CharacterDatabase
.escape_string(name
);
549 result
= CharacterDatabase
.PQuery("SELECT * FROM characters WHERE name = '%s'", name
.c_str());
554 if(!changenth(line
, 37, "1")) // rename on login: `at_login` field 37 in raw field list
555 ROLLBACK(DUMP_FILE_BROKEN
);
558 else if(!changenth(line
, 4, name
.c_str()))
559 ROLLBACK(DUMP_FILE_BROKEN
);
563 case DTT_INVENTORY
: // character_inventory t.
565 if(!changenth(line
, 1, newguid
))
566 ROLLBACK(DUMP_FILE_BROKEN
);
569 if(!changeGuid(line
, 2, items
, sObjectMgr
.m_hiItemGuid
, true))
570 ROLLBACK(DUMP_FILE_BROKEN
);
571 if(!changeGuid(line
, 4, items
, sObjectMgr
.m_hiItemGuid
))
572 ROLLBACK(DUMP_FILE_BROKEN
);
575 case DTT_ITEM
: // item_instance t.
577 // item, owner, data field:item, owner guid
578 if(!changeGuid(line
, 1, items
, sObjectMgr
.m_hiItemGuid
))
579 ROLLBACK(DUMP_FILE_BROKEN
);
580 if(!changenth(line
, 2, newguid
))
581 ROLLBACK(DUMP_FILE_BROKEN
);
582 std::string vals
= getnth(line
,3);
583 if(!changetokGuid(vals
, OBJECT_FIELD_GUID
+1, items
, sObjectMgr
.m_hiItemGuid
))
584 ROLLBACK(DUMP_FILE_BROKEN
);
585 if(!changetoknth(vals
, ITEM_FIELD_OWNER
+1, newguid
))
586 ROLLBACK(DUMP_FILE_BROKEN
);
587 if(!changetokGuid(vals
, ITEM_FIELD_ITEM_TEXT_ID
+1, itemTexts
, sObjectMgr
.m_ItemTextId
,true))
588 ROLLBACK(DUMP_FILE_BROKEN
);
589 if(!changenth(line
, 3, vals
.c_str()))
590 ROLLBACK(DUMP_FILE_BROKEN
);
593 case DTT_ITEM_GIFT
: // character_gift
596 if(!changenth(line
, 1, newguid
))
597 ROLLBACK(DUMP_FILE_BROKEN
);
598 if(!changeGuid(line
, 2, items
, sObjectMgr
.m_hiItemGuid
))
599 ROLLBACK(DUMP_FILE_BROKEN
);
602 case DTT_PET
: // character_pet t
604 //store a map of old pet id to new inserted pet id for use by type 5 tables
605 snprintf(currpetid
, 20, "%s", getnth(line
, 1).c_str());
606 if(strlen(lastpetid
)==0) snprintf(lastpetid
, 20, "%s", currpetid
);
607 if(strcmp(lastpetid
,currpetid
)!=0)
609 snprintf(newpetid
, 20, "%d", sObjectMgr
.GeneratePetNumber());
610 snprintf(lastpetid
, 20, "%s", currpetid
);
613 std::map
<uint32
, uint32
> :: const_iterator petids_iter
= petids
.find(atoi(currpetid
));
615 if(petids_iter
== petids
.end())
617 petids
.insert(PetIdsPair(atoi(currpetid
), atoi(newpetid
)));
620 // item, entry, owner, ...
621 if(!changenth(line
, 1, newpetid
))
622 ROLLBACK(DUMP_FILE_BROKEN
);
623 if(!changenth(line
, 3, newguid
))
624 ROLLBACK(DUMP_FILE_BROKEN
);
628 case DTT_PET_TABLE
: // pet_aura, pet_spell, pet_spell_cooldown t
630 snprintf(currpetid
, 20, "%s", getnth(line
, 1).c_str());
632 // lookup currpetid and match to new inserted pet id
633 std::map
<uint32
, uint32
> :: const_iterator petids_iter
= petids
.find(atoi(currpetid
));
634 if(petids_iter
== petids
.end()) // couldn't find new inserted id
635 ROLLBACK(DUMP_FILE_BROKEN
);
637 snprintf(newpetid
, 20, "%d", petids_iter
->second
);
639 if(!changenth(line
, 1, newpetid
))
640 ROLLBACK(DUMP_FILE_BROKEN
);
644 case DTT_MAIL
: // mail
646 // id,messageType,stationery,mailtemplate,sender,receiver,subject,itemText
647 if(!changeGuid(line
, 1, mails
, sObjectMgr
.m_mailid
))
648 ROLLBACK(DUMP_FILE_BROKEN
);
649 if(!changenth(line
, 6, newguid
))
650 ROLLBACK(DUMP_FILE_BROKEN
);
651 if(!changeGuid(line
, 8, itemTexts
, sObjectMgr
.m_ItemTextId
))
652 ROLLBACK(DUMP_FILE_BROKEN
);
655 case DTT_MAIL_ITEM
: // mail_items
657 // mail_id,item_guid,item_template,receiver
658 if(!changeGuid(line
, 1, mails
, sObjectMgr
.m_mailid
))
659 ROLLBACK(DUMP_FILE_BROKEN
);
660 if(!changeGuid(line
, 2, items
, sObjectMgr
.m_hiItemGuid
))
661 ROLLBACK(DUMP_FILE_BROKEN
);
662 if(!changenth(line
, 4, newguid
))
663 ROLLBACK(DUMP_FILE_BROKEN
);
666 case DTT_ITEM_TEXT
: // item_text
669 if(!changeGuid(line
, 1, itemTexts
, sObjectMgr
.m_ItemTextId
))
670 ROLLBACK(DUMP_FILE_BROKEN
);
673 uint32 id
= atoi(getnth(line
,1).c_str());
674 std::string text
= getnth(line
,2);
675 sObjectMgr
.AddItemText(id
,text
);
679 sLog
.outError("Unknown dump table type: %u",type
);
683 if(!CharacterDatabase
.Execute(line
.c_str()))
684 ROLLBACK(DUMP_FILE_BROKEN
);
687 CharacterDatabase
.CommitTransaction();
689 sObjectMgr
.m_hiItemGuid
+= items
.size();
690 sObjectMgr
.m_mailid
+= mails
.size();
691 sObjectMgr
.m_ItemTextId
+= itemTexts
.size();
694 ++sObjectMgr
.m_hiCharGuid
;