Legends-of-Azeroth-Pandaria-5.4.8
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
24DBCFile::DBCFile(HANDLE mpq, const char* filename) :
25_mpq(mpq), _filename(filename), _file(NULL), _data(NULL), _stringTable(NULL)
26{
27}
28
29bool DBCFile::open()
30{
31if (!SFileOpenFileEx(_mpq, _filename, SFILE_OPEN_PATCHED_FILE, &_file))
32return false;
33
34char header[4];
35unsigned int na, nb, es, ss;
36
37DWORD readBytes = 0;
38SFileReadFile(_file, header, 4, &readBytes, NULL);
39if (readBytes != 4) // Number of records
40return false;
41
42if (header[0] != 'W' || header[1] != 'D' || header[2] != 'B' || header[3] != 'C')
43return false;
44
45readBytes = 0;
46SFileReadFile(_file, &na, 4, &readBytes, NULL);
47if (readBytes != 4) // Number of records
48return false;
49
50readBytes = 0;
51SFileReadFile(_file, &nb, 4, &readBytes, NULL);
52if (readBytes != 4) // Number of fields
53return false;
54
55readBytes = 0;
56SFileReadFile(_file, &es, 4, &readBytes, NULL);
57if (readBytes != 4) // Size of a record
58return false;
59
60readBytes = 0;
61SFileReadFile(_file, &ss, 4, &readBytes, NULL);
62if (readBytes != 4) // String size
63return false;
64
65_recordSize = es;
66_recordCount = na;
67_fieldCount = nb;
68_stringSize = ss;
69if (_fieldCount * 4 != _recordSize)
70return false;
71
72_data = new unsigned char[_recordSize * _recordCount + _stringSize];
73_stringTable = _data + _recordSize*_recordCount;
74
75size_t data_size = _recordSize * _recordCount + _stringSize;
76readBytes = 0;
77SFileReadFile(_file, _data, data_size, &readBytes, NULL);
78if (readBytes != data_size)
79return false;
80
81return true;
82}
83
84DBCFile::~DBCFile()
85{
86delete [] _data;
87if (_file != NULL)
88SFileCloseFile(_file);
89}
90
91DBCFile::Record DBCFile::getRecord(size_t id)
92{
93assert(_data);
94return Record(*this, _data + id*_recordSize);
95}
96
97size_t DBCFile::getMaxId()
98{
99assert(_data);
100
101size_t maxId = 0;
102for(size_t i = 0; i < getRecordCount(); ++i)
103if (maxId < getRecord(i).getUInt(0))
104maxId = getRecord(i).getUInt(0);
105
106return maxId;
107}
108
109DBCFile::Iterator DBCFile::begin()
110{
111assert(_data);
112return Iterator(*this, _data);
113}
114
115DBCFile::Iterator DBCFile::end()
116{
117assert(_data);
118return Iterator(*this, _stringTable);
119}
120
121