root/trunk/src/game/PlayerDump.cpp @ 26

Revision 2, 19.6 kB (checked in by yumileroy, 17 years ago)

[svn] * Proper SVN structure

Original author: Neo2003
Date: 2008-10-02 16:23:55-05:00

Line 
1/*
2 * Copyright (C) 2005-2008 MaNGOS <http://www.mangosproject.org/>
3 *
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.
8 *
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.
13 *
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
17 */
18
19#include "Common.h"
20#include "PlayerDump.h"
21#include "Database/DatabaseEnv.h"
22#include "Database/SQLStorage.h"
23#include "UpdateFields.h"
24#include "ObjectMgr.h"
25
26// Character Dump tables
27#define DUMP_TABLE_COUNT 20
28
29struct DumpTable
30{
31    char const* name;
32    DumpTableType type;
33};
34
35static DumpTable dumpTables[DUMP_TABLE_COUNT] =
36{
37    { "characters",               DTT_CHARACTER  },
38    { "character_queststatus",    DTT_CHAR_TABLE },
39    { "character_reputation",     DTT_CHAR_TABLE },
40    { "character_spell",          DTT_CHAR_TABLE },
41    { "character_spell_cooldown", DTT_CHAR_TABLE },
42    { "character_action",         DTT_CHAR_TABLE },
43    { "character_aura",           DTT_CHAR_TABLE },
44    { "character_homebind",       DTT_CHAR_TABLE },
45    { "character_ticket",         DTT_CHAR_TABLE },
46    { "character_inventory",      DTT_INVENTORY  },
47    { "mail",                     DTT_MAIL       },
48    { "mail_items",               DTT_MAIL_ITEM  },
49    { "item_instance",            DTT_ITEM       },
50    { "character_gifts",          DTT_ITEM_GIFT  },
51    { "item_text",                DTT_ITEM_TEXT  },
52    { "character_pet",            DTT_PET        },
53    { "pet_aura",                 DTT_PET_TABLE  },
54    { "pet_spell",                DTT_PET_TABLE  },
55    { "pet_spell_cooldown",       DTT_PET_TABLE  },
56};
57
58// Low level functions
59static bool findtoknth(std::string &str, int n, std::string::size_type &s, std::string::size_type &e)
60{
61    int i; s = e = 0;
62    std::string::size_type size = str.size();
63    for(i = 1; s < size && i < n; s++) if(str[s] == ' ') ++i;
64    if (i < n)
65        return false;
66
67    e = str.find(' ', s);
68
69    return e != std::string::npos;
70}
71
72std::string gettoknth(std::string &str, int n)
73{
74    std::string::size_type s = 0, e = 0;
75    if(!findtoknth(str, n, s, e))
76        return "";
77
78    return str.substr(s, e-s);
79}
80
81bool findnth(std::string &str, int n, std::string::size_type &s, std::string::size_type &e)
82{
83    s = str.find("VALUES ('")+9;
84    if (s == std::string::npos) return false;
85
86    do
87    {
88        e = str.find("'",s);
89        if (e == std::string::npos) return false;
90    } while(str[e-1] == '\\');
91
92    for(int i = 1; i < n; i++)
93    {
94        do
95        {
96            s = e+4;
97            e = str.find("'",s);
98            if (e == std::string::npos) return false;
99        } while (str[e-1] == '\\');
100    }
101    return true;
102}
103
104std::string gettablename(std::string &str)
105{
106    std::string::size_type s = 13;
107    std::string::size_type e = str.find(_TABLE_SIM_, s);
108    if (e == std::string::npos)
109        return "";
110
111    return str.substr(s, e-s);
112}
113
114bool changenth(std::string &str, int n, const char *with, bool insert = false, bool nonzero = false)
115{
116    std::string::size_type s, e;
117    if(!findnth(str,n,s,e))
118        return false;
119
120    if(nonzero && str.substr(s,e-s) == "0")
121        return true;                                        // not an error
122    if(!insert)
123        str.replace(s,e-s, with);
124    else
125        str.insert(s, with);
126
127    return true;
128}
129
130std::string getnth(std::string &str, int n)
131{
132    std::string::size_type s, e;
133    if(!findnth(str,n,s,e))
134        return "";
135
136    return str.substr(s, e-s);
137}
138
139bool changetoknth(std::string &str, int n, const char *with, bool insert = false, bool nonzero = false)
140{
141    std::string::size_type s = 0, e = 0;
142    if(!findtoknth(str, n, s, e))
143        return false;
144    if(nonzero && str.substr(s,e-s) == "0")
145        return true;                                        // not an error
146    if(!insert)
147        str.replace(s, e-s, with);
148    else
149        str.insert(s, with);
150
151    return true;
152}
153
154uint32 registerNewGuid(uint32 oldGuid, std::map<uint32, uint32> &guidMap, uint32 hiGuid)
155{
156    std::map<uint32, uint32>::iterator itr = guidMap.find(oldGuid);
157    if(itr != guidMap.end())
158        return itr->second;
159
160    uint32 newguid = hiGuid + guidMap.size();
161    guidMap[oldGuid] = newguid;
162    return newguid;
163}
164
165bool changeGuid(std::string &str, int n, std::map<uint32, uint32> &guidMap, uint32 hiGuid, bool nonzero = false)
166{
167    char chritem[20];
168    uint32 oldGuid = atoi(getnth(str, n).c_str());
169    if (nonzero && oldGuid == 0)
170        return true;                                        // not an error
171
172    uint32 newGuid = registerNewGuid(oldGuid, guidMap, hiGuid);
173    snprintf(chritem, 20, "%d", newGuid);
174
175    return changenth(str, n, chritem, false, nonzero);
176}
177
178bool changetokGuid(std::string &str, int n, std::map<uint32, uint32> &guidMap, uint32 hiGuid, bool nonzero = false)
179{
180    char chritem[20];
181    uint32 oldGuid = atoi(gettoknth(str, n).c_str());
182    if (nonzero && oldGuid == 0)
183        return true;                                        // not an error
184
185    uint32 newGuid = registerNewGuid(oldGuid, guidMap, hiGuid);
186    snprintf(chritem, 20, "%d", newGuid);
187
188    return changetoknth(str, n, chritem, false, nonzero);
189}
190
191std::string CreateDumpString(char const* tableName, QueryResult *result)
192{
193    if(!tableName || !result) return "";
194    std::ostringstream ss;
195    ss << "INSERT INTO "<< _TABLE_SIM_ << tableName << _TABLE_SIM_ << " VALUES (";
196    Field *fields = result->Fetch();
197    for(uint32 i = 0; i < result->GetFieldCount(); i++)
198    {
199        if (i == 0) ss << "'";
200        else ss << ", '";
201
202        std::string s = fields[i].GetCppString();
203        CharacterDatabase.escape_string(s);
204        ss << s;
205
206        ss << "'";
207    }
208    ss << ");";
209    return ss.str();
210}
211
212std::string PlayerDumpWriter::GenerateWhereStr(char const* field, uint32 guid)
213{
214    std::ostringstream wherestr;
215    wherestr << field << " = '" << guid << "'";
216    return wherestr.str();
217}
218
219std::string PlayerDumpWriter::GenerateWhereStr(char const* field, GUIDs const& guids, GUIDs::const_iterator& itr)
220{
221    std::ostringstream wherestr;
222    wherestr << field << " IN ('";
223    for(; itr != guids.end(); ++itr)
224    {
225        wherestr << *itr;
226
227        if(wherestr.str().size() > MAX_QUERY_LEN - 50)      // near to max query
228        {
229            ++itr;
230            break;
231        }
232
233        GUIDs::const_iterator itr2 = itr;
234        if(++itr2 != guids.end())
235            wherestr << "','";
236    }
237    wherestr << "')";
238    return wherestr.str();
239}
240
241void StoreGUID(QueryResult *result,uint32 field,std::set<uint32>& guids)
242{
243    Field* fields = result->Fetch();
244    uint32 guid = fields[field].GetUInt32();
245    if(guid)
246        guids.insert(guid);
247}
248
249void StoreGUID(QueryResult *result,uint32 data,uint32 field, std::set<uint32>& guids)
250{
251    Field* fields = result->Fetch();
252    std::string dataStr = fields[data].GetCppString();
253    uint32 guid = atoi(gettoknth(dataStr, field).c_str());
254    if(guid)
255        guids.insert(guid);
256}
257
258// Writing - High-level functions
259bool PlayerDumpWriter::DumpTable(std::string& dump, uint32 guid, char const*tableFrom, char const*tableTo, DumpTableType type)
260{
261    if (!tableFrom || !tableTo)
262        return false;
263
264    GUIDs const* guids = NULL;
265    char const* fieldname = NULL;
266
267    switch ( type )
268    {
269        case DTT_ITEM:      fieldname = "guid";      guids = &items; break;
270        case DTT_ITEM_GIFT: fieldname = "item_guid"; guids = &items; break;
271        case DTT_PET:       fieldname = "owner";                     break;
272        case DTT_PET_TABLE: fieldname = "guid";      guids = &pets;  break;
273        case DTT_MAIL:      fieldname = "receiver";                  break;
274        case DTT_MAIL_ITEM: fieldname = "mail_id";   guids = &mails; break;
275        case DTT_ITEM_TEXT: fieldname = "id";        guids = &texts; break;
276        default:            fieldname = "guid";                      break;
277    }
278
279    // for guid set stop if set is empty
280    if(guids && guids->empty())
281        return true;                                        // nothing to do
282
283    // setup for guids case start position
284    GUIDs::const_iterator guids_itr;
285    if(guids)
286        guids_itr = guids->begin();
287
288    do
289    {
290        std::string wherestr;
291
292        if(guids)                                           // set case, get next guids string
293            wherestr = GenerateWhereStr(fieldname,*guids,guids_itr);
294        else                                                // not set case, get single guid string
295            wherestr = GenerateWhereStr(fieldname,guid);
296
297        QueryResult *result = CharacterDatabase.PQuery("SELECT * FROM %s WHERE %s", tableFrom, wherestr.c_str());
298        if(!result)
299            return false;
300
301        do
302        {
303            // collect guids
304            switch ( type )
305            {
306            case DTT_INVENTORY:
307                StoreGUID(result,3,items); break;           // item guid collection
308            case DTT_ITEM:
309                StoreGUID(result,0,ITEM_FIELD_ITEM_TEXT_ID,texts); break;
310                // item text id collection
311            case DTT_PET:
312                StoreGUID(result,0,pets);  break;           // pet guid collection
313            case DTT_MAIL:
314                StoreGUID(result,0,mails);                  // mail id collection
315                StoreGUID(result,6,texts); break;           // item text id collection
316            case DTT_MAIL_ITEM:
317                StoreGUID(result,1,items); break;           // item guid collection
318            default:                       break;
319            }
320
321            dump += CreateDumpString(tableTo, result);
322            dump += "\n";
323        }
324        while (result->NextRow());
325
326        delete result;
327    }
328    while(guids && guids_itr != guids->end());              // not set case iterate single time, set case iterate for all guids
329
330    return true;
331}
332
333std::string PlayerDumpWriter::GetDump(uint32 guid)
334{
335    std::string dump;
336    for(int i = 0; i < DUMP_TABLE_COUNT; i++)
337        DumpTable(dump, guid, dumpTables[i].name, dumpTables[i].name, dumpTables[i].type);
338
339    // TODO: Add instance/group/gifts..
340    // TODO: Add a dump level option to skip some non-important tables
341
342    return dump;
343}
344
345bool PlayerDumpWriter::WriteDump(std::string file, uint32 guid)
346{
347    FILE *fout = fopen(file.c_str(), "w");
348    if (!fout) { sLog.outError("Failed to open file!\r\n"); return false; }
349
350    std::string dump = GetDump(guid);
351
352    fprintf(fout,"%s\n",dump.c_str());
353    fclose(fout);
354    return true;
355}
356
357// Reading - High-level functions
358#define ROLLBACK {CharacterDatabase.RollbackTransaction(); fclose(fin); return false;}
359
360bool PlayerDumpReader::LoadDump(std::string file, uint32 account, std::string name, uint32 guid)
361{
362    // check character count
363    {
364        QueryResult *result = CharacterDatabase.PQuery("SELECT COUNT(guid) FROM characters WHERE account = '%d'", account);
365        uint8 charcount = 0;
366        if ( result )
367        {
368            Field *fields=result->Fetch();
369            charcount = fields[0].GetUInt8();
370            delete result;
371
372            if (charcount >= 10)
373            {
374                return false;
375            }
376        }
377    }
378    FILE *fin = fopen(file.c_str(), "r");
379    if(!fin) return false;
380
381    QueryResult * result = NULL;
382    char newguid[20], chraccount[20], newpetid[20], currpetid[20], lastpetid[20];
383
384    // make sure the same guid doesn't already exist and is safe to use
385    bool incHighest = true;
386    if(guid != 0 && guid < objmgr.m_hiCharGuid)
387    {
388        result = CharacterDatabase.PQuery("SELECT * FROM characters WHERE guid = '%d'", guid);
389        if (result)
390        {
391            guid = objmgr.m_hiCharGuid;                     // use first free if exists
392            delete result;
393        }
394        else incHighest = false;
395    }
396    else guid = objmgr.m_hiCharGuid;
397
398    // normalize the name if specified and check if it exists
399    if(!normalizePlayerName(name))
400        name = "";
401
402    if(ObjectMgr::IsValidName(name,true))
403    {
404        CharacterDatabase.escape_string(name);              // for safe, we use name only for sql quearies anyway
405        result = CharacterDatabase.PQuery("SELECT * FROM characters WHERE name = '%s'", name.c_str());
406        if (result)
407        {
408            name = "";                                      // use the one from the dump
409            delete result;
410        }
411    }
412    else name = "";
413
414    // name encoded or empty
415
416    snprintf(newguid, 20, "%d", guid);
417    snprintf(chraccount, 20, "%d", account);
418    snprintf(newpetid, 20, "%d", objmgr.GeneratePetNumber());
419    snprintf(lastpetid, 20, "%s", "");
420
421    std::map<uint32,uint32> items;
422    std::map<uint32,uint32> mails;
423    char buf[32000] = "";
424
425    typedef std::map<uint32, uint32> PetIds;                // old->new petid relation
426    typedef PetIds::value_type PetIdsPair;
427    PetIds petids;
428
429    CharacterDatabase.BeginTransaction();
430    while(!feof(fin))
431    {
432        if(!fgets(buf, 32000, fin))
433        {
434            if(feof(fin)) break;
435            sLog.outError("LoadPlayerDump: File read error!");
436            ROLLBACK;
437        }
438
439        std::string line; line.assign(buf);
440
441        // skip empty strings
442        if(line.find_first_not_of(" \t\n\r\7")==std::string::npos)
443            continue;
444
445        // determine table name and load type
446        std::string tn = gettablename(line);
447        if(tn.empty())
448        {
449            sLog.outError("LoadPlayerDump: Can't extract table name from line: '%s'!", line.c_str());
450            ROLLBACK;
451        }
452
453        DumpTableType type;
454        uint8 i;
455        for(i = 0; i < DUMP_TABLE_COUNT; i++)
456        {
457            if (tn == dumpTables[i].name)
458            {
459                type = dumpTables[i].type;
460                break;
461            }
462        }
463
464        if (i == DUMP_TABLE_COUNT)
465        {
466            sLog.outError("LoadPlayerDump: Unknown table: '%s'!", tn.c_str());
467            ROLLBACK;
468        }
469
470        // change the data to server values
471        switch(type)
472        {
473            case DTT_CHAR_TABLE:
474                if(!changenth(line, 1, newguid)) ROLLBACK;
475                break;
476
477            case DTT_CHARACTER:                             // character t.
478            {
479                if(!changenth(line, 1, newguid)) ROLLBACK;
480
481                // guid, data field:guid, items
482                if(!changenth(line, 2, chraccount)) ROLLBACK;
483                std::string vals = getnth(line, 3);
484                if(!changetoknth(vals, OBJECT_FIELD_GUID+1, newguid)) ROLLBACK;
485                for(uint16 field = PLAYER_FIELD_INV_SLOT_HEAD; field < PLAYER_FARSIGHT; field++)
486                    if(!changetokGuid(vals, field+1, items, objmgr.m_hiItemGuid, true)) ROLLBACK;
487                if(!changenth(line, 3, vals.c_str())) ROLLBACK;
488                if (name == "")
489                {
490                    // check if the original name already exists
491                    name = getnth(line, 4);
492                    CharacterDatabase.escape_string(name);
493
494                    result = CharacterDatabase.PQuery("SELECT * FROM characters WHERE name = '%s'", name.c_str());
495                    if (result)
496                    {
497                        delete result;
498                                                            // rename on login: `at_login` field 30 in raw field list
499                        if(!changenth(line, 30, "1")) ROLLBACK;
500                    }
501                }
502                else if(!changenth(line, 4, name.c_str())) ROLLBACK;
503
504                break;
505            }
506            case DTT_INVENTORY:                             // character_inventory t.
507            {
508                if(!changenth(line, 1, newguid)) ROLLBACK;
509
510                // bag, item
511                if(!changeGuid(line, 2, items, objmgr.m_hiItemGuid, true)) ROLLBACK;
512                if(!changeGuid(line, 4, items, objmgr.m_hiItemGuid)) ROLLBACK;
513                break;
514            }
515            case DTT_ITEM:                                  // item_instance t.
516            {
517                // item, owner, data field:item, owner guid
518                if(!changeGuid(line, 1, items, objmgr.m_hiItemGuid)) ROLLBACK;
519                if(!changenth(line, 2, newguid)) ROLLBACK;
520                std::string vals = getnth(line,3);
521                if(!changetokGuid(vals, OBJECT_FIELD_GUID+1, items, objmgr.m_hiItemGuid)) ROLLBACK;
522                if(!changetoknth(vals, ITEM_FIELD_OWNER+1, newguid)) ROLLBACK;
523                if(!changenth(line, 3, vals.c_str())) ROLLBACK;
524                break;
525            }
526            case DTT_ITEM_GIFT:                             // character_gift
527            {
528                // guid,item_guid,
529                if(!changenth(line, 1, newguid)) ROLLBACK;
530                if(!changeGuid(line, 2, items, objmgr.m_hiItemGuid)) ROLLBACK;
531                break;
532            }
533            case DTT_PET:                                   // character_pet t
534            {
535                //store a map of old pet id to new inserted pet id for use by type 5 tables
536                snprintf(currpetid, 20, "%s", getnth(line, 1).c_str());
537                if(strlen(lastpetid)==0) snprintf(lastpetid, 20, "%s", currpetid);
538                if(strcmp(lastpetid,currpetid)!=0)
539                {
540                    snprintf(newpetid, 20, "%d", objmgr.GeneratePetNumber());
541                    snprintf(lastpetid, 20, "%s", currpetid);
542                }
543
544                std::map<uint32, uint32> :: const_iterator petids_iter = petids.find(atoi(currpetid));
545
546                if(petids_iter == petids.end())
547                {
548                    petids.insert(PetIdsPair(atoi(currpetid), atoi(newpetid)));
549                }
550
551                // item, entry, owner, ...
552                if(!changenth(line, 1, newpetid)) ROLLBACK;
553                if(!changenth(line, 3, newguid)) ROLLBACK;
554
555                break;
556            }
557            case DTT_PET_TABLE:                             // pet_aura, pet_spell, pet_spell_cooldown t
558            {
559                snprintf(currpetid, 20, "%s", getnth(line, 1).c_str());
560
561                // lookup currpetid and match to new inserted pet id
562                std::map<uint32, uint32> :: const_iterator petids_iter = petids.find(atoi(currpetid));
563                if(petids_iter == petids.end()) ROLLBACK;   // couldn't find new inserted id
564
565                snprintf(newpetid, 20, "%d", petids_iter->second);
566
567                if(!changenth(line, 1, newpetid)) ROLLBACK;
568
569                break;
570            }
571            case DTT_MAIL:                                  // mail
572            {
573                // id,messageType,stationery,sender,receiver
574                if(!changeGuid(line, 1, mails, objmgr.m_mailid)) ROLLBACK;
575                if(!changenth(line, 5, newguid)) ROLLBACK;
576                break;
577            }
578            case DTT_MAIL_ITEM:                             // mail_items
579            {
580                // mail_id,item_guid,item_template,receiver
581                if(!changeGuid(line, 1, mails, objmgr.m_mailid)) ROLLBACK;
582                if(!changeGuid(line, 2, items, objmgr.m_hiItemGuid)) ROLLBACK;
583                if(!changenth(line, 4, newguid)) ROLLBACK;
584                break;
585            }
586            default:
587                sLog.outError("Unknown dump table type: %u",type);
588                break;
589        }
590
591        if(!CharacterDatabase.Execute(line.c_str())) ROLLBACK;
592    }
593
594    CharacterDatabase.CommitTransaction();
595
596    objmgr.m_hiItemGuid += items.size();
597    objmgr.m_mailid     += mails.size();
598
599    if(incHighest)
600        ++objmgr.m_hiCharGuid;
601
602    fclose(fin);
603
604    return true;
605}
Note: See TracBrowser for help on using the browser.