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
>::const_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.");
363 sLog
.outError("Character DB not have 'character_db_version' table, revision guard query not added to pdump.");
365 for(int i
= 0; i
< DUMP_TABLE_COUNT
; ++i
)
366 DumpTable(dump
, guid
, dumpTables
[i
].name
, dumpTables
[i
].name
, dumpTables
[i
].type
);
368 // TODO: Add instance/group..
369 // TODO: Add a dump level option to skip some non-important tables
374 DumpReturn
PlayerDumpWriter::WriteDump(const std::string
& file
, uint32 guid
)
376 FILE *fout
= fopen(file
.c_str(), "w");
378 return DUMP_FILE_OPEN_ERROR
;
380 std::string dump
= GetDump(guid
);
382 fprintf(fout
,"%s\n",dump
.c_str());
387 // Reading - High-level functions
388 #define ROLLBACK(DR) {CharacterDatabase.RollbackTransaction(); fclose(fin); return (DR);}
390 DumpReturn
PlayerDumpReader::LoadDump(const std::string
& file
, uint32 account
, std::string name
, uint32 guid
)
392 // check character count
394 QueryResult
*result
= CharacterDatabase
.PQuery("SELECT COUNT(guid) FROM characters WHERE account = '%d'", account
);
398 Field
*fields
=result
->Fetch();
399 charcount
= fields
[0].GetUInt8();
403 return DUMP_TOO_MANY_CHARS
;
407 FILE *fin
= fopen(file
.c_str(), "r");
409 return DUMP_FILE_OPEN_ERROR
;
411 QueryResult
* result
= NULL
;
412 char newguid
[20], chraccount
[20], newpetid
[20], currpetid
[20], lastpetid
[20];
414 // make sure the same guid doesn't already exist and is safe to use
415 bool incHighest
= true;
416 if(guid
!= 0 && guid
< objmgr
.m_hiCharGuid
)
418 result
= CharacterDatabase
.PQuery("SELECT * FROM characters WHERE guid = '%d'", guid
);
421 guid
= objmgr
.m_hiCharGuid
; // use first free if exists
424 else incHighest
= false;
427 guid
= objmgr
.m_hiCharGuid
;
429 // normalize the name if specified and check if it exists
430 if(!normalizePlayerName(name
))
433 if(ObjectMgr::IsValidName(name
,true))
435 CharacterDatabase
.escape_string(name
); // for safe, we use name only for sql quearies anyway
436 result
= CharacterDatabase
.PQuery("SELECT * FROM characters WHERE name = '%s'", name
.c_str());
439 name
= ""; // use the one from the dump
445 // name encoded or empty
447 snprintf(newguid
, 20, "%d", guid
);
448 snprintf(chraccount
, 20, "%d", account
);
449 snprintf(newpetid
, 20, "%d", objmgr
.GeneratePetNumber());
450 snprintf(lastpetid
, 20, "%s", "");
452 std::map
<uint32
,uint32
> items
;
453 std::map
<uint32
,uint32
> mails
;
454 char buf
[32000] = "";
456 typedef std::map
<uint32
, uint32
> PetIds
; // old->new petid relation
457 typedef PetIds::value_type PetIdsPair
;
460 CharacterDatabase
.BeginTransaction();
463 if(!fgets(buf
, 32000, fin
))
466 ROLLBACK(DUMP_FILE_BROKEN
);
469 std::string line
; line
.assign(buf
);
471 // skip empty strings
472 size_t nw_pos
= line
.find_first_not_of(" \t\n\r\7");
473 if(nw_pos
==std::string::npos
)
477 if(line
.substr(nw_pos
,15)=="IMPORTANT NOTE:")
480 // add required_ check
481 if(line
.substr(nw_pos
,41)=="UPDATE character_db_version SET required_")
483 if(!CharacterDatabase
.Execute(line
.c_str()))
484 ROLLBACK(DUMP_FILE_BROKEN
);
489 // determine table name and load type
490 std::string tn
= gettablename(line
);
493 sLog
.outError("LoadPlayerDump: Can't extract table name from line: '%s'!", line
.c_str());
494 ROLLBACK(DUMP_FILE_BROKEN
);
499 for(i
= 0; i
< DUMP_TABLE_COUNT
; ++i
)
501 if (tn
== dumpTables
[i
].name
)
503 type
= dumpTables
[i
].type
;
508 if (i
== DUMP_TABLE_COUNT
)
510 sLog
.outError("LoadPlayerDump: Unknown table: '%s'!", tn
.c_str());
511 ROLLBACK(DUMP_FILE_BROKEN
);
514 // change the data to server values
518 if(!changenth(line
, 1, newguid
))
519 ROLLBACK(DUMP_FILE_BROKEN
);
522 case DTT_CHARACTER
: // character t.
524 if(!changenth(line
, 1, newguid
))
525 ROLLBACK(DUMP_FILE_BROKEN
);
527 // guid, data field:guid, items
528 if(!changenth(line
, 2, chraccount
))
529 ROLLBACK(DUMP_FILE_BROKEN
);
530 std::string vals
= getnth(line
, 3);
531 if(!changetoknth(vals
, OBJECT_FIELD_GUID
+1, newguid
))
532 ROLLBACK(DUMP_FILE_BROKEN
);
533 for(uint16 field
= PLAYER_FIELD_INV_SLOT_HEAD
; field
< PLAYER_FARSIGHT
; field
++)
534 if(!changetokGuid(vals
, field
+1, items
, objmgr
.m_hiItemGuid
, true))
535 ROLLBACK(DUMP_FILE_BROKEN
);
536 if(!changenth(line
, 3, vals
.c_str()))
537 ROLLBACK(DUMP_FILE_BROKEN
);
540 // check if the original name already exists
541 name
= getnth(line
, 4);
542 CharacterDatabase
.escape_string(name
);
544 result
= CharacterDatabase
.PQuery("SELECT * FROM characters WHERE name = '%s'", name
.c_str());
548 // rename on login: `at_login` field 30 in raw field list
549 if(!changenth(line
, 30, "1"))
550 ROLLBACK(DUMP_FILE_BROKEN
);
553 else if(!changenth(line
, 4, name
.c_str()))
554 ROLLBACK(DUMP_FILE_BROKEN
);
558 case DTT_INVENTORY
: // character_inventory t.
560 if(!changenth(line
, 1, newguid
))
561 ROLLBACK(DUMP_FILE_BROKEN
);
564 if(!changeGuid(line
, 2, items
, objmgr
.m_hiItemGuid
, true))
565 ROLLBACK(DUMP_FILE_BROKEN
);
566 if(!changeGuid(line
, 4, items
, objmgr
.m_hiItemGuid
))
567 ROLLBACK(DUMP_FILE_BROKEN
);
570 case DTT_ITEM
: // item_instance t.
572 // item, owner, data field:item, owner guid
573 if(!changeGuid(line
, 1, items
, objmgr
.m_hiItemGuid
))
574 ROLLBACK(DUMP_FILE_BROKEN
);
575 if(!changenth(line
, 2, newguid
))
576 ROLLBACK(DUMP_FILE_BROKEN
);
577 std::string vals
= getnth(line
,3);
578 if(!changetokGuid(vals
, OBJECT_FIELD_GUID
+1, items
, objmgr
.m_hiItemGuid
))
579 ROLLBACK(DUMP_FILE_BROKEN
);
580 if(!changetoknth(vals
, ITEM_FIELD_OWNER
+1, newguid
))
581 ROLLBACK(DUMP_FILE_BROKEN
);
582 if(!changenth(line
, 3, vals
.c_str()))
583 ROLLBACK(DUMP_FILE_BROKEN
);
586 case DTT_ITEM_GIFT
: // character_gift
589 if(!changenth(line
, 1, newguid
))
590 ROLLBACK(DUMP_FILE_BROKEN
);
591 if(!changeGuid(line
, 2, items
, objmgr
.m_hiItemGuid
))
592 ROLLBACK(DUMP_FILE_BROKEN
);
595 case DTT_PET
: // character_pet t
597 //store a map of old pet id to new inserted pet id for use by type 5 tables
598 snprintf(currpetid
, 20, "%s", getnth(line
, 1).c_str());
599 if(strlen(lastpetid
)==0) snprintf(lastpetid
, 20, "%s", currpetid
);
600 if(strcmp(lastpetid
,currpetid
)!=0)
602 snprintf(newpetid
, 20, "%d", objmgr
.GeneratePetNumber());
603 snprintf(lastpetid
, 20, "%s", currpetid
);
606 std::map
<uint32
, uint32
> :: const_iterator petids_iter
= petids
.find(atoi(currpetid
));
608 if(petids_iter
== petids
.end())
610 petids
.insert(PetIdsPair(atoi(currpetid
), atoi(newpetid
)));
613 // item, entry, owner, ...
614 if(!changenth(line
, 1, newpetid
))
615 ROLLBACK(DUMP_FILE_BROKEN
);
616 if(!changenth(line
, 3, newguid
))
617 ROLLBACK(DUMP_FILE_BROKEN
);
621 case DTT_PET_TABLE
: // pet_aura, pet_spell, pet_spell_cooldown t
623 snprintf(currpetid
, 20, "%s", getnth(line
, 1).c_str());
625 // lookup currpetid and match to new inserted pet id
626 std::map
<uint32
, uint32
> :: const_iterator petids_iter
= petids
.find(atoi(currpetid
));
627 if(petids_iter
== petids
.end()) // couldn't find new inserted id
628 ROLLBACK(DUMP_FILE_BROKEN
);
630 snprintf(newpetid
, 20, "%d", petids_iter
->second
);
632 if(!changenth(line
, 1, newpetid
))
633 ROLLBACK(DUMP_FILE_BROKEN
);
637 case DTT_MAIL
: // mail
639 // id,messageType,stationery,sender,receiver
640 if(!changeGuid(line
, 1, mails
, objmgr
.m_mailid
))
641 ROLLBACK(DUMP_FILE_BROKEN
);
642 if(!changenth(line
, 5, newguid
))
643 ROLLBACK(DUMP_FILE_BROKEN
);
646 case DTT_MAIL_ITEM
: // mail_items
648 // mail_id,item_guid,item_template,receiver
649 if(!changeGuid(line
, 1, mails
, objmgr
.m_mailid
))
650 ROLLBACK(DUMP_FILE_BROKEN
);
651 if(!changeGuid(line
, 2, items
, objmgr
.m_hiItemGuid
))
652 ROLLBACK(DUMP_FILE_BROKEN
);
653 if(!changenth(line
, 4, newguid
))
654 ROLLBACK(DUMP_FILE_BROKEN
);
658 sLog
.outError("Unknown dump table type: %u",type
);
662 if(!CharacterDatabase
.Execute(line
.c_str()))
663 ROLLBACK(DUMP_FILE_BROKEN
);
666 CharacterDatabase
.CommitTransaction();
668 objmgr
.m_hiItemGuid
+= items
.size();
669 objmgr
.m_mailid
+= mails
.size();
672 ++objmgr
.m_hiCharGuid
;