Legends-of-Azeroth-Pandaria-5.4.8

Форк
0
119 строк · 3.3 Кб
1
/*
2
 * Copyright (C) 2011-2016 Project SkyFire <http://www.projectskyfire.org/>
3
 * Copyright (C) 2008-2016 TrinityCore <http://www.trinitycore.org/>
4
 * Copyright (C) 2005-2016 MaNGOS <http://getmangos.com/>
5
 *
6
 * This program is free software; you can redistribute it and/or modify it
7
 * under the terms of the GNU General Public License as published by the
8
 * Free Software Foundation; either version 3 of the License, or (at your
9
 * option) any later version.
10
 *
11
 * This program is distributed in the hope that it will be useful, but WITHOUT
12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13
 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
14
 * more details.
15
 *
16
 * You should have received a copy of the GNU General Public License along
17
 * with this program. If not, see <http://www.gnu.org/licenses/>.
18
 */
19

20
#define _CRT_SECURE_NO_DEPRECATE
21

22
#include "dbcfile.h"
23

24
DBCFile::DBCFile(HANDLE mpq, const char* filename) :
25
    _mpq(mpq), _filename(filename), _file(NULL), _data(NULL), _stringTable(NULL)
26
{
27
}
28

29
bool DBCFile::open()
30
{
31
    if (!SFileOpenFileEx(_mpq, _filename, SFILE_OPEN_PATCHED_FILE, &_file))
32
        return false;
33

34
    char header[4];
35
    unsigned int na, nb, es, ss;
36

37
    DWORD readBytes = 0;
38
    SFileReadFile(_file, header, 4, &readBytes, NULL);
39
    if (readBytes != 4)                                         // Number of records
40
        return false;
41

42
    if (header[0] != 'W' || header[1] != 'D' || header[2] != 'B' || header[3] != 'C')
43
        return false;
44

45
    readBytes = 0;
46
    SFileReadFile(_file, &na, 4, &readBytes, NULL);
47
    if (readBytes != 4)                                         // Number of records
48
        return false;
49

50
    readBytes = 0;
51
    SFileReadFile(_file, &nb, 4, &readBytes, NULL);
52
    if (readBytes != 4)                                         // Number of fields
53
        return false;
54

55
    readBytes = 0;
56
    SFileReadFile(_file, &es, 4, &readBytes, NULL);
57
    if (readBytes != 4)                                         // Size of a record
58
        return false;
59

60
    readBytes = 0;
61
    SFileReadFile(_file, &ss, 4, &readBytes, NULL);
62
    if (readBytes != 4)                                         // String size
63
        return false;
64

65
    _recordSize = es;
66
    _recordCount = na;
67
    _fieldCount = nb;
68
    _stringSize = ss;
69
    if (_fieldCount * 4 != _recordSize)
70
        return false;
71

72
    _data = new unsigned char[_recordSize * _recordCount + _stringSize];
73
    _stringTable = _data + _recordSize*_recordCount;
74

75
    size_t data_size = _recordSize * _recordCount + _stringSize;
76
    readBytes = 0;
77
    SFileReadFile(_file, _data, data_size, &readBytes, NULL);
78
    if (readBytes != data_size)
79
        return false;
80

81
    return true;
82
}
83

84
DBCFile::~DBCFile()
85
{
86
    delete [] _data;
87
    if (_file != NULL)
88
        SFileCloseFile(_file);
89
}
90

91
DBCFile::Record DBCFile::getRecord(size_t id)
92
{
93
    assert(_data);
94
    return Record(*this, _data + id*_recordSize);
95
}
96

97
size_t DBCFile::getMaxId()
98
{
99
    assert(_data);
100

101
    size_t maxId = 0;
102
    for(size_t i = 0; i < getRecordCount(); ++i)
103
        if (maxId < getRecord(i).getUInt(0))
104
            maxId = getRecord(i).getUInt(0);
105

106
    return maxId;
107
}
108

109
DBCFile::Iterator DBCFile::begin()
110
{
111
    assert(_data);
112
    return Iterator(*this, _data);
113
}
114

115
DBCFile::Iterator DBCFile::end()
116
{
117
    assert(_data);
118
    return Iterator(*this, _stringTable);
119
}
120

121

Использование cookies

Мы используем файлы cookie в соответствии с Политикой конфиденциальности и Политикой использования cookies.

Нажимая кнопку «Принимаю», Вы даете АО «СберТех» согласие на обработку Ваших персональных данных в целях совершенствования нашего веб-сайта и Сервиса GitVerse, а также повышения удобства их использования.

Запретить использование cookies Вы можете самостоятельно в настройках Вашего браузера.