Project SkyFire Core
SkyFire 5.4.8 server core API documentation
Loading...
Searching...
No Matches
dbcfile.cpp
Go to the documentation of this file.
1/*
2* This file is part of Project SkyFire https://www.projectskyfire.org.
3* See LICENSE.md file for Copyright information
4*/
5
6#define _CRT_SECURE_NO_DEPRECATE
7
8#include "dbcfile.h"
9
10DBCFile::DBCFile(HANDLE mpq, const char* filename) :
11 _mpq(mpq), _filename(filename), _file(NULL), _recordSize(0), _recordCount(0),
12 _fieldCount(0), _stringSize(0), _data(NULL), _stringTable(NULL)
13{
14}
15
16bool DBCFile::open()
17{
18 if (!SFileOpenFileEx(_mpq, _filename, SFILE_OPEN_FROM_MPQ, &_file))
19 return false;
20
21 char header[4];
22 unsigned int na, nb, es, ss;
23
24 DWORD readBytes = 0;
25 SFileReadFile(_file, header, 4, &readBytes, NULL);
26 if (readBytes != 4) // Number of records
27 return false;
28
29 if (header[0] != 'W' || header[1] != 'D' || header[2] != 'B' || header[3] != 'C')
30 return false;
31
32 readBytes = 0;
33 SFileReadFile(_file, &na, 4, &readBytes, NULL);
34 if (readBytes != 4) // Number of records
35 return false;
36
37 readBytes = 0;
38 SFileReadFile(_file, &nb, 4, &readBytes, NULL);
39 if (readBytes != 4) // Number of fields
40 return false;
41
42 readBytes = 0;
43 SFileReadFile(_file, &es, 4, &readBytes, NULL);
44 if (readBytes != 4) // Size of a record
45 return false;
46
47 readBytes = 0;
48 SFileReadFile(_file, &ss, 4, &readBytes, NULL);
49 if (readBytes != 4) // String size
50 return false;
51
52 _recordSize = es;
53 _recordCount = na;
54 _fieldCount = nb;
55 _stringSize = ss;
56 if (_fieldCount * 4 != _recordSize)
57 return false;
58
59 _data = new unsigned char[_recordSize * _recordCount + _stringSize];
61
62 size_t data_size = _recordSize * _recordCount + _stringSize;
63 readBytes = 0;
64 SFileReadFile(_file, _data, data_size, &readBytes, NULL);
65 if (readBytes != data_size)
66 return false;
67
68 return true;
69}
70
72{
73 delete [] _data;
74 if (_file != NULL)
75 SFileCloseFile(_file);
76}
77
79{
80 assert(_data);
81 return Record(*this, _data + id*_recordSize);
82}
83
84size_t DBCFile::getMaxId()
85{
86 assert(_data);
87
88 size_t maxId = 0;
89 for(size_t i = 0; i < getRecordCount(); ++i)
90 if (maxId < getRecord(i).getUInt(0))
91 maxId = getRecord(i).getUInt(0);
92
93 return maxId;
94}
95
97{
98 assert(_data);
99 return Iterator(*this, _data);
100}
101
103{
104 assert(_data);
105 return Iterator(*this, _stringTable);
106}
107
unsigned int getUInt(size_t field) const
Definition dbcfile.h:49
size_t _stringSize
Definition dbcfile.h:130
HANDLE _mpq
Definition dbcfile.h:136
Record getRecord(size_t id)
Definition dbcfile.cpp:68
~DBCFile()
Definition dbcfile.cpp:63
unsigned char * _stringTable
Definition dbcfile.h:132
unsigned char * _data
Definition dbcfile.h:131
Iterator end()
Get begin iterator over records.
Definition dbcfile.cpp:92
size_t _recordSize
Definition dbcfile.h:127
size_t getMaxId()
Definition dbcfile.cpp:74
Iterator begin()
Get begin iterator over records.
Definition dbcfile.cpp:86
size_t _fieldCount
Definition dbcfile.h:129
size_t _recordCount
Definition dbcfile.h:128
virtual bool open()
Definition dbcfile.cpp:16
HANDLE _file
Definition dbcfile.h:126
size_t getRecordCount() const
Trivial.
Definition dbcfile.h:121
const char * _filename
Definition dbcfile.h:137
DBCFile(HANDLE file)
Definition dbcfile.cpp:10